home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / comm / spot-1.3.lha / Spot / Rexx / PGP / AutoDecrypt.spot < prev    next >
Text File  |  1994-08-11  |  8KB  |  261 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /* AutoDecrypt.spot v1.00 © 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 /* COMMANDS */
  14.  
  15. PGPpath = ""  /* Path were you keep PGP (must end on "/", ":" or be empty) */
  16. CR = '0d'x
  17. DelPGPPASS=1  /* Set this to '1' if you want that the password will be deleted when the script has done. */
  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. /* Search for netmail areas. */
  32. 'arealist'
  33. 'firstarea'     /* This is always a netmail area. */
  34. DO FOREVER
  35.     'getareaname'
  36.     area=result
  37.     IF LEFT(area,4)=="NET_" THEN
  38.     DO
  39.         CALL SearchImports
  40.     END
  41.     ELSE
  42.     DO
  43.         foo=DELETE("T:Spot.pgptemp")
  44.         'arealist'
  45.         'firstarea'
  46. /*         'unlockgui' */
  47.         IF (DelPGPPASS & ~NoDelPGPPASS) THEN foo=DELETE('Env:PGPPASS')
  48.         EXIT
  49.     END
  50.     'arealist'
  51.     'nextarea'
  52. END
  53.  
  54. /* Search for netmails which should be imported. */
  55. SearchImports:
  56. 'messagelist'
  57. 'firstmessage UNREAD'            /* Go to the first msg which was imported. */
  58. IF ~((rc==2) | (rc==0)) THEN RETURN 0  /* No msg was imported in this area. */
  59. EndOfArea=0
  60. DO WHILE EndOfArea==0  /* Search as long as there are msg's which have to be read. */
  61.     CALL GetMsgFlags
  62.     IF SUBSTR(MsgFlags,3,1)=="1" THEN /* This msg is unread, now we have to see if it has to be decrypted. */
  63.     DO
  64.         CALL DecryptYesNo /* Do we have to decrypt this message? */
  65.         IF result THEN CALL PrepMsg
  66.     END
  67.     'nextmessage'
  68.     IF rc~==0 THEN /* We have reached the end of this area. */
  69.     DO
  70.         'firstmessage UNREAD'
  71.         EndOfArea=1
  72.         'getareaname'
  73.         SAY "Reached the end of area "||result||"."
  74.     END
  75. END
  76. RETURN 0
  77.  
  78. DecryptYesNo:
  79. 'markmessage'
  80. 'saveascii TO T:Spot.pgptemp OVERWRITE NOHEADER NOTEARLINE NOORIGIN NOKLUDGES NOREFLOW'
  81. /* Let's see if we can find a PGP block in here. */
  82. foo=OPEN("infile","T:Spot.pgptemp","R")
  83. IF ~foo THEN
  84. DO
  85.     TextString="Unable to open 'T:Spot.pgptemp'. Exiting!"
  86.     foo=CLOSE('infile')
  87.     CALL Error
  88. END
  89. FoundBlock=0
  90. DO UNTIL EOF('infile') | FoundBlock==1 /* Continue until we find "-----BEGIN PGP MESSAGE-----" or we reach EOF. */
  91.     instring=READLN('infile')
  92.     IF POS("-----BEGIN PGP MESSAGE-----",instring)~==0 THEN FoundBlock=1
  93. END
  94. foo=CLOSE('infile')
  95. 'markmessage CLEAR'
  96. IF FoundBlock=0 THEN RETURN 0
  97. RETURN 1
  98.  
  99. PrepMsg: /* This is a msg we have to decrypt. */
  100. foo=OPEN("infile","T:Spot.pgptemp","R") /* orig mesg */
  101. IF ~foo THEN
  102. DO
  103.     TextString="Unable to open 'T:Spot.pgptemp'. Exiting!"
  104.     foo=CLOSE('infile')
  105.     CALL Error
  106. END
  107. foo=OPEN("outfile_1","T:Spot.pgptemp_a","W") /* piece before the pgpblock */
  108. IF ~foo THEN
  109. DO
  110.     TextString="Unable to open 'T:Spot.pgptemp_a'. Exiting!"
  111.     foo=CLOSE('outfile_1')
  112.     CALL Error
  113. END
  114. foo=OPEN("outfile_pgp","T:Spot.pgptemp_pgp","W") /* The pgp block */
  115. IF ~foo THEN
  116. DO
  117.     TextString="Unable to open 'T:Spot.pgptemp_pgp'. Exiting!"
  118.     foo=CLOSE('outfile_pgp')
  119.     CALL Error
  120. END
  121. foo=OPEN("outfile_2","T:Spot.pgptemp_b","W") /* piece after the pgp block */
  122. IF ~foo THEN
  123. DO
  124.     TextString="Unable to open 'T:Spot.pgptemp_b'. Exiting!"
  125.     foo=CLOSE('outfile_2')
  126.     CALL Error
  127. END
  128. EOBL=0
  129. instring=READLN('infile')
  130. DO WHILE EOBL==0
  131.     IF POS("-----BEGIN PGP MESSAGE-----",instring)~==0 THEN EOBL=1
  132.     ELSE
  133.     DO
  134.         foo=WRITELN('outfile_1',instring)
  135.         instring=READLN('infile')
  136.     END
  137. END
  138. foo=CLOSE('outfile_1')
  139. IF EOBL==1 THEN
  140. DO
  141.     EOBL=0
  142.     DO WHILE EOBL==0
  143.         IF POS("-----END PGP MESSAGE-----",instring)~==0 THEN EOBL=1
  144.         foo=WRITELN('outfile_pgp',instring)
  145.         instring=READLN('infile')
  146.     END
  147.     IF EOBL==1 THEN
  148.     DO
  149.         EOBL=0
  150.         DO WHILE ~EOF('infile')
  151.             foo=WRITELN('outfile_2',instring)
  152.             instring=READLN('infile')
  153.         END
  154.     END
  155. END
  156. foo=CLOSE('infile')
  157. foo=DELETE("T:Spot.pgptemp")
  158. foo=CLOSE('outfile_pgp')
  159. foo=CLOSE('outfile_2')
  160. IF ~EXISTS("Env:PGPPASS") THEN /* Get the passphrase. */
  161. DO
  162.     CALL GetPassWord
  163.     IF ~result THEN
  164.     DO
  165.         TextString="User aborts encryption. Exiting!"
  166.         CALL Error
  167.     END
  168. END
  169. IF ~EXISTS('T:Spot.pgptemp_plainmsg') THEN ADDRESS COMMAND PGPPath"PGP +batchmode T:Spot.pgptemp_pgp -o T:Spot.pgptemp_plainmsg"
  170. Subject=""
  171. foo=OPEN("infile","T:Spot.pgptemp_plainmsg","R")
  172. IF ~foo THEN
  173. DO
  174.     TextString="Unable to open 'T:Spot.pgptemp_plainmsg'. Exiting!"
  175.     foo=CLOSE('infile')
  176.     CALL Error
  177. END
  178. instring=READLN('infile')
  179. IF UPPER(LEFT(instring,10))==UPPER("*** Subj: ") THEN
  180. DO
  181.     Subject=RIGHT(instring,LENGTH(instring)-10)
  182.     foo=OPEN("outfile","T:Spot.pgptemp_plainmsg.temp","W")
  183.     IF ~foo THEN
  184.     DO
  185.         TextString="Unable to open 'T:Spot.pgptemp_plainmsg.temp'. Exiting!"
  186.         foo=CLOSE('outfile_2')
  187.         CALL Error
  188.     END
  189.     instring=READLN('infile')
  190.     DO WHILE ~EOF('infile')
  191.         foo=WRITELN('outfile',instring)
  192.         instring=READLN('infile')
  193.     END
  194.     foo=CLOSE('outfile')
  195.     foo=CLOSE('infile')
  196.     foo=DELETE("T:Spot.pgptemp_plainmsg")
  197.     foo=RENAME("T:Spot.pgptemp_plainmsg.temp","T:Spot.pgptemp_plainmsg")
  198. END
  199. IF Subject=="" THEN foo=CLOSE('infile')
  200. file_a=WORD(STATEF("T:Spot.pgptemp_a"),2)
  201. file_b=WORD(STATEF("T:Spot.pgptemp_b"),2)
  202. IF file_a==0 & file_b==0 THEN ADDRESS COMMAND "Copy T:Spot.pgptemp_plainmsg T:Spot.pgptemp_msg"
  203. IF file_a==0 & file_b>0 THEN ADDRESS COMMAND "Join T:Spot.pgptemp_plainmsg T:Spot.pgptemp_b TO T:Spot.pgptemp_msg"
  204. IF file_a>0 & file_b==0 THEN ADDRESS COMMAND "Join T:Spot.pgptemp_a T:Spot.pgptemp_plainmsg TO T:Spot.pgptemp_msg"
  205. 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"
  206. foo=DELETE("T:Spot.pgptemp_plainmsg")
  207. foo=DELETE("T:Spot.pgptemp_pgp")
  208. foo=DELETE("T:Spot.pgptemp_a")
  209. foo=DELETE("T:Spot.pgptemp_b")
  210. 'markmessage'
  211. 'getfrom'
  212. FromName=result
  213. 'getfromaddress'
  214. FromAddress=result
  215. 'getto'
  216. ToName=result
  217. 'gettoaddress'
  218. ToAddress=result
  219. IF Subject=="" THEN
  220. DO
  221.     'getsubject'
  222.     Subject=result
  223. END
  224. 'edit TO "'ToName'" TOADDR "'ToAddress'" FROM "'FromName'" FROMADDR "'FromAddress'" SUBJECT "'Subject'" FILE T:Spot.pgptemp_msg NOSIG NOEDIT NOGUI NOREQ'
  225. foo=DELETE("T:Spot.pgptemp_msg")
  226. 'markmessage CLEAR'
  227. RETURN
  228.  
  229. /* Ask the user his password and save it as an env variable.  */
  230. GetPassWord: PROCEDURE EXPOSE TextString PGPPath
  231. check=31
  232. DO UNTIL check~==31
  233.     'requeststring TITLE "PGP encryption script" PROMPT "Please give your PGP password." INVISIBLE CENTER'
  234.     IF rc==5 THEN RETURN 0
  235.     ADDRESS COMMAND 'Setenv PGPPASS "'result'"'
  236.     /* Let's decrypt the file to see if the passphrase was correct */
  237.     ADDRESS COMMAND PGPPath"PGP +batchmode T:Spot.pgptemp_pgp -o T:Spot.pgptemp_plainmsg"
  238.     check = rc
  239. END
  240. RETURN 1
  241.  
  242. GetMsgFlags: PROCEDURE EXPOSE MsgFlags
  243. MsgFlags=""
  244. 'getmessageflags'
  245. MsgFlags=REVERSE(OVERLAY(c2b(d2c(result)), '000000000000000000000000', 24-LENGTH(c2b(d2c(result)))+1))
  246. RETURN 0
  247.  
  248. /* A fatal error occured. We have to exit. */
  249. Error:
  250. 'requestnotify PROMPT "'TextString'" CENTER'
  251. foo=DELETE("T:Spot.pgptemp")
  252. foo=DELETE("T:Spot.tempusers")
  253. foo=DELETE("T:Spot.pgptemp_a")
  254. foo=DELETE("T:Spot.pgptemp_pgp")
  255. foo=DELETE("T:Spot.pgptemp_b")
  256. foo=DELETE("T:Spot.pgptemp_plainmsg")
  257. foo=DELETE("T:Spot.pgptemp_msg")
  258. foo=DELETE("T:Spot.pgptemp_plainmsg.temp"