From dbc256b3c0dfc546af05121ead14579bcd5a6f5f Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Fri, 6 Jul 2012 14:56:15 +0200 Subject: [PATCH] Work around unsolicited IMAP server responses in imaplib --- src/mail_iterator.py | 18 ++++++++++++++++++ 1 files changed, 18 insertions(+), 0 deletions(-) diff --git a/src/mail_iterator.py b/src/mail_iterator.py index deae0bd..00d0ae8 100644 --- a/src/mail_iterator.py +++ b/src/mail_iterator.py @@ -80,6 +80,8 @@ class MailIterator: continue # retrieve uidvalidity try: + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('STATUS') _result, data = self.mail_con.status(mailbox[2], '(UIDVALIDITY)') except (self.mail_con.error): raise UserWarning("Could not retrieve mailbox uidvalidity.") @@ -96,6 +98,8 @@ class MailIterator: def fetch_messages(self): """Fetches the messages from the current mailbox, return list of uids.""" try: + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('SEARCH') _result, data = self.mail_con.uid('search', None, "ALL") except (self.mail_con.error): raise UserWarning("Could not fetch messages.") @@ -105,6 +109,8 @@ class MailIterator: def fetch_internal_date(self, mid): """Fetches the internal date of a message, returns a time tuple.""" try: + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('FETCH') _result, data = self.mail_con.uid('fetch', mid, '(INTERNALDATE)') except (self.mail_con.error): raise UserWarning("Could not fetch the internal date of message " + mid.decode('iso-8859-1') + ".") @@ -114,6 +120,8 @@ class MailIterator: def fetch_received_date(self, mid): """Fetches the received date of a message, returns bytes reponse.""" try: + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('FETCH') _result, data = self.mail_con.uid('fetch', mid, '(BODY.PEEK[HEADER.FIELDS (RECEIVED)])') except (self.mail_con.error): raise UserWarning("Could not fetch the received header of message " + mid.decode('iso-8859-1') + ".") @@ -122,6 +130,8 @@ class MailIterator: def fetch_basic_date(self, mid): """Fetches the basic date of a message, returns bytes reponse.""" try: + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('FETCH') _result, data = self.mail_con.uid('fetch', mid, '(BODY.PEEK[HEADER.FIELDS (DATE)])') except (self.mail_con.error): raise UserWarning("Could not fetch the date header of message " + mid.decode('iso-8859-1') + ".") @@ -131,9 +141,13 @@ class MailIterator: """Replaces a message with one with correct internal date.""" internal_date_sec = time.mktime(internal_date.timetuple()) try: + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('FETCH') result, data = self.mail_con.uid('fetch', mid, '(RFC822)') #logging.debug("Entire e-mail is: %s", data[0][1]) + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('FETCH') # retrieve and select flags to upload fetched_flags = self.mail_con.uid('fetch', mid, '(FLAGS)')[1][0] parsed_flags = imaplib.ParseFlags(fetched_flags) @@ -144,6 +158,8 @@ class MailIterator: logging.debug("Selected flags %s from parsed flags %s.", selected_flags, parsed_flags) flags_str = " ".join(flag.decode('iso-8859-1') for flag in selected_flags) + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('APPEND') # upload message copy and delete old one result, data = self.mail_con.append(mailbox, flags_str, internal_date_sec, data[0][1]) @@ -151,6 +167,8 @@ class MailIterator: except (self.mail_con.error): raise UserWarning("Could not replace the e-mail " + mid.decode('iso-8859-1') + ".") try: + # Work around unsolicited server responses in imaplib by clearing them + self.mail_con.response('STORE') result, data = self.mail_con.uid('STORE', mid, '+FLAGS', r'(\Deleted)') logging.debug("Removing old copy of the message reponse: %s %s", result, data) except (self.mail_con.error): -- 1.7.1