home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / bin / rcvfilt.cmd < prev    next >
OS/2 REXX Batch file  |  1996-10-13  |  4KB  |  201 lines

  1. /* REXX: rcvfilt.cmd */
  2.  
  3. /* Author:  Kai-Uwe Rommel <rommel@ars.de>
  4.  * Created: Fri Aug 23 1996
  5.  *
  6.  * inbound mail filter for Elm with sendmail transport
  7.  *
  8.  * $Id: rcvfilt.cmd,v 1.5 1996/10/13 10:19:53 rommel Exp rommel $
  9.  * $Revision: 1.5 $
  10.  */
  11.  
  12. /*
  13.  * $Log: rcvfilt.cmd,v $
  14.  * Revision 1.5  1996/10/13 10:19:53  rommel
  15.  * added comments
  16.  *
  17.  * Revision 1.4  1996/09/03 12:41:38  rommel
  18.  * add support for multi-part messages
  19.  *
  20.  * Revision 1.3  1996/09/03 07:20:00  rommel
  21.  * fix quotes for charset= in Content-Type: lines
  22.  *
  23.  * Revision 1.2  1996/08/30 08:56:46  rommel
  24.  * earlier exit if nothing to do
  25.  *
  26.  * Revision 1.1  1996/08/28 20:52:58  rommel
  27.  * Initial revision
  28.  *
  29.  * 
  30.  */
  31.  
  32. /* Latin-1 to cp850 recoding only for german umlauts at the moment */
  33.  
  34. cp850  = 'äöüÄÖÜß'
  35. latin1 = 'e4f6fcc4d6dcdf'X
  36.  
  37. Parse Arg file receiver
  38.  
  39. file = Translate(file, '\', '/')
  40. path = FileSpec('d', file) || FileSpec('p', file)
  41. name = FileSpec('n', file)
  42. temp = path'flt'SubStr(name, 4)
  43.  
  44. header = 1
  45. multi  = ''
  46. quoted = 0
  47. is8859 = 0
  48.  
  49. Do Forever
  50.   
  51.   line = LineIn(file)
  52.   
  53.   If Stream(file, 'S') = 'NOTREADY'
  54.   Then Leave
  55.   
  56.   If header = 1
  57.   Then Do
  58.     
  59.     If line = ''
  60.     Then Do
  61.       
  62.       header = 0
  63.       
  64.       /* header is done, now body follows */
  65.       
  66.       If quoted = 0 & is8859 = 0 & multi = ''
  67.       Then Leave /* nothing to do */
  68.     
  69.     End
  70.     Else Do
  71.       
  72.       line = Translate(line, ' ', '09'X)
  73.       
  74.       Parse Upper Var line key val1 val2
  75.       
  76.       /* check MIME encoding */
  77.       
  78.       If key = 'CONTENT-TRANSFER-ENCODING:' & val1 = 'QUOTED-PRINTABLE'
  79.       Then Do
  80.     quoted = 1
  81.     Iterate
  82.       End
  83.       
  84.       /* check MIME character set */
  85.       
  86.       If key = 'CONTENT-TYPE:' & val1 = 'TEXT/PLAIN;'
  87.       Then Do
  88.     
  89.     Parse Var val2 'CHARSET=' val2
  90.     
  91.     If Left(val2, 1) = """"
  92.     Then Parse Var val2 """" val2 """"
  93.     
  94.     If val2 = 'ISO-8859-1'
  95.     Then Do
  96.       is8859 = 1
  97.       line = 'Content-Type: text/plain; charset="ibm850"'
  98.     End
  99.  
  100.       End
  101.       
  102.       /* check for MIME multipart messages */
  103.       
  104.       If key = 'CONTENT-TYPE:' & val1 = 'MULTIPART/MIXED;'
  105.       Then Do
  106.     
  107.     Parse Var val2 'BOUNDARY="' multi '"'
  108.     
  109.       End
  110.       
  111.     End
  112.     
  113.   End
  114.   Else Do
  115.     
  116.     /* check for boundary of MIME multipart messages */
  117.     
  118.     If multi \= '' & Right(line, Length(multi)) = multi
  119.     Then Do
  120.       
  121.       /* this body is done, next header follows */
  122.       
  123.       header = 1
  124.       quoted = 0
  125.       is8859 = 0
  126.       
  127.     End
  128.     
  129.     /* undo MIME encoding */
  130.     
  131.     If quoted = 1
  132.     Then Do
  133.       
  134.       /* check for MIME line continuation marks */
  135.       
  136.       Do While Right(line, 1) = '='
  137.     line = Left(line, Length(line) - 1) || LineIn(file)
  138.       End
  139.       
  140.       /* decode all characters */
  141.   
  142.       Do Forever
  143.     
  144.     x = Pos('=', line)
  145.     
  146.     If x = 0
  147.     Then Leave
  148.     
  149.     repl = SubStr(line, x + 1, 2)
  150.     
  151.     /* in case even line breaks are MIME encoded, the CR's are
  152.        discarded (if present) and only the LF's are decoded */
  153.     
  154.     Select
  155.       When repl = '0D' Then repl = ''
  156.       When repl = '3D' Then repl = '00'X
  157.       Otherwise repl = X2C(repl)
  158.     End
  159.     
  160.     line = Left(line, x - 1) || repl || SubStr(line, x + 3)
  161.     
  162.       End
  163.       
  164.       /* any = characters got a special treatment above (because the
  165.      = is the quoting character) that needs to be handled now */
  166.   
  167.       line = Translate(line, '=', '00'X)
  168.     
  169.     End
  170.     
  171.     /* convert to IBM 850 character set */
  172.     
  173.     If is8859 = 1
  174.     Then Do
  175.       
  176.       line = Translate(line, cp850, latin1)
  177.       
  178.     End
  179.     
  180.   End
  181.  
  182.   Call LineOut temp,line
  183.     
  184. End
  185.  
  186. Call Stream file, 'C', 'CLOSE'
  187. Call Stream temp, 'C', 'CLOSE'
  188.  
  189. /* if there was nothing really to do, forget everything */
  190.  
  191. If quoted = 0 & is8859 = 0 & multi = ''
  192. Then Do
  193.   '@del 'temp
  194. End
  195. Else Do
  196.   '@del 'file
  197.   '@ren 'temp' 'name
  198. End
  199.   
  200. /* end of rcvfilt.cmd */
  201.