Improved formatting and separation in blocks
[imap-restore-mail] / mail_iterator.py
index 9aebca2..af45da9 100644 (file)
@@ -24,15 +24,15 @@ class MailIterator:
     # logged in status
     logged_in = None
 
-    def __init__(self, username, server = "/var/imap/socket/imap"):
+    def __init__(self, username, server = "intranator.m.i2n"):
         """Creates a connection and a user session."""
 
         # connect to server
         try:
-            self.mail_con = imaplib.IMAP4("intranator.m.i2n")
+            self.mail_con = imaplib.IMAP4(server)
             # MODIFIED
             imap_socket = socket.socket(socket.AF_UNIX)
-            imap_socket.connect(server)
+            imap_socket.connect("/var/imap/socket/imap")
             self.mail_con.sock = imap_socket
             self.mail_con.file = self.mail_con.sock.makefile('rb')
             logging.info("Connected to mail server %s", server)
@@ -66,12 +66,13 @@ class MailIterator:
 
     def __del__(self):
         """Closes the connection and the user session."""
-        #if self.logged_in:
-        #    self.mail_con.close()
-        #    self.mail_con.logout()
+
+        if self.logged_in:
+            self.mail_con.logout()
 
     def clear_inbox_acls(self, user):
         """Resets the inbox acls for a given user."""
+
         try:
             _result, inbox_acls = self.mail_con.getacl("INBOX")
         except self.mail_con.error as ex:
@@ -86,6 +87,7 @@ class MailIterator:
                     logging.debug("Reset acls on INBOX for user %s", acl_ref[0].decode('iso-8859-1'))
                 except self.mail_con.error as ex:
                     logging.warning("Could not reset acls on INBOX for user %s: %s", acl_ref[0], ex)
+
         return
 
     def add_acls(self, mailbox, mailbox_list, original_user, target_user):
@@ -114,6 +116,7 @@ class MailIterator:
 
     def delete_mailboxes(self, deleted_mailbox):
         """Delete specified mailbox or empty inbox."""
+
         for mailbox in self.mailboxes:
             pattern = '^\"?' + deleted_mailbox
             # if INBOX it cannot be deleted so add delimiter
@@ -124,10 +127,12 @@ class MailIterator:
                 if result == "OK":
                     logging.debug("Deleted mailbox %s", mailbox[2])
                 else:
-                    logging.warning("Could not delete mailbox %s: %s", mailbox[2], data[0])         
+                    logging.warning("Could not delete mailbox %s: %s", mailbox[2], data[0]) 
+        
         return
 
     def create_mailbox(self, mailbox):
+
         """Create new mailbox to inject messages."""
         if mailbox != "INBOX":
             result, data = self.mail_con.create(mailbox)
@@ -135,13 +140,16 @@ class MailIterator:
                 logging.debug("Creating mailbox %s", mailbox)
             else:
                 logging.warning("Could not create mailbox %s: %s", mailbox, data[0])
+
         return
 
     def inject_message(self, message, mailbox, internal_date):
+
         """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])
+
         return
\ No newline at end of file