diff options
author | Derek Stevens <nilix@nilfm.cc> | 2021-08-24 23:10:03 -0600 |
---|---|---|
committer | Derek Stevens <nilix@nilfm.cc> | 2021-08-24 23:10:03 -0600 |
commit | a73e912971f716485fa2cff05eb5fcc0252c193d (patch) | |
tree | df2d2206068177c846cff2f223ddc451b66a1258 | |
parent | 004cb7ff560cf86ad6547e0272382b6d482e17c0 (diff) |
move offlineimap spawning to inside the loop in case it crashes, include some explanatory comments
-rwxr-xr-x | mail2mms.sh | 59 |
1 files changed, 35 insertions, 24 deletions
diff --git a/mail2mms.sh b/mail2mms.sh index 5321b86..f84b4ef 100755 --- a/mail2mms.sh +++ b/mail2mms.sh @@ -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 + + # 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 [ ${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 + + # 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 |