From: Plamen Dimitrov Date: Mon, 9 Jul 2012 14:21:17 +0000 (+0200) Subject: Improved formatting and separation in blocks X-Git-Url: http://developer.intra2net.com/git/?p=imap-restore-mail;a=commitdiff_plain;h=0cf4dc336e6285f8504070f35e34897b462c71ce Improved formatting and separation in blocks --- diff --git a/file_iterator.py b/file_iterator.py index b8f60ef..5cdc5c0 100644 --- a/file_iterator.py +++ b/file_iterator.py @@ -23,28 +23,29 @@ class FileIterator: def __init__(self): """Creates a connection and a user session.""" + self.created_mailboxes = [] self.acl_mailboxes = [] - return - def __del__(self): - """Closes the connection and the user session.""" return @classmethod def _message_read(cls, filename): """Retrieves a message from the message file.""" + try: with open(filename, "r") as msgfile: message = msgfile.read() except IOError: logging.warning("Could not open the e-mail file %s", filename) raise + return message @classmethod def load_mailbox_list(cls, mboxlistfile = ""): - """Load the list of mailboxes and acl rights for each from file.""" + """Load the list of mailboxes and acl rights for each from file.""" + mboxdb = {} if mboxlistfile != "": try: @@ -70,12 +71,14 @@ class FileIterator: mboxdb[key] = acls except IOError: logging.warning("Could not open mboxlist file %s", mboxlistfile) + return mboxdb def load_mails(self, filepath, mailpath): """Loads all e-mails from file hierarchy. This recursive generator always returns a tuple of the next found (e-mail, mailbox to store, internaldate).""" + logging.debug("Entered directory %s -> %s", filepath, mailpath) try: filepath = os.path.abspath(filepath) @@ -116,4 +119,5 @@ class FileIterator: logging.debug("Done with directory %s and mailbox %s", new_filepath, new_mailpath) # mark mailboxes that need acl update self.acl_mailboxes.append(mailpath) + return \ No newline at end of file diff --git a/mail_iterator.py b/mail_iterator.py index 9aebca2..af45da9 100644 --- a/mail_iterator.py +++ b/mail_iterator.py @@ -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 diff --git a/restore_mail_inject.py b/restore_mail_inject.py index 9623ead..627ba41 100644 --- a/restore_mail_inject.py +++ b/restore_mail_inject.py @@ -71,6 +71,7 @@ def main(): def configure_args(): """Configure arguments and return them.""" + # parse arguments parser = argparse.ArgumentParser(description="Tool to inject mails via IMAP.") parser.add_argument('-u', '--username', dest='user', action='store', @@ -120,6 +121,7 @@ def prepare_logger(): warnings_handler = WarningsHandler() warnings_handler.setLevel(LOG_UNCLEAN_EXIT_LEVEL) default_logger.addHandler(warnings_handler) + return warnings_handler if __name__ == "__main__": diff --git a/warnings_handler.py b/warnings_handler.py index bc6af31..2e28470 100644 --- a/warnings_handler.py +++ b/warnings_handler.py @@ -13,10 +13,12 @@ class WarningsHandler(logging.Handler): def __init__(self): """Initialize a handler to count number of warnings.""" + logging.Handler.__init__(self) self.detected_problems = 0 def emit(self, record): """Increase number of warnings found""" + self.detected_problems += 1 \ No newline at end of file