mail: Fix checkmail script password bugging

Fixed script to only ask for password once before understanding that the
user does not want to provide it.

Since gpg seems to not differentiate between a *wrongly* entered
password and one entered not at all, we have to assume the user canceled
the password prompt and not bug him again until the next time the script
is invoked.
It will, however, at least notify the user that the process is aborted
in a quick notification message, so that if it was done on accident the
user will still know what's up.
This commit is contained in:
Marty Oehme 2021-08-19 00:15:44 +02:00
parent f94759697d
commit 78b47a35b2
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -20,71 +20,81 @@
PASSWORD_FILE="$HOME/.local/share/pass/misc/aerc-gmail-app-password.gpg" PASSWORD_FILE="$HOME/.local/share/pass/misc/aerc-gmail-app-password.gpg"
prehook() { prehook() {
if [ -n "$MBSYNC_PRE" ]; then if [ -n "$MBSYNC_PRE" ]; then
eval "$MBSYNC_PRE" eval "$MBSYNC_PRE"
return 0 return 0
fi fi
imapfilter -c "${XDG_CONFIG_HOME:-/home/marty/.config}/imapfilter/config.lua" imapfilter -c "${XDG_CONFIG_HOME:-/home/marty/.config}/imapfilter/config.lua"
} }
posthook() { posthook() {
if [ -n "$MBSYNC_POST" ]; then if [ -n "$MBSYNC_POST" ]; then
eval "$MBSYNC_POST" eval "$MBSYNC_POST"
return 0 return 0
fi fi
notmuch new 2>/dev/null notmuch new 2>/dev/null
afew --tag --new afew --tag --new
afew --move-mail --new afew --move-mail --new
afew --tag --new afew --tag --new
notmuch_foldertags notmuch_foldertags
countnew countnew
} }
# use notmuch to index incoming mail and set the # use notmuch to index incoming mail and set the
# correct number of mails if new ones arrived for notifications # correct number of mails if new ones arrived for notifications
countnew() { countnew() {
num=$(notmuch count tag:unread and tag:inbox) num=$(notmuch count tag:unread and tag:inbox)
HASMAIL="${num:-0}" HASMAIL="${num:-0}"
} }
# fail the routine and optionally send a message why # fail the routine and optionally send a message why
fail() { fail() {
[ -n "$1" ] && echo "$1" [ -n "$1" ] && echo "$1"
exit 1 exit 1
} }
checkmail() { checkmail() {
mbsync -c "${XDG_CONFIG_HOME:-$HOME/.config}/isync/mbsyncrc" -a mbsync -c "${XDG_CONFIG_HOME:-$HOME/.config}/isync/mbsyncrc" -a
} }
checkonline() { checkonline() {
# Ping 1.1.1.1 to confirm that we are on the internet # Ping 1.1.1.1 to confirm that we are on the internet
ping -c 1 "1.1.1.1" >/dev/null 2>/dev/null || fail "checkmail can not access the internet." ping -c 1 "1.1.1.1" >/dev/null 2>/dev/null || fail "checkmail can not access the internet."
} }
# warn user that he has to enter his password in a moment # warn user that he has to enter his password in a moment
# to stop catching him offguard or entering something by accident # to stop catching him offguard or entering something by accident
checkwarnuser() { checkwarnuser() {
agt=$(gpg2 --decrypt --no-tty --quiet --no-verbose --for-your-eyes-only --pinentry-mode cancel "$PASSWORD_FILE" 2>&1) agt=$(gpg2 --decrypt --no-tty --quiet --no-verbose --for-your-eyes-only --pinentry-mode cancel "$PASSWORD_FILE" 2>&1)
if echo "$agt" | grep -qE 'No secret key'; then if echo "$agt" | grep -qE 'No secret key'; then
notify "Mail: Password phrase needed!" notify "Mail" "Password phrase needed!"
sleep 2.5 sleep 2.5
fi fi
}
enablegpgagent() {
## get password from user
agt=$(gpg2 --decrypt --no-tty --quiet --no-verbose --for-your-eyes-only --pinentry-mode ask "$PASSWORD_FILE" 2>&1)
## exit program after first failed attempt
if echo "$agt" | grep -qE 'decryption failed'; then
notify "Mail" "Process aborted."
exit 1
fi
} }
# send out a notification on new mail, to libnotify and stdout # send out a notification on new mail, to libnotify and stdout
notifymail() { notifymail() {
[ "${MBSYNC_NOTIFY:-1}" -eq 0 ] && return [ "${MBSYNC_NOTIFY:-1}" -eq 0 ] && return
if [ "${HASMAIL:-0}" -gt 0 ]; then if [ "${HASMAIL:-0}" -gt 0 ]; then
notify "($HASMAIL) new mail" notify "Mail" "New Mail ($HASMAIL)"
fi fi
} }
notify() { notify() {
command -v notify-send >/dev/null && notify-send "$@" command -v notify-send >/dev/null && notify-send "$@"
echo "$@" echo "$@"
} }
# Routine start # Routine start
@ -93,38 +103,40 @@ notify() {
# skip any retries and pre/post hooks, just run mbsync # skip any retries and pre/post hooks, just run mbsync
if [ "$1" = "raw" ]; then if [ "$1" = "raw" ]; then
checkmail checkmail
exit exit
fi fi
main() { main() {
checkwarnuser checkwarnuser
prehook enablegpgagent
tries=0 prehook
while true; do
if checkmail; then
break
fi
tries=$((tries + 1)) tries=0
if [ $tries -gt "${MBSYNC_MAX_TRIES:-3}" ]; then while true; do
fail "maximum retries reached without success." if checkmail; then
fi break
done fi
unset tries
posthook tries=$((tries + 1))
if [ $tries -gt "${MBSYNC_MAX_TRIES:-3}" ]; then
fail "maximum retries reached without success."
fi
done
unset tries
notifymail posthook
notifymail
} }
notmuch_foldertags() { notmuch_foldertags() {
notmuch tag +dump -inbox -deleted -- folder:Dump and not folder:Inbox notmuch tag +dump -inbox -deleted -- folder:Dump and not folder:Inbox
notmuch tag +archived -inbox -deleted -- folder:Archive and not folder:Inbox notmuch tag +archived -inbox -deleted -- folder:Archive and not folder:Inbox
notmuch tag +deleted -inbox -archived -- folder:Trash and not folder:Inbox notmuch tag +deleted -inbox -archived -- folder:Trash and not folder:Inbox
notmuch tag -inbox -- not folder:Inbox notmuch tag -inbox -- not folder:Inbox
} }
main main