-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
298 lines (234 loc) · 7.19 KB
/
Client.py
File metadata and controls
298 lines (234 loc) · 7.19 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
# A client program of SMTP
import sys
from socket import *
"""helper functions from previous SMTP hw"""
def ifmailbox(str):
# check if there is a "@"
i = 0
for char in str:
if char == "@":
break
i += 1
# i is the index of the str
if i == 0:
# in the str, i = 0, the first char is "@"
print "501 Syntax error in parameters or arguments"
return False
elif i == len(str) - 1:
# i = strlen -1 the last is "@" wrong
print "501 Syntax error in parameters or arguments"
return False
elif i == len(str):
# no "@" in the domain part
print "501 Syntax error in parameters or arguments"
return False
else:
# check the part before "@" is element and recursively check the later part ifdomain()
return iflocalpart(str[0:i]) and ifdomain(str[i + 1:])
def ifdomain(str):
# check if there is a "."
i = 0
for char in str:
if char == ".":
break
i += 1
if i == 0:
print "501 Syntax error in parameters or arguments"
return False
elif i == len(str): # . at the beginning or the end
if str[-1] == ".":
print "501 Syntax error in parameters or arguments"
return False
else:
return ifelement(str)
else:
if not ifelement(str[0:i]):
return False
else:
return ifdomain(str[i + 1:])
def ifelement(str):
if len(str) < 2:
print "501 Syntax error in parameters or arguments"
return False
elif ifname(str):
return True
else:
print "501 Syntax error in parameters or arguments"
return False
def iflocalpart(str):
if not ifc(str[0]):
print "501 Syntax error in parameters or arguments"
return False
elif ifstring(str):
return True
else:
print "501 Syntax error in parameters or arguments"
return False
def ifname(str):
if not ifa(str[0]):
return False
else:
return ifletdigstr(str)
def ifletdigstr(str):
for char in str:
if not ifletdig(char):
return False
return True
def ifletdig(char):
if not ((ifa(char) or ifd(char))):
return False
else:
return True
def ifstring(str):
for char in str:
if not ifc(char):
return False
return True
def ifCRLF(char):
if char == "\n":
return True
else:
return False
def ifsp(char):
if ord(char) == 9 or ord(char) == 32: # acsii value of space and tab
return True
else:
return False
def ifa(char):
if 64 < ord(char) < 91 or 96 < ord(char) < 123: # acsii value for english characters
return True
else:
return False
def ifc(char):
if ifsp(char) or ifspecial(char):
return False
elif ord(char) >= 0 and ord(char) <= 127:
return True
else:
return False
def ifd(char):
if 48 <= ord(char) <= 57:
return True
else:
return False
def ifspecial(char):
spchar = ["<", ">", "(", ")", "[", "]", " \ ", ".", ",", ";", ":", "@", ' " ']
if char in spchar:
return True
else:
return False
""" main client function starts here """
def main():
"""take in user input"""
try:
# wait for mail from cmd
switch_mf = True
while switch_mf:
mfcmd = raw_input("From:").strip()
if ifmailbox(mfcmd):
# verify the mail from cmd, if true continue to next step,
# if false, waiting for re input
switch_mf = False
else:
continue
# wait for rcpt to cmd
switch_rt = True
while switch_rt:
rtcmd = raw_input("To:")
rpt_list_pre = rtcmd.split(",")
# receive all the rcpt to address in single line seperated by comma,
# then store them in list rpt_list
rpt_list = []
for i in rpt_list_pre:
rpt_list.append(i.strip())
switch_rt = False
# default situation is all address we received is valid
for i in rpt_list:
if not ifmailbox(i):
switch_rt = True
# check each one of forward-path, one error cause the whole loop to rerun
# wait for subject input
subject = raw_input("Subject:")
# wait for msg input
msg = []
print "Message:".strip("/n")
while True:
line = raw_input()
if line == ".":
# if period is encountered, exit the input step
break
else:
msg.append(line)
# store each line as a element in the list msg
except EOFError:
# exit when end of file error is met
sys.exit()
# takes two command line arguments, hostname is the domain name of the server this client using
# portname specifies the port number on which the SMTP server is listening to the connection
# after the user enterred a valid emil message
serverName = sys.argv[1]
serverPort = sys.argv[2]
if not ifdomain(serverName):
# check the validility of serverName
print "Error -- wrong hostname"
sys.exit()
# send request to server
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, int(serverPort)))
# receive greeting message
greeting = clientSocket.recv(1024).decode()
# and send HELO message
# clientSocket.send(("HELO %s\n" % serverName).encode())
clientSocket.send(("HELO %s" % gethostname()).encode())
# receive ack msg
ack = clientSocket.recv(1024).decode()
if not ack[0:3] == "250" :
print "Error -- bad connection"
sys.exit()
# send everything in a single package
# send mail from
cmd_st = "MAIL FROM: <%s>\n" % mfcmd
clientSocket.send(cmd_st.encode())
# received 250 for mail from
reply = clientSocket.recv(1024).decode()
if not reply[0:3] == "250":
print reply
sys.exit()
# send rcpt to
for i in rpt_list:
cmd_st = "RCPT TO: <%s>\n" % i
clientSocket.send(cmd_st.encode())
# received 250 for RCPT
reply = clientSocket.recv(1024).decode()
if not reply[0:3] == "250":
print reply
sys.exit()
# send DATA
cmd_st = "DATA\n"
clientSocket.send(cmd_st.encode())
# received 354 for data
reply = clientSocket.recv(1024).decode()
if not reply[0:3] == "354":
print reply
sys.exit()
data = "From: <%s>\n" % mfcmd
for i in rpt_list:
data += ("To: <%s>\n" % i)
data += ("Subject: " + subject + "\n\n")
for i in msg:
data += (i+"\n")
data += ".\n"
clientSocket.send(data.encode())
# received 250 for data sent
reply = clientSocket.recv(1024).decode()
if not reply[0:3] == "250":
print reply
sys.exit()
else:
clientSocket.send("QUIT".encode())
# send the message and close the connection, exit program
# clientSocket.send(data.encode())
clientSocket.close()
sys.exit()
if __name__ == "__main__":
main()