remailback

send email reminders after a time period
git clone https://a3nm.net/git/remailback/
Log | Files | Refs

poll.sh (1927B)


      1 #!/bin/bash
      2 
      3 # Poll all scheduled reminders and send the ones that are due.
      4 # This script is scheduled with "at" when a reminder is added.
      5 # To be safe, it can also be scheduled to run regularly with cron.
      6 # Usage: ./poll.sh
      7 
      8 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
      9 cd "$DIR"
     10 
     11 if [ ! -f email ]
     12 then
     13   echo "Missing from email! Aborting!"
     14   exit 1
     15 fi
     16 
     17 EMAIL=$(cat email)
     18 
     19 CDATE=$(date +%s)
     20 # adjust to time offsets -- potentially send reminders 5min early
     21 # this is in case the clock drifts
     22 CDATEA=$(($CDATE + 300))
     23 
     24 # adapted from https://stackoverflow.com/a/169969
     25 (
     26   # Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds
     27   flock -x -w 10 200 || exit 1
     28 
     29   echo "SELECT * FROM reminders WHERE sendtime < '$CDATEA' AND status='scheduled'" |
     30     sqlite3 remailback.sqlite | while read l; do
     31 
     32     ID=$(echo "$l" | cut -d\| -f1)
     33     TTIME=$(echo "$l" | cut -d\| -f2)
     34     FILENAME=$(echo "$l" | cut -d\| -f5)
     35     EXPL=$(echo "$l" | cut -d\| -f6)
     36     FILE="scheduled/$FILENAME"
     37 
     38     # get_email_header and prepare_email_forward commands from https://a3nm.net/git/mybin
     39     SUBJECT=$(~/bin/get_email_header subject < $FILE)
     40     MESSAGEID=$(~/bin/get_email_header message-id < $FILE)
     41 
     42     mkdir -p sent sent_reminders scheduled_reminders
     43 
     44     # note: we may overwrite a file here
     45     # in case it's not the first time we try to send the reminder
     46     ~/bin/prepare_email_forward "$EMAIL" "$EMAIL" "PING: $SUBJECT: $EXPL" "$MESSAGEID" "$FILE" "$EXPL" \
     47       > "scheduled_reminders/$FILENAME"
     48     /usr/sbin/sendmail -t < "scheduled_reminders/$FILENAME"
     49     if [ $? -ne 0 ]
     50     then
     51       echo "sendmail failed for $FILENAME"
     52       exit 1
     53     fi
     54 
     55     mv "scheduled_reminders/$FILENAME" "sent_reminders/$FILENAME"
     56     mv "scheduled/$FILENAME" "sent/$FILENAME"
     57 
     58     echo "UPDATE reminders SET status='sent' WHERE id='$ID'" |
     59       sqlite3 remailback.sqlite
     60   done
     61 
     62 ) 200>lock.flock
     63