From 8b884417598eda598a0000a8511f69bc864d0721 Mon Sep 17 00:00:00 2001
From: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
Date: Sat, 20 Feb 2021 00:27:08 +0000
Subject: [PATCH] BUG: Gmail FETCH error with synclabels enabled

If synclabels is enabled then offlineimap is sending '1:*' to imaplib2,
and imaplib2 while creating the FETCH command is quoting the sequence
and the command becomes:
b"JFFJ10 FETCH '1:*' (FLAGS X-GM-LABELS UID)\r\n"

Remove the single-quotes to prevent that and also consider the response
as bytes.

Closes: #52

Signed-off-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>
---

Upstream PR: https://github.com/OfflineIMAP/offlineimap3/pull/55

 offlineimap/folder/Gmail.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/offlineimap/folder/Gmail.py b/offlineimap/folder/Gmail.py
index af236e1..8d64ac4 100644
--- a/offlineimap/folder/Gmail.py
+++ b/offlineimap/folder/Gmail.py
@@ -125,11 +125,10 @@ class GmailFolder(IMAPFolder):
             if not msgsToFetch:
                 return  # No messages to sync
 
-            # Get the flags and UIDs for these. single-quotes prevent
-            # imaplib2 from quoting the sequence.
+            # Get the flags and UIDs for these.
             #
             # NB: msgsToFetch are sequential numbers, not UID's
-            res_type, response = imapobj.fetch("'%s'" % msgsToFetch,
+            res_type, response = imapobj.fetch("%s" % msgsToFetch,
                                                '(FLAGS X-GM-LABELS UID)')
             if res_type != 'OK':
                 raise OfflineImapError(
@@ -147,6 +146,9 @@ class GmailFolder(IMAPFolder):
             # Discard initial message number.
             if messagestr is None:
                 continue
+            # We need a str messagestr
+            if isinstance(messagestr, bytes):
+                messagestr = messagestr.decode(encoding='utf-8')
             messagestr = messagestr.split(' ', 1)[1]
             # e.g.: {'X-GM-LABELS': '("Webserver (RW.net)" "\\Inbox" GInbox)', 'FLAGS': '(\\Seen)', 'UID': '275440'}
             options = imaputil.flags2hash(messagestr)
@@ -164,6 +166,8 @@ class GmailFolder(IMAPFolder):
                 else:
                     labels = set()
                 labels = labels - self.ignorelabels
+                if isinstance(messagestr, str):
+                    messagestr = bytes(messagestr, 'utf-8')
                 rtime = imaplibutil.Internaldate2epoch(messagestr)
                 self.messagelist[uid] = {'uid': uid, 'flags': flags, 'labels': labels, 'time': rtime}
 
-- 
2.30.0

