/* 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.03 © 1993 by Wim Van Goethem                      */
/* 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 = "Hard3:Fido/bin/"

ADDRESS SPOT
OPTIONS RESULTS

IF ~SHOW(Libraries,'rexxsupport.library') THEN
    IF ~ADDLIB("rexxsupport.library",0,-30,0) THEN EXIT

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

/* Get all headerinfo */
'getfrom'
from_name = result
'getsubject'
onderwerp = 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. Only netmail can be encrypted."'
    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(to_name,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."
    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."
            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
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 */
ADDRESS COMMAND PGPpath'PGP -esta T:pgptemp_b '||to_name
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 "'onderwerp'" NOEDIT NOGUI NOSIG'
'clearflags EXPORT' /* This msg should not to be exported */

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

/* Quit */
SAY "Please close window to quit."
EXIT
