2022-01-28 17:32:06 +00:00
|
|
|
#!/usr/bin/env bash
|
2021-10-20 07:19:48 +00:00
|
|
|
#
|
|
|
|
# Runs mbsync, with pre-hooks and post-hooks
|
|
|
|
# by default, the pre-hook first runs imapfilter
|
|
|
|
# and afterwards the post-hook indexes with notmuch.
|
|
|
|
#
|
|
|
|
# At the very least, you will have to set up a gpg
|
|
|
|
# encrypted file containing the password for your
|
|
|
|
# remote mailbox. It can be set directly below or
|
|
|
|
# through the environment variable
|
|
|
|
# `MBSYNC_PASSWORD_FILE` and should point to any
|
|
|
|
# gpg decryptable file.
|
2020-09-16 16:49:08 +00:00
|
|
|
#
|
|
|
|
# To run without invoking any hooks or retry attempts,
|
2021-10-20 07:19:48 +00:00
|
|
|
# run `checkmail raw`.
|
2020-09-16 16:49:08 +00:00
|
|
|
# this will simply invoke a single isync run on all
|
|
|
|
# boxes with the correct config file set.
|
|
|
|
#
|
2021-10-20 07:19:48 +00:00
|
|
|
# To invoke mbsync for a specific group or channel,
|
|
|
|
# simply run it with `checkmail <channelname>` and
|
|
|
|
# it will only sync the correspondingly named.
|
|
|
|
#
|
|
|
|
# To run your own commands as hooks overwrite the
|
|
|
|
# environment variables `MBSYNC_PRE` and
|
|
|
|
# `MBSYNC_POST`. If you want to not have anything run
|
|
|
|
# for either of them the easiest way is to set the
|
|
|
|
# corresponding environment variable to true.
|
|
|
|
# Be careful which commands you put here since they
|
|
|
|
# will be evaluated without any special precautions.
|
|
|
|
#
|
2020-09-16 16:49:08 +00:00
|
|
|
# for more advanced, per-account/channel hooks, see
|
|
|
|
# https://sourceforge.net/p/isync/feature-requests/8/
|
|
|
|
|
2020-09-24 12:23:06 +00:00
|
|
|
# available env vars:
|
|
|
|
# MBSYNC_MAX_TRIES=3
|
|
|
|
# MBSYNC_PRE="/bin/usr/cmd-to-run"
|
|
|
|
# MBSYNC_POST="/bin/usr/cmd-to-run"
|
|
|
|
# MBSYNC_NOTIFY=1
|
2021-10-20 07:19:48 +00:00
|
|
|
# MBSYNC_PASSWORD_FILE="/path/to/gpg/file.gpg"
|
2020-09-24 08:00:47 +00:00
|
|
|
|
2022-01-28 17:32:06 +00:00
|
|
|
# What to run before and after decrypting the password file.
|
|
|
|
PASSWORD_CMD="pass open -t 1min"
|
|
|
|
# POST_PASSWORD_CMD=""
|
2021-03-16 13:37:38 +00:00
|
|
|
|
2020-09-24 08:00:47 +00:00
|
|
|
prehook() {
|
2021-08-18 22:15:44 +00:00
|
|
|
if [ -n "$MBSYNC_PRE" ]; then
|
|
|
|
eval "$MBSYNC_PRE"
|
|
|
|
return 0
|
|
|
|
fi
|
2023-02-25 16:11:00 +00:00
|
|
|
checkwarnuser
|
2021-10-20 07:19:48 +00:00
|
|
|
imapfilter -c "${XDG_CONFIG_HOME:-$HOME/.config}/imapfilter/config.lua"
|
2020-09-24 08:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
posthook() {
|
2021-08-18 22:15:44 +00:00
|
|
|
if [ -n "$MBSYNC_POST" ]; then
|
|
|
|
eval "$MBSYNC_POST"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
notmuch new 2>/dev/null
|
|
|
|
countnew
|
2020-09-24 12:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# use notmuch to index incoming mail and set the
|
|
|
|
# correct number of mails if new ones arrived for notifications
|
|
|
|
countnew() {
|
2021-08-18 22:15:44 +00:00
|
|
|
num=$(notmuch count tag:unread and tag:inbox)
|
|
|
|
HASMAIL="${num:-0}"
|
2020-09-24 08:00:47 +00:00
|
|
|
}
|
2020-09-16 16:49:08 +00:00
|
|
|
|
|
|
|
# fail the routine and optionally send a message why
|
|
|
|
fail() {
|
2021-08-18 22:15:44 +00:00
|
|
|
[ -n "$1" ] && echo "$1"
|
|
|
|
exit 1
|
2020-09-16 16:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkmail() {
|
2021-10-20 07:19:48 +00:00
|
|
|
mbsync -c "${XDG_CONFIG_HOME:-$HOME/.config}/isync/mbsyncrc" "${1:--a}"
|
2020-09-16 16:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkonline() {
|
2021-08-18 22:15:44 +00:00
|
|
|
# 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."
|
2020-09-16 16:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 12:23:06 +00:00
|
|
|
# warn user that he has to enter his password in a moment
|
|
|
|
# to stop catching him offguard or entering something by accident
|
|
|
|
checkwarnuser() {
|
2022-01-28 17:32:06 +00:00
|
|
|
enablegpgagent
|
2021-08-18 22:15:44 +00:00
|
|
|
if echo "$agt" | grep -qE 'No secret key'; then
|
|
|
|
notify "Mail" "Password phrase needed!"
|
|
|
|
sleep 2.5
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
enablegpgagent() {
|
2022-01-28 17:32:06 +00:00
|
|
|
[ -n "$agt" ] && return
|
|
|
|
|
|
|
|
if [ -n "$PASSWORD_CMD" ]; then
|
|
|
|
IFS=" " read -r -a PASSWORD_CMD <<<"$PASSWORD_CMD"
|
|
|
|
# shellcheck disable=SC2068
|
|
|
|
agt=$(${PASSWORD_CMD[@]})
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$POST_PASSWORD_CMD" ]; then
|
|
|
|
IFS=" " read -r -a POST_PASSWORD_CMD <<<"$POST_PASSWORD_CMD"
|
|
|
|
"${POST_PASSWORD_CMD[@]}"
|
|
|
|
fi
|
|
|
|
|
2021-08-18 22:15:44 +00:00
|
|
|
## exit program after first failed attempt
|
2022-01-28 17:32:06 +00:00
|
|
|
if echo "$agt" | grep -qE 'decryption failed' ||
|
|
|
|
echo "$agt" | grep -qE 'No such file'; then
|
2021-08-18 22:15:44 +00:00
|
|
|
notify "Mail" "Process aborted."
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-09-24 12:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# send out a notification on new mail, to libnotify and stdout
|
2021-10-20 07:19:48 +00:00
|
|
|
notify_on_new_mail() {
|
2021-08-18 22:15:44 +00:00
|
|
|
[ "${MBSYNC_NOTIFY:-1}" -eq 0 ] && return
|
2020-09-24 12:23:06 +00:00
|
|
|
|
2021-08-18 22:15:44 +00:00
|
|
|
if [ "${HASMAIL:-0}" -gt 0 ]; then
|
|
|
|
notify "Mail" "New Mail ($HASMAIL)"
|
|
|
|
fi
|
2020-09-24 12:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
notify() {
|
2021-08-18 22:15:44 +00:00
|
|
|
command -v notify-send >/dev/null && notify-send "$@"
|
|
|
|
echo "$@"
|
2020-09-24 12:23:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 16:49:08 +00:00
|
|
|
# Routine start
|
|
|
|
|
|
|
|
# check
|
|
|
|
|
|
|
|
# skip any retries and pre/post hooks, just run mbsync
|
|
|
|
if [ "$1" = "raw" ]; then
|
2021-08-18 22:15:44 +00:00
|
|
|
checkmail
|
|
|
|
exit
|
2021-10-20 07:19:48 +00:00
|
|
|
# any other argument passed through selects mbsync targets
|
|
|
|
elif [ -n "$1" ]; then
|
|
|
|
selected_mailbox="$1"
|
2020-09-16 16:49:08 +00:00
|
|
|
fi
|
|
|
|
|
2020-09-17 17:49:53 +00:00
|
|
|
main() {
|
2021-08-18 22:15:44 +00:00
|
|
|
enablegpgagent
|
|
|
|
prehook
|
2020-09-17 17:49:53 +00:00
|
|
|
|
2021-08-18 22:15:44 +00:00
|
|
|
tries=0
|
|
|
|
while true; do
|
2021-10-20 07:19:48 +00:00
|
|
|
if checkmail "$selected_mailbox"; then
|
2021-08-18 22:15:44 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
|
|
|
tries=$((tries + 1))
|
|
|
|
if [ $tries -gt "${MBSYNC_MAX_TRIES:-3}" ]; then
|
|
|
|
fail "maximum retries reached without success."
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
unset tries
|
2020-09-17 17:49:53 +00:00
|
|
|
|
2021-08-18 22:15:44 +00:00
|
|
|
posthook
|
2021-10-20 07:19:48 +00:00
|
|
|
notify_on_new_mail
|
2020-09-18 07:48:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 17:49:53 +00:00
|
|
|
main
|