KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > spam > spamassassin > SpamAssassinPlugin


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.spam.spamassassin;
19
20 import java.io.InputStream JavaDoc;
21 import java.util.logging.Logger JavaDoc;
22
23 import org.columba.mail.folder.IMailbox;
24 import org.columba.mail.spam.ISpamPlugin;
25
26 public class SpamAssassinPlugin implements ISpamPlugin {
27
28     /** JDK 1.4+ logging framework logger, used for logging. */
29     private static final Logger JavaDoc LOG = Logger
30             .getLogger("org.columba.core.gui.htmlviewer");
31
32     public SpamAssassinPlugin() {
33         super();
34
35     }
36
37     public boolean scoreMessage(IMailbox mailbox, Object JavaDoc uid) throws Exception JavaDoc {
38         InputStream JavaDoc rawMessageSource = mailbox.getMessageSourceStream(uid);
39         IPCHelper ipcHelper = new IPCHelper();
40
41         // "-L" use local tests only
42
// String cmd = "spamassassin -L";
43
// String cmd = "spamc -c -L";
44
String JavaDoc cmd = ExternalToolsHelper.getSpamc() + " -c";
45
46         String JavaDoc result = null;
47         int exitVal = -1;
48
49         try {
50             LOG.info("creating process..");
51
52             ipcHelper.executeCommand(cmd);
53
54             LOG.info("sending to stdin..");
55
56             ipcHelper.send(rawMessageSource);
57
58             exitVal = ipcHelper.waitFor();
59
60             LOG.info("exitcode=" + exitVal);
61
62             LOG.info("retrieving output..");
63             result = ipcHelper.getOutputString();
64
65             ipcHelper.waitForThreads();
66         } catch (Exception JavaDoc ex) {
67             ex.printStackTrace();
68         }
69
70         if (result == null) {
71             return false;
72         }
73
74         if (exitVal == 1) {
75             // spam found
76
return true;
77         } else {
78             return false;
79         }
80
81     }
82
83     public void trainMessageAsSpam(IMailbox mailbox, Object JavaDoc uid)
84             throws Exception JavaDoc {
85
86         InputStream JavaDoc rawMessageSource = mailbox.getMessageSourceStream(uid);
87
88         IPCHelper ipcHelper = new IPCHelper();
89
90         LOG.info("creating process..");
91         
92         // --no-rebuild option is deprecated in recent SpamAssassin versions
93
/*
94         ipcHelper.executeCommand(ExternalToolsHelper.getSALearn()
95                 + " --no-rebuild --spam --single");
96                 */

97         
98         ipcHelper.executeCommand(ExternalToolsHelper.getSALearn()
99                 + " --no-sync --spam --single");
100         
101         
102         LOG.info("sending to stdin..");
103
104         ipcHelper.send(rawMessageSource);
105
106         int exitVal = ipcHelper.waitFor();
107
108         LOG.info("exitcode=" + exitVal);
109
110         LOG.info("retrieving output..");
111
112         String JavaDoc result = ipcHelper.getOutputString();
113
114         LOG.info("output=" + result);
115
116         ipcHelper.waitForThreads();
117
118     }
119
120     public void trainMessageAsHam(IMailbox mailbox, Object JavaDoc uid)
121             throws Exception JavaDoc {
122         InputStream JavaDoc rawMessageSource = mailbox.getMessageSourceStream(uid);
123
124         IPCHelper ipcHelper = new IPCHelper();
125
126         LOG.info("creating process..");
127         // --no-rebuild option is deprecated in recent SpamAssassin versions
128
ipcHelper.executeCommand(ExternalToolsHelper.getSALearn()
129                 + " --no-sync --ham --single");
130
131         LOG.info("sending to stdin..");
132
133         ipcHelper.send(rawMessageSource);
134
135         int exitVal = ipcHelper.waitFor();
136
137         LOG.info("exitcode=" + exitVal);
138
139         LOG.info("retrieving output..");
140
141         String JavaDoc result = ipcHelper.getOutputString();
142
143         LOG.info("output=" + result);
144
145         ipcHelper.waitForThreads();
146
147     }
148
149     public void save() {
150         // don't need this
151
}
152
153     public void load() {
154         // don't need this
155
}
156
157 }
158
Popular Tags