home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1997 #5 / AmigaPlus_Extra-CD_5-97.iso / online-tools / mail / yamtools1.7 / unmime.rexx < prev    next >
OS/2 REXX Batch file  |  1997-06-17  |  10KB  |  245 lines

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