remailback

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

add.sh (1445B)


      1 #!/bin/bash
      2 
      3 # Add a reminder for a message
      4 # Usage: cat message | ./add.sh 2 weeks, check that order arrived
      5 
      6 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
      7 cd "$DIR"
      8 
      9 TARGET=$(echo "$@" | cut -d, -f1)
     10 REASON=$(echo "$@" | cut -d, -f2- | sed 's/^\s*//;s/\s*$//')
     11 
     12 CDATE=$(date +%s)
     13 TDATE=$(date +%s -d "$TARGET")
     14 
     15 if [ $? -ne 0 ]
     16 then
     17   echo "Could not parse date $TARGET"
     18   exit 1
     19 fi
     20 
     21 TEMPFILE=$(mktemp)
     22 cat > $TEMPFILE
     23 
     24 # get_email_header command from https://a3nm.net/git/mybin
     25 
     26 SUBJECT=$(~/bin/get_email_header subject < "$TEMPFILE")
     27 MESSAGEID=$(~/bin/get_email_header message-id < "$TEMPFILE")
     28 
     29 # from Debian package uuid-runtime
     30 UUID=$(uuidgen)
     31 
     32 FILENAME=$(echo "${MESSAGEID}_${SUBJECT}_${TDATE}_${REASON}_${UUID}"  | tr -dc 'a-zA-Z@._0-9-' | rev | cut -b1-255 | rev)
     33 
     34 mkdir -p scheduled
     35 
     36 # adapted from https://stackoverflow.com/a/169969
     37 (
     38   # Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds
     39   flock -x -w 10 200 || exit 1
     40 
     41   mv "$TEMPFILE" "scheduled/$FILENAME"
     42 
     43   echo "INSERT INTO reminders (sendtime, createtime, messageid, filename, explanation, status) VALUES('$TDATE', '$CDATE', '$MESSAGEID', '$FILENAME', '$REASON', 'scheduled');" | sqlite3 remailback.sqlite
     44 
     45   # from https://unix.stackexchange.com/a/116451/
     46   TDATEDATE=$(date -d "@$TDATE" +'%H:%M %D')
     47   echo "$DIR/poll.sh" | at "$TDATEDATE"
     48 
     49 ) 200>lock.flock
     50 
     51 echo "Scheduled reminders:"
     52 ./show.sh < "scheduled/$FILENAME"
     53