home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / internet / yamtools20 / yamtools20english / unmime.rexx < prev    next >
OS/2 REXX Batch file  |  1997-08-08  |  11KB  |  248 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*                           UnMime.rexx                                      */
  4. /*                 Copyright ©1997 by Dick Whiting                            */
  5. /*                                                                            */
  6. /*----------------------------------------------------------------------------*/
  7. /*        This version works with either MuiRexx 2.2 or MuiRexx 3.0           */
  8. /*----------------------------------------------------------------------------*/
  9. /* This one attempts to identify the Quoted-Printable portions of a mail      */
  10. /* and converts it back to 8bit text. It also handles header lines with       */
  11. /* the ISO-8859-1?Q type encoding (only single lines are done correctly).     */
  12. /*----------------------------------------------------------------------------*/
  13. /*                                                                            */
  14. /* Standard Disclaimer: I wrote it, it works for me, I don't guarantee        */
  15. /* that it will do anything productive for anyone else, etc. etc. ;-)         */
  16. /*                                                                            */
  17. /*HOWEVER, if you DO find a use for it: I homeschool my kids and they         */
  18. /*would love a postcard from where EVER you live.                             */
  19. /*                                                                            */
  20. /*Instant GEOGRAPHY lesson;)                                                  */
  21. /*                                                                            */
  22. /*                                                                            */
  23. /*POSTCARDS:    Dick Whiting                                                  */
  24. /*              28590 S. Beavercreek Rd.                                      */
  25. /*              Mulino, Oregon 97042                                          */
  26. /*              USA                                                           */
  27. /*                                                                            */
  28. /*----------------------------------------------------------------------------*/
  29. /*                                                                            */
  30. /*               Address Bug Reports or Comments to:                          */
  31. /*                Dick Whiting <dwhiting@europa.com>                          */
  32. /*                          15 June 1997                                      */
  33. /*                                                                            */
  34. /******************************************************************************/
  35. /*
  36. $VER: 1.1 Copyright ©1997 by Dick Whiting
  37. $AUTHOR: Dick Whiting
  38. $DESCRIPTION: Change Quoted-Printable back to text and rewrap.
  39. */
  40.  
  41. options results
  42. options failat 21
  43.  
  44. if ~show('L','rexxsupport.library') then do
  45.    addlib('rexxsupport.library',0,-30)
  46. end
  47.  
  48. Address YAM 'getmailinfo file'              /* find which mail to unmime  */
  49. mfile=result
  50.  
  51. TRUE=1
  52. FALSE=0
  53. choplen=76                                  /* chop lines to this length  */
  54.  
  55. ofile=substr(mfile,lastpos('/',mfile)+1)    /* create a tempfile name     */
  56. bakup='T:'||ofile||'.bak'
  57. ofile='T:'||ofile||'.unmime'
  58.  
  59. Call ReadMail
  60. Call SaveFile
  61.  
  62. exit
  63.  
  64. /**************************************************************************/
  65. /*                        Read the mail file into array                   */
  66. /**************************************************************************/
  67.  
  68. /**************************************************************************/
  69. /*       From: =?iso-8859-1?Q?Ren=E9?= LeBlanc <leblancr@amug.org>        */
  70. /**************************************************************************/
  71.  
  72. ReadMail:     
  73.  
  74.    header=TRUE                            /* start with header lines      */
  75.    qlines.=''                             /* array for quoted lines       */
  76.    mimecnt=0                              /* no mime lines yet            */
  77.    mimeline=''                            /* init empty                   */
  78.    mime=FALSE                             /* NOT quoted-printable         */
  79.    quotehead='CONTENT-TRANSFER-ENCODING: QUOTED-PRINTABLE'
  80.    xferhead='CONTENT-TRANSFER-ENCODING:'  /* other xfer types             */
  81.    CTE8bit='Content-Transfer-Encoding: 8bit'
  82.    isomime='=?ISO-8859-1?Q?'              /* mimed sections of a line     */
  83.    isoend='?='
  84.  
  85.    if open('IN',mfile,'R') & open('OUT',ofile,'W') then do
  86.       do while ~eof('IN')
  87.          linein=readln('IN')
  88.          select
  89.             when upper(linein)=quotehead then do
  90.                linein=CTE8bit             /* replace CTE line with 8bit   */
  91.                mime=TRUE
  92.             end
  93.             when pos(xferhead,upper(linein))=1 then mime=FALSE
  94.             when pos(isomime,upper(linein))>0 then do
  95.                testword=word(linein,1)
  96.                if substr(testword,length(testword),1)=':' then do
  97.                   Call DoQencode
  98.                end
  99.             end
  100.             when header & linein='' then header=FALSE
  101.             otherwise nop
  102.          end
  103.          select
  104.             when ~mime & mimeline='' then do
  105.                foo=writeln('OUT',linein)        /* write lines to output  */
  106.             end
  107.             when ~mime & mimeline~='' then do
  108.                Call ProcessMime
  109.                foo=writeln('OUT',linein)        /* write lines to output  */
  110.             end
  111.             when mime then do
  112.                select
  113.                   when linein='' then do
  114.                      if ~eof('IN') then do 
  115.                         Call ProcessMime
  116.                      end 
  117.                   end
  118.                   when substr(linein,length(linein),1)='=' then do
  119.                      mimeline=mimeline||substr(linein,1,length(linein)-1)
  120.                   end
  121.                   otherwise do
  122.                      mimeline=mimeline||linein
  123.                      Call ProcessMime
  124.                   end
  125.                end
  126.             end
  127.             otherwise do
  128.                say 'Found otherwise in ReadMail..quitting'
  129.                exit
  130.             end
  131.          end
  132.       end
  133.       if mimeline~='' then do
  134.          Call ProcessMime
  135.       end
  136.       foo=close('IN')
  137.       foo=close('OUT')
  138.    end
  139.    else do
  140.       errmsg='Not a valid file selected'
  141.       Call ErrorMsg
  142.       exit     
  143.    end
  144.    
  145. Return
  146.  
  147. /**************************************************************************/
  148. /*                   Handle special characters in line                    */
  149. /**************************************************************************/
  150. ProcessMime:
  151.  
  152.    eqpos=pos('=',mimeline)
  153.  
  154.    do while eqpos>0
  155.       testchar=substr(mimeline,eqpos+1,2)
  156.       if datatype(testchar,'X') then do
  157.          newchar=x2c(testchar)
  158.          mimeline=delstr(mimeline,eqpos,3)
  159.          mimeline=insert(newchar,mimeline,eqpos-1,1)     
  160.       end 
  161.       eqpos=pos('=',mimeline,eqpos+1)
  162.    end
  163.  
  164.    if header then do
  165.       foo=writeln('OUT',mimeline)              /* write lines to output  */
  166.    end
  167.    else do
  168.       do while length(mimeline)>choplen /* chop lines to reasonable len  */
  169.          blankpos=lastpos(' ',substr(mimeline,1,choplen))
  170.          if blankpos~=0 then do
  171.             lineout=substr(mimeline,1,blankpos)
  172.             foo=writeln('OUT',lineout)            /* write piece to output  */
  173.             mimeline=substr(mimeline,blankpos+1)  /* remaining portion      */
  174.          end
  175.          else leave
  176.       end
  177.       foo=writeln('OUT',mimeline)           /* write last piece to out*/
  178.    end 
  179.  
  180.    mimeline=''                                 /* reset to null line     */
  181.  
  182. Return
  183.  
  184. /**************************************************************************/
  185. /*                   Handle special characters in headers                 */
  186. /**************************************************************************/
  187. DoQencode:
  188.  
  189.    qword=''
  190.    qwstart=pos(isomime,upper(linein))
  191.    do while qwstart>0
  192.       qword=substr(linein,qwstart+length(isomime))
  193.       qwend=pos(isoend,qword)
  194.       qword=substr(qword,1,qwend-1)
  195.       qlen=length(isomime)+length(qword)+length(isoend)
  196.       eqpos=pos('=',qword)
  197.       linein=delstr(linein,qwstart,qlen)
  198.       do while eqpos>0
  199.          testchar=substr(qword,eqpos+1,2)
  200.          if datatype(testchar,'X') then do
  201.             newchar=x2c(testchar)
  202.             qword=delstr(qword,eqpos,3)
  203.             qword=insert(newchar,qword,eqpos-1,1)     
  204.          end 
  205.          eqpos=pos('=',qword,eqpos+1)
  206.       end
  207.       qword=translate(qword,' ','_')
  208.       linein=insert(qword,linein,qwstart-1,length(qword))
  209.       qwstart=pos(isomime,upper(linein))
  210.    end
  211. Return
  212.  
  213. /**************************************************************************/
  214. /*                        Save the mail file back                         */
  215. /**************************************************************************/
  216. SaveFile:     
  217.  
  218.    fstate=statef(mfile)                           /* file information     */
  219.    fbytes=subword(fstate,2,1)                     /* length of file       */
  220.    fdate=subword(fstate,5,1)                      /* internal date        */
  221.    fmins=subword(fstate,6,1)                      /* minutes since midnite*/
  222.    fticks=subword(fstate,7,1)                     /* ticks past minutes   */
  223.    fcomm=subword(fstate,8)                        /* old comment          */
  224.  
  225.    fdate=date('E',fdate,'I')                      /* get date in ddmmyy   */
  226.    fdate=translate(fdate,'-','/')                 /* convert / to -       */
  227.    hh=fmins%60                                    /* get hours            */
  228.    hh=right(hh,2,'0')                             /* force to 2 digits    */
  229.    mm=fmins//60                                   /* get minutes          */
  230.    mm=right(mm,2,'0')                             /* force to 2 digits    */
  231.    ss=fticks%50                                   /* seconds              */
  232.    ss=right(ss,2,'0')                             /* force it             */
  233.    ftime=hh||':'||mm||':'||ss                     /* timestamp rebuilt    */
  234.  
  235. /* Address Command 'COPY 'mfile bakup 'QUIET' */
  236.  
  237.    Address Command 'COPY 'ofile mfile 'QUIET'
  238.    Address Command 'SETDATE '  mfile fdate ftime
  239.    Address Command 'FILENOTE ' mfile '"'fcomm'"'
  240.    Address Command 'DELETE ' ofile 'QUIET' 
  241.    
  242.  
  243. Return
  244.  
  245. /**************************************************************************/
  246. /*                           End of Active Code                           */
  247. /**************************************************************************/
  248.