2 restore-mail-inject.py - Tool to inject mails via IMAP
4 Copyright (c) 2012 Intra2net AG
10 MAILBOX_RESP = re.compile(r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)')
11 UIDVAL_RESP = re.compile(r'(?P<name>.*) \(UIDVALIDITY (?P<uidval>.*)\)')
14 """This class communicates with the e-mail server."""
17 # IMAP4_SSL for connection with an IMAP server
19 # list of tuples (uidvalidity, mailboxname) for the retrieved mailboxes
24 def __init__(self, server, username, password):
25 """Creates a connection and a user session."""
28 self.mail_con = imaplib.IMAP4_SSL(server)
29 self.mail_con.login(username, password)
30 print("Logged in as %s." % username)
32 self.logged_in = False
33 print("Could not log in as user " + username + ".")
38 _result, mailboxes = self.mail_con.list()
40 for mailbox in mailboxes:
41 mailbox = MAILBOX_RESP.match(mailbox.decode('iso-8859-1')).groups()
42 self.mailboxes.append(mailbox)
43 self.mailboxes = sorted(self.mailboxes, key=lambda box: box[2], reverse=True)
47 """Closes the connection and the user session."""
51 self.mail_con.logout()
55 def clear_inbox_acls(self, user):
56 """Resets the inbox acls for each user."""
58 hashref = self.mail_con.getacl("INBOX")
60 print("Could not get the acls of INBOX.")
62 for acluser in hashref:
65 #print(self.mail_con.setacl("INBOX", acluser, ""))
66 print("Reset acls on INBOX for user %s" % acluser)
68 print("Could not reset acls on INBOX for user %s" % acluser)
71 def add_acls(self, mailbox, mailbox_list, original_user, target_user):
72 """Add acls to mailbox."""
74 # change encoding to internal cyrus format and make folder absolute
75 mailbox = mailbox.replace("INBOX/", "user/" + original_user + "/")
76 mailbox = mailbox.replace(".", "^")
77 mailbox = mailbox.replace("/", ".")
79 # find folder to set all acls
81 mbox_acls = mailbox_list[mailbox]
83 # no rights for the mailbox were found
85 for acl_user in mbox_acls:
86 print(acl_user, target_user, original_user)
87 if acl_user != target_user and acl_user != original_user:
89 #self.mail_con.setacl(mailbox, thisaclref[0], thisaclref[1])
90 print("Set acls %s for user %s on mailbox %s." % (mbox_acls[acl_user], acl_user, mailbox))
92 print("Could not set acls %s for user %s on mailbox %s." % (mbox_acls[acl_user], acl_user, mailbox))
96 def delete_mailboxes(self, deleted_mailbox):
97 """Delete specified mailbox or empty inbox."""
98 for mailbox in self.mailboxes:
99 pattern = '^\"?' + deleted_mailbox
100 # if INBOX it cannot be deleted so add delimiter
101 if (deleted_mailbox == "INBOX"):
102 pattern += mailbox[1]
103 if re.compile(pattern).match(mailbox[2]):
104 result, data = self.mail_con.delete(mailbox[2])
106 print("Deleted mailbox %s" % mailbox[2])
108 print("Could not delete folder %s: %s" % (mailbox[2], data[0]))
111 def create_mailbox(self, mailbox):
112 """Create new mailbox to inject messages."""
113 if mailbox != "INBOX":
114 result, data = self.mail_con.create(mailbox)
116 print("Creating mailbox %s" % mailbox)
118 print("Could not create mailbox %s: %s" % (mailbox, data[0]))
121 def inject_message(self, message, mailbox, internal_date):
122 """Inject a message into a mailbox."""
124 #$imap->append_file($folder, $full_path, undef, "\\Seen", true)
126 result, data = self.mail_con.append(mailbox, "\\Seen",
127 internal_date, message)
128 print("Appending message to mailbox %s" % mailbox)
130 raise UserWarning("Could not append the e-mail." % message)