Source code: com/xpn/xwiki/test/smtp/SmtpRequest.java
1 /*
2 * Dumbster: a dummy SMTP server.
3 * Copyright (C) 2003, Jason Paul Kitchen
4 * lilnottsman@yahoo.com
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 package com.xpn.xwiki.test.smtp;
21
22 /**
23 * Contains an SMTP client request.
24 */
25 public class SmtpRequest {
26 /** SMTP action received from client. */
27 private SmtpActionType action;
28 /** Current state of the SMTP state table. */
29 private SmtpState state;
30 /** Additional information passed from the client with the SMTP action. */
31 private String params;
32
33 /**
34 * Create a new SMTP client request.
35 * @param actionType type of action/command
36 * @param params remainder of command line once command is removed
37 * @param state current SMTP server state
38 */
39 public SmtpRequest(SmtpActionType actionType, String params, SmtpState state) {
40 this.action = actionType;
41 this.state = state;
42 this.params = params;
43 }
44
45 /**
46 * Execute the SMTP request returning a response. This method models the state transition table for the SMTP server.
47 * @return reponse to the request
48 */
49 public SmtpResponse execute() {
50 SmtpResponse response = null;
51 if (action.isStateless()) {
52 if (SmtpActionType.EXPN == action || SmtpActionType.VRFY == action) {
53 response = new SmtpResponse(252, "Not supported", this.state);
54 } else if (SmtpActionType.HELP == action) {
55 response = new SmtpResponse(211, "No help available", this.state);
56 } else if (SmtpActionType.NOOP == action) {
57 response = new SmtpResponse(250, "OK", this.state);
58 } else if (SmtpActionType.VRFY == action) {
59 response = new SmtpResponse(252, "Not supported", this.state);
60 } else if (SmtpActionType.RSET == action) {
61 response = new SmtpResponse(250, "OK", SmtpState.GREET);
62 } else {
63 response = new SmtpResponse(500, "Command not recognized", this.state);
64 }
65 } else { // Stateful commands
66 if (SmtpActionType.CONNECT == action) {
67 if (SmtpState.CONNECT == state) {
68 response = new SmtpResponse(220, "localhost Dumbster SMTP service ready", SmtpState.GREET);
69 } else {
70 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
71 }
72 } else if (SmtpActionType.EHLO == action) {
73 if (SmtpState.GREET == state) {
74 response = new SmtpResponse(250, "OK", SmtpState.MAIL);
75 } else {
76 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
77 }
78 } else if (SmtpActionType.MAIL == action) {
79 if (SmtpState.MAIL == state) {
80 response = new SmtpResponse(250, "OK", SmtpState.RCPT);
81 } else {
82 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
83 }
84 } else if (SmtpActionType.RCPT == action) {
85 if (SmtpState.RCPT == state) {
86 response = new SmtpResponse(250, "OK", this.state);
87 } else {
88 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
89 }
90 } else if (SmtpActionType.DATA == action) {
91 if (SmtpState.RCPT == state) {
92 response = new SmtpResponse(354, "Start mail input; end with <CRLF>.<CRLF>", SmtpState.DATA_HDR);
93 } else {
94 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
95 }
96 } else if (SmtpActionType.UNRECOG == action) {
97 if (SmtpState.DATA_HDR == state || SmtpState.DATA_BODY == state) {
98 response = new SmtpResponse(-1, "", this.state);
99 } else {
100 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
101 }
102 } else if (SmtpActionType.DATA_END == action) {
103 if (SmtpState.DATA_HDR == state || SmtpState.DATA_BODY == state) {
104 response = new SmtpResponse(250, "OK", SmtpState.QUIT);
105 } else {
106 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
107 }
108 } else if (SmtpActionType.BLANK_LINE == action) {
109 if (SmtpState.DATA_HDR == state) {
110 response = new SmtpResponse(-1, "", SmtpState.DATA_BODY);
111 } else if (SmtpState.DATA_BODY == state) {
112 response = new SmtpResponse(-1, "", this.state);
113 } else {
114 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
115 }
116 } else if (SmtpActionType.QUIT == action) {
117 if (SmtpState.QUIT == state) {
118 response = new SmtpResponse(250, "OK", SmtpState.CONNECT);
119 } else {
120 response = new SmtpResponse(503, "Bad sequence of commands: "+action, this.state);
121 }
122 } else {
123 response = new SmtpResponse(500, "Command not recognized", this.state);
124 }
125 }
126 return response;
127 }
128
129 /**
130 * Create an SMTP request object given a line of the input stream from the client and the current internal state.
131 * @param s line of input
132 * @param state current state
133 * @return a populated SmtpRequest object
134 */
135 public static SmtpRequest createRequest(String s, SmtpState state) {
136 SmtpActionType action = null;
137 String params = null;
138
139 if (state == SmtpState.DATA_HDR) {
140 if (s.equals(".")) {
141 action = SmtpActionType.DATA_END;
142 } else if (s.length() < 1) {
143 action = SmtpActionType.BLANK_LINE;
144 } else {
145 action = SmtpActionType.UNRECOG;
146 params = s;
147 }
148 } else if (state == SmtpState.DATA_BODY) {
149 if (s.equals(".")) {
150 action = SmtpActionType.DATA_END;
151 } else {
152 action = SmtpActionType.UNRECOG;
153 params = s;
154 }
155 }else {
156 String su = s.toUpperCase();
157 if (su.startsWith("EHLO ") || su.startsWith("HELO")) {
158 action = SmtpActionType.EHLO;
159 params = s.substring(5);
160 } else if (su.startsWith("MAIL FROM:")) {
161 action = SmtpActionType.MAIL;
162 params = s.substring(10);
163 } else if (su.startsWith("RCPT TO:")) {
164 action = SmtpActionType.RCPT;
165 params = s.substring(8);
166 } else if (su.startsWith("DATA")) {
167 action = SmtpActionType.DATA;
168 } else if (su.startsWith("QUIT")) {
169 action = SmtpActionType.QUIT;
170 } else if (su.startsWith("RSET")) {
171 action = SmtpActionType.RSET;
172 } else if (su.startsWith("NOOP")) {
173 action = SmtpActionType.NOOP;
174 } else if (su.startsWith("EXPN")) {
175 action = SmtpActionType.EXPN;
176 } else if (su.startsWith("VRFY")) {
177 action = SmtpActionType.VRFY;
178 } else if (su.startsWith("HELP")) {
179 action = SmtpActionType.HELP;
180 }
181 }
182
183 SmtpRequest req = new SmtpRequest(action, params, state);
184 return req;
185 }
186
187 /**
188 * Get the parameters of this request (remainder of command line once the command is removed.
189 * @return parameters
190 */
191 public String getParams() {
192 return params;
193 }
194 }