/***********************************************************************
 * Copyright (c) 2000-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 org.apache.avalon.framework.configuration.DefaultConfiguration;
import org.apache.james.test.util.Util;

public class SMTPTestConfiguration extends DefaultConfiguration {

    private int m_smtpListenerPort;
    private int m_maxMessageSize = 0;
    private String m_authorizedAddresses = "127.0.0.0/8";
    private String m_authorizingMode = "false";
    private boolean m_verifyIdentity = false;
    private Integer m_connectionLimit = null;
    private boolean m_heloResolv = false;
    private boolean m_ehloResolv = false;
    
    public SMTPTestConfiguration(int smtpListenerPort) {
        super("smptserver");

        m_smtpListenerPort = smtpListenerPort;
    }

    public void setMaxMessageSize(int kilobytes)
    {
        m_maxMessageSize = kilobytes;
    }
    
    public int getMaxMessageSize() {
        return m_maxMessageSize;
    }

    public String getAuthorizedAddresses() {
        return m_authorizedAddresses;
    }

    public void setAuthorizedAddresses(String authorizedAddresses) {
        m_authorizedAddresses = authorizedAddresses;
    }

    public void setAuthorizingNotRequired() {
        m_authorizingMode = "false";
        m_verifyIdentity = false; 
    }

    public void setAuthorizingRequired() {
        m_authorizingMode = "true";
        m_verifyIdentity = true; 
    }

    public void setAuthorizingAnnounce() {
        m_authorizingMode = "announce";
        m_verifyIdentity = true; 
    }

    public void setConnectionLimit(int iConnectionLimit) {
        m_connectionLimit = new Integer(iConnectionLimit);
    }
    
    public void setHeloResolv() {
        m_heloResolv = true; 
    }
    
    public void setEhloResolv() {
        m_ehloResolv = true; 
    }

    public void init() {

        setAttribute("enabled", true);

        addChild(Util.getValuedConfiguration("port", "" + m_smtpListenerPort));
        if (m_connectionLimit != null) addChild(Util.getValuedConfiguration("connectionLimit", "" + m_connectionLimit.intValue()));
        
        DefaultConfiguration handlerConfig = new DefaultConfiguration("handler");
        handlerConfig.addChild(Util.getValuedConfiguration("helloName", "myMailServer"));
        handlerConfig.addChild(Util.getValuedConfiguration("connectiontimeout", "360000"));
        handlerConfig.addChild(Util.getValuedConfiguration("authorizedAddresses", m_authorizedAddresses));
        handlerConfig.addChild(Util.getValuedConfiguration("maxmessagesize", "" + m_maxMessageSize));
        handlerConfig.addChild(Util.getValuedConfiguration("authRequired", m_authorizingMode));
        if (m_verifyIdentity) handlerConfig.addChild(Util.getValuedConfiguration("verifyIdentity", "" + m_verifyIdentity));
        
        handlerConfig.addChild(Util.createRemoteManagerHandlerChainConfiguration());
        
        // Add Configuration for Helo checks and Ehlo checks
        DefaultConfiguration heloConfig = (DefaultConfiguration) handlerConfig.getChild("handlerchain").getChild("handler");
        heloConfig.addChild(Util.getValuedConfiguration("checkValidHelo",m_heloResolv+""));     
        heloConfig.addChild(Util.getValuedConfiguration("checkValidEhlo",m_ehloResolv+""));  
        
        addChild(handlerConfig);
    }

}

@@ -1222,16 +1222,155 @@
 ver;%0D%0A%0D%0A
+import org.apache.avalon.framework.configuration.Configuration;%0D%0Aimport org.apache.avalon.framework.configuration.ConfigurationException;%0D%0A
 import o
@@ -1425,32 +1425,32 @@
 Configuration;%0D%0A
-
 import org.apach
@@ -3172,16 +3172,46 @@
 d init()
+ throws ConfigurationException
  %7B%0D%0A%0D%0A  
@@ -4342,39 +4342,32 @@
 checks%0D%0A        
-Default
 Configuration he
@@ -4363,16 +4363,18 @@
 guration
+%5B%5D
  heloCon
@@ -4382,31 +4382,8 @@
 ig =
- (DefaultConfiguration)
  han
@@ -4418,32 +4418,35 @@
 chain%22).getChild
+ren
 (%22handler%22);%0D%0A  
@@ -4447,34 +4447,332 @@
 );%0D%0A        
-heloConfig
+for (int i = 0; i %3C heloConfig.length; i++) %7B%0D%0A            if (heloConfig%5Bi%5D instanceof DefaultConfiguration) %7B%0D%0A                String cmd = ((DefaultConfiguration) heloConfig%5Bi%5D).getAttribute(%22command%22);%0D%0A                if (%22HELO%22.equals(cmd)) %7B%0D%0A                    ((DefaultConfiguration) heloConfig%5Bi%5D)
 .addChild(Ut
@@ -4846,16 +4846,102 @@
 %0A       
+         %7D else if (%22EHLO%22.equals(cmd)) %7B%0D%0A                    ((DefaultConfiguration)
  heloCon
@@ -4939,24 +4939,28 @@
 ) heloConfig
+%5Bi%5D)
 .addChild(Ut
@@ -5020,18 +5020,61 @@
 lv+%22%22));
-  
+%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D
 %0D%0A      

Fix for the test of the previous patch [Check for valid domain in EHLO] (JAMES-451)