summaryrefslogtreecommitdiffstats
path: root/py3status/modules/imap.py
blob: 5953ee3a9a005388c6736eb487eb43ada2ba3847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 = '<IMAP_SERVER>'
        self.port = '993'

        self.user = '<USERNAME>'
        self.password = '<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'