# - .procmailrc file for use with Deerfield's RelayFax as a Windows client
# and HylaFAX as the fax server
# - faxes should be directed to the user where this .procmailrc file is placed
# - faxes may also be sent manually by attaching a tif or ps file to an e-mail
# with the fax number being the subject line (if there's no attachment, then
# it will fax the message text without the FAXPASSWORD line

# We strip double quotes (") from strings because they foul up sh - or
# escaped they appear escaped on the faxcover, and we strip ampersands (@) 
# from FAXDESTINATION because they foul up the 'sendfax -d' option syntax.
# Sadly, RelayFax client isn't moron-proof, i.e. the user can submit
# destination information including ``", "'' in the string, which is quite 
# impossible to handle perfectly.

# uncomment these two if you want [verbose] logging
# VERBOSE=yes
# LOGFILE=procmail.log

RETURNPATH=`grep -e "return-path" -i | sed "s/^.*<//;s/>//"`
MAILSUBJECT=`grep -e "^subject" -i | sed -e 's/[Ss]ubject. //g' -e 's/["@]//g'`
PRINTEDFAX=`grep -e PRINTEDFAX | sed -e 's/PRINTEDFAX: //g'`
FAXDESTINATION=`egrep -e '^(FAX|CC)NUMBER' | sed -e 's/@//g' -e 's/^.*NUMBER: \([^,]*[^ ]*[^"]*\), "\([^"]*[^,]*[^ ]*[^"]*\)", "[^$]*"/\2\@\1 ¿/' -e 's/"//g'`
FAXTOCOMPANY=`egrep -e '^(FAX|CC)NUMBER' | sed -e 's/.*", "\([^$]*\)"/\1 ¿/' -e 's/"//g'`
FAXPASSWORD=`grep -e FAXPASSWORD | sed -e 's/FAXPASSWORD: //g'`
FROMNAME=`grep -e FROMNAME | sed -e 's/FROMNAME: //g'`
FROMMAIL=`grep -e FROMLINE | sed -e 's/FROMLINE: //g'`
FROMLINE=$FROMMAIL\ \($FROMNAME\)
NOCOVERPAGE=`grep -e NOCOVERPAGE | sed -e 's/NOCOVERPAGE: //g'`
TMPHOLD=`grep -e CONFIRMSEND | sed -e 's/CONFIRMSEND: //g'`
CONFIRMSEND=`if [ "$TMPHOLD" = "TRUE" ]; then echo "-R"; else echo; fi`
FAXSUBJECT=`grep -e FAXSUBJECT | sed -e 's/FAXSUBJECT: //g' -e 's/"//g'`
COMMENTS=`grep -e COMMENTS | sed -e 's/COMMENTS: //g' -e 's/%e/\\\n/g' -e 's/"//g'`
ATTACHMENTNAME=`grep -e 'Content-Type.*name=' | sed -e 's/^.*name=//g' | sed -e 's/\"//g'`
METAMAIL_TMPDIR="$HOME/faxtmp"
PASSWDCHECK=`grep -e "$RETURNPATH $FAXPASSWORD" $HOME/faxuserlist`
FAXHOST=`hostname`
TMPHOLD=`grep ^PRIORITY | sed 's/^.* //g'`
PRIORITY=`echo \$[$TMPHOLD*255/100]`
PRIORITY=`if [ -n "$PRIORITY" ]; then echo "-P $PRIORITY"; fi`
COVERPAGE=`if [ -f "$RETURNPATH" ]; then echo "-C $RETURNPATH"; fi`
SCHEDMO=`grep ^SCHEDULE: | sed 's/^.*\(..\).................$/\1/g' | sed \
	-e 's/01/Jan/g' -e 's/02/Feb/g' -e 's/03/Mar/g' -e 's/04/Apr/g' \
	-e 's/05/May/g' -e 's/06/Jun/g' -e 's/07/Jul/g' -e 's/08/Aug/g' \
	-e 's/09/Aug/g' -e 's/10/Oct/g' -e 's/11/Nov/g' -e 's/12/Dec/g'`
SCHEDULE=`grep ^SCHEDULE: | sed "s/^.*...\(.......\).\(........\)$/\2 $SCHEDMO \1/g"`
SCHEDULE=`if [ -n "$SCHEDULE" ]; then echo $SCHEDULE; \ 
	else echo "now + 1 minute"; fi`

:0
* ! $PASSWDCHECK ?? ^^$RETURNPATH $FAXPASSWORD^^
* ! ^FROM_DAEMON
* ! ^X-Loop
{
 :0
 | (formail -r -A"X-Loop: $USER@$FAXHOST" ; \
	echo "FAX ERROR: You do not have permission to fax.") | $SENDMAIL -t

 :0:faxtmp.lock
 /dev/null
}

# Uncomment this recipe if you wish to have copies CC'ed back to you...
# :0 c
# ! $RETURNPATH

:0 ic:faxtmp.lock
| rm -f $METAMAIL_TMPDIR/*

:0 ic:faxtmp.lock
| metamail -w -x

:0 ic:faxtmp.lock
| rm -f $METAMAIL_TMPDIR/mm.*

:0
* PRINTEDFAX ?? ^^TRUE^^
{ INCLUDERC=.faxitrc }

:0 i:faxtmp.lock
* ! $ATTACHMENTNAME ?? ^^^^
| sendfax -f "$RETURNPATH" -n -d "$MAILSUBJECT" $METAMAIL_TMPDIR/*

:0 i:faxtmp.lock
| grep -v FAXPASSWORD | sendfax -f "$RETURNPATH" -n -d "$MAILSUBJECT"
