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
|
import re
import functools
from django.core.files.uploadedfile import SimpleUploadedFile
from django.conf import settings as django_settings
from django.template import Context
from django.utils.translation import ugettext as _
from lamson.routing import route, stateless
from lamson.server import Relay
from askbot.models import ReplyAddress, Tag
from askbot.utils import mail
from askbot.conf import settings as askbot_settings
from askbot.skins.loaders import get_template
#we might end up needing to use something like this
#to distinguish the reply text from the quoted original message
"""
def _strip_message_qoute(message_text):
import re
result = message_text
pattern = "(?P<qoute>" + \
"On ([a-zA-Z0-9, :/<>@\.\"\[\]]* wrote:.*)|" + \
"From: [\w@ \.]* \[mailto:[\w\.]*@[\w\.]*\].*|" + \
"From: [\w@ \.]*(\n|\r\n)+Sent: [\*\w@ \.,:/]*(\n|\r\n)+To:.*(\n|\r\n)+.*|" + \
"[- ]*Forwarded by [\w@ \.,:/]*.*|" + \
"From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Date: [\w@ \.<>\-:,]*\n.*|" + \
"From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Sent: [\*\w@ \.,:/]*(\n|\r\n).*|" + \
"From: [\w@ \.<>\-]*(\n|\r\n)To: [\w@ \.<>\-]*(\n|\r\n)Subject:.*|" + \
"(-| )*Original Message(-| )*.*)"
groups = re.search(pattern, email_text, re.IGNORECASE + re.DOTALL)
qoute = None
if not groups is None:
if groups.groupdict().has_key("qoute"):
qoute = groups.groupdict()["qoute"]
if qoute:
result = reslut.split(qoute)[0]
#if the last line contains an email message remove that one too
lines = result.splitlines(True)
if re.search(r'[\w\.]*@[\w\.]*\].*', lines[-1]):
result = '\n'.join(lines[:-1])
return result
"""
def get_disposition(part):
"""return list of part's content dispositions
or an empty list
"""
dispositions = part.content_encoding.get('Content-Disposition', None)
if dispositions:
return dispositions[0]
else:
return list()
def get_attachment_info(part):
return part.content_encoding['Content-Disposition'][1]
def is_attachment(part):
"""True if part content disposition is
attachment"""
return get_disposition(part) == 'attachment'
def is_inline_attachment(part):
"""True if part content disposition is
inline"""
return get_disposition(part) == 'inline'
def format_attachment(part):
"""takes message part and turns it into SimpleUploadedFile object"""
att_info = get_attachment_info(part)
name = att_info.get('filename', None)
content_type = get_content_type(part)
return SimpleUploadedFile(name, part.body, content_type)
def get_content_type(part):
"""return content type of the message part"""
return part.content_encoding.get('Content-Type', (None,))[0]
def is_body(part):
"""True, if part is plain text and is not attachment"""
if get_content_type(part) == 'text/plain':
if not is_attachment(part):
return True
return False
def get_part_type(part):
if is_body(part):
return 'body'
elif is_attachment(part):
return 'attachment'
elif is_inline_attachment(part):
return 'inline'
def get_parts(message):
"""returns list of tuples (<part_type>, <formatted_part>),
where <part-type> is one of 'body', 'attachment', 'inline'
and <formatted-part> - will be in the directly usable form:
* if it is 'body' - then it will be unicode text
* for attachment - it will be django's SimpleUploadedFile instance
There may be multiple 'body' parts as well as others
usually the body is split when there are inline attachments present.
"""
parts = list()
simple_body = ''
if message.body():
simple_body = message.body()
parts.append(('body', simple_body))
for part in message.walk():
part_type = get_part_type(part)
if part_type == 'body':
part_content = part.body
if part_content == simple_body:
continue#avoid duplication
elif part_type in ('attachment', 'inline'):
part_content = format_attachment(part)
else:
continue
parts.append((part_type, part_content))
return parts
def process_reply(func):
@functools.wraps(func)
def wrapped(message, host = None, address = None):
"""processes forwarding rules, and run the handler
in the case of error, send a bounce email
"""
try:
for rule in django_settings.LAMSON_FORWARD:
if re.match(rule['pattern'], message.base['to']):
relay = Relay(host=rule['host'],
port=rule['port'], debug=1)
relay.deliver(message)
return
except AttributeError:
pass
error = None
try:
reply_address = ReplyAddress.objects.get(
address = address,
allowed_from_email = message.From
)
#here is the business part of this function
func(
from_address = message.From,
subject_line = message['Subject'],
parts = get_parts(message),
reply_address_object = reply_address
)
except ReplyAddress.DoesNotExist:
error = _("You were replying to an email address\
unknown to the system or you were replying from a different address from the one where you\
received the notification.")
except Exception, e:
import sys
sys.stderr.write(str(e))
import traceback
sys.stderr.write(traceback.format_exc())
if error is not None:
template = get_template('email/reply_by_email_error.html')
body_text = template.render(Context({'error':error}))
mail.send_mail(
subject_line = "Error posting your reply",
body_text = body_text,
recipient_list = [message.From],
)
return wrapped
@route('(addr)@(host)', addr = '.+')
@stateless
def ASK(message, host = None, addr = None):
"""lamson handler for asking by email,
to the forum in general and to a specific group"""
#we need to exclude some other emails by prefix
if addr.startswith('reply-'):
return
if addr.startswith('welcome-'):
return
parts = get_parts(message)
from_address = message.From
subject = message['Subject']#why lamson does not give it normally?
if addr == 'ask':
mail.process_emailed_question(from_address, subject, parts)
else:
if askbot_settings.GROUP_EMAIL_ADDRESSES_ENABLED == False:
return
try:
group_tag = Tag.group_tags.get(
deleted = False,
name__iexact = addr
)
mail.process_emailed_question(
from_address, subject, parts, tags = [group_tag.name, ]
)
except Tag.DoesNotExist:
#do nothing because this handler will match all emails
return
except Tag.MultipleObjectsReturned:
return
@route('welcome-(address)@(host)', address='.+')
@stateless
@process_reply
def VALIDATE_EMAIL(
parts = None,
reply_address_object = None,
from_address = None,
**kwargs
):
"""process the validation email and save
the email signature
todo: go a step further and
"""
content, stored_files = mail.process_parts(parts)
#save the signature and mark email as valid
reply_code = reply_address_object.address
user = reply_address_object.user
if reply_code in content:
user.email_signature = reply_address_object.extract_user_signature(
content
)
user.email_isvalid = True
user.save()
data = {
'site_name': askbot_settings.APP_SHORT_NAME,
'site_url': askbot_settings.APP_URL,
'ask_address': 'ask@' + askbot_settings.REPLY_BY_EMAIL_HOSTNAME
}
template = get_template('email/re_welcome_lamson_on.html')
mail.send_mail(
subject_line = _('Re: Welcome to %(site_name)s') % data,
body_text = template.render(Context(data)),
recipient_list = [from_address,]
)
else:
raise ValueError(
_(
'Please reply to the welcome email '
'without editing it'
)
)
@route('reply-(address)@(host)', address='.+')
@stateless
@process_reply
def PROCESS(
parts = None,
reply_address_object = None,
**kwargs
):
"""handler to process the emailed message
and make a post to askbot based on the contents of
the email, including the text body and the file attachments"""
if reply_address_object.was_used:
reply_address_object.edit_post(parts)
else:
reply_address_object.create_reply(parts)
|