From a4c905eaa4b4a22091708b3712a5b4c9a07a6f5e Mon Sep 17 00:00:00 2001 From: Naveen Nathan Date: Fri, 25 Oct 2013 01:47:40 +1100 Subject: [PATCH] Fetch request for ENVELOPE returns an Envelope(...) namedtuple. Backwards compatibility remains with the previous parsed response. --- imapclient/response_parser.py | 38 +++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 0adfc09..92fb91d 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -16,12 +16,14 @@ from __future__ import unicode_literals import sys from collections import defaultdict from datetime import datetime +#from email.utils import parsedate_tz from . import six xrange = six.moves.xrange from .fixed_offset import FixedOffset from .response_lexer import TokenSource +from .response_types import * try: import imaplib2 as imaplib @@ -103,6 +105,8 @@ def parse_fetch_response(text, normalise_times=True, uid_is_key=True): msg_data[word] = uid elif word == 'INTERNALDATE': msg_data[word] = _convert_INTERNALDATE(value, normalise_times) + elif word == 'ENVELOPE': + msg_data[word] = _convert_ENVELOPE(value, normalise_times) elif word in ('BODY', 'BODYSTRUCTURE'): msg_data[word] = BodyData.create(value) else: @@ -168,6 +172,40 @@ def _convert_INTERNALDATE(date_string, normalise_times=True): return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None) return dt +def _convert_ENVELOPE(envelope_response, normalise_times=True): + # XXX: the following commented lines is for parsing the RFC2822 string + # into a datatime structure, but it might be easier (at least for now) + # to remain as a string. + + #parsed_dt = parsedate_tz(envelope_response[0]) + #if parsed_dt == None: + # raise ValueError("couldn't parse date %r" % envelope_response[0]) + + #dt = datetime(*parsed_dt[0:6], tzinfo=FixedOffset(parsed_dt[-1])) + ## this is really difficult to figure out -- sigh python + ##if normalise_times: + ## # Normalise to host system's timezone + ## return dt.astimezone(FixedOffset.for_system()).replace(tzinfo=None) + + # stick with using a string for backwards compatibility + dt = envelope_response[0] + subject = envelope_response[1] + + # addresses contains a tuple of addresses + # from, sender, reply_to, to, cc, bcc headers + addresses = [] + for addr_list in envelope_response[2:8]: + addrs = [] + if addr_list: + for addr_tuple in addr_list: + addrs.append(Address(*addr_tuple)) + addresses.append(tuple(addrs)) + + in_reply_to = envelope_response[8] + message_id = envelope_response[9] + + return Envelope(dt, subject, *addresses, \ + In_Reply_To=in_reply_to, Message_Id=message_id) def atom(src, token): if token == '(': -- 2.43.0