Use range 1:* if we want to examine all messages in a folder

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@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Sebastian Spaeth 2011-08-18 09:08:55 +02:00 committed by Nicolas Sebrecht
parent 8532a69458
commit f755c8b423

View File

@ -105,7 +105,6 @@ class IMAPFolder(BaseFolder):
self.imapserver.releaseconnection(imapobj) self.imapserver.releaseconnection(imapobj)
return False return False
# TODO: Make this so that it can define a date that would be the oldest messages etc.
def cachemessagelist(self): def cachemessagelist(self):
maxage = self.config.getdefaultint("Account %s" % self.accountname, maxage = self.config.getdefaultint("Account %s" % self.accountname,
"maxage", -1) "maxage", -1)
@ -114,10 +113,11 @@ class IMAPFolder(BaseFolder):
self.messagelist = {} self.messagelist = {}
imapobj = self.imapserver.acquireconnection() imapobj = self.imapserver.acquireconnection()
try: try:
# Primes untagged_responses res_type, imapdata = imapobj.select(self.getfullname(), True)
imaptype, imapdata = imapobj.select(self.getfullname(), readonly = 1, force = 1)
# By default examine all UIDs in this folder
msgsToFetch = '1:*'
if (maxage != -1) | (maxsize != -1): if (maxage != -1) | (maxsize != -1):
search_cond = "("; search_cond = "(";
@ -149,28 +149,13 @@ class IMAPFolder(BaseFolder):
if not messagesToFetch: if not messagesToFetch:
return # No messages to sync return # No messages to sync
else: # Get the flags and UIDs for these. single-quotes prevent
# 1. Some mail servers do not return an EXISTS response # imaplib2 from quoting the sequence.
# if the folder is empty. 2. ZIMBRA servers can return res_type, response = imapobj.fetch("'%s'" % msgsToFetch,
# multiple EXISTS replies in the form 500, 1000, 1500, '(FLAGS UID)')
# 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]
finally: finally:
self.imapserver.releaseconnection(imapobj) self.imapserver.releaseconnection(imapobj)
for messagestr in response: for messagestr in response:
# Discard the message number. # Discard the message number.
messagestr = messagestr.split(' ', 1)[1] messagestr = messagestr.split(' ', 1)[1]