From bb6cdeeac591dc2def43407267a036018aeb83f8 Mon Sep 17 00:00:00 2001 From: Plamen Dimitrov Date: Mon, 20 Aug 2012 16:34:29 +0200 Subject: [PATCH] Clear inbox acls and other fixes based on autotest validation --- src/file_iterator.py | 4 ++-- src/imap_restore_mail.py | 2 +- src/mail_iterator.py | 26 +++++++++++++------------- src/unit_tester.py | 6 ++++-- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/file_iterator.py b/src/file_iterator.py index c506e83..843396c 100644 --- a/src/file_iterator.py +++ b/src/file_iterator.py @@ -59,7 +59,7 @@ class FileIterator: Should be used for access of mailbox acls instead of attribute""" # translate the mailbox to internal stored format - internal_mailbox = mailbox.replace("INBOX/", "user/" + original_user + "/") + internal_mailbox = mailbox.replace("INBOX", "user/" + original_user) internal_mailbox = internal_mailbox.replace(".", "^") internal_mailbox = internal_mailbox.replace("/", ".") logging.debug("Get acls for mailbox %s translated as %s", mailbox, internal_mailbox) @@ -70,7 +70,7 @@ class FileIterator: except KeyError: # no rights for the mailbox were found and warn if acl file loaded if len(self._file_acls) > 0: - logging.warning("Could not find the acls for mailbox %s %s", mailbox, self._file_acls.keys()) + logging.warning("Could not find the acls for mailbox %s", mailbox) mb_acls = {} return mb_acls diff --git a/src/imap_restore_mail.py b/src/imap_restore_mail.py index 9d8ca72..79eb1c5 100644 --- a/src/imap_restore_mail.py +++ b/src/imap_restore_mail.py @@ -42,7 +42,7 @@ def main(): # retrieve mailbox list if a mailbox list file is specified if args.mboxlistfile != "": - storage.load_mailbox_list(args.mboxlistfile, args.ouser) + storage.load_mailbox_list(args.mboxlistfile) # delete olf IMAP folders if no append requested if not args.append: diff --git a/src/mail_iterator.py b/src/mail_iterator.py index 24d0376..fab6f1c 100644 --- a/src/mail_iterator.py +++ b/src/mail_iterator.py @@ -22,7 +22,9 @@ import logging MAILBOX_RESP = re.compile(r'\((?P.*?)\) "(?P.*)" (?P.*)') UIDVAL_RESP = re.compile(r'(?P.*) \(UIDVALIDITY (?P.*)\)') -ACLS_RESP = re.compile(b'(?P.*) (?P.*)') +ACLS_RESP = re.compile(b'(?P\S+) (?P\S+)') + +#imaplib.Debug = 4 class MailIterator: """This class communicates with the e-mail server.""" @@ -98,7 +100,7 @@ class MailIterator: inbox_acls = ACLS_RESP.findall(inbox_acls[0][6:]) logging.debug("Retrieved acls from INBOX are %s", inbox_acls) for acl_ref in inbox_acls: - if acl_ref[0] != user: + if acl_ref[0].decode('iso-8859-1') != user: try: self.mail_con.deleteacl("INBOX", acl_ref[0]) logging.debug("Reset acls on INBOX for user %s", acl_ref[0].decode('iso-8859-1')) @@ -134,9 +136,8 @@ class MailIterator: pattern += mailbox[1] if re.compile(pattern).match(mailbox[2]): result, data = self.mail_con.delete(mailbox[2]) - if result == "OK": - logging.debug("Deleted mailbox %s", mailbox[2]) - else: + logging.debug("Deleted mailbox %s", mailbox[2]) + if result != "OK": logging.warning("Could not delete mailbox %s: %s", mailbox[2], data[0]) return @@ -146,10 +147,9 @@ class MailIterator: if mailbox != "INBOX": result, data = self.mail_con.create(mailbox) - if result == "OK": - logging.debug("Creating mailbox %s", mailbox) - else: - logging.warning("Could not create mailbox %s: %s", mailbox, data[0]) + logging.debug("Creating mailbox %s", mailbox) + if result != "OK": + logging.warning("Could not create mailbox %s: %s", mailbox, data[0].decode('iso-8859-1')) return @@ -157,9 +157,9 @@ class MailIterator: """Inject a message into a mailbox.""" result, data = self.mail_con.append(mailbox, "\\Seen", internal_date, message.encode()) - if result == "OK": - logging.debug("Appending message to mailbox %s", mailbox) - else: - logging.warning("Could not append the e-mail %s: %s", message, data[0]) + logging.debug("Appending message to mailbox %s", mailbox) + if result != "OK": + logging.warning("Could not append an e-mail to the mailbox %s: %s.", mailbox, data[0].decode('iso-8859-1')) + #logging.debug("Email content:\n%s", message) return \ No newline at end of file diff --git a/src/unit_tester.py b/src/unit_tester.py index 695a563..6588344 100644 --- a/src/unit_tester.py +++ b/src/unit_tester.py @@ -37,6 +37,7 @@ class FileContentsParse(unittest.TestCase): self.dummy_filename = "test_acls.dump" self.dummy_file = open(self.dummy_filename, "w") self.dummy_file.write("user.00schneider\t0 default 00schneider\tlrswipkxtecda\t\n" + "user.mit^punkt\t0 default mit.punkt\tlrswipkxtecda\tinternaldate_test\tlr\t\n" "user.mit^punkt.Gesendete Objekte\t0 default mit.punkt\tlrswipktecda\t\n" "user.00schneider.Gesendete Objekte\t0 default 00schneider\tlrswipkxtecda\t\n" "user.00schneider.ibx_sub\t0 default 00schneider\tlrswpkxtecda\t\n" @@ -59,17 +60,18 @@ class FileContentsParse(unittest.TestCase): os.unlink(self.dummy_filename) def test_mboxlist_parsed_line1(self): - """Test whether a line was parsed correctly.""" + self.file_iter.load_mailbox_list(self.dummy_filename) self.assertEqual(len(self.file_iter.get_mailbox_acls("INBOX/Gesendete Objekte", "00schneider")), 1) self.assertIn("00schneider", self.file_iter.get_mailbox_acls("INBOX/Gesendete Objekte", "00schneider")) self.assertEqual(self.file_iter.get_mailbox_acls("INBOX/Gesendete Objekte", "00schneider")["00schneider"], "lrswipkxtecda", "Wrong acls were parsed for a user.") def test_mboxlist_parsed_line2(self): - """Test whether a line was parsed correctly.""" + self.file_iter.load_mailbox_list(self.dummy_filename) + self.assertEqual(len(self.file_iter.get_mailbox_acls("INBOX", "mit.punkt")), 2) self.assertEqual(len(self.file_iter.get_mailbox_acls("INBOX/Gesendete Objekte", "mit.punkt")), 1) self.assertIn("mit.punkt", self.file_iter.get_mailbox_acls("INBOX/Gesendete Objekte", "mit.punkt")) self.assertEqual(self.file_iter.get_mailbox_acls("INBOX/Gesendete Objekte", "mit.punkt")["mit.punkt"], "lrswipktecda", "Wrong acls were parsed for a user.") -- 1.7.1