mò
­fIc           @   sß  d  Z  d Z d Z d Z d k Z d d d d g Z e i d	 e i ƒ Z e i d
 e i ƒ Z	 e i d e i e i
 Bƒ Z e i d e i e i
 Bƒ Z e i d ƒ Z e i d e i ƒ Z e i d e i ƒ Z e i d e i ƒ Z d f  d „  ƒ  YZ d f  d „  ƒ  YZ d „  Z d „  Z d d „ Z d d „ Z e i d ƒ Z e i d ƒ Z d „  Z e i d ƒ Z d „  Z e i d e i ƒ Z e i d  e i ƒ Z e i d! ƒ Z d" „  Z d# „  Z  d k! Z! e" d$ j p* e# e! i$ ƒ d j oòe! i$ d d j oÞd k% Z% d% Z& d Z' e% i% e! i$ d& d' ƒ \ Z( Z) e# e) ƒ d. j o e& GHe! i* d& ƒ n d/ e( j d0 e( j j p d1 e( j o# d2 e( j o e& GHe! i* d& ƒ n x¨ e( D]  \ Z+ Z, e+ d) j o
 e  Z- qªe+ d+ j o
 e Z- qªe+ d- j oB y e. e, ƒ Z WqJe/ e0 f j
 o e& GHe! i* d& ƒ qJXqªe+ d, j o
 d& Z' qªqªWe# e) ƒ d j o e! i1 e! i2 f Z3 n? e# e) ƒ d& j o e) d e! i2 f Z3 n e) d e) d& f Z3 e' o e3 e' f Z3 n e- e3 Œ  n d S(3   s¾  Mimification and unmimification of mail messages.

Decode quoted-printable parts of a mail message or encode using
quoted-printable.

Usage:
        mimify(input, output)
        unmimify(input, output, decode_base64 = 0)
to encode and decode respectively.  Input and output may be the name
of a file or an open file object.  Only a readline() method is used
on the input file, only a write() method is used on the output file.
When using file names, the input and output file names may be the
same.

Interactive usage:
        mimify.py -e [infile [outfile]]
        mimify.py -d [infile [outfile]]
to encode and decode respectively.  Infile defaults to standard
input and outfile to standard output.
iÈ   s
   ISO-8859-1s   > Nt   mimifyt   unmimifyt   mime_encode_headert   mime_decode_headers.   ^content-transfer-encoding:\s*quoted-printables$   ^content-transfer-encoding:\s*base64s0   ^content-type:.*multipart/.*boundary="?([^;"
]*)s:   ^(content-type:.*charset=")(us-ascii|iso-8859-[0-9]+)(".*)s   ^-*
s   =([0-9a-f][0-9a-f])s   =\?iso-8859-1\?q\?([^? 	
]+)\?=s   ^subject:\s+re: t   Filec           B   s    t  Z d  Z d „  Z d „  Z RS(   s{   A simple fake file object that knows about limited read-ahead and
    boundaries.  The only supported method is readline().c         C   s   | |  _  | |  _ d  |  _ d  S(   N(   t   filet   selft   boundaryt   Nonet   peek(   R   R   R   (    (    t#   /mit/python/lib/python2.4/mimify.pyt   __init__/   s    		c         C   s’   |  i d  j	 o d Sn |  i i ƒ  } | p | Sn |  i oN | |  i d j o | |  _ d Sn | |  i d j o | |  _ d SqŽ n | S(   Nt    s   
s   --
(   R   R	   R   R   t   readlinet   lineR   (   R   R   (    (    R
   R   4   s    
		(   t   __name__t
   __module__t   __doc__R   R   (    (    (    R
   R   +   s    	t
   HeaderFilec           B   s   t  Z d „  Z d „  Z RS(   Nc         C   s   | |  _  d  |  _ d  S(   N(   R   R   R   R	   (   R   R   (    (    R
   R   D   s    	c         C   sÖ   |  i d  j	 o |  i } d  |  _ n |  i i ƒ  } | p | Sn t i | ƒ o | Sn xs |  i i ƒ  |  _ t |  i ƒ d j p( |  i d d j o |  i d d j o | Sn | |  i } d  |  _ q_ Wd  S(   Ni   i    t    s   	(	   R   R	   R   R   R   R   t   het   matcht   len(   R   R   (    (    R
   R   H   s    	  >(   R   R   R   R   (    (    (    R
   R   C   s   	c         C   s‰   d } d } xn t i |  | ƒ } | d j o Pn | |  | | i d ƒ !t t	 | i
 d ƒ d ƒ ƒ } | i d ƒ } q W| |  | S(   s6   Decode a single line of quoted-printable text to 8bit.R   i    i   i   N(   t   newlinet   post	   mime_codet   searchR   t   resR   t   startt   chrt   intt   groupt   end(   R   R   R   R   (    (    R
   t   mime_decodeZ   s       6c         C   sž   d } d } xƒ t i |  | ƒ } | d j o Pn | i d ƒ } d i	 | i
 d ƒ ƒ } | |  | | i d ƒ !t | ƒ } | i d ƒ } q W| |  | S(   s   Decode a header line to 8bit.R   i    i   R   t   _N(   R   R   t	   mime_headR   R   R   R   R   R   t   joint   splitR   R!   R    (   R   R   R   R   R   (    (    R
   R   g   s       $i    c         C   s~  d } d } d }	 d } |  i o |  i d  t j o
 t } n d } t	 |  ƒ } x | i ƒ  } | p d Sn | o1 | t | ƒ  | j o | t | ƒ } | } n d } t | ƒ } t i | ƒ o d } qU n | o t i | ƒ o d }	 qU n | i | | ƒ | o t i | ƒ o
 d } n t i | ƒ } | o d | i d ƒ } n t i | ƒ o PqU qU W| o | p | o
 d } n xã|  i ƒ  } | p d Sn t i t d | ƒ } | o1 | t | ƒ  | j o | t | ƒ } | } n d } x | o• | | d j o" | i | | ƒ d } d } Pn | | d	 j oL | i | | ƒ t |  | ƒ } t  | | | ƒ | i! } | p Pqqn PqW| o{ | ot xa | d
 d j oO | d
  } |  i ƒ  }
 |
 t t ƒ  t j o |
 t t ƒ }
 n | |
 } q¶Wt# | ƒ } n | o+ |	 o$ | o d k$ } | i% | ƒ } n | o | i | | ƒ q—q—Wd S(   s?   Convert a quoted-printable part of a MIME mail message to 8bit.i    i   R   i   Ns   --s   \1s   --
s   
iþÿÿÿs   =
(&   R   t	   multipartt   quoted_printablet	   is_base64t   is_replt   ifileR   t   QUOTEt   prefixR   t   hfileR   R   R   t   prefR   t   qpR   t   decode_base64t	   base64_ret   ofilet   writet   replt   mpt   mp_resR   R   t   ret   subR#   R   t   nifilet   unmimify_partR	   R   R!   t   base64t   decodestring(   R*   R2   R0   R)   R,   R9   R&   R6   R'   R(   R   R;   R.   R   R-   (    (    R
   R:   v   s–     
  



  
 	 
	c   	      C   sü   t  |  ƒ t  d ƒ j o~ t |  ƒ } t  | ƒ t  d ƒ j oU |  | j oH d k } | i i |  ƒ \ } } | i
 |  | i i | d | ƒ ƒ q n |  } t  | ƒ t  d ƒ j o t | d ƒ } n | } t | d ƒ } t | | | ƒ | i ƒ  d S(   s>   Convert quoted-printable parts of a MIME mail message to 8bit.R   Nt   ,t   w(   t   typet   infilet   openR*   t   outfilet   ost   pathR%   t   dt   ft   renameR$   R2   R   R   R9   R:   R0   t   flush(	   R@   RB   R0   R*   R9   RC   RF   R2   RE   (    (    R
   R   Ë   s     &	+s   [=-ÿ]s   [=?-ÿ]c         C   st  | o
 t } n t } d } d } t |  ƒ d j o1 |  d  d j o  d t d ƒ i	 ƒ  } d } n xo | i
 |  | ƒ } | d j o Pn | |  | | i d ƒ !d t | i d ƒ ƒ i	 ƒ  } | i d ƒ } qj W| |  | }  d } x| t |  ƒ d j oh d	 } x5 |  | d
 j p |  | d d
 j o | d } qW| d } | |  |  d } |  | }  qð W| |  S(   sZ   Code a single line as quoted-printable.
    If header is set, quote some extra characters.R   i    i   s   From s   =%02xt   Fi   iK   iI   t   =s   =
N(   t   headert   mime_header_chart   regt	   mime_charR   R   R   R   t   ordt   upperR   R   R   R   R   R    t   i(   R   RK   RQ   R   R   R   RM   (    (    R
   t   mime_encodeà   s8     
$
  7  &
s<   ([ 	(]|^)([-a-zA-Z0-9_+]*[-ÿ][-a-zA-Z0-9_+-ÿ]*)(?=[ 	)]|
)c         C   s—   d } d } x| t i |  | ƒ } | d j o Pn d | |  | | i d ƒ !| i d ƒ t	 t
 | i d ƒ d ƒ f } | i d ƒ } q W| |  | S(   s.   Code a single header line as quoted-printable.R   i    i   s   %s%s%s=?%s?Q?%s?=i   N(   R   R   t   mime_headerR   R   R   R   R   R   t   CHARSETRR   R    (   R   R   R   R   (    (    R
   R     s       Ds   ^mime-version:s   ^content-transfer-encoding:s   [-ÿ]c         C   s  d } } } d } d }
 } } g  } d } g  } d } t |  ƒ } xü | i ƒ  } | p Pn | o t i | ƒ o
 d } n t i | ƒ o
 d } n t i | ƒ o> d } t i | ƒ o
 d } që t i | ƒ o
 d } që n t i | ƒ }	 |	 o d |	 i d ƒ } n t i | ƒ o | } Pn | i | ƒ qI WxT|  i ƒ  } | p Pn | o< | | d j o | } Pn | | d j o | } Pq£n | o | i | ƒ qHn | ot xa | d d j oO | d  } |  i ƒ  } | t t  ƒ  t  j o | t t  ƒ } n | | } qÈWt! | ƒ } n | i | ƒ | p" t i | ƒ o d } }
 qon |
 p! t | ƒ t" j o
 d }
 q—qHqHWx| D]ÿ } | o t# | ƒ } n t$ i | ƒ } | oj | oI | i d	 ƒ i& ƒ  d
 j o) d | i d ƒ t' | i d ƒ f } q<q@d | i d d ƒ } n | oN t i | ƒ o> d } | o | d } q•|
 o | d } q•| d } n | i) | ƒ q£W| p |
 oO | oG | i) d ƒ | i) d ƒ | o | i) d t' ƒ q| i) d ƒ n |
 o | o | i) d ƒ n | i) | ƒ x5 | D]- } |
 o t* | d ƒ } n | i) | ƒ q7W| i) | ƒ | } x| o| | d j oN xK |  i ƒ  } | p d Sn |
 o t* | d ƒ } n | i) | ƒ q™Wn | | d j oH t+ |  | ƒ } t- | | d ƒ | i. } | p Pn | i) | ƒ q~n xG |  i ƒ  } | p d Sn |
 o t* | d ƒ } n | i) | ƒ q@Wq~Wd S(   s@   Convert an 8bit part of a MIME mail message to quoted-printable.i    R   i   s   --s   --
s   
iþÿÿÿs   =
i   s   us-asciis   %s%s%si   s   %sus-ascii%ss   Content-Transfer-Encoding: s   base64
s   quoted-printable
s   7bit
s   Mime-Version: 1.0
s   Content-Type: text/plain; s   charset="%s"
s   charset="us-ascii"
s,   Content-Transfer-Encoding: quoted-printable
N(/   t   has_ctet   is_qpR(   R   R&   t   must_quote_bodyt   must_quote_headert   has_iso_charsRK   t
   header_endt   messaget   message_endR   R*   R-   R   R   t   iso_charR   t   mvR   t   is_mimet   cteR/   R1   R5   R6   R   R   t   appendR   R   R+   R!   t   MAXLENR   t   chrsett
   chrset_rest   lowerRT   R2   R3   RR   R   R9   t   mimify_partR	   (   R*   R2   R_   R9   RU   RK   RY   R[   Rd   R6   RW   R(   R   RV   R   RX   RZ   R\   R-   R&   (    (    R
   Rf     sö       


  	 
 -    	  c         C   sü   t  |  ƒ t  d ƒ j o~ t |  ƒ } t  | ƒ t  d ƒ j oU |  | j oH d k } | i i |  ƒ \ } } | i
 |  | i i | d | ƒ ƒ q n |  } t  | ƒ t  d ƒ j o t | d ƒ } n | } t | d ƒ } t | | d ƒ | i ƒ  d S(   s>   Convert 8bit parts of a MIME mail message to quoted-printable.R   NR=   R>   i    (   R?   R@   RA   R*   RB   RC   RD   R%   RE   RF   RG   R$   R2   R   R   R9   Rf   RH   (   R@   RB   R*   R9   RE   RF   R2   RC   (    (    R
   R    ›  s     &	+t   __main__s/   Usage: mimify [-l len] -[ed] [infile [outfile]]i   s   l:edbi   s   -eR   s   -ds   -bs   -l(   i    i   i   (   s   -eR   (   s   -dR   (   s   -bR   (   s   -dR   (4   R   Rb   RT   R+   R7   t   __all__t   compilet   IR/   R1   t   SR5   Rc   R   R   R#   R4   R   R   R!   R   R:   R   RN   RL   RR   RS   R   R^   R`   R]   Rf   R    t   sysR   R   t   argvt   getoptt   usageR0   t   optst   argst   exitt   ot   at   encodeR   t
   ValueErrort   OverflowErrort   stdint   stdoutt   encode_args('   RT   R   R:   R#   RR   R!   R   Rs   Rh   Rb   R+   R   R7   RS   R   Ro   Rn   R   Rc   Rf   Rz   R1   Rq   RL   Rl   Ru   R4   R]   R   Rt   R/   RN   R`   R^   R5   R   R    R0   Rp   (    (    R
   t   ?   s~   			U	 		‡		7	3 

