KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > filters > Jasen > JasenSpamFilter


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.plugins.filters.Jasen;
22
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.text.DecimalFormat JavaDoc;
26 import java.text.NumberFormat JavaDoc;
27 import java.util.LinkedList JavaDoc;
28
29 import javax.mail.MessagingException JavaDoc;
30 import javax.mail.internet.MimeMessage JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.jasen.JasenScanner;
35 import org.jasen.core.engine.Jasen;
36 import org.jasen.error.JasenException;
37 import org.jasen.interfaces.JasenScanResult;
38 import org.jsmtpd.core.common.PluginInitException;
39 import org.jsmtpd.core.common.filter.FilterTreeFailureException;
40 import org.jsmtpd.core.common.filter.IFilter;
41 import org.jsmtpd.core.mail.Email;
42 import org.jsmtpd.core.send.QueueService;
43
44 /**
45  * Jasen anti spam filter
46  * <BR>This plugin is experimental !<br>
47  * 7/05/2005
48  * Changed Email type, adapted here.
49  *
50  * Note: got sometimes a null ptr excep while JasenEngin is running
51  * @author Jean-Francois POUX
52  *
53  */

54 public class JasenSpamFilter implements IFilter {
55
56     private Log log = LogFactory.getLog(JasenSpamFilter.class);
57     private boolean failOnError = false;
58     private double threshold = 0.9;
59
60     public boolean doFilter(Email input) throws FilterTreeFailureException {
61         MimeMessage JavaDoc mesg = null;
62
63         // Do something with IS of javamail (it dups the data)
64
InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(input.getDataAsByte());
65
66         log.debug("Start scaning " + input.getDiskName());
67         try {
68             mesg = new MimeMessage JavaDoc(null, is);
69         } catch (MessagingException JavaDoc e) {
70             log.error("Failed to parse MimeMessage for "+input.getDiskName());
71             return true;
72         }
73         try {
74             log.debug("MimeMessage prepared " + input.getDiskName());
75             JasenScanResult result = JasenScanner.getInstance().scan(mesg);
76             if (result.getProbability() > threshold) {
77                 log.debug("Message " + input.getDiskName() + " is a spam ");
78                 if (failOnError) // This will break the chain, so try to generate error message
79
{
80                     if (!input.getFrom().toString().equals("<>")) // is it an internal mail already ?
81
{
82                         LinkedList JavaDoc<String JavaDoc> messages = new LinkedList JavaDoc<String JavaDoc>();
83                         messages.add("");
84                         messages.add("Hello, this is the Jsmtpd mailer daemon");
85                         messages.add("");
86                         messages.add("I'm affraid I can't deliver your email to " + input.getRcptAsString());
87                         messages.add("");
88
89                         messages.add(getPluginName() + " found your email as a spam : ");
90                         messages.add("");
91                         NumberFormat JavaDoc formatter = new DecimalFormat JavaDoc("0.000");
92                         String JavaDoc lineResult;
93                         for (int i = 0; i < result.getTestResults().length; i++) {
94                             lineResult = "";
95                             lineResult += formatter.format(Double.parseDouble(result.getTestResults()[i][Jasen.RESULT_INDEX_PROBABILITY])) + "\t: ";
96                             lineResult += result.getTestResults()[i][Jasen.RESULT_INDEX_DISPLAY] + " (";
97                             lineResult += result.getTestResults()[i][Jasen.RESULT_INDEX_NAME] + ")";
98                             messages.add(lineResult);
99                         }
100                         messages.add("Overall result : " + result.getProbability());
101                         messages.add("");
102                         messages.add("This is a fatal error, giving up");
103                         messages.add("");
104                         Email error = Email.createInternalMail(input.getFrom(), "Mailer-daemon error, spam", messages, input);
105                         QueueService.getInstance().queueMail(error);
106                         log.debug("sent mail back to sender ");
107                     } else {
108                         log.debug("internal message, message not sent back ?");
109                     }
110                     throw new FilterTreeFailureException();
111                 } else {
112                     return false;
113                 }
114             }
115             log.debug("Message "+input.getDiskName()+" is clear ("+result.getProbability()+")");
116             //mesg.addHeader("X-Spam-Flag:","NO");
117
//X-Spam-Level: *********
118
} catch (JasenException e1) {
119             log.warn( "jsmtpd-jasen error in engine : " + e1.getMessage());
120         } catch (Throwable JavaDoc t) {
121             log.debug("error scanning " + input.getDiskName() + " in engine " + t);
122         }
123
124         return true;
125     }
126
127     public String JavaDoc getPluginName() {
128
129         return "Jsmtpd-Jasen anti spam filter";
130     }
131
132     public void initPlugin() throws PluginInitException {
133         log.info("Jsmtd-jasen initing");
134         try {
135             JasenScanner.getInstance().init();
136         } catch (JasenException e) {
137             throw new PluginInitException(e);
138         } catch (Throwable JavaDoc t) {
139             throw new PluginInitException(t);
140         }
141         log.info("Jsmtpd-jasen initialized");
142     }
143
144     public void shutdownPlugin() {
145         JasenScanner.getInstance().destroy();
146     }
147
148     public void setFailOnError(boolean fail) {
149         failOnError = fail;
150     }
151
152 }
Popular Tags