001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.pop3.command;
017:
018: import org.columba.api.command.ICommandReference;
019: import org.columba.api.command.IWorkerStatusController;
020: import org.columba.core.command.Command;
021: import org.columba.core.command.CommandProcessor;
022: import org.columba.core.command.CompoundCommand;
023: import org.columba.core.filter.Filter;
024: import org.columba.core.filter.FilterList;
025: import org.columba.core.filter.IFilter;
026: import org.columba.core.filter.IFilterList;
027: import org.columba.mail.command.IMailFolderCommandReference;
028: import org.columba.mail.command.MailFolderCommandReference;
029: import org.columba.mail.config.AccountItem;
030: import org.columba.mail.filter.FilterCompoundCommand;
031: import org.columba.mail.folder.IMailbox;
032: import org.columba.mail.folder.RootFolder;
033: import org.columba.mail.folder.command.MarkMessageCommand;
034: import org.columba.mail.folder.command.MoveMessageCommand;
035: import org.columba.mail.gui.tree.FolderTreeModel;
036: import org.columba.mail.mailchecking.MailCheckingManager;
037: import org.columba.mail.message.IColumbaMessage;
038: import org.columba.mail.spam.command.CommandHelper;
039: import org.columba.mail.spam.command.ScoreMessageCommand;
040: import org.columba.ristretto.io.SourceInputStream;
041:
042: /**
043: * After downloading the message from the POP3 server, its added to the Inbox
044: * folder.
045: * <p>
046: * The spam filter is executed on this message.
047: * <p>
048: * The Inbox filters are applied to the message.
049: *
050: * @author fdietz
051: */
052: public class AddPOP3MessageCommand extends Command {
053:
054: private IMailbox inboxFolder;
055:
056: /**
057: * @param references
058: * command arguments
059: */
060: public AddPOP3MessageCommand(ICommandReference reference) {
061: super (reference);
062: }
063:
064: /** {@inheritDoc} */
065: public void execute(IWorkerStatusController worker)
066: throws Exception {
067: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
068:
069: inboxFolder = (IMailbox) r.getSourceFolder();
070:
071: IColumbaMessage message = (IColumbaMessage) r.getMessage();
072:
073: // add message to folder
074: SourceInputStream messageStream = new SourceInputStream(message
075: .getSource());
076: Object uid = inboxFolder.addMessage(messageStream);
077: messageStream.close();
078:
079: // mark message as recent
080: r.setSourceFolder(inboxFolder);
081: r.setUids(new Object[] { uid });
082: r.setMarkVariant(MarkMessageCommand.MARK_AS_RECENT);
083: new MarkMessageCommand(r).execute(worker);
084:
085: // apply spam filter
086: boolean messageWasMoved = applySpamFilter(uid, worker);
087:
088: if (messageWasMoved == false) {
089: // apply filter on message
090: applyFilters(uid);
091: }
092: }
093:
094: /**
095: * Apply spam filter engine on message.
096: * <p>
097: * Message is marked as ham or spam.
098: *
099: * @param uid
100: * message uid.
101: * @throws Exception
102: */
103: private boolean applySpamFilter(Object uid,
104: IWorkerStatusController worker) throws Exception {
105: // message belongs to which account?
106: AccountItem item = CommandHelper.retrieveAccountItem(
107: inboxFolder, uid);
108: if (item == null)
109: return false;
110:
111: // if spam filter is not enabled -> return
112: if (!item.getSpamItem().isEnabled()) {
113: return false;
114: }
115:
116: // create reference
117: IMailFolderCommandReference r = new MailFolderCommandReference(
118: inboxFolder, new Object[] { uid });
119:
120: // score message and mark as "spam" or "not spam"
121: new ScoreMessageCommand(r).execute(worker);
122:
123: // is message marked as spam
124: boolean spam = ((Boolean) inboxFolder.getAttribute(uid,
125: "columba.spam")).booleanValue();
126: if (spam == false)
127: return false;
128:
129: if (item.getSpamItem().isMoveIncomingJunkMessagesEnabled()) {
130: if (item.getSpamItem().isIncomingTrashSelected()) {
131: // move message to trash
132: IMailbox trash = (IMailbox) ((RootFolder) inboxFolder
133: .getRootFolder()).getTrashFolder();
134:
135: // create reference
136: MailFolderCommandReference ref2 = new MailFolderCommandReference(
137: inboxFolder, trash, new Object[] { uid });
138:
139: CommandProcessor.getInstance().addOp(
140: new MoveMessageCommand(ref2));
141: } else {
142: // move message to user-configured folder (generally "Junk"
143: // folder)
144: IMailbox destFolder = (IMailbox) FolderTreeModel
145: .getInstance().getFolder(
146: item.getSpamItem()
147: .getIncomingCustomFolder());
148:
149: // create reference
150: MailFolderCommandReference ref2 = new MailFolderCommandReference(
151: inboxFolder, destFolder, new Object[] { uid });
152:
153: CommandProcessor.getInstance().addOp(
154: new MoveMessageCommand(ref2));
155:
156: }
157:
158: return true;
159: }
160:
161: return false;
162: }
163:
164: /**
165: * Apply filters on new message.
166: *
167: * @param uid
168: * message uid
169: */
170: private void applyFilters(Object uid) throws Exception {
171: IFilterList list = inboxFolder.getFilterList();
172:
173: for (int j = 0; j < list.count(); j++) {
174: IFilter filter = list.get(j);
175:
176: Object[] result = inboxFolder.searchMessages(filter,
177: new Object[] { uid });
178:
179: if (result.length != 0) {
180: CompoundCommand command = new FilterCompoundCommand(
181: filter, inboxFolder, result);
182:
183: CommandProcessor.getInstance().addOp(command);
184: }
185: }
186: }
187:
188: }
|