package me.normanmaurer.niosmtpproxy;

import me.normanmaurer.niosmtp.core.SMTPRequestImpl;
import me.normanmaurer.niosmtp.transport.SMTPClientSession;

import org.apache.james.protocols.smtp.SMTPResponse;
import org.apache.james.protocols.smtp.SMTPSession;
import org.apache.james.protocols.smtp.core.esmtp.EhloCmdHandler;

public class SMTPProxyEhloCmdHandler extends EhloCmdHandler implements SMTPProxyConstants{

    @Override
    protected SMTPResponse doCoreCmd(SMTPSession session, String command, String parameters) {
        SMTPResponse response =  super.doCoreCmd(session, command, parameters);
        int retCode = Integer.parseInt(response.getRetCode());
        
        // check if the return code was smaller then 400. If so we don't failed the command yet and so can forward it to the real server
        if (retCode < 400) {
            FutureSMTPResponse futureResponse = new FutureSMTPResponse();
            
            SMTPClientSession clientSession = (SMTPClientSession) session.getConnectionState().get(SMTP_CLIENT_SESSION_KEY);
            final String heloName = (String) session.getState().get(SMTPSession.CURRENT_HELO_NAME);
            clientSession.send(SMTPRequestImpl.ehlo(heloName), new ExtensibleSMTPProxyResponseCallback(session, futureResponse){

                @Override
                protected void onFailure(SMTPSession session, SMTPClientSession clientSession) {
                    session.getState().remove(SMTPSession.CURRENT_HELO_NAME);
                }
            });
            return futureResponse;
            
        } else {
            return response;
        }
    }

}

@@ -69,24 +69,132 @@
 mtp.
-core.SMTPRequest
+SMTPClientConstants;%0D%0Aimport me.normanmaurer.niosmtp.core.SMTPRequestImpl;%0D%0Aimport me.normanmaurer.niosmtp.core.SMTPResponse
 Impl
@@ -1407,24 +1407,723 @@
 sponse)%7B%0D%0A%0D%0A
+                @Override%0D%0A                public void onResponse(SMTPClientSession clientSession, me.normanmaurer.niosmtp.SMTPResponse serverResponse) %7B%0D%0A                    SMTPResponseImpl copiedResponse = new SMTPResponseImpl(serverResponse.getCode());%0D%0A                    for (String responseLine: serverResponse.getLines()) %7B%0D%0A                        // remove announced STARTTLS%0D%0A                        if (!responseLine.equalsIgnoreCase(SMTPClientConstants.STARTTLS_EXTENSION)) %7B%0D%0A                            copiedResponse.addLine(responseLine);%0D%0A                        %7D%0D%0A                    %7D%0D%0A                    super.onResponse(clientSession, copiedResponse);%0D%0A                %7D%0D%0A%0D%0A
             

Make sure we remove STARTTLS from the supported EXTENSIONS when proxy EHLO requests/responses