From 1ff628bd5cf6a36ff1925c246f6975d21432641a Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Tue, 26 Apr 2011 15:18:54 +0200 Subject: [PATCH] folder/IMAP: cleanup getmessage() Add some comments how the data structures actually look like. Describe the function properly, and make sure we only hold on to the data connection as quickly as possible. Signed-off-by: Sebastian Spaeth Signed-off-by: Nicolas Sebrecht --- offlineimap/folder/IMAP.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index f2d0dda..a5c71b5 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -209,16 +209,25 @@ class IMAPFolder(BaseFolder): return self.messagelist def getmessage(self, uid): + """Retrieve message with UID from the IMAP server (incl body) + + :returns: the message body + """ imapobj = self.imapserver.acquireconnection() try: imapobj.select(self.getfullname(), readonly = 1) - initialresult = imapobj.uid('fetch', '%d' % uid, '(BODY.PEEK[])') - self.ui.debug('imap', 'Returned object from fetching %d: %s' % \ - (uid, str(initialresult))) - return initialresult[1][0][1].replace("\r\n", "\n") - + res_type, data = imapobj.uid('fetch', '%d' % uid, '(BODY.PEEK[])') finally: self.imapserver.releaseconnection(imapobj) + assert res_type == 'OK', "Fetching message with UID '%d' failed" % uid + # data looks now e.g. [('320 (UID 17061 BODY[] + # {2565}','msgbody....')] we only asked for one message, + # and that msg is in data[0]. msbody is in [0][1] + data = data[0][1] + self.ui.debug('imap', '%s bytes returned from fetching %d: %s...%s' % \ + (len(data), uid, data[0:100], data[-100:])) + return data.replace("\r\n", "\n") + def getmessagetime(self, uid): return self.messagelist[uid]['time']