/* This is an Arexx script which you can use to decrypt your netmail. This  */
/* script is only useful in conjunction with PGP.                           */
/* Decrypt.spot Version 1.22 © 1993 by Wim Van Goethem                      */
/*                            Enhancements by Nico Francois                 */
/* For comments, remarks, bugs, etc. contact:                               */
/* Fidonet: 2:292/603.6                                                     */
/* Internet: wim@wolf359.augfl.be                                           */

/* Path were you keep PGP (must end on "/" or ":" or be empty) */
PGPpath = ""
DelPGPPASS=1  /* Set this to '1' if you want that the password will be deleted when the script has done. */

options results
address spot

if ~show(Libraries,'rexxsupport.library') then
    if ~addlib("rexxsupport.library",0,-30,0) then exit

/* Does PGPPASS already exists? If so then we won't delete PGPPASS if DelPGPPASS=1. */
NoDelPGPPASS=0
IF EXISTS('env:PGPPASS') THEN NoDelPGPPASS=1

'saveascii TO T:pgptemp_a OVERWRITE NOKLUDGES'    /* Save the msg */

/* Get all headerinfo */
'getfrom'
from_name = result
'getfromaddress'
from_adres = result
'getsubject'
subject = result
'getto'
to_name = result
'gettoaddress'
to_adres = result

/* Do we have a key from this user? */
address command PGPpath"PGP -kv >T:users"
foo = open('file','T:users','R')
ok = 0
do until (ok > 0) | (eof('file'))
    ok = pos(upper(from_name),upper(readln('file')))
end
foo = close('file')
foo = delete('T:users')
if ok == 0 then    /* Key not found! */
do
    address spot
    'requestnotify "Public key of 'from_name' missing! If an error occurs, press Return"'
end

/* Let's get the password */ 
if ~exists("ENV:PGPPASS") then  /* We suppose that this password is correct, we do not test any further */
do
    foo = open('file','T:pgptemp_x','W')    /* This is a testfile */
    foo = writeln('file','Testing...')
    foo = close('file')
    do until rc == 0     /* if rc = 0 , the password is valid */
        address spot
        'requeststring PROMPT "     Please give your password     " TITLE "Encryption" INVISIBLE'
        if rc == 5 then  /* User pressed 'Cancel' */
        do
            foo = delete('T:pgptemp_x')
            foo = delete('T:pgptemp_x.asc')
            foo = delete('T:pgptemp_a')
            foo = delete('env:PGPPASS')
            say "Please close window to quit."
            IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
            EXIT
        end
        address command ""setenv PGPPASS ""||result||""
        /* Is it correct? */
        address command PGPpath'PGP >NIL: +batchmode -sa T:pgptemp_x'
        foo = delete('T:pgptemp_x.asc')
    end
    foo = delete('T:pgptemp_x')
end

/* Search the PGP block */
foo = open('infile','T:pgptemp_a','R')  /* The input file */
foo = open('outfile','T:pgptemp_b','W') /* The output file */
do while ~eof('infile')                 /* Repeat until end of file */
    instring = readln('infile')
    if left(instring,26)=="-----BEGIN PGP MESSAGE----" then /* We found the beginning */
    do
        foo = writeln('outfile',instring)
        do while left(instring,24)~=="-----END PGP MESSAGE----" /* We found the end */
            instring = readln('infile')
            foo = writeln('outfile',instring)
        end
    end
end
foo = close('infile')
foo = close('outfile')
foo = delete('T:pgptemp_a') /* We won't need this anymore */

/* Decrypt the message */
options failat 100
address command PGPpath"PGP T:pgptemp_b -o T:pgptemp_c"
if rc ~= 0 then do
    foo = delete('T:pgptemp_b')
    say "Please close window to quit."
    IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
    exit
    end
foo = delete('T:pgptemp_b') /* We won't need this anymore */

/* Make sure the subject is back restored to the original subject           */
foo = open('infile','T:pgptemp_c','R')          /* The input file           */
foo = open('outfile','T:pgptemp_d','W')         /* The output file          */
instring = readln('infile')                     /* Get the subject line     */
subject = right(instring,length(instring)-10)   /* Get the subject          */
do while ~eof('infile')                         /* Repeat until end of file */
    instring = readln('infile')
    foo = writeln('outfile',instring)
end
foo = close('infile')
foo = close('outfile')
foo = delete('T:pgptemp_c') /* We won't need this anymore */

/* Put the decrypted msg back into the message base */
address spot
'edit FILE T:pgptemp_d REFLOW=OFF SUBJECT "'subject'" NOEDIT NOGUI NOSIG NOREQ'
foo = delete('T:pgptemp_d') /* We won't need this anymore (gets a bit boring, don't you think?:-) */

/* Quit */
say "Please close window to quit."
IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
exit
