add.sh (1572B)
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 if [ -z "${MESSAGEID}" ] 30 then 31 echo "Could not parse Message-Id of email; cannot add reminder" 32 echo "Aborting" 33 exit 1 34 fi 35 36 # from Debian package uuid-runtime 37 UUID=$(uuidgen) 38 39 FILENAME=$(echo "${MESSAGEID}_${SUBJECT}_${TDATE}_${REASON}_${UUID}" | tr -dc 'a-zA-Z@._0-9-' | rev | cut -b1-255 | rev) 40 41 mkdir -p scheduled 42 43 # adapted from https://stackoverflow.com/a/169969 44 ( 45 # Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds 46 flock -x -w 10 200 || exit 1 47 48 mv "$TEMPFILE" "scheduled/$FILENAME" 49 50 echo "INSERT INTO reminders (sendtime, createtime, messageid, filename, explanation, status) VALUES('$TDATE', '$CDATE', '$MESSAGEID', '$FILENAME', '$REASON', 'scheduled');" | sqlite3 remailback.sqlite 51 52 # from https://unix.stackexchange.com/a/116451/ 53 TDATEDATE=$(date -d "@$TDATE" +'%H:%M %D') 54 echo "$DIR/poll.sh" | at "$TDATEDATE" 55 56 ) 200>lock.flock 57 58 echo "Scheduled reminders:" 59 ./show.sh < "scheduled/$FILENAME" 60