jadex.platform.service.email.SubscriptionInfo Maven / Gradle / Ivy
package jadex.platform.service.email;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.mail.search.FlagTerm;
import jadex.bridge.service.types.email.Email;
import jadex.bridge.service.types.email.EmailAccount;
import jadex.commons.IFilter;
import jadex.commons.SUtil;
/**
* Info struct for email subscriptions.
*/
public class SubscriptionInfo
{
//-------- attributes --------
/** The filter. */
protected IFilter filter;
/** The account. */
protected EmailAccount account;
/** Flag if full email conversion is necessary for filtering. */
protected boolean fullconv;
/** The number of messages in the folder. */
protected int total;
protected int lastseenno;
protected Email lastseenmsg;
//-------- constructors --------
/**
* Create a new subscription info.
*/
public SubscriptionInfo(IFilter filter, EmailAccount account, boolean fullconv)
{
this.total = 0;
this.filter = filter;
this.account = account;
this.fullconv = fullconv;
}
//-------- methods --------
/**
* Get the filter.
* @return The filter.
*/
public IFilter getFilter()
{
return filter;
}
/**
* Set the filter.
* @param filter The filter to set.
*/
public void setFilter(IFilter filter)
{
this.filter = filter;
}
/**
* Get the account.
* @return The account.
*/
public EmailAccount getAccount()
{
return account;
}
/**
* Set the account.
* @param account The account to set.
*/
public void setAccount(EmailAccount account)
{
this.account = account;
}
/**
* Get the total.
* return The total.
*/
public int getTotal()
{
return total;
}
/**
* Set the total.
* @param total The total to set.
*/
public void setTotal(int total)
{
this.total = total;
}
/**
* Get the lastseenno.
* return The lastseenno.
*/
public int getLastSeenNo()
{
return lastseenno;
}
/**
* Set the lastseenno.
* @param lastseenno The lastseenno to set.
*/
public void setLastSeenNo(int lastseenno)
{
this.lastseenno = lastseenno;
}
/**
* Get the lastseenmsg.
* return The lastseenmsg.
*/
public Email getLastSeenMsg()
{
return lastseenmsg;
}
/**
* Set the lastseenmsg.
* @param lastseenmsg The lastseenmsg to set.
*/
public void setLastSeenMsg(Email lastseenmsg)
{
this.lastseenmsg = lastseenmsg;
}
/**
* Fetch new emails from the inbox.
*/
public List getNewEmails()
{
List ret = null;
Store store = null;
try
{
Properties props = account.getProperties();
Session session = Session.getDefaultInstance(props, null);
store = session.getStore(account.getReceiveProtocol());
// Warning this is a blocking call that may block the agent several seconds
// Should be done on worker component / thread
store.connect(account.getReceiveHost(), account.getUser(), account.getPassword());
Folder f = store.getFolder("inbox");
f.open(Folder.READ_ONLY);
int newtotal = f.getMessageCount();
// Rescan messages
if(lastseenno>0 && total>0)
{
// if less messages than last time
if(lastseenno>newtotal)
{
total = 0;
}
else
{
// if the position of the last seen message has changed
Message lsm = f.getMessage(lastseenno);
if(lsm!=null && !convertMessage(lsm).equals(lastseenmsg))
{
total = 0;
}
}
}
if(total0)
{
Email email = null;
ret = new ArrayList();
for(int i=0; i texts = new ArrayList();
List