1 /**************************************************************** 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 ****************************************************************/ 19 20 21 package org.apache.james.transport.mailets; 22 23 import java.util.Iterator; 24 25 import org.apache.james.util.scanner.SpamAssassinInvoker; 26 import org.apache.mailet.base.GenericMailet; 27 import org.apache.mailet.Mail; 28 29 import javax.mail.MessagingException; 30 import javax.mail.internet.MimeMessage; 31 32 /** 33 * Sends the message through daemonized SpamAssassin (spamd), visit <a 34 * href="SpamAssassin.org">SpamAssassin.org</a> for info on configuration. The 35 * header X-Spam-Status is added to every message, this contains the score and 36 * the threshold score for spam (usually 5.0). If the message exceeds the 37 * threshold, the header X-Spam-Flag will be added with the value of YES. The 38 * default host for spamd is localhost and the default port is 783. <br> 39 * 40 * org.apache.james.spamassassin.status - Holds the status 41 * org.apache.james.spamassassin.flag - Holds the flag 42 * <br> 43 * Sample Configuration: <br> 44 * <br> 45 * <mailet notmatch="SenderHostIsLocal" class="SpamAssassin"> 46 * <spamdHost>localhost</spamdHost> 47 * <spamdPort>783</spamdPort> <br> 48 */ 49 public class SpamAssassin extends GenericMailet { 50 51 String spamdHost; 52 53 int spamdPort; 54 55 /** 56 * @see org.apache.mailet.GenericMailet#init() 57 */ 58 public void init() throws MessagingException { 59 spamdHost = getInitParameter("spamdHost"); 60 if (spamdHost == null || spamdHost.equals("")) { 61 spamdHost = "127.0.0.1"; 62 } 63 64 String port = getInitParameter("spamdPort"); 65 if (port == null || port.equals("")) { 66 spamdPort = 783; 67 } else { 68 69 try { 70 spamdPort = Integer.parseInt(getInitParameter("spamdPort")); 71 } catch (NumberFormatException e) { 72 throw new MessagingException( 73 "Please configure a valid port. Not valid: " 74 + spamdPort); 75 } 76 } 77 } 78 79 /** 80 * @see org.apache.mailet.GenericMailet#service(Mail) 81 */ 82 public void service(Mail mail) { 83 try { 84 MimeMessage message = mail.getMessage(); 85 86 // Invoke spamassian connection and scan the message 87 SpamAssassinInvoker sa = new SpamAssassinInvoker(spamdHost, 88 spamdPort); 89 sa.scanMail(message); 90 91 Iterator headers = sa.getHeadersAsAttribute().keySet().iterator(); 92 93 // Add headers as attribute to mail object 94 while (headers.hasNext()) { 95 String key = headers.next().toString(); 96 mail.setAttribute(key, (String) sa.getHeadersAsAttribute().get(key)); 97 } 98 99 message.saveChanges(); 100 } catch (MessagingException e) { 101 log(e.getMessage()); 102 } 103 104 } 105 106 /** 107 * @see org.apache.mailet.GenericMailet#getMailetInfo() 108 */ 109 public String getMailetInfo() { 110 return "Checks message against SpamAssassin"; 111 } 112 }