Clean up and improve APPENDUID handling

We were not cleaning out possibly existing APPENDUID messages before
APPENDing a new message. In case an old message were still hanging
around, this could *possibly* lead to retrieving and old UID. Things
should have been fine, but we do want to play safe here.

Also, make use of the "official" imaplib2 .response() command rather
than the internal _get_untagged_response() function.

Remove the hack that we would be looking for APPENDUID responses even if
the server claimed not to support the UIDPLUS ext. We now poll server
CAPABILITIES after login, and Gmail does provide us with the UIDPLUS
capability after login.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2012-01-20 14:33:16 +01:00
parent 36dd46abfc
commit 4d47f7bf3c

View File

@ -538,12 +538,13 @@ class IMAPFolder(BaseFolder):
self.ui.msgtoreadonly(self, uid, content, flags) self.ui.msgtoreadonly(self, uid, content, flags)
return uid return uid
#Do the APPEND # Clean out existing APPENDUID responses and do APPEND
try: try:
(typ, dat) = imapobj.append(self.getfullname(), imapobj.response('APPENDUID') # flush APPENDUID responses
typ, dat = imapobj.append(self.getfullname(),
imaputil.flagsmaildir2imap(flags), imaputil.flagsmaildir2imap(flags),
date, content) date, content)
retry_left = 0 # Mark as success retry_left = 0 # Mark as success
except imapobj.abort, e: except imapobj.abort, e:
# connection has been reset, release connection and retry. # connection has been reset, release connection and retry.
retry_left -= 1 retry_left -= 1
@ -568,41 +569,35 @@ class IMAPFolder(BaseFolder):
OfflineImapError.ERROR.MESSAGE) OfflineImapError.ERROR.MESSAGE)
# Checkpoint. Let it write out stuff, etc. Eg searches for # Checkpoint. Let it write out stuff, etc. Eg searches for
# just uploaded messages won't work if we don't do this. # just uploaded messages won't work if we don't do this.
(typ,dat) = imapobj.check() typ, dat = imapobj.check()
assert(typ == 'OK') assert(typ == 'OK')
# get the new UID. Test for APPENDUID response even if the # get the new UID, default to 0 (=unknown)
# server claims to not support it, as e.g. Gmail does :-( uid = 0
if use_uidplus or imapobj._get_untagged_response('APPENDUID', True): if use_uidplus:
# get new UID from the APPENDUID response, it could look # get new UID from the APPENDUID response, it could look
# like OK [APPENDUID 38505 3955] APPEND completed with # like OK [APPENDUID 38505 3955] APPEND completed with
# 38505 bein folder UIDvalidity and 3955 the new UID. # 38505 being folder UIDvalidity and 3955 the new UID.
# note: we would want to use .response() here but that typ, resp = imapobj.response('APPENDUID')
# often seems to return [None], even though we have if resp == [None] or resp == None:
# data. TODO
resp = imapobj._get_untagged_response('APPENDUID')
if resp == [None]:
self.ui.warn("Server supports UIDPLUS but got no APPENDUID " self.ui.warn("Server supports UIDPLUS but got no APPENDUID "
"appending a message.") "appending a message.")
return 0 else:
uid = long(resp[-1].split(' ')[1]) uid = long(resp[-1].split(' ')[1])
else: else:
# we don't support UIDPLUS # Don't support UIDPLUS
uid = self.savemessage_searchforheader(imapobj, headername, uid = self.savemessage_searchforheader(imapobj, headername,
headervalue) headervalue)
# See docs for savemessage in Base.py for explanation of this and other return values # If everything failed up to here, search the message
# manually TODO: rather than inserting and searching for our
# custom header, we should be searching the Message-ID and
# compare the message size...
if uid == 0: if uid == 0:
self.ui.debug('imap', 'savemessage: first attempt to get new UID failed. \ self.ui.debug('imap', 'savemessage: attempt to get new UID '
Going to run a NOOP and try again.') 'UID failed. Search headers manually.')
assert(imapobj.noop()[0] == 'OK') uid = self.savemessage_fetchheaders(imapobj, headername,
uid = self.savemessage_searchforheader(imapobj, headername, headervalue)
headervalue)
if uid == 0:
self.ui.debug('imap', 'savemessage: second attempt to get new UID failed. \
Going to try search headers manually')
uid = self.savemessage_fetchheaders(imapobj, headername, headervalue)
finally: finally:
self.imapserver.releaseconnection(imapobj) self.imapserver.releaseconnection(imapobj)