Skip to content

Commit

Permalink
Finalizing #4 and providing an abstract mail server handler
Browse files Browse the repository at this point in the history
  • Loading branch information
glaslos committed Jan 30, 2013
1 parent 63e00b0 commit d571c39
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
23 changes: 22 additions & 1 deletion modules/mail_util.py
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
from modules.protocols import imap_util, pop_util
from modules.protocols import imap_util, pop_util


class MailUtil(object):

def __init__(self):
self.imap_handler = imap_util.IMAPUtil()
self.pop_handler = pop_util.POPUtil()

def request(self, account):
protocol_handler = None
if account.protocol == "imap":
self.imap_handler.imap_connect(account.user_name,
account.password,
account.hostname)
protocol_handler = self.imap_handler
elif account.protocol == "pop":
self.pop_handler.pop_connect(account.user_name,
account.password,
account.hostname)
protocol_handler = self.pop_handler
return protocol_handler
15 changes: 13 additions & 2 deletions modules/protocols/imap_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ class IMAPUtil(object):
def __init__(self):
pass

def imap_connect(self, user_name, password, hostname, port=143, keyfile=None, certfile=None):
self.mail = imaplib.IMAP4_SSL(hostname, port, keyfile, certfile)
def imap_connect(self, user_name, password, hostname, ssl=False, keyfile=None, certfile=None):
if ":" in hostname:
host, port = hostname.split(":", 1)
port = int(port)
if ssl:
self.mail = imaplib.IMAP4_SSL(host, port, keyfile, certfile)
else:
self.mail = imaplib.IMAP4(host, port)
else:
if ssl:
self.mail = imaplib.IMAP4_SSL(hostname, keyfile, certfile)
else:
self.mail = imaplib.IMAP4(hostname)
self.mail.login(user_name, password)

def get_stats(self):
Expand Down
17 changes: 13 additions & 4 deletions modules/protocols/pop_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

from email.parser import Parser

from pprint import pprint


class POPUtil(object):

def __init__(self):
pass

def pop_connect(self, user_name, password, hostname, port=110, keyfile=None, certfile=None):
self.M = poplib.POP3_SSL(hostname, port, keyfile, certfile)
def pop_connect(self, user_name, password, hostname, ssl=False, keyfile=None, certfile=None):
if ":" in hostname:
host, port = hostname.split(":", 1)
port = int(port)
if ssl:
self.M = poplib.POP3_SSL(host, port, keyfile, certfile)
else:
self.M = poplib.POP3(host, port)
else:
if ssl:
self.M = poplib.POP3_SSL(hostname, keyfile, certfile)
else:
self.M = poplib.POP3(hostname)
self.M.set_debuglevel(0)
self.M.user(user_name)
self.M.pass_(password)
Expand Down
49 changes: 8 additions & 41 deletions web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from jinja2 import Environment, FileSystemLoader

import database
from modules import imap_util, imap_ssl_util, pop_util, pop_ssl_util, maildir_utils
from modules import mail_util, maildir_utils

if not os.path.exists("data/"):
os.makedirs("data/")
Expand All @@ -21,35 +21,17 @@
db = database.Database()
mdir = maildir_utils.MaildirUtil()

imap_handler = imap_util.IMAPUtil()
imap_ssl_handler = imap_ssl_util.IMAPSUtil()
pop_handler = pop_util.POPUtil()
pop_ssl_handler = pop_ssl_util.POPSUtil()
mail_handler = mail_util.MailUtil()

accounts = db.fetch_all()


def get_account_stats(account):
if account.protocol == "imap":
imap_handler.imap_connect(account.user_name,
account.password,
account.hostname)
account.count = imap_handler.get_stats()
elif account.protocol == "imaps":
imap_ssl_handler.imaps_connect(account.user_name,
account.password,
account.hostname)
account.count = pop_handler.get_stats()
elif account.protocol == "pop":
pop_handler.pop_connect(account.user_name,
account.password,
account.hostname)
account.count = pop_handler.get_stats()
elif account.protocol == "pops":
pop_ssl_handler.pops_connect(account.user_name,
account.password,
account.hostname)
account.count = pop_ssl_handler.get_stats()
protocol_handler = mail_handler.request(account)
if protocol_handler:
account.count = protocol_handler.get_stats()
else:
raise Exception("Invalid account: {0}".format(account))

for account in accounts:
get_account_stats(account)
Expand Down Expand Up @@ -113,22 +95,7 @@ def add_account():
account_config["protocol"] = request.forms.get('protocol')
account_config["smtp_host"] = request.forms.get('smtp_host')
try:
if account.protocol == "imap":
imap_handler.imap_connect(account_config["user_name"],
account_config["password"],
account_config["hostname"])
elif account.protocol == "imaps":
imap_ssl_handler.imaps_connect(account_config["user_name"],
account_config["password"],
account_config["hostname"])
elif account.protocol == "pop":
pop_handler.pop_connect(account.user_name,
account.password,
account.hostname)
elif account.protocol == "pops":
pop_ssl_handler.pops_connect(account.user_name,
account.password,
account.hostname)
mail_handler.request(account)
except Exception as e:
error = "Connection error ({0})".format(e)
else:
Expand Down

0 comments on commit d571c39

Please sign in to comment.