001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.smtp.processor;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.StringTokenizer;
021    
022    import com.hs.mail.container.config.Config;
023    import com.hs.mail.container.server.socket.TcpTransport;
024    import com.hs.mail.smtp.SmtpException;
025    import com.hs.mail.smtp.SmtpSession;
026    
027    /**
028     * Handler for EHLO command. Responds with a greeting and informs the client the
029     * supported extensions.
030     * 
031     * @author Won Chul Doh
032     * @since May 29, 2010
033     * 
034     */
035    public class EhloProcessor extends AbstractSmtpProcessor {
036    
037            @Override
038            protected void doProcess(SmtpSession session, TcpTransport trans,
039                            StringTokenizer st) throws SmtpException {
040                    if (st.countTokens() < 1) {
041                            throw new SmtpException(SmtpException.MISSING_DOMAIN_ADDRESS);
042                    } else {
043                            String clientDomain = nextToken(st);
044                            if (clientDomain.length() > 256) {
045                                    throw new SmtpException(SmtpException.DOMAIN_NAME_LENGTH_LIMIT);
046                            } else {
047                                    session.setClientDomain(clientDomain);
048                                    session.setProtocol("ESMTP");
049                                    List<String> extensions = new ArrayList<String>();
050                                    extensions.add(new StringBuilder().append(Config.getHelloName())
051                                                    .append(" Hello ")
052                                                    .append(session.getRemoteHost())
053                                                    .append(" [")
054                                                    .append(session.getRemoteIP())
055                                                    .append("]").toString());
056                                    long maxMessageSize = Config.getMaxMessageSize();
057                                    if (maxMessageSize > 0) {
058                                            extensions.add("SIZE " + maxMessageSize);
059                                    }
060                                    if (Config.isSaslAuthEnabled()) {
061                                            extensions.add("AUTH LOGIN PLAIN");
062                                            extensions.add("AUTH=LOGIN PLAIN");
063                                    }
064                                    extensions.add("ENHANCEDSTATUSCODES");
065                                    for (String extension : extensions) {
066                                            session.writeResponse("250-" + extension);
067                                    }
068                                    session.writeResponse("250 HELP");
069                            }
070                    }
071            }
072    
073    }