[PATCH 5/6] Use range 1:* if we want to examine all messages in a folder

Sebastian Spaeth Sebastian at SSpaeth.de
Mon Aug 15 13:15:58 BST 2011


Some code cleanup. If we want to examine all messages of a folder, don't
try to find out how many there are and request a long list of all of them,
but simply request 1:*. This obliviates us from the need to force a select
even if we already had the folder selected and it requires us to send a
few less bytes over the wire.

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
 offlineimap/folder/IMAP.py |   33 +++++++++------------------------
 1 files changed, 9 insertions(+), 24 deletions(-)

diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py
index f04b783..74cef92 100644
--- a/offlineimap/folder/IMAP.py
+++ b/offlineimap/folder/IMAP.py
@@ -105,7 +105,6 @@ class IMAPFolder(BaseFolder):
             self.imapserver.releaseconnection(imapobj)
         return False
 
-    # TODO: Make this so that it can define a date that would be the oldest messages etc.
     def cachemessagelist(self):
         maxage = self.config.getdefaultint("Account %s" % self.accountname,
                                            "maxage", -1)
@@ -114,10 +113,11 @@ class IMAPFolder(BaseFolder):
         self.messagelist = {}
 
         imapobj = self.imapserver.acquireconnection()
-
         try:
-            # Primes untagged_responses
-            imaptype, imapdata = imapobj.select(self.getfullname(), readonly = 1, force = 1)
+            res_type, imapdata = imapobj.select(self.getfullname(), True)
+
+            # By default examine all UIDs in this folder
+            msgsToFetch = '1:*'
 
             if (maxage != -1) | (maxsize != -1):
                 search_cond = "(";
@@ -149,28 +149,13 @@ class IMAPFolder(BaseFolder):
                 if not messagesToFetch:
                     return # No messages to sync
 
-            else:
-                # 1. Some mail servers do not return an EXISTS response
-                # if the folder is empty.  2. ZIMBRA servers can return
-                # multiple EXISTS replies in the form 500, 1000, 1500,
-                # 1623 so check for potentially multiple replies.
-                if imapdata == [None]:
-                    return
-
-                maxmsgid = 0
-                for msgid in imapdata:
-                    maxmsgid = max(long(msgid), maxmsgid)
-                if maxmsgid < 1:
-                    #no messages; return
-                    return
-                messagesToFetch = '1:%d' % maxmsgid;
-
-            # Now, get the flags and UIDs for these.
-            # We could conceivably get rid of maxmsgid and just say
-            # '1:*' here.
-            response = imapobj.fetch("'%s'" % messagesToFetch, '(FLAGS UID)')[1]
+            # Get the flags and UIDs for these. single-quotes prevent
+            # imaplib2 from quoting the sequence.
+            res_type, response = imapobj.fetch("'%s'" % msgsToFetch,
+                                               '(FLAGS UID)')
         finally:
             self.imapserver.releaseconnection(imapobj)
+
         for messagestr in response:
             # Discard the message number.
             messagestr = messagestr.split(' ', 1)[1]
-- 
1.7.4.1





More information about the OfflineIMAP-project mailing list