Refactoring of cyrus stored acls formatting - now performed by FileIterator where...
[imap-restore-mail] / src / file_iterator.py
index c704142..7cfc42c 100644 (file)
@@ -31,14 +31,15 @@ class FileIterator:
     created_mailboxes = None
     # mailboxes to update during file traversal
     acl_mailboxes = None
+    # acls retrieved from a file
+    file_acls = None
 
     def __init__(self):
         """Creates a connection and a user session."""
 
         self.created_mailboxes = []
         self.acl_mailboxes = []
-
-        return
+        self.file_acls = {}
 
     @classmethod
     def _message_read(cls, filename):
@@ -53,41 +54,41 @@ class FileIterator:
 
         return message
 
-    @classmethod
-    def load_mailbox_list(cls, mboxlistfile = ""):
+    def load_mailbox_list(self, mboxlistfile, original_user):
         """Load the list of mailboxes and acl rights for each from file."""
 
-        mboxdb = {}
-        if mboxlistfile != "":
-            try:
-                with open(mboxlistfile, 'r') as acl_file:
-                    for line in acl_file:
-
-                        acls = {}
+        try:
+            with open(mboxlistfile, 'r') as acl_file:
+                for line in acl_file:
+
+                    # read a line using regex
+                    acls = {}
+                    try:
+                        linedata = MBOXFILE_LINE.match(line).groups()
+                    except AttributeError:
+                        logging.warning("Illegal line in mailbox list dump: %s", line)
+                        continue
+                    aclstr = linedata[1]
+
+                    # changes acls key encoding to internal cyrus format and makes it absolute (as folder).
+                    key = linedata[0].replace("INBOX/", "user/" + original_user + "/")
+                    key = key.replace(".", "^")
+                    key = key.replace("/", ".")
+
+                    # loop through acl rights string and build dictionary of users and rights
+                    while(aclstr != ""):
                         try:
-                            linedata = MBOXFILE_LINE.match(line).groups()
+                            acldata = ACL_STRING.match(aclstr).groups()
                         except AttributeError:
-                            logging.warning("Illegal line in mailbox list dump: %s", line)
+                            logging.warning("Illegal acl string in mailbox list dump: %s", line)
+                            aclstr = ""
                             continue
-                        key = linedata[0]
-                        aclstr = linedata[1]
-
-                        # loop through acl rights string and build dictionary of users and rights
-                        while(aclstr != ""):
-                            try:
-                                acldata = ACL_STRING.match(aclstr).groups()
-                            except AttributeError:
-                                logging.warning("Illegal acl string in mailbox list dump: %s", line)
-                                aclstr = ""
-                                continue
-                            aclstr = acldata[2]
-                            acls[acldata[0]] = acldata[1]
-
-                        mboxdb[key] = acls
-            except IOError:
-                logging.warning("Could not open mboxlist file %s", mboxlistfile)
-
-        return mboxdb
+                        aclstr = acldata[2]
+                        acls[acldata[0]] = acldata[1]
+
+                    self.acl_mailboxes[key] = acls
+        except IOError:
+            logging.warning("Could not open mboxlist file %s", mboxlistfile)
 
     def load_mails(self, filepath, mailpath):
         """Loads all e-mails from file hierarchy.
@@ -140,5 +141,3 @@ class FileIterator:
 
         # mark mailboxes that need acl update
         self.acl_mailboxes.append(mailpath)
-
-        return
\ No newline at end of file