Author: norman
Date: Thu Dec 28 02:50:46 2006
New Revision: 490695

URL: http://svn.apache.org/viewvc?view=rev&rev=490695
Log:
Modify MailCmdHandler to extend AbstractHookableCmdHandler. See JAMES-750 and 
JAMES-549

Added:
    
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/hook/MailHook.java
   (with props)
Modified:
    
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/AbstractHookableCmdHandler.java
    
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/MailCmdHandler.java
    
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/CoreFilterCmdHandlerLoader.java
    
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/GreylistHandler.java
    
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SupressDuplicateRcptHandler.java
    
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
    
james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java

Modified: 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/AbstractHookableCmdHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/AbstractHookableCmdHandler.java?view=diff&rev=490695&r1=490694&r2=490695
==============================================================================
--- 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/AbstractHookableCmdHandler.java
 (original)
+++ 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/AbstractHookableCmdHandler.java
 Thu Dec 28 02:50:46 2006
@@ -32,6 +32,7 @@
 import org.apache.james.smtpserver.hook.HeloHook;
 import org.apache.james.smtpserver.hook.HookResult;
 import org.apache.james.smtpserver.hook.HookReturnCode;
+import org.apache.james.smtpserver.hook.MailHook;
 import org.apache.james.smtpserver.hook.RcptHook;
 import org.apache.james.util.mail.SMTPRetCode;
 import org.apache.mailet.MailAddress;
@@ -77,7 +78,7 @@
     private SMTPResponse processHooks(SMTPSession session,String 
command,String parameters) {
         List hooks = getHooks();
         
-    if(hooks != null) {
+        if(hooks != null) {
             getLogger().debug("executing  hooks");
             int count = hooks.size();
             for(int i =0; i < count; i++) {
@@ -92,6 +93,10 @@
                     result = ((EhloHook) rawHook).doEhlo(session, parameters); 
   
                 }
                 
+                if ("MAIL".equals(command) && rawHook instanceof MailHook) {
+                    result = ((MailHook) rawHook).doMail(session, 
(MailAddress) session.getState().get(SMTPSession.SENDER));
+                }
+                
                 if ("RCPT".equals(command) && rawHook instanceof RcptHook) {
                     result = ((RcptHook) rawHook).doRcpt(session, 
(MailAddress) session.getState().get(SMTPSession.SENDER), (MailAddress) 
session.getState().get(SMTPSession.CURRENT_RECIPIENT));
                 }
@@ -109,9 +114,15 @@
                         
                         return new SMTPResponse(smtpRetCode, smtpDesc);
                     }else if (rCode == HookReturnCode.DENYSOFT) {
-                        return new 
SMTPResponse(SMTPRetCode.LOCAL_ERROR,"Temporary problem. Please try again 
later");
+                        if (smtpRetCode == null) smtpRetCode = 
SMTPRetCode.LOCAL_ERROR;
+                        if (smtpDesc == null) smtpDesc = "Temporary problem. 
Please try again later";
+                        
+                        return new SMTPResponse(smtpRetCode,smtpDesc);
                     } else if (rCode == HookReturnCode.OK) {
-                    return new SMTPResponse(SMTPRetCode.MAIL_OK,"Accepted.");
+                        if (smtpRetCode == null) smtpRetCode = 
SMTPRetCode.MAIL_OK;
+                        if (smtpDesc == null) smtpDesc = "Command accepted";
+                        
+                        return new SMTPResponse(smtpRetCode,smtpDesc);
                     }
                 }
             }

Modified: 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/MailCmdHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/MailCmdHandler.java?view=diff&rev=490695&r1=490694&r2=490695
==============================================================================
--- 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/MailCmdHandler.java
 (original)
+++ 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/MailCmdHandler.java
 Thu Dec 28 02:50:46 2006
@@ -23,11 +23,14 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.StringTokenizer;
 
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.james.smtpserver.CommandHandler;
 import org.apache.james.smtpserver.SMTPResponse;
 import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.smtpserver.hook.MailHook;
 import org.apache.james.util.mail.SMTPRetCode;
 import org.apache.james.util.mail.dsn.DSNStatus;
 import org.apache.mailet.MailAddress;
@@ -36,18 +39,14 @@
   * Handles MAIL command
   */
 public class MailCmdHandler
-    extends AbstractLogEnabled
+    extends AbstractHookableCmdHandler
     implements CommandHandler {
 
-    
-    /**
-     * handles MAIL command
-     *
-     * @see 
org.apache.james.smtpserver.CommandHandler#onCommand(org.apache.james.smtpserver.SMTPSession,
 java.lang.String, java.lang.String) 
-     */
-    public SMTPResponse onCommand(SMTPSession session, String command, String 
arguments) {
-        return doMAIL(session, arguments);
-    }
+    private final static String MAIL_OPTION_SIZE = "SIZE";
+
+    private final static String MESG_SIZE = "MESG_SIZE"; // The size of the 
message
+
+    private List hooks;
 
 
     /**
@@ -73,5 +72,211 @@
         implCommands.add("MAIL");
         
         return implCommands;
+    }
+    
+    /**
+     * @see 
org.apache.james.smtpserver.core.AbstractHookableCmdHandler#doCoreCmd(org.apache.james.smtpserver.SMTPSession,
 java.lang.String, java.lang.String)
+     */
+    protected SMTPResponse doCoreCmd(SMTPSession session, String command, 
String parameters) {
+        return doMAIL(session,parameters);
+    }
+
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.AbstractHookableCmdHandler#doFilterChecks(org.apache.james.smtpserver.SMTPSession,
 java.lang.String, java.lang.String)
+     */
+    protected SMTPResponse doFilterChecks(SMTPSession session, String command, 
String parameters) {
+        return doMAILFilter(session,parameters);
+    }
+
+    /**
+     * @param session SMTP session object
+     * @param argument the argument passed in with the command by the SMTP 
client
+     */
+    private SMTPResponse doMAILFilter(SMTPSession session, String argument) {
+        String sender = null;
+        
+        if ((argument != null) && (argument.indexOf(":") > 0)) {
+            int colonIndex = argument.indexOf(":");
+            sender = argument.substring(colonIndex + 1);
+            argument = argument.substring(0, colonIndex);
+        }
+        if (session.getState().containsKey(SMTPSession.SENDER)) {
+            return new SMTPResponse(SMTPRetCode.BAD_SEQUENCE, 
DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_OTHER)+" Sender 
already specified");
+        } else if 
(!session.getConnectionState().containsKey(SMTPSession.CURRENT_HELO_MODE) && 
session.getConfigurationData().useHeloEhloEnforcement()) {
+            return new SMTPResponse(SMTPRetCode.BAD_SEQUENCE, 
DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_OTHER)+" Need HELO 
or EHLO before MAIL");
+        } else if (argument == null || 
!argument.toUpperCase(Locale.US).equals("FROM")
+                   || sender == null) {
+            return new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_ARGUMENTS, 
DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_INVALID_ARG)+" 
Usage: MAIL FROM:<sender>");
+        } else {
+            sender = sender.trim();
+            // the next gt after the first lt ... AUTH may add more <>
+            int lastChar = sender.indexOf('>', sender.indexOf('<'));
+            // Check to see if any options are present and, if so, whether 
they are correctly formatted
+            // (separated from the closing angle bracket by a ' ').
+            if ((lastChar > 0) && (sender.length() > lastChar + 2) && 
(sender.charAt(lastChar + 1) == ' ')) {
+                String mailOptionString = sender.substring(lastChar + 2);
+
+                // Remove the options from the sender
+                sender = sender.substring(0, lastChar + 1);
+
+                StringTokenizer optionTokenizer = new 
StringTokenizer(mailOptionString, " ");
+                while (optionTokenizer.hasMoreElements()) {
+                    String mailOption = optionTokenizer.nextToken();
+                    int equalIndex = mailOption.indexOf('=');
+                    String mailOptionName = mailOption;
+                    String mailOptionValue = "";
+                    if (equalIndex > 0) {
+                        mailOptionName = mailOption.substring(0, 
equalIndex).toUpperCase(Locale.US);
+                        mailOptionValue = mailOption.substring(equalIndex + 1);
+                    }
+
+                    // Handle the SIZE extension keyword
+
+                    if (mailOptionName.startsWith(MAIL_OPTION_SIZE)) {
+                        SMTPResponse res = doMailSize(session, 
mailOptionValue, sender);
+                        if (res != null) {
+                            return res;
+                        }
+                    } else {
+                        // Unexpected option attached to the Mail command
+                        if (getLogger().isDebugEnabled()) {
+                            StringBuffer debugBuffer =
+                                new StringBuffer(128)
+                                    .append("MAIL command had 
unrecognized/unexpected option ")
+                                    .append(mailOptionName)
+                                    .append(" with value ")
+                                    .append(mailOptionValue);
+                            getLogger().debug(debugBuffer.toString());
+                        }
+                    }
+                }
+            }
+            if ( 
session.getConfigurationData().useAddressBracketsEnforcement() && 
(!sender.startsWith("<") || !sender.endsWith(">"))) {
+                if (getLogger().isErrorEnabled()) {
+                    StringBuffer errorBuffer =
+                        new StringBuffer(128)
+                            .append("Error parsing sender address: ")
+                            .append(sender)
+                            .append(": did not start and end with < >");
+                    getLogger().error(errorBuffer.toString());
+                }
+                return new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_ARGUMENTS, 
DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+" 
Syntax error in MAIL command");
+            }
+            MailAddress senderAddress = null;
+            
+            if (session.getConfigurationData().useAddressBracketsEnforcement() 
|| (sender.startsWith("<") && sender.endsWith(">"))) {
+                //Remove < and >
+                sender = sender.substring(1, sender.length() - 1);
+            }
+            
+            if (sender.length() == 0) {
+                //This is the <> case.  Let senderAddress == null
+            } else {
+                 
+                if (sender.indexOf("@") < 0) {
+                    sender = sender + "@" + 
session.getConfigurationData().getMailServer().getDefaultDomain();
+                }
+                
+                try {
+                    senderAddress = new MailAddress(sender);
+                } catch (Exception pe) {
+                    if (getLogger().isErrorEnabled()) {
+                        StringBuffer errorBuffer =
+                            new StringBuffer(256)
+                                    .append("Error parsing sender address: ")
+                                    .append(sender)
+                                    .append(": ")
+                                    .append(pe.getMessage());
+                        getLogger().error(errorBuffer.toString());
+                    }
+                    return new 
SMTPResponse(SMTPRetCode.SYNTAX_ERROR_ARGUMENTS, 
DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+" 
Syntax error in sender address");
+                }
+            }
+         
+            // Store the senderAddress in session map
+            session.getState().put(SMTPSession.SENDER, senderAddress);
+        }
+        return null;
+    }
+
+    /**
+     * Handles the SIZE MAIL option.
+     *
+     * @param session SMTP session object
+     * @param mailOptionValue the option string passed in with the SIZE option
+     * @param tempSender the sender specified in this mail command (for 
logging purpose)
+     * @return true if further options should be processed, false otherwise
+     */
+    private SMTPResponse doMailSize(SMTPSession session, String 
mailOptionValue, String tempSender) {
+        int size = 0;
+        try {
+            size = Integer.parseInt(mailOptionValue);
+        } catch (NumberFormatException pe) {
+            getLogger().error("Rejected syntactically incorrect value for SIZE 
parameter.");
+            // This is a malformed option value.  We return an error
+            return new SMTPResponse(SMTPRetCode.SYNTAX_ERROR_ARGUMENTS, 
DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.DELIVERY_INVALID_ARG)+" 
Syntactically incorrect value for SIZE parameter");
+        }
+        if (getLogger().isDebugEnabled()) {
+            StringBuffer debugBuffer =
+                new StringBuffer(128)
+                    .append("MAIL command option SIZE received with value ")
+                    .append(size)
+                    .append(".");
+                    getLogger().debug(debugBuffer.toString());
+        }
+        long maxMessageSize = 
session.getConfigurationData().getMaxMessageSize();
+        if ((maxMessageSize > 0) && (size > maxMessageSize)) {
+            // Let the client know that the size limit has been hit.
+            StringBuffer errorBuffer =
+                new StringBuffer(256)
+                    .append("Rejected message from ")
+                    .append(tempSender != null ? tempSender : null)
+                    .append(" from host ")
+                    .append(session.getRemoteHost())
+                    .append(" (")
+                    .append(session.getRemoteIPAddress())
+                    .append(") of size ")
+                    .append(size)
+                    .append(" exceeding system maximum message size of ")
+                    .append(maxMessageSize)
+                    .append("based on SIZE option.");
+            getLogger().error(errorBuffer.toString());
+            
+            return new SMTPResponse(SMTPRetCode.QUOTA_EXCEEDED, 
DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SYSTEM_MSG_TOO_BIG)+" Message 
size exceeds fixed maximum message size");
+        } else {
+            // put the message size in the message state so it can be used
+            // later to restrict messages for user quotas, etc.
+            session.getState().put(MESG_SIZE, new Integer(size));
+        }
+        return null;
+    }
+    
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.AbstractHookableCmdHandler#getHooks()
+     */
+    protected List getHooks() {
+        return hooks;
+    }
+
+
+    /**
+     * @see org.apache.james.smtpserver.ExtensibleHandler#getMarkerInterfaces()
+     */
+    public List getMarkerInterfaces() {
+        List interfaces = new ArrayList(1);
+        interfaces.add(MailHook.class);
+        return interfaces;
+    }
+
+
+    /**
+     * @see 
org.apache.james.smtpserver.ExtensibleHandler#wireExtensions(java.lang.Class, 
java.util.List)
+     */
+    public void wireExtensions(Class interfaceName, List extension) {
+        if(MailHook.class.equals(interfaceName)) {
+            hooks = extension;
+        }
     }
 }

Modified: 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/CoreFilterCmdHandlerLoader.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/CoreFilterCmdHandlerLoader.java?view=diff&rev=490695&r1=490694&r2=490695
==============================================================================
--- 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/CoreFilterCmdHandlerLoader.java
 (original)
+++ 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/CoreFilterCmdHandlerLoader.java
 Thu Dec 28 02:50:46 2006
@@ -48,7 +48,7 @@
         commands.put("DATA", DATABASEFILTERCMDHANDLER);
         //commands.put("EHLO", EHLOBASEFILTERCMDHANDLER);
         //commands.put("HELO", HELOBASEFILTERCMDHANDLER);
-        commands.put("MAIL", MAILBASEFILTERCMDHANDLER);
+        //commands.put("MAIL", MAILBASEFILTERCMDHANDLER);
         // not needed any more the RCPT filters get loaded from the main class
         //commands.put("RCPT", RCPTBASEFILTERCMDHANDLER);
         

Modified: 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/GreylistHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/GreylistHandler.java?view=diff&rev=490695&r1=490694&r2=490695
==============================================================================
--- 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/GreylistHandler.java
 (original)
+++ 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/GreylistHandler.java
 Thu Dec 28 02:50:46 2006
@@ -46,9 +46,10 @@
 
 import org.apache.james.services.DNSServer;
 import org.apache.james.services.FileSystem;
-import org.apache.james.smtpserver.CommandHandler;
-import org.apache.james.smtpserver.SMTPResponse;
 import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.smtpserver.hook.HookResult;
+import org.apache.james.smtpserver.hook.HookReturnCode;
+import org.apache.james.smtpserver.hook.RcptHook;
 import org.apache.james.util.JDBCUtil;
 import org.apache.james.util.NetMatcher;
 import org.apache.james.util.SqlResources;
@@ -61,7 +62,7 @@
  * GreylistHandler which can be used to activate Greylisting
  */
 public class GreylistHandler extends AbstractLogEnabled implements
-    CommandHandler, Configurable, Serviceable, Initializable {
+    RcptHook, Configurable, Serviceable, Initializable {
 
     private DataSourceSelector datasources = null;
 
@@ -286,45 +287,16 @@
         this.unseenLifeTime = TimeConverter.getMilliSeconds(unseenLifeTime);
     }
 
-    /**
-     * @see 
org.apache.james.smtpserver.CommandHandler#onCommand(org.apache.james.smtpserver.SMTPSession,
 java.lang.String, java.lang.String) 
-     */
-    public SMTPResponse onCommand(SMTPSession session, String command, String 
arguments) {
-        if (!session.isRelayingAllowed() && !(session.isAuthRequired() && 
session.getUser() != null)) {
-
-            if ((wNetworks == null) || 
(!wNetworks.matchInetNetwork(session.getRemoteIPAddress()))) {
-                return doGreyListCheck(session, arguments);
-            } else {
-                getLogger().info("IpAddress " + session.getRemoteIPAddress() + 
" is whitelisted. Skip greylisting.");
-            }
-        } else {
-            getLogger().info("IpAddress " + session.getRemoteIPAddress() + " 
is allowed to send. Skip greylisting.");
-        }
-        return null;
-    }
-
-    /**
-     * Handler method called upon receipt of a RCPT command. Calls a greylist
-     * check
-     * 
-     * 
-     * @param session
-     *            SMTP session object
-     * @param argument
-     */
-    private SMTPResponse doGreyListCheck(SMTPSession session, String argument) 
{
+    private HookResult doGreyListCheck(SMTPSession session, MailAddress 
senderAddress, MailAddress recipAddress) {
         String recip = "";
         String sender = "";
-        MailAddress recipAddress = (MailAddress) 
session.getState().get(SMTPSession.CURRENT_RECIPIENT);
-        MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
 
         if (recipAddress != null) recip = recipAddress.toString();
         if (senderAddress != null) sender = senderAddress.toString();
     
         long time = System.currentTimeMillis();
         String ipAddress = session.getRemoteIPAddress();
-    
-        SMTPResponse ret = null;
+        
         try {
             long createTimeStamp = 0;
             int count = 0;
@@ -345,7 +317,7 @@
                 long acceptTime = createTimeStamp + tempBlockTime;
         
                 if ((time < acceptTime) && (count == 0)) {
-                    ret = new SMTPResponse(SMTPRetCode.LOCAL_ERROR, 
DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.NETWORK_DIR_SERVER) 
+                    return new HookResult(HookReturnCode.DENYSOFT, 
SMTPRetCode.LOCAL_ERROR, DSNStatus.getStatus(DSNStatus.TRANSIENT, 
DSNStatus.NETWORK_DIR_SERVER) 
                         + " Temporary rejected: Reconnect to fast. Please try 
again later");
                 } else {
                     
@@ -362,7 +334,7 @@
                 insertTriplet(datasource.getConnection(), ipAddress, sender, 
recip, count, time);
       
                 // Tempory block on new triplet!
-                ret = new SMTPResponse(SMTPRetCode.LOCAL_ERROR, 
DSNStatus.getStatus(DSNStatus.TRANSIENT, DSNStatus.NETWORK_DIR_SERVER) 
+                return new HookResult(HookReturnCode.DENYSOFT, 
SMTPRetCode.LOCAL_ERROR, DSNStatus.getStatus(DSNStatus.TRANSIENT, 
DSNStatus.NETWORK_DIR_SERVER) 
                     + " Temporary rejected: Please try again later");
             }
 
@@ -380,7 +352,7 @@
             // just log the exception
             getLogger().error("Error on SQLquery: " + e.getMessage());
         }
-        return ret;
+        return new HookResult(HookReturnCode.DECLINED);
     }
 
     /**
@@ -676,11 +648,19 @@
     }
 
     /**
-     * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
+     * @see 
org.apache.james.smtpserver.hook.RcptHook#doRcpt(org.apache.james.smtpserver.SMTPSession,
 org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
      */
-    public Collection getImplCommands() {
-        Collection c = new ArrayList();
-        c.add("RCPT");
-        return c;
+    public HookResult doRcpt(SMTPSession session, MailAddress sender, 
MailAddress rcpt) {
+        if (!session.isRelayingAllowed() && !(session.isAuthRequired() && 
session.getUser() != null)) {
+
+            if ((wNetworks == null) || 
(!wNetworks.matchInetNetwork(session.getRemoteIPAddress()))) {
+                return doGreyListCheck(session, sender,rcpt);
+            } else {
+                getLogger().info("IpAddress " + session.getRemoteIPAddress() + 
" is whitelisted. Skip greylisting.");
+            }
+        } else {
+            getLogger().info("IpAddress " + session.getRemoteIPAddress() + " 
is allowed to send. Skip greylisting.");
+        }
+        return new HookResult(HookReturnCode.DECLINED);
     }
 }

Modified: 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SupressDuplicateRcptHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SupressDuplicateRcptHandler.java?view=diff&rev=490695&r1=490694&r2=490695
==============================================================================
--- 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SupressDuplicateRcptHandler.java
 (original)
+++ 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/SupressDuplicateRcptHandler.java
 Thu Dec 28 02:50:46 2006
@@ -22,13 +22,13 @@
 
 package org.apache.james.smtpserver.core.filter.fastfail;
 
-import java.util.ArrayList;
 import java.util.Collection;
 
 import org.apache.avalon.framework.logger.AbstractLogEnabled;
-import org.apache.james.smtpserver.CommandHandler;
-import org.apache.james.smtpserver.SMTPResponse;
 import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.smtpserver.hook.HookResult;
+import org.apache.james.smtpserver.hook.HookReturnCode;
+import org.apache.james.smtpserver.hook.RcptHook;
 import org.apache.james.util.mail.SMTPRetCode;
 import org.apache.james.util.mail.dsn.DSNStatus;
 import org.apache.mailet.MailAddress;
@@ -37,25 +37,12 @@
  * 
  * This handler can be used to just ignore duplicated recipients. 
  */
-public class SupressDuplicateRcptHandler extends AbstractLogEnabled implements 
CommandHandler {
+public class SupressDuplicateRcptHandler extends AbstractLogEnabled implements 
RcptHook {
 
     /**
-     * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
+     * @see 
org.apache.james.smtpserver.hook.RcptHook#doRcpt(org.apache.james.smtpserver.SMTPSession,
 org.apache.mailet.MailAddress, org.apache.mailet.MailAddress)
      */
-    public Collection getImplCommands() {
-        Collection c = new ArrayList();
-        c.add("RCPT");
-    
-        return c;
-    }
-
-    /**
-     * Ignore duplicated recipients and just return 250 as return code.
-     * 
-     * @see 
org.apache.james.smtpserver.CommandHandler#onCommand(org.apache.james.smtpserver.SMTPSession,
 java.lang.String, java.lang.String) 
-     */
-    public SMTPResponse onCommand(SMTPSession session, String command, String 
parameters) {
-        MailAddress rcpt = (MailAddress) 
session.getState().get(SMTPSession.CURRENT_RECIPIENT);
+    public HookResult doRcpt(SMTPSession session, MailAddress sender, 
MailAddress rcpt) {
         Collection rcptList = (Collection) 
session.getState().get(SMTPSession.RCPT_LIST);
     
         // Check if the recipient is allready in the rcpt list
@@ -67,8 +54,8 @@
                           .append(rcpt.toString())
                           .append("> OK");
             getLogger().debug("Duplicate recipient not add to recipient list: 
" + rcpt.toString());
-            return new SMTPResponse(SMTPRetCode.MAIL_OK, responseBuffer);
+            return new HookResult(HookReturnCode.OK,SMTPRetCode.MAIL_OK, 
responseBuffer.toString());
         }
-        return null;
+        return new HookResult(HookReturnCode.DECLINED);
     }
 }

Modified: 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java?view=diff&rev=490695&r1=490694&r2=490695
==============================================================================
--- 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
 (original)
+++ 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
 Thu Dec 28 02:50:46 2006
@@ -18,20 +18,21 @@
  ****************************************************************/
 package org.apache.james.smtpserver.core.filter.fastfail;
 
-import java.util.ArrayList;
 import java.util.Collection;
 
 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.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.james.dnsserver.TemporaryResolutionException;
 import org.apache.james.services.DNSServer;
-import org.apache.james.smtpserver.CommandHandler;
-import org.apache.james.smtpserver.SMTPResponse;
 import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.smtpserver.hook.HookResult;
+import org.apache.james.smtpserver.hook.HookReturnCode;
+import org.apache.james.smtpserver.hook.MailHook;
 import org.apache.james.util.mail.SMTPRetCode;
 import org.apache.james.util.mail.dsn.DSNStatus;
 import org.apache.mailet.MailAddress;
@@ -41,8 +42,8 @@
  * 
  */
 public class ValidSenderDomainHandler
-    extends AbstractJunkHandler
-    implements CommandHandler, Configurable, Serviceable {
+    extends AbstractLogEnabled
+    implements MailHook, Configurable, Serviceable {
     
     private boolean checkAuthNetworks = false;
     
@@ -58,8 +59,6 @@
         if(configRelay != null) {
             setCheckAuthNetworks(configRelay.getValueAsBoolean(false));
         }
-        
-        super.configure(handlerConfiguration);
     }
     
     /**
@@ -86,20 +85,9 @@
     public void setCheckAuthNetworks(boolean checkAuthNetworks) {
         this.checkAuthNetworks = checkAuthNetworks;
     }
+
     
-    /**
-     * @see 
org.apache.james.smtpserver.CommandHandler#onCommand(org.apache.james.smtpserver.SMTPSession,
 java.lang.String, java.lang.String) 
-     */
-    public SMTPResponse onCommand(SMTPSession session, String command, String 
parameters) {
-        return doProcessing(session);
-    }
-    
-    /**
-     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#check(org.apache.james.smtpserver.SMTPSession)
-     */
-    protected boolean check(SMTPSession session) {
-        MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
-            
+    protected boolean check(SMTPSession session, MailAddress senderAddress) {
         // null sender so return
         if (senderAddress == null) return false;
             
@@ -125,26 +113,13 @@
     }
     
     /**
-     * @see org.apache.james.smtpserver.CommandHandler#getImplCommands()
+     * @see 
org.apache.james.smtpserver.hook.MailHook#doMail(org.apache.james.smtpserver.SMTPSession,
 org.apache.mailet.MailAddress)
      */
-    public Collection getImplCommands() {
-        Collection implCommands = new ArrayList();
-        implCommands.add("MAIL");
-        
-        return implCommands;
-    }
-    
-    /**
-     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractJunkHandler#getJunkHandlerData(org.apache.james.smtpserver.SMTPSession)
-     */
-    public JunkHandlerData getJunkHandlerData(SMTPSession session) {
-        MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
-        JunkHandlerData data = new JunkHandlerData();
-    
-        data.setRejectResponseString(new 
SMTPResponse(SMTPRetCode.SYNTAX_ERROR_ARGUMENTS,DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+
 " sender " + senderAddress + " contains a domain with no valid MX records"));
-        data.setJunkScoreLogString("Sender " + senderAddress + " contains a 
domain with no valid MX records. Add Junkscore: " + getScore());
-        data.setRejectLogString("Sender " + senderAddress + " contains a 
domain with no valid MX records");
-        data.setScoreName("ValidSenderDomainCheck");
-        return data;
+    public HookResult doMail(SMTPSession session, MailAddress sender) {
+        if (check(session,sender)) {
+            return new 
HookResult(HookReturnCode.DENY,SMTPRetCode.SYNTAX_ERROR_ARGUMENTS,DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+
 " sender " + sender + " contains a domain with no valid MX records");
+        } else {
+            return new HookResult(HookReturnCode.DECLINED);
+        }
     }
 }

Added: 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/hook/MailHook.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/hook/MailHook.java?view=auto&rev=490695
==============================================================================
--- 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/hook/MailHook.java
 (added)
+++ 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/hook/MailHook.java
 Thu Dec 28 02:50:46 2006
@@ -0,0 +1,9 @@
+package org.apache.james.smtpserver.hook;
+
+import org.apache.james.smtpserver.SMTPSession;
+import org.apache.mailet.MailAddress;
+
+public interface MailHook {
+
+    public HookResult doMail(SMTPSession session, MailAddress sender);
+}

Propchange: 
james/server/sandbox/handlerapi-experiment/src/java/org/apache/james/smtpserver/hook/MailHook.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java
URL: 
http://svn.apache.org/viewvc/james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java?view=diff&rev=490695&r1=490694&r2=490695
==============================================================================
--- 
james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java
 (original)
+++ 
james/server/sandbox/handlerapi-experiment/src/test/org/apache/james/smtpserver/ValidSenderDomainHandlerTest.java
 Thu Dec 28 02:50:46 2006
@@ -30,9 +30,8 @@
 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.smtpserver.hook.HookReturnCode;
 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;
@@ -57,8 +56,7 @@
     private SMTPSession setupMockedSession(final MailAddress sender) {
         SMTPSession session = new AbstractSMTPSession() {
             HashMap state = new HashMap();
-            boolean processing = false;
-            
+
             public Map getState() {
 
                 state.put(SMTPSession.SENDER, sender);
@@ -69,14 +67,7 @@
             public boolean isRelayingAllowed() {
                 return false;
             }
-            
-            public void setStopHandlerProcessing(boolean processing) {
-                this.processing = processing;
-            }
-            
-            public boolean getStopHandlerProcessing() {
-                return processing;
-            }
+
             
         };
         return session;
@@ -89,32 +80,18 @@
         ContainerUtil.enableLogging(handler, new MockLogger());
         
         handler.setDnsServer(setupDNSServer());
-        SMTPResponse response = 
handler.onCommand(setupMockedSession(null),"MAIL","<>");
-        
-        assertNull("Not blocked cause its a nullsender",response);
-    }
-    
-    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());
-        SMTPResponse response = handler.onCommand(session,"MAIL","<" + 
session.getState().get(SMTPSession.SENDER) + ">");
+        int response = 
handler.doMail(setupMockedSession(null),null).getResult();
         
-        assertNull("Not blocked cause we use JunkScore", response);
-        assertEquals("JunkScore is stored",((JunkScore) 
session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidSenderDomainCheck"),20.0,0d);
+        assertEquals("Not blocked cause its a 
nullsender",response,HookReturnCode.DECLINED);
     }
-    
+
     public void testInvalidSenderDomainReject() throws ParseException {
         ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
         SMTPSession session = setupMockedSession(new MailAddress("[EMAIL 
PROTECTED]"));
         ContainerUtil.enableLogging(handler, new MockLogger());
         handler.setDnsServer(setupDNSServer());
-        SMTPResponse response = handler.onCommand(session,"MAIL","<" + 
session.getState().get(SMTPSession.SENDER) + ">");
+        int response = handler.doMail(session,(MailAddress) 
session.getState().get(SMTPSession.SENDER)).getResult();
         
-        assertNotNull("Blocked cause we use reject action", response);
+        assertEquals("Blocked cause we use reject action", 
response,HookReturnCode.DENY);
     }
 }



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

Reply via email to