--- /dev/null
+Small shell script to regularly check a target person's responsiveness via mail.
+
+Expects mutt to be usable for sending mails via command line.
+
+When run as "pingmail check", searches a maildir for the most recent file
+matching a regex pattern (such as a "From: " field matching the target person),
+and if the most recent file is too old, sends an e-mail to the target person
+requesting some sign of life. After some more wait time without a sign of life,
+sends a warning message to another mail address that target person is
+unresponsive.
+
+The wait time counter can be reset manually with "pingmail touch".
+
+The wait time, the target and checking persons' mail addresses, the regex, the
+maildir path etc. must be set in a dotfile ~/.pingmailrc. See comments in
+pingmailrc.example file for more details.
+
+To install as an hourly cronjob, define $path_to_pingmail_script_file, then do:
+
+line='0 * * * * '$path_to_pingmail_script_file' check'
+(crontab -l; echo $line) | crontab -
+++ /dev/null
-# pingmail
-regularly check target person's responsiveness by mail
--- /dev/null
+#!/bin/sh
+
+set -e
+
+# read in dotfile
+pingmailrc=$HOME'/.pingmailrc'
+if [ ! -f $pingmailrc ]; then
+ echo 'No .pingmailrc found at '$pingmailrc', aborting.'
+ exit
+fi
+. $HOME'/.pingmailrc'
+mkdir -p $testdir
+
+# interpret arguments
+if [ "$1" = "check" ]; then
+ continue
+elif [ "$1" = "touch" ]; then
+ touch $ping_touch
+ exit
+else
+ echo 'usage: '$0' COMMAND\n(COMMAND one of "check", "touch")'
+ exit
+fi
+
+# check test file for last modtime
+modtime_pingfile=0
+if [ -f $ping_touch ]; then
+ modtime_pingfile=`stat $ping_touch --format=%Y`
+fi
+
+# search maildir for last life sign datetime
+modtime_mails=0
+while read file; do
+ if [ -z $file ]; then
+ break
+ fi
+ modtime=`stat $file --format=%Y`
+ if [ "$modtime" -gt "$modtime_mails" ]; then
+ modtime_mails=$modtime
+ fi
+done <<EOF
+$(grep -lER "$matchstring" $maildir)
+EOF
+
+# find delta of last life sign datetime / test file modtime and current datetime
+now=`date +%s`
+modtime=0
+if [ "$modtime_mails" -gt "$modtime_pingfile" ]; then
+ echo "The most recent life sign is in the mails."
+ modtime=$modtime_mails
+else
+ echo "Ping wait file was updated more recently than last life sign from the mails."
+ modtime=$modtime_pingfile
+fi
+delta=`expr $now - $modtime`
+echo "Wait time since last life sign or ping sent: "$delta" seconds:"
+
+# if delta > wait time, send reminder to person to check with, start reminder
+# mail timer if not in existence, re-touch ping wait file
+if [ "$delta" -gt "$wait_time" ]; then
+ echo $msg2checked | mutt -s "$subj2checked" $checked_address
+ echo "Sending ping message."
+ touch $ping_touch
+ if [ ! -f "$reminder_touch" ]; then
+ touch $reminder_touch
+ echo "Creating reminder message wait time file."
+ fi
+fi
+
+# if last life sign datetime / test file modtime > modtime of reminder touch
+# file, delete it; otherwise, if delta between those > wait time, send reminder
+# to person checking and then delete reminder touch file
+if [ -f "$reminder_touch" ]; then
+ modtime_reminder=`stat $reminder_touch --format=%Y`
+ if [ "$modtime" -gt "$modtime_reminder" ]; then
+ rm $reminder_touch
+ echo "Deleting reminder message wait time file."
+ else
+ delta=`expr $now - $modtime_reminder`
+ if [ "$delta" -gt "$wait_time" ]; then
+ echo $msg2checker | mutt -s "$subj2checker" $checker_address
+ echo "Sending reminder message."
+ rm $reminder_touch
+ echo "Deleting reminder message wait time file."
+ fi
+ fi
+fi
--- /dev/null
+# place for test files whose modification times are used to track lifesigns
+testdir=$HOME'/.pingmail'
+
+# modification time is the last time a ping was sent or a lifetime received
+ping_touch=$testdir'/ping_touch'
+
+# modification time is when the count for sending checker a warning mail starts
+reminder_touch=$testdir'/reminder_touch'
+
+# to recursively search for most recent matches to $matchstring as lifesigns
+maildir=$HOME'/mail'
+
+# how long to wait for lifesigns before sending a ping; double is time to wait
+# for a lifesign before sending a warning message to checker
+wait_time=86400
+
+# address of the checker, receives warning message after too long wait
+checker_address='bar@example.org'
+
+# address of the checked person, ping is sent here
+checked_address='foo@example.org'
+
+# content of ping message sent to checked person
+subj2checked='[pingmail] Ping!'
+msg2checked='Hi!\n
+\nThis is an automated mail ping from '$checker_address'.
+\nRespond to show that you are still alive!'
+
+# content of warning message sent to checker
+id_target='foo'
+subj2checker='[pingmail] No recent life signs from '$id_target
+reminder_time=`expr $wait_time \* 2`
+msg2checker='pingmail reporting in:\n
+\nNo life signs from '$id_target' for the last '$reminder_time' seconds.
+\nMaybe you should give them a call to check if they are okay.'
+
+# pattern to search $maildir for recursively for lifesigns
+checked_address_escaped=`echo $checked_address | sed 's/\./\\./g'`
+matchstring='^From: .*('$checked_address_escaped'|alternate@example\.org)'