ó
Ú"wTc           @   s—   d  Z  d d l Z d d l Z d „  Z d „  Z d „  Z d „  Z d „  Z d e j f d	 „  ƒ  YZ d
 e j	 f d „  ƒ  YZ	 d „  Z
 e j e
 ƒ d S(   sö  
Modified utf-7 encoding as used in IMAP v4r1 for encoding mailbox names.
Code from here; couldn't find a license statement:
http://www.koders.com/python/fid744B4E448B1689C0963942A7928FA049084FAC86.aspx

From the RFC:

5.1.3.  Mailbox International Naming Convention

   By convention, international mailbox names are specified using a
   modified version of the UTF-7 encoding described in [UTF-7].  The
   purpose of these modifications is to correct the following problems
   with UTF-7:

      1) UTF-7 uses the "+" character for shifting; this conflicts with
         the common use of "+" in mailbox names, in particular USENET
         newsgroup names.

      2) UTF-7's encoding is BASE64 which uses the "/" character; this
         conflicts with the use of "/" as a popular hierarchy delimiter.

      3) UTF-7 prohibits the unencoded usage of ""; this conflicts with
         the use of "" as a popular hierarchy delimiter.

      4) UTF-7 prohibits the unencoded usage of "~"; this conflicts with
         the use of "~" in some servers as a home directory indicator.

      5) UTF-7 permits multiple alternate forms to represent the same
         string; in particular, printable US-ASCII chararacters can be
         represented in encoded form.

   In modified UTF-7, printable US-ASCII characters except for "&"
   represent themselves; that is, characters with octet values 0x20-0x25
   and 0x27-0x7e.  The character "&" (0x26) is represented by the two-
   octet sequence "&-".

   All other characters (octet values 0x00-0x1f, 0x7f-0xff, and all
   Unicode 16-bit octets) are represented in modified BASE64, with a
   further modification from [UTF-7] that "," is used instead of "/".
   Modified BASE64 MUST NOT be used to represent any printing US-ASCII
   character which can represent itself.

   "&" is used to shift to modified BASE64 and "-" to shift back to US-
   ASCII.  All names start in US-ASCII, and MUST end in US-ASCII (that
   is, a name that ends with a Unicode 16-bit octet MUST end with a "-
   ").
iÿÿÿÿNc         C   s1   |  j  d ƒ }  t j |  ƒ j d ƒ j d d ƒ S(   Ns   utf-16bes   
=t   /t   ,(   t   encodet   binasciit
   b2a_base64t   rstript   replace(   t   s(    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyt   modified_base647   s    c         C   s1   |  r- | j  d t d j |  ƒ ƒ ƒ |  2n  d  S(   Ns   &%s-t    (   t   appendR   t   join(   t   _int   r(    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyt   doB64;   s     c         C   sà   g  } g  } x¥ |  D] } t  | ƒ } d | k o< d k n s] d | k oX d k n rz t | | ƒ | j | ƒ q | d k r£ t | | ƒ | j d ƒ q | j | ƒ q Wt | | ƒ t d j | ƒ ƒ t |  ƒ f S(   Ni    i%   i'   i~   t   &s   &-R	   (   t   ordR   R
   t   strR   t   len(   R   R   R   t   ct   ordC(    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyt   encoder@   s    8c         C   s,   t  j |  j d d ƒ d ƒ } t | d ƒ S(   NR   R    s   ===s   utf-16be(   R   t
   a2b_base64R   t   unicode(   R   t   b(    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyt   modified_unbase64S   s    c         C   s  g  } g  } x± |  D]© } | d k r< | r< | j  d ƒ q | d k r™ | r™ t | ƒ d k rp | j  d ƒ n  | j  t d j | d ƒ ƒ ƒ g  } q | r¯ | j  | ƒ q | j  | ƒ q W| ré | j  t d j | d ƒ ƒ ƒ n  d j | ƒ } | t |  ƒ f S(   NR   t   -i   R	   (   R
   R   R   R   (   R   R   t   decodeR   t   bin_str(    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyt   decoderW   s"     	#t   StreamReaderc           B   s   e  Z d  d „ Z RS(   t   strictc         C   s
   t  | ƒ S(   N(   R   (   t   selfR   t   errors(    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyR   n   s    (   t   __name__t
   __module__R   (    (    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyR   m   s   t   StreamWriterc           B   s   e  Z d  d „ Z RS(   R   c         C   s
   t  | ƒ S(   N(   R   (   R    R   R!   (    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyR   s   s    (   R"   R#   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyR$   r   s   c         C   s    |  d k r t  t t t f Sd  S(   Ns   imap4-utf-7(   R   R   R   R$   (   t   name(    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyt   imap4_utf_7w   s    (   t   __doc__R   t   codecsR   R   R   R   R   R   R$   R&   t   register(    (    (    s9   /usr/lib/python2.7/dist-packages/getmailcore/imap_utf7.pyt   <module>0   s   						