#!/bin/sh

########################################################
## ez-pine-gpg-sign-and-encrypt                       ##
## part of ez-pine-gpg v0.4h // gpg filters for pine  ##
## http://Business-PHP.com/opensource/ez-pine-gpg/    ##
## (c) 13 Feb 2004, Atom Emet                         ##
## (c) 16 Dec 2004, Atom Emet                         ##
## Atom {at} Business-PHP.com                         ##
## Distributed under the GNU General Public License   ##
## http://www.gnu.org/copyleft/gpl.html               ##
##                                                    ##
##  ################################################  ##
##                                                    ##
## This program is free software; you can             ##
## redistribute it and/or modify it under the terms   ##
## of the GNU General Public License as published by  ##
## the Free Software Foundation; either version 2 of  ##
## the License, or (at your option) any later         ##
## version.                                           ##
##                                                    ##
## This program is distributed in the hope that it    ##
## will be useful, but WITHOUT ANY WARRANTY; without  ##
## even the implied warranty of MERCHANTABILITY or    ##
## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     ##
## General Public License for more details.           ##
##                                                    ##
## You should have received a copy of the GNU         ##
## General Public License along with this program;    ##
## if not, write to the                               ##
## Free Software Foundation, Inc.,                    ##
## 59 Temple Place - Suite 330,                       ##
## Boston, MA  02111-1307, USA.                       ##
########################################################

#########################################
## PATH is configured during installation
PATH=/bin:/usr/bin:/usr/bin

###################
## clear the screen
clear 1>&2

######################################################
## this compensates for an un-acknowledged bug in pine
stty -f /dev/tty echo

###############################################################
## display notices and give the user an opportunity to bail out
##
echo '#########################################################################' 1>&2
echo '## This message is being processed by ez-pine-gpg v0.4h                ##' 1>&2
echo '## http://Business-PHP.com/opensource/ez-pine-gpg/                     ##' 1>&2
echo '## (c) 13 Feb 2004, Atom Emet                                          ##' 1>&2
echo '## (c) 16 Dec 2004, Atom Emet                                          ##' 1>&2
echo '## Distributed under the GNU General Public License                    ##' 1>&2
echo '## See source for details                                              ##' 1>&2
echo '##                                                                     ##' 1>&2
echo '##             This filter signs and encrypts your message.            ##' 1>&2
echo '##                                                                     ##' 1>&2
echo '##      (May not be safe for BCC recipients -- see documentation)      ##' 1>&2
echo '##                                                                     ##' 1>&2
echo '## If you want to cancel sending press ^C                              ##' 1>&2
echo '#########################################################################' 1>&2

#######################################
## function to gracefully handle errors
## if something goes wrong display an error message
## that includes the recipients
##
gpg_failed () {
    echo '#########################################################################' 1>&2
    echo '##      Something went wrong     ####     See above for more info      ##' 1>&2
    echo '#########################################################################' 1>&2
    echo "## Tried signing message from: ${from}"                                    1>&2
    echo '#########################################################################' 1>&2
    echo "## Tried encrypting to: <user>`printf '\011'`(key ID)"                     1>&2
    for each_recipient in ${@}
    do
	[ "`echo ${each_recipient} | egrep '@'`" ] && echo "##     <${each_recipient}>`printf '\011'`(`gpg --with-colons --list-keys ${each_recipient} 2> /dev/null | egrep ^pub | head -n 1 | cut -d : -f 5`)" 1>&2
    done
    echo '#########################################################################' 1>&2
    ########################
    ## unset these variables
    recipients= ; each_recipient= ; from=
    #############################################################################
    ## a non-zero exit status will force pine to recognize that the script failed
    ## forcing an acknowledgment here will leave diagnostic info visible to user
    echo '/////////////////////// Press <ENTER> to continue ///////////////////////' 1>&2
    read dead < /dev/tty
    clear 1>&2
    exit 1
}

###########################################
## do the right thing even if ^C is pressed
##
trap 'echo 1>&2 ; gpg_failed ${@}' 2

##########################################################
## the variable "msg" is the full headers and message body
##
msg="`cat`"

########################################################################
## use "ez-pine-gpg-helper-sender" to find what role is sending the mail
##
from=`echo "${msg}" | ez-pine-gpg-helper-sender`

###############################################
## recipients are passed as arguments from pine
## and processed by "ez-pine-gpg-helper-recipient"
## if a key can't be found for an address then
## bail out with an error message
##
recipients=`ez-pine-gpg-helper-recipient ${@}` || gpg_failed ${@}

################################################################
## pipe the message body (minus headers) into gpg sign + encrypt
## pine will read the signed message from STDOUT
##
cr=`printf '\015'`
echo "${msg}" | sed "1,/^${cr}\$/d" \
	| gpg --sign --encrypt --local-user "${from}" --armor ${recipients} || gpg_failed ${recipients}

#############################################
## unset variables that we don't need anymore
##
recipients= ; each_recipient= ; from= ; msg=

#############
## we're done
##
exit 0
