-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncommingThread.py
76 lines (57 loc) · 2.26 KB
/
IncommingThread.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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 3 13:04:14 2016
@author: markus
"""
import mailbox
from MGFilter import MGFilter
def IncommingThread (conn, REPLY, KNOWN):
incomming = dict (data = False, rcpt = None, mlfr = None, msg = "", mldr = "")
conn.send (REPLY["ready"])
while True:
data = conn.recv (1024)
if not data:
break
data = data.decode()
if incomming["data"]:
incomming["msg"] += data
# last line is only a . ? data transfer is finished
# tias
# stop = data.split("\n")[-1].strip()
if data.split("\n")[-1].strip() == "." or data.split("\n")[-2].strip() == ".":
# call filter Module
if MGFilter(incomming):
md = mailbox.Maildir(incomming["mldr"], factory = None, create = True)
md.add(incomming["msg"])
incomming["data"] = False
elif data.find("DATA") == 0:
# only accept data when rcpt and mlfr are known
if incomming["rcpt"] is not None and incomming["mlfr"] is not None:
incomming["data"] = True
conn.send (REPLY["start"])
else:
break
elif data.find("QUIT") == 0:
break
elif data.find("RCPT TO:") == 0:
rcpt = data.split("RCPT TO:")[1].strip()
# if RCPT is unknown, just quit connection immediately :)
if rcpt in KNOWN:
incomming["mldr"] = KNOWN[rcpt]
incomming["rcpt"] = rcpt
conn.send (REPLY["ok"])
else:
break
elif data.find("HELO ") == 0 or data.find("EHLO") == 0:
# HELO blabla, trust no one
conn.send (REPLY["ok"])
elif data.find("MAIL FROM:") == 0:
mlfr = data.split("MAIL FROM:")[1].strip()
incomming["mlfr"] = mlfr
conn.send (REPLY["ok"])
else:
# dunno what you want...bye!
break
conn.send (REPLY["close"])
conn.close ()
return