Commit | Line | Data |
---|---|---|
67e2ec02 PD |
1 | ''' |
2 | restore-mail-inject.py - Tool to inject mails via IMAP | |
3 | ||
4 | Copyright (c) 2012 Intra2net AG | |
20760d94 | 5 | Author: Plamen Dimitrov and Thomas Jarosch |
e42bd6a5 | 6 | |
20760d94 PD |
7 | This program is free software: you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
67e2ec02 | 16 | ''' |
f797b0fd | 17 | import logging |
f72ba42a | 18 | import argparse, getpass |
67e2ec02 PD |
19 | from mail_iterator import MailIterator |
20 | from file_iterator import FileIterator | |
5737b10e | 21 | from warnings_handler import WarningsHandler |
67e2ec02 | 22 | |
12d5b37a | 23 | # logging settings |
f72ba42a | 24 | LOG_FILENAME = "imap_restore_mail.log" |
f797b0fd PD |
25 | LOG_FILE_LEVEL = logging.DEBUG |
26 | LOG_SHELL_LEVEL = logging.INFO | |
5737b10e | 27 | LOG_UNCLEAN_EXIT_LEVEL = logging.WARNING |
f797b0fd | 28 | |
67e2ec02 PD |
29 | def main(): |
30 | """Main function.""" | |
31 | ||
32 | # prepare configuration | |
33 | args = configure_args() | |
5737b10e | 34 | warnings_handler = prepare_logger() |
38f15e57 PD |
35 | logging.info("The module restore_mail_inject.py started with user %s, folder %s and source %s.", |
36 | args.user, args.folder, args.srcdir) | |
67e2ec02 | 37 | |
38f15e57 | 38 | # connect to unix socket or server |
f72ba42a PD |
39 | psw = getpass.getpass() |
40 | session = MailIterator(args.user, psw) | |
f797b0fd | 41 | storage = FileIterator() |
67e2ec02 | 42 | |
e42bd6a5 PD |
43 | # retrieve mailbox list from the mailbox list file |
44 | mailbox_list = storage.load_mailbox_list(args.mboxlistfile) | |
45 | ||
67e2ec02 | 46 | # delete olf IMAP folders if no append requested |
e42bd6a5 | 47 | if not args.append: |
67e2ec02 | 48 | session.delete_mailboxes(args.folder) |
e42bd6a5 | 49 | if args.folder == "INBOX": |
67e2ec02 PD |
50 | session.clear_inbox_acls(args.user) |
51 | ||
52 | # inject emails | |
53 | path_generator = storage.load_mails(args.srcdir, args.folder) | |
54 | for message, mailbox, date_modified in path_generator: | |
f797b0fd | 55 | |
e42bd6a5 PD |
56 | # mailboxes marked for creating and acl update |
57 | # add acls after all subfolders or their acls will be derived from parent folder | |
67e2ec02 PD |
58 | for new_mailbox in storage.created_mailboxes: |
59 | session.create_mailbox(new_mailbox) | |
60 | for acl_mailbox in storage.acl_mailboxes: | |
e42bd6a5 PD |
61 | session.add_acls(acl_mailbox, mailbox_list, args.ouser, args.user) |
62 | storage.created_mailboxes = [] | |
63 | storage.acl_mailboxes = [] | |
f797b0fd | 64 | |
67e2ec02 | 65 | session.inject_message(message, mailbox, date_modified) |
f797b0fd | 66 | |
e42bd6a5 PD |
67 | # last iteration mailboxes in case root mailbox has no e-mails for injection |
68 | for new_mailbox in storage.created_mailboxes: | |
69 | session.create_mailbox(new_mailbox) | |
70 | for acl_mailbox in storage.acl_mailboxes: | |
71 | session.add_acls(acl_mailbox, mailbox_list, args.ouser, args.user) | |
f797b0fd | 72 | |
38f15e57 | 73 | logging.info("Finished injecting mails. Exiting with code %s.", warnings_handler.detected_problems) |
5737b10e | 74 | return warnings_handler.detected_problems |
67e2ec02 PD |
75 | |
76 | def configure_args(): | |
77 | """Configure arguments and return them.""" | |
0cf4dc33 | 78 | |
67e2ec02 PD |
79 | # parse arguments |
80 | parser = argparse.ArgumentParser(description="Tool to inject mails via IMAP.") | |
f72ba42a PD |
81 | parser.add_argument('-s', '--sourcedir', dest='srcdir', action='store', |
82 | required=True, help='folder to read mail from') | |
67e2ec02 PD |
83 | parser.add_argument('-u', '--username', dest='user', action='store', |
84 | required=True, help='user to store mails to') | |
f72ba42a PD |
85 | parser.add_argument('-o', '--ouser', dest='ouser', action='store', |
86 | default="", help='name of the original user (=username if not specified)') | |
67e2ec02 PD |
87 | parser.add_argument('-f', '--foldername', dest='folder', action='store', |
88 | default="INBOX", help='folder to store mails to - if not specified we overwrite INBOX') | |
e42bd6a5 | 89 | parser.add_argument('-m', '--mboxlistfile', dest='mboxlistfile', action='store', |
67e2ec02 | 90 | default="", help='mboxlist file (flat file format) to read the ACLs from') |
67e2ec02 PD |
91 | parser.add_argument('-a', '--append', dest='append', action='store_true', |
92 | default=False, help="append mails, don't delete anything") | |
93 | args = parser.parse_args() | |
94 | ||
95 | if (args.folder != "INBOX"): | |
96 | args.folder = "INBOX/" + args.folder | |
97 | if (args.ouser == ""): | |
98 | args.ouser = args.user | |
99 | ||
100 | return args | |
101 | ||
f797b0fd PD |
102 | def prepare_logger(): |
103 | """Sets up the logging functionality""" | |
104 | ||
105 | # reset the log | |
106 | with open(LOG_FILENAME, 'w'): | |
107 | pass | |
108 | ||
109 | # add basic configuration | |
110 | logging.basicConfig(filename=LOG_FILENAME, | |
111 | format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
112 | level=LOG_FILE_LEVEL) | |
113 | ||
114 | # add a handler for a console output | |
5737b10e | 115 | default_logger = logging.getLogger('') |
f797b0fd PD |
116 | console = logging.StreamHandler() |
117 | console.setLevel(LOG_SHELL_LEVEL) | |
118 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
119 | console.setFormatter(formatter) | |
5737b10e | 120 | default_logger.addHandler(console) |
79c28101 | 121 | |
5737b10e PD |
122 | # add a handler for warnings counting |
123 | warnings_handler = WarningsHandler() | |
124 | warnings_handler.setLevel(LOG_UNCLEAN_EXIT_LEVEL) | |
125 | default_logger.addHandler(warnings_handler) | |
0cf4dc33 | 126 | |
5737b10e | 127 | return warnings_handler |
67e2ec02 | 128 | |
e42bd6a5 | 129 | if __name__ == "__main__": |
67e2ec02 | 130 | main() |