You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2006/07/08 14:11:29 UTC

svn commit: r420100 - /james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java

Author: norman
Date: Sat Jul  8 05:11:29 2006
New Revision: 420100

URL: http://svn.apache.org/viewvc?rev=420100&view=rev
Log:
Add spamassassin message handler. See JAMES-557

Added:
    james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java

Added: james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java?rev=420100&view=auto
==============================================================================
--- james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java (added)
+++ james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java Sat Jul  8 05:11:29 2006
@@ -0,0 +1,155 @@
+/***********************************************************************
+ * Copyright (c) 2006 The Apache Software Foundation.                  *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you *
+ * may not use this file except in compliance with the License. You    *
+ * may obtain a copy of the License at:                                *
+ *                                                                     *
+ *     http://www.apache.org/licenses/LICENSE-2.0                      *
+ *                                                                     *
+ * Unless required by applicable law or agreed to in writing, software *
+ * distributed under the License is distributed on an "AS IS" BASIS,   *
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
+ * implied.  See the License for the specific language governing       *
+ * permissions and limitations under the License.                      *
+ ***********************************************************************/
+package org.apache.james.smtpserver;
+
+import java.util.Iterator;
+
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.james.util.SpamAssassinInvoker;
+import org.apache.james.util.mail.dsn.DSNStatus;
+
+public class SpamAssassinHandler extends AbstractLogEnabled implements
+        MessageHandler, Configurable {
+
+    private int spamdPort = 783;
+
+    private String spamdHost = "localhost";
+
+    private double spamdRejectionHits = 0.0;
+
+    /**
+     * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
+     */
+    public void configure(Configuration arg0) throws ConfigurationException {
+        Configuration spamdHostConf = arg0.getChild("spamdHost", false);
+        if (spamdHostConf != null) {
+            setSpamdHost(spamdHostConf.getValue("localhost"));
+        }
+
+        Configuration spamdPortConf = arg0.getChild("spamdPort", false);
+        if (spamdPortConf != null) {
+            setSpamdPort(spamdPortConf.getValueAsInteger(783));
+        }
+
+        Configuration spamdRejectionHitsConf = arg0.getChild(
+                "spamdRejectionHits", false);
+        if (spamdRejectionHitsConf != null) {
+            setSpamdRejectionHits(spamdRejectionHitsConf.getValueAsDouble(0.0));
+        }
+
+    }
+
+    /**
+     * Set the host the spamd daemon is running at
+     * 
+     * @param spamdHost
+     *            The spamdHost
+     */
+    public void setSpamdHost(String spamdHost) {
+        this.spamdHost = spamdHost;
+    }
+
+    /**
+     * Set the port the spamd damon is listen on
+     * 
+     * @param spamdPort
+     *            the spamdPort
+     */
+    public void setSpamdPort(int spamdPort) {
+        this.spamdPort = spamdPort;
+    }
+
+    /**
+     * Set the hits on which the message will be rejected.
+     * 
+     * @param spamdRejectionHits
+     *            The hits
+     */
+    public void setSpamdRejectionHits(double spamdRejectionHits) {
+        this.spamdRejectionHits = spamdRejectionHits;
+
+    }
+
+    /**
+     * @see org.apache.james.smtpserver.MessageHandler#onMessage(SMTPSession)
+     */
+    public void onMessage(SMTPSession session) {
+
+        // Not scan the message if relaying allowed
+        if (session.isRelayingAllowed()) {
+            return;
+        }
+
+        try {
+            MimeMessage message = session.getMail().getMessage();
+            SpamAssassinInvoker sa = new SpamAssassinInvoker(spamdHost,
+                    spamdPort);
+            sa.scanMail(message);
+
+            Iterator headers = sa.getHeaders().keySet().iterator();
+
+            // Add the headers
+            while (headers.hasNext()) {
+                String key = headers.next().toString();
+
+                message.setHeader(key, (String) sa.getHeaders().get(key));
+            }
+
+            // Check if rejectionHits was configured
+            if (spamdRejectionHits > 0) {
+                try {
+                    double hits = Double.parseDouble(sa.getHits());
+
+                    // if the hits are bigger the rejectionHits reject the
+                    // message
+                    if (spamdRejectionHits <= hits) {
+                        String responseString = "554 "
+                                + DSNStatus.getStatus(DSNStatus.PERMANENT,
+                                        DSNStatus.SECURITY_OTHER)
+                                + " This message smells like SPAM. Message rejected";
+                        StringBuffer buffer = new StringBuffer(256).append(
+                                "Rejected message from ").append(
+                                session.getState().get(SMTPSession.SENDER)
+                                        .toString()).append(" from host ")
+                                .append(session.getRemoteHost()).append(" (")
+                                .append(session.getRemoteIPAddress()).append(
+                                        ") " + responseString).append(
+                                        ". Required rejection hits: "
+                                                + spamdRejectionHits
+                                                + " hits: " + hits);
+                        getLogger().info(buffer.toString());
+                        session.writeResponse(responseString);
+
+                        // Message reject .. abort it!
+                        session.abortMessage();
+                    }
+                } catch (NumberFormatException e) {
+                    // hits unknown
+                }
+            }
+        } catch (MessagingException e) {
+            getLogger().error(e.getMessage());
+        }
+
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Re: svn commit: r420100 - /james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java

Posted by Stefano Bagnara <ap...@bago.org>.
Noel J. Bergman wrote:
>> + Iterator headers = sa.getHeaders().keySet().iterator();
>> + // Add the headers
>> + while (headers.hasNext()) {
>> +   String key = headers.next().toString();
>> +   message.setHeader(key, (String) sa.getHeaders().get(key));
>> + }
> 
> Do we want to add the header's now, or set attributes that we can map to headers later?  Personally, I'm leaning towards the latter.  We might even have a generic mailet along the lines:
> 
>   <mailet match="All" class="Attributes2Headers">
>     <attribute name="org.apache.james.SpamAssassin-Status>
>       <header>X-Spam-Status</header>
>       <mapping> <!-- optional (see AbstractVirtualUser.regexMap for idea) -->
>         <regex>...</regex> <!-- regex to match against attribute value -->
>         <value>...</value> <!-- parameterized string to build header   -->
>       </mapping>
>     </attribute>
>     <attribute name="...">
>       ...
>     </attribute>
>   </mailet
> 
> This would give us more control over header names and content (if there were no mapping element, the value would be copied directly).  For example, the ASF version of the header looks like:
> 
>   X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME
> 
> And it would also be more efficient, since we could modify the message just once, rather than each time we want to add meta-data.

I agree on this.
We should not touch the MimeMessage if possible, and only store mail 
attributes that give us much more flexibility.
About the Attributes2Headers I would name it MailAttributesToMimeHeaders 
and I would probably skip the regex part for the first version.

Stefano


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Re: svn commit: r420100 - /james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java

Posted by Norman Maurer <nm...@byteaction.de>.
Am Samstag, den 08.07.2006, 16:51 +0200 schrieb Stefano Bagnara:
> Noel J. Bergman wrote:
> >> + Iterator headers = sa.getHeaders().keySet().iterator();
> >> + // Add the headers
> >> + while (headers.hasNext()) {
> >> +   String key = headers.next().toString();
> >> +   message.setHeader(key, (String) sa.getHeaders().get(key));
> >> + }
> > 
> > Do we want to add the header's now, or set attributes that we can map to headers later?  Personally, I'm leaning towards the latter.  We might even have a generic mailet along the lines:
> > 
> >   <mailet match="All" class="Attributes2Headers">
> >     <attribute name="org.apache.james.SpamAssassin-Status>
> >       <header>X-Spam-Status</header>
> >       <mapping> <!-- optional (see AbstractVirtualUser.regexMap for idea) -->
> >         <regex>...</regex> <!-- regex to match against attribute value -->
> >         <value>...</value> <!-- parameterized string to build header   -->
> >       </mapping>
> >     </attribute>
> >     <attribute name="...">
> >       ...
> >     </attribute>
> >   </mailet
> > 
> > This would give us more control over header names and content (if there were no mapping element, the value would be copied directly).  For example, the ASF version of the header looks like:
> > 
> >   X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME
> > 
> > And it would also be more efficient, since we could modify the message just once, rather than each time we want to add meta-data.
> 
> I agree on this.
> We should not touch the MimeMessage if possible, and only store mail 
> attributes that give us much more flexibility.
> About the Attributes2Headers I would name it MailAttributesToMimeHeaders 
> and I would probably skip the regex part for the first version.
> 
> Stefano

Maybe it could look like:

<mailet match="All" class="MailAttributesToMimeHeaders">
    <map>org.apache.james.spamassassin.example1, headername</map>
    ...
    ...
    ...
</mailet>

bye
Norman

RE: svn commit: r420100 - /james/server/trunk/src/java/org/apache/james/smtpserver/SpamAssassinHandler.java

Posted by "Noel J. Bergman" <no...@devtech.com>.
Several comments inline:

> + // Not scan the message if relaying allowed

Should that be configurable?

> + Iterator headers = sa.getHeaders().keySet().iterator();
> + // Add the headers
> + while (headers.hasNext()) {
> +   String key = headers.next().toString();
> +   message.setHeader(key, (String) sa.getHeaders().get(key));
> + }

Do we want to add the header's now, or set attributes that we can map to headers later?  Personally, I'm leaning towards the latter.  We might even have a generic mailet along the lines:

  <mailet match="All" class="Attributes2Headers">
    <attribute name="org.apache.james.SpamAssassin-Status>
      <header>X-Spam-Status</header>
      <mapping> <!-- optional (see AbstractVirtualUser.regexMap for idea) -->
        <regex>...</regex> <!-- regex to match against attribute value -->
        <value>...</value> <!-- parameterized string to build header   -->
      </mapping>
    </attribute>
    <attribute name="...">
      ...
    </attribute>
  </mailet

This would give us more control over header names and content (if there were no mapping element, the value would be copied directly).  For example, the ASF version of the header looks like:

  X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME

And it would also be more efficient, since we could modify the message just once, rather than each time we want to add meta-data.

> + String responseString = "554 "
> +        + DSNStatus.getStatus(DSNStatus.PERMANENT,
> +                              DSNStatus.SECURITY_OTHER)
> +        + " This message smells like SPAM. Message rejected";

Please use more useful and less "cutsie" replies.  This reply is not useful to a sender who might want to resolve the problem.  And once again, I would turn to a generalized version of the regexMap code to allow the administrator to configure precisely the content of the string portion of the response.  This approach to message strings, possibly combined with XMLResources, could become pervasive in JAMES v3 and later.

	--- Noel


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org