From 9744cc04af54c49a311911cfcc9372a32cd470ba Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 9 Feb 2022 00:33:17 +0100 Subject: local mail setup --- .config/mbsyncrc | 20 ++++++++++++++++++++ bin/hibiki/mailsync | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 .config/mbsyncrc create mode 100755 bin/hibiki/mailsync diff --git a/.config/mbsyncrc b/.config/mbsyncrc new file mode 100644 index 0000000..30c5c0b --- /dev/null +++ b/.config/mbsyncrc @@ -0,0 +1,20 @@ +IMAPAccount gnzler +Host mail.gandi.net +User robert@gnzler.io +PassCmd "pass email/gnzler.io | head -n1" +SSLType IMAPS + +MaildirStore local +Path ~/Mail/gnzler/ +INBOX ~/Mail/gnzler/INBOX +SubFolders Verbatim + +IMAPStore gnzler +Account gnzler + +Channel primary +Far :gnzler: +Near :local: +Patterns INBOX Archive/2022 Drafts Sent lists +Create Near +Expunge both diff --git a/bin/hibiki/mailsync b/bin/hibiki/mailsync new file mode 100755 index 0000000..ae96bd0 --- /dev/null +++ b/bin/hibiki/mailsync @@ -0,0 +1,54 @@ +#!/bin/bash + +set -e +[ -n "$DEBUG" ] && set -x + +# +# mbsync call here: +# +mbsync -c "$HOME/.config/mbsyncrc" primary + +# +# notifications-related only from here: +# + +MAILDIR_PATTERN=$HOME/Mail/* +new_mail=$(find ${MAILDIR_PATTERN}/*/new/ -type f | wc -l) +old_mail=$(find ${MAILDIR_PATTERN}/*/cur/ -type f | wc -l) + +notify() { + notify-send \ + -i /usr/share/icons/mdi/scalable/email-plus.svg \ + -a mailsync \ + -c audible \ + "$1" "$2" +} + +decode() { + python -c 'import sys;import email.header;print("".join(list(map(lambda s: s[0] if (type(s[0]) == str) else s[0].decode(s[1] or "utf-8"), email.header.decode_header(sys.stdin.readlines()[0])))))' +} + +handle_new_msg() { + msg=$1 + + from=$(awk '/^From:/{print $2}' "$msg" | decode) + subject=$(awk '/^Subject:/{print}' "$msg" | cut -d' ' -f2- | decode) + to=$(awk '/^To:/{print $2}' "$msg" | decode) + + if [[ "$to" =~ (lists.sr.ht|noreply.github.com) ]]; then + subject="$to\n$subject" + fi + + notify "new mail from: $from" "$subject" +} + +if [ "$new_mail" -gt 0 ]; then + new_inbox=$(find ${MAILDIR_PATTERN}/INBOX/new/ -type f) + if [ -n "$new_inbox" ]; then + for msg in "${new_inbox[@]}"; do + handle_new_msg "$msg" + done + else + notify "new mail" "new: $new_mail / old: $old_mail" + fi +fi -- cgit 1.4.1