From 522faf4dc92b24e92b38829a555fc75ed8817271 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Sun, 9 Nov 2014 18:21:02 +0100 Subject: new imap mail checker module by @obb --- modules/imap.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 modules/imap.py diff --git a/modules/imap.py b/modules/imap.py new file mode 100644 index 0000000..5953ee3 --- /dev/null +++ b/modules/imap.py @@ -0,0 +1,58 @@ +# -*- coding: utf8 -*- + +from time import time +import imaplib + +""" +Module displaying the number of unread messages +on an IMAP inbox (configurable). + +@author obb +""" + + +class Py3status: + + def __init__(self): + self.service_name = 'Mail' + self.imap_server = '' + self.port = '993' + + self.user = '' + self.password = '' + + self.mailbox = 'INBOX' + self.criterion = 'UNSEEN' + + self.check_frequency = 60 + + def check_mail(self, json, i3status_config): + mail_count = self._get_mail_count() + + response = { + 'name': self.service_name + '_checker', + 'full_text': '{}: {}'.format(self.service_name, mail_count), + 'cached_until': time() + self.check_frequency + } + + new_mail_color = i3status_config['color_good'] + check_failed_color = i3status_config['color_bad'] + + if mail_count == 'N/A': + response['color'] = check_failed_color + elif mail_count != '0': + response['color'] = new_mail_color + + return (0, response) + + def _get_mail_count(self): + try: + connection = imaplib.IMAP4_SSL(self.imap_server, self.port) + connection.login(self.user, self.password) + connection.select(self.mailbox) + unseen_response = connection.search(None, self.criterion) + mails = unseen_response[1][0].split() + mail_count = len(mails) + return mail_count + except: + return 'N/A' -- cgit v1.3