#!/bin/sh
#
# mail->fax gateway
#
# send fax to a GIVEN phone number (on the command line). Everybody may send.
#
# $Id: mail2fax.space,v 1.5 2002/04/23 14:08:42 gert Exp $
#
# $Log: mail2fax.space,v $
# Revision 1.5  2002/04/23 14:08:42  gert
# use .txs suffix for "short"
#
# Revision 1.4  2002/01/11 14:12:40  gert
# add -s (short)
#
# Revision 1.3  2001/10/17 12:18:26  gert
# add "-f <mail to>" switch
# change error detection logic (error code, not "anything on stderr?")
#
# Revision 1.2  2001/10/17 11:02:44  gert
# -A <acct>
# DATE: ausgeben
#
# Revision 1.1  2001/10/17 10:53:30  gert
# mail2fax gateway, currently used at SpaceNet
#

# options
CATPGM="cat"
acct=""
short=""

# Make sure mgetty components can be found.
PATH=$PATH:/usr/local/bin

#
# this user will get all the status mails...
#
admin="gert@space.net"

while expr "X$1" : "X-" >/dev/null; do
    case X$1 in
	X-p) CATPGM="head -"`echo $2 | awk '{ print ($1 * 66) - 12}'`; 
	     shift; shift ;;
	X-A) acct="-A $2"
	     shift; shift ;;
	X-f) admin="$2"
	     shift; shift ;;
	X-s) short="yes"
	     shift ;;
	*) echo "unknown option: $1" >&2 ; exit 99 ;;
    esac
done


# get phone number
if [ $# -ne 1 ]
then
    echo "faxmail [-p pages] <faxnumber>" >&2
    exit 1
fi
faxphone="$1"

#
# copy mail (on stdin) to separate files for header and body,
# extracting "From:" and "Reply-To:" headers on the way
#
head=/tmp/mf$$.head
body=/tmp/mf$$.body
fax=/tmp/mf$$.txt
tmp=/tmp/mf$$.tmp
LOG=/tmp/mf.log

# mega hack value :-) - if "short" is requested, change file name so
# that it matches a "faxspool.rules" entry, cutting of whitespace at EOP
test "$short" = "yes" && fax=/tmp/mf$$.txs 

#
eval `awk ' function getaddr(line) {
		b = index( line, "<" );
		if ( b != 0 )
		{
		    f=substr( line, b+1 );
		    return substr( f, 1, index( f, ">" ) -1 );
		}
		split( line, broken );
		return broken[2];
	    }
	    BEGIN { header=1; from=""; replyto=""; subject=""; fullfrom=""; date="" }
	    NF == 0 { if ( header ) { header = 0; next; } }
	            { if ( header ) print >> "'$head'";
                               else print >> "'$body'" }
	    /^From:[ \t]/ \
                    { if ( header ) from=getaddr($0); }
	    /^Reply-To:[ \t]/ \
                    { if ( header ) replyto=getaddr($0); }
	    /^Subject:[ \t]/ \
                    { if ( header ) subject=substr($0, 10); }
	    /^From:[ \t]/ \
                    { if ( header ) fullfrom=substr($0, 7); }
	    /^Date:[ \t]/ \
                    { if ( header ) date=substr($0, 7); }
	    END { gsub( "\"", "", subject); gsub( "\"", "", from); 
	          gsub( "\"", "", replyto); gsub( "\"", "", fullfrom); 
		  printf "subject=\"%s\"; from=\"%s\"; date=\"%s\"; replyto=\"%s\"; fullfrom=\"%s\"\n", \
			 subject, from, date, replyto, fullfrom }
           ' - `

# determine reply address
mailto=${replyto:-$from}

echo "`date`: $from $replyto -> $mailto" >>$LOG

# which is my host???
whoami=`hostname 2>/dev/null` || whoami=`uname -n`

# NO authentication
# (could be done *here* if requested)

#
# ok. Authentication granted. Queue fax.
#
# Status mails are sent to administrator, not to sender of mail
#
echo "faxspool $acct -f $admin $faxphone $fax" >>$LOG

cat <<EOF >$fax

FAX FROM: $fullfrom
      TO: $faxphone
 SUBJECT: $subject
    DATE: $date

EOF

[ "$short" != "yes" ] &&
  cat <<EOF >>$fax
This fax was converted automatically from an electronic mail, so 
please don't wonder too much about the unusual form. Thanks.

If you have any questions about it, contact SpaceNET, Munich.
Our telephone number: ++49-89-32356-0, fax: ++49-89-32356-299.

     --------- original message text follows ---------

EOF
$CATPGM $body >>$fax

faxspool $acct -C - -f "$admin" \
	-h /usr/local/etc/mgetty+sendfax/faxhead.mail \
	$faxphone $fax >$tmp 2>&1

if [ $? != 0 ] ; then
    echo "FAX SPOOLING FAILED" ; cat $tmp
    exit 9
fi

#
# fax denied
#
#    ( cat <<EOF
#To: $mailto
#CC: postmaster
#From: root@$whoami (The Automatic Fax Robot)
#Subject: Your fax to $faxphone
#
#Dear user,
#
#you are not authorized to use greenie's mail-to-fax gateway service.
#
#If you want to use it, please contact the system administrator,
#root@$whoami.
#
#Thanks,
#	your fax robot
#
#------ your mail follows -------
#EOF
#    cat $head
#    echo ""
#    head -50 $body
#    ) | /usr/lib/sendmail -t
#fi

# clean up
rm -f $head $body $fax $tmp

echo "`date`: fax queued successfully" >>$LOG

exit 0
