KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > spam > command > ScoreMessageCommand


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo
13
// Stich.
14
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15
//
16
//All Rights Reserved.
17

18 package org.columba.mail.spam.command;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23 import org.columba.api.command.ICommandReference;
24 import org.columba.api.command.IWorkerStatusController;
25 import org.columba.core.command.Command;
26 import org.columba.core.command.StatusObservableImpl;
27 import org.columba.core.logging.Logging;
28 import org.columba.mail.command.IMailFolderCommandReference;
29 import org.columba.mail.command.MailFolderCommandReference;
30 import org.columba.mail.config.AccountItem;
31 import org.columba.mail.filter.plugins.AddressbookFilter;
32 import org.columba.mail.folder.IMailbox;
33 import org.columba.mail.folder.command.MarkMessageCommand;
34 import org.columba.mail.spam.SpamController;
35
36 /**
37  * Score selected messages as spam, meaning calculate the likelyhood that the
38  * message is spam.
39  *
40  * @author fdietz
41  */

42 public class ScoreMessageCommand extends Command {
43
44     private Object JavaDoc[] uids;
45
46     private IMailbox srcFolder;
47
48     private MarkMessageCommand markAsSpamCommand;
49
50     private MarkMessageCommand markAsNotSpamCommand;
51
52     /**
53      * @param references
54      */

55     public ScoreMessageCommand(ICommandReference reference) {
56         super(reference);
57     }
58
59     public void updateGUI() throws Exception JavaDoc {
60         // update table
61
if (markAsSpamCommand != null) {
62             markAsSpamCommand.updateGUI();
63         }
64         if (markAsNotSpamCommand != null) {
65             markAsNotSpamCommand.updateGUI();
66         }
67     }
68
69     /**
70      * @see org.columba.api.command.Command#execute(org.columba.api.command.Worker)
71      */

72     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
73         // get source reference
74
IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
75
76         // get array of message UIDs
77
uids = r.getUids();
78
79         // get source folder
80
srcFolder = (IMailbox) r.getSourceFolder();
81
82         // register for status events
83
((StatusObservableImpl) srcFolder.getObservable()).setWorker(worker);
84
85         // update status message
86
// TODO (@author fdietz): i18n
87
worker.setDisplayText("Scoring messages ...");
88         worker.setProgressBarMaximum(uids.length);
89
90         ArrayList JavaDoc spamList = new ArrayList JavaDoc();
91         ArrayList JavaDoc nonspamList = new ArrayList JavaDoc();
92
93         for (int j = 0; j < uids.length; j++) {
94             if (worker.cancelled()) {
95                 return;
96             }
97
98             try {
99
100                 // score message
101
boolean result = scoreMessage(j);
102
103                 // if message is spam
104
if (result) {
105                     // mark message as spam
106
spamList.add(uids[j]);
107                 } else {
108                     // mark message as *not* spam
109
nonspamList.add(uids[j]);
110                 }
111
112                 // train message as spam or non spam
113
trainMessage(j, result);
114
115                 worker.setProgressBarValue(j);
116
117                 if (worker.cancelled()) {
118                     break;
119                 }
120             } catch (Exception JavaDoc e) {
121                 if (Logging.DEBUG) {
122                     e.printStackTrace();
123                 }
124             }
125         }
126
127         // mark spam messages
128
if (spamList.size() != 0) {
129             MailFolderCommandReference ref = new MailFolderCommandReference(
130                     srcFolder, spamList.toArray());
131             ref.setMarkVariant(MarkMessageCommand.MARK_AS_SPAM);
132             markAsSpamCommand = new MarkMessageCommand(ref);
133             markAsSpamCommand.execute(worker);
134         }
135
136         // mark non spam messages
137
if (nonspamList.size() != 0) {
138             MailFolderCommandReference ref = new MailFolderCommandReference(
139                     srcFolder, nonspamList.toArray());
140             ref.setMarkVariant(MarkMessageCommand.MARK_AS_NOTSPAM);
141             markAsNotSpamCommand = new MarkMessageCommand(ref);
142             markAsNotSpamCommand.execute(worker);
143         }
144
145     }
146
147     /**
148      * Score message, meaning decide if message is spam or non spam.
149      *
150      * @param j
151      * message UID index
152      * @return true, if spam. False, otherwise.
153      * @throws Exception
154      * @throws IOException
155      */

156     private boolean scoreMessage(int j) throws Exception JavaDoc, IOException JavaDoc {
157
158         // calculate message score
159
boolean result = SpamController.getInstance().scoreMessage(srcFolder,
160                 uids[j]);
161
162         // message belongs to which account?
163
AccountItem item = CommandHelper
164                 .retrieveAccountItem(srcFolder, uids[j]);
165
166         if (item.getSpamItem().checkAddressbook()) {
167             // check if sender is already in addressbook
168
boolean isInAddressbook = new AddressbookFilter().process(
169                     srcFolder, uids[j]);
170             result = result && !isInAddressbook;
171         }
172
173         return result;
174     }
175
176     /**
177      * Train selected message as spam or non spam.
178      *
179      * @param j
180      * UID index
181      * @param result
182      * true, if spam. False, otherwise.
183      * @throws Exception
184      */

185     private void trainMessage(int j, boolean result) throws Exception JavaDoc {
186
187         // add this message to frequency database
188
if (result) {
189             SpamController.getInstance().trainMessageAsSpam(srcFolder, uids[j]);
190         } else {
191             SpamController.getInstance().trainMessageAsHam(srcFolder, uids[j]);
192         }
193
194     }
195 }
Popular Tags