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.StringTokenizer;
019    
020    import com.hs.mail.container.config.Config;
021    import com.hs.mail.container.server.socket.TcpTransport;
022    import com.hs.mail.smtp.SmtpException;
023    import com.hs.mail.smtp.SmtpSession;
024    import com.hs.mail.smtp.message.Recipient;
025    import com.hs.mail.smtp.message.SmtpMessage;
026    
027    /**
028     * Handler for RCPT command. Read recipient. Does some recipient verification.
029     * 
030     * @author Won Chul Doh
031     * @since May 29, 2010
032     * 
033     */
034    public class RcptProcessor extends AbstractSmtpProcessor {
035    
036            @Override
037            protected void doProcess(SmtpSession session, TcpTransport trans,
038                            StringTokenizer st) throws SmtpException {
039                    SmtpMessage message = session.getMessage();
040                    if (message == null || message.getFrom() == null) {
041                            throw new SmtpException(SmtpException.COMMAND_OUT_OF_SEQUENCE);
042                    }
043                    if (st.countTokens() < 1) {
044                            throw new SmtpException(SmtpException.INVALID_COMMAND_PARAM);
045                    }
046                    String to = nextToken(st);
047                    if (!startsWith(to, "TO:")) {
048                            throw new SmtpException(SmtpException.INVALID_COMMAND_PARAM);
049                    }
050                    if (to.length() == 3) {
051                            if (!st.hasMoreTokens()) {
052                                    throw new SmtpException(SmtpException.MISSING_RECIPIENT_ADDRESS);
053                            }
054                            to = nextToken(st);
055                    } else {
056                            to = to.substring(3);
057                    }
058                    if ("postmaster".equalsIgnoreCase(to)) {
059                            to = Config.getPostmaster();
060                    }
061                    int maxRcpt = Config.getMaxRcptCount();
062                    if (maxRcpt > 0) {
063                            int rcptCount = message.getRecipientsSize();
064                            if (rcptCount >= maxRcpt) {
065                                    throw new SmtpException(SmtpException.RECIPIENTS_COUNT_LIMIT);
066                            }
067                    }
068                    Recipient recipient = new Recipient(to);
069                    String toDomain = recipient.getHost();
070                    if (!Config.isLocal(toDomain)) {
071                            if (!Config.getAuthorizedNetworks().matches(
072                                            session.getClientAddress())) {
073                                    throw new SmtpException(SmtpException.RELAY_DENIED);
074                            }
075                    }
076                    message.addRecipient(recipient);
077                    session.writeResponse("250 2.1.5 Recipient <" + recipient.getMailbox()
078                                            + "> OK");
079            }
080    
081    }