home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / sconv02.zip / SageConv.cmd next >
OS/2 REXX Batch file  |  1996-03-23  |  5KB  |  153 lines

  1. /* SageR's mail conversion utility - last modified on 3/23/96 */
  2. /* converts from ultimail to mr/2 format */
  3. /* functions on a file by file or directory by directory basis */
  4.  
  5. /* note: only converts text mime parts and ignores other mime types */
  6. /* remember: SysTempFileName() can't have more than 5 wildcards */
  7.  
  8. debug = 0
  9. logfile = "scerror.log"
  10. outfile = "tempfile.byl"
  11.  
  12. rc = rxfuncadd('SysLoadFuncs','RexxUtil','SysLoadFuncs')
  13.   if (rc <> 1) then do
  14.     say "ERROR: Problem loading rexx utility function..."
  15.     say "Quitting..."
  16.     EXIT
  17.   end
  18.   else do
  19.     call sysloadfuncs
  20.   end
  21.  
  22. parse arg arg.1 arg.2
  23.  
  24.   if (arg.1 = "") then do
  25.     say "Error: I need to know your userid in order to determine"
  26.     say "      whether a piece of mail is incoming our outgoing."
  27.     say "Usage: SageConvert <username> [filename]"
  28.     EXIT
  29.   end
  30.   else do
  31.     username = translate(arg.1)
  32.     say "Converting on the basis of username:" username
  33.   end
  34.  
  35.   if (arg.2 = "") then do     /* convert all files in directory */
  36.     rc = SysFileTree('*.ENV' , 'dirlist', 'F')
  37.     say dirlist.0 "files found."
  38.  
  39.     do M = 1 to dirlist.0    /* recursion */
  40.       parse var dirlist.M . . . . pathname
  41.       target = filespec("name", pathname)
  42.       call sparse target
  43.     end
  44.   end
  45.   else do             /* convert one file */
  46.     target = arg.2
  47.     call sparse target
  48.   end
  49.  
  50. EXIT
  51.  
  52. sparse:
  53.   parse arg mailfile
  54.  
  55.   say "Converting" mailfile"."
  56.   inheader = 1
  57.   inpart = 0
  58.   istext = 0
  59.   ext = "RCV"
  60.  
  61.   do while (lines(mailfile) > 0)
  62.     oneline = strip(linein(mailfile))
  63.  
  64.     if (inheader) then do
  65.       parse var oneline . 'boundary="'openbound'"' .
  66.       parse var oneline word.1 word.0
  67.  
  68.       if (openbound <> '') then do
  69.         if (debug) then say "Part Boundary Determined:" openbound
  70.         openbound = "--" || openbound
  71.         closebound = openbound || "--"
  72.         inheader = 0
  73.       end
  74.  
  75.       if (inheader = 0) then NOP
  76.       else if (translate(word.1) = "MIME-VERSION:") then NOP
  77.       else if (translate(word.1) = "CONTENT-TYPE:") then NOP
  78.       else if (translate(word.1) = "FROM:") then do
  79.         if (lastpos(translate(username), translate(word.0)) <> 0) then do
  80.           ext = "OUT"
  81.         end
  82.         rc = lineout(outfile, oneline)
  83.       end
  84.       else if (translate(word.1) = "TO:") then do
  85.         if (lastpos(translate(username), translate(word.0)) <> 0) then do
  86.           ext = "RCV"
  87.         end
  88.         rc = lineout(outfile, oneline)
  89.       end
  90.       else do
  91.         rc = lineout(outfile, oneline)
  92.       end
  93.     end
  94.  
  95.     else if (inpart) then do
  96.       if (oneline = closebound) then do    /* closing encounter */
  97.         if (debug) then say "Part Closed:" oneline
  98.         inpart = 0
  99.       end
  100.  
  101.       else do            /* parsing within a mime definition */
  102.         parse var oneline word.1 word.2 word.0
  103.         if (translate(word.1) == "CONTENT-TYPE:") then do
  104.           if (translate(word.2) == "TEXT/PLAIN;") then do
  105.             parse var word.0 ."filename="mimetext
  106.             if (debug) then say "Mime text part" mimetext "inserted."
  107.             call readmime mimetext outfile 
  108.           end
  109.           else do        /* store non-text mime lines */
  110.             say "Skipping non-text mime:" oneline
  111.             say "Placeholder inserted and filename written to logfile."
  112.             rc = lineout(logfile, "Error parsing mime part in:" mailfile)
  113.             rc = lineout(logfile, "        "oneline)
  114.             rc = lineout(outfile, "Error parsing following mime part.")
  115.             rc = lineout(outfile, "        "oneline)
  116.             ext = "ERR"
  117.           end
  118.         end
  119.       end
  120.     end
  121.  
  122.     else do                /* parse NON-MIME body lines */
  123.       if (oneline = openbound) then do        /* part boundary */
  124.         if (debug) then say "Part Start Detected:" oneline
  125.         inpart = 1
  126.       end
  127.       else if (oneline = "> THIS IS A MESSAGE IN 'MIME' FORMAT.  Your mail reader does not support MIME.") then NOP
  128.       else if (oneline = "> You may not be able to read some parts of this message.") then NOP
  129.       else do                /* line that will be put */
  130.         rc = lineout(outfile, oneline)
  131.       end
  132.     end
  133.   end
  134.  
  135.   rc = lineout(mailfile)
  136.   rc = lineout(outfile)
  137.   /* if you comment out the next two lines it will consolidate your mail */
  138.   template = "SC?????."ext 
  139.   randname = SysTempFileName(template)
  140.   'rename' outfile randname
  141.   say ''
  142. return
  143.  
  144. readmime:    /* reads a mime textfile "thisfile" into "thatfile" */
  145.   parse arg thisfile thatfile
  146.  
  147.   do while (lines(thisfile) > 0)
  148.     rc = lineout(thatfile, linein(thisfile))
  149.   end
  150.   rc = lineout(thisfile)
  151. return
  152.  
  153.