From: W. Trevor King Date: Fri, 26 Mar 2010 11:00:44 +0000 (-0400) Subject: Fixed up IncomingEmailDispatcher to work in real life ;). X-Git-Tag: 0.1~23 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=aea74558cfceccd79bd1028134cfb382c801481b;p=pyrisk.git Fixed up IncomingEmailDispatcher to work in real life ;). --- diff --git a/pyrisk/player/email.py b/pyrisk/player/email.py index 630c83a..988f151 100644 --- a/pyrisk/player/email.py +++ b/pyrisk/player/email.py @@ -43,7 +43,8 @@ SENDMAIL = None class IncomingEmailDispatcher (object): """For reading reply messages. """ - def __init__(self, fifo_path=None): + def __init__(self, fifo_path=None, verbose=True): + self.verbose = verbose self._cache = [] if fifo_path == None: self.dir_path = tempfile.mkdtemp(suffix='.pyrisk') @@ -57,8 +58,6 @@ class IncomingEmailDispatcher (object): if self.dir_path != None: os.rmdir(self.dir_path) def get(self, tag): - # FIFO blocks on open until a writer also opens - self.fifo = open(self.fifo_path, 'r') for msg_tag, msg in self._cache: if msg_tag == tag: self._cache.remove(msg) @@ -69,7 +68,8 @@ class IncomingEmailDispatcher (object): self._cache.append((msg_tag, msg)) msg = self._get_msg() msg_tag = self._msg_tag(msg['Subject']) - self.fifo.close() + if verbose == True: + print >> sys.stderr, msg return msg def _msg_tag(self, subject): """ Return the tag portion of a message subject. @@ -83,6 +83,8 @@ class IncomingEmailDispatcher (object): u'[t]' >>> ied.close() """ + if subject == None: + return None subject = subject.strip() if subject.startswith('Re:'): subject = subject[len('Re:'):] @@ -92,7 +94,10 @@ class IncomingEmailDispatcher (object): return None return args[0]+u']' def _get_msg(self): + # FIFO blocks on open until a writer also opens + self.fifo = open(self.fifo_path, 'r') text = self.fifo.read() + self.fifo.close() p = Parser() return p.parsestr(text) @@ -233,10 +238,17 @@ class EmailPlayer (Player): def _get_mail(self, tag): msg = self.incoming.get(tag) msg_charset = msg.get_content_charset('utf-8') - first_part = [self.msg.walk()][0] - body = first_part.get_payload(decode=True) - charset = first_part.get_content_charset(msg_charset) - mime_type = first_part.get_content_type() + if msg.is_multipart(): + for part in msg.walk(): + mime_type = part.get_content_type() + if mime_type == 'text/plain': + break + body = part.get_payload(decode=True) + charset = part.get_content_charset(msg_charset) + else: + body = msg.get_payload(decode=True) + charset = msg_charset + mime_type = msg.get_content_type() if not mime_type.startswith('text/plain'): raise PlayerError('Invalid MIME type %s (must be text/plain)' % mime_type)