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)
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]