-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_notify.py
62 lines (47 loc) · 1.77 KB
/
mail_notify.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import pyinotify
import mailbox
import pynotify
import os
import gtk
import re
from email.header import decode_header
from os.path import expanduser
MAILBOX = ["~/.mail/gmail/inbox", "~/.mail/work/inbox"] # mailboxes
MASK = pyinotify.IN_MOVED_TO # event to trigger
SOUND = "~/.mybashscripts/mail.wav" # notification sound
def parse_encoding(value):
_r = []
#"=?utf-8?B?eWFuZ2JvLCDmnajljZo=?=" <yangbo#xxx.com>
#WTF, From section in qq enterprise mailbox has a pair quotes around name
value = re.sub('"(.*?)"', r'\1', value)
for msg, enc in decode_header(value):
if enc == 'gb2312' or not enc:
_r.append((msg, 'gb18030'))
else:
_r.append((msg, enc))
return ' '.join(item[0].decode(item[1]) for item in _r)
def get_mbox_name(path):
#path's pattern: /home/ggarlic/.mail/gmail/inbox/new
mailbox_name = path.split("/")[-3]
return '[' + mailbox_name + ']'
def parse_mail(event):
with open(event.pathname, "r") as f:
msg = mailbox.MaildirMessage(f)
msg_from = parse_encoding(msg["From"])
msg_subject = parse_encoding(msg["subject"])
#todo: markup and multiline
notifier = pynotify.Notification(get_mbox_name(event.path)+msg_from,
msg_subject)
notifier.set_icon_from_pixbuf(gtk.Label()
.render_icon(gtk.STOCK_DIALOG_INFO,
gtk.ICON_SIZE_DIALOG))
os.system("aplay -q " + SOUND)
notifier.show()
wm = pyinotify.WatchManager()
pynotify.init("mail notifier")
notifier = pyinotify.Notifier(wm, parse_mail)
for mbox in MAILBOX:
wm.add_watch(expanduser(mbox)+"/new", MASK)
notifier.loop()