/********************************************************************/
/*                                                                  */
/* EncipherPGP.ced - ARexx macro for use with Cygnus Editor and PGP */
/*                                                                  */
/* Mark a block of text you want to be encrypted using PGP, then    */
/* run this macro to encrypt it wit someone's public key. It works  */
/* similar to builtin rot13 command. (Only MUCH slower).            */
/* This macro was tested to work with Cygnus Editor 2.0 to 3.5.     */
/*                                                                  */
/* This code is in Public Domain under GNU General Public License   */
/*                                                                  */
/* author: Janusz A. Urbanowicz <alex@vm.cc.uni,torun.pl>           */
/*                                                                  */
/********************************************************************/

/*
 *                $VER: EncipherPGP.ced 1.2 (30.10.1994)
 */

Trace Off

Options Results

lf = '0A'X
tempfile = 'T:ced$pgp.tmp'
cmdline = ''
cmdn = '-ca'
cmdf = '<'||tempfile||' >'||tempfile||'.asc -fca'
pgpt = 0 /* pgp 'type' 0 = 2.3a, 1 = 2.3a.?, 2 = 2.6<ui> */
scrname = 'CygnusEdScreen'
scrnum = 1
portname = 'rexx_ced'
portnum = 0

cmd = ''
scratch = 0
ID = ''

status portnumber
portnum = result
if (portnum ~= 0)&(portnum ~== 'RESULT') Then
Do
        scrnum = scrnum + portnum
        portname = portname||portnum
End

scrname = scrname||scrnum

Call Close 'STDOUT'             /* Thanx for that goes to Rick Younie*/
Call Close 'STDIN'
Call Open 'STDOUT','CON:16/24/620/130/PGPAmiga Output/CLOSE/WAIT/SCREEN'||scrname
Call Pragma '*','STDOUT'
Call Open 'STDIN','*'

Call checkf
pgpt = result
If pgpt > 0 then cmd = cmdf
Else
Do
        cmd = cmdn
        cmdline = tempfile||' '
End

Address 'rexx_ced'
If pgpt = 1 Then /* is PGP26_IMPERSONATE option avaliable ? */
Do
        okay2 "Do you want the enciphered packet to be"||lf,
                "in PGP 2.6 format ?"
        If result = 1 Then
        Do
                okay1 "WARNING: Packet created with this"||lf,
                ||"option set cannot be processed"||lf,
                ||"using 'standard' PGP 2.3a."||lf||lf,
                ||"You SHOULD NOT use this option"
                okay2 "Are you sure you want to use"||lf,
                        ||"this option ?"
                If result = 1 Then cmdline = cmdline||' +PGP26_IMPERSONATE=on'
        End
End
Else cmdline = cmdline||' +batchmode=on'

If cmd = cmdn Then Call ask

cmdline = cmd||' '||cmdline

cut block
If result = 1 Then Call encrypt
Else
Do
        okay2 "No area selected."||lf||"Encipher whole file ?"
        If result = 1 Then
        Do
                'beg of file'
                'mark block'
                'end of file'
                'cut block'
                Call encrypt
        End
End
Call quit

encrypt:
Do
        menu 0 6 0 tempfile /* save block to file */
        Address Command
        'PGP '||cmdline
        If ~Exists(tempfile||'.asc') Then Call wrongpass
        Else
        Do
/* We check if the output file length is 0 what happens if PGP was in -f mode and passphrase was wrong */
                If Open('outf',tempfile||'.asc','R') Then
                Do
                        If Seek('outf',100,'Begin') = 0 Then Call wrongpass
                        Else
                        Do
                                Address 'rexx_ced'
                                include file tempfile||'.asc'
                                status 21
                        End
                End
        End
End
Return

wrongpass:
Address 'rexx_ced'
undo
okay1 "Encryption error !"

quit:
Address Command
If Exists(tempfile||'.info') Then 'Delete '||tempfile||'.info QUIET'
If Exists(tempfile||'.asc') Then 'Delete '||tempfile||'.asc QUIET'
If Exists(tempfile) Then 'Delete '||tempfile||' QUIET'
If scratch == 1 Then 'Delete env:PGPPASS QUIET'
If ~Close('STDOUT') Then Nop
Exit 0

ask: Procedure Expose scratch lf
Address Command
'GetEnv >NIL: PGPPASS'
If rc ~= 0 Then
Do
        Address 'rexx_ced'
        okay2 "Your passphrase is not set in PGPPASS variable."||lf||,
        "It must be set temporarily for running PGP".||lf||,
        "Should it be deleted (for higher security) after use ??"
        If result = 1 then scratch = 1
        else scratch = 0
        okay1 'WARNING: Your passphrase will be visible when you type it in.'
        getstring "'  '  'Please enter passphrase.'"
        pgppass = result
        pgppass = Strip(pgppass,'T')
        Address Command
        'SetEnv PGPPASS '||'"'||pgppass||'"'
End
Return

checkf: Procedure
Address Command
Call getpath
path = result
If path = "" Then Return 0
Else
Do
        'Version >PIPE:PGPVERSION '||path
        If Open('pvers','PIPE:PGPVERSION','R') Then
        Do
                verstring = ReadLN('pvers')
                If Close('pvers') Then Nop
        End
        Parse Var verstring . version
        Select
                When version = '2.6ui' Then Return 2
                When version = '2.3a.5' Then Return 1
                When version = '2.3a.4' Then Return 1
                When version = '2.3a.3' Then Return 1
                Otherwise Return 0
        End
End

getpath: procedure
'Which >PIPE:PGPPATH PGP'
If rc = 0 Then
Do
        If Open('ppath','PIPE:PGPPATH','R') Then
        Do
                path = ReadLN('ppath')
                If ~Close('ppath') Then Nop
                Return path
        End
End
Else
Do
        'GetEnv >PIPE:PGPPATH PGPPATH'
        If rc = 0 Then
        Do
                If Open('ppath','PIPE:PGPPATH','R') Then
                Do
                        path = ReadLN('ppath')
                        If ~Close('ppath') Then Nop
                        path = path||'/PGP'
                        Return path
                End
                Else Return ""
        End
