9ce05937f54bc4c881116f90f5650421715247de
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 #
18 '''
19 An e-mail gateway for Roundup.
21 Incoming messages are examined for multiple parts:
22 . In a multipart/mixed message or part, each subpart is extracted and
23 examined. The text/plain subparts are assembled to form the textual
24 body of the message, to be stored in the file associated with a "msg"
25 class node. Any parts of other types are each stored in separate files
26 and given "file" class nodes that are linked to the "msg" node.
27 . In a multipart/alternative message or part, we look for a text/plain
28 subpart and ignore the other parts.
30 Summary
31 -------
32 The "summary" property on message nodes is taken from the first non-quoting
33 section in the message body. The message body is divided into sections by
34 blank lines. Sections where the second and all subsequent lines begin with
35 a ">" or "|" character are considered "quoting sections". The first line of
36 the first non-quoting section becomes the summary of the message.
38 Addresses
39 ---------
40 All of the addresses in the To: and Cc: headers of the incoming message are
41 looked up among the user nodes, and the corresponding users are placed in
42 the "recipients" property on the new "msg" node. The address in the From:
43 header similarly determines the "author" property of the new "msg"
44 node. The default handling for addresses that don't have corresponding
45 users is to create new users with no passwords and a username equal to the
46 address. (The web interface does not permit logins for users with no
47 passwords.) If we prefer to reject mail from outside sources, we can simply
48 register an auditor on the "user" class that prevents the creation of user
49 nodes with no passwords.
51 Actions
52 -------
53 The subject line of the incoming message is examined to determine whether
54 the message is an attempt to create a new item or to discuss an existing
55 item. A designator enclosed in square brackets is sought as the first thing
56 on the subject line (after skipping any "Fwd:" or "Re:" prefixes).
58 If an item designator (class name and id number) is found there, the newly
59 created "msg" node is added to the "messages" property for that item, and
60 any new "file" nodes are added to the "files" property for the item.
62 If just an item class name is found there, we attempt to create a new item
63 of that class with its "messages" property initialized to contain the new
64 "msg" node and its "files" property initialized to contain any new "file"
65 nodes.
67 Triggers
68 --------
69 Both cases may trigger detectors (in the first case we are calling the
70 set() method to add the message to the item's spool; in the second case we
71 are calling the create() method to create a new node). If an auditor raises
72 an exception, the original message is bounced back to the sender with the
73 explanatory message given in the exception.
75 $Id: mailgw.py,v 1.16 2001-10-05 02:23:24 richard Exp $
76 '''
79 import string, re, os, mimetools, cStringIO, smtplib, socket, binascii, quopri
80 import traceback
81 import hyperdb, date
83 class MailUsageError(ValueError):
84 pass
86 class Message(mimetools.Message):
87 ''' subclass mimetools.Message so we can retrieve the parts of the
88 message...
89 '''
90 def getPart(self):
91 ''' Get a single part of a multipart message and return it as a new
92 Message instance.
93 '''
94 boundary = self.getparam('boundary')
95 mid, end = '--'+boundary, '--'+boundary+'--'
96 s = cStringIO.StringIO()
97 while 1:
98 line = self.fp.readline()
99 if not line:
100 break
101 if line.strip() in (mid, end):
102 break
103 s.write(line)
104 if not s.getvalue().strip():
105 return None
106 s.seek(0)
107 return Message(s)
109 subject_re = re.compile(r'(\[?(fwd|re):\s*)*'
110 r'(\[(?P<classname>[^\d]+)(?P<nodeid>\d+)?\])'
111 r'(?P<title>[^\[]+)(\[(?P<args>.+?)\])?', re.I)
113 class MailGW:
114 def __init__(self, db):
115 self.db = db
117 def main(self, fp):
118 ''' fp - the file from which to read the Message.
120 Read a message from fp and then call handle_message() with the
121 result. This method's job is to make that call and handle any
122 errors in a sane manner. It should be replaced if you wish to
123 handle errors in a different manner.
124 '''
125 # ok, figure the subject, author, recipients and content-type
126 message = Message(fp)
127 m = []
128 try:
129 self.handle_message(message)
130 except MailUsageError, value:
131 # bounce the message back to the sender with the usage message
132 fulldoc = '\n'.join(string.split(__doc__, '\n')[2:])
133 sendto = [message.getaddrlist('from')[0][1]]
134 m = ['Subject: Failed issue tracker submission', '']
135 m.append(str(value))
136 m.append('\nMail Gateway Help\n=================')
137 m.append(fulldoc)
138 except:
139 # bounce the message back to the sender with the error message
140 sendto = [message.getaddrlist('from')[0][1]]
141 m = ['Subject: failed issue tracker submission']
142 m.append('')
143 # TODO as attachments?
144 m.append('---- traceback of failure ----')
145 s = cStringIO.StringIO()
146 import traceback
147 traceback.print_exc(None, s)
148 m.append(s.getvalue())
149 m.append('---- failed message follows ----')
150 try:
151 fp.seek(0)
152 except:
153 pass
154 m.append(fp.read())
155 if m:
156 try:
157 smtp = smtplib.SMTP(self.MAILHOST)
158 smtp.sendmail(self.ADMIN_EMAIL, sendto, '\n'.join(m))
159 except socket.error, value:
160 return "Couldn't send confirmation email: mailhost %s"%value
161 except smtplib.SMTPException, value:
162 return "Couldn't send confirmation email: %s"%value
164 def handle_message(self, message):
165 ''' message - a Message instance
167 Parse the message as per the module docstring.
168 '''
169 # handle the subject line
170 subject = message.getheader('subject', '')
171 m = subject_re.match(subject)
172 if not m:
173 raise MailUsageError, '''
174 The message you sent to roundup did not contain a properly formed subject
175 line. The subject must contain a class name or designator to indicate the
176 "topic" of the message. For example:
177 Subject: [issue] This is a new issue
178 - this will create a new issue in the tracker with the title "This is
179 a new issue".
180 Subject: [issue1234] This is a followup to issue 1234
181 - this will append the message's contents to the existing issue 1234
182 in the tracker.
184 Subject was: "%s"
185 '''%subject
186 classname = m.group('classname')
187 nodeid = m.group('nodeid')
188 title = m.group('title').strip()
189 subject_args = m.group('args')
190 try:
191 cl = self.db.getclass(classname)
192 except KeyError:
193 raise MailUsageError, '''
194 The class name you identified in the subject line ("%s") does not exist in the
195 database.
197 Valid class names are: %s
198 Subject was: "%s"
199 '''%(classname, ', '.join(self.db.getclasses()), subject)
200 properties = cl.getprops()
201 props = {}
202 args = m.group('args')
203 if args:
204 for prop in string.split(m.group('args'), ';'):
205 try:
206 key, value = prop.split('=')
207 except ValueError, message:
208 raise MailUsageError, '''
209 Subject argument list not of form [arg=value,value,...;arg=value,value...]
210 (specific exception message was "%s")
212 Subject was: "%s"
213 '''%(message, subject)
214 try:
215 type = properties[key]
216 except KeyError:
217 raise MailUsageError, '''
218 Subject argument list refers to an invalid property: "%s"
220 Subject was: "%s"
221 '''%(key, subject)
222 if isinstance(type, hyperdb.String):
223 props[key] = value
224 elif isinstance(type, hyperdb.Date):
225 props[key] = date.Date(value)
226 elif isinstance(type, hyperdb.Interval):
227 props[key] = date.Interval(value)
228 elif isinstance(type, hyperdb.Link):
229 props[key] = value
230 elif isinstance(type, hyperdb.Multilink):
231 props[key] = value.split(',')
233 #
234 # handle the users
235 #
236 author = self.db.uidFromAddress(message.getaddrlist('from')[0])
237 recipients = []
238 for recipient in message.getaddrlist('to') + message.getaddrlist('cc'):
239 if recipient[1].strip().lower() == self.ISSUE_TRACKER_EMAIL:
240 continue
241 recipients.append(self.db.uidFromAddress(recipient))
243 # now handle the body - find the message
244 content_type = message.gettype()
245 attachments = []
246 if content_type == 'multipart/mixed':
247 # skip over the intro to the first boundary
248 part = message.getPart()
249 content = None
250 while 1:
251 # get the next part
252 part = message.getPart()
253 if part is None:
254 break
255 # parse it
256 subtype = part.gettype()
257 if subtype == 'text/plain' and not content:
258 # add all text/plain parts to the message content
259 if content is None:
260 content = part.fp.read()
261 else:
262 content = content + part.fp.read()
264 elif subtype == 'message/rfc822':
265 # handle message/rfc822 specially - the name should be
266 # the subject of the actual e-mail embedded here
267 i = part.fp.tell()
268 mailmess = Message(part.fp)
269 name = mailmess.getheader('subject')
270 part.fp.seek(i)
271 attachments.append((name, 'message/rfc822', part.fp.read()))
273 else:
274 # try name on Content-Type
275 name = part.getparam('name')
276 # this is just an attachment
277 data = part.fp.read()
278 encoding = part.getencoding()
279 if encoding == 'base64':
280 data = binascii.a2b_base64(data)
281 elif encoding == 'quoted-printable':
282 data = quopri.decode(data)
283 elif encoding == 'uuencoded':
284 data = binascii.a2b_uu(data)
285 attachments.append((name, part.gettype(), data))
287 if content is None:
288 raise MailUsageError, '''
289 Roundup requires the submission to be plain text. The message parser could
290 not find a text/plain part o use.
291 '''
293 elif content_type[:10] == 'multipart/':
294 # skip over the intro to the first boundary
295 message.getPart()
296 content = None
297 while 1:
298 # get the next part
299 part = message.getPart()
300 if part is None:
301 break
302 # parse it
303 if part.gettype() == 'text/plain' and not content:
304 # this one's our content
305 content = part.fp.read()
306 if content is None:
307 raise MailUsageError, '''
308 Roundup requires the submission to be plain text. The message parser could
309 not find a text/plain part o use.
310 '''
312 elif content_type != 'text/plain':
313 raise MailUsageError, '''
314 Roundup requires the submission to be plain text. The message parser could
315 not find a text/plain part o use.
316 '''
318 else:
319 content = message.fp.read()
321 summary, content = parseContent(content)
323 # handle the files
324 files = []
325 for (name, type, data) in attachments:
326 files.append(self.db.file.create(type=type, name=name,
327 content=data))
329 # now handle the db stuff
330 if nodeid:
331 # If an item designator (class name and id number) is found there,
332 # the newly created "msg" node is added to the "messages" property
333 # for that item, and any new "file" nodes are added to the "files"
334 # property for the item.
335 message_id = self.db.msg.create(author=author,
336 recipients=recipients, date=date.Date('.'), summary=summary,
337 content=content, files=files)
338 try:
339 messages = cl.get(nodeid, 'messages')
340 except IndexError:
341 raise MailUsageError, '''
342 The node specified by the designator in the subject of your message ("%s")
343 does not exist.
345 Subject was: "%s"
346 '''%(nodeid, subject)
347 messages.append(message_id)
348 props['messages'] = messages
349 cl.set(nodeid, **props)
350 else:
351 # If just an item class name is found there, we attempt to create a
352 # new item of that class with its "messages" property initialized to
353 # contain the new "msg" node and its "files" property initialized to
354 # contain any new "file" nodes.
355 message_id = self.db.msg.create(author=author,
356 recipients=recipients, date=date.Date('.'), summary=summary,
357 content=content, files=files)
358 # fill out the properties with defaults where required
359 if properties.has_key('assignedto') and \
360 not props.has_key('assignedto'):
361 props['assignedto'] = '1' # "admin"
362 if properties.has_key('status') and not props.has_key('status'):
363 props['status'] = '1' # "unread"
364 if properties.has_key('title') and not props.has_key('title'):
365 props['title'] = title
366 props['messages'] = [message_id]
367 props['nosy'] = recipients[:]
368 props['nosy'].append(author)
369 props['nosy'].sort()
370 nodeid = cl.create(**props)
372 def parseContent(content, blank_line=re.compile(r'[\r\n]+\s*[\r\n]+'),
373 eol=re.compile(r'[\r\n]+'), signature=re.compile(r'^[>|\s]*[-_]+\s*$')):
374 ''' The message body is divided into sections by blank lines.
375 Sections where the second and all subsequent lines begin with a ">" or "|"
376 character are considered "quoting sections". The first line of the first
377 non-quoting section becomes the summary of the message.
378 '''
379 sections = blank_line.split(content)
380 # extract out the summary from the message
381 summary = ''
382 l = []
383 for section in sections:
384 section = section.strip()
385 if not section:
386 continue
387 lines = eol.split(section)
388 if lines[0] and lines[0][0] in '>|':
389 continue
390 if len(lines) > 1 and lines[1] and lines[1][0] in '>|':
391 continue
392 if not summary:
393 summary = lines[0]
394 l.append(section)
395 continue
396 if signature.match(lines[0]):
397 break
398 l.append(section)
399 return summary, '\n'.join(l)
401 #
402 # $Log: not supported by cvs2svn $
403 # Revision 1.15 2001/08/30 06:01:17 richard
404 # Fixed missing import in mailgw :(
405 #
406 # Revision 1.14 2001/08/13 23:02:54 richard
407 # Make the mail parser a little more robust.
408 #
409 # Revision 1.13 2001/08/12 06:32:36 richard
410 # using isinstance(blah, Foo) now instead of isFooType
411 #
412 # Revision 1.12 2001/08/08 01:27:00 richard
413 # Added better error handling to mailgw.
414 #
415 # Revision 1.11 2001/08/08 00:08:03 richard
416 # oops ;)
417 #
418 # Revision 1.10 2001/08/07 00:24:42 richard
419 # stupid typo
420 #
421 # Revision 1.9 2001/08/07 00:15:51 richard
422 # Added the copyright/license notice to (nearly) all files at request of
423 # Bizar Software.
424 #
425 # Revision 1.8 2001/08/05 07:06:07 richard
426 # removed some print statements
427 #
428 # Revision 1.7 2001/08/03 07:18:22 richard
429 # Implemented correct mail splitting (was taking a shortcut). Added unit
430 # tests. Also snips signatures now too.
431 #
432 # Revision 1.6 2001/08/01 04:24:21 richard
433 # mailgw was assuming certain properties existed on the issues being created.
434 #
435 # Revision 1.5 2001/07/29 07:01:39 richard
436 # Added vim command to all source so that we don't get no steenkin' tabs :)
437 #
438 # Revision 1.4 2001/07/28 06:43:02 richard
439 # Multipart message class has the getPart method now. Added some tests for it.
440 #
441 # Revision 1.3 2001/07/28 00:34:34 richard
442 # Fixed some non-string node ids.
443 #
444 # Revision 1.2 2001/07/22 12:09:32 richard
445 # Final commit of Grande Splite
446 #
447 #
448 # vim: set filetype=python ts=4 sw=4 et si