From f0ae1de9e1c897b2828f6ede82ffd9e66e8fcae7 Mon Sep 17 00:00:00 2001 From: Luke Smith Date: Mon, 13 Feb 2023 20:15:59 -0500 Subject: [PATCH] mail notifications back, close #907, fix #095 use MAILSYNC_MUTE variable, and non-silly commands to derive $from and $subject --- README.md | 2 ++ bin/mailsync | 51 ++++++++++++++++++++++++++++++--------------------- mailsync.1 | 7 +++++++ 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9db681f..5883014 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,8 @@ The mutt-wizard runs via the command `mw`. Once setup is complete, you'll use connecting online at all. - `-o` -- Configure mutt for an account, but do not keep mail offline. - `-p` -- Use POP protocol instead of IMAP (requires `mpop` installed). +- `mailsync` gives visual messages of new mail by default. Or, set + `MAILSYNC_MUTE=1` as an environmental variable if you prefer not having them. ## Neomutt user interface diff --git a/bin/mailsync b/bin/mailsync index 35a341c..537e0f1 100755 --- a/bin/mailsync +++ b/bin/mailsync @@ -33,23 +33,21 @@ lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun" # Settings are different for MacOS (Darwin) systems. case "$(uname)" in - Darwin) - notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;} - ;; + Darwin) notify() { osascript -e "display notification \"$2\" with title \"$1\"" ;} ;; *) - case "$(readlink -f /sbin/init)" in - *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;; - esac - # remember if a display server is running since `ps` doesn't always contain a display - pgrepoutput="$(pgrep -ax X\(\|org\|wayland\))" - displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)" - [ -z "$displays" ] && [ -d /tmp/.X11-unix ] && displays=$(cd /tmp/.X11-unix && for x in X*; do echo ":${x#X}"; done) - - notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do - export DISPLAY="$x" - notify-send --app-name="mutt-wizard" "New mail!" "📬 $2 new mail(s) in \`$1\` account." - done ;} - ;; + case "$(readlink -f /sbin/init)" in + *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;; + esac + # remember if a display server is running since `ps` doesn't always contain a display + pgrepoutput="$(pgrep -ax X\(\|org\|wayland\))" + displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)" + [ -z "$displays" ] && [ -d /tmp/.X11-unix ] && displays=$(cd /tmp/.X11-unix && for x in X*; do echo ":${x#X}"; done) + + notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do + export DISPLAY="$x" + notify-send --app-name="mutt-wizard" "$1" "$2" + done ;} + ;; esac # Check account for new mail. Notify if there is new content. @@ -63,10 +61,22 @@ syncandnotify() { "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/cur/ \ -type f -newer "$lastrun" 2> /dev/null) newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l) - case "$newcount" in - 0) echo "No new mail for $2." ;; - *) echo "$newcount new mail for $2." - [ -z "$MAILSYNC_MUTE" ] && notify "$2" "$newcount" ;; + case 1 in + $((newcount > 5)) ) + echo "$newcount new mail for $2." + [ -z "$MAILSYNC_MUTE" ] && notify "New Mail!" "📬 $newcount new mail(s) in \`$2\` account." + ;; + $((newcount > 0)) ) + echo "$newcount new mail for $2." + [ -z "$MAILSYNC_MUTE" ] && + for file in $new; do + # Extract subject and sender from mail. + from="$(sed -n "/^From:/ s/\(.*: \| *<.*\)//g p" "$file")" + subject="$(sed -n "/^Subject:/ s|.*: || p" "$file")" + notify "📧$from:" "$subject" + done + ;; + *) echo "No new mail for $2." ;; esac } @@ -91,7 +101,6 @@ for account in $tosync; do esac done - wait notmuch new --quiet diff --git a/mailsync.1 b/mailsync.1 index 236a418..e9bdd6d 100644 --- a/mailsync.1 +++ b/mailsync.1 @@ -35,6 +35,13 @@ sync all mail accounts and notify user if there is new mail only sync the .B account@example.org account. +.SH CONFIGURATION +While +.B +mailsync +gives notifications on the arrival of new mail by default, this can be diabled by setting the variable +.I +MAILSYNC_MUTE=1. .SH AUTHORS Written by Luke Smith originally in 2018. .SH LICENSE