Commit | Line | Data |
---|---|---|
67e2ec02 PD |
1 | ''' |
2 | restore-mail-inject.py - Tool to inject mails via IMAP | |
3 | ||
4 | Copyright (c) 2012 Intra2net AG | |
5 | ''' | |
6 | ||
e42bd6a5 | 7 | import sys, os |
67e2ec02 PD |
8 | import re |
9 | ||
10 | MAIL_FILENAME = re.compile("^[0-9]+\.$") | |
e42bd6a5 PD |
11 | MBOXFILE_LINE = re.compile("^(.*?)\t(?:\d )?default[\t ](.*)$") |
12 | ACL_STRING = re.compile("^(.*?)[\t ](.*?)\t(.*)$") | |
67e2ec02 PD |
13 | |
14 | class FileIterator: | |
15 | """This class iterates through the e-mail files.""" | |
16 | ||
17 | # class attributes | |
18 | # mailbox database? | |
19 | mboxdb = None | |
20 | # mailboxes created during file traversal | |
21 | created_mailboxes = None | |
22 | # mailboxes to update during file traversal | |
23 | acl_mailboxes = None | |
24 | ||
e42bd6a5 | 25 | def __init__(self): |
67e2ec02 | 26 | """Creates a connection and a user session.""" |
e42bd6a5 PD |
27 | self.created_mailboxes = [] |
28 | self.acl_mailboxes = [] | |
67e2ec02 PD |
29 | return |
30 | ||
31 | def __del__(self): | |
32 | """Closes the connection and the user session.""" | |
33 | return | |
34 | ||
67e2ec02 PD |
35 | def _message_read(self, filename): |
36 | """Retrieves a message from the message file.""" | |
37 | try: | |
38 | with open(filename, "r") as msgfile: | |
39 | message = msgfile.read() | |
40 | except: | |
41 | print("Could not open the e-mail file %s.", filename) | |
42 | return message | |
43 | ||
e42bd6a5 | 44 | def load_mailbox_list(self, mboxlistfile = ""): |
b0169e56 | 45 | mboxdb = {} |
e42bd6a5 PD |
46 | if mboxlistfile != "": |
47 | try: | |
48 | with open(mboxlistfile, 'r') as acl_file: | |
49 | for line in acl_file: | |
b0169e56 | 50 | acls = {} |
e42bd6a5 | 51 | linedata = MBOXFILE_LINE.match(line).groups() |
b0169e56 | 52 | #!!! test this condition |
e42bd6a5 PD |
53 | if len(linedata) == 0: |
54 | print("Illegal line in mailbox list dump: %s" % line) | |
55 | sys.exit() | |
b0169e56 | 56 | key = linedata[0] |
e42bd6a5 | 57 | aclstr = linedata[1] |
b0169e56 PD |
58 | |
59 | # loop through acl rights string and build dictionary of users and rights | |
e42bd6a5 | 60 | while(aclstr != ""): |
e42bd6a5 PD |
61 | acldata = ACL_STRING.match(aclstr).groups() |
62 | if len(acldata) == 0: | |
63 | print("Illegal line in mailbox list dump: %s" % line) | |
64 | sys.exit() | |
65 | aclstr = acldata[2] | |
b0169e56 PD |
66 | acls[acldata[0]]=acldata[1] |
67 | ||
68 | mboxdb[key] = acls | |
69 | ||
e42bd6a5 PD |
70 | except IOError: |
71 | print("Could not open mboxlist file %s." % mboxlistfile) | |
72 | return mboxdb | |
73 | ||
67e2ec02 PD |
74 | def load_mails(self, filepath, mailpath): |
75 | """Loads all e-mails from file hierarchy. | |
76 | This recursive generator always returns a tuple of | |
77 | the next found (e-mail, mailbox to store, internaldate).""" | |
67e2ec02 PD |
78 | #print("Entered directory %s -> %s." % (filepath, mailpath)) |
79 | try: | |
80 | filepath = os.path.abspath(filepath) | |
81 | os.chdir(filepath) | |
82 | except OSError: | |
83 | print("Can't open the directory %s." % filepath) | |
84 | return | |
85 | # mark mailboxes that should be created | |
86 | self.created_mailboxes.append(mailpath) | |
87 | subpaths = os.listdir(filepath) | |
88 | for subpath in subpaths: | |
89 | #print("Now checking subpath %s in %s" % (subpath, filepath)) | |
e42bd6a5 | 90 | if subpath == "." or subpath == "..": |
67e2ec02 PD |
91 | continue |
92 | new_filepath = filepath + "/" + subpath | |
93 | if (os.path.isfile(new_filepath)): | |
e42bd6a5 | 94 | if os.path.getsize(new_filepath) == 0: |
67e2ec02 PD |
95 | print("Skipping empty file %s." % subpath) |
96 | else: | |
e42bd6a5 PD |
97 | if MAIL_FILENAME.match(subpath): |
98 | #print("Injecting file %s." % subpath) | |
67e2ec02 PD |
99 | try: |
100 | message = self._message_read(new_filepath) | |
101 | # suggest file modification date for internaldate | |
102 | yield (message, mailpath, os.path.getmtime(new_filepath)) | |
103 | except: | |
b0169e56 | 104 | print("Could not retrieve mail from file: %s" % new_filepath) |
67e2ec02 | 105 | else: |
e42bd6a5 | 106 | if os.path.isdir(new_filepath): |
67e2ec02 PD |
107 | # cyrus ^ vs . storage replacement |
108 | subpath = subpath.replace("^", ".") | |
109 | new_mailpath = mailpath + "/" + subpath | |
110 | #print("Inserting mails from directory %s into mailbox %s." % (new_filepath, new_mailpath)) | |
111 | # load_mails($mboxdbref, $origuser, $targetuser) | |
112 | rcrs_generator = self.load_mails(new_filepath, new_mailpath) | |
113 | # you enter the generator in the for loop | |
114 | for rcr in rcrs_generator: | |
115 | yield rcr | |
116 | #print("Done with directory %s and mailbox %s." % (new_filepath, new_mailpath)) | |
117 | # mark mailboxes that need acl update | |
118 | self.acl_mailboxes.append(mailpath) | |
119 | return | |
120 | # | |
121 | # | |
122 | # |