#!/bin/sh # # simple mail-to-fax utility designed to replace HylaFAX's faxmail # uses shell, sendfax, metamail, and uudecode # # - Lee Howard - NubJub and Marek # # # Mail addresses can be three ways: # 1) user@host.org # 2) "User Name" # 3) user@host.org (User Name) # # in the latter cases, quotes may or may not be used, # there may or may not be any User Name at all, and User Name # may come before or after user@host.org. # # We need to make sure we handle all these possibilities for # both TO and FROM situations. Return-Path is different. # if [ -f /etc/hyfax.conf ] then . /etc/hyfax.conf if [ "$LOG_FILE" != "" ] then if [ ! -f $LOG_FILE ] then LOG=false fi fi fi TIMESTAMP=`/bin/date --iso-8601=seconds` RANDOMFAX=/tmp/faxtmp.$RANDOM export METAMAIL_TMPDIR=$RANDOMFAX if [ ! -d $METAMAIL_TMPDIR ]; then mkdir $METAMAIL_TMPDIR; fi rm -f $METAMAIL_TMPDIR/* # # metamail doesn't understand "multipart/alternative" # sed 's/multipart\/alternative/multipart\/mixed/' > $RANDOMFAX/_message_ TOLINE=`grep -e "^to:" -i $RANDOMFAX/_message_ | sed q` FROMLINE=`grep -e "^from:" -i $RANDOMFAX/_message_ | sed q` SUBJECT=`grep -e "^subject:" -i $RANDOMFAX/_message_ | sed q | sed -e 's/^[Ss]ubject:[ ]*//g'` if [ "$LOG" == "true" ] then echo "$TIMESTAMP | to line: $TOLINE" >>$LOG_FILE echo "$TIMESTAMP | from line: $FROMLINE" >>$LOG_FILE fi if [ "`echo $TOLINE | grep '<.*>'`" != "" ]; then TONUMBER=`echo $TOLINE| sed -e 's/.*<\(.*\)@[^\.]*.*>.*/\1/'` else TONUMBER=`echo $TOLINE| sed -e 's/^[Tt]o://g' -e 's/[ ]*\(.*@[^\.]*\).*/\1/'` fi FROM=`echo $FROMLINE | sed -e 's/^[Ff]rom:[ ]*//g' -e 's/"//g'` FROMCO=`echo $FROMLINE | sed -e 's/.*"\(.*\)".*/\1/'` COMPANY=`echo $TOLINE | sed -e 's/.*"\(.*\)".*/\1/' -e 's/.*<\(.*\)@.*/\1/'` metamail -w -x $RANDOMFAX/_message_ > /dev/null 2>&1 # We also have to use uudecode, because metamail will only # help us with mail that has Content-Type headers, although # uudecode can only handle one attachment. (Can a # multi-attachment mail not have Content-Type headers?) uudecode -o $METAMAIL_TMPDIR/_uudecoded_message_ $RANDOMFAX/_message_ > /dev/null 2>&1 rm -f $RANDOMFAX/_message_ DONE=false COMMENT= for FILE in `/bin/ls $RANDOMFAX/1-*` do if [ -f $FILE ] then if [ "$DONE" != "true" ] then if [ "$LOG" == "true" ] then echo "$TIMESTAMP | using: $FILE" >>$LOG_FILE fi /usr/bin/html2text -ascii "$FILE" >"$FILE.tmp.$$" COMMENT=`/bin/cat "$FILE.tmp.$$" |perl -e 'while (<>) { s/\r//; print; }' |perl -e 'while (<>) { s/\n/ /; print; }'` COMMENT=`echo $COMMENT` DONE=true fi if [ "$LOG" == "true" ] then echo "$TIMESTAMP | removing: $FILE" >>$LOG_FILE fi /bin/rm -f $FILE fi done if [ "$LOG" == "true" ] then echo "$TIMESTAMP | removing temp files" >>$LOG_FILE fi /bin/rm -rf $RANDOMFAX/*.tmp.$$ if [ "$LOG" == "true" ] then echo "$TIMESTAMP | from: $FROM" >>$LOG_FILE echo "$TIMESTAMP | to: $COMPANY" >>$LOG_FILE echo "$TIMESTAMP | from-company: $FROMCO" >>$LOG_FILE echo "$TIMESTAMP | subject line: $SUBJECT" >>$LOG_FILE echo "$TIMESTAMP | number: $TONUMBER" >> $LOG_FILE echo "$TIMESTAMP | comment: $COMMENT" >> $LOG_FILE fi if [ "$SUBJECT" != "" ]; then sendfax -R -s na-let -f "$FROM" -X "$FROMCO" -x "$COMPANY" -W "$OUTGOINGFAXNUMBER" -r "$SUBJECT" -c "$COMMENT" -d "$TONUMBER" $METAMAIL_TMPDIR/* # sendfax -R -f "$FROM" -r "$SUBJECT" -c "$COMMENT" -d "$TONUMBER" $METAMAIL_TMPDIR/* else sendfax -R -f "$FROM" -n -d "$TONUMBER" $METAMAIL_TMPDIR/* fi