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')
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)
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.
u'[t]'
>>> ied.close()
"""
+ if subject == None:
+ return None
subject = subject.strip()
if subject.startswith('Re:'):
subject = subject[len('Re:'):]
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)
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)