home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / comm / fido / spot / rexx / pgp / autoencrypt.spot < prev    next >
Text File  |  1994-07-27  |  13KB  |  369 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /* AutoEncrypt.spot v1.01 © 1994 by Wim Van Goethem                        */
  4. /* If you have any suggestions or problems with this script then you can   */
  5. /* contact me at:                                                          */
  6. /* FidoNet : 2:292/603.6                                                   */
  7. /* UseNet  : wim@augfl.be                                                  */
  8. /*                                                                         */
  9. /***************************************************************************/
  10.  
  11. ADDRESS SPOT
  12. OPTIONS RESULTS
  13. TRACE OFF
  14.  
  15. PGPpath = ""  /* Path were you keep PGP (must end on "/", ":" or be empty) */
  16. CR = '0d'x
  17. DelPGPPASS=0  /* Set this to 1 if PGPPASS has to be deleted when the script exits. */
  18.  
  19. /* See if 'rexxsupport.library' is already loaded and load it if not.      */
  20. IF ~SHOW(Libraries,'rexxsupport.library') THEN
  21.     IF ~ADDLIB("rexxsupport.library",0,-30,0) THEN
  22.     DO
  23.         TextString="RexxSupport.library not found! Exitting."
  24.         CALL Error
  25.     END
  26.  
  27. /* Does PGPPASS already exists? If so then we won't delete PGPPASS if DelPGPPASS=1. */
  28. NoDelPGPPASS=0
  29. IF EXISTS('env:PGPPASS') THEN NoDelPGPPASS=1
  30.  
  31. 'lockgui'
  32.  
  33. /* Load the encryption userlist. This is a list of people were too          */
  34. /* you will only (or never) send encrypted mail.                            */
  35. foo=OPEN('userlist','Mail:Spot.encryption','R')
  36. IF ~foo THEN
  37. DO
  38.     TextString="Unable to open Mail:Spot.encryption. Exiting!"
  39.     CALL Error
  40. END
  41. UselistEntries=0
  42. DO WHILE(~EOF('userlist'))
  43.     UselistEntries=UselistEntries+1
  44.     instring=READLN('userlist')
  45.     IF POS(';',instring)~==0 THEN 
  46.     DO
  47.         User.UselistEntries=COMPRESS(LEFT(instring,POS(';',instring)-1))
  48.     END
  49.     ELSE User.UselistEntries=instring
  50.     IF LEFT(User.UselistEntries,1)==" " | LENGTH(User.UselistEntries)==0 | LEFT(User.UselistEntries,1)==";" THEN UselistEntries=UselistEntries-1
  51. END
  52. foo=CLOSE('userlist')
  53.  
  54. /* Search for netmail areas.                                                */
  55. 'firstarea'                 /* This is always a netmail area.               */
  56. DO FOREVER
  57.     'getareaname'
  58.     area=result
  59.     IF LEFT(area,4)=="NET_" THEN CALL SearchExports
  60.     ELSE
  61.     DO
  62.         foo=DELETE("T:Spot.pgptemp_orig")
  63.         foo=DELETE("T:Spot.tempusers")
  64.         'arealist'
  65.         'firstarea'
  66.         'unlockgui'
  67.         IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
  68.         EXIT
  69.     END
  70.  
  71.     'nextarea'
  72. END
  73.  
  74. /* Search for netmails which should be exported.                            */
  75. SearchExports:
  76. 'messages'
  77. 'firstmessage EXPORT'       /* Go to the first msg which has to be exported */
  78. IF ~((rc==2) | (rc==0)) THEN RETURN 0   /* No msg has to be exported in this area. */
  79. EndOfArea=0
  80. DO UNTIL EndOfArea==1  /* Search as long as there are msg's which have to be exported. */
  81.     CALL GetMsgFlags
  82.     IF SUBSTR(MsgFlags,4,1)=="1" THEN /* This msg has to be exported, now we have to see if it has to be encrypted. */
  83.     DO
  84.         CALL EncryptYesNo
  85.         IF result THEN CALL EncryptMsg /* EncryptYesNo returned 1, this msg has to be encrypted. */
  86.     END
  87.     'nextmessage'
  88.     IF rc~==0 THEN /* We have reached the end of this area.                 */
  89.     DO
  90.         EndOfArea=1
  91.         'getareaname'
  92.         SAY "Reached the end of area "||result||"."
  93.     END
  94. END
  95. RETURN 0
  96.  
  97. /* See if the reciever is in our userlist, if so we know what to do.        */
  98. /* A plus sign (+) in front of the name means encryption on.                */
  99. /* A minus sign (-) in front of the name means encryption off.              */
  100. /* If the name of the reciever isn't in the userlist, then we see if we     */
  101. /* have his key, and if so, we ask the user if he wants the msg encrypted.  */
  102. EncryptYesNo:
  103. EMailAddress=""         /* We yet don't know if this is a usenet msg or a fidonet msg. */
  104. 'gettoaddress'
  105. RecieverAddress=result  /* This is the fidonet address of the reciever.     */
  106. 'getto'
  107. Reciever=result         /* This is the name of the reciever.                */
  108. 'getfromaddress'
  109. SenderAddress=result    /* This is the fidonet address of the msg writer.   */
  110. 'getfrom'
  111. Sender=result           /* This is the name of the msg writer.              */
  112. 'getsubject'
  113. Subject=result          /* The subject of the msg.                          */
  114. 'saveascii TO "T:Spot.pgptemp_orig" OVERWRITE NOHEADER NOKLUDGES' /* Save the actual msgbody. */
  115. IF RC~==0 THEN
  116. DO
  117.     TextString="Error writing 'T:Spot.pgptemp_orig'. Exiting!"
  118.     CALL Error
  119. END
  120. /* Read the first line. If the first 3 chars are 'TO:', then the changes    */
  121. /* are that this is a usenet msg. The 'To:' is neccessary for the gate.     */
  122. foo=OPEN("infile","T:Spot.pgptemp_orig","R")
  123. IF ~foo THEN
  124. DO
  125.     TextString="Unable to open 'T:Spot.pgptemp_orig'. Exiting!"
  126.     foo=CLOSE('infile')
  127.     CALL Error
  128. END
  129. instring=READLN('infile')
  130. IF UPPER(LEFT(instring,3))=="TO:" THEN /* This is a usenet e-mail. */
  131. DO
  132.     EMailAddress=RIGHT(instring,LENGTH(instring)-4) /* Get the e-mail address. */
  133.     KeyIDString=EMailAddress
  134. END
  135. ELSE KeyIDString=RecieverAddress
  136. DO x=1 to 10 /* If there is in the first 10 lines no "-----BEGIN PGP MESSAGE----" then this msg is probable not yet encrypted. */
  137.     IF POS("-----BEGIN PGP MESSAGE-----",instring)>=1 THEN
  138.     DO
  139.         foo=CLOSE('infile')
  140.         RETURN 0
  141.     END
  142.     instring=READLN('infile')
  143. END
  144. foo=CLOSE('infile')
  145. DO x=1 TO UselistEntries /* Let's see if we have an entry in our Spot.encryption list for this reciever. */
  146.     IF POS(UPPER(KeyIDString),UPPER(User.x))~==0 THEN
  147.     DO
  148.         IF LEFT(User.x,1)=="+" THEN
  149.         DO
  150.             SAY "Msg to:'"||KeyIDString||"', subject:'"||Subject||"', encryption=ON."
  151.             RETURN 1
  152.         END
  153.         ELSE
  154.         DO
  155.             SAY "Msg to:'"||KeyIDString||"', subject:'"||Subject||"', encryption=OFF."
  156.             RETURN 0
  157.         END
  158.     END
  159. END
  160. /* Couldn't find KeyIDString in userlist, search in keyring.               */
  161. IF ~EXISTS("T:Spot.tempusers") THEN
  162. DO
  163.     ADDRESS COMMAND PGPpath"PGP -kv +verbose=0 >T:Spot.tempusers"
  164.     IF rc~==0 THEN
  165.     DO
  166.         TextString="Unable to create 'T:Spot.tempusers'. Please check that"||CR||"the script can find PGP and that you have enough memory! Exiting!"
  167.         CALL Error
  168.     END
  169. END
  170. foo=OPEN('tempusers','T:Spot.tempusers','R')
  171. IF ~foo THEN
  172. DO
  173.     TextString="Unable to open 'T:Spot.tempusers'. Exiting!"
  174.     foo=CLOSE('tempusers')
  175.     CALL Error
  176. END
  177. DO UNTIL(EOF('tempusers'))
  178.     instring=READLN('tempusers')
  179.     IF POS(UPPER(KeyIDString),UPPER(instring))~==0 THEN
  180.     DO
  181.         'getsubject'
  182.         Subject=result
  183.         'requestresponse TITLE "PGP encryption script" PROMPT "Would you like to encrypt the msg'||CR||'for '||KeyIDString||' about '||Subject||'?" GADGETS "_Yes|_No" CENTER'
  184.         IF rc==1 THEN
  185.         DO
  186.             foo=CLOSE('tempusers')
  187.             SAY "Msg to:'"||KeyIDString||"', subject:'"||Subject||"', encryption=ON."
  188.             RETURN 1
  189.         END
  190.         ELSE
  191.         DO
  192.             foo=CLOSE('tempusers')
  193.             SAY "Msg to:'"||KeyIDString||"', subject:'"||Subject||"', encryption=OFF."
  194.             RETURN 0
  195.         END
  196.     END
  197. END
  198. foo=CLOSE('tempusers')
  199. SAY "Msg to:'"||KeyIDString||"', subject:'"||Subject||"', encryption=OFF."
  200. RETURN 0
  201.  
  202. /* Here we prepare the msg for encypting.                                   */
  203. EncryptMsg:
  204. IF ~EXISTS("Env:PGPPASS") THEN
  205. DO
  206.     CALL GetPassWord
  207.     IF ~result THEN
  208.     DO
  209.         TextString="User aborts encryption. Exiting!"
  210.         CALL Error
  211.     END
  212. END
  213. NewSubject="*** Subj: "||Subject
  214. foo=OPEN('outfile',"T:Spot.pgptemp_temp","W")
  215. IF ~foo THEN
  216. DO
  217.     TextString="Unable to open 'T:Spot.pgptemp_temp'. Exiting!"
  218.     CALL Error
  219. END
  220. foo=WRITELN('outfile',NewSubject)
  221. IF foo~==LENGTH(NewSubject)+1 THEN
  222. DO
  223.     TextString="Error writing 'T:Spot.pgptemp_temp'. Exiting!"
  224.     CALL Error
  225. END
  226. foo=CLOSE('outfile')
  227. IF EMailAddress=="" THEN /* The reciever has a FidoNet address.             */
  228. DO
  229.     ADDRESS COMMAND "Join T:Spot.pgptemp_temp T:Spot.pgptemp_orig to T:Spot.pgptemp_plainmsg"
  230.     foo=DELETE("T:Spot.pgptemp_temp")
  231.     foo=DELETE("T:Spot.pgptemp_orig")
  232.     CALL Encrypt
  233.     CALL GetMsgFlags
  234.     IF SUBSTR(MsgFlags,5,1)~=="1" THEN Subject="-- PGP --"
  235.     'write TO "'Reciever'" TOADDR "'RecieverAddress'" FROM "'Sender'" FROMADDR "'SenderAddress'" SUBJECT "'Subject'" FILE T:Spot.msgbody REFLOW=OFF NOEDIT NOGUI NOSIG'
  236. END
  237. IF EMailAddress~=="" THEN /* The reciever has a UseNet address.             */
  238. DO
  239.     /* Get those first 2 lines out of the msg text.                         */
  240.     foo=OPEN('outfile',"T:Spot.pgptemp_temp","A")
  241.     IF ~foo THEN
  242.     DO
  243.         TextString="Unable to open 'T:Spot.pgptemp_temp'. Exiting!"
  244.         CALL Error
  245.     END
  246.     foo=OPEN("infile","T:Spot.pgptemp_orig","R")
  247.     IF ~foo THEN
  248.     DO
  249.         TextString="Unable to open 'T:Spot.pgptemp_orig'. Exiting!"
  250.         CALL Error
  251.     END
  252.     DO x=1 TO 2
  253.         instring=READLN('infile')
  254.     END
  255.     DO UNTIL(EOF('infile'))
  256.         instring=READLN('infile')
  257.         foo=WRITELN('outfile',instring)
  258.         IF foo~==LENGTH(instring)+1 THEN
  259.         DO
  260.             TextString="Error writing 'T:Spot.pgptemp_orig'. Exiting!"
  261.             CALL Error
  262.         END
  263.     END
  264.     foo=CLOSE('outfile')
  265.     foo=CLOSE('infile')
  266.     foo=DELETE("T:Spot.pgptemp_orig")
  267.     ADDRESS COMMAND "Rename T:Spot.pgptemp_temp T:Spot.pgptemp_plainmsg"
  268.     CALL Encrypt
  269.     CALL GetMsgFlags
  270.     IF SUBSTR(MsgFlags,5,1)~=="1" THEN Subject="-- PGP --"
  271.     'write TO "'Reciever'" TOADDR "'RecieverAddress'" EMAILADDR "'EMailAddress'" FROM "'Sender'" FROMADDR "'SenderAddress'" SUBJECT "'Subject'" FILE T:Spot.msgbody REFLOW=OFF NOEDIT NOGUI NOSIG'
  272. END
  273. foo=DELETE("T:Spot.msgbody")
  274. 'getmessagenum'
  275. OrigMsg=result
  276. CALL GetMsgFlags
  277. 'lastmessage'
  278. 'setflags KILLSENT'
  279. IF SUBSTR(MsgFlags,2,1)=="1" THEN 'setflags CRASH'
  280. IF SUBSTR(MsgFlags,5,1)=="1" THEN 'setflags FILEATTACH'
  281. IF SUBSTR(MsgFlags,10,1)=="1" THEN 'setflags HOLD'
  282. 'gotomessage "'OrigMsg'"'
  283. 'clearflags EXPORT'
  284. IF SUBSTR(MsgFlags,8,1)=="1" THEN
  285. DO
  286.     'deletemessage NOREQ'
  287.     'prevmessage'
  288. END
  289. RETURN 0
  290.  
  291. GetMsgFlags: PROCEDURE EXPOSE MsgFlags
  292. MsgFlags=""
  293. 'getmessageflags'
  294. MsgFlags=REVERSE(OVERLAY(c2b(d2c(result)), '000000000000000000000000', 24-LENGTH(c2b(d2c(result)))+1))
  295. RETURN 0
  296.  
  297. /* Ask the user his password and save it as an env variable.                */
  298. GetPassWord:
  299. 'requeststring TITLE "PGP encryption script" PROMPT "Please give your PGP password." INVISIBLE CENTER'
  300. IF rc==5 THEN RETURN 0
  301. ADDRESS COMMAND 'Setenv PGPPASS "'result'"'
  302. RETURN 1
  303.  
  304. Encrypt:
  305. check=-1
  306. DO UNTIL check==0 | check==1 /* Repeat until PGP exits normally */
  307.     ADDRESS COMMAND PGPpath'PGP +batchmode -esta T:Spot.pgptemp_plainmsg "'KeyIDString'"'
  308.     check = rc
  309.     IF ~(check==0 | check==1) THEN
  310.     DO
  311.         SELECT
  312.             WHEN check==6 THEN 
  313.             DO
  314.                 TextString="Stopped at user request. Exitting..."
  315.                 CALL Error
  316.             END
  317.             WHEN check==20 THEN
  318.             DO
  319.                 'requestresponse TITLE "PGP error" PROMPT "Bad pass phrase. Do you wish to enter a new one?" GADGETS "_Yes, I want to try again.|_No, Quit." CENTER'
  320.                 IF rc~==0 THEN
  321.                 DO
  322.                     CALL GetPassWord
  323.                     IF ~result THEN
  324.                     DO
  325.                         TextString="User aborts encryption. Exiting!"
  326.                         CALL Error
  327.                     END
  328.                 END
  329.                 ELSE
  330.                 DO
  331.                     TextString="User aborts encryption. Exiting!"
  332.                     CALL Error
  333.                 END
  334.             END
  335.             WHEN check==21 THEN 
  336.             DO
  337.                 TextString="Cannot find the public key matching userid '"||KeyIDString||"'."||CR||"Exitting..."
  338.                 CALL Error
  339.             END
  340.             OTHERWISE
  341.             DO
  342.                 TextString="Error '"||check||"' occured. Exitting..."
  343.                 CALL Error
  344.             END
  345.         END
  346.     END
  347. END
  348. ADDRESS COMMAND "Rename T:Spot.pgptemp_plainmsg.asc T:Spot.msgbody"
  349. IF rc~==0 THEN
  350. DO
  351.     TextString="Error renaming 'T:Spot.pgptemp_plainmsg.asc' to 'T:Spot.msgbody'. Exiting!"
  352.     CALL Error
  353. END
  354. foo=DELETE("T:Spot.pgptemp_plainmsg")
  355. RETURN 0
  356.  
  357.  
  358. /* A fatal error occured. We have to exit.                                  */
  359. Error:
  360. 'requestnotify PROMPT "'TextString'" CENTER'
  361. foo=DELETE("T:Spot.pgptemp_orig")
  362. foo=DELETE("T:Spot.tempusers")
  363. foo=DELETE("T:Spot.pgptemp_plainmsg")
  364. foo=DELETE("T:Spot.pgptemp_temp")
  365. foo=DELETE("T:Spot.pgptemp_msgbody")
  366. foo=DELETE("T:Spot.pgptemp_plainmsg.asc")
  367. 'unlockgui'
  368. IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
  369. EXIT