commit 2a60bfb6d91c8a96b2c0607c73f356fa55f07d87
parent 4773839af2b143190ddfc95db19d3fdbaeb958f4
Author: Luke Smith <luke@lukesmith.xyz>
Date:   Mon, 13 Feb 2023 15:21:28 -0500
simplify mailsync, remove silly option passing
Diffstat:
3 files changed, 32 insertions(+), 54 deletions(-)
diff --git a/bin/mailsync b/bin/mailsync
@@ -43,10 +43,10 @@ case "$(uname)" in
 		# 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)
+		[ -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
+				export DISPLAY="$x"
 				notify-send --app-name="mutt-wizard" "New mail!" "📬 $2 new mail(s) in \`$1\` account."
 			done ;}
 		;;
@@ -54,55 +54,42 @@ esac
 
 # Check account for new mail. Notify if there is new content.
 syncandnotify() {
-	acc="$(echo "$account" | sed "s/.*\///")"
-	if [ "$1" = "pop" ]; then
-		# Handle POP
-		mpop "$acc"
-	else
-		# Handle IMAP
-		if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
-	fi
+	case "$1" in
+		imap) mbsync -q "$2" ;;
+		pop) mpop -q "$2" ;;
+	esac
 	new=$(find\
-		"$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/new/ \
-		"$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/cur/ \
+		"$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/new/ \
+		"$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)
 	[ -z "$MAILSYNC_MUTE" ] && case 1 in
-		$((newcount > 0)) ) notify "$acc" "$newcount" ;;
+		$((newcount > 0)) ) notify "$2" "$newcount" ;;
 	esac
 }
 
-# Sync accounts passed as argument or all.
-if [ "$#" -gt "0" ]; then
-	for arg in "$@"; do
-		[ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
-	done
-	accounts=$*
+allaccounts="$(grep -hs "^\(Channel\|account\)" "$MBSYNCRC" "$MPOPRC")"
+
+# Get accounts to sync. All if no argument. Prefix with `error` if non-existent.
+IFS='
+'
+if [ -z "$1" ]; then
+	tosync="$allaccounts"
+else
+	tosync="$(for arg in "$@"; do for availacc in $allaccounts; do
+		[ "$arg" = "${availacc##* }" ] && echo "$availacc" && break
+	done || echo "error $arg"; done)"
 fi
-[ -z "$imap_accounts" ] && [ -r "$MBSYNCRC" ] && imap_accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC" 2>/dev/null)"
-[ -z "$pop_accounts" ] && [ -r "$MPOPRC" ] && pop_accounts="$(awk '/^account/ {print $2}' "$MPOPRC" 2>/dev/null)"
-
-# Parallelize multiple accounts
-for account in $imap_accounts; do
-	if [ -n "$accounts" ]; then
-		for tmp_ac in $accounts; do
-			[ "$tmp_ac" = "$account" ] && syncandnotify "imap" &
-		done
-		continue
-	fi
-	syncandnotify "imap" &
-done
 
-for account in $pop_accounts; do
-	if [ -n "$accounts" ]; then
-		for tmp_ac in $accounts; do
-			[ "$tmp_ac" = "$account" ] && syncandnotify "pop" &
-		done
-		continue
-	fi
-	syncandnotify "pop" &
+for account in $tosync; do
+	case $account in
+		Channel*) syncandnotify imap "${account##* }" & ;;
+		account*) syncandnotify pop "${account##* }" & ;;
+		error*) echo "ERROR: Account ${account##* } not found." ;;
+	esac
 done
 
+
 wait
 
 notmuch new --quiet
diff --git a/mailsync.1 b/mailsync.1
@@ -18,14 +18,11 @@ or if account names are given, syncs only those accounts.
 
 .B
 mailsync
-can also pass on options to
-.B
-mbsync,
-which it uses to sync mail.
-.B
-mailsync
 is a wrapper for
-.B mbsync,
+.B mbsync
+and
+.B
+mpop,
 but also automatically reindexes new mail with
 .B notmuch,
 gives notifications if new mail is found and can also be set as a cronjob to sync and index mail quietly in the background.
@@ -38,13 +35,6 @@ sync all mail accounts and notify user if there is new mail
 only sync the
 .B account@example.org
 account.
-.TP
-.B
--*
-See the
-.B
-mbsync
-manual for additional options that can be used.
 .SH AUTHORS
 Written by Luke Smith <luke@lukesmith.xyz> originally in 2018.
 .SH LICENSE
diff --git a/share/mailcap b/share/mailcap
@@ -7,3 +7,4 @@ audio/*; mpv %s ;
 application/pdf; /usr/local/lib/mutt-wizard/openfile %s ;
 application/pgp-encrypted; gpg -d '%s'; copiousoutput;
 application/pgp-keys; gpg --import '%s'; copiousoutput;
+application/x-subrip; $EDITOR %s ;