#!/bin/sh
#	(c) Copyright 1993 Digi, Intl.  All Rights Reserved.
#
#	File:	/usr/bin/mailfaxer
#
#	Usage:	cat email-msg | mailfaxer [options] TELNO
#
#	Options:
#		-t who-to	Override "To:" name
#		-f who-from	Override "From:" name
#
#	This script converts an EMAIL message into a DigiFAX fax job.
#	You must have DigiFAX v2.6.0 or later installed on your computer.
#
#	The fax is sent to the telephone number specified by TELNO.
#
#	A cover sheet is generated for the fax job.  The cover sheet
#	will have all of the e-mail message headers printed in the Notes:
#	section of the cover sheet.  The name of the sender and the
#	receiver is taken from the From: and To: headers in the email
#	message.  The To: information can be overriden using the -t
#	option.
#
#	The -f who-from option can be used to prevent people from outside
#	the local domain from accessing the fax service.
#
#	This script is meant to be called from your mail delivery agent,
#	such as "sendmail".  At our site, we have configured our sendmail.cf
#	file so that addresses like Rick.Richardson%555-1212@fax.digibd.com
#	cause this script to be run with these arguments:
#
#		mailfaxer -t Rick.Richardson 555-1212 < email-msg
#
#	Configuring and installing sendmail on your system is not something
#	we can describe here, since each version of Unix seems to do it
#	slightly differently, and sendmail.cf files are by definition highly
#	specific to each installation of sendmail.  A good system
#	administrators guide should help you to set up sendmail.
#
#	That said, here are the sendmail.cf lines which we used in our own
#	sendmail.cf file.  First, here's the lines that appears in the
#	machine dependent part of ruleset zero:
#
#		#	 Rules to send mail out using DigiFAX
#		R$+%$+<@fax.digibd.com>$*	$#faxmail$@$2$:$1
#		#	 End Rules to send mail out using DigiFAX
#
#	Note that the string "digibd.com" should be replaced by your
#	own domain name.
#	And, here is the mailer definition that we added at the end.
#	
#		##############################################
#		#  FAX mailer for digifax
#		##############################################
#		Mfaxmail, P=/usr/bin/mailfaxer, F=DFMshux, S=16, R=26,
#				M=100000000, A=mailfaxer -t $u $h
#		S16
#		R$*<@$+>$*		$@$1<@$2>$3
#		S26
#		#### End of FAX mailer stuff #####
#

##############################################################################
#	Configuration options for this script
##############################################################################

#
#	Our domain name.  If a -f option is given, reject attempts to fax
#	mail unless the -f option ends with this domain name.
#
DOMAIN=digibd.com

#	List of FAX numbers (separated by vertical bars | ) which shouldn't
#	be faxed to.  Instead, we simply print the fax to our printer.
#	Note: this has to be an exact match with what the user used for a
#	TELNO, so forms with and without punctuation should be listed.
#	Set this to "none" if all email should be faxed (and don't bother
#	setting values for LPDEST, FAX2PR, and PR below).
LOCALFAX="943-0803|9430803"

#	For FAXes which are local, the name of the printer to print them on.
LPDEST=laser

#	The program and options used to convert the output of faxcover into
#	a printer stream for the above printer.  If your faxcover script
#	will be outputting straight text, just use "cat".  If it produces
#	a fax file as output (has a merged logo), then used fax2pr or fax2ps
#	as appropriate.
FAX2PR="cat"			# Text cover, to any non-PS printer
FAX2PR="fax2pr -Tlj-100"	# Graphics cover, to a LaserJet II, at 100 DPI

#
#	The program to use to pretty print the rest of the email message
#
PR=pr		# Regular "pr" program
PR=jetpr	# jetpr LaserJet pretty printer (part of JetRoff package)

##############################################################################
#	End of (easy) configuration section.  No need to change anything
#	below here unless you are a hacker. 
##############################################################################

usage() {
	echo "Usage: mailfaxer [-t who-to] [-f who-from] telno"
}

TO=
FROM="$DOMAIN"
while getopts '?t:f:' i
do
	case "$i" in
	t)	TO="$OPTARG";;
	f)	FROM="$OPTARG";;
	\?)	usage; exit 0;;
	esac
done
shift `expr $OPTIND - 1`

if [ "$#" = 0 ]; then
	usage; exit 1;
fi

TELNO="$*"

#
#	Verify that sender is allowed to fax email
#
case "$FROM" in
*$DOMAIN)
	;;
*)
	echo "You have attempted to FAX E-MAIL from an illegal domain."
	exit 1
	;;
esac

#
#	Stash the headers and message body in temp files
#
TMP="/tmp/fax$$"
csplit -s -f "$TMP." - '/^$/'

#
#	Get various header fields
#
exec 3<&0 0<$TMP.00
while read header value
do
	case "$header" in
	"To:")		To="$value";;
	"From:")	From="$value"
			# A_ is login id, N_ is full name
			case "$From" in
			*"("*")"*)
				A_From=`echo "$From" | sed 's/(.*)//'`
				N_From=`echo "$From" | sed 's/.*(\(.*\))/\1/'`
				;;
			*"<"*">"*)
				N_From=`echo "$From" | sed 's/<.*>//'`
				A_From=`echo "$From" | sed 's/.*<\(.*\)>/\1/'`
				;;
			*)
				N_From="$From"
				A_From="$From"
				;;
			esac
			;;
	"Subject:")	Subject="$value";;
	"Date:")	Date="$value";;
	"From")		A_From=`echo "$value" | sed 's/ .*//'`
			N_From="$A_From"
			;;
	"")		break;;
	esac
done
exec 0<&3 3<&-

#
#	Use faxsend to queue job.  Note: -u "" forces cover sheet generation,
#	even if sender/receiver aren't known (need DigiFAX v2.6.0).  Put the
#	message headers in the notes portion of the cover sheet, and put the
#	body of the message on subsequent pages.  
#
FAX_n="$N_From"
if [ "Q$TO" = Q ]; then
	FAX_N="$To"
else
	FAX_N="$TO"
fi
SENDER=$A_From
export FAX_n FAX_N SENDER

#
#	Special case faxes to our own fax number - just send them to
#	the printer dedicated to printing faxes.
#
case "$TELNO" in
$LOCALFAX)
	note=$TMP.00; export note;
	(faxcover -l | $FAX2PR) | lp -d$LPDEST
	$PR -h "FAX to $FAX_N" $TMP.01 | lp -d$LPDEST
	;;
*)
	faxsend -u "" -l -q -n $TMP.00 $TELNO $TMP.01
	;;
esac
rm $TMP.*
exit 0

#
#	Here's what a mail message looks like, for reference
#
From rick Thu Jan  7 11:01:10 1993
Received: by digibd.com (5.65/DBI-1.15)
	id AA08976; Thu, 7 Jan 93 11:01:10 -0600
From: rick (Rick Richardson)
Message-Id: <9301071701.AA08976@digibd.com>
Subject: Testing
To: veep
Date: Thu, 7 Jan 93 11:01:09 CST
X-Mailer: ELM [version 2.3 PL9]

message body
