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

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