home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / rxjis096.zip / mimesend.cmd < prev    next >
OS/2 REXX Batch file  |  1997-08-07  |  11KB  |  348 lines

  1. /* product name: RXJIS                                                */
  2. /* Version:      0.85                                                 */
  3. /* author:       YANO Takashi                                         */
  4. /* target:       OS/2 Warp J3.0+                                      */
  5. /* module name:  MIMESEND.CMD                                         */
  6. /* source name:  MIMESEND.CMD                                         */
  7. /* compiler:     N.A.                                                 */
  8. /* address:      tyano@ca2.so-net.or.jp or tyano@yamato.ibm.co.jp     */
  9. /* comment:      RXJIS is a utility functions for REXX.               */
  10. /*               It encodes and decodes JIS 7 bit strings and MIME    */
  11. /*               format strings.                                      */
  12. /*                                                                    */
  13. /*               MIMESEND.CMD is a sample program how to use          */
  14. /*               RXJIS. I am using it with PostRoadMailer 1.03a.      */
  15. /*               It converts a mail message with PC Kanji codes       */
  16. /*               into MIME or JIS7bit mail message.                   */
  17. /*                                                                    */
  18. /* how to use:   MIMESEND fn                                          */
  19. /*               fn is a mail message file. The converted result is   */
  20. /*               in fn. The original is lost.                         */
  21. /*                                                                    */
  22. /* history:      1996-2-5 initial release                             */
  23. /*               1996-2-14 0.10 fix small bugs                        */
  24. /*                         missing REXXUTIL loading.                  */
  25. /*                         if NOMIME is missing, novalue occurs.      */
  26. /*               1996-2-19 remove trace                               */
  27. /*               1996-10-20 Avoid to put duplicated headers.          */
  28. /*                                                                    */
  29. /*                                                                    */
  30. /*                                                                    */
  31. /*                                                                    */
  32. arg fn
  33. signal on novalue
  34. call rxfuncadd 'SysLoadFuncs', 'REXXUTIL', 'SysLoadFuncs' /* A@0.10 */
  35. call SysLoadFuncs                                         /* A@0.10 */
  36. call rxfuncdrop 'RxJisLoadFuncs'
  37. call rxfuncadd 'RxJisLoadFuncs', 'RXJIS', 'RxJisLoadFuncs'
  38. call RxJisLoadFuncs
  39. if stream(fn, 'c', 'query exists') = '' then exit
  40. call LoadHeader fn
  41. call LoadBody fn
  42. call stream fn, 'c', 'close'
  43. call CheckIfNomimeHost
  44. i = 1
  45. j = 1
  46. newheader.1 = 'MIME-Version: 1.0'
  47. do while i <= header.0
  48.    select
  49.       when translate(word(header.i, 1)) = 'SUBJECT:' then parse value processsubject(i, j) with i j
  50.       when translate(word(header.i, 1)) = 'REPLY-TO:' then parse value processreplyto(i, j) with i j
  51.       when translate(word(header.i, 1)) = 'FROM:' then parse value processaddress(i, j) with i j
  52.       when translate(word(header.i, 1)) = 'TO:' then parse value processaddress(i, j) with i j
  53.       when translate(word(header.i, 1)) = 'CC:' then parse value processaddress(i, j) with i j
  54.       when translate(word(header.i, 1)) = 'BCC:' then parse value processaddress(i, j) with i j
  55.       when wordpos(translate(word(header.i, 1)), 'MIME-VERSION: CONTENT-TYPE: CONTENT-TRANSFER-ENCODING:') > 0 then i = i + 1 /* A@0.85 */
  56.    otherwise
  57.       j = j + 1
  58.       newheader.j = header.i
  59.       i = i + 1
  60.    end  /* select */
  61. end /* do */
  62. if iso2022jp then do
  63.    j = j + 1
  64.    newheader.j = 'Content-Type: text/plain; charset="ISO-2022-JP"'
  65.    j = j + 1
  66.    newheader.j = 'Content-Transfer-Encoding: 7bit'
  67.    do i = 1 to body.0
  68.       if body.i <> '' then body.i = RxJisToJis(body.i)
  69.    end /* do */
  70. end  /* Do */
  71. else do
  72.    j = j + 1
  73.    newheader.j = 'Content-Type: text/plain; charset="US-ASCII"'
  74.    j = j + 1
  75.    newheader.j = 'Content-Transfer-Encoding: 7bit'
  76. end  /* Do */
  77. newheader.0 = j
  78. tfn = value('TMP', , 'OS2ENVIRONMENT')
  79. if tfn = '' then tfn = workingdir
  80. if right(tfn, 1) <> '\' then tfn = tfn || '\'
  81. tfn = SysTempFileName(tfn || '????')
  82. do i = 1 to newheader.0
  83.    call lineout tfn, newheader.i
  84. end /* do */
  85. call lineout tfn, ''
  86. do i = 1 to body.0
  87.    call lineout tfn, body.i
  88. end /* do */
  89. call stream tfn, 'C', 'CLOSE'
  90. '@COPY' tfn fn
  91. call SysFileDelete tfn
  92. exit
  93.  
  94. processreplyto: procedure expose header. newheader. nomimeisrequired
  95. arg i, j
  96. c = words(header.i)
  97. h = word(header.i, 1)
  98. a = word(header.i, c)
  99. j = j + 1
  100. if words(header.i) = 2 then newheader.j = h a
  101. else do
  102.    c = wordindex(header.i, words(header.i))
  103.    m = wordindex(header.i, 2)
  104.    n = strip(substr(header.i, m, c - m))
  105.    if dbvalidate(header.i) = 0 then do
  106.       if left(n, 1) <> '"' & n <> '' then n = '"' || n || '"'
  107.    end  /* Do */
  108.    else do
  109.       options 'EXMODE'
  110.       if dbwidth(n) <> length(n) then do
  111.          n = RxJisToJis(n)
  112.          if nomimeisrequired = 0 then n = '=?ISO-2022-JP?B?' || RxJisToBase64(n) || '?='
  113.       end  /* Do */
  114.       if n <> '' then n = '"' || n || '"'
  115.    end  /* Do */
  116.    newheader.j = h n a
  117. end  /* Do */
  118. return i + 1 j
  119.  
  120. CheckIfNomimeHost: procedure expose header. nomimeaddress. nomimeisrequired workingdir errorlog
  121. parse source . . a
  122. workingdir = filespec('D', a) || filespec('P', a)
  123. errorlog = workingdir || 'errorlog.log'
  124. f = workingdir || 'NOMIME'
  125. nomimeisrequired = 0 /* A@0.10 */
  126. if stream(f, 'c', 'query exists') = '' then return
  127. nomimeaddress = ''
  128. i = 0
  129. do while lines(f)
  130.    parse upper value linein(f) with a
  131.    a = strip(a)
  132.    if a = '' then iterate
  133.    if pos(left(a, 1), '#;*') > 0 then iterate
  134.    i = i + 1
  135.    nomimeaddress.i = word(a, 1)
  136. end /* do */
  137. call stream f, 'c', 'close'
  138. nomimeaddress.0 = i
  139. i = 1
  140. do while i <= header.0
  141.    select
  142.       when translate(word(header.i, 1)) = 'TO:' then i = checkdestaddress(i)
  143.       when translate(word(header.i, 1)) = 'CC:' then i = checkdestaddress(i)
  144.       when translate(word(header.i, 1)) = 'BCC:' then i = checkdestaddress(i)
  145.    otherwise
  146.       i = i + 1
  147.    end  /* select */
  148. end /* do */
  149. return
  150.  
  151. checkdestaddress: procedure expose header. nomimeaddress. nomimeisrequired
  152. arg i
  153. line = header.i
  154. i = i + 1
  155. do while i <= header.0
  156.    a = header.i
  157.    if left(a, 1) <> ' ' then leave
  158.    i = i + 1
  159.    line = line a
  160. end /* do */
  161. do j = 1 to nomimeaddress.0
  162.    n = pos(nomimeaddress.j, translate(line))
  163.    if n = 0 then iterate
  164.    nomimeisrequired = 1
  165.    leave
  166. end /* do */
  167. if nomimeisrequired = 0 then return i
  168. line = translate(subword(line, 2), ' ', '09'x)
  169. do while line <> ''
  170.    do j = 1 by 1
  171.       t = gettoken()
  172.       if t = '' | t = ',' then leave
  173.       t.j = t
  174.    end /* do */
  175.    j = j - 1
  176.    parse var t.j with '<' a '>'
  177.    if a = '' then a = t.j
  178.    do z = 1 to nomimeaddress.0
  179.       if translate(right(a, length(nomimeaddress.z))) = nomimeaddress.z then return i
  180.    end /* do */
  181. end /* do */
  182. nomimeisrequired = 0
  183. return i
  184.  
  185. gettoken: procedure expose line
  186. parse value strip(line) with t line
  187. if left(t, 1) = ',' then do
  188.    parse var t 2 a
  189.    line = a line
  190.    return t
  191. end  /* Do */
  192. zdsl = '"<'
  193. zdsr = '">'  
  194. i = pos(left(t, 1), zdsl)
  195. if i = 0 then do
  196.    i = pos(',', t)
  197.    if i = 0 then return t
  198.    if i > 1 then do
  199.       line = substr(t, i) line
  200.       t = left(t, i - 1)
  201.    end  /* Do */
  202.    else do
  203.       t = left(t, 1)
  204.       line = substr(t, 2) line
  205.    end  /* Do */
  206.    return t
  207. end  /* Do */
  208. zd = substr(zdsr, i, 1)
  209. i = 2
  210. line = t line
  211. t = ''
  212. do while line <> ''
  213.    i = pos(zd, line, i)
  214.    if i = 0 then do
  215.       t = t || line
  216.       line = ''
  217.       leave
  218.    end  /* Do */
  219.    if i = 1 then do
  220.       parse var line a 2 line
  221.       t = t || a
  222.       leave
  223.    end  /* Do */
  224.    if substr(line, i - 1, 1) <> '\' then do
  225.       a = left(line, i)
  226.       line = substr(line, i + 1)
  227.       t = t || a
  228.       if pos(left(line, 1), ' ,') > 0 then leave
  229.       t = t || gettoken()
  230.       leave
  231.    end  /* Do */
  232.    i = i + 1
  233. end /* do */
  234. return t
  235.  
  236. LoadHeader: procedure expose header.
  237. arg fn
  238. do i = 1 by 1 while lines(fn)
  239.    header.i = linein(fn)
  240.    if header.i = '' then do
  241.       header.0 = i - 1
  242.       leave
  243.    end  /* Do */
  244. end /* do */
  245. return
  246.  
  247. LoadBody: procedure expose body. iso2022jp
  248. arg fn
  249. options 'EXMODE'
  250. iso2022jp = 0
  251. do i = 1 by 1 while lines(fn)
  252.    body.i = linein(fn)
  253.    if dbvalidate(body.i) = 0 then iterate
  254.    if dbwidth(body.i) = length(body.i) then iterate
  255.    iso2022jp = 1
  256. end /* do */
  257. body.0 = i - 1
  258. return
  259.  
  260. processaddress: procedure expose nomimeisrequired header. newheader.
  261. arg i, j
  262. zi = i
  263. line = header.i
  264. i = i + 1
  265. do while i <= header.0
  266.    a = header.i
  267.    if left(a, 1) <> ' ' then leave
  268.    i = i + 1
  269.    line = line a
  270. end /* do */
  271. if dbvalidate(line) = 0 then do
  272.    do m = zi to i - 1
  273.       j = j + 1
  274.       newheader.j = header.m
  275.    end /* do */
  276.    return i j
  277. end  /* Do */
  278. options 'EXMODE'
  279. zh = word(line, 1)
  280. line = translate(subword(line, 2), ' ', '09'x)
  281. do m = 1 by 1 while line <> ''
  282.    do z = 1 by 1
  283.       t = gettoken()
  284.       if t = '' then leave
  285.       if t = ',' & z > 2 then leave
  286.       if t = ',' & z = 2 then do
  287.          if pos('@', t.1) > 0 then leave
  288.       end  /* Do */
  289.       t.z = t
  290.    end /* do */
  291.    t.0 = z - 1
  292.    n = ''
  293.    do z = 1 to t.0 - 1
  294.       if z = 2 & t.2 = ',' then n = n || t.z
  295.       else n = n t.z
  296.       say 1 t.z
  297.    end /* do */
  298.    n = strip(n)
  299.    if left(n, 1) <> '"' & n <> '' then n = '"' || n || '"'
  300.    a = t.z t
  301.    if dbwidth(n) <> length(n) then do
  302.       n = RxJisToJis(n)
  303.       if nomimeisrequired = 0 then n = '=?ISO-2022-JP?B?' || RxJisToBase64(n) || '?='
  304.    end  /* Do */
  305.    j = j + 1
  306.    if m = 1 then newheader.j = zh n a
  307.    else newheader.j = copies(' ', length(zh)) n a
  308. end /* do */
  309. return i j
  310.  
  311. processsubject: procedure expose nomimeisrequired header. newheader.
  312. parse arg i, j
  313. options 'EXMODE'
  314. do n = 1 by 1
  315.    if i > header.0 then leave
  316.    if n > 1 & left(header.i, 1) <> '' then leave
  317.    if dbvalidate(header.i) = 0 then do
  318.       j = j + 1
  319.       newheader.j = header.i
  320.       i = i + 1
  321.       iterate
  322.    end  /* Do */
  323.    if dbwidth(header.i) = length(header.i) then do
  324.       j = j + 1
  325.       newheader.j = header.i
  326.       i = i + 1
  327.       iterate
  328.    end  /* Do */
  329.    if n = 1 then parse var header.i h b
  330.    else do
  331.       h = ' '
  332.       b = header.i
  333.    end  /* Do */
  334.    b = RxJisToJis(strip(b))
  335.    j = j + 1
  336.    if nomimeisrequired then newheader.j = h b
  337.    else newheader.j = h '=?ISO-2022-JP?B?' || RxJisToBase64(b) || '?='
  338.    i = i + 1
  339. end /* do */
  340. return i j
  341.  
  342. novalue:
  343. call lineout errorlog, date('S') time() condition('C') '@' sigl
  344. call lineout errorlog, date('S') time() condition('D')
  345. call lineout errorlog, date('S') time() condition('I')
  346. call stream errorlog, 'c', 'close'
  347. exit
  348.