001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.spam.spamassassin;
019:
020: import java.io.InputStream;
021: import java.util.logging.Logger;
022:
023: import org.columba.mail.folder.IMailbox;
024: import org.columba.mail.spam.ISpamPlugin;
025:
026: public class SpamAssassinPlugin implements ISpamPlugin {
027:
028: /** JDK 1.4+ logging framework logger, used for logging. */
029: private static final Logger LOG = Logger
030: .getLogger("org.columba.core.gui.htmlviewer");
031:
032: public SpamAssassinPlugin() {
033: super ();
034:
035: }
036:
037: public boolean scoreMessage(IMailbox mailbox, Object uid)
038: throws Exception {
039: InputStream rawMessageSource = mailbox
040: .getMessageSourceStream(uid);
041: IPCHelper ipcHelper = new IPCHelper();
042:
043: // "-L" use local tests only
044: // String cmd = "spamassassin -L";
045: // String cmd = "spamc -c -L";
046: String cmd = ExternalToolsHelper.getSpamc() + " -c";
047:
048: String result = null;
049: int exitVal = -1;
050:
051: try {
052: LOG.info("creating process..");
053:
054: ipcHelper.executeCommand(cmd);
055:
056: LOG.info("sending to stdin..");
057:
058: ipcHelper.send(rawMessageSource);
059:
060: exitVal = ipcHelper.waitFor();
061:
062: LOG.info("exitcode=" + exitVal);
063:
064: LOG.info("retrieving output..");
065: result = ipcHelper.getOutputString();
066:
067: ipcHelper.waitForThreads();
068: } catch (Exception ex) {
069: ex.printStackTrace();
070: }
071:
072: if (result == null) {
073: return false;
074: }
075:
076: if (exitVal == 1) {
077: // spam found
078: return true;
079: } else {
080: return false;
081: }
082:
083: }
084:
085: public void trainMessageAsSpam(IMailbox mailbox, Object uid)
086: throws Exception {
087:
088: InputStream rawMessageSource = mailbox
089: .getMessageSourceStream(uid);
090:
091: IPCHelper ipcHelper = new IPCHelper();
092:
093: LOG.info("creating process..");
094:
095: // --no-rebuild option is deprecated in recent SpamAssassin versions
096: /*
097: ipcHelper.executeCommand(ExternalToolsHelper.getSALearn()
098: + " --no-rebuild --spam --single");
099: */
100:
101: ipcHelper.executeCommand(ExternalToolsHelper.getSALearn()
102: + " --no-sync --spam --single");
103:
104: LOG.info("sending to stdin..");
105:
106: ipcHelper.send(rawMessageSource);
107:
108: int exitVal = ipcHelper.waitFor();
109:
110: LOG.info("exitcode=" + exitVal);
111:
112: LOG.info("retrieving output..");
113:
114: String result = ipcHelper.getOutputString();
115:
116: LOG.info("output=" + result);
117:
118: ipcHelper.waitForThreads();
119:
120: }
121:
122: public void trainMessageAsHam(IMailbox mailbox, Object uid)
123: throws Exception {
124: InputStream rawMessageSource = mailbox
125: .getMessageSourceStream(uid);
126:
127: IPCHelper ipcHelper = new IPCHelper();
128:
129: LOG.info("creating process..");
130: // --no-rebuild option is deprecated in recent SpamAssassin versions
131: ipcHelper.executeCommand(ExternalToolsHelper.getSALearn()
132: + " --no-sync --ham --single");
133:
134: LOG.info("sending to stdin..");
135:
136: ipcHelper.send(rawMessageSource);
137:
138: int exitVal = ipcHelper.waitFor();
139:
140: LOG.info("exitcode=" + exitVal);
141:
142: LOG.info("retrieving output..");
143:
144: String result = ipcHelper.getOutputString();
145:
146: LOG.info("output=" + result);
147:
148: ipcHelper.waitForThreads();
149:
150: }
151:
152: public void save() {
153: // don't need this
154: }
155:
156: public void load() {
157: // don't need this
158: }
159:
160: }
|