View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.smtp.command;
14  
15  import java.io.IOException;
16  
17  import javax.mail.MessagingException;
18  
19  import org.abstracthorizon.mercury.common.StorageManager;
20  import org.abstracthorizon.mercury.common.command.CommandException;
21  import org.abstracthorizon.mercury.common.exception.UnknownUserException;
22  import org.abstracthorizon.mercury.common.exception.UserRejectedException;
23  import org.abstracthorizon.mercury.smtp.SMTPResponses;
24  import org.abstracthorizon.mercury.smtp.SMTPScanner;
25  import org.abstracthorizon.mercury.smtp.SMTPSession;
26  import org.abstracthorizon.mercury.smtp.exception.ParserException;
27  import org.abstracthorizon.mercury.smtp.util.Path;
28  
29  /**
30   * RCPT command
31   *
32   * @author Daniel Sendula
33   */
34  public class RcptCommand extends SMTPCommand {
35  
36      /**
37       * Constructor
38       */
39      public RcptCommand() {
40      }
41  
42      /**
43       * Executed the command
44       * @param connection smtp session
45       * @throws CommandException
46       * @throws IOException
47       * @throws ParserException
48       */
49      protected void execute(SMTPSession connection) throws CommandException, IOException, ParserException {
50          if (connection.getState() != SMTPSession.STATE_MAIL) {
51              connection.sendResponse(SMTPResponses.BAD_SEQUENCE_OF_COMMANDS_RESPONSE);
52              return;
53          }
54  
55          SMTPScanner scanner = connection.getScanner();
56          if (!scanner.is_char(' ')) { throw new ParserException("space"); }
57          if (!scanner.keyword("TO:")) { throw new ParserException("MAIL:"); }
58          if (scanner.is_char(' ')) {
59              // Allow for incorrect clients that add space here..
60          }
61          Path path = new Path();
62          if (scanner.keyword("<Postmaster>")) {
63              path.setMailbox("postmaster");
64          } else if (scanner.path(path)) {
65          } else {
66              throw new ParserException("reverse path");
67          }
68          readExtraParameters(connection, scanner);
69  
70          processPath(connection, path);
71      }
72  
73      /**
74       * Obtains extra parameters.
75       *
76       * @param session SMTP session
77       * @param scanner STMP scanner
78       * @throws IOException io exception
79       * @throws ParserException parsing exception
80       * @throws CommandException command exception
81       */
82      protected void readExtraParameters(SMTPSession connection, SMTPScanner scanner) throws IOException, ParserException, CommandException {
83          scanner.skip_line();
84      }
85  
86      /**
87       * Processes path
88       * @param connection smtp session
89       * @param path path
90       * @throws IOException
91       */
92      protected void processPath(SMTPSession connection, Path path) throws IOException {
93          StorageManager manager = connection.getConnectionHandler().getStorageManager();
94  
95          path.setLocalDomain(manager.hasDomain(path.getDomain()));
96          if (path.isLocalDomain()) {
97              try {
98                  // Store store = manager.getLocalMailbox(path.getMailbox(), path.getDomain());
99                  // path.setStore(store);
100                 path.setFolder(manager.findInbox(path.getMailbox(), path.getDomain(), null));
101                 connection.getMailSessionData().getDestinationMailboxes().add(path);
102                 connection.sendResponse(SMTPResponses.OK_RESPONSE);
103             } catch (MessagingException e) {
104                 connection.sendResponse(SMTPResponses.GENERIC_ERROR_RESPONSE);
105             } catch (UnknownUserException e) {
106                 connection.sendResponse(SMTPResponses.MAILBOX_UNAVAILABLE_RESPONSE);
107             } catch (UserRejectedException e) {
108                 connection.sendResponse(SMTPResponses.MAILBOX_UNAVAILABLE_RESPONSE);
109             }
110         } else {
111             connection.getMailSessionData().getDestinationMailboxes().add(path);
112             connection.sendResponse(SMTPResponses.MAILBOX_UNAVAILABLE_RESPONSE);
113         }
114     }
115 
116 }