/***************************************************************************/
/*                                                                         */
/* AutoDecrypt.spot v1.00 © 1994 by Wim Van Goethem                        */
/* If you have any suggestions or problems with this script then you can   */
/* contact me at:                                                          */
/* FidoNet : 2:292/603.6                                                   */
/* UseNet  : wim@augfl.be                                                  */
/*                                                                         */
/***************************************************************************/

ADDRESS SPOT
OPTIONS RESULTS
TRACE OFF /* COMMANDS */

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

/* See if 'rexxsupport.library' is already loaded and load it if not. */
IF ~SHOW(Libraries,'rexxsupport.library') THEN
    IF ~ADDLIB("rexxsupport.library",0,-30,0) THEN
    DO
        TextString="RexxSupport.library not found! Exitting."
        CALL Error
    END

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

/* Search for netmail areas. */
'arealist'
'firstarea'     /* This is always a netmail area. */
DO FOREVER
    'getareaname'
    area=result
    IF LEFT(area,4)=="NET_" THEN
    DO
        CALL SearchImports
    END
    ELSE
    DO
        foo=DELETE("T:Spot.pgptemp")
        'arealist'
        'firstarea'
/*         'unlockgui' */
        IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
        EXIT
    END
    'arealist'
    'nextarea'
END

/* Search for netmails which should be imported. */
SearchImports:
'messagelist'
'firstmessage UNREAD'            /* Go to the first msg which was imported. */
IF ~((rc==2) | (rc==0)) THEN RETURN 0  /* No msg was imported in this area. */
EndOfArea=0
DO WHILE EndOfArea==0  /* Search as long as there are msg's which have to be read. */
    CALL GetMsgFlags
    IF SUBSTR(MsgFlags,3,1)=="1" THEN /* This msg is unread, now we have to see if it has to be decrypted. */
    DO
        CALL DecryptYesNo /* Do we have to decrypt this message? */
        IF result THEN CALL PrepMsg
    END
    'nextmessage'
    IF rc~==0 THEN /* We have reached the end of this area. */
    DO
        'firstmessage UNREAD'
        EndOfArea=1
        'getareaname'
        SAY "Reached the end of area "||result||"."
    END
END
RETURN 0

DecryptYesNo:
'markmessage'
'saveascii TO T:Spot.pgptemp OVERWRITE NOHEADER NOTEARLINE NOORIGIN NOKLUDGES NOREFLOW'
/* Let's see if we can find a PGP block in here. */
foo=OPEN("infile","T:Spot.pgptemp","R")
IF ~foo THEN
DO
    TextString="Unable to open 'T:Spot.pgptemp'. Exiting!"
    foo=CLOSE('infile')
    CALL Error
END
FoundBlock=0
DO UNTIL EOF('infile') | FoundBlock==1 /* Continue until we find "-----BEGIN PGP MESSAGE-----" or we reach EOF. */
    instring=READLN('infile')
    IF POS("-----BEGIN PGP MESSAGE-----",instring)~==0 THEN FoundBlock=1
END
foo=CLOSE('infile')
'markmessage CLEAR'
IF FoundBlock=0 THEN RETURN 0
RETURN 1

PrepMsg: /* This is a msg we have to decrypt. */
foo=OPEN("infile","T:Spot.pgptemp","R") /* orig mesg */
IF ~foo THEN
DO
    TextString="Unable to open 'T:Spot.pgptemp'. Exiting!"
    foo=CLOSE('infile')
    CALL Error
END
foo=OPEN("outfile_1","T:Spot.pgptemp_a","W") /* piece before the pgpblock */
IF ~foo THEN
DO
    TextString="Unable to open 'T:Spot.pgptemp_a'. Exiting!"
    foo=CLOSE('outfile_1')
    CALL Error
END
foo=OPEN("outfile_pgp","T:Spot.pgptemp_pgp","W") /* The pgp block */
IF ~foo THEN
DO
    TextString="Unable to open 'T:Spot.pgptemp_pgp'. Exiting!"
    foo=CLOSE('outfile_pgp')
    CALL Error
END
foo=OPEN("outfile_2","T:Spot.pgptemp_b","W") /* piece after the pgp block */
IF ~foo THEN
DO
    TextString="Unable to open 'T:Spot.pgptemp_b'. Exiting!"
    foo=CLOSE('outfile_2')
    CALL Error
END
EOBL=0
instring=READLN('infile')
DO WHILE EOBL==0
    IF POS("-----BEGIN PGP MESSAGE-----",instring)~==0 THEN EOBL=1
    ELSE
    DO
        foo=WRITELN('outfile_1',instring)
        instring=READLN('infile')
    END
END
foo=CLOSE('outfile_1')
IF EOBL==1 THEN
DO
    EOBL=0
    DO WHILE EOBL==0
        IF POS("-----END PGP MESSAGE-----",instring)~==0 THEN EOBL=1
        foo=WRITELN('outfile_pgp',instring)
        instring=READLN('infile')
    END
    IF EOBL==1 THEN
    DO
        EOBL=0
        DO WHILE ~EOF('infile')
            foo=WRITELN('outfile_2',instring)
            instring=READLN('infile')
        END
    END
END
foo=CLOSE('infile')
foo=DELETE("T:Spot.pgptemp")
foo=CLOSE('outfile_pgp')
foo=CLOSE('outfile_2')
IF ~EXISTS("Env:PGPPASS") THEN /* Get the passphrase. */
DO
    CALL GetPassWord
    IF ~result THEN
    DO
        TextString="User aborts encryption. Exiting!"
        CALL Error
    END
END
IF ~EXISTS('T:Spot.pgptemp_plainmsg') THEN ADDRESS COMMAND PGPPath"PGP +batchmode T:Spot.pgptemp_pgp -o T:Spot.pgptemp_plainmsg"
Subject=""
foo=OPEN("infile","T:Spot.pgptemp_plainmsg","R")
IF ~foo THEN
DO
    TextString="Unable to open 'T:Spot.pgptemp_plainmsg'. Exiting!"
    foo=CLOSE('infile')
    CALL Error
END
instring=READLN('infile')
IF UPPER(LEFT(instring,10))==UPPER("*** Subj: ") THEN
DO
    Subject=RIGHT(instring,LENGTH(instring)-10)
    foo=OPEN("outfile","T:Spot.pgptemp_plainmsg.temp","W")
    IF ~foo THEN
    DO
        TextString="Unable to open 'T:Spot.pgptemp_plainmsg.temp'. Exiting!"
        foo=CLOSE('outfile_2')
        CALL Error
    END
    instring=READLN('infile')
    DO WHILE ~EOF('infile')
        foo=WRITELN('outfile',instring)
        instring=READLN('infile')
    END
    foo=CLOSE('outfile')
    foo=CLOSE('infile')
    foo=DELETE("T:Spot.pgptemp_plainmsg")
    foo=RENAME("T:Spot.pgptemp_plainmsg.temp","T:Spot.pgptemp_plainmsg")
END
IF Subject=="" THEN foo=CLOSE('infile')
file_a=WORD(STATEF("T:Spot.pgptemp_a"),2)
file_b=WORD(STATEF("T:Spot.pgptemp_b"),2)
IF file_a==0 & file_b==0 THEN ADDRESS COMMAND "Copy T:Spot.pgptemp_plainmsg T:Spot.pgptemp_msg"
IF file_a==0 & file_b>0 THEN ADDRESS COMMAND "Join T:Spot.pgptemp_plainmsg T:Spot.pgptemp_b TO T:Spot.pgptemp_msg"
IF file_a>0 & file_b==0 THEN ADDRESS COMMAND "Join T:Spot.pgptemp_a T:Spot.pgptemp_plainmsg TO T:Spot.pgptemp_msg"
IF file_a>0 & file_b>0 THEN ADDRESS COMMAND "Join T:Spot.pgptemp_a T:Spot.pgptemp_plainmsg T:Spot.pgptemp_b TO T:Spot.pgptemp_msg"
foo=DELETE("T:Spot.pgptemp_plainmsg")
foo=DELETE("T:Spot.pgptemp_pgp")
foo=DELETE("T:Spot.pgptemp_a")
foo=DELETE("T:Spot.pgptemp_b")
'markmessage'
'getfrom'
FromName=result
'getfromaddress'
FromAddress=result
'getto'
ToName=result
'gettoaddress'
ToAddress=result
IF Subject=="" THEN
DO
    'getsubject'
    Subject=result
END
'edit TO "'ToName'" TOADDR "'ToAddress'" FROM "'FromName'" FROMADDR "'FromAddress'" SUBJECT "'Subject'" FILE T:Spot.pgptemp_msg NOSIG NOEDIT NOGUI NOREQ'
foo=DELETE("T:Spot.pgptemp_msg")
'markmessage CLEAR'
RETURN

/* Ask the user his password and save it as an env variable.  */
GetPassWord: PROCEDURE EXPOSE TextString PGPPath
check=31
DO UNTIL check~==31
    'requeststring TITLE "PGP encryption script" PROMPT "Please give your PGP password." INVISIBLE CENTER'
    IF rc==5 THEN RETURN 0
    ADDRESS COMMAND 'Setenv PGPPASS "'result'"'
    /* Let's decrypt the file to see if the passphrase was correct */
    ADDRESS COMMAND PGPPath"PGP +batchmode T:Spot.pgptemp_pgp -o T:Spot.pgptemp_plainmsg"
    check = rc
END
RETURN 1

GetMsgFlags: PROCEDURE EXPOSE MsgFlags
MsgFlags=""
'getmessageflags'
MsgFlags=REVERSE(OVERLAY(c2b(d2c(result)), '000000000000000000000000', 24-LENGTH(c2b(d2c(result)))+1))
RETURN 0

/* A fatal error occured. We have to exit. */
Error:
'requestnotify PROMPT "'TextString'" CENTER'
foo=DELETE("T:Spot.pgptemp")
foo=DELETE("T:Spot.tempusers")
foo=DELETE("T:Spot.pgptemp_a")
foo=DELETE("T:Spot.pgptemp_pgp")
foo=DELETE("T:Spot.pgptemp_b")
foo=DELETE("T:Spot.pgptemp_plainmsg")
foo=DELETE("T:Spot.pgptemp_msg")
foo=DELETE("T:Spot.pgptemp_plainmsg.temp")
/* 'unlockgui' */
IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
EXIT