#!/bin/sh
# Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
# 
# Permission to use, copy, modify, and distribute this material 
# for any purpose and without fee is hereby granted, provided 
# that the above copyright notice and this permission notice 
# appear in all copies, and that the name of Bellcore not be 
# used in advertising or publicity pertaining to this 
# material without the specific, prior written permission 
# of an authorized representative of Bellcore.  BELLCORE 
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.

# Conversion from C shell to Bourne shell by Z-Code Software Corp.
# Conversion Copyright (c) 1992 Z-Code Software Corp.
# Permission to use, copy, modify, and distribute this material
# for any purpose and without fee is hereby granted, provided
# that the above copyright notice and this permission notice
# appear in all copies, and that the name of Z-Code Software not
# be used in advertising or publicity pertaining to this
# material without the specific, prior written permission
# of an authorized representative of Z-Code.  Z-CODE SOFTWARE
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.

# Brought into line with metamail 2.7 beta release Csh version
#    Dave Shield	February 1994

if test -f /usr/sbin/sendmail
then
    MAILCOMMAND=/usr/sbin/sendmail
else
    MAILCOMMAND=/bin/mail
fi

if test "$#" -lt 3
then
	echo "Usage: showexternal body-file access-type name [site [directory [mode]]]"
	exit 1
fi

if [ -z "$METAMAIL_TMPDIR" ]
then
	METAMAIL_TMPDIR=/tmp
fi

# Check argument integrity. Don't trust mail headers
if echo "$1$2$3$4$5$6$7" | grep -q '[[:space:]]'
then
	echo "Illegal white space in arguments -- possibly a mail bomb?!"
	echo "Command was:"
	echo \'$0\' \'$1\' \'$2\' \'$3\' \'$4\' \'$5\' \'$6\' \'$7\'
	exit 2
fi

bodyfile="$1"
atype=`echo $2 | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz`
name="$3"

site="$4"

dir="$5"

mode="$6"

server="$7"

if test -z "$server" -a $atype = "mail-server"
then
	server=${name}@${site}
	echo WARNING -- old style mailserver syntax, using server $server
fi

ctype=`grep -i content-type: $bodyfile | sed -e 's/............: //'`
if test -z "$ctype"
then ctype="text/plain"
fi
cenc=`grep -i content-transfer-encoding: $bodyfile | sed -e 's/.........................: //'`
username=""
pass=""
TMPDIR=$METAMAIL_TMPDIR/XXXternal.$$
trap 'cd $METAMAIL_TMPDIR ; rmdir "$TMPDIR" >/dev/null 2>&1' 1 2 3 15
mkdir $TMPDIR
PUSHED_DIR=`pwd`
cd $TMPDIR
NEWNAME="mm.ext.$$"
NEEDSCONFIRMATION=1

case $atype in
	anon-ftp)
		echo "This mail message contains a POINTER (reference) to data that is "
		echo not included in the message itself.  Rather, the data can be retrieved 
		echo automatically using anonymous FTP to a site on the network. ;;
		
	ftp)
		echo "This mail message contains a POINTER (reference) to data that is "
		echo not included in the message itself.  Rather, the data can be retrieved 
		echo automatically using the FTP protocol to a site on the network. ;;

	mail-server)
		cat > $METAMAIL_TMPDIR/ext.junk.$$ <<!
This mail message contains a POINTER (reference) to data that is not
included in the message itself.  Rather, the data can be retrieved by
sending a special mail message to a mail server on the network.
However, doing this automatically is slightly dangerous, because
someone might be using this mechanism to cause YOU to send obnoxious
mail.  For that reason, the mail message that WOULD be sent is being
shown to you first for your approval.

This is the message that will be sent if you choose to go ahead and
retrieve the external data:

Subject: Automated Mail Server Request
To: $server

!
		sed -e 1,/^\$/d < $bodyfile >> $METAMAIL_TMPDIR/ext.junk.$$
		more $METAMAIL_TMPDIR/ext.junk.$$
		rm $METAMAIL_TMPDIR/ext.junk.$$ ;;

	*)
		NEEDSCONFIRMATION=0 ;;
esac

if test $NEEDSCONFIRMATION -ne 0
then
	echo ""
	echo -n "Do you want to proceed with retrieving the external data? [y] "
	read ANS
	case "$ANS" in
		[Nn]*)	cd $METAMAIL_TMPDIR
			rm -rf $TMPDIR;
			exit 0 ;;
	esac
fi

case "$atype" in
	anon-ftp | ftp)
		case "$atype" in
		anon-ftp )
			username=anonymous
			pass=`whoami`@`hostname`
			;;
		esac

		if test -z "$site"
		then
			echo -n "Site for ftp access: "
			read site
		fi
		if test -z "$username"
		then
			echo -n "User name at site ${site}: "
			read username
		fi
		if test -z "$pass"
		then
			echo -n "Password for user $username at site ${site}: "
			stty -echo
			read pass
			stty echo
			echo ""
		fi
		if test -z "$dir"
		then
			DIRCMD=""
		else
			DIRCMD="cd $dir"
		fi
		if test -z "$mode"
		then
			MODECMD=""
		else
			MODECMD="type $mode"
		fi
		echo OBTAINING MESSAGE BODY USING FTP
		echo SITE: $site USER: $username
		${FTP:-ftp} -n <<!
open $site
user $username $pass
$DIRCMD
$MODECMD
get $name $NEWNAME
quit
!
		if test ! -r "$NEWNAME"
		then
			echo FTP failed.
			cd $METAMAIL_TMPDIR
			rm -rf $TMPDIR
			exit 1
		fi
		;;

	afs|local-file)
		if test ! -r $name
		then
			echo local file not found
			cd $METAMAIL_TMPDIR
			rm -rf $TMPDIR
			exit 1
		fi
		NEWNAME=$name
		echo GETTING BODY FROM FILE NAMED: $NEWNAME ;;

	mail-server)
		if test -z "$bodyfile"
		then
			echo mail-server access-type requires a body file
			cd $METAMAIL_TMPDIR
			rm -rf $TMPDIR
			exit 1
		fi
		echo Subject: Automated Mail Server Request > $NEWNAME
		echo To: $server >> $NEWNAME
		echo "" >> $NEWNAME
		sed -e 1,/^\$/d < $bodyfile >> $NEWNAME
		$MAILCOMMAND -t < $NEWNAME
		if test $? -ne 0
		then
			echo sendmail failed
			cd $METAMAIL_TMPDIR
			rm -rf $TMPDIR
			exit 1
		fi
		cd $METAMAIL_TMPDIR
		rm -rf $TMPDIR
		echo Your $ctype data has been requested from a mail server.
		exit 0 ;;
	*)
		echo UNRECOGNIZED ACCESS-TYPE
		cd $METAMAIL_TMPDIR
		rm -rf $TMPDIR
		exit 1 ;;
esac

if test "$cenc" = base64
then
	mimencode -u -b < $NEWNAME > OUT
	mv OUT $NEWNAME
elif test "$cenc" = quoted-printable
then
	mimencode -u -q < $NEWNAME > OUT
	mv OUT $NEWNAME
fi

cd $PUSHED_DIR
case "$atype" in
    local-file ) metamail -b -p -c $ctype $NEWNAME ;;
    * ) metamail -b -p -c "$ctype" $TMPDIR/$NEWNAME ;;
esac

if test $? -ne 0
then
	echo metamail failed
	cd $METAMAIL_TMPDIR
	rm -rf $TMPDIR
	exit 1
fi

if test ! "$NEWNAME" = "$name"
then
	echo ""
	echo The data just displayed is stored in the file $TMPDIR/$NEWNAME
	echo "Do you want to delete it?"
	rm -i $TMPDIR/$NEWNAME
fi

if test ! -r ${TMPDIR}/${NEWNAME}
then
	cd /
	cd $METAMAIL_TMPDIR
	rmdir $TMPDIR
fi
