def __init__(self):
"""Creates a connection and a user session."""
+
self.created_mailboxes = []
self.acl_mailboxes = []
- return
- def __del__(self):
- """Closes the connection and the user session."""
return
@classmethod
def _message_read(cls, filename):
"""Retrieves a message from the message file."""
+
try:
with open(filename, "r") as msgfile:
message = msgfile.read()
except IOError:
logging.warning("Could not open the e-mail file %s", filename)
raise
+
return message
@classmethod
def load_mailbox_list(cls, mboxlistfile = ""):
- """Load the list of mailboxes and acl rights for each from file."""
+ """Load the list of mailboxes and acl rights for each from file."""
+
mboxdb = {}
if mboxlistfile != "":
try:
mboxdb[key] = acls
except IOError:
logging.warning("Could not open mboxlist file %s", mboxlistfile)
+
return mboxdb
def load_mails(self, filepath, mailpath):
"""Loads all e-mails from file hierarchy.
This recursive generator always returns a tuple of
the next found (e-mail, mailbox to store, internaldate)."""
+
logging.debug("Entered directory %s -> %s", filepath, mailpath)
try:
filepath = os.path.abspath(filepath)
logging.debug("Done with directory %s and mailbox %s", new_filepath, new_mailpath)
# mark mailboxes that need acl update
self.acl_mailboxes.append(mailpath)
+
return
\ No newline at end of file
# logged in status
logged_in = None
- def __init__(self, username, server = "/var/imap/socket/imap"):
+ def __init__(self, username, server = "intranator.m.i2n"):
"""Creates a connection and a user session."""
# connect to server
try:
- self.mail_con = imaplib.IMAP4("intranator.m.i2n")
+ self.mail_con = imaplib.IMAP4(server)
# MODIFIED
imap_socket = socket.socket(socket.AF_UNIX)
- imap_socket.connect(server)
+ imap_socket.connect("/var/imap/socket/imap")
self.mail_con.sock = imap_socket
self.mail_con.file = self.mail_con.sock.makefile('rb')
logging.info("Connected to mail server %s", server)
def __del__(self):
"""Closes the connection and the user session."""
- #if self.logged_in:
- # self.mail_con.close()
- # self.mail_con.logout()
+
+ if self.logged_in:
+ self.mail_con.logout()
def clear_inbox_acls(self, user):
"""Resets the inbox acls for a given user."""
+
try:
_result, inbox_acls = self.mail_con.getacl("INBOX")
except self.mail_con.error as ex:
logging.debug("Reset acls on INBOX for user %s", acl_ref[0].decode('iso-8859-1'))
except self.mail_con.error as ex:
logging.warning("Could not reset acls on INBOX for user %s: %s", acl_ref[0], ex)
+
return
def add_acls(self, mailbox, mailbox_list, original_user, target_user):
def delete_mailboxes(self, deleted_mailbox):
"""Delete specified mailbox or empty inbox."""
+
for mailbox in self.mailboxes:
pattern = '^\"?' + deleted_mailbox
# if INBOX it cannot be deleted so add delimiter
if result == "OK":
logging.debug("Deleted mailbox %s", mailbox[2])
else:
- logging.warning("Could not delete mailbox %s: %s", mailbox[2], data[0])
+ logging.warning("Could not delete mailbox %s: %s", mailbox[2], data[0])
+
return
def create_mailbox(self, mailbox):
+
"""Create new mailbox to inject messages."""
if mailbox != "INBOX":
result, data = self.mail_con.create(mailbox)
logging.debug("Creating mailbox %s", mailbox)
else:
logging.warning("Could not create mailbox %s: %s", mailbox, data[0])
+
return
def inject_message(self, message, mailbox, internal_date):
+
"""Inject a message into a mailbox."""
result, data = self.mail_con.append(mailbox, "\\Seen", internal_date, message.encode())
if result == "OK":
logging.debug("Appending message to mailbox %s", mailbox)
else:
logging.warning("Could not append the e-mail %s: %s", message, data[0])
+
return
\ No newline at end of file