
# Perl script for passing PGP portions through PGP and leaving the
# rest alone.
# This version just does 1 PGP portion, and it has to be just after
# the header.  This is for security, so people can't send us packets
# to be decrypted and get them back.

# Scratch file names
$scr1 = "/tmp/pm1_" . $$ ;
$scr2 = "/tmp/pm2_" . $$ ;

open (STDOUT, "| recurse.pl");
#open (LOG, ">/tmp/PGP$$");
open (LOG, ">/dev/null");

print (LOG "Doing header...\n" );
# Print out message header.
while (<>) {
    last if (/^$/) ;
    next if /^Encrypted:/ ;	# Delete 'Encrypted' header
    print $_ ;
}

    print "\n" ;

# Eat blank lines
while (<>) {
    last if (!/^$/) ;
}
print (LOG "Blank lines eaten...\n" );

if (/^-----BEGIN PGP MESSAGE-----$/) {
   print (LOG "Found PGP header...\n" );
    open ( OUT1, ">" .  $scr1 ) ;

    print OUT1 $_ ;

    while ( <> ) {
	print OUT1 $_ ;
	last if (/^-----END PGP MESSAGE-----$/) ;
    }

    close ( OUT1 ) ;

   print (LOG "Printing PGP header to file " . $scr1 . "\n" );

    $stat = system ( "PGPPATH=./pgp PGPPASS=remailer_key  ./pgp/pgp -f " . "<" . $scr1 . " 2>/dev/null >" . $scr2 ) ;

    if ( $stat == 0 ) {
   print (LOG "PGP Succeeded\n" );
	open ( IN1, $scr2 ) ;	# Use output if PGP succeeded
    } else {
   print (LOG "PGP Failed\n" );
	open ( IN1, $scr1 ) ;	# Ignore output if PGP failed
    }

   print (LOG "Copying results of PGP run...\n" );
    while ( <IN1> ) {
	s/\r$// ;		# Remove trailing CR's
	print $_ ;
    }

    unlink ( $scr1 ) ;		# Remove scratch files
    unlink ( $scr2 ) ;
} else {
    print $_ ;
}

print (LOG "Copying remainder of file...\n" );

#Copy remainder of file
print <>;
close(LOG);
