Author: norman
Date: Thu Nov 16 04:16:59 2006
New Revision: 475703

URL: http://svn.apache.org/viewvc?view=rev&rev=475703
Log:
First "Prototype" of an Handler which supports more the one "action". See 
JAMES-614

Modified:
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
    
james/server/trunk/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java

Modified: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java?view=diff&rev=475703&r1=475702&r2=475703
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
 Thu Nov 16 04:16:59 2006
@@ -34,6 +34,8 @@
 import org.apache.james.services.DNSServer;
 import org.apache.james.smtpserver.CommandHandler;
 import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.util.junkscore.JunkScore;
+import org.apache.james.util.junkscore.JunkScoreConfigUtil;
 import org.apache.james.util.mail.dsn.DSNStatus;
 import org.apache.mailet.MailAddress;
 
@@ -44,6 +46,10 @@
     private boolean checkAuthClients = false;
     
     private DNSServer dnsServer = null;
+
+    private String action = "reject";
+
+    private double score = 0;
     
     /**
      * @see 
org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
@@ -54,6 +60,16 @@
         if(configRelay != null) {
             setCheckAuthClients(configRelay.getValueAsBoolean(false));
         }
+        
+        Configuration configAction = 
handlerConfiguration.getChild("action",false);
+        if(configAction != null) {
+            String configString = configAction.getValue();
+            if (configString.startsWith(JunkScoreConfigUtil.JUNKSCORE)) {
+                setAction(JunkScoreConfigUtil.JUNKSCORE);
+                
setScore(JunkScoreConfigUtil.getJunkScore(configAction.getValue()));
+            }
+        }
+        
     }
     
     /**
@@ -81,38 +97,70 @@
         this.checkAuthClients = checkAuthClients;
     }
     
+    /**
+     * Set the Action which should be taken if the mail from has no valid 
domain.
+     * Supported are: junkScore and reject
+     * 
+     * @param action the action
+     */
+    public void setAction(String action) {
+        this.action = action.toLowerCase();
+    }
+    
+    /**
+     * Set the score which will get added to the JunkScore object if the 
action is junkScore andt the sender has no valid domain
+     * 
+     * @param score the score
+     */
+    public void setScore(double score) {
+        this.score = score;
+    }
+    
 
     /**
      * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
      */
     public void onCommand(SMTPSession session) {
-        
-       String responseString = null;
+        boolean match = checkRBL(session);
+        if (match == true) {
         MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
-        
+
+            if (action.equals(JunkScoreConfigUtil.JUNKSCORE)) {
+                String response = "Sender " + senderAddress + " contains a 
domain with no valid MX records. Add Junkscore: " + score;
+                getLogger().info(response);
+                JunkScore junk = (JunkScore) 
session.getState().get(JunkScore.JUNK_SCORE);
+                junk.setStoredScore("ValidSenderDomainCheck", score);
+                 
+            } else {
+                String response = "501 
"+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+ " 
sender " + senderAddress + " contains a domain with no valid MX records";
+                getLogger().info(response);
+                session.writeResponse(response);
+                // After this filter match we should not call any other 
handler!
+                session.setStopHandlerProcessing(true);
+            }
+        } 
+    }
+    
+    private boolean checkRBL(SMTPSession session) {
+        MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
+            
         // null sender so return
-        if (senderAddress == null) return;
-        
+        if (senderAddress == null) return false;
+            
         /**
          * don't check if the ip address is allowed to relay. Only check if it 
is set in the config. 
          */
         if (checkAuthClients || !session.isRelayingAllowed()) {
-
-            // Maybe we should build a static method in 
org.apache.james.dnsserver.DNSServer ?
             Collection records;
-        
             
+                
             // try to resolv the provided domain in the senderaddress. If it 
can not resolved do not accept it.
             records = dnsServer.findMXRecords(senderAddress.getHost());
             if (records == null || records.size() == 0) {
-                responseString = "501 
"+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+ " 
sender " + senderAddress + " contains a domain with no valid MX records";
-                session.writeResponse(responseString);
-                getLogger().info(responseString);
-                
-                // After this filter match we should not call any other 
handler!
-                session.setStopHandlerProcessing(true);
+                return true;
             }
         }
+        return false;
     }
     
     /**

Modified: 
james/server/trunk/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java?view=diff&rev=475703&r1=475702&r2=475703
==============================================================================
--- 
james/server/trunk/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java
 (original)
+++ 
james/server/trunk/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java
 Thu Nov 16 04:16:59 2006
@@ -24,11 +24,15 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.mail.internet.ParseException;
+
 import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.james.services.AbstractDNSServer;
 import org.apache.james.services.DNSServer;
 import 
org.apache.james.smtpserver.core.filter.fastfail.ValidSenderDomainHandler;
 import org.apache.james.test.mock.avalon.MockLogger;
+import org.apache.james.util.junkscore.JunkScore;
+import org.apache.james.util.junkscore.JunkScoreImpl;
 import org.apache.mailet.MailAddress;
 
 import junit.framework.TestCase;
@@ -59,6 +63,7 @@
     private SMTPSession setupMockedSession(final MailAddress sender) {
         SMTPSession session = new AbstractSMTPSession() {
             HashMap state = new HashMap();
+            boolean processing = false;
             
             public Map getState() {
 
@@ -75,6 +80,14 @@
                 response = resp;
             }
             
+            public void setStopHandlerProcessing(boolean processing) {
+                this.processing = processing;
+            }
+            
+            public boolean getStopHandlerProcessing() {
+                return processing;
+            }
+            
         };
         return session;
     }
@@ -84,7 +97,7 @@
     }
     
     // Test for JAMES-580
-    public void testNullSender() {
+    public void testNullSenderNotReject() {
         ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
         ContainerUtil.enableLogging(handler, new MockLogger());
         
@@ -92,5 +105,29 @@
         handler.onCommand(setupMockedSession(null));
         
         assertNull("Not blocked cause its a nullsender",getResponse());
+    }
+    
+    public void testInvalidSenderDomainAddJunkScore() throws ParseException {
+        ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
+        SMTPSession session = setupMockedSession(new MailAddress("[EMAIL 
PROTECTED]"));
+        ContainerUtil.enableLogging(handler, new MockLogger());
+        session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
+        handler.setAction("JunkScore");
+        handler.setScore(20);
+        handler.setDnsServer(setupDNSServer());
+        handler.onCommand(session);
+        
+        assertNull("Not blocked cause we use JunkScore",getResponse());
+        assertEquals("JunkScore is stored",((JunkScore) 
session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidSenderDomainCheck"),20.0,0d);
+    }
+    
+    public void testInvalidSenderDomainReject() throws ParseException {
+        ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
+        SMTPSession session = setupMockedSession(new MailAddress("[EMAIL 
PROTECTED]"));
+        ContainerUtil.enableLogging(handler, new MockLogger());
+        handler.setDnsServer(setupDNSServer());
+        handler.onCommand(session);
+        
+        assertNotNull("Blocked cause we use reject action",getResponse());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to