mail: Add checkmail routine

Added script to call for checking mail. Will run individual pre- and
post-hooks. (By default, imapfilter and notmuch update.)

Simple options can be changed through environment variables:

Hooks can be set to different scripts with MBSYNC_PRE and
MBSYNC_POST, respectively.

Will also run mbsync repeatedly, until it exits without error. By
default it will try running it 3 times before giving up, this can be set
with MBSYNC_MAX_TRIES.

No functional imapfilters have been added yet.

mbsync and imapfilter both adhere to the XDG base directory layout
(somewhat), checking for their option files in .config/{isync,imapfilter}
respectively.

Everything still pending changes, the mail eco-system is hard to
wrap one's head around.
This commit is contained in:
Marty Oehme 2020-09-16 18:49:08 +02:00
parent 674b1cfeac
commit 3bbb7d0360
Signed by: Marty
GPG key ID: B7538B8F50A1C800
7 changed files with 112 additions and 12 deletions

57
mail/.local/bin/checkmail Executable file
View file

@ -0,0 +1,57 @@
#!/usr/bin/env sh
# runs mbsync, with pre-hooks and post-hooks
# by default, first runs imapfilter
# and after indexes with notmuch
#
# To run without invoking any hooks or retry attempts,
# run `checkmail raw`
# this will simply invoke a single isync run on all
# boxes with the correct config file set.
#
# for more advanced, per-account/channel hooks, see
# https://sourceforge.net/p/isync/feature-requests/8/
MBSYNC_MAX_TRIES=3
MBSYNC_PRE="imapfilter"
MBSYNC_POST="notmuch new"
success="false"
tries=0
# fail the routine and optionally send a message why
fail() {
[ -n "$1" ] && echo "$1"
exit 1
}
checkmail() {
mbsync -c "$XDG_CONFIG_HOME/isync/mbsyncrc" -a
}
checkonline() {
# 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."
}
# Routine start
# check
# skip any retries and pre/post hooks, just run mbsync
if [ "$1" = "raw" ]; then
checkmail
exit
fi
$MBSYNC_PRE
while true; do
if checkmail; then
success="true"
fi
if [ $success = "true" ] || [ $tries -gt $MBSYNC_MAX_TRIES ]; then
fail "Maximum retries reached unsuccessfully."
fi
tries=$((tries + 1))
done
$MBSYNC_POST