From d7a3bcd650d8dee4d31ba458ecf84a5bd1d16596 Mon Sep 17 00:00:00 2001 From: A Farzat Date: Thu, 24 Oct 2024 20:40:37 +0900 Subject: Add mailsync script Used to sync mails in parallel while also providing notifications. Based on the helper script provided with mutt-wizard by Luke Smith. https://github.com/LukeSmithxyz/mutt-wizard/blob/master/bin/mailsync Might try to add a man page later based on the one provided as well. https://github.com/LukeSmithxyz/mutt-wizard/blob/master/mailsync.1 --- bin/scripts/mailsync | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 bin/scripts/mailsync (limited to 'bin') diff --git a/bin/scripts/mailsync b/bin/scripts/mailsync new file mode 100755 index 0000000..edd0da1 --- /dev/null +++ b/bin/scripts/mailsync @@ -0,0 +1,45 @@ +#!/usr/bin/env sh + +# - Syncs mail for all accounts, or a single account given as an argument. +# - Displays a notification showing the number of new mails. +# - Displays a notification for each new mail with its subject displayed. +# - Runs notmuch to index new mail. + +MAILDIR="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}" +MBSYNCRC="${MBSYNCRC:-${XDG_CONFIG_HOME:-$HOME/.config}/isyncrc}" +MS_last_run="${XDG_STATE_HOME:-$HOME/.local/state}/.mailsynclastrun" + +# Run only if not already running in other instance +pgrep mbsync >/dev/null && { echo "mbsync is already running." >&2; exit ;} + +# Check account for new mail. Notify if there is new content. +syncandnotify() { + acc="$(printf %s "$account" | sed "s/.*\///")" + if [ -z "$opts" ]; then mbsync -c "$MBSYNCRC" "$acc"; else mbsync -c "$MBSYNCRC" "$opts" "$acc"; fi + newcount=$(find "$MAILDIR/$acc/"[Ii][Nn][Bb][Oo][Xx]/new/ -type f -newer "$MS_last_run" 2> /dev/null | sed '/^\s*$/d' | wc -l) + notify-send --app-name="mailsync" "Mailsync" "📬 $newcount new mail(s) in \`$acc\` account." +} + +# Sync accounts passed as argument or all. +if [ "$#" -eq "0" ]; then + accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")" +else + for arg in "$@"; do + [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1 + done + accounts=$* +fi + +# Parallelize multiple accounts +for account in $accounts; do + syncandnotify & +done + +wait + +notmuch new 2>/dev/null + +#Create a touch file that indicates the time of the last run of mailsync +touch "$MS_last_run" +# Update the corresponding block in the statusbar. +pkill -RTMIN+12 "${STATUSBAR:-dwmblocks}" -- cgit v1.2.3-70-g09d2