/* This is an Arexx script which you can use to encrypt your netmail. This  */
/* script is only useful in conjunction with PGP.                           */
/* Encrypt.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 = ""
cr = '0D'x
DelPGPPASS=1  /* Set this to '1' if you want that the password will be deleted when the script has done. */

address spot
options results

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
'getsubject'
subject = result
'getto'
to_name = result
'gettoaddress'
to_adres = result
if rc == 5 then /* This is not a netmail area, exit */
do
    foo = delete('T:pgptemp_a')
    'requestnotify "You are not in a netmail area.'cr'Only netmail can be encrypted."'
    IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
    exit
end

/* 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(to_name),upper(readln('file')))
end
foo = close('file')
foo = delete('T:users')
address spot
if ok == 0 then /* Key not found! */
do
    foo = delete('T:pgptemp_a')    
    'requestnotify "Public key of 'to_name' missing!"'
    say "Please close window to quit."
    IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
    exit
end
'clearflags EXPORT' /* We want only the encrypted msg to be exported */

/* 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')
            'setflags EXPORT' /* We haven't encrypted anything, so this msg must be exported */
            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

/* Delete the five first lines from the msg */
foo = open('infile','T:pgptemp_a','R')
foo = open('outfile','T:pgptemp_b','W')
do x=1 to 5
    foo = readln('infile')
end
foo = writeln('outfile','*** Subj: ' || subject || '0a'x)
do while ~eof('infile')
    instring = readln('infile')
    foo = writeln('outfile',instring)
end
foo = close('infile')
foo = close('outfile')
foo = delete('T:pgptemp_a') /* We won't need this anymore */

/* Encrypt the msg with PGP */
options failat 100
address command PGPpath'PGP -esta T:pgptemp_b "'to_name'"'
if rc ~= 0 then do
    foo = delete('T:pgptemp_b')
    foo = delete('T:pgptemp_b.asc')
    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 */

/* Put the encrypted msg back into the msgbase */
address spot
'write FILE T:pgptemp_b.asc REFLOW=OFF TO "'to_name'" TOADDR 'to_adres' FROM "'from_name'" SUBJECT "-- PGP --" NOEDIT NOGUI NOSIG'
'clearflags EXPORT' /* This msg should not to be exported */
'getmessagenum'     /* Get this msg number so we can jump back to it later */
orig_msg = result
'lastmessage'       /* The (encrypted) msg we just wrote should be the last in the msgbase */
'setflags KILLSENT' /* Set the 'Kill/Sent' flag on */
'gotomessage "'orig_msg'"'  /* Go back to the original msg */

/* Delete the *.asc file in T: */
foo = delete('T:pgptemp_b.asc')

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