[PATCH] Don't cache empty IMAP folders

Sebastian Spaeth Sebastian at SSpaeth.de
Wed Sep 7 14:23:28 BST 2011

If a folder is empty, most servers will return EXISTS 0 and imaplib2
passes back ['0'] as return value to a select(). It returns [None] if
no EXISTS response was given by the server at all.

Attempting to fetch the UIDs of 0 emails which leads to
various error messages (One server responds with "NO No matching
messages", Gmail seems to say "BAD Bad message sequence 1:*" for some
(although it is working fine for me with Gmail, so it might behave
different for different people).

In case we get an None or 0 back, we simply stop caching messages as the
folder is empty. This should fix the various error reports that have
popped up.

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
I was able to test this with a Gmail account. This should fix both the 
"NO No matching messages" and the BAD Bad message sequence errors that
people are seeing on empty folders. Unfortunately my mail server had 
never shown me errors, and my Gmail account did not send any error 
message either. This fix should be good and fine in any case.

 offlineimap/folder/IMAP.py |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py
index 5a14776..d860e85 100644
--- a/offlineimap/folder/IMAP.py
+++ b/offlineimap/folder/IMAP.py
@@ -120,7 +120,9 @@ class IMAPFolder(BaseFolder):
         imapobj = self.imapserver.acquireconnection()
             res_type, imapdata = imapobj.select(self.getfullname(), True)
+            if imapdata == [None] or imapdata[0] == '0':
+                # Empty folder, no need to populate message list
+                return
             # By default examine all UIDs in this folder
             msgsToFetch = '1:*'

More information about the OfflineIMAP-project mailing list