From 1b53be92e0ae073e03d2730b7dce726926458944 Mon Sep 17 00:00:00 2001 From: Johannes Rothe Date: Sun, 20 May 2018 22:20:51 +0200 Subject: [PATCH] add script for offlineimap notifications --- mutt/muttrc | 3 --- offlineimap_notify.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 offlineimap_notify.py diff --git a/mutt/muttrc b/mutt/muttrc index adec03d..43dc9f0 100644 --- a/mutt/muttrc +++ b/mutt/muttrc @@ -1,7 +1,4 @@ # simplify folder change -# cronjob for offlineimap -# also update when the cursor is not moving -# dunst notifications # notmuch for searching # SMTP setting (smtp_pass is set in sourced file) diff --git a/offlineimap_notify.py b/offlineimap_notify.py new file mode 100644 index 0000000..1556428 --- /dev/null +++ b/offlineimap_notify.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +import pynotify +import pyinotify +from os.path import expanduser, join +from mailbox import MaildirMessage + +maildir = expanduser('~/Mail') +pynotify.init('offlineimap_notify') + +with open(expanduser('~/.mutt/mailboxes')) as f: + mailboxes = f.read() + +boxes = [] +for m in mailboxes.strip('\n').split(" "): + if not m == 'mailboxes': + boxes.append(m.replace('"', '').replace('+', '')) + + +def new_mail(event): + with open(event.pathname, 'r') as f: + mail = MaildirMessage(message=f) + print(mail) + efrom = 'From: ' + mail['From'] + print(efrom) + esubject = 'Subject: ' + mail['Subject'] + print(esubject) + n = pynotify.Notification("New mail in " + '/'.join( + event.path.split('/')[-3:-1]), efrom + "\n" + esubject) + n.set_timeout(8000) + n.show() + + +watch_manager = pyinotify.WatchManager() +file_notifier = pyinotify.Notifier(watch_manager, new_mail) + +for box in boxes: + watch_manager.add_watch(join(maildir, box, 'new'), + pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO) + +file_notifier.loop()