From 820e5c855f72f6dbc6bb61ab9cb96fded853d9d4 Mon Sep 17 00:00:00 2001 From: Thomas De Schampheleire Date: Wed, 21 Oct 2020 14:25:48 +0200 Subject: [PATCH 1/2] IMAP: Python 3 bytes fix on first download of account ERROR: ERROR in syncfolder for gmail folder INBOX: Traceback (most recent call last): File ".../offlineimap3/offlineimap/accounts.py", line 634, in syncfolder cachemessagelists_upto_date(maxage) File ".../offlineimap3/offlineimap/accounts.py", line 526, in cachemessagelists_upto_date min_date=time.gmtime(time.mktime(date) + 24 * 60 * 60)) File ".../offlineimap3/offlineimap/folder/IMAP.py", line 277, in cachemessagelist imapobj, min_date=min_date, min_uid=min_uid) File ".../offlineimap3/offlineimap/folder/IMAP.py", line 259, in _msgs_to_fetch search_result = search(search_cond) File ".../offlineimap3/offlineimap/folder/IMAP.py", line 222, in search if ' ' in res_data[0] or res_data[0] == '': TypeError: a bytes-like object is required, not 'str' --- offlineimap/folder/IMAP.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index efeafa7..3988681 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -219,7 +219,7 @@ class IMAPFolder(BaseFolder): OfflineImapError.ERROR.FOLDER) # Davmail returns list instead of list of one element string. # On first run the first element is empty. - if ' ' in res_data[0] or res_data[0] == '': + if b' ' in res_data[0] or res_data[0] == b'': res_data = res_data[0].split() # Some servers are broken. if 0 in res_data: From 33e0efa163f0dbe2e9d74c1e8199eede99bbc8ad Mon Sep 17 00:00:00 2001 From: Thomas De Schampheleire Date: Wed, 21 Oct 2020 16:29:13 +0200 Subject: [PATCH 2/2] IMAP: replace non-UTF-8 characters rather than aborting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Emails received may not be UTF-8. Following error was observed on a specific mail: Traceback (most recent call last): File "/home/tdescham/repo/offlineimap3/offlineimap/threadutil.py", line 146, in run Thread.run(self) File "/usr/lib/python3.7/threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "/home/tdescham/repo/offlineimap3/offlineimap/folder/Base.py", line 850, in copymessageto message = self.getmessage(uid) File "/home/tdescham/repo/offlineimap3/offlineimap/folder/IMAP.py", line 327, in getmessage data = self._fetch_from_imap(str(uid), self.retrycount) File "/home/tdescham/repo/offlineimap3/offlineimap/folder/IMAP.py", line 844, in _fetch_from_imap ndata1 = data[0][1].decode('utf-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 10177: invalid start byte This completely aborted offlineimap3, blocking further mail reception. Instead, use the 'replace' error strategy in Python: Replace with a suitable replacement character; Python will use the official U+FFFD REPLACEMENT CHARACTER for the built-in Unicode codecs on decoding and ‘?’ on encoding. https://docs.python.org/2/library/codecs.html#codec-base-classes --- offlineimap/folder/IMAP.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index 3988681..c7febea 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -839,7 +839,7 @@ class IMAPFolder(BaseFolder): # Convert bytes to str ndata0 = data[0][0].decode('utf-8') - ndata1 = data[0][1].decode('utf-8') + ndata1 = data[0][1].decode('utf-8', errors='replace') ndata = [ndata0, ndata1] return ndata