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