summaryrefslogtreecommitdiff
path: root/bin/mailsync
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mailsync')
-rwxr-xr-xbin/mailsync47
1 files changed, 35 insertions, 12 deletions
diff --git a/bin/mailsync b/bin/mailsync
index cbd36ff..7bca2d0 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -24,6 +24,9 @@ eval "$(grep -h -- \
"$HOME/.config/zsh/.zshenv" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" \
"$HOME/.pam_environment" 2>/dev/null)"
+# For non-interactive shell (e.g. cron job) run only when the GPG key (in $GNUPGHOME or pass --homedir) is unlocked
+tty -s || (echo "dummy" | gpg --sign --batch --pinentry-mode error -o /dev/null > /dev/null 2>&1) || exit
+
export GPG_TTY="$(tty)"
[ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
@@ -45,7 +48,7 @@ case "$(uname)" in
notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-:0}; do
export DISPLAY="$x"
- notify-send --app-name="mutt-wizard" "$1" "$2"
+ notify-send --app-name="mutt-wizard" -- "$1" "$2"
done ;}
;;
esac
@@ -55,7 +58,7 @@ syncandnotify() {
case "$1" in
imap) mbsync -q "$2" ;;
pop) mpop -q "$2" ;;
- esac
+ esac || return 1
new=$(find\
"$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/new/ \
"$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/cur/ \
@@ -63,16 +66,17 @@ syncandnotify() {
newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
case 1 in
$((newcount > 5)) )
- echo "$newcount new mail for $2."
- [ -z "$MAILSYNC_MUTE" ] && notify "New Mail!" "📬 $newcount new mail(s) in \`$2\` account."
+ echo "$newcount new mails for $2." >/dev/tty
+ [ -z "$MAILSYNC_MUTE" ] && notify "New Mail!" "📬 $newcount new mails in \`$2\` account."
;;
$((newcount > 0)) )
- echo "$newcount new mail for $2."
+ echo "$newcount new mail(s) for $2." >/dev/tty
[ -z "$MAILSYNC_MUTE" ] &&
for file in $new; do
- # Extract and decode subject and sender from mail.
- subject="$(sed -n "/^Subject:/ s|Subject: *|| p" "$file" |
- perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')"
+ # Extract and decode subject and sender from mail.
+ subject=$(awk '/^Subject: / && ++n == 1,/^.*: / && ++i == 2' "$file" | sed '$d' |
+ perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' |
+ sed 's/^Subject: //' | tr -d '\n\t')
from="$(sed -n "/^From:/ s|From: *|| p" "$file" |
perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')"
from="${from% *}" ; from="${from%\"}" ; from="${from#\"}"
@@ -96,15 +100,34 @@ else
done || echo "error $arg"; done)"
fi
+pids= # PIDs of backgrounded subshells
+count=0 # number of accounts in $tosync
+failed=0 # number of failed sync attempts
for account in $tosync; do
+ count=$((count+1))
case $account in
- Channel*) syncandnotify imap "${account##* }" & ;;
- account*) syncandnotify pop "${account##* }" & ;;
- error*) echo "ERROR: Account ${account##* } not found." ;;
+ Channel*)
+ syncandnotify imap "${account##* }" &
+ pids="$pids $!"
+ ;;
+ account*)
+ syncandnotify pop "${account##* }" &
+ pids="$pids $!"
+ ;;
+ error*)
+ echo "ERROR: Account ${account##* } not found."
+ failed=$((failed+1))
+ ;;
esac
done
-wait
+IFS=' '
+for pid in $pids; do
+ wait $pid || failed=$((failed+1))
+done
+
+# Abort if no account sync succeeded
+[ "$count" -gt "$failed" ] || exit 1
notmuch new --quiet