summaryrefslogtreecommitdiff
path: root/bin/mailsync
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mailsync')
-rwxr-xr-xbin/mailsync67
1 files changed, 27 insertions, 40 deletions
diff --git a/bin/mailsync b/bin/mailsync
index d2394f9..53f639e 100755
--- 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