/
MailServer.py
336 lines (291 loc) · 12.7 KB
/
MailServer.py
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
335
336
import getopt
import socket
import sys
from threading import Thread, Condition, Lock
import time
import shutil
# Don't change 'host' and 'port' values below. If you do, we will not be able to
# find your server when grading.
host = "127.0.0.1"
port = 8765
# Instead, you can pass command-line arguments
# -h/--host [IP] -p/--port [PORT]
# to put your server on a different IP/port.
threadNumber = 32
netID = "yf263"
class ConnectionHandler:
"""Handles a single client request"""
def __init__(self, socket, WriteMonitor):
self.socket = socket
self.writer = WriteMonitor
self.incomingMessage = ""
self.expectedCommand = "CLOSED"
self.email = {}
self.email['to'] = []
def handle(self):
# update the expected command so that it's ready to handle msg
self.expectedCommand = "HELO"
self.sendAndReset("220 " + netID + " SMTP CS4410MP3\r\n")
while (self.expectedCommand != "CLOSED"):
if (self.expectedCommand == "HELO"):
self.HELO()
elif (self.expectedCommand == "MAIL"):
self.MAIL()
elif (self.expectedCommand == "RCPT"):
self.RCPT()
elif (self.expectedCommand == "DATA"):
self.DATA()
elif (self.expectedCommand == "WRITE"):
self.WRITE()
self.socket.close()
def getSocketMessage(self, token):
while self.incomingMessage.find(token) == -1:
try:
self.incomingMessage = self.incomingMessage + self.socket.recv(500)
except:
self.expectedCommand = "CLOSED"
self.sendMessage("421 4.4.2 "+ netID +" Error: timeout exceeded\r\n")
return
index = self.incomingMessage.find(token);
msg = self.incomingMessage[0:index]
self.incomingMessage = self.incomingMessage[(index + len(token)):]
return msg
def HELO(self):
message = self.getSocketMessage("\r\n")
if (message != None):
tokens = message.split()
if (len(tokens) == 0):
# empty command
self.sendMessage("502 5.5.2 Error: command not recognized\r\n")
else:
if (tokens[0].upper() == "HELO"):
if (len(tokens) == 2):
# correct command syntax, update accordingly
self.email["clientHostName"] = tokens[1]
self.expectedCommand = "MAIL"
self.sendAndReset("250 " + netID + "\r\n")
else:
# command starts with HELO but wrong agurments
self.sendMessage("501 Syntax: HELO clientHostName\r\n")
elif (tokens[0].upper() == "MAIL" or tokens[0].upper() == "RCPT"
or tokens[0].upper() == "DATA"):
# command not in the right order
self.sendMessage("503 Error: need HELO command\r\n")
else:
# un-recognized command
self.sendMessage("502 5.5.2 Error: command not recognized\r\n")
def MAIL(self):
message = self.getSocketMessage("\r\n")
if (message != None):
tokens = message.split()
if (len(tokens) == 0):
# empty command
self.sendMessage("502 5.5.2 Error: command not recognized\r\n")
else:
if (tokens[0].upper() == "HELO"):
self.sendMessage("503 Error: duplicate HELO\r\n")
elif (tokens[0].upper() == "MAIL"):
if (len(tokens) == 1):
# only contain MAIL, no following command/argument
self.sendMessage("501 Syntax: MAIL FROM:email@host\r\n")
elif (tokens[1].upper().find("FROM") == -1):
self.sendMessage("501 Syntax: MAIL FROM:email@host\r\n")
else:
# at least found "MAIL FROM"
if ("from" in self.email):
# duplicate MAIL command
self.sendMessage("503 5.5.1 Error: nested MAIL command\r\n")
else:
colonIdx = message.find(":")
address = message[colonIdx + 1 : ].strip()
if (address.find(" ") == -1 and address.find("@") > 0 and address.find("@") < len(address) - 1):
# no whitespace, and there are content before @ and after @
self.email['from'] = address
self.expectedCommand = "RCPT"
self.sendAndReset("250 2.1.0 OK\r\n")
else:
self.sendMessage("504 5.5.2 <" + address +">: Sender address rejected\r\n")
elif (tokens[0].upper() == "RCPT" or tokens[0].upper() == "DATA"):
# command not in the right order
self.sendMessage("503 Error: need MAIL command\r\n")
else:
# un-recognized command
self.sendMessage("502 5.5.2 Error: command not recognized\r\n")
def RCPT(self):
message = self.getSocketMessage("\r\n")
if (message != None):
tokens = message.split()
if (len(tokens) == 0):
# empty command
self.sendMessage("502 5.5.2 Error: command not recognized\r\n")
else:
if (tokens[0].upper() == "HELO"):
self.sendMessage("503 Error: duplicate HELO\r\n")
elif (tokens[0].upper() == "MAIL"):
self.sendMessage("503 5.5.1 Error: nested MAIL command\r\n")
elif (tokens[0].upper() == "RCPT"):
if (len(tokens) == 1):
# only contain RCPT, no following command/argument
self.sendMessage("501 Syntax: RCPT TO:email@host\r\n")
elif (tokens[1].upper().find("TO") == -1):
self.sendMessage("501 Syntax: RCPT TO:email@host\r\n")
else:
# at least found "RCPT TO"
colonIdx = message.find(":")
address = message[colonIdx + 1 : ].strip()
if (address.find(" ") == -1 and address.find("@") > 0 and address.find("@") < len(address) - 1):
# no whitespace, and there are content before @ and after @
self.email['to'].append(address)
self.sendAndReset("250 2.1.5 OK\r\n")
else:
self.sendMessage("504 5.5.2 <" + address +">: Recipient address invalid\r\n")
elif (tokens[0].upper() == "DATA"):
if (len(self.email['to']) == 0):
self.sendMessage("503 Error: need RCPT TO command\r\n")
else:
if (len(tokens) > 1):
self.sendMessage("501 Syntax: DATA\r\n")
else:
self.expectedCommand = "DATA"
self.sendMessage("354 End data with <CR><LF>.<CR><LF>\r\n")
else:
self.sendMessage("502 5.5.2 Error: command not recognized\r\n")
def DATA(self):
message = self.getSocketMessage("\r\n.\r\n")
if (message is not None):
self.email['content'] = message
self.expectedCommand = "WRITE"
else:
self.expectedCommand = "CLOSED"
def WRITE(self):
msgNum = self.writer.writeToFile(self.email)
self.sendAndReset("250 OK: delivered message " + str(msgNum) + "\r\n")
self.expectedCommand = "MAIL"
self.email['to'] = []
self.email.pop("from")
self.email.pop("content")
def sendAndReset(self, content):
self.sendMessage(content)
self.socket.settimeout(10.0)
def sendMessage(self, msg):
try:
self.socket.send(msg.encode('utf-8'))
except socket.error:
self.expectedCommand == "CLOSED"
# self.socket.close()
class ThreadPool:
def __init__(self):
# self.queue = []
self.lock = Lock()
self.waitForTask = Condition(self.lock)
self.waitForThread = Condition(self.lock)
self.task = None
def enqueue(self, clientsocket):
with self.lock:
while (self.task is not None):
self.waitForThread.wait()
self.task = clientsocket
self.waitForTask.notify()
def dequeue(self):
with self.lock:
while (self.task is None):
self.waitForTask.wait()
item = self.task
self.task = None
self.waitForThread.notify()
return item
class Consumer(Thread):
def __init__(self, threadPool, writePool):
Thread.__init__(self)
self.threadPool = threadPool
self.writePool = writePool
def run(self):
while True:
handler = ConnectionHandler(self.threadPool.dequeue(), self.writePool)
handler.handle()
class WriteMonitor():
def __init__(self):
self.lock = Lock()
self.toWrite = Condition(self.lock)
self.toBackup = Condition(self.lock)
self.isWriting = False
self.isBackup = False
self.emailNum = 1
def writeToFile(self, email):
"""Return the message number"""
# wait while back up or other thread is writing
with self.lock:
while (self.isWriting or self.isBackup):
self.toWrite.wait()
self.isWriting = True
mailbox = 0
if self.emailNum == 1:
mailbox = open("mailbox","w")
else:
mailbox = open("mailbox","a")
mailbox.write("Received: from " + email['clientHostName'] + " by " + netID + " (CS4410MP3)\n")
mailbox.write("Number: " + str(self.emailNum) + "\n")
mailbox.write("From: " + email['from'] + "\n")
for recipient in email['to']:
mailbox.write("To: " + recipient + "\n")
mailbox.write("\n")
mailbox.write(email['content'] + "\n")
mailbox.write("\n")
mailbox.close()
self.emailNum += 1
# self.isWriting = False
if (self.emailNum > 1 and self.emailNum % 32 == 1):
self.isBackup = True
self.isWriting = False
self.toBackup.notify()
else:
self.isWriting = False
self.toWrite.notify()
return self.emailNum - 1
def writeToBackUp(self):
# wait while writing or less than 32 emails
with self.lock:
while (self.isWriting or self.emailNum % 32 != 1 or self.isBackup == False):
self.toBackup.wait()
shutil.copyfile("mailbox", "mailbox." + str(self.emailNum - 32) + "-" + str(self.emailNum - 1))
with open("mailbox","w") as mailbox:
mailbox.write("")
self.isBackup = False
self.toWrite.notify()
class BackupWriter(Thread):
def __init__(self, writeMonitor):
Thread.__init__(self)
self.writer = writeMonitor
def run(self):
while True:
self.writer.writeToBackUp()
def serverloop():
"""The main server loop"""
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# mark the socket so we can rebind quickly to this port number
# after the socket is closed
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# bind the socket to the local loopback IP address and special port
serversocket.bind((host, port))
# start listening with a backlog of 5 connections
serversocket.listen(5)
threadPool = ThreadPool()
writePool = WriteMonitor()
BackupWriter(writePool).start()
for i in range(threadNumber):
Consumer(threadPool, writePool).start()
while True:
# accept a connection
(clientsocket, address) = serversocket.accept()
#ct = ConnectionHandler(clientsocket)
#ct.handle()
threadPool.enqueue(clientsocket)
# DO NOT CHANGE BELOW THIS LINE
opts, args = getopt.getopt(sys.argv[1:], 'h:p:', ['host=', 'port='])
for k, v in opts:
if k in ('-h', '--host'):
host = v
if k in ('-p', '--port'):
port = int(v)
print("Server coming up on %s:%i" % (host, port))
serverloop()