move offlineimap spawning to inside the loop in case it crashes, include some explanatory comments

This commit is contained in:
Iris Lightshard 2021-08-24 23:10:03 -06:00
parent 004cb7ff56
commit a73e912971
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398

View file

@ -19,13 +19,6 @@
. ./.config
if [ "$1" != -l ]; then
if ! pgrep offlineimap; then
offlineimap &
fi
fi
oldnew=0
summary() {
@ -45,25 +38,43 @@ summary() {
}
while true; do
newnew=$(ls -1 ${inbox}/new | wc -l)
if [ ${newnew} -gt ${oldnew} ]; then
if [ "$1" = -l ]; then
echo "$(summary)" \
| mail -r ${addr} \
-s "new mail [${newnew}]" \
${phone}
else
echo "$(summary)" \
| mail -r ${addr} \
-s "new mail [${newnew}]" \
-S smtp=${smtp_server} \
-S smtp-use-starttls \
-S smtp-auth=login \
-S smtp-auth-user=${smtp_user} \
-S smtp-auth-password=${smtp_password} \
${phone}
fi
# Start offlineimap if it's not running and we are not on the mailserver
# Sometimes it can crash, so it's in the loop here instead of at startup
if [ "$1" != -l ]; then
if ! pgrep offlineimap; then
offlineimap &
fi
fi
# Count the number of new mails
newnew=$(ls -1 ${inbox}/new | wc -l)
# If the number of new mails has increased
if [ ${newnew} -gt ${oldnew} ]; then
# If we are on the mailserver, just mail the alert out from here
if [ "$1" = "-l" ]; then
echo "$(summary)" \
| mail -r ${addr} \
-s "new mail [${newnew}]" \
${phone}
# Otherwise, use the smtp configuration from the config file to send it
else
echo "$(summary)" \
| mail -r ${addr} \
-s "new mail [${newnew}]" \
-S smtp=${smtp_server} \
-S smtp-use-starttls \
-S smtp-auth=login \
-S smtp-auth-user=${smtp_user} \
-S smtp-auth-password=${smtp_password} \
${phone}
fi
fi
oldnew=${newnew}
sleep 2m
done