#!/bin/bash
#
# remind-me-later: file a message away until a certain date in the future,
# a la FutureMail, GTD tickler files http://en.wikipedia.org/wiki/Tickler_file
#
# Gerald Oskoboiny, 10 Jan 2009
#
# usage (typically from within a mail client like mutt or pine):
#
#  |rml			[ defaults to 5am tomorrow ]
#  |rml tomorrow	[ this time tomorrow ]
#  |rml 5pm
#  |rml jan 20
#
# (acceptable date formats are those accepted by touch(1))
#
# or:
#   rml process-queue	[ to process the queue, typically from cron ]
#
# source: http://impressive.net/people/gerald/2009/01/remind-me-later
#   info: http://impressive.net/archives/fogo/20090111042224.GA30463@impressive.net
#
# $Id: remind-me-later,v 1.3 2009/07/27 22:22:10 gerald Exp $
#

inbox=$HOME/mail/inbox
queue=$HOME/mail/later; [ -d $queue ] || mkdir $queue || exit

if [ "$1" == "process-queue" ]; then
    lockfile $inbox.lock
    touch $queue/.now
    find $queue -type f -not -newer $queue/.now | while read f ; do
	cat $f | sed '1,/^$/s/^Status: [RO]*/Status: /' >> $inbox \
	    && rm $f
    done
    rm -f $inbox.lock
    exit
fi

temp=$queue/remind-me-later.$$; export temp
cat - > $temp

when='5am tomorrow'
if [ ! -z "$1" ]; then
    when="$@"
fi

date=$(cat $temp | formail -cxDate:    | sed -r 's/[^a-z0-9:-]/ /gi; s/\s+/-/g')
from=$(cat $temp | formail -cXFrom:    | \
	  formail -r -xTo: | tr -d " " | sed -r 's/[^a-z0-9:-]/ /gi; s/\s+/-/g')
subj=$(cat $temp | formail -cxSubject: | sed -r 's/[^a-z0-9:-]/ /gi; s/\s+/-/g')

target=$(printf "%.20s%.40s%s" "$from" "$subj" "$date")
while [ -f $target ]; do
    target=$target.$RANDOM
done

mv $temp $queue/$target
touch -d "$when" $queue/$target

#
# changelog:
# $Log: remind-me-later,v $
# Revision 1.3  2009/07/27 22:22:10  gerald
# xc skiing
#
# Revision 1.2  2009-01-11 04:24:15  gerald
# added an info link
#
# Revision 1.1  2009-01-11 01:44:52  gerald
# shell script to file a message away until a certain date in the future
# a la FutureMail, GTD tickler files http://en.wikipedia.org/wiki/Tickler_file
#
#

