-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
106 lines (81 loc) · 2.61 KB
/
utils.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
import re
from gi.repository import Notify
from os.path import dirname, basename
from config import VIPs
def spammy_bogo(message):
spamh = message["x-bogosity"]
if spamh is not None:
try:
spambool = spamh.split(',')[0]
if spambool == "Spam":
# spam!
return True
except ValueError:
print("bogo couldn't split %s %s" % (spamh, message))
return False
def spammy_spamc(message):
spamh = message["x-spam-flag"]
if spamh is not None:
try:
if spamh == "YES":
# spam!
return True
except ValueError:
print("spamc header error %s: %s" % (spamh, message))
return False
def notify(message):
if spammy_spamc(message): return
Notify.init("new mail")
if message.is_multipart(): msg = "%s attachments ..." % len(message.get_payload())
else: msg = "%s ..." % message.get_payload(decode=True)[:100]
try:
n = Notify.Notification.new(
"\n".join([
'From: %s' % message.get("from", ""),
'To: %s' % message.get("to", ""),
'Subject: %s' % message.get("subject", "")
]),
msg,
"dialog-information")
n.set_timeout(30000)
n.show()
except Exception, e:
print(e)
def maildirname(maildir):
return basename(dirname(maildir._paths["new"]))
def file(src, dst, message, key):
# ignore any message that is a tickler message
if 'x-tickler' in message: return
# notify, but only if from a VIP
# extract addr from string in fmt "Joe P. Quinn <joe@quinn.com>"
from_header = message.get('from', '')
from_match = re.search("[^ <]+@.[^ >]+", from_header)
if from_match:
from_addr = from_match.group(0)
if from_addr in VIPs.keys():
notify(message)
else:
print "bad from address:", from_addr
# file away, no matter whether from VIP or not
print("mv %s/%s -> %s" % (maildirname(src), message.get("subject", ''), maildirname(dst)))
dst.lock()
dst.add(message)
dst.flush()
dst.unlock()
src.lock()
src.discard(key)
src.flush()
src.unlock()
def rm(src, key):
print("rm %s" % key)
src.lock()
src.discard(key)
src.flush()
src.unlock()
def mark_as_read(message):
print("marked as read", message)
message.add_flag('S') # ('S'een)
def uniq(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if x not in seen and not seen_add(x)]