Code

#614188 ] Exception in mailgw.py
[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 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 #
19 __doc__ = '''
20 An e-mail gateway for Roundup.
22 Incoming messages are examined for multiple parts:
23  . In a multipart/mixed message or part, each subpart is extracted and
24    examined. The text/plain subparts are assembled to form the textual
25    body of the message, to be stored in the file associated with a "msg"
26    class node. Any parts of other types are each stored in separate files
27    and given "file" class nodes that are linked to the "msg" node. 
28  . In a multipart/alternative message or part, we look for a text/plain
29    subpart and ignore the other parts.
31 Summary
32 -------
33 The "summary" property on message nodes is taken from the first non-quoting
34 section in the message body. The message body is divided into sections by
35 blank lines. Sections where the second and all subsequent lines begin with
36 a ">" or "|" character are considered "quoting sections". The first line of
37 the first non-quoting section becomes the summary of the message. 
39 Addresses
40 ---------
41 All of the addresses in the To: and Cc: headers of the incoming message are
42 looked up among the user nodes, and the corresponding users are placed in
43 the "recipients" property on the new "msg" node. The address in the From:
44 header similarly determines the "author" property of the new "msg"
45 node. The default handling for addresses that don't have corresponding
46 users is to create new users with no passwords and a username equal to the
47 address. (The web interface does not permit logins for users with no
48 passwords.) If we prefer to reject mail from outside sources, we can simply
49 register an auditor on the "user" class that prevents the creation of user
50 nodes with no passwords. 
52 Actions
53 -------
54 The subject line of the incoming message is examined to determine whether
55 the message is an attempt to create a new item or to discuss an existing
56 item. A designator enclosed in square brackets is sought as the first thing
57 on the subject line (after skipping any "Fwd:" or "Re:" prefixes). 
59 If an item designator (class name and id number) is found there, the newly
60 created "msg" node is added to the "messages" property for that item, and
61 any new "file" nodes are added to the "files" property for the item. 
63 If just an item class name is found there, we attempt to create a new item
64 of that class with its "messages" property initialized to contain the new
65 "msg" node and its "files" property initialized to contain any new "file"
66 nodes. 
68 Triggers
69 --------
70 Both cases may trigger detectors (in the first case we are calling the
71 set() method to add the message to the item's spool; in the second case we
72 are calling the create() method to create a new node). If an auditor raises
73 an exception, the original message is bounced back to the sender with the
74 explanatory message given in the exception. 
76 $Id: mailgw.py,v 1.90 2002-09-25 05:13:34 richard Exp $
77 '''
79 import string, re, os, mimetools, cStringIO, smtplib, socket, binascii, quopri
80 import time, random, sys
81 import traceback, MimeWriter
82 import hyperdb, date, password
84 SENDMAILDEBUG = os.environ.get('SENDMAILDEBUG', '')
86 class MailGWError(ValueError):
87     pass
89 class MailUsageError(ValueError):
90     pass
92 class MailUsageHelp(Exception):
93     pass
95 class Unauthorized(Exception):
96     """ Access denied """
98 def initialiseSecurity(security):
99     ''' Create some Permissions and Roles on the security object
101         This function is directly invoked by security.Security.__init__()
102         as a part of the Security object instantiation.
103     '''
104     security.addPermission(name="Email Registration",
105         description="Anonymous may register through e-mail")
106     p = security.addPermission(name="Email Access",
107         description="User may use the email interface")
108     security.addPermissionToRole('Admin', p)
110 class Message(mimetools.Message):
111     ''' subclass mimetools.Message so we can retrieve the parts of the
112         message...
113     '''
114     def getPart(self):
115         ''' Get a single part of a multipart message and return it as a new
116             Message instance.
117         '''
118         boundary = self.getparam('boundary')
119         mid, end = '--'+boundary, '--'+boundary+'--'
120         s = cStringIO.StringIO()
121         while 1:
122             line = self.fp.readline()
123             if not line:
124                 break
125             if line.strip() in (mid, end):
126                 break
127             s.write(line)
128         if not s.getvalue().strip():
129             return None
130         s.seek(0)
131         return Message(s)
133 subject_re = re.compile(r'(?P<refwd>\s*\W?\s*(fwd|re|aw)\s*\W?\s*)*'
134     r'\s*(\[(?P<classname>[^\d\s]+)(?P<nodeid>\d+)?\])?'
135     r'\s*(?P<title>[^[]+)?(\[(?P<args>.+?)\])?', re.I)
137 class MailGW:
138     def __init__(self, instance, db):
139         self.instance = instance
140         self.db = db
142         # should we trap exceptions (normal usage) or pass them through
143         # (for testing)
144         self.trapExceptions = 1
146     def do_pipe(self):
147         ''' Read a message from standard input and pass it to the mail handler.
148         '''
149         self.main(sys.stdin)
150         return 0
152     def do_mailbox(self, filename):
153         ''' Read a series of messages from the specified unix mailbox file and
154             pass each to the mail handler.
155         '''
156         # open the spool file and lock it
157         import fcntl, FCNTL
158         f = open(filename, 'r+')
159         fcntl.flock(f.fileno(), FCNTL.LOCK_EX)
161         # handle and clear the mailbox
162         try:
163             from mailbox import UnixMailbox
164             mailbox = UnixMailbox(f, factory=Message)
165             # grab one message
166             message = mailbox.next()
167             while message:
168                 # handle this message
169                 self.handle_Message(message)
170                 message = mailbox.next()
171             # nuke the file contents
172             os.ftruncate(f.fileno(), 0)
173         except:
174             import traceback
175             traceback.print_exc()
176             return 1
177         fcntl.flock(f.fileno(), FCNTL.LOCK_UN)
178         return 0
180     def do_pop(self, server, user='', password=''):
181         '''Read a series of messages from the specified POP server.
182         '''
183         import getpass, poplib, socket
184         try:
185             if not user:
186                 user = raw_input(_('User: '))
187             if not password:
188                 password = getpass.getpass()
189         except (KeyboardInterrupt, EOFError):
190             # Ctrl C or D maybe also Ctrl Z under Windows.
191             print "\nAborted by user."
192             return 1
194         # open a connection to the server and retrieve all messages
195         try:
196             server = poplib.POP3(server)
197         except socket.error, message:
198             print "POP server error:", message
199             return 1
200         server.user(user)
201         server.pass_(password)
202         numMessages = len(server.list()[1])
203         for i in range(1, numMessages+1):
204             # retr: returns 
205             # [ pop response e.g. '+OK 459 octets',
206             #   [ array of message lines ],
207             #   number of octets ]
208             lines = server.retr(i)[1]
209             s = cStringIO.StringIO('\n'.join(lines))
210             s.seek(0)
211             self.handle_Message(Message(s))
212             # delete the message
213             server.dele(i)
215         # quit the server to commit changes.
216         server.quit()
217         return 0
219     def main(self, fp):
220         ''' fp - the file from which to read the Message.
221         '''
222         return self.handle_Message(Message(fp))
224     def handle_Message(self, message):
225         '''Handle an RFC822 Message
227         Handle the Message object by calling handle_message() and then cope
228         with any errors raised by handle_message.
229         This method's job is to make that call and handle any
230         errors in a sane manner. It should be replaced if you wish to
231         handle errors in a different manner.
232         '''
233         # in some rare cases, a particularly stuffed-up e-mail will make
234         # its way into here... try to handle it gracefully
235         sendto = message.getaddrlist('from')
236         if sendto:
237             if not self.trapExceptions:
238                 return self.handle_message(message)
239             try:
240                 return self.handle_message(message)
241             except MailUsageHelp:
242                 # bounce the message back to the sender with the usage message
243                 fulldoc = '\n'.join(string.split(__doc__, '\n')[2:])
244                 sendto = [sendto[0][1]]
245                 m = ['']
246                 m.append('\n\nMail Gateway Help\n=================')
247                 m.append(fulldoc)
248                 m = self.bounce_message(message, sendto, m,
249                     subject="Mail Gateway Help")
250             except MailUsageError, value:
251                 # bounce the message back to the sender with the usage message
252                 fulldoc = '\n'.join(string.split(__doc__, '\n')[2:])
253                 sendto = [sendto[0][1]]
254                 m = ['']
255                 m.append(str(value))
256                 m.append('\n\nMail Gateway Help\n=================')
257                 m.append(fulldoc)
258                 m = self.bounce_message(message, sendto, m)
259             except Unauthorized, value:
260                 # just inform the user that he is not authorized
261                 sendto = [sendto[0][1]]
262                 m = ['']
263                 m.append(str(value))
264                 m = self.bounce_message(message, sendto, m)
265             except:
266                 # bounce the message back to the sender with the error message
267                 sendto = [sendto[0][1], self.instance.config.ADMIN_EMAIL]
268                 m = ['']
269                 m.append('An unexpected error occurred during the processing')
270                 m.append('of your message. The tracker administrator is being')
271                 m.append('notified.\n')
272                 m.append('----  traceback of failure  ----')
273                 s = cStringIO.StringIO()
274                 import traceback
275                 traceback.print_exc(None, s)
276                 m.append(s.getvalue())
277                 m = self.bounce_message(message, sendto, m)
278         else:
279             # very bad-looking message - we don't even know who sent it
280             sendto = [self.instance.config.ADMIN_EMAIL]
281             m = ['Subject: badly formed message from mail gateway']
282             m.append('')
283             m.append('The mail gateway retrieved a message which has no From:')
284             m.append('line, indicating that it is corrupt. Please check your')
285             m.append('mail gateway source. Failed message is attached.')
286             m.append('')
287             m = self.bounce_message(message, sendto, m,
288                 subject='Badly formed message from mail gateway')
290         # now send the message
291         if SENDMAILDEBUG:
292             open(SENDMAILDEBUG, 'w').write('From: %s\nTo: %s\n%s\n'%(
293                 self.instance.config.ADMIN_EMAIL, ', '.join(sendto),
294                     m.getvalue()))
295         else:
296             try:
297                 smtp = smtplib.SMTP(self.instance.config.MAILHOST)
298                 smtp.sendmail(self.instance.config.ADMIN_EMAIL, sendto,
299                     m.getvalue())
300             except socket.error, value:
301                 raise MailGWError, "Couldn't send error email: "\
302                     "mailhost %s"%value
303             except smtplib.SMTPException, value:
304                 raise MailGWError, "Couldn't send error email: %s"%value
306     def bounce_message(self, message, sendto, error,
307             subject='Failed issue tracker submission'):
308         ''' create a message that explains the reason for the failed
309             issue submission to the author and attach the original
310             message.
311         '''
312         msg = cStringIO.StringIO()
313         writer = MimeWriter.MimeWriter(msg)
314         writer.addheader('Subject', subject)
315         writer.addheader('From', '%s <%s>'% (self.instance.config.TRACKER_NAME,
316             self.instance.config.TRACKER_EMAIL))
317         writer.addheader('To', ','.join(sendto))
318         writer.addheader('MIME-Version', '1.0')
319         part = writer.startmultipartbody('mixed')
320         part = writer.nextpart()
321         body = part.startbody('text/plain')
322         body.write('\n'.join(error))
324         # reconstruct the original message
325         m = cStringIO.StringIO()
326         w = MimeWriter.MimeWriter(m)
327         # default the content_type, just in case...
328         content_type = 'text/plain'
329         # add the headers except the content-type
330         for header in message.headers:
331             header_name = header.split(':')[0]
332             if header_name.lower() == 'content-type':
333                 content_type = message.getheader(header_name)
334             elif message.getheader(header_name):
335                 w.addheader(header_name, message.getheader(header_name))
336         # now attach the message body
337         body = w.startbody(content_type)
338         try:
339             message.rewindbody()
340         except IOError:
341             body.write("*** couldn't include message body: read from pipe ***")
342         else:
343             body.write(message.fp.read())
345         # attach the original message to the returned message
346         part = writer.nextpart()
347         part.addheader('Content-Disposition','attachment')
348         part.addheader('Content-Description','Message you sent')
349         part.addheader('Content-Transfer-Encoding', '7bit')
350         body = part.startbody('message/rfc822')
351         body.write(m.getvalue())
353         writer.lastpart()
354         return msg
356     def get_part_data_decoded(self,part):
357         encoding = part.getencoding()
358         data = None
359         if encoding == 'base64':
360             # BUG: is base64 really used for text encoding or
361             # are we inserting zip files here. 
362             data = binascii.a2b_base64(part.fp.read())
363         elif encoding == 'quoted-printable':
364             # the quopri module wants to work with files
365             decoded = cStringIO.StringIO()
366             quopri.decode(part.fp, decoded)
367             data = decoded.getvalue()
368         elif encoding == 'uuencoded':
369             data = binascii.a2b_uu(part.fp.read())
370         else:
371             # take it as text
372             data = part.fp.read()
373         return data
375     def handle_message(self, message):
376         ''' message - a Message instance
378         Parse the message as per the module docstring.
379         '''
380         # handle the subject line
381         subject = message.getheader('subject', '')
383         if subject.strip() == 'help':
384             raise MailUsageHelp
386         m = subject_re.match(subject)
388         # check for well-formed subject line
389         if m:
390             # get the classname
391             classname = m.group('classname')
392             if classname is None:
393                 # no classname, fallback on the default
394                 if hasattr(self.instance.config, 'MAIL_DEFAULT_CLASS') and \
395                         self.instance.config.MAIL_DEFAULT_CLASS:
396                     classname = self.instance.config.MAIL_DEFAULT_CLASS
397                 else:
398                     # fail
399                     m = None
401         if not m:
402             raise MailUsageError, '''
403 The message you sent to roundup did not contain a properly formed subject
404 line. The subject must contain a class name or designator to indicate the
405 "topic" of the message. For example:
406     Subject: [issue] This is a new issue
407       - this will create a new issue in the tracker with the title "This is
408         a new issue".
409     Subject: [issue1234] This is a followup to issue 1234
410       - this will append the message's contents to the existing issue 1234
411         in the tracker.
413 Subject was: "%s"
414 '''%subject
416         # get the class
417         try:
418             cl = self.db.getclass(classname)
419         except KeyError:
420             raise MailUsageError, '''
421 The class name you identified in the subject line ("%s") does not exist in the
422 database.
424 Valid class names are: %s
425 Subject was: "%s"
426 '''%(classname, ', '.join(self.db.getclasses()), subject)
428         # get the optional nodeid
429         nodeid = m.group('nodeid')
431         # title is optional too
432         title = m.group('title')
433         if title:
434             title = title.strip()
435         else:
436             title = ''
438         # but we do need either a title or a nodeid...
439         if nodeid is None and not title:
440             raise MailUsageError, '''
441 I cannot match your message to a node in the database - you need to either
442 supply a full node identifier (with number, eg "[issue123]" or keep the
443 previous subject title intact so I can match that.
445 Subject was: "%s"
446 '''%subject
448         # If there's no nodeid, check to see if this is a followup and
449         # maybe someone's responded to the initial mail that created an
450         # entry. Try to find the matching nodes with the same title, and
451         # use the _last_ one matched (since that'll _usually_ be the most
452         # recent...)
453         if nodeid is None and m.group('refwd'):
454             l = cl.stringFind(title=title)
455             if l:
456                 nodeid = l[-1]
458         # if a nodeid was specified, make sure it's valid
459         if nodeid is not None and not cl.hasnode(nodeid):
460             raise MailUsageError, '''
461 The node specified by the designator in the subject of your message ("%s")
462 does not exist.
464 Subject was: "%s"
465 '''%(nodeid, subject)
467         #
468         # handle the users
469         #
470         # Don't create users if anonymous isn't allowed to register
471         create = 1
472         anonid = self.db.user.lookup('anonymous')
473         if not self.db.security.hasPermission('Email Registration', anonid):
474             create = 0
476         # ok, now figure out who the author is - create a new user if the
477         # "create" flag is true
478         author = uidFromAddress(self.db, message.getaddrlist('from')[0],
479             create=create)
481         # no author? means we're not author
482         if not author:
483             raise Unauthorized, '''
484 You are not a registered user.
486 Unknown address: %s
487 '''%message.getaddrlist('from')[0][1]
489         # make sure the author has permission to use the email interface
490         if not self.db.security.hasPermission('Email Access', author):
491             raise Unauthorized, 'You are not permitted to access this tracker.'
493         # make sure they're allowed to edit this class of information
494         if not self.db.security.hasPermission('Edit', author, classname):
495             raise Unauthorized, 'You are not permitted to edit %s.'%classname
497         # the author may have been created - make sure the change is
498         # committed before we reopen the database
499         self.db.commit()
501         # reopen the database as the author
502         username = self.db.user.get(author, 'username')
503         self.db.close()
504         self.db = self.instance.open(username)
506         # re-get the class with the new database connection
507         cl = self.db.getclass(classname)
509         # now update the recipients list
510         recipients = []
511         tracker_email = self.instance.config.TRACKER_EMAIL.lower()
512         for recipient in message.getaddrlist('to') + message.getaddrlist('cc'):
513             r = recipient[1].strip().lower()
514             if r == tracker_email or not r:
515                 continue
517             # look up the recipient - create if necessary (and we're
518             # allowed to)
519             recipient = uidFromAddress(self.db, recipient, create)
521             # if all's well, add the recipient to the list
522             if recipient:
523                 recipients.append(recipient)
525         #
526         # extract the args
527         #
528         subject_args = m.group('args')
530         #
531         # handle the subject argument list
532         #
533         # figure what the properties of this Class are
534         properties = cl.getprops()
535         props = {}
536         args = m.group('args')
537         if args:
538             errors = []
539             for prop in string.split(args, ';'):
540                 # extract the property name and value
541                 try:
542                     propname, value = prop.split('=')
543                 except ValueError, message:
544                     errors.append('not of form [arg=value,'
545                         'value,...;arg=value,value...]')
546                     break
548                 # ensure it's a valid property name
549                 propname = propname.strip()
550                 try:
551                     proptype =  properties[propname]
552                 except KeyError:
553                     errors.append('refers to an invalid property: '
554                         '"%s"'%propname)
555                     continue
557                 # convert the string value to a real property value
558                 if isinstance(proptype, hyperdb.String):
559                     props[propname] = value.strip()
560                 if isinstance(proptype, hyperdb.Password):
561                     props[propname] = password.Password(value.strip())
562                 elif isinstance(proptype, hyperdb.Date):
563                     try:
564                         props[propname] = date.Date(value.strip())
565                     except ValueError, message:
566                         errors.append('contains an invalid date for '
567                             '%s.'%propname)
568                 elif isinstance(proptype, hyperdb.Interval):
569                     try:
570                         props[propname] = date.Interval(value)
571                     except ValueError, message:
572                         errors.append('contains an invalid date interval'
573                             'for %s.'%propname)
574                 elif isinstance(proptype, hyperdb.Link):
575                     linkcl = self.db.classes[proptype.classname]
576                     propkey = linkcl.labelprop(default_to_id=1)
577                     try:
578                         props[propname] = linkcl.lookup(value)
579                     except KeyError, message:
580                         errors.append('"%s" is not a value for %s.'%(value,
581                             propname))
582                 elif isinstance(proptype, hyperdb.Multilink):
583                     # get the linked class
584                     linkcl = self.db.classes[proptype.classname]
585                     propkey = linkcl.labelprop(default_to_id=1)
586                     if nodeid:
587                         curvalue = cl.get(nodeid, propname)
588                     else:
589                         curvalue = []
591                     # handle each add/remove in turn
592                     # keep an extra list for all items that are
593                     # definitely in the new list (in case of e.g.
594                     # <propname>=A,+B, which should replace the old
595                     # list with A,B)
596                     set = 0
597                     newvalue = []
598                     for item in value.split(','):
599                         item = item.strip()
601                         # handle +/-
602                         remove = 0
603                         if item.startswith('-'):
604                             remove = 1
605                             item = item[1:]
606                         elif item.startswith('+'):
607                             item = item[1:]
608                         else:
609                             set = 1
611                         # look up the value
612                         try:
613                             item = linkcl.lookup(item)
614                         except KeyError, message:
615                             errors.append('"%s" is not a value for %s.'%(item,
616                                 propname))
617                             continue
619                         # perform the add/remove
620                         if remove:
621                             try:
622                                 curvalue.remove(item)
623                             except ValueError:
624                                 errors.append('"%s" is not currently in '
625                                     'for %s.'%(item, propname))
626                                 continue
627                         else:
628                             newvalue.append(item)
629                             if item not in curvalue:
630                                 curvalue.append(item)
632                     # that's it, set the new Multilink property value,
633                     # or overwrite it completely
634                     if set:
635                         props[propname] = newvalue
636                     else:
637                         props[propname] = curvalue
638                 elif isinstance(proptype, hyperdb.Boolean):
639                     value = value.strip()
640                     props[propname] = value.lower() in ('yes', 'true', 'on', '1')
641                 elif isinstance(proptype, hyperdb.Number):
642                     value = value.strip()
643                     props[propname] = int(value)
645             # handle any errors parsing the argument list
646             if errors:
647                 errors = '\n- '.join(errors)
648                 raise MailUsageError, '''
649 There were problems handling your subject line argument list:
650 - %s
652 Subject was: "%s"
653 '''%(errors, subject)
655         #
656         # handle message-id and in-reply-to
657         #
658         messageid = message.getheader('message-id')
659         inreplyto = message.getheader('in-reply-to') or ''
660         # generate a messageid if there isn't one
661         if not messageid:
662             messageid = "<%s.%s.%s%s@%s>"%(time.time(), random.random(),
663                 classname, nodeid, self.instance.config.MAIL_DOMAIN)
665         #
666         # now handle the body - find the message
667         #
668         content_type =  message.gettype()
669         attachments = []
670         # General multipart handling:
671         #   Take the first text/plain part, anything else is considered an 
672         #   attachment.
673         # multipart/mixed: multiple "unrelated" parts.
674         # multipart/signed (rfc 1847): 
675         #   The control information is carried in the second of the two 
676         #   required body parts.
677         #   ACTION: Default, so if content is text/plain we get it.
678         # multipart/encrypted (rfc 1847): 
679         #   The control information is carried in the first of the two 
680         #   required body parts.
681         #   ACTION: Not handleable as the content is encrypted.
682         # multipart/related (rfc 1872, 2112, 2387):
683         #   The Multipart/Related content-type addresses the MIME
684         #   representation of compound objects.
685         #   ACTION: Default. If we are lucky there is a text/plain.
686         #   TODO: One should use the start part and look for an Alternative
687         #   that is text/plain.
688         # multipart/Alternative (rfc 1872, 1892):
689         #   only in "related" ?
690         # multipart/report (rfc 1892):
691         #   e.g. mail system delivery status reports.
692         #   ACTION: Default. Could be ignored or used for Delivery Notification 
693         #   flagging.
694         # multipart/form-data:
695         #   For web forms only.
696         if content_type == 'multipart/mixed':
697             # skip over the intro to the first boundary
698             part = message.getPart()
699             content = None
700             while 1:
701                 # get the next part
702                 part = message.getPart()
703                 if part is None:
704                     break
705                 # parse it
706                 subtype = part.gettype()
707                 if subtype == 'text/plain' and not content:
708                     # The first text/plain part is the message content.
709                     content = self.get_part_data_decoded(part) 
710                 elif subtype == 'message/rfc822':
711                     # handle message/rfc822 specially - the name should be
712                     # the subject of the actual e-mail embedded here
713                     i = part.fp.tell()
714                     mailmess = Message(part.fp)
715                     name = mailmess.getheader('subject')
716                     part.fp.seek(i)
717                     attachments.append((name, 'message/rfc822', part.fp.read()))
718                 else:
719                     # try name on Content-Type
720                     name = part.getparam('name')
721                     # this is just an attachment
722                     data = self.get_part_data_decoded(part) 
723                     attachments.append((name, part.gettype(), data))
724             if content is None:
725                 raise MailUsageError, '''
726 Roundup requires the submission to be plain text. The message parser could
727 not find a text/plain part to use.
728 '''
730         elif content_type[:10] == 'multipart/':
731             # skip over the intro to the first boundary
732             message.getPart()
733             content = None
734             while 1:
735                 # get the next part
736                 part = message.getPart()
737                 if part is None:
738                     break
739                 # parse it
740                 if part.gettype() == 'text/plain' and not content:
741                     content = self.get_part_data_decoded(part) 
742             if content is None:
743                 raise MailUsageError, '''
744 Roundup requires the submission to be plain text. The message parser could
745 not find a text/plain part to use.
746 '''
748         elif content_type != 'text/plain':
749             raise MailUsageError, '''
750 Roundup requires the submission to be plain text. The message parser could
751 not find a text/plain part to use.
752 '''
754         else:
755             content = self.get_part_data_decoded(message) 
756  
757         # figure how much we should muck around with the email body
758         keep_citations = getattr(self.instance, 'EMAIL_KEEP_QUOTED_TEXT',
759             'no') == 'yes'
760         keep_body = getattr(self.instance, 'EMAIL_LEAVE_BODY_UNCHANGED',
761             'no') == 'yes'
763         # parse the body of the message, stripping out bits as appropriate
764         summary, content = parseContent(content, keep_citations, 
765             keep_body)
767         # 
768         # handle the attachments
769         #
770         files = []
771         for (name, mime_type, data) in attachments:
772             if not name:
773                 name = "unnamed"
774             files.append(self.db.file.create(type=mime_type, name=name,
775                 content=data))
777         # 
778         # create the message if there's a message body (content)
779         #
780         if content:
781             message_id = self.db.msg.create(author=author,
782                 recipients=recipients, date=date.Date('.'), summary=summary,
783                 content=content, files=files, messageid=messageid,
784                 inreplyto=inreplyto)
786             # attach the message to the node
787             if nodeid:
788                 # add the message to the node's list
789                 messages = cl.get(nodeid, 'messages')
790                 messages.append(message_id)
791                 props['messages'] = messages
792             else:
793                 # pre-load the messages list
794                 props['messages'] = [message_id]
796                 # set the title to the subject
797                 if properties.has_key('title') and not props.has_key('title'):
798                     props['title'] = title
800         #
801         # perform the node change / create
802         #
803         try:
804             if nodeid:
805                 cl.set(nodeid, **props)
806             else:
807                 nodeid = cl.create(**props)
808         except (TypeError, IndexError, ValueError), message:
809             raise MailUsageError, '''
810 There was a problem with the message you sent:
811    %s
812 '''%message
814         # commit the changes to the DB
815         self.db.commit()
817         return nodeid
819 def extractUserFromList(userClass, users):
820     '''Given a list of users, try to extract the first non-anonymous user
821        and return that user, otherwise return None
822     '''
823     if len(users) > 1:
824         for user in users:
825             # make sure we don't match the anonymous or admin user
826             if userClass.get(user, 'username') in ('admin', 'anonymous'):
827                 continue
828             # first valid match will do
829             return user
830         # well, I guess we have no choice
831         return user[0]
832     elif users:
833         return users[0]
834     return None
836 def uidFromAddress(db, address, create=1):
837     ''' address is from the rfc822 module, and therefore is (name, addr)
839         user is created if they don't exist in the db already
840     '''
841     (realname, address) = address
843     # try a straight match of the address
844     user = extractUserFromList(db.user, db.user.stringFind(address=address))
845     if user is not None: return user
847     # try the user alternate addresses if possible
848     props = db.user.getprops()
849     if props.has_key('alternate_addresses'):
850         users = db.user.filter(None, {'alternate_addresses': address},
851             [], [])
852         user = extractUserFromList(db.user, users)
853         if user is not None: return user
855     # try to match the username to the address (for local
856     # submissions where the address is empty)
857     user = extractUserFromList(db.user, db.user.stringFind(username=address))
859     # couldn't match address or username, so create a new user
860     if create:
861         return db.user.create(username=address, address=address,
862             realname=realname, roles=db.config.NEW_EMAIL_USER_ROLES)
863     else:
864         return 0
866 def parseContent(content, keep_citations, keep_body,
867         blank_line=re.compile(r'[\r\n]+\s*[\r\n]+'),
868         eol=re.compile(r'[\r\n]+'), 
869         signature=re.compile(r'^[>|\s]*[-_]+\s*$'),
870         original_message=re.compile(r'^[>|\s]*-----Original Message-----$')):
871     ''' The message body is divided into sections by blank lines.
872         Sections where the second and all subsequent lines begin with a ">"
873         or "|" character are considered "quoting sections". The first line of
874         the first non-quoting section becomes the summary of the message. 
876         If keep_citations is true, then we keep the "quoting sections" in the
877         content.
878         If keep_body is true, we even keep the signature sections.
879     '''
880     # strip off leading carriage-returns / newlines
881     i = 0
882     for i in range(len(content)):
883         if content[i] not in '\r\n':
884             break
885     if i > 0:
886         sections = blank_line.split(content[i:])
887     else:
888         sections = blank_line.split(content)
890     # extract out the summary from the message
891     summary = ''
892     l = []
893     for section in sections:
894         #section = section.strip()
895         if not section:
896             continue
897         lines = eol.split(section)
898         if (lines[0] and lines[0][0] in '>|') or (len(lines) > 1 and
899                 lines[1] and lines[1][0] in '>|'):
900             # see if there's a response somewhere inside this section (ie.
901             # no blank line between quoted message and response)
902             for line in lines[1:]:
903                 if line and line[0] not in '>|':
904                     break
905             else:
906                 # we keep quoted bits if specified in the config
907                 if keep_citations:
908                     l.append(section)
909                 continue
910             # keep this section - it has reponse stuff in it
911             if not summary:
912                 # and while we're at it, use the first non-quoted bit as
913                 # our summary
914                 summary = line
915             lines = lines[lines.index(line):]
916             section = '\n'.join(lines)
918         if not summary:
919             # if we don't have our summary yet use the first line of this
920             # section
921             summary = lines[0]
922         elif signature.match(lines[0]) and 2 <= len(lines) <= 10:
923             # lose any signature
924             break
925         elif original_message.match(lines[0]):
926             # ditch the stupid Outlook quoting of the entire original message
927             break
929         # and add the section to the output
930         l.append(section)
932     # Now reconstitute the message content minus the bits we don't care
933     # about.
934     if not keep_body:
935         content = '\n\n'.join(l)
937     return summary, content
939 # vim: set filetype=python ts=4 sw=4 et si