INBOX acls reset parsing with regex
authorPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Fri, 6 Jul 2012 07:31:33 +0000 (09:31 +0200)
committerPlamen Dimitrov <plamen.dimitrov@intra2net.com>
Fri, 6 Jul 2012 07:31:33 +0000 (09:31 +0200)
mail_iterator.py

index 1c4550c..3c260c3 100644 (file)
@@ -9,7 +9,7 @@ import re
 
 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(r'(?P<name>.*) \(UIDVALIDITY (?P<uidval>.*)\)')
+ACLS_RESP = re.compile(r'(?P<user>.*) (?P<acls>.*)')
 
 class MailIterator:
     """This class communicates with the e-mail server."""
@@ -24,7 +24,7 @@ class MailIterator:
 
     def __init__(self, server, username, password):
         """Creates a connection and a user session."""
-        
+
         # connect to server
         try:
             self.mail_con = imaplib.IMAP4_SSL(server)
@@ -40,7 +40,7 @@ class MailIterator:
         except:
             self.logged_in = False
             raise UserWarning("Could not log in as user " + username)
-            
+
         # list mailboxes
         try:
             _result, mailboxes = self.mail_con.list()
@@ -51,7 +51,7 @@ class MailIterator:
             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):
@@ -66,14 +66,15 @@ class MailIterator:
             _result, inbox_acls = self.mail_con.getacl("INBOX")
         except:
             print("Could not get the acls of INBOX.")
-        inbox_acls = inbox_acls[0].split(' ')[1:]
-        for acl_user in inbox_acls:
-            if acl_user != user:
+        inbox_acls = ACLS_RESP.findall(inbox_acls[0][6:])
+        #print(inbox_acls)
+        for acl_ref in inbox_acls:
+            if acl_ref[0] != user:
                 try:
-                    self.mail_con.setacl("INBOX", acl_user, "")
-                    print("Reset acls on INBOX for user %s" % acl_user)
+                    self.mail_con.setacl("INBOX", acl_ref[0], "")
+                    print("Reset acls on INBOX for user %s" % acl_ref[0])
                 except:
-                    print("Could not reset acls on INBOX for user %s" % acl_user)
+                    print("Could not reset acls on INBOX for user %s" % acl_ref[0])
         return
 
     def add_acls(self, mailbox, mailbox_list, original_user, target_user):