1
2
3
4
5
6
7
8
9
10
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
31
32
33
34 public class RcptCommand extends SMTPCommand {
35
36
37
38
39 public RcptCommand() {
40 }
41
42
43
44
45
46
47
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
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
75
76
77
78
79
80
81
82 protected void readExtraParameters(SMTPSession connection, SMTPScanner scanner) throws IOException, ParserException, CommandException {
83 scanner.skip_line();
84 }
85
86
87
88
89
90
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
99
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 }