-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
334 lines (300 loc) · 9.88 KB
/
Client.java
File metadata and controls
334 lines (300 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import java.io.*;
import java.net.*;
public class Client {
//enum class to maintain sate
public enum States {
START, FROM, TO, DATA, MESSAGE
}
/**
* @param args
*/
public static void main(String[] args) {
//get hostname and port number from command line args
String hostname = args[0];
int portNum = Integer.parseInt(args[1]);
//create client socket with hostname and port number
try {
Socket clientSocket = new Socket(hostname, portNum);
//build streams to handle client/server communication
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Receive server welcome and send HELO command
String serverWelcome = inFromServer.readLine();
if(serverWelcome.trim().substring(0, 3).equals("220")){
String response = "HELO " + InetAddress.getLocalHost().getHostName() + '\n';
outToServer.writeBytes(response);
if(waitForResponse(States.START, inFromServer)){
sendOutgoingfile(outToServer, inFromServer);
clientSocket.close();
System.exit(1);
}else{
System.out.println("504 Bad Server Response");
clientSocket.close();
System.exit(1);
}
}else{
System.out.println("500 Syntax error: command unrecognized");
clientSocket.close();
System.exit(1);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println(e.getLocalizedMessage());
System.exit(1);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getLocalizedMessage());
System.exit(1);
}
}
//method to check syntax of outgoing file
private static boolean checkCMD(String input, States curState){
String[] cmd = input.split(":");
//first split input into two pieces and check mail from cmd or rcpt cmd
if(curState == States.FROM){
if(!cmd[0].equals("From") || cmd.length > 2){
return false;
}
}else{
if(!cmd[0].equals("To") || cmd.length > 2){
return false;
}
}
//now split the rest of the input by whitespace
String[] tokens = cmd[1].split("\\s+");
if(tokens.length > 2){
return false;
}
//variable to handle if there is no whitespace between FROM:<sender@com>
int index = 1;
if(tokens.length == 1){
index = 0;
}
//check for '<' and '>' surrounding path and remove them
if(!tokens[index].substring(0, 1).equals("<")){
return false;
}
tokens = tokens[index].split("<");
if(tokens.length > 2){
return false;
}
if(!tokens[1].substring(tokens[1].length()-1).equals(">")){
return false;
}
if(tokens[1].contains(">>")){
return false;
}
tokens = tokens[1].split(">");
//now parse the mailbox
tokens = tokens[0].split("@");
if(tokens.length != 2){
return false;
}
//parse the local-part
if(tokens[0].contains("<") || tokens[0].contains(">") || tokens[0].contains("(") || tokens[0].contains(")")
|| tokens[0].contains("[") || tokens[0].contains("]") || tokens[0].contains("\"") || tokens[0].contains(".")
|| tokens[0].contains(",") || tokens[0].contains(";") || tokens[0].contains(":") || tokens[0].contains("@") || tokens[0].contains("\\")){
return false;
}
//parse the domain
//handle first case when there is no '.'
if(tokens[1].split("\\.").length == 1){
//parse name
if(!Character.isLetter(tokens[1].charAt(0))){
return false;
}
for(int i=1; i<tokens[1].length(); i++){
if(!Character.isLetter(tokens[1].charAt(i)) && !Character.isDigit(tokens[1].charAt(i))){
return false;
}
}
}//else the domain contains a '.'
else if(tokens[1].split("\\.").length != 0){
//check all <element> strings are valid in domain
tokens = tokens[1].split("\\.");
for(int j=0; j<tokens.length; j++){
//handle '..' in middle of domain
if(!tokens[j].equals("")){
if(!Character.isLetter(tokens[j].charAt(0))){
return false;
}
for(int i=1; i<tokens[j].length(); i++){
if(!Character.isLetter(tokens[j].charAt(i)) && !Character.isDigit(tokens[j].charAt(i))){
return false;
}
}
}else{
return false;
}
}
}//the domain has two '..' in a row
else{
return false;
}
//if we get this far, should have a valid command
return true;
}
private static void sendOutgoingfile(DataOutputStream outToServer, BufferedReader inFromServer){
//get the working directory
String wkdir = System.getProperty("user.dir");
//read the outgoing file for messages
try (BufferedReader br = new BufferedReader(new FileReader(wkdir + "/outgoing")))
{
String sCurrentLine;
States curState = States.FROM;
//read through each line in the file
while ((sCurrentLine = br.readLine()) != null) {
String[] tokens;
//start with From: <reverse-path>
if(curState == States.FROM){
if(checkCMD(sCurrentLine, curState)){
tokens = sCurrentLine.split("From:");
tokens = tokens[1].split("\\s+");
//get reverse path to send to server
outToServer.writeBytes("MAIL FROM: " + tokens[1] + '\n');
//wait for 250 response from server, if fails quit program
if(waitForResponse(curState, inFromServer)){
curState = States.TO;
}else{
System.out.println("504 Bad Server Response");
return;
}
}else{
System.out.println("501 Syntax error in parameters or arguments");
return;
}
}else if(curState == States.TO){
if(checkCMD(sCurrentLine, curState)){
//do exact same processing as above for To: <forward-path>
tokens = sCurrentLine.split("To:");
tokens = tokens[1].split("\\s+");
outToServer.writeBytes("RCPT TO: " + tokens[1] + '\n');
if(waitForResponse(curState, inFromServer)){
curState = States.DATA;
}else{
System.out.println("504 Bad Server Response");
return;
}
}else{
System.out.println("501 Syntax error in parameters or arguments");
return;
}
}else if(curState == States.DATA){
//handle multiple rcpt's
if(sCurrentLine.trim().substring(0, 3).equals("To:")){
if(checkCMD(sCurrentLine, curState)){
tokens = sCurrentLine.split("To:");
tokens = tokens[1].split("\\s+");
outToServer.writeBytes("RCPT TO: " + tokens[1] + '\n');
if(waitForResponse(States.TO, inFromServer)){
curState = States.DATA;
}else{
System.out.println("504 Bad Server Response");
return;
}
}else{
System.out.println("501 Syntax error in parameters or arguments");
return;
}
}else{
//now have processed rcpt to command so output DATA command
outToServer.writeBytes("DATA\n");
//wait for 354 response
if(waitForResponse(curState, inFromServer)){
outToServer.writeBytes(sCurrentLine + '\n');
if(waitForResponse(States.MESSAGE, inFromServer)){
curState = States.MESSAGE;
}else{
System.out.println("504 Bad Server Response");
return;
}
}else{
System.out.println("504 Bad Server Response");
return;
}
}
}else if (curState == States.MESSAGE){
//data command has been received successfully so send each line of message
//first check for "From:" denoting a new email to be parsed
if(sCurrentLine.substring(0, 5).equals("From:")){
//means we have finished outputting first message so tell server with "."
outToServer.writeBytes(".\n");
if(waitForResponse(curState, inFromServer)){
//means message was received by server begin parsing the next email
curState = States.FROM;
tokens = sCurrentLine.split("From:");
tokens = tokens[1].split("\\s+");
//get reverse path to print out
outToServer.writeBytes("MAIL FROM: " + tokens[1] + '\n');
//wait for 250 response from server, if fails quit program
if(waitForResponse(curState, inFromServer)){
curState = States.TO;
}else{
System.out.println("504 Bad Server Response");
return;
}
}else{
System.out.println("504 Bad Server Response");
return;
}
}else{
//continue to print out contents of message
outToServer.writeBytes(sCurrentLine + '\n');
if(waitForResponse(curState, inFromServer)){
curState = States.MESSAGE;
}else{
System.out.println("504 Bad Server Response");
return;
}
}
}
}
//have reached end of file input so last email has been sent to server
//need to let server know message is complete with "."
outToServer.writeBytes(".\n");
//either way emit quit command and exit
if(waitForResponse(States.MESSAGE, inFromServer)){
outToServer.writeBytes("QUIT\n");
return;
}else{
System.out.println("504 Bad Server Response");
return;
}
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
System.exit(1);
} catch(ArrayIndexOutOfBoundsException e){
System.out.println(e.getLocalizedMessage());
System.exit(1);
}
}
private static boolean waitForResponse(States curState, BufferedReader inFromServer){
try{
String input;
while((input=inFromServer.readLine())!=null){
String[] tokens;
//handles mail-from, rcpt-to, and "." responses since they are the same
if(curState == States.FROM || curState == States.TO || curState == States.MESSAGE || curState == States.START){
tokens = input.split("\\s+");
if(tokens[0].equals("250")){
return true;
}else{
return false;
}
}else if(curState == States.DATA){
//waits for data command 354 response
tokens = input.split("\\s+");
if(tokens[0].equals("354")){
return true;
}else{
return false;
}
}
}
}catch(IOException io){
System.out.println(io.getLocalizedMessage());
System.exit(1);
}
return false;
}
}