Clear inbox acls and other fixes based on autotest validation
[imap-restore-mail] / src / mail_iterator.py
index 24d0376..fab6f1c 100644 (file)
@@ -22,7 +22,9 @@ import logging
 
 MAILBOX_RESP = re.compile(r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)')
 UIDVAL_RESP = re.compile(r'(?P<name>.*) \(UIDVALIDITY (?P<uidval>.*)\)')
-ACLS_RESP = re.compile(b'(?P<user>.*) (?P<acls>.*)')
+ACLS_RESP = re.compile(b'(?P<user>\S+) (?P<acls>\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