#!/usr/local/bin/perl #
#
# filter for pine 3.93 and newer for decoding pgp mails
# use this with the following in your .pinerc:
# display-filters=_BEGINNING("-----BEGIN PGP")_ /home/roland/bin/pgpdecode
#
# add the command line option -dontask to pgpdecode, if you do not want to be 
# asked whether to run pgp on the mail or not.
#
# $ Id: pgpdecode,v 1.8 1997/03/19 13:59:37 roland Exp $
#
#   Copyright (C) 1996  Roland Rosenfeld 
#
#   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
#
# 21.03.96 Roland Rosenfeld  
# 23.03.96 $TMPFILE moved to $PGPPATH.
# 17.05.96 total rewrite in perl; many new features
# 27.08.96 corrected code for adding new keys
# 19.11.96 ask whether to run pgp before running it
# 25.11.96 some compatibility fixes
# 02.02.97 stty option configurable (Hoss Firooznia )

umask 077;

$stty="/bin/stty";             # where to find a stty, which supports -cbreak
                               # or raw/-raw
$sttyopt="cbreak";             # if your stty does not understand -cbreak
                               # try raw here.

$tmpdir = $ENV{'PGPPATH'};     # this offers more security than /tmp 
if ($tmpdir =~ /^$/) {
    $tmpdir = $ENV{'HOME'};    # if not set $PGPPATH try $HOME as tmp-dir
}

$pgpout = "$tmpdir/pgpout$$";
$pgperr = "$tmpdir/pgperr$$";

if (! (@ARGV[0] =~ /dontask/i) ) {  # option -dontask given?
    # no -dontask:
    print STDERR "Message uses PGP, do you wish to run PGP on it? [Y/n] ";
    system("$stty $sttyopt < /dev/tty");  # read char without pressing RETURN
    open (IN, ") { 
            print;
        }
        exit;
    }
    # when we did not exit, user wants to run pgp now.
} else {
    # -dontask set:
    shift;  # otherwise pgp seems to be confused.
}

#
# Now let us start the real decoder
#

$pgpfound=1;

while ($pgpfound) {

    while (($_=<>) && !(/^-----BEGIN PGP/)) {
        print $_;
    }
    if (/^-----BEGIN PGP/) {
        $pgpfound=1;
    } else {
        $pgpfound=0;
    }
    
    if (/^-----BEGIN PGP PUBLIC KEY BLOCK-----$/) {
        $pgpkeyblock .= $_;
        while (($_=<>) && !(/^-----END PGP PUBLIC KEY BLOCK-----$/)) {
            $pgpkeyblock .= $_;
        }
        $pgpkeyblock .= $_;
        
        open(SAVEOUT, ">&STDOUT");
        open(SAVEERR, ">&STDERR");
        open(STDOUT, ">$pgpout") || &abort("Can't redirect stdout to $pgpout");
        open(STDERR, "| tee $pgperr 1>&2") || &abort("Can't redirect stderr");
        open(PGP, "|pgp -kaf") || &abort("Can't execute pgp");
        print PGP $pgpkeyblock;
        close(PGP);
        close(STDOUT);
        close(STDERR);
        open(STDOUT, ">&SAVEOUT");
        open(STDERR, ">&SAVEERR");
        open(PGPERR, "<$pgperr") || &abort("Can't read $pgperr");

        while () {
            print "| $_";
        }
        close(PGPERR);
        unlink $pgperr;

        open(PGPOUT, "<$pgpout") || &abort("Can't read $pgpout");
        while () {
            print ": $_";
        }
        close(PGPOUT);
        unlink $pgpout;
       
    } elsif (/^-----BEGIN PGP( SIGNED)? MESSAGE-----$/) {
        $pgpmessage .= $_;
        $pgpheader = $_;

        while (($_=<>) && !(/^-----END PGP (MESSAGE|SIGNATURE)-----$/)) {
            $pgpmessage .= $_;
        }
        $pgpmessage .= $_;
        
        open(SAVEOUT, ">&STDOUT");
        open(SAVEERR, ">&STDERR");
        open(STDOUT, ">$pgpout") || &abort("Can't redirect stdout");
        open(STDERR, "| tee $pgperr 1>&2") || &abort("Can't redirect stderr");
        open(PGP, "|pgp -f") || &abort("Can't execute pgp");
        print PGP $pgpmessage;
        close(PGP);
        close(STDOUT);
        close(STDERR);
        open(STDOUT, ">&SAVEOUT");
        open(STDERR, ">&SAVEERR");
        
        open(PGPERR, "<$pgperr") || &abort("Can't read $pgperr");
        while () {
            print "| $_";
        }
        close(PGPERR);
        unlink $pgperr;

        print "|\n| $pgpheader|\n";

        open(PGPOUT, "<$pgpout") || &abort("Can't read $pgpout");
        while () {
            print $_;
        }
        close(PGPOUT);
        unlink $pgpout;
        
        $pgpheader =~ s/BEGIN/END/;
        print "\n|\n| $pgpheader|\n\n";
    }
}

exit;

sub abort {
    local ($message) = @_;
    unlink $pgpout;
    unlink $pgperr;
    die $message
}