:
# 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.

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

if test -z "$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

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

site=$4

dir=$5

mode=$6

ctype=`grep -i content-type: $bodyfile | sed -e 's/............: //'`
cenc=`grep -i content-transfer-encoding: $bodyfile | sed -e 's/.........................: //'`
username=""
pass=""
TMPDIR=$METAMAIL_TMPDIR/XXXternal.$$
trap 'rmdir "$TMPDIR" >/dev/null 2>&1' 1 2 3 15
mkdir $TMPDIR
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:

To: ${name}@${site}:
Subject: Automated Mail Server Request

!
		cat $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]*) 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.
			rm -rf $TMPDIR
			exit 1
		fi
		;;

	afs|local-file)
		if test ! -r $name
		then
			echo File not found
			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
			rm -rf $TMPDIR
			exit 1
		fi
		echo Subject: Automated Mail Server Request > $NEWNAME
		echo To: ${name}@${site} >> $NEWNAME
		echo "" >> $NEWNAME
		cat $bodyfile >> $NEWNAME
		$MAILCOMMAND -t < $NEWNAME
		if test $? -ne 0
		then
			echo sendmail failed
			rm -rf $TMPDIR
			exit 1
		fi
		rm -rf $TMPDIR
		echo Your $ctype data has been requested from a mail server.
		exit 0 ;;
	*)
		echo UNRECOGNIZED ACCESS-TYPE
		rm -rf $TMPDIR
		exit 1 ;;
esac

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

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

if test $? -ne 0
then
	echo metamail failed
	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 $NEWNAME
fi

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