#! /bin/sh -

## $Id: mhpgp,v 1.1.0.7 2005/11/29 06:25:05 rickert Exp $

backup="#"

moreproc=more
moreproc=`mhparam moreproc`
if [ X$moreproc = X ] ; then moreproc=more ; fi

### MHPGP
# -b	strip trailing blanks on pgpmime content before verification
# -w	save the decrypted message to the current folder

USAGE="mhpgp [-bw] message"

outfile=""
deblank="s/

*$//"

while getopts bw op
do
  case "$op" in
   b)	deblank='s/[ 
	][ 
	]*$//' ;;
   w)	outmsg=`mhpath new`
	outfile=`echo "$outmsg" | sed 's=/\([^/]*\)$=/'"$backup"'\1'".$$"'='`
	;;
   *)	echo "$USAGE" >&2
	exit 1
	;;
  esac
done
shift `expr $OPTIND - 1 || :`	## sigh! expr gives status 1 if answer=0

TEMP=/tmp/mhpgp.$$

umask 077
mkdir $TEMP || exit 1
trap "rm -rf $TEMP" 0 1 2 15

case "$#" in
 0)	FILE=`mhpath cur` || exit 1 ;;
 *)	FILE=`mhpath "$@"` || exit 1 ;;
esac

set X $FILE

if [ $# != 2 ] ; then
	echo "One message at a time, please!" >&2
	exit 1
fi

# get mime-version and content-type headers.

CH=`sed -n \
    -e	':a
	/^-*$/q
	/^[Mm][Ii][Mm][Ee]-[Vv][Ee][Rr][Ss][Ii][Oo][Nn]:/b x
	/^[Cc][Oo][Nn][Tt][Ee][Nn][Tt]-[Tt][Yy][Pp][Ee]:/b x
	d
	:x
	p
	n
	/^[ 	]/b x
	b a' $FILE`

if echo "$CH" | grep -i mime-version >/dev/null 2>&1
then
	:	## nothing, this is good
else
	CH=
fi

mt=
case "$CH" in
 *application/pgp-signature*) mt=ms ;;	## pgp mime signature
 *application/pgp-encrypted*) mt=me ;;  ## pgp mime encrypted
 *) P=`grep '^-----BEGIN PGP' $FILE 2>/dev/null`
    case "$P" in
     *"PGP SIGNED MESSAGE"*) mt=ps ;;	## plain signed message
     *"BEGIN PGP MESSAGE"*)  mt=pe ;;	## plain encrypted message
    esac
    ;;
esac

case "$mt" in
 "")	echo "I can't find a PGP message there" >&2
	exit 1 ;;
 ms)	;;	## postpone this one
 ps)	gpg2 --verify $FILE
	exit ;;
 me|pe)	### we can cheat, and combine these
	sed -n -e ':a
		/^-----BEGIN PGP MESSAGE/b x
		d
		:x
		p
		/^-----END PGP MESSAGE/b y
		n
		b x
		:y
		n
		b y' $FILE | gpg2 --output $TEMP/msg --decrypt
	X=`tail -1c $TEMP/msg`
	if [ "$X" != "" ] ; then echo >> $TEMP/msg ; fi # make sure trailing \n
	if [ "$outfile" = "" ] ; then
		$moreproc $TEMP/msg
	else
		if [ "$mt" = "pe" ] ; then
			sedcmd="/^[Mm][Ii][Mm][Ee]-.*:/b r"
		else
			sedcmd='/^-*$/q'
		fi

		rm -f "$outfile" 2>/dev/null

		sed -n ':a
			/^-*$/q
			'"$sedcmd"'
			/^[Cc][Oo][Nn][Tt][Ee][Nn][Tt]-/b r
			p
			n
			b a
			:r
			n
			/^[ 	]/b r
			b a' "$FILE" > "$outfile"

		if [ "$mt" = "pe" ] ; then echo "" >> "$outfile" ; fi
		sed -e 's/
$//' $TEMP/msg >> "$outfile" || exit 1
		if ln -n "$outfile" "$outmsg" ; then
			rm -f $outfile
		else
			echo "Message left in $outfile" >&2
			exit 1
		fi
	fi
	;;
esac

 [ "$mt" = "ms" ] || exit 1	## sanity check

bdry=`echo "$CH" | sed -n \
		-e 's/[Bb][Oo][Uu][Nn][Dd][Aa][Rr][Yy]=/;boundary=/' \
		-e 's/.*;boundary=/boundary=/' \
		-e 's/^boundary=\([^;]*\);.*/boundary=\1/' \
		-e 's/^boundary="\([^"]*\)".*/boundary=\1/' \
		-e 's/[ 
	][ 
	]*$//' \
		-e 's/^boundary=//p'`

xbdry=`echo "$bdry" | sed -e 's"/"\\\\/"g' -e 's"\."\\\\."g'`

sed -e '1,/^--'"$xbdry"'[ 
	]*$/d' $FILE > $TEMP/body

sed -e '/^--'"$xbdry"'[ 
	]*$/,$d' \
	-e "$deblank" $TEMP/body |
    sed -e '$d' -e 's/$/
/' > $TEMP/msg

sed -e '1,/^--'"$xbdry"'[ 
	]*$/d' $TEMP/body |
	sed -n -e '/BEGIN PGP /,/END PGP /p' > $TEMP/msg.asc

gpg2 --verify $TEMP/msg.asc

