home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / modem / ozpgp.zip / OZPGPD.CMD next >
OS/2 REXX Batch file  |  1993-10-13  |  5KB  |  139 lines

  1. extproc perl -x
  2. #!perl
  3. # Since these perl scripts are FREE, they are provided AS IS. I MAKE NO
  4. # WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING WITHOUT LIMITATION,
  5. # ANY WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  6. #
  7. # If you find any bugs, please mail me a detailed description so I can fix it.
  8. # Better yet, fix it yourself and mail the fix to me. :-)
  9. # Any comment is welcome.
  10. #
  11. # Eric Veldhuyzen 
  12. #
  13. #   CIS ID: [100010,3051]
  14. # Internet: v912182@si.hhs.nl
  15.  
  16. $cismail = $ARGV[$#ARGV];
  17. if ("$cismail" eq  "") {
  18.   die "Argument required!\n";
  19. }
  20.  
  21. open (mail, "$cismail") || die "Can't open $cismail: $!.\n";
  22. open (out, ">decoded.txt") || die "Can't open decoded.txt: $!.\n"; 
  23.  
  24. while (<mail>) {
  25.   if (/^-----BEGIN PGP.*MESSAGE.*/) {
  26.     if (open (tmp, ">PGPTEMP")) { 
  27.       do {
  28.         print tmp $_;
  29.         $_ = <mail>;
  30.       } until (/^-----END.*/);
  31.       print tmp $_;
  32.       close tmp;                # File must be closed   or we lose
  33.                                 # some information.
  34.       system "pgp +batchmode=on +force=on PGPTEMP -o PGPTEMP.OUT >output";
  35.       if (open (tmp, "output")) {
  36.         print out "-----BEGIN DECODING INFORMATION-----\n";
  37.         while (<tmp>) {
  38.           s/^plaintext.*\n//i;  # needless information
  39.           s/^For .*\n//;
  40.           s/\cG//g;             # strip the ^G. I don't need a beep
  41.                                 # in a message.
  42.           print out $_;
  43.         }
  44.         close tmp;
  45.         print out "-----END DECODING INFORMATION-----\n";
  46.       } else {
  47.         print out "\nCan't open the output of pgp for some reason.\n\n";
  48.       }
  49.       if (open (tmp, "PGPTEMP.OUT")) {
  50.         print out "-----BEGIN DECODED PGP MESSAGE-----\n";
  51.         while (<tmp>) {
  52.           print out $_;
  53.         }
  54.         print out "-----END DECODED PGP MESSAGE-----\n";
  55.         close tmp;
  56.       } else {
  57.         print out "\nAn error ocurred.\n";
  58.         print out "This message is probably not encrypted with your public key.\n";
  59.                                 # Add an extra space to prevent retrying
  60.                                 # to decrypt next time.
  61.         print out "Original message follows:\n\n ";  
  62.         if (open (tmp, "PGPTEMP")) {
  63.           while (<tmp>) {
  64.             print out $_;
  65.           }
  66.           close tmp;
  67.         } else {
  68.           print out "Can't open original message for some reason.\n";
  69.           print out "Message lost. Sorry.\n";
  70.         }
  71.       }
  72.       if (-e "PGPTEMP") {       # remove temporary files.
  73.         unlink ("PGPTEMP");
  74.       }
  75.       if (-e "PGPTEMP.OUT") {
  76.         unlink ("PGPTEMP.OUT");
  77.       }
  78.       if (-e "output") {
  79.         unlink ("output");
  80.       }
  81.     } else {                    # Maybe this error should be changed to
  82.                                 # something non fatal.
  83.       die "Can't open PGPTEMP: $!.\n";
  84.     }
  85.   } elsif (/^-----BEGIN PGP PUBLIC KEY.*/) {
  86.     if (open (tmp, ">PGPTEMP")) {
  87.       do {
  88.         print tmp $_;
  89.         $_ = <mail>;
  90.       } until (/^-----END.*/);
  91.       print tmp $_;
  92.       close tmp;                # File must be closed or we lose
  93.                                 # some information.
  94.       system "pgp +batchmode=on +force=on -ka PGPTEMP >output";
  95.       if (open (tmp, "output")) {
  96.         print out "-----BEGIN KEY ADDING INFORMATION-----\n";
  97.         $added = 1;
  98.         while (<tmp>) {
  99.           if (/^No new  keys or signatures in keyfile\.$/i) {
  100.             $added = 0;
  101.           }           
  102.           s/\cG//g;             # Strip the ^G. I don't need a beep
  103.                                 # in a message.
  104.           print out $_;
  105.         }
  106.         close tmp;
  107.         print out "-----END KEY ADDING INFORMATION-----\n";
  108.         if ($added) {
  109.           system "pgp -kv >list";# A new key was added. Generate a new list file.
  110.         }
  111.         if (-e  "PGPTEMP") {    # remove temporary files.
  112.           unlink ("PGPTEMP");
  113.         }
  114.         if (-e "output") {
  115.           unlink ("output");
  116.         }
  117.       } else {
  118.         print out "\nCan't open the output of pgp for some reason.\n\n";
  119.       }
  120.     } else {                    # Maybe this error should be changed to
  121.                                 # something non fatal.
  122.       die "Can't open PGPTEMP: $!.\n";
  123.     }
  124.   } else {
  125.     print out $_;
  126.   }
  127. }
  128. close mail;
  129. close out;
  130. $bakfile = $cismail;            # Make a backupfile...
  131. while (chop($bakfile) ne "\."){}
  132. $bakfile = "$bakfile\.bak";
  133. if (-e "$bakfile") {
  134.   unlink ("$bakfile");
  135. }
  136. rename ("$cismail" , "$bakfile");
  137. rename ("decoded.txt", "$cismail");
  138. __END__
  139.