Code

missing import ... how odd
[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.88 2002-09-20 23:20:57 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 = self.instance.open(username)
505         # re-get the class with the new database connection
506         cl = self.db.getclass(classname)
508         # now update the recipients list
509         recipients = []
510         tracker_email = self.instance.config.TRACKER_EMAIL.lower()
511         for recipient in message.getaddrlist('to') + message.getaddrlist('cc'):
512             r = recipient[1].strip().lower()
513             if r == tracker_email or not r:
514                 continue
516             # look up the recipient - create if necessary (and we're
517             # allowed to)
518             recipient = uidFromAddress(self.db, recipient, create)
520             # if all's well, add the recipient to the list
521             if recipient:
522                 recipients.append(recipient)
524         #
525         # extract the args
526         #
527         subject_args = m.group('args')
529         #
530         # handle the subject argument list
531         #
532         # figure what the properties of this Class are
533         properties = cl.getprops()
534         props = {}
535         args = m.group('args')
536         if args:
537             errors = []
538             for prop in string.split(args, ';'):
539                 # extract the property name and value
540                 try:
541                     propname, value = prop.split('=')
542                 except ValueError, message:
543                     errors.append('not of form [arg=value,'
544                         'value,...;arg=value,value...]')
545                     break
547                 # ensure it's a valid property name
548                 propname = propname.strip()
549                 try:
550                     proptype =  properties[propname]
551                 except KeyError:
552                     errors.append('refers to an invalid property: '
553                         '"%s"'%propname)
554                     continue
556                 # convert the string value to a real property value
557                 if isinstance(proptype, hyperdb.String):
558                     props[propname] = value.strip()
559                 if isinstance(proptype, hyperdb.Password):
560                     props[propname] = password.Password(value.strip())
561                 elif isinstance(proptype, hyperdb.Date):
562                     try:
563                         props[propname] = date.Date(value.strip())
564                     except ValueError, message:
565                         errors.append('contains an invalid date for '
566                             '%s.'%propname)
567                 elif isinstance(proptype, hyperdb.Interval):
568                     try:
569                         props[propname] = date.Interval(value)
570                     except ValueError, message:
571                         errors.append('contains an invalid date interval'
572                             'for %s.'%propname)
573                 elif isinstance(proptype, hyperdb.Link):
574                     linkcl = self.db.classes[proptype.classname]
575                     propkey = linkcl.labelprop(default_to_id=1)
576                     try:
577                         props[propname] = linkcl.lookup(value)
578                     except KeyError, message:
579                         errors.append('"%s" is not a value for %s.'%(value,
580                             propname))
581                 elif isinstance(proptype, hyperdb.Multilink):
582                     # get the linked class
583                     linkcl = self.db.classes[proptype.classname]
584                     propkey = linkcl.labelprop(default_to_id=1)
585                     if nodeid:
586                         curvalue = cl.get(nodeid, propname)
587                     else:
588                         curvalue = []
590                     # handle each add/remove in turn
591                     # keep an extra list for all items that are
592                     # definitely in the new list (in case of e.g.
593                     # <propname>=A,+B, which should replace the old
594                     # list with A,B)
595                     set = 0
596                     newvalue = []
597                     for item in value.split(','):
598                         item = item.strip()
600                         # handle +/-
601                         remove = 0
602                         if item.startswith('-'):
603                             remove = 1
604                             item = item[1:]
605                         elif item.startswith('+'):
606                             item = item[1:]
607                         else:
608                             set = 1
610                         # look up the value
611                         try:
612                             item = linkcl.lookup(item)
613                         except KeyError, message:
614                             errors.append('"%s" is not a value for %s.'%(item,
615                                 propname))
616                             continue
618                         # perform the add/remove
619                         if remove:
620                             try:
621                                 curvalue.remove(item)
622                             except ValueError:
623                                 errors.append('"%s" is not currently in '
624                                     'for %s.'%(item, propname))
625                                 continue
626                         else:
627                             newvalue.append(item)
628                             if item not in curvalue:
629                                 curvalue.append(item)
631                     # that's it, set the new Multilink property value,
632                     # or overwrite it completely
633                     if set:
634                         props[propname] = newvalue
635                     else:
636                         props[propname] = curvalue
637                 elif isinstance(proptype, hyperdb.Boolean):
638                     value = value.strip()
639                     props[propname] = value.lower() in ('yes', 'true', 'on', '1')
640                 elif isinstance(proptype, hyperdb.Number):
641                     value = value.strip()
642                     props[propname] = int(value)
644             # handle any errors parsing the argument list
645             if errors:
646                 errors = '\n- '.join(errors)
647                 raise MailUsageError, '''
648 There were problems handling your subject line argument list:
649 - %s
651 Subject was: "%s"
652 '''%(errors, subject)
654         #
655         # handle message-id and in-reply-to
656         #
657         messageid = message.getheader('message-id')
658         inreplyto = message.getheader('in-reply-to') or ''
659         # generate a messageid if there isn't one
660         if not messageid:
661             messageid = "<%s.%s.%s%s@%s>"%(time.time(), random.random(),
662                 classname, nodeid, self.instance.config.MAIL_DOMAIN)
664         #
665         # now handle the body - find the message
666         #
667         content_type =  message.gettype()
668         attachments = []
669         # General multipart handling:
670         #   Take the first text/plain part, anything else is considered an 
671         #   attachment.
672         # multipart/mixed: multiple "unrelated" parts.
673         # multipart/signed (rfc 1847): 
674         #   The control information is carried in the second of the two 
675         #   required body parts.
676         #   ACTION: Default, so if content is text/plain we get it.
677         # multipart/encrypted (rfc 1847): 
678         #   The control information is carried in the first of the two 
679         #   required body parts.
680         #   ACTION: Not handleable as the content is encrypted.
681         # multipart/related (rfc 1872, 2112, 2387):
682         #   The Multipart/Related content-type addresses the MIME
683         #   representation of compound objects.
684         #   ACTION: Default. If we are lucky there is a text/plain.
685         #   TODO: One should use the start part and look for an Alternative
686         #   that is text/plain.
687         # multipart/Alternative (rfc 1872, 1892):
688         #   only in "related" ?
689         # multipart/report (rfc 1892):
690         #   e.g. mail system delivery status reports.
691         #   ACTION: Default. Could be ignored or used for Delivery Notification 
692         #   flagging.
693         # multipart/form-data:
694         #   For web forms only.
695         if content_type == 'multipart/mixed':
696             # skip over the intro to the first boundary
697             part = message.getPart()
698             content = None
699             while 1:
700                 # get the next part
701                 part = message.getPart()
702                 if part is None:
703                     break
704                 # parse it
705                 subtype = part.gettype()
706                 if subtype == 'text/plain' and not content:
707                     # The first text/plain part is the message content.
708                     content = self.get_part_data_decoded(part) 
709                 elif subtype == 'message/rfc822':
710                     # handle message/rfc822 specially - the name should be
711                     # the subject of the actual e-mail embedded here
712                     i = part.fp.tell()
713                     mailmess = Message(part.fp)
714                     name = mailmess.getheader('subject')
715                     part.fp.seek(i)
716                     attachments.append((name, 'message/rfc822', part.fp.read()))
717                 else:
718                     # try name on Content-Type
719                     name = part.getparam('name')
720                     # this is just an attachment
721                     data = self.get_part_data_decoded(part) 
722                     attachments.append((name, part.gettype(), data))
723             if content is None:
724                 raise MailUsageError, '''
725 Roundup requires the submission to be plain text. The message parser could
726 not find a text/plain part to use.
727 '''
729         elif content_type[:10] == 'multipart/':
730             # skip over the intro to the first boundary
731             message.getPart()
732             content = None
733             while 1:
734                 # get the next part
735                 part = message.getPart()
736                 if part is None:
737                     break
738                 # parse it
739                 if part.gettype() == 'text/plain' and not content:
740                     content = self.get_part_data_decoded(part) 
741             if content is None:
742                 raise MailUsageError, '''
743 Roundup requires the submission to be plain text. The message parser could
744 not find a text/plain part to use.
745 '''
747         elif content_type != 'text/plain':
748             raise MailUsageError, '''
749 Roundup requires the submission to be plain text. The message parser could
750 not find a text/plain part to use.
751 '''
753         else:
754             content = self.get_part_data_decoded(message) 
755  
756         # figure how much we should muck around with the email body
757         keep_citations = getattr(self.instance, 'EMAIL_KEEP_QUOTED_TEXT',
758             'no') == 'yes'
759         keep_body = getattr(self.instance, 'EMAIL_LEAVE_BODY_UNCHANGED',
760             'no') == 'yes'
762         # parse the body of the message, stripping out bits as appropriate
763         summary, content = parseContent(content, keep_citations, 
764             keep_body)
766         # 
767         # handle the attachments
768         #
769         files = []
770         for (name, mime_type, data) in attachments:
771             if not name:
772                 name = "unnamed"
773             files.append(self.db.file.create(type=mime_type, name=name,
774                 content=data))
776         # 
777         # create the message if there's a message body (content)
778         #
779         if content:
780             message_id = self.db.msg.create(author=author,
781                 recipients=recipients, date=date.Date('.'), summary=summary,
782                 content=content, files=files, messageid=messageid,
783                 inreplyto=inreplyto)
785             # attach the message to the node
786             if nodeid:
787                 # add the message to the node's list
788                 messages = cl.get(nodeid, 'messages')
789                 messages.append(message_id)
790                 props['messages'] = messages
791             else:
792                 # pre-load the messages list
793                 props['messages'] = [message_id]
795                 # set the title to the subject
796                 if properties.has_key('title') and not props.has_key('title'):
797                     props['title'] = title
799         #
800         # perform the node change / create
801         #
802         try:
803             if nodeid:
804                 cl.set(nodeid, **props)
805             else:
806                 nodeid = cl.create(**props)
807         except (TypeError, IndexError, ValueError), message:
808             raise MailUsageError, '''
809 There was a problem with the message you sent:
810    %s
811 '''%message
813         # commit the changes to the DB
814         self.db.commit()
816         return nodeid
818 def extractUserFromList(userClass, users):
819     '''Given a list of users, try to extract the first non-anonymous user
820        and return that user, otherwise return None
821     '''
822     if len(users) > 1:
823         for user in users:
824             # make sure we don't match the anonymous or admin user
825             if userClass.get(user, 'username') in ('admin', 'anonymous'):
826                 continue
827             # first valid match will do
828             return user
829         # well, I guess we have no choice
830         return user[0]
831     elif users:
832         return users[0]
833     return None
835 def uidFromAddress(db, address, create=1):
836     ''' address is from the rfc822 module, and therefore is (name, addr)
838         user is created if they don't exist in the db already
839     '''
840     (realname, address) = address
842     # try a straight match of the address
843     user = extractUserFromList(db.user, db.user.stringFind(address=address))
844     if user is not None: return user
846     # try the user alternate addresses if possible
847     props = db.user.getprops()
848     if props.has_key('alternate_addresses'):
849         users = db.user.filter(None, {'alternate_addresses': address},
850             [], [])
851         user = extractUserFromList(db.user, users)
852         if user is not None: return user
854     # try to match the username to the address (for local
855     # submissions where the address is empty)
856     user = extractUserFromList(db.user, db.user.stringFind(username=address))
858     # couldn't match address or username, so create a new user
859     if create:
860         return db.user.create(username=address, address=address,
861             realname=realname, roles=db.config.NEW_EMAIL_USER_ROLES)
862     else:
863         return 0
865 def parseContent(content, keep_citations, keep_body,
866         blank_line=re.compile(r'[\r\n]+\s*[\r\n]+'),
867         eol=re.compile(r'[\r\n]+'), 
868         signature=re.compile(r'^[>|\s]*[-_]+\s*$'),
869         original_message=re.compile(r'^[>|\s]*-----Original Message-----$')):
870     ''' The message body is divided into sections by blank lines.
871         Sections where the second and all subsequent lines begin with a ">"
872         or "|" character are considered "quoting sections". The first line of
873         the first non-quoting section becomes the summary of the message. 
875         If keep_citations is true, then we keep the "quoting sections" in the
876         content.
877         If keep_body is true, we even keep the signature sections.
878     '''
879     # strip off leading carriage-returns / newlines
880     i = 0
881     for i in range(len(content)):
882         if content[i] not in '\r\n':
883             break
884     if i > 0:
885         sections = blank_line.split(content[i:])
886     else:
887         sections = blank_line.split(content)
889     # extract out the summary from the message
890     summary = ''
891     l = []
892     for section in sections:
893         #section = section.strip()
894         if not section:
895             continue
896         lines = eol.split(section)
897         if (lines[0] and lines[0][0] in '>|') or (len(lines) > 1 and
898                 lines[1] and lines[1][0] in '>|'):
899             # see if there's a response somewhere inside this section (ie.
900             # no blank line between quoted message and response)
901             for line in lines[1:]:
902                 if line[0] not in '>|':
903                     break
904             else:
905                 # we keep quoted bits if specified in the config
906                 if keep_citations:
907                     l.append(section)
908                 continue
909             # keep this section - it has reponse stuff in it
910             if not summary:
911                 # and while we're at it, use the first non-quoted bit as
912                 # our summary
913                 summary = line
914             lines = lines[lines.index(line):]
915             section = '\n'.join(lines)
917         if not summary:
918             # if we don't have our summary yet use the first line of this
919             # section
920             summary = lines[0]
921         elif signature.match(lines[0]) and 2 <= len(lines) <= 10:
922             # lose any signature
923             break
924         elif original_message.match(lines[0]):
925             # ditch the stupid Outlook quoting of the entire original message
926             break
928         # and add the section to the output
929         l.append(section)
931     # Now reconstitute the message content minus the bits we don't care
932     # about.
933     if not keep_body:
934         content = '\n\n'.join(l)
936     return summary, content
938 # vim: set filetype=python ts=4 sw=4 et si