1 21 package org.jsmtpd.plugins.filters.Jasen; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.InputStream ; 25 import java.text.DecimalFormat ; 26 import java.text.NumberFormat ; 27 import java.util.LinkedList ; 28 29 import javax.mail.MessagingException ; 30 import javax.mail.internet.MimeMessage ; 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 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 mesg = null; 62 63 InputStream is = new ByteArrayInputStream (input.getDataAsByte()); 65 66 log.debug("Start scaning " + input.getDiskName()); 67 try { 68 mesg = new MimeMessage (null, is); 69 } catch (MessagingException 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) { 80 if (!input.getFrom().toString().equals("<>")) { 82 LinkedList <String > messages = new LinkedList <String >(); 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 formatter = new DecimalFormat ("0.000"); 92 String 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 } catch (JasenException e1) { 119 log.warn( "jsmtpd-jasen error in engine : " + e1.getMessage()); 120 } catch (Throwable t) { 121 log.debug("error scanning " + input.getDiskName() + " in engine " + t); 122 } 123 124 return true; 125 } 126 127 public String 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 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 |