mailchecker ; didn't really test it yet

This commit is contained in:
enkore 2013-02-15 21:14:39 +01:00
parent c4e80b3e40
commit 7e2c61c21a

View File

@ -6,9 +6,9 @@ import json
from datetime import datetime,timedelta from datetime import datetime,timedelta
import imaplib import imaplib
from i3pystatus import Module from i3pystatus import IntervalModule
class MailChecker(Module): class MailChecker(IntervalModule):
""" """
This class handles mailservers and outputs i3status compatible This class handles mailservers and outputs i3status compatible
json data for the accumulated unread count. The mail server json data for the accumulated unread count. The mail server
@ -16,8 +16,8 @@ class MailChecker(Module):
""" """
settings = { settings = {
'color': '#ff0000', "color": "#ff0000",
'servers': [] "servers": []
} }
servers = [] servers = []
@ -25,29 +25,29 @@ class MailChecker(Module):
def __init__(self, settings = None): def __init__(self, settings = None):
self.settings.update(settings) self.settings.update(settings)
for server in settings['servers']: for server in settings["servers"]:
srv = MailChecker.MailServer(server) srv = MailChecker.MailServer(server)
self.servers.append(srv) self.servers.append(srv)
def output(self): def run(self):
unread = 0 unread = sum([server.get_unread_count() for server in self.servers])
for srv in self.servers:
unread += srv.get_unread_count()
if not unread: if not unread:
return None return None
return {'full_text' : '%d new email%s' % (unread, ('s' if unread > 1 else '')), self.output = {
'name' : 'newmail', "full_text" : "%d new email%s" % (unread, ("s" if unread > 1 else "")),
'urgent' : 'true', "name" : "newmail",
'color' : self.settings['color']} "urgent" : "true",
"color" : self.settings["color"]
}
class MailServer: class MailServer:
""" """
This class provides the functionality to connect This class provides the functionality to connect
to a mail server and fetch the count of unread emails. to a mail server and fetch the count of unread emails.
When the server connection is lost, it returns 0 and When the server connection is lost, it returns 0 and
tries to reconnect. It checks every 'pause' seconds. tries to reconnect. It checks every "pause" seconds.
""" """
host = "" host = ""
@ -56,52 +56,36 @@ class MailChecker(Module):
username = "" username = ""
password = "" password = ""
connection = None connection = None
pause = 30
unread_cache = 0
last_checked = datetime.now()
def __init__(self, settings_dict): def __init__(self, settings_dict):
self.host = settings_dict['host'] self.host = settings_dict["host"]
self.port = settings_dict['port'] self.port = settings_dict["port"]
self.username = settings_dict['username'] self.username = settings_dict["username"]
self.password = settings_dict['password'] self.password = settings_dict["password"]
self.pause = settings_dict['pause']
if settings_dict['ssl']: if settings_dict["ssl"]:
self.imap_class = imaplib.IMAP4_SSL self.imap_class = imaplib.IMAP4_SSL
self.last_checked = \
datetime.now() - timedelta(seconds=self.pause)
def get_connection(self): def get_connection(self):
if not has_internet_connection(): if not self.connection:
self.connection = None
else:
if not self.connection:
try:
self.connection = self.imap_class(self.host, self.port)
self.connection.login(self.username, self.password)
self.connection.select()
except Exception:
self.connection = None
try: try:
self.connection = self.imap_class(self.host, self.port)
self.connection.login(self.username, self.password)
self.connection.select() self.connection.select()
except Exception as e: except Exception:
self.connection = None self.connection = None
try:
self.connection.select()
except Exception as e:
self.connection = None
return self.connection return self.connection
def get_unread_count(self): def get_unread_count(self):
delta = datetime.now() - self.last_checked unread = 0
conn = self.get_connection()
if conn:
unread += len(conn.search(None,"UnSeen")[1][0].split())
if delta.total_seconds() > self.pause: return unread
unread = 0
conn = self.get_connection()
if conn:
unread += len(conn.search(None,'UnSeen')[1][0].split())
self.unread_cache = unread
self.last_checked = datetime.now()
return self.unread_cache