session = MailIterator(args.server, args.user, psw)
storage = FileIterator()
- # retrieve mailbox list from the mailbox list file
- mailbox_list = storage.load_mailbox_list(args.mboxlistfile)
+ # retrieve mailbox list if a mailbox list file is specified
+ if args.mboxlistfile != "":
+ storage.load_mailbox_list(args.mboxlistfile, args.ouser)
# delete olf IMAP folders if no append requested
if not args.append:
# inject emails
path_generator = storage.load_mails(args.source, args.destination)
for message, mailbox, date_modified in path_generator:
-
- # mailboxes marked for creating and acl update
- # add acls after all subfolders or their acls will be derived from parent folder
- for new_mailbox in storage.created_mailboxes:
- session.create_mailbox(new_mailbox)
- for acl_mailbox in storage.acl_mailboxes:
- session.add_acls(acl_mailbox, mailbox_list, args.ouser, args.user)
- storage.created_mailboxes = []
- storage.acl_mailboxes = []
-
+ update_mailboxes(storage, session, args)
session.inject_message(message, mailbox, date_modified)
- # last iteration mailboxes in case root mailbox has no e-mails for injection
- for new_mailbox in storage.created_mailboxes:
- session.create_mailbox(new_mailbox)
- for acl_mailbox in storage.acl_mailboxes:
- session.add_acls(acl_mailbox, mailbox_list, args.ouser, args.user)
+ # last iteration for mailboxes in case root mailbox has no e-mails for injection
+ update_mailboxes(storage, session, args)
logging.info("Finished injecting mails. Exiting with code %s.", warnings_handler.detected_problems)
return warnings_handler.detected_problems
+def update_mailboxes(storage, session, args):
+ """Create new mailboxes if necessary and add acls to mailboxes if necessary."""
+
+ # create new mailboxes if found by the file iterator
+ for new_mailbox in storage.created_mailboxes:
+ session.create_mailbox(new_mailbox)
+
+ # add acls after all subfolders or their acls will be derived from parent folder
+ for acl_mailbox in storage.acl_mailboxes:
+ # find folder acls record and retrieve them
+ try:
+ mb_acls = storage.file_acls[acl_mailbox]
+ except KeyError:
+ # no rights for the mailbox were found and warn if acl file loaded
+ if len(storage.file_acls) > 0:
+ logging.warning("Could not find the acls for mailbox %s for user %s.", acl_mailbox, args.ouser)
+ mb_acls = {}
+ session.add_acls(acl_mailbox, mb_acls, args.ouser, args.user)
+ storage.created_mailboxes = []
+ storage.acl_mailboxes = []
+
def configure_args():
"""Configure arguments and return them."""