Cover V04, I01
Article
Listing 1

jan95.tar


Listing 1: Inetmail script

#! /bin/sh
#@(#) SCCS_ID="inetmail  (LMN) TUPILAQ  940310 ver. 1.1"
#*******************************************************
# inetmail   : A simple mailserver to get mail from
#              MS-Mail out on Internet. Processes
#              mail with an extra To:- header as first
#              line in mailbody. Uses shell-functions.
#
# 10.03.1994 ; AmuGroup AB/TUPILAQ Arctica, Sweden
# Author     : Lars Magnusson (LMN)
#              TUPILAQ Arctica
#              m8827@abc.se (address for bugreports)
#
#*******************************************************
#
# Basic security
#
trap 'rm $TMPDIR/*.$$; exit' 1 2 3 5 10 13
umask 077

# Site-dependent
#
SITE=your.domain
ADMIN=postmaster
MBOX=inetmail

# System-dependent   (Here SunOS)
#
# Queueing process
QUEUE=Mail

# Mailer process
MAIL=/usr/lib/sendmail

# Sys5Rx echo, not BSD echo !!!
ECHO=/usr/5bin/echo

# init-values
#
HELP=
READY=
SEND=
INFILE=/tmp/inetin
OUTFILE=/tmp/inetout
SCRIPT=/tmp/inetscript
LOGFILE=/var/adm/inetmail.log

#**************
# Process the batch-queue.
#
get_request() {

$ECHO "s 1 $INFILE.$$ \n q \n" | \
$QUEUE -N -u $MBOX 2>&1 > /dev/null

# Test if there where any letter in queue
test ! -f $INFILE.$$
then
exit
fi
}

#***************
# Rebuild mail for gate
#

make_request() {

# Get header info from letter
#
FROM=`grep "^From " $INFILE.$$ | \
awk '{ print $0; exit }' | \
cut -d" " -f2`
TO=`grep "^To: " $INFILE.$$ | \
awk '{ getline; print $0; exit }' | \
cut -d" " -f2`
LINE=`grep -n "^To: " $INFILE.$$ | \
awk '{ getline; print $0; exit }' | \
cut -d":" -f1`
SUBJ=`grep "^Subject: " $INFILE.$$ | \
awk '{ print $0; exit }' `
CC=`grep "^Cc: " $INFILE.$$ | \
awk '{ print $0; exit }' `
BCC=`grep "^Bcc: " $INFILE.$$ | \
awk '{ print $0; exit }' `

# Make a new header for sendmail
#
$ECHO "X-Mailer: Amu MS-Mail Internet OutGate v1.1" \
> $OUTFILE.$$
$ECHO "To: $TO"          >> $OUTFILE.$$
$ECHO "From: $FROM"      >> $OUTFILE.$$

if [ "$CC" != "" ]
then
$ECHO "$CC"      >> $OUTFILE.$$

fi
if [ "$BCC" != "" ]
then
$ECHO "$BCC"     >> $OUTFILE.$$
fi
$ECHO "$SUBJ\n"          >> $OUTFILE.$$

# Restore the letter without the extra To:-address.
#
LINE="`expr $LINE + 1`"
$ECHO "$LINE,\$p" >> $SCRIPT.$$
sed -n -f $SCRIPT.$$  $INFILE.$$   >> $OUTFILE.$$
}

#***************
# Check mail for lack of receiver
#

error_check() {

# Reset the To:-address in lacking recipient

# Softswitch EMX has a funny addressing of
# external mail
#
if [ "$TO" = "\"INTERNET-inetmail" ]
then
TO=
fi

# Other mailers get it more normal
#
if [ "$TO" = "inetmail" ]
then
TO=
fi

# If no To:-line, alert the sender
#
if [ "$TO" = "" ]
then
if [ "$FROM" = "" ]
then
# Not a letter
exit
else
$QUEUE \
-s "Error from Internet-gate $SITE" \
$FROM << ETOX

Your letter where missing a valid reference
of recipient on 1:st line in letter.

Format:

To: internet-adress             f.x.

To: postmaster@$SITE

postmaster@$SITE

ETOX
fi
clean_up
exit
fi
}

#***************
# remail the letter with help of sendmail
#

resend_request() {

$MAIL -f$FROM $TO < $OUTFILE.$$

# Debug-info, can be left out if all is OK
#
DATE=`date | cut -d" " -f2,3`
SIZE=`wc -c $OUTFILE.$$ `
echo "gate $FROM -> $TO : $DATE : $SIZE" \
>> $LOGFILE
}

#***************
# Clean up the tmp-files
#

clean_up() {

rm $INFILE.$$ $OUTFILE.$$ $SCRIPT.$$
}

#
# Main function of script
#

get_request
make_request
error_check
resend_request
clean_up

exit

#
# end script
#