Code

6ba384201701fb4cca1514a3e24c72f655948204
[roundup.git] / roundup / mailgw.py
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 THE 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.9 2001-08-07 00:15:51 richard Exp $
76 '''
79 import string, re, os, mimetools, cStringIO, smtplib, socket, binascii, quopri
80 import traceback
81 import date
83 class Message(mimetools.Message):
84     ''' subclass mimetools.Message so we can retrieve the parts of the
85         message...
86     '''
87     def getPart(self):
88         ''' Get a single part of a multipart message and return it as a new
89             Message instance.
90         '''
91         boundary = self.getparam('boundary')
92         mid, end = '--'+boundary, '--'+boundary+'--'
93         s = cStringIO.StringIO()
94         while 1:
95             line = self.fp.readline()
96             if not line:
97                 break
98             if line.strip() in (mid, end):
99                 break
100             s.write(line)
101         if not s.getvalue().strip():
102             return None
103         s.seek(0)
104         return Message(s)
106 subject_re = re.compile(r'(\[?(fwd|re):\s*)*'
107     r'(\[(?P<classname>[^\d]+)(?P<nodeid>\d+)?\])'
108     r'(?P<title>[^\[]+)(\[(?P<args>.+?)\])?', re.I)
110 class MailGW:
111     def __init__(self, db):
112         self.db = db
114     def main(self, fp):
115         ''' fp - the file from which to read the Message.
117         Read a message from fp and then call handle_message() with the
118         result. This method's job is to make that call and handle any
119         errors in a sane manner. It should be replaced if you wish to
120         handle errors in a different manner.
121         '''
122         # ok, figure the subject, author, recipients and content-type
123         message = Message(fp)
124         try:
125             self.handle_message(message)
126         except:
127             # bounce the message back to the sender with the error message
128             sendto = [message.getaddrlist('from')[0][1]]
129             m = ['Subject: failed issue tracker submission']
130             m.append('')
131             # TODO as attachments?
132             m.append('----  traceback of failure  ----')
133             s = cStringIO.StringIO()
134             import traceback
135             traceback.print_exc(None, s)
136             m.append(s.getvalue())
137             m.append('---- failed message follows ----')
138             try:
139                 fp.seek(0)
140             except:
141                 pass
142             m.append(fp.read())
143             try:
144                 smtp = smtplib.SMTP(self.MAILHOST)
145                 smtp.sendmail(self.ADMIN_EMAIL, sendto, '\n'.join(m))
146             except socket.error, value:
147                 return "Couldn't send confirmation email: mailhost %s"%value
148             except smtplib.SMTPException, value:
149                 return "Couldn't send confirmation email: %s"%value
151     def handle_message(self, message):
152         ''' message - a Message instance
154         Parse the message as per the module docstring.
155         '''
156         # handle the subject line
157         m = subject_re.match(message.getheader('subject'))
158         if not m:
159             raise ValueError, 'No [designator] found in subject "%s"'
160         classname = m.group('classname')
161         nodeid = m.group('nodeid')
162         title = m.group('title').strip()
163         subject_args = m.group('args')
164         cl = self.db.getclass(classname)
165         properties = cl.getprops()
166         props = {}
167         args = m.group('args')
168         if args:
169             for prop in string.split(m.group('args'), ';'):
170                 try:
171                     key, value = prop.split('=')
172                 except ValueError, message:
173                     raise ValueError, 'Args list not of form [arg=value,value,...;arg=value,value,value..]  (specific exception message was "%s")'%message
174                 type =  properties[key]
175                 if type.isStringType:
176                     props[key] = value 
177                 elif type.isDateType:
178                     props[key] = date.Date(value)
179                 elif type.isIntervalType:
180                     props[key] = date.Interval(value)
181                 elif type.isLinkType:
182                     props[key] = value
183                 elif type.isMultilinkType:
184                     props[key] = value.split(',')
186         # handle the users
187         author = self.db.uidFromAddress(message.getaddrlist('from')[0])
188         recipients = []
189         for recipient in message.getaddrlist('to') + message.getaddrlist('cc'):
190             if recipient[1].strip().lower() == self.ISSUE_TRACKER_EMAIL:
191                 continue
192             recipients.append(self.db.uidFromAddress(recipient))
194         # now handle the body - find the message
195         content_type =  message.gettype()
196         attachments = []
197         if content_type == 'multipart/mixed':
198             # skip over the intro to the first boundary
199             part = message.getPart()
200             content = None
201             while 1:
202                 # get the next part
203                 part = message.getPart()
204                 if part is None:
205                     break
206                 # parse it
207                 subtype = part.gettype()
208                 if subtype == 'text/plain' and not content:
209                     # add all text/plain parts to the message content
210                     if content is None:
211                         content = part.fp.read()
212                     else:
213                         content = content + part.fp.read()
215                 elif subtype == 'message/rfc822':
216                     # handle message/rfc822 specially - the name should be
217                     # the subject of the actual e-mail embedded here
218                     i = part.fp.tell()
219                     mailmess = Message(part.fp)
220                     name = mailmess.getheader('subject')
221                     part.fp.seek(i)
222                     attachments.append((name, 'message/rfc822', part.fp.read()))
224                 else:
225                     # try name on Content-Type
226                     name = part.getparam('name')
227                     # this is just an attachment
228                     data = part.fp.read()
229                     encoding = part.getencoding()
230                     if encoding == 'base64':
231                         data = binascii.a2b_base64(data)
232                     elif encoding == 'quoted-printable':
233                         data = quopri.decode(data)
234                     elif encoding == 'uuencoded':
235                         data = binascii.a2b_uu(data)
236                     attachments.append((name, part.gettype(), data))
238             if content is None:
239                 raise ValueError, 'No text/plain part found'
241         elif content_type[:10] == 'multipart/':
242             # skip over the intro to the first boundary
243             message.getPart()
244             content = None
245             while 1:
246                 # get the next part
247                 part = message.getPart()
248                 if part is None:
249                     break
250                 # parse it
251                 if part.gettype() == 'text/plain' and not content:
252                     # this one's our content
253                     content = part.fp.read()
254             if content is None:
255                 raise ValueError, 'No text/plain part found'
257         elif content_type != 'text/plain':
258             raise ValueError, 'No text/plain part found'
260         else:
261             content = message.fp.read()
263         summary, content = parseContent(content)
265         # handle the files
266         files = []
267         for (name, type, data) in attachments:
268             files.append(self.db.file.create(type=type, name=name,
269                 content=data))
271         # now handle the db stuff
272         if nodeid:
273             # If an item designator (class name and id number) is found there,
274             # the newly created "msg" node is added to the "messages" property
275             # for that item, and any new "file" nodes are added to the "files" 
276             # property for the item. 
277             message_id = self.db.msg.create(author=author,
278                 recipients=recipients, date=date.Date('.'), summary=summary,
279                 content=content, files=files)
280             messages = cl.get(nodeid, 'messages')
281             messages.append(message_id)
282             props['messages'] = messages
283             cl.set(nodeid, **props)
284         else:
285             # If just an item class name is found there, we attempt to create a
286             # new item of that class with its "messages" property initialized to
287             # contain the new "msg" node and its "files" property initialized to
288             # contain any new "file" nodes. 
289             message_id = self.db.msg.create(author=author,
290                 recipients=recipients, date=date.Date('.'), summary=summary,
291                 content=content, files=files)
292             # fill out the properties with defaults where required
293             if properties.has_key('assignedto') and \
294                     not props.has_key('assignedto'):
295                 props['assignedto'] = '1'             # "admin"
296             if properties.has_key('status') and not props.has_key('status'):
297                 props['status'] = '1'                 # "unread"
298             if properties.has_key('title') and not props.has_key('title'):
299                 props['title'] = title
300             props['messages'] = [message_id]
301             props['nosy'] = recipients[:]
302             props['nosy'].append(author)
303             props['nosy'].sort()
304             nodeid = cl.create(**props)
306 def parseContent(content, blank_line=re.compile(r'[\r\n]+\s*[\r\n]+'),
307         eol=re.compile(r'[\r\n]+'), signature=re.compile(r'^[>|\s]*[-_]+\s*$')):
308     ''' The message body is divided into sections by blank lines.
309     Sections where the second and all subsequent lines begin with a ">" or "|"
310     character are considered "quoting sections". The first line of the first
311     non-quoting section becomes the summary of the message. 
312     '''
313     sections = blank_line.split(content)
314     # extract out the summary from the message
315     summary = ''
316     l = []
317     for section in sections:
318         section = section.strip()
319         if not section:
320             continue
321         lines = eol.split(section)
322         if lines[0] and lines[0][0] in '>|':
323             continue
324         if len(lines) > 1 and lines[1] and lines[1][0] in '>|':
325             continue
326         if not summary:
327             summary = lines[0]
328             l.append(section)
329             continue
330         if signature.match(lines[0]):
331             break
332         l.append(section)
333     return summary, '\n'.join(l)
336 # $Log: not supported by cvs2svn $
337 # Revision 1.8  2001/08/05 07:06:07  richard
338 # removed some print statements
340 # Revision 1.7  2001/08/03 07:18:22  richard
341 # Implemented correct mail splitting (was taking a shortcut). Added unit
342 # tests. Also snips signatures now too.
344 # Revision 1.6  2001/08/01 04:24:21  richard
345 # mailgw was assuming certain properties existed on the issues being created.
347 # Revision 1.5  2001/07/29 07:01:39  richard
348 # Added vim command to all source so that we don't get no steenkin' tabs :)
350 # Revision 1.4  2001/07/28 06:43:02  richard
351 # Multipart message class has the getPart method now. Added some tests for it.
353 # Revision 1.3  2001/07/28 00:34:34  richard
354 # Fixed some non-string node ids.
356 # Revision 1.2  2001/07/22 12:09:32  richard
357 # Final commit of Grande Splite
360 # vim: set filetype=python ts=4 sw=4 et si