From d79b3e6c9a904d8fc4f28f85ea60cb6ab040164b Mon Sep 17 00:00:00 2001 From: gxlin Date: Sat, 26 Jun 2021 10:49:25 +0800 Subject: Notification of offlineimap --- .aliases | 3 +++ .gitattributes | 2 +- .offlineimap.py | 13 ------------- .offlineimaprc | Bin 1063 -> 0 bytes .zshrc | 1 + offlineimap/.offlineimaprc | Bin 0 -> 1140 bytes offlineimap/offlineimap.py | 13 +++++++++++++ offlineimap/postsynchook.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 .aliases delete mode 100644 .offlineimap.py delete mode 100644 .offlineimaprc create mode 100644 offlineimap/.offlineimaprc create mode 100644 offlineimap/offlineimap.py create mode 100755 offlineimap/postsynchook.sh diff --git a/.aliases b/.aliases new file mode 100644 index 0000000..5d4ad13 --- /dev/null +++ b/.aliases @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +alias mu4e='emacs -f mu4e' diff --git a/.gitattributes b/.gitattributes index 25a9039..f53df20 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,4 @@ .emacs.d/init-mu4e.el filter=git-crypt diff=git-crypt -.offlineimaprc filter=git-crypt diff=git-crypt +offlineimap/.offlineimaprc filter=git-crypt diff=git-crypt .muttrc filter=git-crypt diff=git-crypt .authinfo filter=git-crypt diff=git-crypt diff --git a/.offlineimap.py b/.offlineimap.py deleted file mode 100644 index c304ef4..0000000 --- a/.offlineimap.py +++ /dev/null @@ -1,13 +0,0 @@ -import re - -def gmail_nametrans(foldername): - return re.sub('^\[gmail\].', '', - re.sub('all_mail', 'archive', - re.sub('sent_mail', 'sent', - re.sub('starred', 'flagged', - re.sub('bin', 'trash', - re.sub(' ', '_', foldername.lower())))))) - -def gmail_folderfilter(foldername): - return foldername in ('INBOX', '[Gmail]/Starred', - '[Gmail]/Drafts', '[Gmail]/All Mail') diff --git a/.offlineimaprc b/.offlineimaprc deleted file mode 100644 index 87cfe5a..0000000 Binary files a/.offlineimaprc and /dev/null differ diff --git a/.zshrc b/.zshrc index ed96e66..39a53bb 100644 --- a/.zshrc +++ b/.zshrc @@ -64,3 +64,4 @@ if ! zplug check --verbose; then fi zplug load +source $HOME/.dotfiles/.aliases diff --git a/offlineimap/.offlineimaprc b/offlineimap/.offlineimaprc new file mode 100644 index 0000000..918f26b Binary files /dev/null and b/offlineimap/.offlineimaprc differ diff --git a/offlineimap/offlineimap.py b/offlineimap/offlineimap.py new file mode 100644 index 0000000..c304ef4 --- /dev/null +++ b/offlineimap/offlineimap.py @@ -0,0 +1,13 @@ +import re + +def gmail_nametrans(foldername): + return re.sub('^\[gmail\].', '', + re.sub('all_mail', 'archive', + re.sub('sent_mail', 'sent', + re.sub('starred', 'flagged', + re.sub('bin', 'trash', + re.sub(' ', '_', foldername.lower())))))) + +def gmail_folderfilter(foldername): + return foldername in ('INBOX', '[Gmail]/Starred', + '[Gmail]/Drafts', '[Gmail]/All Mail') diff --git a/offlineimap/postsynchook.sh b/offlineimap/postsynchook.sh new file mode 100755 index 0000000..7262a50 --- /dev/null +++ b/offlineimap/postsynchook.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +clean () { + sed "s/^\[/\\\[/g" | sed "s/\"/'/g" | sed 's/\!/❕/g' +} + +get_sender () { + grep -o '^From: \(.*\)$' $1 | sed "s/^From: //g" | clean +} + +get_subject () { + grep -o '^Subject: \(.*\)$' $1 | sed "s/^Subject: //g" | perl -pe 'use MIME::Words(decode_mimewords); $_=decode_mimewords($_);' | clean +} + +notify () { + account=$1 + previous_messages=/tmp/offlineimap-$account-messages + maildir="$HOME/.mail/$account/inbox/new" + + if [ ! -e $previous_messages ]; then + touch $previous_messages + fi + + for file in $maildir/*; do + if [ ! -f $file ]; then + continue + fi + + filename=$(basename $file) + if grep -Fxq $filename $previous_messages; then + : echo "seen" + else + echo $filename >> $previous_messages + sender="`get_sender $file`" + subject=`get_subject $file` + message="New message for $account" + + if [ -e /usr/local/bin/terminal-notifier ]; then + terminal-notifier -title $subject -subtitle $sender -message $message -sender 'org.gnu.Emacs' -activate 'org.gnu.Emacs' + fi + fi + done +} + +mu index +notify $1 -- cgit v1.2.3