From 5eb3384f04442becef4ca74ee51efed3a755ffd1 Mon Sep 17 00:00:00 2001 From: Anton Bobov Date: Mon, 30 Jan 2012 20:45:23 +0600 Subject: Initial commit. --- new-mail-notify | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 new-mail-notify (limited to 'new-mail-notify') 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 = '' + escape(message['Subject']) + '\n' + detail += '' + escape(message['From']) + '' + 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)) -- cgit v1.2.3