diff options
Diffstat (limited to 'new-mail-notify')
| -rwxr-xr-x | new-mail-notify | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/new-mail-notify b/new-mail-notify new file mode 100755 index 0000000..92f8bcf --- /dev/null +++ b/new-mail-notify @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from email.header import decode_header +from os import execlp +from xml.sax.saxutils import escape +import email +import mailbox +import sys +import re + +INBOX_DIR = '~/Mail/Gmail/INBOX' +N_ICON = '~/.icons/email.svg' +N_TIME = 15 * 1000 # 15 seconds +MAX_DETAIL = 6 + +def decode(string, charset = 'utf8'): + return ' '.join(d[0].decode(d[1] or charset) for d in decode_header(string)) + +def notify(messages): + if len(messages) == 0: + sys.exit(0) + title = "Новая почта (%d)" % len(messages) + m = [] + for message in messages[:MAX_DETAIL]: + detail = '<b>' + escape(message['Subject']) + '</b>\n' + detail += '<i>' + escape(message['From']) + '</i>' + m.append(detail) + details = '\n\n'.join(m) + details += '\n' + execlp('notify-send', 'notify-send', '-t', str(N_TIME), '-i', N_ICON, title, details) + +def get_messages(inbox): + box = mailbox.Maildir(inbox, factory=None) + msgs = [] + for key in sorted(box.iterkeys(), reverse=True): + try: + message = box[key] + except email.Errors.MessageParseError: + continue + if 'S' in message.get_flags(): + continue + + msgs += [{ + 'From': decode(message.get('From')), + 'Subject': decode(message.get('Subject')) + },] + return msgs + +if __name__ == '__main__': + notify(get_messages(INBOX_DIR)) |
