From: Plamen Dimitrov Date: Thu, 5 Jul 2012 16:16:06 +0000 (+0200) Subject: Connection error handling and some formatting X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=b2bbd1f55e9718fc6c48c98425d25fe793bbd6bd;p=imap-restore-mail Connection error handling and some formatting --- diff --git a/file_iterator.py b/file_iterator.py index 3c3c975..5c3512d 100644 --- a/file_iterator.py +++ b/file_iterator.py @@ -15,8 +15,6 @@ class FileIterator: """This class iterates through the e-mail files.""" # class attributes - # mailbox database? - mboxdb = None # mailboxes created during file traversal created_mailboxes = None # mailboxes to update during file traversal @@ -116,7 +114,4 @@ class FileIterator: #print("Done with directory %s and mailbox %s." % (new_filepath, new_mailpath)) # mark mailboxes that need acl update self.acl_mailboxes.append(mailpath) - return - # - # - # \ No newline at end of file + return \ No newline at end of file diff --git a/mail_iterator.py b/mail_iterator.py index 3bf06b1..1c4550c 100644 --- a/mail_iterator.py +++ b/mail_iterator.py @@ -9,6 +9,7 @@ import re MAILBOX_RESP = re.compile(r'\((?P.*?)\) "(?P.*)" (?P.*)') UIDVAL_RESP = re.compile(r'(?P.*) \(UIDVALIDITY (?P.*)\)') +ACLS_RESP = re.compile(r'(?P.*) \(UIDVALIDITY (?P.*)\)') class MailIterator: """This class communicates with the e-mail server.""" @@ -23,59 +24,66 @@ class MailIterator: def __init__(self, server, username, password): """Creates a connection and a user session.""" - # authentication + + # connect to server try: self.mail_con = imaplib.IMAP4_SSL(server) + print("Connected to %s." % server) + except Exception as ex: + raise UserWarning("Could not connect to host %s: %s" % (server, ex)) + + # log in + try: self.mail_con.login(username, password) + self.logged_in = True print("Logged in as %s." % username) except: self.logged_in = False - print("Could not log in as user " + username + ".") - raise UserWarning - self.logged_in = True - - # prepare mailboxes - _result, mailboxes = self.mail_con.list() + raise UserWarning("Could not log in as user " + username) + + # list mailboxes + try: + _result, mailboxes = self.mail_con.list() + except (self.mail_con.error): + raise UserWarning("Could not retrieve mailboxes for user " + username) self.mailboxes = [] for mailbox in mailboxes: mailbox = MAILBOX_RESP.match(mailbox.decode('iso-8859-1')).groups() self.mailboxes.append(mailbox) self.mailboxes = sorted(self.mailboxes, key=lambda box: box[2], reverse=True) + return def __del__(self): """Closes the connection and the user session.""" - if self.logged_in: - try: - self.mail_con.close() - self.mail_con.logout() - except: - pass + #if self.logged_in: + # self.mail_con.close() + # self.mail_con.logout() def clear_inbox_acls(self, user): - """Resets the inbox acls for each user.""" + """Resets the inbox acls for a given user.""" try: - hashref = self.mail_con.getacl("INBOX") + _result, inbox_acls = self.mail_con.getacl("INBOX") except: print("Could not get the acls of INBOX.") - print(hashref) - for acluser in hashref: - if acluser != user: + inbox_acls = inbox_acls[0].split(' ')[1:] + for acl_user in inbox_acls: + if acl_user != user: try: - #print(self.mail_con.setacl("INBOX", acluser, "")) - print("Reset acls on INBOX for user %s" % acluser) + self.mail_con.setacl("INBOX", acl_user, "") + print("Reset acls on INBOX for user %s" % acl_user) except: - print("Could not reset acls on INBOX for user %s" % acluser) + print("Could not reset acls on INBOX for user %s" % acl_user) return def add_acls(self, mailbox, mailbox_list, original_user, target_user): """Add acls to mailbox.""" - + # change encoding to internal cyrus format and make folder absolute mailbox = mailbox.replace("INBOX/", "user/" + original_user + "/") mailbox = mailbox.replace(".", "^") mailbox = mailbox.replace("/", ".") - + # find folder to set all acls try: mbox_acls = mailbox_list[mailbox] @@ -83,14 +91,13 @@ class MailIterator: # no rights for the mailbox were found return for acl_user in mbox_acls: - print(acl_user, target_user, original_user) if acl_user != target_user and acl_user != original_user: try: - #self.mail_con.setacl(mailbox, thisaclref[0], thisaclref[1]) + self.mail_con.setacl(mailbox, acl_user, mbox_acls[acl_user]) print("Set acls %s for user %s on mailbox %s." % (mbox_acls[acl_user], acl_user, mailbox)) except: print("Could not set acls %s for user %s on mailbox %s." % (mbox_acls[acl_user], acl_user, mailbox)) - + return def delete_mailboxes(self, deleted_mailbox): @@ -120,12 +127,9 @@ class MailIterator: def inject_message(self, message, mailbox, internal_date): """Inject a message into a mailbox.""" - - #$imap->append_file($folder, $full_path, undef, "\\Seen", true) - try: - result, data = self.mail_con.append(mailbox, "\\Seen", - internal_date, message) + result, data = self.mail_con.append(mailbox, "\\Seen", internal_date, message) + if result == "OK": print("Appending message to mailbox %s" % mailbox) - except: - raise UserWarning("Could not append the e-mail." % message) + else: + print("Could not append the e-mail %s: %s" % (message, data[0])) return \ No newline at end of file