From e5b6397dd52e8ce0214a3a18801f61bb5a5fe261 Mon Sep 17 00:00:00 2001 From: Nikolaus Schulz Date: Tue, 8 Apr 2008 19:06:42 +0000 Subject: [PATCH] Moved IMAP SELECT code into a separate function. --- archivemail.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/archivemail.py b/archivemail.py index afd532a..13e6ffb 100755 --- a/archivemail.py +++ b/archivemail.py @@ -1371,21 +1371,8 @@ def _archive_imap(mailbox_name, final_archive_name): user_error("imap server %s has login disabled (hint: " "try ssl/imaps)" % imap_server) - imap_folder = imap_find_mailbox(imap_srv, imap_folder) - roflag = options.dry_run or options.copy_old_mail - # Work around python bug #1277098 (still pending in python << 2.5) - if not roflag: - roflag = None - if roflag: - vprint("examining imap folder '%s' read-only" % imap_folder) - else: - vprint("selecting imap folder '%s'" % imap_folder) - result, response = imap_srv.select(imap_folder, roflag) - if result != 'OK': - unexpected_error("selecting '%s' failed; server says: '%s'." \ - % (imap_folder, response[0])) - # response is e.g. ['1016'] for 1016 messages in folder - total_msg_count = int(response[0]) + imap_smart_select(imap_srv, imap_folder) + total_msg_count = int(imap_srv.response("EXISTS")[1][0]) vprint("folder has %d message(s)" % total_msg_count) # IIUIC the message sequence numbers are stable for the whole session, since @@ -1552,6 +1539,24 @@ def imap_get_namespace(srv): return ns +def imap_smart_select(imap_srv, imap_folder): + """Select the given mailbox on the IMAP server, correcting an invalid + mailbox path if possible.""" + imap_folder = imap_find_mailbox(imap_srv, imap_folder) + roflag = options.dry_run or options.copy_old_mail + # Work around python bug #1277098 (still pending in python << 2.5) + if not roflag: + roflag = None + if roflag: + vprint("examining imap folder '%s' read-only" % imap_folder) + else: + vprint("selecting imap folder '%s'" % imap_folder) + result, response = imap_srv.select(imap_folder, roflag) + if result != 'OK': + unexpected_error("selecting '%s' failed; server says: '%s'." \ + % (imap_folder, response[0])) + + def imap_find_mailbox(srv, mailbox): """Find the given mailbox on the IMAP server, correcting an invalid mailbox path if possible. Return the found mailbox name."""