-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailcheck.py
executable file
·127 lines (108 loc) · 4.02 KB
/
mailcheck.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python3
# TODO: use url = 'https://%s:%[email protected]/mail/feed/atom' % (email, password)
# see conky !aw
import imaplib
import email
import sys
import configparser
import os.path
from email.parser import HeaderParser
from email.header import decode_header
from hashlib import sha512
import subprocess
CONFIGFILE = os.path.expanduser('~') + '/.config/scripts/mailcheck.ini'
notified_file = '/home/spotlight/.config/mailcheck/notified'
parser = HeaderParser()
def check_notified(hash):
directory = os.path.split(notified_file)[0]
if not os.path.exists(directory):
os.makedirs(directory)
if not os.path.exists(notified_file):
with open(notified_file, 'x'):
pass
with open(notified_file, 'r+') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if line == hash:
return True
return False
def write_hash(hash):
with open(notified_file, 'a+') as f:
f.write(hash+"\n")
def checknew(imap, messages):
for i in messages:
result, data = imap.fetch(str(i), '(BODY.PEEK[HEADER])')
for response in data:
if isinstance(response, tuple):
header_data = response[1]
try: # email only works on strings, not bytes
header_data = header_data.decode('utf-8')
except:
pass
header = parser.parsestr(header_data)
subject = decode_header(header['Subject'])[0][0]
sender = decode_header(header['From'])
msg_id = decode_header(header['Message-Id'])[0][0]
msg_id = msg_id.encode('utf-8')
msg_id_hash = sha512(msg_id).hexdigest()
try:
sender_email = sender[0][0].split('<')[1].split('>')[0]
except:
sender_email = sender[0][0] # what'ev
if not check_notified(msg_id_hash):
subprocess.call(["notify-send", sender_email, subject])
write_hash(msg_id_hash)
else:
return
def getnewmails(host, user, pw):
imaplib.socket.setdefaulttimeout(2)
imap = imaplib.IMAP4_SSL(host)
imap.login(user, pw)
imap.select('inbox')
result, data = imap.search(None, "NOT SEEN")
data = data[0].decode('ascii')
try:
messages = data.split(" ")
except:
print("error while decoding/parsing messages")
imap.close()
imap.logout()
if len(messages) == 1 and messages[0]=='':
return 0
else:
checknew(imap, messages)
return len(messages)
if __name__ == '__main__':
width = 1920
try:
output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4 | cut -d "x" -f1',shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
width = int(output)
except:
pass
config = configparser.ConfigParser()
config.read(CONFIGFILE)
output_type = config.get('General', 'output', fallback='conky')
output = ''
for section in config.sections():
if section in ['General']:
continue
server = config.get(section, 'server', fallback='')
login = config.get(section, 'login', fallback='')
password = config.get(section, 'password', fallback='')
new_mails = 0
try:
new_mails = getnewmails(server, login, password)
except Exception as e:
new_mails = -1
if output_type == 'conky':
conky_pos = config.get(section, 'conky_pos' + str(width), fallback='')
if not server or not login or not password or not conky_pos:
continue
output += '${goto '+conky_pos+'}' + str(new_mails)
elif output_type == 'polybar':
prefix = config.get(section, 'prefix', fallback=section)
output += '%s %d ' % (prefix, new_mails)
else:
assert False, 'Output "%s" not supported!' % output_type
print(output)