This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemaillistener.py
241 lines (193 loc) · 6.89 KB
/
emaillistener.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
# This file is part of Heapkeeper.
#
# Heapkeeper is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Heapkeeper is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Heapkeeper. If not, see <http://www.gnu.org/licenses/>.
# Copyright (C) 2012 Attila Nagy
# Copyright (C) 2012 Csaba Hoch
import asyncore
import base64
import datetime
import email
import email.header
import quopri
import re
import smtpd
import time
import multiprocessing
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.shortcuts import render, redirect
from hk.models import *
##### SMTP server to receive mail
email_thread = None
def normalize_str(s):
s = re.sub(r'\r\n', r'\n', s) # Windows EOL
s = re.sub(r'\xc2\xa0', ' ', s) # Non-breaking space
return s
def utf8(s, charset):
if charset is not None:
return s.decode(charset).encode('utf-8')
else:
return s
class FakeSMTPServer(smtpd.DebuggingServer):
def __init__(*args, **kwargs):
smtpd.SMTPServer.__init__(*args, **kwargs)
def process_message(self, peer, mailfrom, rcpttos, data):
print 'Received mail from %s to %s.' % (mailfrom, rcpttos,)
mail = email.message_from_string(data)
subject = email.header.decode_header(mail['Subject'])[0][0]
message_id = email.header.decode_header(mail['Message-ID'])[0][0]
in_reply_to = email.header.decode_header(mail['In-Reply-To'])[0][0]
text = mail.get_payload()
encoding = mail['Content-Transfer-Encoding']
if encoding != None:
if encoding.lower() in ('7bit', '8bit', 'binary'):
pass # no conversion needed
elif encoding.lower() == 'base64':
text = base64.b64decode(text)
elif encoding.lower() == 'quoted-printable':
text = quopri.decodestring(text)
else:
print('WARNING: Unknown encoding, skipping decoding: '
'%s\n'
'text:\n%s\n' % (encoding, text))
charset = mail.get_content_charset()
text = utf8(text, charset)
text = normalize_str(text)
message_from_mail(mailfrom, rcpttos,
subject, message_id, in_reply_to,
text)
def server_thread(port=25):
smtp_server = FakeSMTPServer(('0.0.0.0', port), None)
asyncore.loop()
def run_server_thread(port):
global email_thread
if email_thread is not None and email_thread.is_alive():
print "STMP thread already running!"
return
email_thread = multiprocessing.Process(target=server_thread,
args=(int(port),))
email_thread.start()
print "STMP thread started."
def stop_server_thread():
email_thread.terminate()
##### Mail to message
# This is function is almost verbatim from the first generation Hk
def parse_subject(subject):
"""Parses the subject of an email.
Parses the labels and removes the "Re:" prefix and whitespaces.
**Argument:**
- `subject` (str)
**Returns:** (str, [str]) -- The remaining subject and the labels.
"""
# last_bracket==None <=> we are outside of a [label]
last_bracket = None
brackets = []
i = 0
while i < len(subject):
c = subject[i]
if c == '[' and last_bracket == None:
last_bracket = i
elif c == ']' and last_bracket != None:
brackets.append((last_bracket, i))
last_bracket = None
elif c != ' ' and last_bracket == None:
break
i += 1
real_subject = subject[i:]
if re.match('[Rr]e:', subject):
subject = subject[3:]
real_subject = real_subject.strip()
labels = [ subject[first+1:last].strip() for first, last in brackets ]
return real_subject, labels
def message_from_mail(mailfrom, rcpttos,
subject, message_id, in_reply_to,
text):
# TODO Add access control!!!
# TODO Should cross posting be allowed?
heaps = []
for rcpt in rcpttos:
heapname = re.search('^([^@].*)@', rcpt).group(1)
try:
heap = Heap.objects.get(short_name=heapname)
heaps.append(heap)
except Heap.DoesNotExist:
print '%s attempted to post to nonexistent heap "%s".' % (mailfrom, heapname)
for heap in heaps:
# Cross-posting is enabled for now
now = datetime.datetime.now()
try:
r = re.compile('[-._A-Za-z0-9]+@[-._A-Za-z0-9]+')
match = r.search(mailfrom)
if match is not None:
sender_mail = match.group(0)
author = User.objects.get(email=sender_mail)
else:
author = None
except User.DoesNotExist:
author = None
try:
parent = Message.objects.get(message_id=in_reply_to)
except Message.DoesNotExist:
parent = None
real_subject, labels = parse_subject(subject)
msg = Message()
msg.message_id = message_id
msg.save()
mv = MessageVersion(
message=msg,
author=author,
creation_date=now,
version_date=now,
parent=parent,
text=text
)
mv.save()
if parent is None:
conv = Conversation(
heap=heap,
subject=real_subject,
root_message=msg
)
conv.save()
label_target = conv
else:
label_target = msg
label_target.add_label(labels)
##### "smtp" views
def smtp(request):
# Must be admin to use
global email_thread
if not request.user.is_superuser:
raise PermissionDenied
if email_thread is None:
alive = False
else:
alive = email_thread.is_alive()
return render(
request,
'smtp.html',
{'running': alive})
def enable_smtp(request, port=25):
# Must be admin to use
if not request.user.is_superuser:
raise PermissionDenied
run_server_thread(port)
return redirect(reverse('hk.views.smtp'))
def disable_smtp(request):
# Must be admin to use
global email_thread
if not request.user.is_superuser:
raise PermissionDenied
stop_server_thread()
return redirect(reverse('hk.views.smtp'))