/* SignText.spot Version 1.0 © 1994 by Chris Dee                            */
/*   (Based on Encrypt.spot Version 1.2 © 1993 by Wim Van Goethem)          */
/*                                                                          */
/* This Arexx script is intended to sign your current displayed msg (with   */
/* PGP). It uses PGP which has been ported to the Amiga by Peter Simons.    */
/*                                                                          */
/* For comments, remarks, bugs, etc. contact:                               */
/* Fidonet: 2:286/407.34 (Chris Dee)                                        */
/* Internet: cd@grafix.wlink.nl                                             */

/* Path were you keep PGP (must end on "/" or ":" or be empty) */
PGPpath = "Mail: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 NOTEARLINE NOORIGIN NOKLUDGES' /* Save the msg */

/* Get all headerinfo */
'getfrom'
from_name = result

/* 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 "Make signature" 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."
            exit
        end
        address command ""setenv PGPPASS ""||result||""
        /* Is it correct? */
        address command PGPpath'PGP >NIL: +batchmode -sa T:pgptemp_x -u "'from_name'"'
        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

/* Check for the To: klugde */
instring = readln('infile')
ok = pos('To:', instring)
if ok == 0 then
do
    /* To: kludge not found so let's put this line in the output file */
    foo = writeln('outfile',instring)
end
else
do
    /* To: kludge found so let's strip the next line, which is empty, too */
    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 */

/* Sign the msg with PGP */
options failat 100
address command PGPpath'PGP +clearsig +batchmode -ftsa <T:pgptemp_b >T:pgptemp_c -u "'from_name'"'
if rc ~= 0 then do
    foo = delete('T:pgptemp_b')
    foo = delete('T:pgptemp_c')
    say ""
    say ""
    say "Please close window to quit."
    exit
    end

/* Put the signed msg back into the message base */
address spot
'edit FILE T:pgptemp_c REFLOW=OFF NOEDIT NOGUI NOSIG NOREQ'

foo = delete('T:pgptemp_b')
foo = delete('T:pgptemp_c')

/* Quit */
say ""
say ""
say "Please close window to quit."
exit
