001: package org.claros.intouch.webmail.controllers;
002:
003: import java.util.ArrayList;
004: import java.util.StringTokenizer;
005:
006: import javax.mail.Flags;
007: import javax.mail.Message;
008:
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011: import org.claros.commons.auth.models.AuthProfile;
012: import org.claros.commons.mail.models.ConnectionMetaHandler;
013: import org.claros.commons.mail.models.ConnectionProfile;
014: import org.claros.commons.mail.protocols.ImapProtocolImpl;
015: import org.claros.commons.mail.protocols.ProtocolFactory;
016:
017: /**
018: * @author Umut Gokbayrak
019: */
020: public class ImapInboxControllerImpl extends InboxControllerBase
021: implements InboxController {
022: private static Log log = LogFactory
023: .getLog(ImapInboxControllerImpl.class);
024:
025: /**
026: * @param auth
027: * @param profile
028: * @param handler
029: */
030: public ImapInboxControllerImpl(AuthProfile auth,
031: ConnectionProfile profile, ConnectionMetaHandler handler) {
032: super (auth, profile, handler);
033: }
034:
035: /* (non-Javadoc)
036: * @see org.claros.groupware.webmail.controllers.InboxController#checkEmail()
037: */
038: public ConnectionMetaHandler checkEmail() throws Exception {
039: // fetch all messages from the remote pop3 server
040: ProtocolFactory factory = new ProtocolFactory(profile, auth,
041: handler);
042: ImapProtocolImpl protocol = (ImapProtocolImpl) factory
043: .getImap(null);
044:
045: if (handler == null || !handler.getStore().isConnected()) {
046: handler = protocol
047: .connect(org.claros.commons.mail.utility.Constants.CONNECTION_READ_WRITE);
048: }
049:
050: // ImapIdleNewMsgThread newMsgThread = new ImapIdleNewMsgThread(protocol.getFolder());
051: // newMsgThread.start();
052:
053: // ImapIdleModifiedMsgThread modifiedMsgThread = new ImapIdleModifiedMsgThread(protocol.getFolder());
054: // modifiedMsgThread.start();
055:
056: // for imap fetching the message headers is enough. No need to fetch the whole message.
057: // pros: performance gain. cons: spam protection is done only with the message headers.
058: ArrayList headers = protocol.fetchAllHeadersAsMessages();
059: ArrayList toBeMoved = new ArrayList();
060: if (headers != null) {
061: Message msg = null;
062: String folderId = null;
063:
064: // FolderControllerFactory fact = new FolderControllerFactory(auth, profile, handler);
065: // FolderController cont = fact.getFolderController();
066: for (int i = 0; i < headers.size(); i++) {
067: msg = (Message) headers.get(i);
068:
069: Flags.Flag flags[] = msg.getFlags().getSystemFlags();
070: boolean isSeen = false;
071: if (flags != null) {
072: Flags.Flag flag = null;
073: for (int m = 0; m < flags.length; m++) {
074: flag = flags[m];
075: if (flag.equals(Flags.Flag.SEEN)) {
076: isSeen = true;
077: }
078: }
079: }
080:
081: if (!isSeen) {
082: try {
083: // find the destionation folderId for the message
084: folderId = findDestinationFolderId(msg);
085: if (!folderId
086: .equals(org.claros.commons.mail.utility.Constants
087: .FOLDER_INBOX(profile))) {
088: toBeMoved.add(msg.getMessageNumber() + "_"
089: + folderId);
090: }
091: /*
092: // if message should be directly deleted it shouldn't be
093:
094: // stored in DB.
095: // if (folderId == null) {
096: // toBeDeleted.add(new Integer(msg.getMessageNumber()));
097: } else if (folderId.toUpperCase().equals(junkFolderName.toUpperCase())) {
098: MailControllerFactory mailFact = new MailControllerFactory(auth, profile, handler, Constants.FOLDER_INBOX(profile));
099: MailController mailCont = mailFact.getMailController();
100: mailCont.moveEmail(new Long(msg.getMessageNumber()), folderId);
101: }
102: */
103: } catch (Exception e) {
104: log.error("Error while processing mail.", e);
105: }
106: }
107: }
108: }
109:
110: /*
111: // remove the filtered messages from the server.
112: if (toBeDeleted.size() > 0) {
113: int ids[] = new int[toBeDeleted.size()];
114: for (int i=0;i<toBeDeleted.size();i++) {
115: Integer id = (Integer)toBeDeleted.get(i);
116: ids[i] = id.intValue();
117: }
118: protocol.deleteMessages(ids);
119: }
120: */
121:
122: if (toBeMoved.size() > 0) {
123: int ids[] = new int[toBeMoved.size()];
124: String folders[] = new String[toBeMoved.size()];
125: String tmp = null;
126: for (int i = 0; i < toBeMoved.size(); i++) {
127: tmp = (String) toBeMoved.get(i);
128: StringTokenizer token = new StringTokenizer(tmp, "_");
129: ids[i] = Integer.parseInt(token.nextToken());
130: folders[i] = token.nextToken();
131: }
132: protocol.moveEmails(ids, folders);
133: }
134: return handler;
135: }
136: }
|