%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python3/dist-packages/twisted/mail/__pycache__/
Upload File :
Create Path :
Current File : //usr/lib/python3/dist-packages/twisted/mail/__pycache__/imap4.cpython-312.pyc

�

Ϫ�f�9�	���dZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
mZddlmZddlmZddlmZmZmZddlmZddlmZdd	lmZmZdd
lmZm Z m!Z!ddl"m#Z#ddl$m%Z%m&Z&m'Z'm(Z(m)Z)dd
l*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:ddl;m<Z=m>Z>m?Z@mAZBmCZDmEZFmGZHmIZJmKZLmMZNmOZOmPZQddlRmSZSmTZTddlUmVZVmWZWddlXmYZYmZZZm[Z[m\Z\m]Z]e^e_e`dd�dj����Zbd�Zcd�ZdGd�d�ZeGd�d�ZfGd�d�ZgGd�d�ZhGd�d �Zid!Zjekee`d"�e`d#d$���Zld%ejzelzZmemj�d&�Zoej�d'eozd(z�Zqekd)�e`d$�D��ZreeF�Gd*�d+eSj�eTj���Zue j��ZweeF�Gd,�d-eSj�eTj���Zxdwd.�Zyd/Zzd0Z{e|Z}dxd1�Z~d2�Zd3�Z�dwd4�Z�d5�Z�d6�Z�d7�Z�dyd8�Z�d9�Z�d:ekd;ekfd<�Z�Gd=�d>�Z�d?Z�d@�Z�dA�Z�dB�Z�dC�Z�dD�Z�ee=�GdE�dF��Z�eeO�GdG�dHe���Z�dIdJdKdLdMdN�Z�dO�Z�dP�Z�dQ�Z�dR�Z�dS�Z�dT�Z�dU�Z�GdV�dW�Z�GdX�dYe��Z�GdZ�d[e��Z�Gd\�d]e��Z�Gd^�d_e��Z�dzd`�Z�da�Z�db�Z�dc�Z�Gdd�de�Z�Gdf�dg�Z�Gdh�di�Z�dj�Z�e�e�dkdl��Z�dm�Z�dn�Z�dwdo�Z�dwdp�Z�Gdq�dre�jZ�Z�Gds�dte�j\�Z�e�j^e�e�e�e��Z�du�Z�e�jde��gdv�Z�y){a�
An IMAP4 protocol implementation

@author: Jp Calderone

To do::
  Suspend idle timeout while server is processing
  Use an async message parser instead of buffering in memory
  Figure out a way to not queue multi-message client requests (Flow? A simple callback?)
  Clarify some API docs (Query, etc)
  Make APPEND recognize (again) non-existent mailboxes before accepting the literal
�N)�decodebytes�encodebytes)�BytesIO)�chain)�Any�List�cast)�implementer)�credentials)�UnauthorizedLogin�UnhandledCredentials)�defer�error�
interfaces)�
maybeDeferred)�CramMD5ClientAuthenticator�LOGINAuthenticator�LOGINCredentials�PLAINAuthenticator�PLAINCredentials)�IllegalClientResponse�IllegalIdentifierError�IllegalMailboxEncoding�IllegalOperation�IllegalQueryError�IllegalServerResponse�IMAP4Exception�MailboxCollision�MailboxException�MismatchedNesting�MismatchedQuoting�NegativeResponse�
NoSuchMailbox�NoSupportedAuthentication�ReadOnlyMailbox�UnhandledResponse)�IAccountIMAP�IClientAuthentication�ICloseableMailboxIMAP�IMailboxIMAP�IMailboxIMAPInfo�IMailboxIMAPListener�IMessageIMAP�IMessageIMAPCopier�IMessageIMAPFile�IMessageIMAPPart�INamespacePresenter�ISearchableIMAPMailbox)�basic�policies)�log�text)�_get_async_param�_matchingString�	iterbytes�nativeString�
networkString��
z/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Decc��||ur|S|S)z�
    Swap C{this} with C{that} if C{this} is C{ifIs}.

    @param this: The object that may be replaced.

    @param that: The object that may replace C{this}.

    @param ifIs: An object whose identity will be compared to
        C{this}.
    �)�this�that�ifIss   �4/usr/lib/python3/dist-packages/twisted/mail/imap4.py�_swaprD`s���4�<�4�)�T�)�c
�h�|D��cgc]\}}t|||�t|||�f��!c}}Scc}}w)a�
    Swap each element in each pair in C{of} with C{that} it is
    C{ifIs}.

    @param of: A list of 2-L{tuple}s, whose members may be the object
        C{that}
    @type of: L{list} of 2-L{tuple}s

    @param ifIs: An object whose identity will be compared to members
        of each pair in C{of}

    @return: A L{list} of 2-L{tuple}s with all occurences of C{ifIs}
        replaced with C{that}
    )rD)�ofrArB�first�seconds     rC�
_swapAllPairsrJns>�� TV��BO�%���u�d�D�	!�5���t�#<�=����s�$.c���eZdZUdZgZeeed<ed�Z	eefd�Z
ed��Zejd��Zefd�Zd�Zd	�Zd
�Zd�Zd�Zd
�Zd�Zd�Zdefd�Zdefd�Zdedefd�Zy)�
MessageSeta


    A set of message identifiers usable by both L{IMAP4Client} and
    L{IMAP4Server} via L{IMailboxIMAP.store} and
    L{IMailboxIMAP.fetch}.

    These identifiers can be either message sequence numbers or unique
    identifiers.  See Section 2.3.1, "Message Numbers", RFC 3501.

    This represents the C{sequence-set} described in Section 9,
    "Formal Syntax" of RFC 3501:

        - A L{MessageSet} can describe a single identifier, e.g.
          C{MessageSet(1)}

        - A L{MessageSet} can describe C{*} via L{None}, e.g.
          C{MessageSet(None)}

        - A L{MessageSet} can describe a range of identifiers, e.g.
          C{MessageSet(1, 2)}.  The range is inclusive and unordered
          (see C{seq-range} in RFC 3501, Section 9), so that
          C{Message(2, 1)} is equivalent to C{MessageSet(1, 2)}, and
          both describe messages 1 and 2.  Ranges can include C{*} by
          specifying L{None}, e.g. C{MessageSet(None, 1)}.  In all
          cases ranges are normalized so that the smallest identifier
          comes first, and L{None} always comes last; C{Message(2, 1)}
          becomes C{MessageSet(1, 2)} and C{MessageSet(None, 1)}
          becomes C{MessageSet(1, None)}

        - A L{MessageSet} can describe a sequence of single
          identifiers and ranges, constructed by addition.
          C{MessageSet(1) + MessageSet(5, 10)} refers the message
          identified by C{1} and the messages identified by C{5}
          through C{10}.

    B{NB: The meaning of * varies, but it always represents the
    largest number in use}.

    B{For servers}: Your L{IMailboxIMAP} provider must set
    L{MessageSet.last} to the highest-valued identifier (unique or
    message sequence) before iterating over it.

    B{For clients}: C{*} consumes ranges smaller than it, e.g.
    C{MessageSet(1, 100) + MessageSet(50, None)} is equivalent to
    C{1:*}.

    @type getnext: Function taking L{int} returning L{int}
    @ivar getnext: A function that returns the next message number,
        used when iterating through the L{MessageSet}.  By default, a
        function returning the next integer is supplied, but as this
        can be rather inefficient for sparse UID iterations, it is
        recommended to supply one when messages are requested by UID.
        The argument is provided as a hint to the implementation and
        may be ignored if it makes sense to do so (eg, if an iterator
        is being used that maintains its own state, it is guaranteed
        that it will not be called out-of-order).
    �_empty�infc���|j|_g|_d�|_||juryt	|t
�r|dd|_|j
�y|j||�y)z�
        Create a new MessageSet()

        @type start: Optional L{int}
        @param start: Start of range, or only message number

        @type end: Optional L{int}
        @param end: End of range.
        c��|dzS�Nr<r?��xs rC�<lambda>z%MessageSet.__init__.<locals>.<lambda>�s
���Q��rEN)rM�_last�ranges�getnext�
isinstance�list�clean�add��self�start�ends   rC�__init__zMessageSet.__init__�sZ���[�[��
����&����D�K�K����e�T�"���(�D�K��J�J�L��H�H�U�C� rEc��|jS)z{
        The largest number in use.
        This is undefined until it has been set by assigning to this property.
        )rU�r]s rC�lastzMessageSet.last�s���z�z�rEc��|j|jurtd��||_t|j�D]*\}\}}|�|}|�|}||kDr||}}||f|j|<�,|j�y)z�
        Replaces all occurrences of "*".  This should be the
        largest number in use.  Must be set before attempting to
        use the MessageSet as a container.

        @raises ValueError: if a largest value has already been set.
        zlast already setN)rUrM�
ValueError�	enumeraterVrZ)r]�value�i�low�highs     rCrczMessageSet.last�s����:�:�T�[�[�(��/�0�0���
�'����4�	)�N�A�{��T��{����|����T�z� �#�T��!�4�[�D�K�K��N�	)�	
�
�
�rEc�R�||jur|}|j|jur|�|j}|�|j}t||gt	j
t|jd����\}}|jj||f�|j�y)z�
        Add another range

        @type start: L{int}
        @param start: Start of range, or only message number

        @type end: Optional L{int}
        @param end: End of range.
        N�rArB)�key)rMrUrc�sorted�	functools�partialrD�	_infinityrV�appendrZr\s   rCr[zMessageSet.add�s����$�+�+���C��:�:�T�[�[�(��}��	�	���{��i�i���
�C�L�i�/�/��D�N�N�QU�V�
�
��s�	
�����E�3�<�(��
�
�rEc�L�t|t�r$|j|jz}t|�St|j�}|j|jur|j|_	|j
|�|S#t$r|j|�Y|SwxYw�N)rXrLrVrcrMr[�	TypeError)r]�otherrV�ress    rC�__add__zMessageSet.__add__s����e�Z�(��[�[�5�<�<�/�F��f�%�%��T�[�[�)�C��y�y����+��9�9���
��������J���
�������J�
�s�4B�B#�"B#c��t|t�r7|jj|j�|j	�|S	|j
|�|S#t$r|j|�Y|SwxYw)z�
        Extend our messages with another message or set of messages.

        @param other: The messages to include.
        @type other: L{MessageSet}, L{tuple} of two L{int}s, or a
            single L{int}
        )rXrLrV�extendrZr[ru�r]rvs  rCrzzMessageSet.extendso���e�Z�(��K�K���u�|�|�,��J�J�L���
 �����%� �����
 ��������
 �s�	A�A8�7A8c�Z�tt|j|jd���}t	d�t	d�fg}|D]D\}}|d\}}||dz
kr|j||f��*t
||�t||�f|d<�Ft|ddd|j��|_y)z>
        Clean ranges list, combining adjacent ranges
        Nrlz-inf���r<)rnrJrVrq�floatrr�min�max)r]rV�mergedRangesrirj�previousLow�previousHighs       rCrZzMessageSet.clean0s���
�
�d�k�k����T�R�S���v���f�
�6�7���	P�I�C��(4�R�(8�%�K���c�A�g�%��#�#�S�$�K�0�� #�K�� 5�s�<��7N�O�L���	P�$�L���$4�4�d�n�n�U��rEc�*�|jddduS)a�
        Is there a L{None} in our ranges?

        L{MessageSet.clean} merges overlapping or consecutive ranges.
        None is represents a value larger than any number.  There are
        thus two cases:

            1. C{(x, *) + (y, z)} such that C{x} is smaller than C{y}

            2. C{(z, *) + (x, y)} such that C{z} is larger than C{y}

        (Other cases, such as C{y < x < z}, can be split into these
        two cases; for example C{(y - 1, y)} + C{(x, x) + (z, z + 1)})

        In case 1, C{* > y} and C{* > z}, so C{(x, *) + (y, z) = (x,
        *)}

        In case 2, C{z > x and z > y}, so the intervals do not merge,
        and the ranges are sorted as C{[(x, y), (z, *)]}.  C{*} is
        represented as C{(*, *)}, so this is the same as 2.  but with
        a C{z} that is greater than everything.

        The result is that there is a maximum of two L{None}s, and one
        of them has to be the high element in the last tuple in
        C{self.ranges}.  That means checking if C{self.ranges[-1][-1]}
        is L{None} suffices to check if I{any} element is L{None}.

        @return: L{True} if L{None} is in some range in ranges and
            L{False} if otherwise.
        r}N)rVrbs rC�
_noneInRangeszMessageSet._noneInRangesDs��>�{�{�2��r�"�d�*�*rEc��|j�rtd��|jD]\}}||cxkr|ks�y�y)z�
        May raise TypeError if we encounter an open-ended range

        @param value: Is this in our ranges?
        @type value: L{int}
        z.Can't determine membership; last value not setTF)r�rurV)r]rgrirjs    rC�__contains__zMessageSet.__contains__esM�������L�M�M����	�I�C���e�#�t�#��$�	�rEc#�K�|jD]:\}}|j|dz
�}||ks� |��|j|�}||kr��<y�wrQ)rVrW)r]�l�hs   rC�	_iteratorzMessageSet._iteratorvsQ�����K�K�	$�D�A�q����Q��U�#�A��q�&����L�L��O���q�&�	$�s�,A
�A
�
A
c�X�|j�rtd��|j�S)Nz!Can't iterate; last value not set)r�rur�rbs rC�__iter__zMessageSet.__iter__}s'�������?�@�@��~�~��rEc�r�d}|jD]%\}}|�|dz
}�|�td��|||z
dzz
}�'|S)Nrr<z%Can't size object; last value not set)rVru)r]rwr�r�s    rC�__len__zMessageSet.__len__�sV�����K�K�	#�D�A�q��y��q������ G�H�H���A���{�"��
	#��
rE�returnc��g}|jD]g\}}||k(r/|�|jd��|jt|���:|�|jd|fz��R|jd||fz��idj|�S)N�*z%d:*z%d:%d�,)rVrr�str�join)r]�prirjs    rC�__str__zMessageSet.__str__�s��������		0�I�C���d�{��;��H�H�S�M��H�H�S��X�&�������3�&��)�����C��;�.�/�		0��x�x��{�rEc� �dt|��d�S)Nz<MessageSet �>)r�rbs rC�__repr__zMessageSet.__repr__�s���c�$�i�[��*�*rErvc�|�t|t�r'tt|j|jk(�St
Srt)rXrLr	�boolrV�NotImplementedr{s  rC�__eq__zMessageSet.__eq__�s,���e�Z�(���d�k�k�U�\�\�9�:�:��rEN)�__name__�
__module__�__qualname__�__doc__rMrr�__annotations__r~rqr`�propertyrc�setterr[rxrzrZr�r�r�r�r�r�r�r��objectr�r�r?rErCrLrL�s���7�r�F�D��I���e��I�#��!�0����
�[�[����,$��2��&V�(+�B�"$� �
����+�#�+��F��t�rErLc��eZdZd�Zd�Zd�Zy)�
LiteralStringc�.�||_g|_||_yrt)�size�datar�r]r��defereds   rCr`zLiteralString.__init__�s����	���	���
rEc�2�|xjt|�zc_d}|jdkDr|jj|�|S|jr|d|j||jd}}nd}|r|jj|�|S�NrrE)r��lenr�rr�r]r��passons   rC�writezLiteralString.write�s����	�	�S��Y��	����9�9�q�=��I�I���T�"��
��y�y�#�K�d�i�i�0�$�t�y�y�{�2C�f������	�	� � ��&��
rEc�p�|jjdj|j�|f�y)�:
        Call deferred with data and rest of line
        rEN)r�callbackr�r��r]�lines  rCr�zLiteralString.callback�s(��	
�
�
���S�X�X�d�i�i�0�$�7�8rEN)r�r�r�r`r�r�r?rErCr�r��s���

�9rEr�c�"�eZdZdZd�Zd�Zd�Zy)�LiteralFile��c��||_||_||jkDrtj�|_yt
�|_yrt)r�r�_memoryFileLimit�tempfile�
TemporaryFiler�rr�s   rCr`zLiteralFile.__init__�s7����	���
��$�'�'�'� �.�.�0�D�I��	�D�IrEc�2�|xjt|�zc_d}|jdkDr|jj|�|S|jr|d|j||jd}}nd}|r|jj|�|Sr�)r�r�r�r�r�s   rCr�zLiteralFile.write�s����	�	�S��Y��	����9�9�q�=��I�I�O�O�D�!��
�
�y�y�#�K�d�i�i�0�$�t�y�y�{�2C�f������	�	����%��
rEc��|jjdd�|jj|j|f�y)r�rN)r��seekrr�r�s  rCr�zLiteralFile.callback�s1��	
�	�	���q�!���
�
���T�Y�Y��-�.rEN)r�r�r�r�r`r�r�r?rErCr�r��s��'��"��/rEr�c�$�eZdZdZdd�Zd�Zd�Zy)�WriteBufferzU
    Buffer up a bunch of writes before sending them all to a transport at once.
    c�<�||_||_d|_g|_y�Nr)�
bufferSize�	transport�_length�_writes)r]r�r�s   rCr`zWriteBuffer.__init__�s�����"��������rEc���|xjt|�z
c_|jj|�|j|jkDr|j�yyrt)r�r�r�rrr��flush�r]�ss  rCr�zWriteBuffer.write�sD������A���������A���<�<�$�/�/�)��J�J�L�*rEc��|jr4|jj|j�g|_d|_yyr�)r�r��
writeSequencer�rbs rCr�zWriteBuffer.flush�s2���<�<��N�N�(�(����6��D�L��D�L�rEN)i )r�r�r�r�r`r�r�r?rErCr�r��s�����rEr�c�B�eZdZdZdZdZdZ			d
d�Zdefd�Z	d�Z
d	�Zy)�Command)�
CAPABILITY�FLAGS�LIST�LSUB�STATUS�SEARCH�	NAMESPACE)�EXISTS�EXPUNGE�FETCH�RECENT)�UIDVALIDITY�UNSEEN�
READ-WRITE�	READ-ONLY�UIDNEXT�PERMANENTFLAGSNc�Z����||_||_||_���fd�|_g|_y)Nc����|g���i���Srtr?)rS�contArgs�contKw�continuations ���rCrTz"Command.__init__.<locals>.<lambda>s���l�1�&J�x�&J�6�&J�rE)�command�args�wantResponser��lines)r]r�r�r�r�r�r�s    ```rCr`zCommand.__init__s*��������	�(���J�����
rEr�c��dj|j|j|j|j|j
�S)Nz(<imap4.Command {!r} {!r} {!r} {!r} {!r}>)�formatr�r�r�r�r�rbs rCr�zCommand.__repr__s8��9�@�@��L�L�$�)�)�T�%6�%6��8I�8I�4�:�:�
�	
rEc��|j�dj||jf�Sdj||j|jf�S�N� )r�r�r��r]�tags  rCr�zCommand.format$sB���9�9���9�9�c�4�<�<�0�1�1��y�y�#�t�|�|�T�Y�Y�7�8�8rEc���g}g}|jD]�}t|�}t|�}|dk\r|d|jvsJ|dk\r|d|jvs4|dk\rA|ddk(r9t|dt�r&|dd|jvr|j|���|j|���|jdc}|_	|j||f�|r	||�yy)Nr<r��OK)r��parseNestedParensr��_1_RESPONSES�_2_RESPONSESrXrY�
_OK_RESPONSESrrrr�)	r]�lastLine�unusedCallback�send�unuse�L�names�N�ds	         rC�finishzCommand.finish)s����������	$�A�%�a�(�E��E�
�A��Q���!�H�� 1� 1�1���6��!�H�� 1� 1�1���6��!�H��%��u�Q�x��.��!�H�Q�K�4�#5�#5�5����E�"����U�#�	$� �
�
�D�
��4�:�	�
�
�D�(�#�$���5�!�rE)Nr?N)r�r�r�r�r�r�rr`r�r�r�rr?rErCr�r��sE���L�@�L��M�
�E�
���
�
�#�
�
9�
"rEr�r��!��s	]\\(){%*"�charmap�[�]c#�2K�|]}|tvs�|���y�wrt)�
_nonAtomChars)�.0�chs  rC�	<genexpr>rRs����H�"��-�0G�2�H�s�
�c���eZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
dZdZdZhd�Zd	Zd
Zd�d�Zd�Zd
�Zd�Zd�Zd�ZdZd�Zd�Zd�Zd�Zd�d�Zd�Z d�Z!d�Z"d�Z#d�Z$d�Z%d�Z&d�d�Z'e(jRde(jTe+�zdz�Z,d �Z-d!�Z.d"�Z/d#�Z0d$�Z1d%�Z2d&�Z3d'�Z4d(�Z5d)�Z6d*�Z7d+�Z8d�d,�Z9d�d-�Z:d�d.�Z;d�d/�Z<d�d0�Z=d1�Z>d2�Z?d3�Z@e@fZAeAZBeAZCeAZDd4�ZEeEfZFeFZGeFZHeFZId5�ZJeJfZKeKZLeKZMeKZNd6�ZOeOe-fZPd7�ZQd8�ZRd9�ZSd:�ZTd;�ZUd<�ZVd=�ZWeWfZXd>�ZYeYe'e&fZZd?�Z[d@�Z\dA�Z]dB�Z^e^fZ_e_Z`dC�ZadD�ZbdE�Zceae'dFdGfZdedZeeae'dHdIfZfefZgdJ�ZhdK�ZiehfZjejZkdL�Zlele&fZmemZndM�Zoeoe&fZpepZqdN�Zrere'e&fZsesZtdO�Zueue&fZvevZwdP�Zxexe&fZyeyZzdQ�Z{dR�Z|dS�Z}e{e'e'dHdTfZ~e~Ze{e'e'dFdUfZ�e�Z�dV�Z�dW�Z�dX�Z�e�e'e.fZ�e�Z�dY�Z�dZ�Z�d[�Z�d\�Z�d]�Z�e�e'e5e6e/fZ�e�Z�d^�Z�d_�Z�d`�Z�e�fZ�da�Z�db�Z�dc�Z�dd�Z�e�fZ�de�Z�df�Z�dg�Z�e�fZ�dh�Z�di�Z�d�dj�Z�e�e7e0fZ�dk�Z�d�dl�Z�dm�Z�dn�Z�do�Z�dp�Z�dq�Z�dr�Z�ds�Z�dt�Z�du�Z�dv�Z�dw�Z�dx�Z�dy�Z�dz�Z�d{�Z�d|�Z�d}�Z�d~�Z�d�Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�d�d��Z�e�e1e2fZ�d��Z�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d�d��Z�d��Z�d��Z�d�d��Z�e�e1e-e3fZ�d��Z�d��Z�d�d��Z�e�e1e&fZ�d��Z�d��Z�d��Z�d��Z�d��Z�d��Z�e�e-e4fZ�d��Z�d��Z�d��Z�y)��IMAP4Serverz�
    Protocol implementation for an IMAP4rev1 server.

    The server can be in any of four states:
        - Non-authenticated
        - Authenticated
        - Selected
        - Logout
    sTwisted IMAP4rev1 Ready�<iFNi>�OR�NOT�UID�unauthr�c�T�|�i}||_||_|�t}||_g|_yrt)�challengers�ctx�iterateInReactor�
_scheduler�_queuedAsync)r]�chal�contextFactory�	schedulers    rCr`zIMAP4Server.__init__�s5���<��D����!�����(�I�#�����rEc��dt|jj��i}|jrB|jr6|j
s*t
j|jd��
d|d<d|d<d|d<d|d<|S)N�AUTH�
LOGINDISABLED�STARTTLSr�sIDLE)	rYr�keysr�canStartTLS�
startedTLSr�
ISSLTransportr�)r]�caps  rC�capabilitieszIMAP4Server.capabilities�sz����T�-�-�2�2�4�5�6���8�8��(�(��O�O��,�,�T�^�^�T�B�J�(,��$�%�#'��K� � ��L����G���
rEc��i|_tj|jd�du|_|j|j�|j�yrt)�tagsr�
ITLSTransportr�r$�
setTimeout�timeOut�sendServerGreetingrbs rC�connectionMadezIMAP4Server.connectionMade�sD����	�%�3�3�D�N�N�D�I�QU�U���������%����!rEc�n�|jd�|jr|j�d|_yyrt)r,�	_onLogout)r]�reasons  rC�connectionLostzIMAP4Server.connectionLost�s+��������>�>��N�N��!�D�N�rEc�X�|jd�|jj�|jrl|jj	|�t|jd�}|�2t
|j�jtj�d|_d|_y)Ns** BYE Autologout; connection idle too long�timeout)�sendLiner��loseConnection�mbox�removeListener�ICloseableMailboxr�close�
addErrbackr5�err�state)r]�cmbxs  rC�timeoutConnectionzIMAP4Server.timeoutConnection�sw���
�
�C�D����%�%�'��9�9��I�I�$�$�T�*�$�T�Y�Y��5�D����d�j�j�)�4�4�S�W�W�=��D�I���
rEc��|j�|jj|�}|�|j|�yyrt)�resetTimeout�_pendingLiteralr��setLineModer�s   rC�rawDataReceivedzIMAP4Server.rawDataReceived�s=�������%�%�+�+�D�1�������V�$�rEc���|j}d|_|r;|j�/|j|jd��|r
|j��/|j�|jj|�yyr�)�blocked�lineReceived�poprz)r]�commandss  rC�_unblockzIMAP4Server._unblock�sc���<�<������4�<�<�/����h�l�l�1�o�.��4�<�<�/��<�<�#��L�L����)�$rEc	�X�|j�|jj|�y|j�t|d|jz�}	||�y#t
$rD}|j
dtt|��z�tj�Yd}~yd}~wwxYw)N�parse_sBAD Server error: )rGrrrB�getattr�
parseState�	Exception�sendUntaggedResponser;r�r5r=)r]r��f�es    rCrHzIMAP4Server.lineReceived�s����<�<�#��L�L����%�������D�(�T�_�_�4�5��	�
�d�G���	��%�%�&;�m�C�PQ�F�>S�&S�T��G�G�I�I��	�s�A�	B)�%:B$�$B)c
��|jdd�}d}t|�dk(r|\}}}nMt|�dk(r|\}}n9t|�dk(r|d}|j|d�y|jdd�y|j�}	|j	|||�S#t
$r1}|j|dt
t|��z�Yd}~yd}~wt$r1}|j|dt
t|��z�Yd}~yd}~wt$r1}|j|d	t
t|��z�Yd}~yd}~wwxYw)
Nr��r<rsMissing commandsNull command�Illegal syntax: �Illegal operation: �Illegal mailbox name: )�splitr��sendBadResponse�upper�dispatchCommandrr;r�r�sendNegativeResponser)r]r�r��restr��cmdrSs       rC�
parse_commandzIMAP4Server.parse_command�s6���z�z�$��"�����t�9��>�!�N�C��d�
��Y�!�^��H�C��
��Y�!�^��q�'�C�� � ��&8�9��� � ���7���i�i�k��	��'�'��S�$�7�7��$�	S�� � ��&9�M�#�a�&�<Q�&Q�R�R���	��%�%��+�m�C��F�.C�C�
�
��&�	��%�%��.��s�1�v�1F�F�
�
��	�s0�B�	E�$'C�E�'D�E�'E�Ec�Z�|j}d|_d|_|j|�y)Nr�)rCrOr�)r]r�rs   rC�
parse_pendingzIMAP4Server.parse_pending�s(��� � ��#���#���	�
�
�4�rEc��|j|�}|r#|d}|dd}|j||||g|||�y|j|d�y)Nrr<sUnsupported command)�
lookupCommand�_IMAP4Server__doCommandrZ)r]r�r_r^�uidrR�fn�	parseargss        rCr\zIMAP4Server.dispatchCommandsV�����s�#����1��B��!�"��I����S�"�t�S�k�9�d�C�H�� � ��&<�=rEc
��t|dj|jt|j	��f�d�S)N�_)rNr�r>r:r[)r]r_s  rCrdzIMAP4Server.lookupCommand	s/���t�S�X�X�t�z�z�<��	�	��3L�&M�N�PT�U�UrEc	�R�t|�D]p\}}t|�rO||dzd}t|||�j|j|||||�j|j|�y|j|��r|rtdt|�z��|�	||d|i�y||�y)Nr<z Too many arguments for command: rf)
rf�callabler�addCallback�_IMAP4Server__cbDispatchr<�_IMAP4Server__ebDispatchrrr�repr)	r]r��handlerr�rhr�rfrh�args	         rC�__doCommandzIMAP4Server.__doCommands����	�*�	!�F�A�s���}�%�a�!�e�g�.�	��c�4��.�:�:��%�%�s�G�T�9�c���*�T�.�.��4�����C� �	!��'�(J�T�RV�Z�(W�X�X��?��d�#��#��T�NrEc�\�|\}}|j|�|j||||||�yrt)rrre)	r]�resultr�rgr�rhrfrrr^s	         rC�__cbDispatchzIMAP4Server.__cbDispatch s/�����d����C������b�$�	�4��=rEc	�:�|jt�r2|j|dtt	|j
��z�y|jt�r2|j|dtt	|j
��z�y|jt�r2|j|dtt	|j
��z�y|j|dtt	|j
��z�tj|�y)NrVrWrX�Server error: )�checkrrZr;r�rgrr]rr5r=�r]�failurer�s   rC�__ebDispatchzIMAP4Server.__ebDispatch%s����=�=�.�/�� � ��(�=��W�]�]�9K�+L�L�
��]�]�+�
,��%�%��+�m�C��
�
�<N�.O�O�
��]�]�1�
2��%�%��.��s�7�=�=�?Q�1R�R�
�
� � ��&��s�7�=�=�7I�)J�J�
�
�G�G�G�rEc��||jkDrtd|jfz��tj�}d|_t||�|_|jtd|z��|j�|S)Nz,Literal too long! I accept at most %d octets�pendingzReady for %d octets of text)
�_literalStringLimitrr�DeferredrOr�rC�sendContinuationRequestr;�
setRawMode�r]r�rs   rC�_stringLiteralzIMAP4Server._stringLiteral8s����$�*�*�*�'�>��+�+�-�.��
�
�N�N���#���,�T�1�5����$�$��7�$�>�?�	
�	
�����rEc��tj�}d|_t||�|_|jt
d|z��|j�|S)Nr~zReady for %d octets of data)rr�rOr�rCr�r;r�r�s   rC�_fileLiteralzIMAP4Server._fileLiteralGsQ���N�N���#���*�4��3����$�$��7�$�>�?�	
�	
�����rEc�(�|j|d��S)a
        Parse an astring from line that represents a command's final
        argument.  This special case exists to enable parsing empty
        string literals.

        @param line: A line that contains a string literal.
        @type line: L{bytes}

        @return: A 2-tuple containing the parsed argument and any
            trailing data, or a L{Deferred} that fires with that
            2-tuple
        @rtype: L{tuple} of (L{bytes}, L{bytes}) or a L{Deferred}

        @see: https://twistedmatrix.com/trac/ticket/9207
        T)�final)�arg_astringr�s  rC�arg_finalastringzIMAP4Server.arg_finalastringQs�� ����D��1�1rEc	��|j�}|std��d}d\}}|dddk(r	|jdd�\}}}|dd}nw|ddd	k(r9|d
ddk7rtd��	t	|dd
�}|r|sy|j
|�}n6|jdd�}t|�dk(r|jd�|\}}|xs||fS#t$rtd��wxYw#t$rtd
t|dd
�z��wxYw)a�
        Parse an astring from the line, return (arg, rest), possibly
        via a deferred (to handle literals)

        @param line: A line that contains a string literal.
        @type line: L{bytes}

        @param final: Is this the final argument?
        @type final L{bool}

        @return: A 2-tuple containing the parsed argument and any
            trailing data, or a L{Deferred} that fires with that
            2-tuple
        @rtype: L{tuple} of (L{bytes}, L{bytes}) or a L{Deferred}

        �Missing argumentN�NNrr<�"r�zUnmatched quotes�{r}�}�Malformed literal�Bad literal size: )rErEr�rE)	�striprrYre�intrpr�r�rr)r]r�r�rrrr^�spamr�s        rCr�zIMAP4Server.arg_astringcsE��"�z�z�|���'�(:�;�;����	��T���!�9���
@�"&�*�*�T�1�"5���c�4��A�B�x���!�A�Y�$�
��B�C�y�D� �+�,?�@�@�
U��4��"�:����T�!��#�#�D�)�A��*�*�T�1�%�C��3�x�1�}��
�
�3���I�C����S�$�K���%�
@�+�,>�?�?�
@���
U�+�,@�4��Q�r�
�CS�,S�T�T�
U�s�C	�&C!�	C�!$Ds
(?P<atom>[s]+)( (?P<rest>.*$)|$)c��|std��|jj|�}|r"|jd�|jd�fStd��)z-
        Parse an atom from the line
        r��atomr^zMalformed ATOM)r�atomre�match�group)r]r��ms   rC�arg_atomzIMAP4Server.arg_atom�sQ���'�(:�;�;��K�K���d�#����7�7�6�?�A�G�G�F�O�3�3�'�(8�9�9rEc��|std��|dddk7rtd��|jd�}|dk(rtd��t|d|d	�||d
zdfS)zG
        Parse a (non-nested) parenthesised list from the line
        r�Nr<�(zMissing parenthesis�)r}�Mismatched parenthesisrr�)r�findr�)r]r�rhs   rC�	arg_plistzIMAP4Server.arg_plist�so���'�(:�;�;����8�t��'�(=�>�>��I�I�d�O����7�'�(@�A�A�!�$�q��)�Q�/��a�!�e�g��?�?rEc���|std��|dddk7rtd��|dddk7rtd��	t|dd�}|j|�S#t$rtd	|dd����wxYw)
z/
        Parse a literal from the line
        r�Nr<r�zMissing literalr}r�r�r�)rr�rer�)r]r�r�s   rC�arg_literalzIMAP4Server.arg_literal�s����'�(:�;�;����8�t��'�(9�:�:����9���'�(;�<�<�	M��t�A�b�z�?�D�� � ��&�&���	M�'�*<�T�!�B�Z�N�(K�L�L�	M�s�A�A/c� �t|�}|dfS)z
        searchkeys
        rE)r�)r]r��querys   rC�arg_searchkeyszIMAP4Server.arg_searchkeys�s��"�$�'���s�|�rEc���d}|jdd�}t|�dk(r|d}|d}	t|�|fS#t$r}t	dt|�z��d}~wwxYw)z
        sequence-set
        rEr�r<r�rzBad message number N)rYr��parseIdListrrr�)r]r�r^rrrSs     rC�
arg_seqsetzIMAP4Server.arg_seqset�ss�����j�j��q�!���s�8�q�=��q�6�D��!�f��	H���$�d�+�+��%�	H�'�(=��A��(F�G�G��	H�s�;�	A �A�A c�T�t�}|j|�|jdfS)z
        fetch-att
        rE)�_FetchParser�parseStringru)r]r�r�s   rC�arg_fetchattzIMAP4Server.arg_fetchatt�s%��
�N��	�
�
�d�����#��rEc���g}|dddk(r|dddk7rtd��|dd}|r�|jj|�}|std��|ddd	k(r7|j�dk(r$|j	d	|jd
�z�n?|j�dk(r!|j	|jd
��ntd��|jd�}|r��|dfS)
z-
        Flag part of store-att-flag
        rr<r�r}Nr�r�zMalformed flag�\r�r^rE)rr��searchr^rrr�)r]r��flagsr�s    rC�arg_flaglistzIMAP4Server.arg_flaglist�s�������!�9����B�C�y�D� �+�,D�E�E���"�:�D�����"�"�4�(�A��+�,<�=�=��A�a�y�E�!�a�g�g�i�1�n����U�Q�W�W�V�_�4�5�����a�����Q�W�W�V�_�-�+�,<�=�=��7�7�6�?�D���s�|�rEc�
�|dfS)z-
        Command line of UID command
        rEr?r�s  rC�arg_linezIMAP4Server.arg_line�s���c�{�rEc�N�|jd�r|j|�Sd|fS)z-
        Optional parenthesised list
        r�N)�
startswithr�r�s  rC�	opt_plistzIMAP4Server.opt_plists)���?�?�4� ��>�>�$�'�'��$�<�rEc��|jd�r	|jdd�\}}}||ddfSd|fS#t$rtd��wxYw)z+
        Optional date-time string
        r�r�zMalformed date-timer<N)r�rYrer)r]r�r��dater^s     rC�opt_datetimezIMAP4Server.opt_datetimesi���?�?�4� �
C�#'�:�:�d�A�#6� ��d�D��$�q�r�(�#�#��$�<���	�
C�+�,A�B�B�
C�s	�4�A	c���|ddj�dk(rT|jdd�}t|�dk(rtd��t|�dk(r|j	d�|\}}}||fSd|fS)	z4
        Optional charset of SEARCH command
        N�sCHARSETr�r�r<zMissing charset identifierrE)r[rYr�rrr)r]r�rrr�r^s     rC�opt_charsetzIMAP4Server.opt_charsetsu�����8�>�>��z�)��*�*�T�1�%�C��3�x�1�}�+�,H�I�I��3�x�1�}��
�
�3��!�O�D�#�t���;���$�<�rEc��ddj|j��zdz|jz}|j|��y)Ns[CAPABILITY r��] )�message)r��listCapabilities�IDENT�sendPositiveResponse�r]�msgs  rCr.zIMAP4Server.sendServerGreeting*s=����	�	�$�*?�*?�*A� B�B�U�J�T�Z�Z�W���!�!�#�!�.rEc�*�|jd||�y)N�BAD��_respond�r]r�r�s   rCrZzIMAP4Server.sendBadResponse.s���
�
�f�c�7�+rEc�*�|jd||�y)Nr�r�r�s   rCr�z IMAP4Server.sendPositiveResponse1����
�
�e�S�'�*rEc�*�|jd||�y)N�NOr�r�s   rCr]z IMAP4Server.sendNegativeResponse4r�rEc��t|fi|��}|r|j�|j|dd�y|jj	|�yrt)r7rGr�rrr)r]r��isAsync�kwargss    rCrQz IMAP4Server.sendUntaggedResponse7sA��"�7�5�f�5���4�<�<�/��M�M�'�4��.����$�$�W�-rEc�T�|r|jd|z�y|jd�y)Ns+ �+�r6r�s  rCr�z#IMAP4Server.sendContinuationRequest>s!����M�M�%�#�+�&��M�M�$�rEc��|dvr9|jr-|j}g|_|D]}|j|dd��|sd}|r$|jdj|||f��y|jdj||f��y)N)r�r�r��*r�)rr�r6r�)r]r>r�r�r�r�s      rCr�zIMAP4Server._respondDs����*�*�t�/@�/@��%�%�E� "�D���
/���
�
�c�4��.�
/���C���M�M�$�)�)�S�%��$9�:�;��M�M�$�)�)�S�%�L�1�2rEc���dg}|j�j�D]J\}}|�|j|��t|�s�&|j	|D�cgc]
}|dz|z��c}��L|Scc}w)Ns	IMAP4rev1�=)r(�itemsrrr�rz)r]�caps�c�vr's     rCr�zIMAP4Server.listCapabilitiesQsp���~���%�%�'�-�-�/�	=�D�A�q��y����A���Q������;�#�a�$�h��n�;�<�		=�
���<s�A1
c��|jddj|j��z�|j|d�y)NsCAPABILITY r�sCAPABILITY completed)rQr�r�r�r�s  rC�
do_CAPABILITYzIMAP4Server.do_CAPABILITYZs8���!�!�.�4�9�9�T�=R�=R�=T�3U�"U�V��!�!�#�'>�?rEc�~�|jd�|j|d�|jj�y)NsBYE Nice talking to yousLOGOUT successful)rQr�r�r7r�s  rC�	do_LOGOUTzIMAP4Server.do_LOGOUTcs2���!�!�"<�=��!�!�#�';�<����%�%�'rEc�(�|j|d�y)NsNOOP No operation performed�r�r�s  rC�do_NOOPzIMAP4Server.do_NOOPms���!�!�#�'E�FrEc���|j�j�}||jvr|j|d�y|j	|j|�|�y)NsAUTHENTICATE method unsupported)r[r�rr]�authenticate)r]r�r�s   rC�do_AUTHENTICATEzIMAP4Server.do_AUTHENTICATEusV���z�z�|�!�!�#���t�'�'�'��%�%�c�+M�N����4�d�.�.�t�4�6��<rEc�f�|j�|j|d�y|j||�y)Ns Temporary authentication failure)�portalr]�_setupChallenge)r]rr�s   rCr�zIMAP4Server.authenticate~s0���;�;���%�%�c�+N�O�����T�3�'rEc
��	|j�}t|�dd}d|_tj�|_|j
|�|j
j|j||�|j
j|j|�y#t$r1}|j|dtt|��z�Yd}~yd}~wwxYw)Nr}r~rx)�getChallengerrOrr�rCr�rm�_IMAP4Server__cbAuthChunkr<�_IMAP4Server__ebAuthChunkrPrZr;r�)r]rr��	challenge�codedrSs      rCr�zIMAP4Server._setupChallenge�s���
	E��)�)�+�I� �	�*�3�B�/�E�'�D�O�#(�>�>�#3�D� ��(�(��/�� � �,�,�T�-?�-?��s�K�� � �+�+�D�,>�,>��D���	Q�� � ��&7�-��A��:O�&O�P�P��	Q�s�B�	C�('C�Cc�^�	t|�}|j	|�|j�r|j
||�y|jj|dt�j|j|j|fd|fd�y#tj$rtd��wxYw)NzMalformed Response - not base64)
r�binascii�Errorr�setResponse�moreChallengesr�r��login�IAccount�addCallbacks�_IMAP4Server__cbAuthResp�_IMAP4Server__ebAuthResp)r]rurr��uncodeds     rC�
__cbAuthChunkzIMAP4Server.__cbAuthChunk�s���	K�!�&�)�G�	
����!���� �� � ��s�+��K�K���d�D�(�3�@�@��!�!�4�#4�#4�s�f�d�S�F�D�
���~�~�	K�'�(I�J�J�	K�s�B
�
B,c��|\}}}|tusJd��||_d|_||_|j	|d�|j|j�y)Nz(IAccount is the only supported interface�authsAuthentication successful)r��accountr>r1r�r,�POSTAUTH_TIMEOUT�r]rur��iface�avatar�logouts      rC�__cbAuthRespzIMAP4Server.__cbAuthResp�sZ��"(������� �L�"L�L� ������
�����!�!�#�'C�D�����-�-�.rEc���|jt�r|j|d�y|jt�r|j|d�y|j	|d�tj|�y)Ns#Authentication failed: unauthorizeds+Authentication failed: server misconfigureds'Server error: login failed unexpectedly)ryrr]r
rZr5r=rzs   rC�__ebAuthRespzIMAP4Server.__ebAuthResp�s`���=�=�*�+��%�%�c�+Q�R�
�]�]�/�
0��%�%��C�
�
� � ��&P�Q��G�G�G�rEc	�f�|j|dtt|j��z�y)NsAuthentication failed: )r]r;r�rgrzs   rC�
__ebAuthChunkzIMAP4Server.__ebAuthChunk�s)���!�!��+�m�C��
�
�<N�.O�O�	
rEc���|jr|j|d�y|jr�|jr�|j	|d�|j
j
|j�d|_|jj�|_d|jvrt|jd<d|jvrt|jd<yy|j|d�y)NsTLS already negotiatedsBegin TLS negotiation nowT�LOGINsPLAINsTLS not available)r%r]rr$r�r��startTLSr�copyrrr�s  rC�do_STARTTLSzIMAP4Server.do_STARTTLS�s����?�?��%�%�c�+D�E�
�X�X�$�*�*��%�%�c�+G�H��N�N�#�#�D�H�H�-�"�D�O�#�/�/�4�4�6�D���t�/�/�/�-=�� � ��*��t�/�/�/�-=�� � ��*�0�
�%�%�c�+?�@rEc���d|j�vr|j|d�yt|j||�j	|j
|�j
|j|�y)Nr!s!LOGIN is disabled before STARTTLS)r(rZr�authenticateLoginrm�_IMAP4Server__cbLoginr<�_IMAP4Server__ebLogin)r]r��user�passwds    rC�do_LOGINzIMAP4Server.do_LOGIN�s]���t�0�0�2�2�� � ��&J�K���d�,�,�d�F�;�G�G��N�N�C�	
�
�*�T�^�^�S�
)rEc��|jr5|jjtj||�dt�St��)a�
        Lookup the account associated with the given parameters

        Override this method to define the desired authentication behavior.

        The default behavior is to defer authentication to C{self.portal}
        if it is not None, or to deny the login otherwise.

        @type user: L{str}
        @param user: The username to lookup

        @type passwd: L{str}
        @param passwd: The password to login with
        N)r�r�r�UsernamePasswordr�r)r]rrs   rCrzIMAP4Server.authenticateLogin�sA���;�;��;�;�$�$��,�,�T�6�:�D�(��
� �!�!rEc��|\}}}|tur,|j|d�tjd|�d��y||_||_|j
|d�d|_|j|j�y)Ns-Server error: login returned unexpected valuez__cbLogin called with z, IAccount expectedsLOGIN succeededr�)
r�rZr5r=r�r1r�r>r,rrs      rC�	__cbLoginzIMAP4Server.__cbLogin�sv��"(������� �� � ��&V�W��G�G�,�U�I�5H�I�J�!�D�L�#�D�N��%�%�c�+=�>��D�J��O�O�D�1�1�2rEc	���|jt�r|j|d�y|j|dt	t|j��z�tj|�y)NsLOGIN failedrx)	ryrr]rZr;r�rgr5r=rzs   rC�	__ebLoginzIMAP4Server.__ebLogin�sT���=�=�*�+��%�%�c�?�;�� � ��&��s�7�=�=�7I�)J�J�
�
�G�G�G�rEc��dx}x}}t|jd�}|�0|j�}|j�}|j�}|j	dt|||g�z�|j
|d�y)Ns
NAMESPACE sNAMESPACE command completed)r1r��getPersonalNamespaces�getSharedNamespacesrQ�collapseNestedListsr�)r]r��personal�public�shared�nps      rC�do_NAMESPACEzIMAP4Server.do_NAMESPACE�s���%)�)��)�6�F�
 ����t�
4��
�>��/�/�1�H��+�+�-�F��+�+�-�F��!�!��/��6�6�0J�K�K�	
�	
�!�!�#�'E�FrEc���|jrs|jj|�t|jd�}|�2t|j�jtj�d|_d|_t|�}t|jjt|�|�j|j||�j|j||�y)Nr�)r8r9r:rr;r<r5r=r>�
_parseMboxr��selectrm�
_cbSelectWork�
_ebSelectWork)r]r��name�rw�cmdNamer?s      rC�_selectWorkzIMAP4Server._selectWorks����9�9��I�I�$�$�T�*�$�T�Y�Y��5�D����d�j�j�)�4�4�S�W�W�=��D�I��D�J��$����d�l�l�)�)�:�d�+;�R�@�L�L������	
�
�*�T�'�'��#�
6rEc�X�|j||dz�tj|�y)Ns failed: Server error�rZr5r=)r]r{r,r�s    rCr)zIMAP4Server._ebSelectWorks$�����S�'�,D�"D�E�����rEc���|�|j|d�yd|j�D�cgc]}|j���c}vr|j|d�y|j�D�cgc]
}t|���}}|j	d|j�fz�|j	d|j
�fz�|j	ddj|�zdz�|jdd	|j�fz�|j�xrd
xsd}|j|�|j|d|zd
z|zdz�d|_||_
ycc}wcc}w)NsNo such mailboxz	\noselectzMailbox cannot be selected�	%d EXISTS�	%d RECENT�FLAGS (r�r�s[UIDVALIDITY %d]r�r��[r�s successfulr')r]�getFlags�lowerr;rQ�getMessageCount�getRecentCountr�r��getUIDValidity�isWriteable�addListenerr>r8)r]r8r,r�r��flagr�s       rCr(zIMAP4Server._cbSelectWork sP���<��%�%�c�+=�>���t�}�}��?�!�A�G�G�I�?�?��%�%�c�+G�H��15����A���t�$�A��A��!�!�,�$�2F�2F�2H�1J�"J�K��!�!�,�$�2E�2E�2G�1I�"I�J��!�!�*�t�y�y��/?�"?�$�"F�G��!�!�$�(;�t�?R�?R�?T�>V�(V�W�����0�=�@�L��������!�!�#�t�a�x�%�'7�'�'A�N�'R�S���
���	��@��Bs�E#�)E(r<�SELECTr�EXAMINEc�d�|jd�||_|j|_d|_y)N�idle)r��parseTagrO�	lastStater�s  rC�do_IDLEzIMAP4Server.do_IDLE:s)���$�$�T�*���
������ ��rEc�f�|j|_|`|j|jd�|`y)NsIDLE terminated)rBrOr�rA)r]r�s  rC�
parse_idlezIMAP4Server.parse_idle@s,���.�.����N��!�!�$�-�-�1C�D��MrEc	�t�t|�}	|jj|�}|r|j|d�y|j	|d�y#t
$r.}|j	|t
t|���Yd}~yd}~wt$r)|j|d�tj�YywxYw)NsMailbox createdsMailbox not createds/Server error encountered while creating mailbox)r&r��creater�r]rr;r��
BaseExceptionrZr5r=)r]r�r*rur�s     rC�	do_CREATEzIMAP4Server.do_CREATEIs����$���
	G��\�\�(�(��.�F���)�)�#�/A�B��)�)�#�/E�F�� �	B��%�%�c�=��Q��+@�A�A���	�� � ��G�
�
�G�G�I�		�s�A�	B7�$B�2B7�6B7c��t|�}|j�dk(r|j|d�y	|jj	|�|j|d�y#t$r4}|j|t|�jd��Yd}~yd}~wt$r)|j|d�tj�YywxYw)N�inboxsYou cannot delete the inboxsMailbox deleted�imap4-utf-7s/Server error encountered while deleting mailbox)
r&r6r]r��deleter�rr��encoderHrZr5r=�r]r�r*r�s    rC�	do_DELETEzIMAP4Server.do_DELETE]s����$����:�:�<�7�"��%�%�c�+I�J��
	?��L�L����%�
�%�%�c�+=�>�� �	I��%�%�c�3�q�6�=�=��+G�H�H���	�� � ��G�
�
�G�G�I�		�s�A!�!	C�**B�2C�
Cc	��d�||fD�\}}|j�dk(s|j�dk(r|j|d�y	|jj||�|j	|d�y#t
$r|j
|d�Yyt$r.}|j|tt|���Yd}~yd}~wt$r)|j
|d�tj�YywxYw)Nc3�2K�|]}t|����y�wrt)r&)r�ns  rCrz(IMAP4Server.do_RENAME.<locals>.<genexpr>rs����F�a�J�q�M�F���rKs@You cannot rename the inbox, or rename another mailbox to inbox.sMailbox renamedsInvalid command syntaxs/Server error encountered while renaming mailbox)
r6r]r��renamer�rurZrr;r�rHr5r=)r]r��oldname�newnamer�s     rC�	do_RENAMEzIMAP4Server.do_RENAMEqs���F�G�W�3E�F�����=�=�?�g�%�����G�)C��%�%��X�
�
�	?��L�L�����1�
�%�%�c�+=�>���	A�� � ��&?�@��	B��%�%�c�=��Q��+@�A�A���	�� � ��G�
�
�G�G�I�		�s$�	A8�8C;�C;�$C�2C;�:C;c	�J�t|�}	|jj|�|j|d�y#t$r.}|j|t
t|���Yd}~yd}~wt$r)|j|d�tj�YywxYw)Ns
Subscribeds5Server error encountered while subscribing to mailbox)r&r��	subscriber�rr]r;r�rHrZr5r=rOs    rC�do_SUBSCRIBEzIMAP4Server.do_SUBSCRIBE�s����$���
	:��L�L�"�"�4�(�
�%�%�c�=�9�� �	B��%�%�c�=��Q��+@�A�A���	�� � ��M�
�
�G�G�I�		���;�	B"�$A-�-2B"�!B"c	�J�t|�}	|jj|�|j|d�y#t$r.}|j|t
t|���Yd}~yd}~wt$r)|j|d�tj�YywxYw)NsUnsubscribeds9Server error encountered while unsubscribing from mailbox)r&r��unsubscriber�rr]r;r�rHrZr5r=rOs    rC�do_UNSUBSCRIBEzIMAP4Server.do_UNSUBSCRIBE�s����$���
	<��L�L�$�$�T�*�
�%�%�c�?�;�� �	B��%�%�c�=��Q��+@�A�A���	�� � ��Q�
�
�G�G�I�		�r\c���t|�}t|�}t|jj||�j	|j
|||�j
|j|�yrt)r&rr��
listMailboxesrm�_cbListWorkr<�_ebListWork)r]r��refr8�subr,s      rC�	_listWorkzIMAP4Server._listWork�sW���$�����o���d�l�l�0�0�#�t�<�H�H����c�3��	
�
�*�T�%�%�s�
+rEc��|D]�\}}|r|jj|�s�$|j�D�cgc]
}t|���}}|j	�jd�}	t
|�tt|�|	|jd�f}
|jt|
����|j||dz�ycc}w)NrLs
 completed)r��isSubscribedr5r;�getHierarchicalDelimiterrN�DontQuoteMe�maprQrr�)r]�	mailboxesr�rer,r*�boxr<r��delim�resps           rCrbzIMAP4Server._cbListWork�s���"�
	E�I�D�#��$�,�,�3�3�D�9�9<����H���t�,�H��H��4�4�6�=�=�m�L����(���U�+���K�K�
�.�	���)�)�*=�d�*C�D�
	E�	
�!�!�#�w��'>�?��Is�C
c�R�|j|d�tj|�y)Ns1Server error encountered while listing mailboxes.r/rzs   rCrczIMAP4Server._ebListWork�s�����S�"V�W�����rEr�r�c��g}|D]}|jt|���t|�}t|jj
|d�j
|j|||�j|j|�yr�)
rrr:r&rr�r'rm�_cbStatusGotMailboxr<�_ebStatusGotMailbox)r]r��mailboxr�nativeNamesr*s      rC�	do_STATUSzIMAP4Server.do_STATUS�sw�����	3�D����|�D�1�2�	3��W�%���d�l�l�)�)�7�A�6�B�B��$�$�c�7�K�	
�
�*�T�-�-�s�
3rEc��|rCt|j|�j|j|j||fd||fd�y|j|d�y)NsCould not open mailbox)r�
requestStatusr��_IMAP4Server__cbStatus�_IMAP4Server__ebStatusr])r]r8r�rtrs     rCrrzIMAP4Server._cbStatusGotMailbox�sU����$�,�,�e�4�A�A��������g����g���

�
�%�%�c�+D�ErEc�R�|j|d�tj|�y�Ns/Server error encountered while opening mailbox.r/rzs   rCrszIMAP4Server._ebStatusGotMailbox�������S�"T�U�����rEc	��tdj|j�D�cgc]}d|z��	c}��}|jd|j	d�zdz|zdz�|j|d�ycc}w)N� z%s %s�STATUS rL� (r�sSTATUS complete)r;r�r�rQrNr�)r]�statusr�rmrSr�s      rC�
__cbStatuszIMAP4Server.__cbStatus�sq���S�X�X�F�L�L�N�&K�q�w��{�&K�L�M���!�!�����M�2�2�U�:�T�A�D�H�	
�	
�!�!�#�'9�:��	'Ls�A9c	�r�|j|d|zdztt|j��z�y)Nr�s	 failed: �rZr;r�rg)r]r{r�rms    rC�
__ebStatuszIMAP4Server.__ebStatus�s2�������c�!�L�0�=��W�]�]�AS�3T�T�	
rEc���t|�}t|jj|�j	|j
||||�j
|j|�yrt)r&rr�r'rm�_cbAppendGotMailboxr<�_ebAppendGotMailbox)r]r�rtr�r�r�s      rC�	do_APPENDzIMAP4Server.do_APPEND�sN���W�%���d�l�l�)�)�7�3�?�?��$�$�c�5�$��	
�
�*�T�-�-�s�
3rEc��|s|j|d�y|D�cgc]
}t|���}}|j|||�}|j|j||�|j|j|�ycc}w)NzRYCREATE] No such mailbox)r]r:�
addMessagerm�_IMAP4Server__cbAppendr<�_IMAP4Server__ebAppend)	r]r8r�r�r�r�r<�decodedFlagsrs	         rCr�zIMAP4Server._cbAppendGotMailbox�sn����%�%�c�+H�I��7<�=�t��T�*�=��=��O�O�G�\�4�8��	�
�
�d�o�o�s�D�1�	���T�_�_�c�*��>s�A;c�R�|j|d�tj|�yr|r/rzs   rCr�zIMAP4Server._ebAppendGotMailboxr}rEc�n�|jd|j�fz�|j|d�y)Nr1sAPPEND complete)rQr7r�)r]rur�r8s    rC�
__cbAppendzIMAP4Server.__cbAppend
s2���!�!�,�$�2F�2F�2H�1J�"J�K��!�!�#�'9�:rEc	�f�|j|dtt|j��z�y)NsAPPEND failed: r�rzs   rC�
__ebAppendzIMAP4Server.__ebAppends)������#�m�C��
�
�4F�&G�G�	
rEc��|j�}|�|jd|�y|j|j|j|f|f��y)N)�callbackArgs�errbackArgs)�
checkpoint�_IMAP4Server__cbCheckr��_IMAP4Server__ebCheck)r]r�rs   rC�do_CHECKzIMAP4Server.do_CHECKsI���O�O����9��N�N�4��%�
�N�N�������c�V�RU�QW�
�
rEc�(�|j|d�y)NsCHECK completedr��r]rur�s   rC�	__cbCheckzIMAP4Server.__cbCheck!s���!�!�#�'9�:rEc	�f�|j|dtt|j��z�y)NsCHECK failed: r�rzs   rC�	__ebCheckzIMAP4Server.__ebCheck$�&�����S�"3�m�C��
�
�DV�6W�"W�XrEc��y)ah
        Called when the client issues a CHECK command.

        This should perform any checkpoint operations required by the server.
        It may be a long running operation, but may not block.  If it returns
        a deferred, the client will only be informed of success (or failure)
        when the deferred's callback (or errback) is invoked.
        Nr?rbs rCr�zIMAP4Server.checkpoint's��rEc���d}|jj�rt|jj�}t	|jd����,|�|j�fd��nt�j�}|�-|j|j|j|fd|fd�y|jd|�y)Nc�$���j�Srt)r;)rur?s �rCrTz&IMAP4Server.do_CLOSE.<locals>.<lambda>9s
���T�Z�Z�\�rE)
r8r:r�expunger:rmr;r��_IMAP4Server__cbClose�_IMAP4Server__ebClose)r]r�rr?s   @rC�do_CLOSEzIMAP4Server.do_CLOSE2s�������9�9� � �"��d�i�i�/�/�0�A� ����D�1�����}��
�
�9�:�!�$�*�*�-���=�
�N�N�4�>�>�4�>�>�C�6�4�#��QU�V��N�N�4��%rEc�z�|j|d�|jj|�d|_d|_y)NsCLOSE completedr�)r�r8r9r>r�s   rC�	__cbClosezIMAP4Server.__cbCloseCs3���!�!�#�'9�:��	�	� � ��&���	���
rEc	�f�|j|dtt|j��z�y)NsCLOSE failed: r�rzs   rC�	__ebClosezIMAP4Server.__ebCloseIr�rEc���|jj�rJt|jj�j	|j
|j|fd|fd�y|j|d�y)Ns$EXPUNGE ignored on read-only mailbox)r8r:rr�r��_IMAP4Server__cbExpunge�_IMAP4Server__ebExpunger]r�s  rC�
do_EXPUNGEzIMAP4Server.do_EXPUNGELs_���9�9� � �"��$�)�)�+�+�,�9�9�� � �$�"2�"2�S�F�D�3�&�$�
�
�%�%�c�+R�SrEc�`�|D]}|jd|fz��|j|d�y)Ns
%d EXPUNGEsEXPUNGE completed)rQr�)r]rur�rSs    rC�__cbExpungezIMAP4Server.__cbExpungeVs7���	<�A��%�%�m�q�d�&:�;�	<��!�!�#�';�<rEc	��|j|dtt|j��z�t	j
|�y)NsEXPUNGE failed: �rZr;r�rgr5r=rzs   rC�__ebExpungezIMAP4Server.__ebExpunge[s7������$�}�S����5G�'H�H�	
�	����rEc���t|jd�}|�Yt|j||��j	|j
||j|�j
|j|�ytd�}t|jj||��j	|j||j||�j
|j|�y)N�rfs1:*)�ISearchableMailboxr8rr�rm�_IMAP4Server__cbSearchr<�_IMAP4Server__ebSearchr��fetch�_IMAP4Server__cbManualSearch)r]r��charsetr�rf�smr�s       rC�	do_SEARCHzIMAP4Server.do_SEARCHas���
��	�	�4�
0��
�>��"�)�)�U��4�@�@�����d�i�i��
��j����#�.��F�#�A��$�)�)�/�/�1�#�6�B�B��%�%�s�D�I�I�u�c�
��j����#�.rEc
���|rt|j|�}tdj|D�cgc]
}t	|���c}��}|jd|z�|j
|d�ycc}w)Nr�SEARCH �SEARCH completed)rk�getUIDr;r�r�rQr�)r]rur�r8rfrh�idss       rC�
__cbSearchzIMAP4Server.__cbSearchqs`�������f�-�F��C�H�H�f�%=��c�!�f�%=�>�?���!�!�*�s�"2�3��!�!�#�':�;��&>s�A1c
�0�|�g}d}|xr|dd}|xr|ddj�}	tttd�|��D]X\}\}
}|j	tj|�|
|||	�s�2|jd|r|j�n|
fz��Z|dk(r5ddlm	}|jd|jt|dd�|||||�y|r#|jd	d
j|�z�|j|d�y)aL
        Apply the search filter to a set of messages. Send the response to the
        client.

        @type result: L{list} of L{tuple} of (L{int}, provider of
            L{imap4.IMessage})
        @param result: A list two tuples of messages with their sequence ids,
            sorted by the ids in descending order.

        @type tag: L{str}
        @param tag: A command tag.

        @type mbox: Provider of L{imap4.IMailbox}
        @param mbox: The searched mailbox.

        @type query: L{list}
        @param query: A list representing the parsed form of the search query.

        @param uid: A flag indicating whether the search is over message
            sequence numbers or UIDs.

        @type searchResults: L{list}
        @param searchResults: The search results so far or L{None} if no
            results yet.
        Nrr}r<�s%d���reactorr�r�r�)r�rY�zip�range�
_searchFilterr
�deepcopyrr�twisted.internetr��	callLaterr�rQr�r�)
r]rur�r8r�rf�
searchResultsrh�lastSequenceId�
lastMessageId�msgIdr�r�s
             rC�__cbManualSearchzIMAP4Server.__cbManualSearchxs'��4� ��M�
�� �1�F�2�J�q�M���9�6�"�:�a�=�#7�#7�#9�
�#�C��a��&�$9�:�	P�O�A�|��s��!�!��
�
�e�$�e�S�.�-���$�$�U�c�c�j�j�l�u�-N�%N�O�	P�
��6�0������%�%��V�A�B�Z� ������	
���)�)�*�t�y�y��7O�*O�P��%�%�c�+>�?rEc�:�|r|j|||||�sy|r�y)a�
        Pop search terms from the beginning of C{query} until there are none
        left and apply them to the given message.

        @param query: A list representing the parsed form of the search query.

        @param id: The sequence number of the message being checked.

        @param msg: The message being checked.

        @type lastSequenceId: L{int}
        @param lastSequenceId: The highest sequence number of any message in
            the mailbox being searched.

        @type lastMessageId: L{int}
        @param lastMessageId: The highest UID of any message in the mailbox
            being searched.

        @return: Boolean indicating whether all of the query terms match the
            message.
        FT��_singleSearchStep)r]r��idr�r�r�s      rCr�zIMAP4Server._searchFilter�s/��,��)�)��r�3��
���	�
rEc��|jd�}t|t�r|j|||||�syy|j	�}|ddj�st
||�}||vSt|dt|�zd�}	|	�tdt|�z��||jvr|	|||||f�}
n
|	|||�}
|
syy)a�
        Pop one search term from the beginning of C{query} (possibly more than
        one element) and return whether it matches the given message.

        @param query: A list representing the parsed form of the search query.

        @param msgId: The sequence number of the message being checked.

        @param msg: The message being checked.

        @param lastSequenceId: The highest sequence number of any message in
            the mailbox being searched.

        @param lastMessageId: The highest UID of any message in the mailbox
            being searched.

        @return: Boolean indicating whether the query term matched the message.
        rFNr<�search_zInvalid search command %sT)rIrXrYr�r[�isalphar�rNr:r�_requiresLastMessageInfo)r]r�r�r�r�r��qr��
messageSetrRrus           rCr�zIMAP4Server._singleSearchStep�s���(
�I�I�a�L���a����%�%�a���^�]�S��.�+���	�A��R�a�5�=�=�?�)��N�;�
��
�*�*��D�)�l�1�o�"=�t�D���9�+�3�l�1�o�E�����5�5�5��u�e�S�>�=�2Q�R�F��u�e�S�1�F�� �rEc��y)aS
        Returns C{True} if the message matches the ALL search key (always).

        @type query: A L{list} of L{str}
        @param query: A list representing the parsed query string.

        @type id: L{int}
        @param id: The sequence number of the message being checked.

        @type msg: Provider of L{imap4.IMessage}
        Tr?�r]r�r�r�s    rC�
search_ALLzIMAP4Server.search_ALLs��rEc�&�d|j�vS)aA
        Returns C{True} if the message has been answered.

        @type query: A L{list} of L{str}
        @param query: A list representing the parsed query string.

        @type id: L{int}
        @param id: The sequence number of the message being checked.

        @type msg: Provider of L{imap4.IMessage}
        �	\Answered�r5r�s    rC�search_ANSWEREDzIMAP4Server.search_ANSWEREDs���s�|�|�~�-�-rEc���|jdd�jdd�}|j�j|j	d�j��dk7S)aR
        Returns C{True} if the message has a BCC address matching the query.

        @type query: A L{list} of L{str}
        @param query: A list whose first element is a BCC L{str}

        @type id: L{int}
        @param id: The sequence number of the message being checked.

        @type msg: Provider of L{imap4.IMessage}
        F�bcc�rr}��
getHeaders�getr6r�rI)r]r�r�r�r�s     rC�
search_BCCzIMAP4Server.search_BCCsN���n�n�U�E�*�.�.�u�b�9���y�y�{����	�	�!�� 2� 2� 4�5��;�;rEc��t|jd��}tjj	t|j
���|kSr�)�	parseTimerI�email�utils�	parsedater:�getInternalDate�r]r�r�r�r�s     rC�
search_BEFOREzIMAP4Server.search_BEFORE,s<������1��&���{�{�$�$�\�#�2E�2E�2G�%H�I�D�P�PrEc��|jd�j�}tj||j	�d�S�NrF�rIr6r6�strFile�getBodyFile�r]r�r�r��bodys     rC�search_BODYzIMAP4Server.search_BODY0s2���y�y��|�!�!�#���|�|�D�#�/�/�"3�U�;�;rEc���|jdd�jdd�}|j�j|j	d�j��dk7S)NF�ccr�rr}r�)r]r�r�r�r�s     rC�	search_CCzIMAP4Server.search_CC4�J��
�^�^�E�4�
(�
,�
,�T�2�
6���x�x�z���u�y�y��|�1�1�3�4��:�:rEc�&�d|j�vS�Nz\Deletedr�r�s    rC�search_DELETEDzIMAP4Server.search_DELETED8����c�l�l�n�,�,rEc�&�d|j�vS�Nz\Draftr�r�s    rC�search_DRAFTzIMAP4Server.search_DRAFT;s���C�L�L�N�*�*rEc�&�d|j�vS�Nz\Flaggedr�r�s    rC�search_FLAGGEDzIMAP4Server.search_FLAGGED>rrEc���|jdd�jdd�}|j�j|j	d�j��dk7S)NF�fromr�rr}r�)r]r�r�r��fms     rC�search_FROMzIMAP4Server.search_FROMAsJ��
�^�^�E�6�
*�
.�
.�v�r�
:���x�x�z���u�y�y��|�1�1�3�4��:�:rEc��|jd�j�}|jd|�j|d�}|j�j	|jd�j��dk7S)NrFr�r})rIr6r�r�r�)r]r�r�r��hdrs     rC�
search_HEADERzIMAP4Server.search_HEADEREsb���i�i��l� � �"���n�n�U�C�(�,�,�S�"�5���y�y�{����	�	�!�� 2� 2� 4�5��;�;rEc�&�|jd�yr��rIr�s    rC�search_KEYWORDzIMAP4Server.search_KEYWORDJ���
�	�	�!��rEc�X�t|jd��|j�kSr��r�rI�getSizer�s    rC�
search_LARGERzIMAP4Server.search_LARGERN� ���5�9�9�Q�<� �3�;�;�=�0�0rEc�N�d|j�vxrd|j�vS)N�\Recent�\Seenr�r�s    rC�
search_NEWzIMAP4Server.search_NEWQs#���S�\�\�^�+�N������0N�NrEc�8�|\}}|j|||||�S)a�
        Returns C{True} if the message does not match the query.

        @type query: A L{list} of L{str}
        @param query: A list representing the parsed form of the search query.

        @type id: L{int}
        @param id: The sequence number of the message being checked.

        @type msg: Provider of L{imap4.IMessage}
        @param msg: The message being checked.

        @type lastIDs: L{tuple}
        @param lastIDs: A tuple of (last sequence id, last message id).
        The I{last sequence id} is an L{int} containing the highest sequence
        number of a message in the mailbox.  The I{last message id} is an
        L{int} containing the highest UID of a message in the mailbox.
        r�)r]r�r�r��lastIDsr�r�s       rC�
search_NOTzIMAP4Server.search_NOTTs*��&+2�'����)�)�%��S�.�-�X�X�XrEc�&�d|j�vS�Nrr�r�s    rC�
search_OLDzIMAP4Server.search_OLDjs�������/�/rEc��t|jd��}tjj	|j��|k(Sr��r�rIr�r�r�r�r�s     rC�	search_ONzIMAP4Server.search_ONms7������1��&���{�{�$�$�S�%8�%8�%:�;�t�C�CrEc�l�|\}}|j|||||�}|j|||||�}|xs|S)a�
        Returns C{True} if the message matches any of the first two query
        items.

        @type query: A L{list} of L{str}
        @param query: A list representing the parsed form of the search query.

        @type id: L{int}
        @param id: The sequence number of the message being checked.

        @type msg: Provider of L{imap4.IMessage}
        @param msg: The message being checked.

        @type lastIDs: L{tuple}
        @param lastIDs: A tuple of (last sequence id, last message id).
        The I{last sequence id} is an L{int} containing the highest sequence
        number of a message in the mailbox.  The I{last message id} is an
        L{int} containing the highest UID of a message in the mailbox.
        r�)	r]r�r�r�r!r�r��a�bs	         rC�	search_ORzIMAP4Server.search_ORqsG��(+2�'����"�"�5�"�c�>�=�Q���"�"�5�"�c�>�=�Q���v�A�
rEc�&�d|j�vSr$r�r�s    rC�
search_RECENTzIMAP4Server.search_RECENT�s���S�\�\�^�+�+rEc�&�d|j�vS�Nrr�r�s    rC�search_SEENzIMAP4Server.search_SEEN�s���3�<�<�>�)�)rEc��|jdd�jdd�}tjj	|�}|t|j
d��kS)a�
        Returns C{True} if the message date is earlier than the query date.

        @type query: A L{list} of L{str}
        @param query: A list whose first element starts with a stringified date
            that is a fragment of an L{imap4.Query()}. The date must be in the
            format 'DD-Mon-YYYY', for example '03-March-2003' or '03-Mar-2003'.

        @type id: L{int}
        @param id: The sequence number of the message being checked.

        @type msg: Provider of L{imap4.IMessage}
        Fr�r�r�r�r�r�r�r�r�rIr�s     rC�search_SENTBEFOREzIMAP4Server.search_SENTBEFORE�sM���~�~�e�V�,�0�0���<���{�{�$�$�T�*���i��	�	�!��-�-�-rEc���|jdd�jdd�}tjj	|�}|ddt|j
d��ddk(S)a�
        Returns C{True} if the message date is the same as the query date.

        @type query: A L{list} of L{str}
        @param query: A list whose first element starts with a stringified date
            that is a fragment of an L{imap4.Query()}. The date must be in the
            format 'DD-Mon-YYYY', for example '03-March-2003' or '03-Mar-2003'.

        @type msg: Provider of L{imap4.IMessage}
        Fr�r�NrUrr3r�s     rC�
search_SENTONzIMAP4Server.search_SENTON�sZ���~�~�e�V�,�0�0���<���{�{�$�$�T�*���B�Q�x�9�U�Y�Y�q�\�2�2�A�6�6�6rEc��|jdd�jdd�}tjj	|�}|t|j
d��kDS)a�
        Returns C{True} if the message date is later than the query date.

        @type query: A L{list} of L{str}
        @param query: A list whose first element starts with a stringified date
            that is a fragment of an L{imap4.Query()}. The date must be in the
            format 'DD-Mon-YYYY', for example '03-March-2003' or '03-Mar-2003'.

        @type msg: Provider of L{imap4.IMessage}
        Fr�r�rr3r�s     rC�search_SENTSINCEzIMAP4Server.search_SENTSINCE�sM���~�~�e�V�,�0�0���<���{�{�$�$�T�*���i��	�	�!��-�-�-rEc��t|jd��}tjj	|j��|kDSr�r'r�s     rC�search_SINCEzIMAP4Server.search_SINCE�s7������1��&���{�{�$�$�S�%8�%8�%:�;�d�B�BrEc�X�t|jd��|j�kDSr�rr�s    rC�search_SMALLERzIMAP4Server.search_SMALLER�rrEc���|jdd�jdd�}|j�j|j	d�j��dk7S)NF�subjectr�rr}r�)r]r�r�r��subjs     rC�search_SUBJECTzIMAP4Server.search_SUBJECT�sL���~�~�e�Y�/�3�3�I�r�B���z�z�|� � ����1��!3�!3�!5�6�"�<�<rEc��|jd�j�}tj||j	�d�Sr�r�r�s     rC�search_TEXTzIMAP4Server.search_TEXT�s2���y�y��|�!�!�#���|�|�D�#�/�/�"3�U�;�;rEc���|jdd�jdd�}|j�j|j	d�j��dk7S)NF�tor�rr}r�)r]r�r�r�rDs     rC�	search_TOzIMAP4Server.search_TO�rrEc�j�|\}}|jd�}t||�}|j�|vS)al
        Returns C{True} if the message UID is in the range defined by the
        search query.

        @type query: A L{list} of L{bytes}
        @param query: A list representing the parsed form of the search
            query. Its first element should be a L{str} that can be interpreted
            as a sequence range, for example '2:4,5:*'.

        @type id: L{int}
        @param id: The sequence number of the message being checked.

        @type msg: Provider of L{imap4.IMessage}
        @param msg: The message being checked.

        @type lastIDs: L{tuple}
        @param lastIDs: A tuple of (last sequence id, last message id).
        The I{last sequence id} is an L{int} containing the highest sequence
        number of a message in the mailbox.  The I{last message id} is an
        L{int} containing the highest UID of a message in the mailbox.
        r)rIr�r�)	r]r�r�r�r!r�r�r�r�s	         rC�
search_UIDzIMAP4Server.search_UID�s8��,+2�'����I�I�a�L����=�)���z�z�|�q� � rEc�&�d|j�vS)Nr�r�r�s    rC�search_UNANSWEREDzIMAP4Server.search_UNANSWERED�s���3�<�<�>�1�1rEc�&�d|j�vSrr�r�s    rC�search_UNDELETEDzIMAP4Server.search_UNDELETED�����#�,�,�.�0�0rEc�&�d|j�vSrr�r�s    rC�search_UNDRAFTzIMAP4Server.search_UNDRAFT�s�������.�.rEc�&�d|j�vSr
r�r�s    rC�search_UNFLAGGEDzIMAP4Server.search_UNFLAGGED�rLrEc�&�|jd�yr�rr�s    rC�search_UNKEYWORDzIMAP4Server.search_UNKEYWORD�rrEc�&�d|j�vSr0r�r�s    rC�
search_UNSEENzIMAP4Server.search_UNSEEN�s���s�|�|�~�-�-rEc	��|j|dtt|j��z�t	j
|�y)NsSEARCH failed: r�rzs   rC�
__ebSearchzIMAP4Server.__ebSearchs7������#�m�C��
�
�4F�&G�G�	
�	����rEc�0�|r�|jd�|_t|jj||��jt�j|j|||�j|j|�y|j|d�y)Nr�sFETCH complete)r,�_oldTimeoutrr8r�rm�iter�_IMAP4Server__cbFetchr<�_IMAP4Server__ebFetchr�)r]r��messagesr�rfs     rC�do_FETCHzIMAP4Server.do_FETCHsq���#���t�4�D���$�)�)�/�/�8��=�I�I��
��k�$�.�.�#�u�c�:�:�:�����<�
�%�%�c�+<�=rEc�t�������j�g�_	t��\}}�j||���j�����fd��j	�j
�y#t$rB�j�j��`�j�d��j�YywxYw)Nc�,���j�����Srt)rZ)rjr��resultsr]r�rfs �����rCrTz'IMAP4Server.__cbFetch.<locals>.<lambda>4s���$�.�.��#�u�c�B�rEsFETCH completed)rG�next�spewMessagermr<�_IMAP4Server__ebSpewMessage�
StopIterationr,rXr�rK)r]r`r�r�rfr�r�s`````  rC�	__cbFetchzIMAP4Server.__cbFetchs�����<�<���D�L�	/��7�m�G�B��4
���R��e�S�1�=�=�B�
��j��-�-�.��7�	�
�O�O�D�,�,�-�� �
�%�%�c�+=�>�

�M�M�O�/	�s�A,�,AB7�6B7c�b�tj|�|jj�yrt)r5r=r�r7�r]r{s  rC�__ebSpewMessagezIMAP4Server.__ebSpewMessage7s ��
	��������%�%�'rEc�p�|�|jj}|dtt|�g�z�y)Ns	ENVELOPE )r�r�r�getEnvelope�r]r�r��_w�_fs     rC�
spew_envelopezIMAP4Server.spew_envelope?s1��
�:����%�%�B�
�<�-�{�3�/?�.@�A�A�BrEc���|�|jj}|j�D�cgc]
}t|���}}|ddj	|�zdz�ycc}w)Nr3r�r�)r��writenr5r;r�)r]r�r�rlrmr<�encodedFlagss       rC�
spew_flagszIMAP4Server.spew_flagsDsV��
�:����&�&�B�8;����G��
�d�+�G��G�
��d�i�i��5�5��<�=��Hs�Ac��|�|jj}|j�}tjjt
|��}|�&tjd|||fz�td��tjd|dd�}t|t|dfz�}|d�|dz}n@|ddk\rd}	nd	}	||	zd
t|d�dzdzt|d�dzd
zzfzz}|dt|�z�y)Nz$%d:%r: unpareseable internaldate: %rz(Internal failure generating INTERNALDATEz%d-%%s-%Y %H:%M:%S �	r<s+0000rr��-s%04di�drs
INTERNALDATE )r�r�r�r�r��parsedate_tzr:r5r�r�time�strftimer;�_MONTH_NAMES�abs�_quote)
r]r�r�rlrm�idate�ttup�strdate�odate�signs
          rC�spew_internaldatezIMAP4Server.spew_internaldateJs%��
�:����%�%�B��#�#�%���{�{�'�'��U�(;�<���<��G�G�:�b�#�u�=M�M�N� �!K�L�L��-�-� 5�t�B�Q�x�@���g��d�1�g�)>�(@�@�A����7�?��H�$�E��A�w�!�|�����������Q��L�D�(�C�/�3�t�A�w�<�$�3F�2�2M�M�O�P�P�
�	��f�U�m�+�,rEc��|�|jj}t|jd��}|dt	|�z�y)NTsRFC822.HEADER )r�r��_formatHeadersr��_literal)r]r�r�rlrm�hdrss      rC�spew_rfc822headerzIMAP4Server.spew_rfc822headeres;��
�:����%�%�B��c�n�n�T�2�3��
��x��~�-�.rEc��|�|jj}|d�|�t|j��j	|j�S)NsRFC822.TEXT )r�r��FileProducerr��beginProducingrks     rC�spew_rfc822textzIMAP4Server.spew_rfc822textksD��
�:����%�%�B�
�?��
���C�O�O�-�.�=�=�d�n�n�M�MrEc�h�|�|jj}|d|j�fz�y)NsRFC822.SIZE %d)r�r�rrks     rC�spew_rfc822sizezIMAP4Server.spew_rfc822sizers-��
�:����%�%�B�
�����
�/�/�0rEc�0�|�|jj}|d�|�t|d�}|�2t|j	��j|j�St
|d|j�j|j�S)NsRFC822 )r�r��IMessageFiler��openr��MessageProducerr)r]r�r�rlrm�mfs      rC�spew_rfc822zIMAP4Server.spew_rfc822wsz��
�:����%�%�B�
�:��
��
�#�t�
$��
�>�����	�*�9�9�$�.�.�I�I��s�D�$�/�/�:�I�I��N�N�
�	
rEc�h�|�|jj}|d|j�fz�y)NsUID %d)r�r�r�rks     rC�spew_uidzIMAP4Server.spew_uid�s+��
�:����%�%�B�
�9��
�
���&�'rEc	�B�|dtt|d�g�z�y)NsBODYSTRUCTURE T)r�getBodyStructurerks     rC�spew_bodystructurezIMAP4Server.spew_bodystructure�s!��
��2�4D�S�$�4O�3P�Q�Q�RrEc���|�|jj}|jD]4}|j�r|j	|�}�%|dkDs�+td��|jrl|j|jjg|jj���}t|�}||j�dzt|�z�y|jrR||j�dz�|�t|j��j!|j�S|j"r@t|jd��}||j�dzt|�z�y|j$r�||j�dz�|�|jr2t|j��j!|j�St'|d�}|�2t|j)��j!|j�St+|d|j,�j!|j�S|dt/t1|�g�z�y)Nrz*Requested subpart of non-multipart messager�TsBODY )r�r��part�isMultipart�
getSubPartru�headerr��negate�fieldsr��	__bytes__r�r6r�r�r��mime�emptyr�r�r�rrr�)	r]r�r�r�rlrmr�r�r�s	         rC�	spew_bodyzIMAP4Server.spew_body�s���
�:����%�%�B����	N�A���� ��n�n�Q�'���Q�� � L�M�M�
	N��;�;�!�3�>�>�$�+�+�"4�"4�J�t�{�{�7I�7I�J�D�!�$�'�D��t�~�~��$�&��$��7�8�
�Y�Y��t�~�~��$�&�'��D����� 1�2�A�A�$�.�.�Q�Q�
�Y�Y�!�#�.�.��"6�7�D��t�~�~��$�&��$��7�8�
�Z�Z��t�~�~��$�&�'��D��y�y�#�C�O�O�$5�6�E�E�d�n�n�U�U�!�#�t�,���>�'����	�2�A�A�$�.�.�Q�Q�&�s�D�$�/�/�B�Q�Q��N�N���

�x�-�/?��/D�.E�F�F�GrEc
����������	�
��t�j�}|j�|j���fd��
�fd���fd��	�������	�
��f
d�}�j	|��S)Nc����d�fz�y)Ns* %d FETCH (r?)r�r�s��rCr^z&IMAP4Server.spewMessage.<locals>.start�s����/�R�E�)�*rEc����d�y)Ns)
r?�r�s�rCrz'IMAP4Server.spewMessage.<locals>.finish�s
����(�OrEc����d�yr�r?r�s�rC�spacez&IMAP4Server.spewMessage.<locals>.space�s
����$�KrEc3�f�
K�d}�
��D]o}|jdk(rd}|jdk(r�j|�������n&t�d|jz�}|�������|�dus�i�	��q�r|s�	��j�����������y�w)NFrfTr��spew_r})�typer�rNr�)
�seenUIDr�rRrr�r�r�r�r]r�r^rfr�s
   ����������rC�spewz%IMAP4Server.spewMessage.<locals>.spew�s�������G��G��	
���9�9��%�"�G��9�9��&��.�.��r�3��u�E�E���g��	�	�&9�:�A��B��U�E�2�2��u�R�y�(��G�	
��7����m�m�B��U�E�:�:��H��G�s�A4B1�89B1)r�r�r�r�r)r]r�r�r�rf�wbufr�rr�r�r^r�s`````  @@@@@rCrbzIMAP4Server.spewMessage�sP�����4�>�>�*���
�
���
�
��	+�	�	�	�	�&���t�v�&�&rEc	���|j|j�|`tj|�|j	|dtt
|j��z�y)NsFETCH failed: )r,rXr5r=rZr;r�rgrzs   rC�	__ebFetchzIMAP4Server.__ebFetch�sJ������(�(�)�����������S�"3�m�C��
�
�DV�6W�"W�XrEc��|j�}|jd�}|jd�rd}n|jd�rd}nd}|D�cgc]
}t|���}}t	|j
j||||��j|j|j||j
||fd|fd�ycc}w)NsSILENTr�r<rur}rr�)
r[�endswithr�r:rr8�storer��_IMAP4Server__cbStore�_IMAP4Server__ebStore)r]r�r\�moder�rf�silentr<s        rC�do_STOREzIMAP4Server.do_STORE�s����z�z�|�����y�)���?�?�4� ��D�
�_�_�T�
"��D��D�05�6���d�#�6��6��d�i�i�o�o�x���#�F�S�S��N�N��N�N�
�$�)�)�S�&�)��
�F��
	
��7s�Cc��|rr|sp|j�D]]\}}|rd|j|�fz}nd}|D�	cgc]
}	t|	���}
}	|jd|dj	|
�|fz��_|j|d�ycc}	w)Ns UID %drEs%d FETCH (FLAGS (%b)%b)r�sSTORE completed)r�r�r;rQr�r�)r]rur�r8rfr��kr��uidstrr<r�s           rC�	__cbStorezIMAP4Server.__cbStore�s����&�����	
���1��'�4�;�;�q�>�*;�;�F� �F�9:�;���t�,�;��;��)�)�.�!�T�Y�Y�u�5E�v�1N�N��	
�	
�!�!�#�'9�:��	<s�Bc	�f�|j|dtt|j��z�y�Nrxr�rzs   rC�	__ebStorezIMAP4Server.__ebStore�r�rEc���t|�}t|jj|�j	|j
||||�j
|j|�yrt)r&rr�r'rm�_cbCopySelectedMailboxr<�_ebCopySelectedMailbox)r]r�r\rtrfs     rC�do_COPYzIMAP4Server.do_COPY	sN���W�%���d�l�l�)�)�7�3�?�?��'�'��h���	
�
�*�T�0�0�#�
6rEc��|s|j|d|z�yt|jj||�j	|j
||�j	|j||�j|j|�y)NzNo such mailbox: )	r]rr8r�rm�_IMAP4Server__cbCopy�_IMAP4Server__cbCopiedr<�_IMAP4Server__ebCopy)r]r8r�r\rtrfs      rCr�z"IMAP4Server._cbCopySelectedMailbox		sg����%�%�c�+>��+H�I��$�)�)�/�/�8�S�9�E�E��
�
�s�D�
��k�$�/�/�3��5�j�j����PS�6TrEc	�f�|j|dtt|j��z�yr�r�rzs   rCr�z"IMAP4Server._ebCopySelectedMailbox	r�rEc�,���g}t�d�}|D]�\}}|�(t|j|�}|j|��0|j	�}	|j�}
t
|d�}|�)|j�}t�j||	|
�}nTd��tj�}
t||
|j�jd�j|
|	|
f��fd�	�}|j|���tj |�S)Nc�(�|jd�|Sr�)r��rRs rC�rewindz$IMAP4Server.__cbCopy.<locals>.rewind+	s���F�F�1�I��HrEc�6���j�|�||�Srt)r�)rjr+rRrr8r�s    ��rCrTz&IMAP4Server.__cbCopy.<locals>.<lambda>4	s���T�_�_�"�1�I�q�!�>�rE)�IMessageCopierrr
rrr5r�r�r�r�r�r�r�rr�rmr�DeferredList)r]r\r�r8�addedDeferreds�fastCopyMboxr�r�rr�r�r��bodyFile�bufferr�s   `          @rC�__cbCopyzIMAP4Server.__cbCopy	s�����%�d�D�1��� 	%�G�B���'�!�,�"3�"3�S�9���%�%�a�(��
�L�L�N�E��&�&�(�D���T�*�D����9�9�;��!�$�/�/�8�U�D�I���"�/�/�1��#�C�����A�#�^�D�)� �[�$*�e�t����
�!�!�!�$�A 	%�B�!�!�.�1�1rEc���g}g}|D]4\}}|r|j|��|j|j��6|r|j|d�y|j|d�y)Nz%[ALERT] Some messages were not copiedsCOPY completed)rrrgr]r�)r]�deferredIdsr�r8r��failuresr�rus        rC�
__cbCopiedzIMAP4Server.__cbCopied<	se������)�	.�N�F�F���
�
�6�"�������-�		.�
��%�%�c�+R�S��%�%�c�+<�=rEc	��|j|dtt|j��z�t	j
|�y)NsCOPY failed:r�rzs   rC�__ebCopyzIMAP4Server.__ebCopyI	s1�����S�/�M�#�g�m�m�BT�4U�"U�V�����rEc�l�|j�}|dvrt|��|j|||d��y)N)�COPYr��STOREr�r<r�)r[rr\)r]r�r�r�s    rC�do_UIDzIMAP4Server.do_UIDM	s8���-�-�/���B�B�'��0�0����S�'�4�Q��7rEc�V�|r|jdd��y|jdd��y)Ns[READ-WRITE]T)r�r�s[READ-ONLY]�rQ�r]�	writeables  rC�modeChangedzIMAP4Server.modeChangedZ	s+����%�%�o�t�%�L��%�%�n�d�%�KrEc��|j�D]F\}}|D�cgc]
}t|���}}d|dj|�fz}|j|d���Hycc}w)Ns%d FETCH (FLAGS (%b))r�T�r�)r�r;r�rQ)r]�newFlags�mIdr�r<rqr�s       rC�flagsChangedzIMAP4Server.flagsChanged`	sb��"�.�.�*�	9�J�C��<A�B�D�M�$�/�B�L�B�*�c�4�9�9�\�3J�-K�K�C��%�%�c�4�%�8�	9��Bs�Ac�j�|�|jd|fzd��|�|jd|fzd��yy)Nr1Tr�r2r��r]�exists�recents   rC�newMessageszIMAP4Server.newMessagesf	sG�����%�%�l�f�Y�&>��%�M����%�%�l�f�Y�&>��%�M�rE�NNNrt�F�NrE)s!Ready for additional command text�rr�)�r�r�r�r�r�r-rr%r$r*r�r�r1r8rCrrr�r>rOr`r(r/r3r@rErGrKrHr`rbr\rdrernror�r�r�r��re�compile�escape�
_atomCharsr�r�r�r�r�r�r�r�r�r�r�r�r.rZr�r]rQr�r�r�r��unauth_CAPABILITY�auth_CAPABILITY�select_CAPABILITY�logout_CAPABILITYr��
unauth_LOGOUT�auth_LOGOUT�
select_LOGOUT�
logout_LOGOUTr��unauth_NOOP�	auth_NOOP�select_NOOP�logout_NOOPr��unauth_AUTHENTICATEr�r�r�r�r�r�r�unauth_STARTTLSr�unauth_LOGINrrrr$�auth_NAMESPACE�select_NAMESPACEr-r)r(�auth_SELECT�
select_SELECT�auth_EXAMINE�select_EXAMINErCrE�select_IDLE�	auth_IDLErI�auth_CREATE�
select_CREATErP�auth_DELETE�
select_DELETErX�auth_RENAME�
select_RENAMEr[�auth_SUBSCRIBE�select_SUBSCRIBEr_�auth_UNSUBSCRIBE�select_UNSUBSCRIBErfrbrc�	auth_LIST�select_LIST�	auth_LSUB�select_LSUBrvrrrs�auth_STATUS�
select_STATUSryrzr�r�r��auth_APPEND�
select_APPENDr�r�r��select_CHECKr�r�r�r��select_CLOSEr�r�r��select_EXPUNGEr�r�r��
select_SEARCHr�r�r�r�r�r�r�r�r�rrrrrrrrrr"r%r(r,r.r1r4r6r8r:r<r@rBrErGrIrKrNrPrRrTr�r]�select_FETCHrZrcrnrrr�r�r�r�r�r�r�r�rbr[r��select_STOREr�r�r��select_COPYr�r�r�r�r�r��
select_UIDr�r�r�r?rErCrrUs����
'�E��G����J��K��D��F��G��I��D��O����K� 7���E��J���"�"�	�%��G�*���:�>�V��(>�
�&
��2�$, �^�R�Z�Z��	��	�	�*�-�-�0H�H��F�
:�@�"'�(�
H���0� � �
 �/�,�+�+�.� �3��@�'�(��'�O�)��)��(�
�L�M��K�!�M�!�M�G��*�K��I��K��K�=�+�H�5��(�E��/�	�
�

A�#�n�O�*��k�+;�<�L�"�*
3��
G�#�_�N�%��7���(��Q�	�:�K��M���a��<�L�!�N�!���*�K��I�G�"�.�/�K��M�?�"�.�/�K��M�?�*�k�+;�<�K��M�:�#�$4�5�N�%��<�'�(8�9��)��,�@���K��a��A�I��K��K��a��A�I��K�	4�F���k�9�5�K��M�;�
�
4�+���k�9�l�K�P�K��M�;�
�
��;�L�;�Y�	�
&��;�L��Y�T�!�]�N�=�
�/���^�<�M�<�:@�x�:.�`�.�
<�Q�<�;�-�+�-�;�<�
�1�O�Y�,0�D��2,�*�.�$
7�
.�C�1�=�<�
;�!�62�1�/�1��.��	>��j�,�7�L� /�D(�C�
>�-�6/�N�1�


�(�
S�$H�L!'�FY�
�(�j�(�L�A�L�;�Y�7��J�(8�9�K�U�Y�&2�P>��8��(�H�-�J�
L�9�NrErc���eZdZdZdZdZdZdZdZdZ	dZ
dZdZdZ
dZeeed�ZdZdbd	�Zd
�Zd�Zd�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z d�Z!d�Z"d�Z#dcd�Z$d�Z%d�Z&d�Z'd�Z(d �Z)dbd!�Z*d"�Z+d#�Z,d$�Z-d%�Z.d&�Z/d'�Z0d(�Z1d)�Z2d*�Z3d+�Z4d,�Z5d-�Z6d.�Z7d/�Z8d0�Z9d1�Z:d2�Z;d3�Z<d4�Z=d5�Z>d6�Z?d7�Z@d8�ZAd9�ZBd:�ZCd;D��cic]}||j�d<���c}}ZEd=�ZFd>�ZGddd?�ZHd@�ZIdA�ZJdB�ZKdC�ZLdD�ZMdE�ZNddF�dG�ZOdH�ZPdedI�ZQdedJ�ZRdedK�ZSdedL�ZTdedM�ZUdedN�ZVdedO�ZWdedP�ZXdedQ�ZYdedR�ZZdedS�Z[dedT�Z\dedU�Z]dV�Z^dW�Z_							dfdX�Z`dedY�ZadgdZ�Zbdgd[�Zcdgd\�Zdd]�Zed^�Zfd_�Zgd`�Zhda�Ziycc}}w)h�IMAP4ClientzIMAP4 client protocol implementation

    @ivar state: A string representing the state the connection is currently
    in.
    Nr<Frr�)�OK�NO�BAD�PREAUTH�BYE)�MESSAGES�RECENT�UNSEENc�f�i|_g|_i|_||_d|_d|_d|_yrt)r*�queued�authenticators�context�_tag�_parts�_lastCmd)r]rs  rCr`zIMAP4Client.__init__�	s4����	���� ���%�����	������
rEc�Z�||j|j�j�<y)a�
        Register a new form of authentication

        When invoking the authenticate() method of IMAP4Client, the first
        matching authentication scheme found will be used.  The ordering is
        that in which the server lists support authentication schemes.

        @type auth: Implementor of C{IClientAuthentication}
        @param auth: The object to use to perform the client
        side of this authentication scheme.
        N)r4�getNamer[)r]r�s  rC�registerAuthenticatorz!IMAP4Client.registerAuthenticator�	s#��7;����D�L�L�N�0�0�2�3rEc�R�|jdkDr|j�|xjt|�zc_|jdkDr|jj|�yd}|jdkr|d|j||jd}}|jj|�|j}d|_d|_|j
dd�|jj|j��|j|jd��y)NrrE�
)r5rB�_pendingSizer��_pendingBufferr�r�r7rr�readrD�lstrip)r]r�r�r^s    rCrEzIMAP4Client.rawDataReceived�	s����<�<�!���������S��Y�&�����q� ����%�%�d�+��F�� � �1�$�#�$7�d�&7�&7�8�$�t�?P�?P�?R�:S�f�����%�%�d�+��&�&�D�"&�D�� $�D���I�I�a��O��K�K���t�y�y�{�+����V�]�]�7�3�4rEc���|j|�|_||_|j�
|dg|_n|jj	|dg�|j�y)Nr=)�messageFiler?r>r7rzr�)r]r^�octetss   rC�_setupForLiteralzIMAP4Client._setupForLiteral�	sR��"�.�.�v�6���"����;�;����/�D�K��K�K����g��/����rEc�Z�|jdkDr|j|j�yyr�)r5r,rbs rCr/zIMAP4Client.connectionMade�	s#���<�<�!���O�O�D�L�L�)�rEc��|jdkDr|jd�|j�5|j}d|_|D]}|jj	|��|j
�T|j
}d|_|j
�D]-}|��|j��|jj	|��/yy)z,
        We are no longer connected
        rN)r5r,r3r�errbackr*�values)r]r2r3r_r*s     rCr3zIMAP4Client.connectionLost�	s����<�<�!���O�O�D�!��;�;�"��[�[�F��D�K��
*���	�	�!�!�&�)�
*��9�9� ��9�9�D��D�I��{�{�}�
.���?�s�y�y�'<��I�I�%�%�f�-�
.�!rEc�f�|jdkDr|j�|jd�}|dk7rd||dzd}|jd�rK	t	|dd�}|j�|jdd�\|_	}n|}|j||�y|j�|j|�y|jj|�|jdj|j�}}dx|_	|_|j||�y#t
$rt
|��wxYw)aF
        Attempt to parse a single line from the server.

        @type line: L{bytes}
        @param line: The line from the server, without the line delimiter.

        @raise IllegalServerResponse: If the line or some part of the line
            does not represent an allowed message from the server at this time.
        rr�r}r<Nr�rE)r5rB�rfindr�r�rerr7rYr6rE�_regularDispatchrrr�r\)r]r��lastPartrD�partsr�r^s       rCrHzIMAP4Client.lineReceived�	s���<�<�!�������:�:�d�#���r�>��H�q�L�N�+�H�� � ��&�6� ��#�2��/�F��;�;�&�'+�z�z�$��':�$�D�I�u� �E��%�%�e�V�4���;�;���!�!�$�'�

�K�K���t�$��	�	�3�8�8�D�K�K�#8��C�&*�*�D�I���� � ��d�+��'"�6�/��5�5�6�s�D�D0c��|jrS|jj�=|jjdc}|j_|jt�|jrG|jD]8}|j��|jc}|_|jt��:|j
j
�yrt)r8rrH�
TIMEOUT_ERRORr3r�r7)r]rr_s   rCr@zIMAP4Client.timeoutConnection
s����=�=�T�]�]�0�0�<�%)�]�]�%8�%8�$�"�A�t�}�}�"�
�I�I�m�$��;�;��{�{�
-���9�9�(�#&�9�9�a�L�A�s�y��I�I�m�,�
-�
	
���%�%�'rEc��|jdd�}t|�dk7r|jd�|\}}|j||�y)Nr<r�rE)rYr�rrr\)r]r�rNr�r^s     rCrLzIMAP4Client._regularDispatch
sB���
�
�4��#���u�:��?��L�L����	��T����S�$�'rEc�\�||jkDrtj�St�S)a8
        Create a file to which an incoming message may be written.

        @type octets: L{int}
        @param octets: The number of octets which will be written to the file

        @rtype: Any object which implements C{write(string)} and
        C{seek(int, int)}
        @return: A file-like object
        )r�r�r�r)r]rDs  rCrCzIMAP4Client.messageFile!
s)���D�)�)�)��)�)�+�+��9�rEc�l�d|jzjd�}|xjdz
c_|S)Nz%0.4X�asciir<)�tagIDrNr�s  rC�makeTagzIMAP4Client.makeTag1
s-������#�+�+�G�4���
�
�a��
��
rEc��|j�
|j}n(t|d|jj�zd�}|r	|||�ytjd|j�d|�d|���|jj�y#t$r1tj�|jj�YywxYw)N�	response_zCannot dispatch: z, )	r>�response_UNAUTHrNr[rHr5r=r�r7)r]r�r^rRs    rCr\zIMAP4Client.dispatchCommand6
s����:�:���$�$�A���k�D�J�J�,<�,<�,>�>��E�A��
0��#�t��

�G�G�'��
�
�|�2�c�W�B�t�h�G�H��N�N�)�)�+��!�
0����	����-�-�/�
0�s�	B�7C�Cc	��|j��|jdd�\}}|j�dk(rd|_nF|j�dk(rd|_n+|jj	�t|dz|z��|j
d�|j
d�}}|d	k7r8|d	k7r3|j|jt||dz|�gdf��y|jd�y|j||�y)
Nr<r�rsPREAUTHr�r�r4�]r})r>rYr[r�r7rr��serverGreeting�_IMAP4Client__cbCapabilitiesr��_defaultHandler)r]r�r^r�r+rSs      rCrYzIMAP4Client.response_UNAUTHE
s����:�:���:�:�d�A�.�L�F�D��|�|�~��&�%��
�����:�-�#��
����-�-�/�+�C�$�J��,=�>�>��9�9�T�?�D�I�I�d�O�q�A��B�w�1��7��#�#��)�)�,=�d�1�q�5�1�o�,N�+O�QU�*V�W���#�#�D�)�� � ��d�+rEc�(�|j||�yrt)r^)r]r�r^s   rC�
response_AUTHzIMAP4Client.response_AUTH\
s�����S�$�'rEc��|dk(s|dk(rt|js|jt|�g�y|j|j}|dk(r|j	|�y|j
j
|�y	|j|}|jdd�\}}|dk(r|j||j�n$|jjt|��|j|=d|_|j�y#t$r,|jj�t!|dz|z��wxYw)Nr�r�r<r�r�)�waiting�
_extraInfor�r*r�r�rrrYrrrHr�_flushQueue�KeyErrorr�r7r)r]r�r^r_r�r�s      rCr^zIMAP4Client._defaultHandler_
s���$�;�#��+��<�<����!2�4�!8� 9�:��i�i����-���$�;��$�$�T�*��I�I�$�$�T�*�
#��i�i��n�� $�z�z�$��2�����U�?��J�J�t�T�_�_�5��I�I�%�%�n�T�&:�;��I�I�c�N�#���� � �"���
?����-�-�/�+�C�$�J��,=�>�>�
?�s�D�5Ec���|jrb|jjd�}|j�}||j|<|j	|j|��||_yyr�)r3rIrVr*r6r�rb�r]r_�ts   rCrdzIMAP4Client._flushQueue{
sS���;�;��+�+�/�/�!�$�C�����A��D�I�I�a�L��M�M�#�*�*�Q�-�(��D�L�rEc�t�i}dx}}|D�]}t|�}|dk(r|ddgk(r|jd��/|dk(r|ddgk(r|jd��O|dk(r|ddk(rt|d�}�k|dk(r|dd	k(rt|d�}��|d
k(r_|ddk(rWt|d�}|j|d�\}}	|j	|g�j|j
dd
����tjd|�����|r|j|�|�|�|j||�yy)Nr<rr�Fr�Tr�r�r�rUr��FLAGSr?z Unhandled unsolicited response: )r�r�r��_parseFetchPairs�
setdefaultrzr�r5r�r�r�)
r]r�r�r�r��response�elementsr�rIrjs
          rCrczIMAP4Client._extraInfo�
sV���������	G�H��8�}�H��1�}��!����!>�� � ��'��Q��8�A�;�=�/�#A�� � ��&��Q��8�A�;�)�#;��X�a�[�)���Q��8�A�;�)�#;��X�a�[�)���Q��8�A�;�(�#:��(�1�+�&�� �1�1�(�1�+�>�	���� � ��b�)�0�0����G�R�1H�I����:�8�*�E�F�	G�"����e�$����!3����V�V�,�"4rEc�L�tj�|_|jr'|jj	|�|jS|j�}||j|<|j|j|��||_||_	|jSrt)
rr�rbr3rrrVr*r6r�r8rgs   rC�sendCommandzIMAP4Client.sendCommand�
sw���N�N�$��	��<�<��K�K���s�#��9�9���L�L�N����	�	�!���
�
�c�j�j��m�$������
��y�y�rEc���|r+|j�tj|j�Sd}d}|jt	||���}|j|j�|S)av
        Request the capabilities available on this server.

        This command is allowed in any state of connection.

        @type useCache: C{bool}
        @param useCache: Specify whether to use the capability-cache or to
        re-retrieve the capabilities from the server.  Server capabilities
        should never change, so for normal use, this flag should never be
        false.

        @rtype: L{Deferred}
        @return: A deferred whose callback will be invoked with a
        dictionary mapping capability types to lists of supported
        mechanisms, or to None if a support list is not applicable.
        r�)r��r�)�	_capCacher�succeedrpr�rmr])r]�useCacher_rors     rC�getCapabilitieszIMAP4Client.getCapabilities�
s[��"����2��=�=����0�0��������W�S�t�<�=��	�
�
�d�+�+�,��rEc��|\}}i}|D]Z}|ddD]P}|jdd�}t|�dk(r|dd}	}n|\}}	|j|g�j|	��R�\|D]}||dgk(s�
d||<�||_|S)Nr<r�r)rYr�rlrrrs)
r]rur��tagliner�r^r'rN�categoryrgs
          rC�__cbCapabilitieszIMAP4Client.__cbCapabilities�
s���!�������	<�D��A�B�x�
<���	�	�$��*���u�:��?�&+�A�h��e�H�&+�O�H�e�����"�-�4�4�U�;�

<�	<��	&�H��H�~�$��'�!%��X��	&�����rEc�t�|jtdd���}|j|j�|S)a*
        Inform the server that we are done with the connection.

        This command is allowed in any state of connection.

        @rtype: L{Deferred}
        @return: A deferred whose callback will be invoked with None
        when the proper server acknowledgement has been received.
        sLOGOUT)sBYErr)rpr�rm�_IMAP4Client__cbLogout�r]rs  rCrzIMAP4Client.logout�
s0��
���W�Y�Y�G�H��	�
�
�d�o�o�&��rEc�B�|\}}|jj�yrt)r�r7�r]rur�rxs    rC�
__cbLogoutzIMAP4Client.__cbLogout�
s��!�������%�%�'�rEc�p�|jtd��}|j|j�|S)a
        Perform no operation.

        This command is allowed in any state of connection.

        @rtype: L{Deferred}
        @return: A deferred whose callback will be invoked with a list
        of untagged status updates the server responds with.
        sNOOP)rpr�rm�_IMAP4Client__cbNoopr}s  rC�noopzIMAP4Client.noop�
s.��
���W�W�-�.��	�
�
�d�m�m�$��rEc��|\}}|Srtr?rs    rC�__cbNoopzIMAP4Client.__cbNoop�
s��"�����rEc����jrJd��|��j�}|�tjt	d��Sd�j
vrtjt	d��St
j�jd�}|�tjt	d��S�jtd��}|j�j|�|j�fd��|S)a:
        Initiates a 'STARTTLS' request and negotiates the TLS / SSL
        Handshake.

        @param contextFactory: The TLS / SSL Context Factory to
        leverage.  If the contextFactory is None the IMAP4Client will
        either use the current TLS / SSL Context Factory or attempt to
        create a new one.

        @type contextFactory: C{ssl.ClientContextFactory}

        @return: A Deferred which fires when the transport has been
        secured according to the given contextFactory, or which fails
        if the transport cannot be secured.
        z5Client and Server are currently communicating via TLSNzEIMAP4Client requires a TLS context to initiate the STARTTLS handshaker"z:Server does not support secure communication via TLS / SSLzAIMAP4Client transport does not implement interfaces.ITLSTransportc�$���j�Srt�rv�rjr]s �rCrTz&IMAP4Client.startTLS.<locals>.<lambda>1s���� 4� 4� 6�rE)
r%�_getContextFactoryr�failrrsrr+r�rpr�rm�_startedTLS)r]r�tlsrs`   rCrzIMAP4Client.startTLSs����"���	C�B�	C���!�!�4�4�6�N��!��:�:��6���
��d�n�n�,��:�:��S���
��&�&�t�~�~�t�<���;��:�:��/���
�
���W�[�1�2��	�
�
�d�&�&��7�	�
�
�6�7��rEc��|j�|j�}ntj|j�}|j	|j
|�|S)a2
        Attempt to enter the authenticated state with the server

        This command is allowed in the Non-Authenticated state.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked if the authentication
        succeeds and whose errback will be invoked otherwise.
        )rsrvrrtrm�_IMAP4Client__cbAuthenticate)r]�secretrs   rCr�zIMAP4Client.authenticate4sG���>�>�!��$�$�&�A��
�
�d�n�n�-�A�	�
�
�d�+�+�V�4��rEc	����|jdd���D]K}|j��jvs� td|d�j||�}�j|�cS�jr7tjt��jj���S��fd�}�j�}|j|�|j�fd��|j�j|�|S)Nr r?�AUTHENTICATEc���|jt�tjt	��j
j
���Srt)�traprrr�r$r4r#)r=�authsr]s ��rC�
ebStartTLSz0IMAP4Client.__cbAuthenticate.<locals>.ebStartTLSTs9�������(��z�z�-�e�T�5H�5H�5M�5M�5O�P��rEc�$���j�Srtr�r�s �rCrTz.IMAP4Client.__cbAuthenticate.<locals>.<lambda>]s���D�$8�$8�$:�rE)r�r[r4r��_IMAP4Client__cbContinueAuthrpr%rr�r$r#rr<rm�_IMAP4Client__cbAuthTLS)r]r�r��schemer_r�rr�s`      @rC�__cbAuthenticatezIMAP4Client.__cbAuthenticateEs��������"�%���	-�F��|�|�~��!4�!4�4��#�V�R��1F�1F��PV����'�'��,�,�	-��?�?��:�:�)�%��1D�1D�1I�1I�1K�L��
�

��
�
��A�
�L�L��$�
�M�M�:�;�
�M�M�$�*�*�F�3��HrEc��	t|dz�}|j|}|j||�}|jt	|�j��y#tj$r|jd�t|��wxYw)N�
r�)	rr4�challengeResponser6rr�r�r�r)r]r^r�r�rr�s      rC�__cbContinueAuthzIMAP4Client.__cbContinueAuthas��	5��t�e�|�,�D�
�&�&�v�.�D��)�)�&�$�7�D��M�M�+�d�+�1�1�3�4��
�~�~�	.��M�M�$��'��-�-�	.�s�A�0B
c	��|jdd�}|D]K}|j�|jvs� td|d|j||�}|j|�cSt
||jj���)Nr r?r�)r�r[r4r�r�rpr$r#)r]r�r�r�r�r_s      rC�__cbAuthTLSzIMAP4Client.__cbAuthTLSls�������"�%���	-�F��|�|�~��!4�!4�4��#�V�R��1F�1F��PV����'�'��,�,�	-�(��t�/B�/B�/G�/G�/I�J�JrEc�j�t|j�}|j|j||�|S)a�
        Authenticate with the server using a username and password

        This command is allowed in the Non-Authenticated state.  If the
        server supports the STARTTLS capability and our transport supports
        TLS, TLS is negotiated before the login command is issued.

        A more secure way to log in is to use C{startTLS} or
        C{authenticate} or both.

        @type username: L{str}
        @param username: The username to log in with

        @type password: L{str}
        @param password: The password to log in with

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked if login is successful
        and whose errback is invoked otherwise.
        )rrvrm�_IMAP4Client__cbLoginCaps)r]�username�passwordrs    rCr�zIMAP4Client.loginvs/��*
�$�.�.�/��	�
�
�d�(�(�(�H�=��rEc��y)z�
        Called when the server has sent us a greeting.

        @type caps: C{dict}
        @param caps: Capabilities the server advertised in its greeting.
        Nr?)r]r�s  rCr\zIMAP4Client.serverGreeting���rEc�~�|j�|jS	ddlm}|j�S#t$rYywxYw)Nr)�ssl)r5r�r��ClientContextFactory�ImportError)r]r�s  rCr�zIMAP4Client._getContextFactory�sC���<�<�#��<�<��	.�,��+�+�-�-���	��	�s�0�	<�<c���d|v}tj|jd�du}tj|jd�du}|jsB|r@|r>|r<|j�}|j
|j|j||f��|S|rtjd�djt|�t|�f�}|jtd|��S)Nr")r�z5Server has no TLS support. logging in over cleartext!r�r)rr+r�r&r%rr��_IMAP4Client__cbLoginTLS�_IMAP4Client__ebLoginTLSr5r�r�r|rpr�)	r]r(r�r��tryTLS�tlsableTransport�nontlsTransportrr�s	         rC�
__cbLoginCapszIMAP4Client.__cbLoginCaps�s�����,��&�3�3�D�N�N�D�I�QU�U��%�2�2�4�>�>�4�H�D�P�����6�.>�?��
�
��A�
�N�N��!�!��!�!�&��1�
�
�
�H�����O�P��9�9�f�X�.��x�0@�A�B�D��#�#�G�H�d�$;�<�<rEc�X�|jj|�d|_d|_|S)NT)r�rrsr%)r]rur5s   rCr�zIMAP4Client._startedTLS�s'��������(��������
rEc��djt|�t|�f�}|jtd|��S)Nr�r)r�r|rpr�)r]rur�r�r�s     rC�__cbLoginTLSzIMAP4Client.__cbLoginTLS�s7���y�y�&��*�F�8�,<�=�>�������$� 7�8�8rEc�0�tj|�|Srt)r5r=rgs  rC�__ebLoginTLSzIMAP4Client.__ebLoginTLS�s��������rEc�|�d}d}|jt||���}|j|j�|S)a�
        Retrieve information about the namespaces available to this account

        This command is allowed in the Authenticated and Selected states.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with namespace
        information.  An example of this information is::

            [[['', '/']], [], []]

        which indicates a single personal namespace called '' with '/'
        as its hierarchical delimiter, and no shared or user namespaces.
        r�)r�rr)rpr�rm�_IMAP4Client__cbNamespace�r]r_rors    rC�	namespacezIMAP4Client.namespace�s<���������W�S�t�<�=��	�
�
�d�(�(�)��rEc��|\}}d�}|D]H}t|�dk(s�|ddk(s�|ddD��cgc]}|�gn|D�cgc]
}||���c}��c}}cStjd�ggggScc}wcc}}w)Nc�J�|D�cgc]}|jd���c}Scc}w�NrL)�decode)�
namespaceList�elements  rC�_prepareNamespaceOrDelimiterz?IMAP4Client.__cbNamespace.<locals>._prepareNamespaceOrDelimiter�s��AN�O�g�G�N�N�=�1�O�O��Os� r�rr�r<z*No NAMESPACE response to NAMESPACE command)r�r5r=)r]rur�rcr�rN�
pairOrNonergs        rC�
__cbNamespacezIMAP4Client.__cbNamespace�s����
���	P��	�E��5�z�Q��5��8�|�#;�
',�A�B�i�	�#�"�)��KU�V�%�6�u�=�V�W���	�	���<�=��B��|���	W��s�
A5�A0�
A5�0A5c��d}t|�}d}|jt|||���}|j|jd�|S)a�
        Select a mailbox

        This command is allowed in the Authenticated and Selected states.

        @type mailbox: L{str}
        @param mailbox: The name of the mailbox to select

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with mailbox
        information if the select is successful and whose errback is
        invoked otherwise.  Mailbox information consists of a dictionary
        with the following L{str} keys and values::

                FLAGS: A list of strings containing the flags settable on
                        messages in this mailbox.

                EXISTS: An integer indicating the number of messages in this
                        mailbox.

                RECENT: An integer indicating the number of "recent"
                        messages in this mailbox.

                UNSEEN: The message sequence number (an integer) of the
                        first unseen message in the mailbox.

                PERMANENTFLAGS: A list of strings containing the flags that
                        can be permanently set on messages in this mailbox.

                UIDVALIDITY: An integer uniquely identifying this mailbox.
        r=)rj�EXISTSr0r1�PERMANENTFLAGS�UIDVALIDITYrrr<��_prepareMailboxNamerpr�rm�_IMAP4Client__cbSelect�r]rtr_r�rors      rCr'zIMAP4Client.select�sL��@��"�7�+��X�����W�S�$�T�B�C��	�
�
�d�o�o�q�)��rEc��d}t|�}d}|jt|||���}|j|jd�|S)a�
        Select a mailbox in read-only mode

        This command is allowed in the Authenticated and Selected states.

        @type mailbox: L{str}
        @param mailbox: The name of the mailbox to examine

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with mailbox
        information if the examine is successful and whose errback
        is invoked otherwise.  Mailbox information consists of a dictionary
        with the following keys and values::

            'FLAGS': A list of strings containing the flags settable on
                        messages in this mailbox.

            'EXISTS': An integer indicating the number of messages in this
                        mailbox.

            'RECENT': An integer indicating the number of "recent"
                        messages in this mailbox.

            'UNSEEN': An integer indicating the number of messages not
                        flagged \Seen in this mailbox.

            'PERMANENTFLAGS': A list of strings containing the flags that
                        can be permanently set on messages in this mailbox.

            'UIDVALIDITY': An integer uniquely identifying this mailbox.
        r>)r�r�r�r�r�r�rrrr�r�s      rC�examinezIMAP4Client.examine!sM��@��"�7�+��
��
���W�S�$�T�B�C��	�
�
�d�o�o�q�)��rEc�J�	t|�S#t$rt|��wxYw)z�
        Parse C{value} as an integer and return the result or raise
        L{IllegalServerResponse} with C{phrase} as an argument if C{value}
        cannot be parsed as an integer.
        )r�rer)r]rg�phrases   rC�_intOrRaisezIMAP4Client._intOrRaiseOs+��	0��u�:����	0�'��/�/�	0�s�

�"c�H�|\}}d|i}|jt|��|D�]�}t|�dkDr�|dj�dk(r�|d}t	|t
�r|d}n|}|j�}|dk(rd|d<�`|dk(rd|d<�k|d	k(r|j
|d|�|d
<��|dk(r|j
|d|�|d<��|d
k(r|j
|d|�|d<��|dk(rtd�|dD��|d<��tjd|�����t|�dk(r�|dj�dk(rtd�|dD��|d<��:t	|dt�rz|dj�dk(r|j
|d|�|d<��}|dj�dk(r|j
|d|�|d<���tjd|������tjd|������tjd|������|S)zy
        Handle lines received in response to a SELECT or EXAMINE command.

        See RFC 3501, section 6.3.1.
        z
READ-WRITErr�r<r�Fr�Tr�r�r�r1r��UIDNEXTr�c3�2K�|]}t|����y�wrt�r:�rr<s  rCrz)IMAP4Client.__cbSelect.<locals>.<genexpr>zs����4�/3��T�*�4�rTr�zUnhandled SELECT response (2): r�r�c3�2K�|]}t|����y�wrtr�r�s  rCrz)IMAP4Client.__cbSelect.<locals>.<genexpr>�s����*S�$�<��+=�*S�rTrjr�r�r�r0zUnhandled SELECT response (0): zUnhandled SELECT response (1): zUnhandled SELECT response (4): )rrr�r�r[rXrYr��tupler5r=�bytes)	r]rur+r�rx�datumrY�contentrms	         rC�
__cbSelectzIMAP4Client.__cbSelectZs:��"�����r�"��
���&�w�/�0��+	C�E��5�z�A�~�%��(�.�.�"2�e�";���(���g�t�,�!�!�*�C�"�C��i�i�k���,�&�*/�E�,�'��M�)�*.�E�,�'��N�*�+/�+;�+;�G�A�J��+N�E�-�(��I�%�&*�&6�&6�w�q�z�5�&I�E�(�O��J�&�'+�'7�'7���
�E�'J�E�)�$��-�-�.3�4�7>�q�z�4�/�E�*�+��G�G�=�e�W�E�F��U��q����8�>�>�#�x�/�%*�*S�%�PQ�(�*S�%S�E�'�N���a��%�0��Q�x�~�~�'�9�4�*.�*:�*:�5��8�U�*K��h���q����)�Y�6�*.�*:�*:�5��8�U�*K��h�����"A�%�� I�J��G�G�=�e�W�E�F����9�%��A�B�W+	C�X�rEc�J�|jtdt|���S)a|
        Create a new mailbox on the server

        This command is allowed in the Authenticated and Selected states.

        @type name: L{str}
        @param name: The name of the mailbox to create.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked if the mailbox creation
        is successful and whose errback is invoked otherwise.
        sCREATE�rpr�r��r]r*s  rCrGzIMAP4Client.create��"������	�3F�t�3L� M�N�NrEc�J�|jtdt|���S)ak
        Delete a mailbox

        This command is allowed in the Authenticated and Selected states.

        @type name: L{str}
        @param name: The name of the mailbox to delete.

        @rtype: L{Deferred}
        @return: A deferred whose calblack is invoked if the mailbox is
        deleted successfully and whose errback is invoked otherwise.
        sDELETEr�r�s  rCrMzIMAP4Client.delete�r�rEc	��t|�}t|�}|jtddj||f���S)a�
        Rename a mailbox

        This command is allowed in the Authenticated and Selected states.

        @type oldname: L{str}
        @param oldname: The current name of the mailbox to rename.

        @type newname: L{str}
        @param newname: The new name to give the mailbox.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked if the rename is
        successful and whose errback is invoked otherwise.
        sRENAMEr�)r�rpr�r�)r]rVrWs   rCrUzIMAP4Client.rename�s>�� &�g�.��%�g�.������	�4�9�9�g�w�=O�3P� Q�R�RrEc�J�|jtdt|���S)a�
        Add a mailbox to the subscription list

        This command is allowed in the Authenticated and Selected states.

        @type name: L{str}
        @param name: The mailbox to mark as 'active' or 'subscribed'

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked if the subscription
        is successful and whose errback is invoked otherwise.
        s	SUBSCRIBEr�r�s  rCrZzIMAP4Client.subscribe�s"�������6I�$�6O� P�Q�QrEc�J�|jtdt|���S)a{
        Remove a mailbox from the subscription list

        This command is allowed in the Authenticated and Selected states.

        @type name: L{str}
        @param name: The mailbox to unsubscribe

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked if the unsubscription
        is successful and whose errback is invoked otherwise.
        sUNSUBSCRIBEr�r�s  rCr^zIMAP4Client.unsubscribe�s"�������8K�D�8Q� R�S�SrEc��d}d|�d|�d�jd�}d}|jt|||���}|j|jd�|S)ap
        List a subset of the available mailboxes

        This command is allowed in the Authenticated and Selected
        states.

        @type reference: L{str}
        @param reference: The context in which to interpret
            C{wildcard}

        @type wildcard: L{str}
        @param wildcard: The pattern of mailbox names to match,
            optionally including either or both of the '*' and '%'
            wildcards.  '*' will match zero or more characters and
            cross hierarchical boundaries.  '%' will also match zero
            or more characters, but is limited to a single
            hierarchical level.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a list of
            L{tuple}s, the first element of which is a L{tuple} of
            mailbox flags, the second element of which is the
            hierarchy delimiter for this mailbox, and the third of
            which is the mailbox name; if the command is unsuccessful,
            the deferred's errback is invoked instead.  B{NB}: the
            delimiter and the mailbox name are L{str}s.
        r��"z" "rL)r�rr)rNrpr�rm�_IMAP4Client__cbList)r]�	reference�wildcardr_r�rors       rCrYzIMAP4Client.list�s]��8���I�;�c�(��1�-�5�5�m�D�������W�S�$�T�B�C��	�
�
�d�m�m�W�-��rEc���d}|jd�}|jd�}djd|d|dg�}d}|jt|||���}|j	|j
d�|S)	aa
        List a subset of the subscribed available mailboxes

        This command is allowed in the Authenticated and Selected states.

        The parameters and returned object are the same as for the L{list}
        method, with one slight difference: Only mailboxes which have been
        subscribed can be included in the resulting list.
        r�rTrLrEr�s" ")r�rr)rNr�rpr�rmr�)	r]r�r�r_�encodedReference�encodedWildcardr�rors	         rC�lsubzIMAP4Client.lsub
s�����$�+�+�G�4��"�/�/�-�8���x�x�� ����
�
�������W�S�$�T�B�C��	�
�
�d�m�m�W�-��rEc��|\}}g}|D]}}t|�dk(s�|d|k(s�td�|dD��|d<|djd�|d<|djd�|d<|jt|dd���|S)Nr�rc3�2K�|]}t|����y�wrtr�r�s  rCrz'IMAP4Client.__cbList.<locals>.<genexpr>)
s���� I���d�!3� I�rTr<r�rLrU)r�r�r�rr)r]rur�r�rcr`rNs       rC�__cbListzIMAP4Client.__cbList"
s����
������	1�E��5�z�Q��5��8�w�#6� � I��a�� I�I��a��!��8�?�?�=�9��a�� ��8�?�?�=�9��a�����u�U�1�2�Y�/�0�)	1�*�rE�r/r0r�r�r1rTc�j��d}t|�}	dj�fd�|D��}dj|d|dg�}d}�j
t|||�	��}|j�j�|S#t$r.tdt	|�t	�j
�z
����wxYw)
a7
        Retrieve the status of the given mailbox

        This command is allowed in the Authenticated and Selected states.

        @type mailbox: L{str}
        @param mailbox: The name of the mailbox to query

        @type names: L{bytes}
        @param names: The status names to query.  These may be any number of:
            C{'MESSAGES'}, C{'RECENT'}, C{'UIDNEXT'}, C{'UIDVALIDITY'}, and
            C{'UNSEEN'}.

        @rtype: L{Deferred}
        @return: A deferred which fires with the status information if the
            command is successful and whose errback is invoked otherwise.  The
            status information is in the form of a C{dict}.  Each element of
            C{names} is a key in the dictionary.  The value for each key is the
            corresponding response from the server.
        r�r�c3�<�K�|]}�j|���y�wrt)�_statusNames)rr*r]s  �rCrz%IMAP4Client.status.<locals>.<genexpr>a
s�����H�$�d�/�/��5�H�s�zUnknown names: rEr�r�)r�rr)
r�r�rere�setr�rpr�rm�_IMAP4Client__cbStatus)r]rtrr_�preparedMailboxr�rors`       rCr�zIMAP4Client.statusH
s����*��-�g�6��	X��I�I�H�%�H�H�E��x�x��%���=�>�������W�S�$�T�B�C��	�
�
�d�o�o�&�����	X���s�5�z�C��@Q�@Q�<R�/R�.U�V�W�W�	X�s�A;�;7B2c��|\}}i}|D]R}|ddk(s�|d}tdt|�d�D�cgc]
}|||dz��}}|D]\}}		|	|t|�<��T|j
�D]/}|jj|�}
|
s�!	|
||�||<�1|Scc}w#t$rt	t|���wxYw#t$r+}t	d|zdz||zdzt|�z��d}~wwxYw)Nrr�r��(rz): )r�r�r:�UnicodeDecodeErrorrrpr#�STATUS_TRANSFORMATIONSr�rPr�)r]rur�rcr�rNr�rhr�r�rhrSs            rCr�zIMAP4Client.__cbStatusk
s?���
������	A�E��Q�x�9�$��a���38��C��J��3J�K�a��q�1�q�5�)�K��K�!�A�D�A�q�A�23��|�A��/�A�		A�����	�A��+�+�/�/��2�A��� !�&��)��F�1�I�		��
��L��.�A�3�D��K�@�@�A��!��/��a��#�
��q�	�1�E�9�C��F�B�����s)�B#�
B(�C	�(C�		C=�&C8�8C=c	�r�|jdd�|j�}|jdd�|rtdt|�z�}nd}|D�cgc]
}t|���}}dt	|�dj|�||fz}|j
td|d|j|��}	|	Scc}w)	a�
        Add the given message to the given mailbox.

        This command is allowed in the Authenticated and Selected states.

        @type mailbox: L{str}
        @param mailbox: The mailbox to which to add this message.

        @type message: Any file-like object opened in B{binary mode}.
        @param message: The message to add, in RFC822 format.  Newlines
        in this file should be \r\n-style.

        @type flags: Any iterable of L{str}
        @param flags: The flags to associated with this message.

        @type date: L{str}
        @param date: The date to associate with this message.  This should
        be of the format DD-MM-YYYY HH:MM:SS +/-HHMM.  For example, in
        Eastern Standard Time, on July 1st 2004 at half past 1 PM,
        "01-07-2004 13:30:00 -0500".

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked when this command
        succeeds or whose errback is invoked if it fails.
        rr�z "%s"rEs%b (%b)%b {%d}r�sAPPENDr?)	r��tellr;r:r�r�rpr��_IMAP4Client__cbContinueAppend)
r]rtr�r�r�r�r<rqr_rs
          rCrrzIMAP4Client.append�
s���4	���Q����L�L�N�����Q���� ��<��+=�!=�>�D��D�8=�>��
�d�+�>��>����(��I�I�l�#��
�	#
�
��
����I�s�B��(?�(?��I�
�����?s�B4c��tj�}|j||jd�j	|j
�Srt)r3�
FileSender�beginFileTransferr�rm�_IMAP4Client__cbFinishAppend)r]r�r�r�s    rC�__cbContinueAppendzIMAP4Client.__cbContinueAppend�
s?��������"�"�7�D�N�N�D�A�M�M��!�!�
�	
rEc�&�|jd�yr�r�)r]�foos  rC�__cbFinishAppendzIMAP4Client.__cbFinishAppend�
s���
�
�c�rEc�6�|jtd��S)a
        Tell the server to perform a checkpoint

        This command is allowed in the Selected state.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked when this command
        succeeds or whose errback is invoked if it fails.
        sCHECK�rpr�rbs rCryzIMAP4Client.check�
s������� 1�2�2rEc�6�|jtd��S)a�
        Return the connection to the Authenticated state.

        This command is allowed in the Selected state.

        Issuing this command will also remove all messages flagged \Deleted
        from the selected mailbox if it is opened in read-write mode,
        otherwise it indicates success by no messages are removed.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked when the command
        completes successfully or whose errback is invoked if it fails.
        sCLOSErrbs rCr;zIMAP4Client.close�
s������� 1�2�2rEc�|�d}d}|jt||���}|j|j�|S)a�
        Return the connection to the Authenticate state.

        This command is allowed in the Selected state.

        Issuing this command will perform the same actions as issuing the
        close command, but will also generate an 'expunge' response for
        every message deleted.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a list of the
        'expunge' responses when this command is successful or whose errback
        is invoked otherwise.
        r�)r�rr)rpr�rm�_IMAP4Client__cbExpunger�s    rCr�zIMAP4Client.expunge�
s<���������W�S�t�<�=��	�
�
�d�&�&�'��rEc��|\}}g}|D]>}t|�dk(s�|ddk(s�|j|j|d|���@|S)Nr�r<r�r)r�rrr�)r]rur�rcr�rNs      rCr�zIMAP4Client.__cbExpunge�
sY���
������	>�E��5�z�Q��5��8�z�#9��
�
�4�+�+�E�!�H�e�<�=�	>��
rEr�c���|D�cgc]}|jd���}}|rdnd}dj|�}|jt|||f���}|j	|j
�|Scc}w)a�
        Search messages in the currently selected mailbox

        This command is allowed in the Selected state.

        Any non-zero number of queries are accepted by this method, as returned
        by the C{Query}, C{Or}, and C{Not} functions.

        @param uid: if true, the server is asked to return message UIDs instead
            of message sequence numbers.
        @type uid: L{bool}

        @rtype: L{Deferred}
        @return: A deferred whose callback will be invoked with a list of all
            the message sequence numbers return by the search, or whose errback
            will be invoked if there is an error.
        rs
UID SEARCHr�r�rr)rNr�rpr�rm�_IMAP4Client__cbSearch)r]rf�queriesr�r_r�rs       rCr�zIMAP4Client.search�
sn��(9@�@�u�5�<�<�	�*�@��@�"�m�	���y�y��!�����W�S�$�c�V�D�E��	�
�
�d�o�o�&����
As�A1c
��|\}}g}|D]L}t|�dkDs�|ddk(s�|j|ddD�cgc]}|j||���c}��N|Scc}w)Nrr�r<)r�rzr�)r]rur�r_r�rNr�s       rCr�zIMAP4Client.__cbSearchsn���������	L�E��5�z�A�~�%��(�i�"7��
�
��a�b�	�J�1�D�,�,�Q��6�J�K�	L��
��Ks�A
c�*�|j||d��S)aV
        Retrieve the unique identifier for one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message sequence numbers to unique message identifiers, or whose
        errback is invoked if there is an error.
        r<)�useUIDrf��_fetch�r]r\rfs   rC�fetchUIDzIMAP4Client.fetchUIDs��$�{�{�8�C�Q�{�7�7rEc�*�|j||d��S)aH
        Retrieve the flags for one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: The messages for which to retrieve flags.

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to lists of flags, or whose errback is invoked if
        there is an error.
        r<)rr�rrs   rC�
fetchFlagszIMAP4Client.fetchFlags,s��$�{�{�8�C�q�{�9�9rEc�*�|j||d��S)a�
        Retrieve the internal date associated with one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: The messages for which to retrieve the internal date.

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to date strings, or whose errback is invoked
        if there is an error.  Date strings take the format of
        "day-month-year time timezone".
        r<)r�internaldaterrs   rC�fetchInternalDatezIMAP4Client.fetchInternalDate@s��&�{�{�8�C�a�{�@�@rEc�*�|j||d��S)a�
        Retrieve the envelope data for one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: The messages for which to retrieve envelope
            data.

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of
            message numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict
            mapping message numbers to envelope data, or whose errback
            is invoked if there is an error.  Envelope data consists
            of a sequence of the date, subject, from, sender,
            reply-to, to, cc, bcc, in-reply-to, and message-id header
            fields.  The date, subject, in-reply-to, and message-id
            fields are L{str}, while the from, sender, reply-to, to,
            cc, and bcc fields contain address data as L{str}s.
            Address data consists of a sequence of name, source route,
            mailbox name, and hostname.  Fields which are not present
            for a particular address may be L{None}.
        r<)r�enveloperrs   rC�
fetchEnvelopezIMAP4Client.fetchEnvelopeUs��6�{�{�8�C�!�{�<�<rEc�*�|j||d��S)a�
        Retrieve the structure of the body of one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: The messages for which to retrieve body structure
        data.

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to body structure data, or whose errback is invoked
        if there is an error.  Body structure data describes the MIME-IMB
        format of a message and consists of a sequence of mime type, mime
        subtype, parameters, content id, description, encoding, and size.
        The fields following the size field are variable: if the mime
        type/subtype is message/rfc822, the contained message's envelope
        information, body structure data, and number of lines of text; if
        the mime type is text, the number of lines of text.  Extension fields
        may also be included; if present, they are: the MD5 hash of the body,
        body disposition, body language.
        r<)r�
bodystructurerrs   rC�fetchBodyStructurezIMAP4Client.fetchBodyStructurers��6�{�{�8�C�q�{�A�ArEc�*�|j||d��S)a�
        Retrieve the simplified body structure of one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: C{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to body data, or whose errback is invoked
        if there is an error.  The simplified body structure is the same
        as the body structure, except that extension fields will never be
        present.
        r<)rr�rrs   rC�fetchSimplifiedBodyzIMAP4Client.fetchSimplifiedBody�s��(�{�{�8�C�a�{�8�8rEc�*�|j||d��S)a)
        Retrieve one or more entire messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: C{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}

        @return: A L{Deferred} which will fire with a C{dict} mapping message
            sequence numbers to C{dict}s giving message data for the
            corresponding message.  If C{uid} is true, the inner dictionaries
            have a C{'UID'} key mapped to a L{str} giving the UID for the
            message.  The text of the message is a L{str} associated with the
            C{'RFC822'} key in each dictionary.
        r<)r�rfc822rrs   rC�fetchMessagezIMAP4Client.fetchMessage�s��,�{�{�8�C��{�:�:rEc�*�|j||d��S)a<
        Retrieve headers of one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to dicts of message headers, or whose errback is
        invoked if there is an error.
        r<)r�rfc822headerrrs   rC�fetchHeaderszIMAP4Client.fetchHeaders�s��$�{�{�8�C�a�{�@�@rEc�*�|j||d��S)aL
        Retrieve body text of one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to file-like objects containing body text, or whose
        errback is invoked if there is an error.
        r<)r�
rfc822textrrs   rC�	fetchBodyzIMAP4Client.fetchBody����$�{�{�8�C�A�{�>�>rEc�*�|j||d��S)a6
        Retrieve the size, in octets, of one or more messages

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to sizes, or whose errback is invoked if there is
        an error.
        r<)r�
rfc822sizerrs   rC�	fetchSizezIMAP4Client.fetchSize�r-rEc	�2�|j||ddddd��S)aV
        Retrieve several different fields of one or more messages

        This command is allowed in the Selected state.  This is equivalent
        to issuing all of the C{fetchFlags}, C{fetchInternalDate},
        C{fetchSize}, C{fetchEnvelope}, and C{fetchSimplifiedBody}
        functions.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to dict of the retrieved data values, or whose
        errback is invoked if there is an error.  They dictionary keys
        are "flags", "date", "size", "envelope", and "body".
        r<)rr�rr/rr�rrs   rC�	fetchFullzIMAP4Client.fetchFull�s/��,�{�{���������
�	
rEc�0�|j||dddd��S)a.
        Retrieve several different fields of one or more messages

        This command is allowed in the Selected state.  This is equivalent
        to issuing all of the C{fetchFlags}, C{fetchInternalDate},
        C{fetchSize}, and C{fetchEnvelope} functions.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to dict of the retrieved data values, or whose
        errback is invoked if there is an error.  They dictionary keys
        are "flags", "date", "size", and "envelope".
        r<)rr�rr/rrrs   rC�fetchAllzIMAP4Client.fetchAlls(��*�{�{��S���a�RS��
�	
rEc�.�|j||ddd��S)a
        Retrieve several different fields of one or more messages

        This command is allowed in the Selected state.  This is equivalent
        to issuing all of the C{fetchFlags}, C{fetchInternalDate}, and
        C{fetchSize} functions.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a dict mapping
        message numbers to dict of the retrieved data values, or whose
        errback is invoked if there is an error.  They dictionary keys are
        "flags", "date", and "size".
        r<)rr�rr/rrs   rC�	fetchFastzIMAP4Client.fetchFast2s��*�{�{�8�C�q�q�UV�{�W�WrEc�F���fd��i}g}t|�}		t|�}	t|�}|dvrd}n>t	|t
�sd}n+t
|�dkDrd}n|rt	|dt
�rd}nd}t|�}|j|�|r�t
|�dkr7|D�cgc]
}t|���}}|j|�|t|�f}nKt|d�}	|dD�cgc]
}t|���}
}|j|	|
g�||	t|
�ff}	t|�}|jd	�rN|jd
�r=	t|dd�t|�}|j|�||fz}	t|�}�|�}|j|�|||<���#t$rY||fSwxYw#t$r
td|��wxYwcc}wcc}w#t$r
td|��wxYw#t$r
td|��wxYw#t$rY��wxYw)a�
        Given the result of parsing a single I{FETCH} response, construct a
        L{dict} mapping response keys to response values.

        @param fetchResponseList: The result of parsing a I{FETCH} response
            with L{parseNestedParens} and extracting just the response data
            (that is, just the part that comes after C{"FETCH"}).  The form
            of this input (and therefore the output of this method) is very
            disagreeable.  A valuable improvement would be to enumerate the
            possible keys (representing them as structured objects of some
            sort) rather than using strings and tuples of tuples of strings
            and so forth.  This would allow the keys to be documented more
            easily and would allow for a much simpler application-facing API
            (one not based on looking up somewhat hard to predict keys in a
            dict).  Since C{fetchResponseList} notionally represents a
            flattened sequence of pairs (identifying keys followed by their
            associated values), collapsing such complex elements of this
            list as C{["BODY", ["HEADER.FIELDS", ["SUBJECT"]]]} into a
            single object would also greatly simplify the implementation of
            this method.

        @return: A C{dict} of the response data represented by C{pairs}.  Keys
            in this dictionary are things like C{"RFC822.TEXT"}, C{"FLAGS"}, or
            C{("BODY", ("HEADER.FIELDS", ("SUBJECT",)))}.  Values are entirely
            dependent on the key with which they are associated, but retain the
            same structured as produced by L{parseNestedParens}.
        c���t|t�r|jd�St|t�r|D�cgc]
}�|���c}Sycc}w)Nr)rXr�r�rY)�thing�subthing�nativeStringResponses  �rCr;z:IMAP4Client._parseFetchPairs.<locals>.nativeStringResponseosE����%��'��|�|�I�.�.��E�4�(�GL�M�8�,�X�6�M�M�)��Ms�A
TsNot enough arguments)�BODYs	BODY.PEEKFr�rr<�<�>r})rYrardrrXrYr�r:rrr�r�r�r�re)r]�fetchResponseListrI�unstructured�
responsePartsrmrg�
hasSectionr��	valueHead�	valueTailr;s           @rCrkzIMAP4Client._parseFetchPairsIs����L	N������.�/�
��
��=�)��
X��]�+��6�1�1�#�
���t�,�#�
��U��a��"�
��:�e�A�h��5�#�
�"�
�
�s�#�C�����$���u�:��>�6;�<��\�!�_�<�E�<� �'�'��.���e��-�C� ,�U�1�X� 6�I�:?��(� C�Q��a�� C�I� C� �'�'��I�(>�?���E�)�,<� =�>�C�� ��/�E��#�#�D�)�e�n�n�T�.B���E�!�B�K�(�
!-�U� 3��$�+�+�E�2�!�U�H�n���$(��$7�E�)��/�E�����&��F�3�K�E��!�
��@�|�#�#�C
��
!�
X�+�,C�EV�W�W�
X��j=��!D��%��/�/�1B�����$ -��"7� 7�9J�#����&����sR�F-�F?�$G�,G�#G"�H�G;�-	F<�;F<�?G�"G8�;H�	H �H c�(�|\}}i}|D]W}t|�dk(s�|ddk(s�|j|d|�}||vr
|dg||<�>||dj|d��Yi}	i}
|j�D]b\}}|j	|d�\}
}|
j|gg�dj|�|	j|i�j
|
��d|
}i}t|	j��D]~}|	|}t|j��D][}||vs�|dk(s�|d||<tt||d��D] }||d|dk(s�||d||dz�=n|d=|r�Y|	|=�]��|r|j|�|r|	S|S)NrUr<r�rr�rj)r�r�rzr�rkrl�updaterYr#r�r�)r]ru�requestedParts�
structuredr�rc�inforNr�r`�decodedInfo�	messageIdrI�
structuredMap�unstructuredList�flagChangesr�rhs                  rC�_cbFetchzIMAP4Client._cbFetch�s����
������	1�E��5�z�Q��5��8�x�#7��%�%�e�A�h��6���T�>� %�a��z�D��H���H�Q�K�&�&�u�Q�x�0�
	1�����!%����	D��I�v�.2�.C�.C�F�1�I�.N�+�M�+��"�"�9�r�d�3�A�6�=�=�>N�O����y�"�-�4�4�]�C�	D������g�l�l�n�-�	/�I��Y�'�F��V�[�[�]�+�

/���~�-�$�'�/�-3�G�_�K�	�*�"�3�t�I��q�'9�#:�;�"���	�?�1�-�a�0�G�;� $�Y��� 2�1�q�1�u�9� =�!�"��w��!�#�I�.�

/�	/�����k�*���N��KrEc	���d}	|�d}
n;t|t�rt|�}
ndjt	t|��}
|�d}n
|
rd|z}n|}|r|dvr|�ddj|�z}nd}nd}|�d}
nd||fz}
|xrd	xsd
}|	||xrdxsd|
|||
fz}|jd�}|j
t||d
���}|j|jdd�|S)a^
        Retrieve a specific section of one or more messages

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
            numbers or of unique message IDs.

        @type headerType: L{str}
        @param headerType: If specified, must be one of HEADER, HEADER.FIELDS,
            HEADER.FIELDS.NOT, MIME, or TEXT, and will determine which part of
            the message is retrieved.  For HEADER.FIELDS and HEADER.FIELDS.NOT,
            C{headerArgs} must be a sequence of header names.  For MIME,
            C{headerNumber} must be specified.

        @type headerNumber: L{int} or L{int} sequence
        @param headerNumber: The nested rfc822 index specifying the entity to
            retrieve.  For example, C{1} retrieves the first entity of the
            message, and C{(2, 1, 3}) retrieves the 3rd entity inside the first
            entity inside the second entity of the message.

        @type headerArgs: A sequence of L{str}
        @param headerArgs: If C{headerType} is HEADER.FIELDS, these are the
            headers to retrieve.  If it is HEADER.FIELDS.NOT, these are the
            headers to exclude from retrieval.

        @type peek: C{bool}
        @param peek: If true, cause the server to not set the \Seen flag on
            this message as a result of this command.

        @type offset: L{int}
        @param offset: The number of octets at the beginning of the result to
            skip.

        @type length: L{int}
        @param length: The number of octets to retrieve.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a mapping of message
            numbers to retrieved data, or whose errback is invoked if there is
            an error.
        z%s BODY%s[%s%s%s]%sr��.)z
HEADER.FIELDSzHEADER.FIELDS.NOTz (%s)rz ()z<%d.%d>�	UID FETCHr�z.PEEKr�r�rrr?F)
rXr�r�r�rkrNrpr�rmrO)r]r\rf�
headerType�headerNumber�
headerArgs�peek�offset�length�fmt�numberr��payload�extrar�r_rs                 rC�
fetchSpecificzIMAP4Client.fetchSpecific	s��n$�����F�
��c�
*���&�F��X�X�c�#�|�4�5�F����F�
��:�%�F��F��j�$J�J��%�!�C�H�H�Z�$8�8�����G��>��E���� 0�0�E��$��0����X�t�/��5�2�v�v�w�PU�V�V���j�j��#�����W�U�C�k�J�K��	�
�
�d�m�m�R��/��rEc��t|�jd�}|xrdxsd}d|vr|d=d|d<d|vr|d=d|d<d	|vr|d	=d|d
<|D�cgc]
}t|���}}|dzdj|D�cgc]}|j	���c}�zd
z}|jt
||d���}|j|j|j�D�	cgc]}	|	j	���c}	d�|Scc}wcc}wcc}	w)NrTrRr�r+T�rfc822.textr/�rfc822.sizer(�
rfc822.headerr�r�r�rSrr)
r�rNr;r�r[rpr�rmrOr#)
r]r\r�termsr�r��encodedTermsr_rrhs
          rCrzIMAP4Client._fetchcs���x�=�'�'��0���'�<�3�8���5� ��l�#�#'�E�-� ��5� ��l�#�#'�E�-� ��U�"��n�%�%)�E�/�"�
38�8�Q�
�a�(�8��8�������|�+L�!�A�G�G�I�+L�!M�M�PT�T�����W�U�C�k�J�K��	�
�
�d�m�m�����%F�A�a�g�g�i�%F��M����9��+L��&Gs�
C=�4D
�D
c�,�|j|d|||�S)a
        Set the flags for one or more messages.

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type flags: Any iterable of L{str}
        @param flags: The flags to set

        @type silent: L{bool}
        @param silent: If true, cause the server to suppress its verbose
        response.

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a list of the
        server's responses (C{[]} if C{silent} is true) or whose
        errback is invoked if there is an error.
        r���_store�r]r\r�r�rfs     rC�setFlagszIMAP4Client.setFlags{s��2�{�{�8�X�v�u�c�B�BrEc�,�|j|d|||�S)a
        Add to the set flags for one or more messages.

        This command is allowed in the Selected state.

        @type messages: C{MessageSet} or L{str}
        @param messages: A message sequence set

        @type flags: Any iterable of L{str}
        @param flags: The flags to set

        @type silent: C{bool}
        @param silent: If true, cause the server to suppress its verbose
        response.

        @type uid: C{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a list of the
        server's responses (C{[]} if C{silent} is true) or whose
        errback is invoked if there is an error.
        s+FLAGSrfrhs     rC�addFlagszIMAP4Client.addFlags����2�{�{�8�Y���s�C�CrEc�,�|j|d|||�S)a
        Remove from the set flags for one or more messages.

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type flags: Any iterable of L{str}
        @param flags: The flags to set

        @type silent: L{bool}
        @param silent: If true, cause the server to suppress its verbose
        response.

        @type uid: L{bool}
        @param uid: Indicates whether the message sequence set is of message
        numbers or of unique message IDs.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a list of the
        server's responses (C{[]} if C{silent} is true) or whose
        errback is invoked if there is an error.
        s-FLAGSrfrhs     rC�removeFlagszIMAP4Client.removeFlags�rlrEc�h�t|�jd�}|D�cgc]
}t|���}}|r|dz}|xrdxsd}dj||ddj|�zdzf�}	|j	t||	d�	��}
d
}|sd}|
j
|j|d�|
Scc}w)
NrTs.SILENTs	UID STOREr�r�r�r�rSrrr?)rjT)r�rNr;r�rpr�rmrO)r]r\r_r�r�rfr<rqr�r�r�expecteds            rCrgzIMAP4Client._store�s����x�=�'�'��0��8=�>��
�d�+�>��>���
�"�C��$��0����y�y�(�C���	�	�,�0G�)G�$�)N�O�P�����W�U�D�{�K�L�����!�H�	�
�
�d�m�m�X�t�4����?s�B/c��t|�jd�}|rd}nd}dj|t|�g�}|j	t||��S)a�
        Copy the specified messages to the specified mailbox.

        This command is allowed in the Selected state.

        @type messages: L{MessageSet} or L{str}
        @param messages: A message sequence set

        @type mailbox: L{str}
        @param mailbox: The mailbox to which to copy the messages

        @type uid: C{bool}
        @param uid: If true, the C{messages} refers to message UIDs, rather
        than message sequence numbers.

        @rtype: L{Deferred}
        @return: A deferred whose callback is invoked with a true value
        when the copy is successful, or whose errback is invoked if there
        is an error.
        rTsUID COPYr�r�)r�rNr�r�rpr�)r]r\rtrfr_r�s      rCr
zIMAP4Client.copy�sV��*�x�=�'�'��0����C��C��y�y�(�$7��$@�A�B�������T� 2�3�3rEc��y�zOverride meNr?r�s  rCr�zIMAP4Client.modeChanged�r�rEc��yrsr?)r]r�s  rCr�zIMAP4Client.flagsChanged�r�rEc��yrsr?r�s   rCr�zIMAP4Client.newMessagesr�rErt�r<)r?Nr�)rNNNNNN)r<r)jr�r�r�r�r*rbr3rUr>r%r5rsr�r4�STATUS_CODESr�r�r5r`r;rErEr/r3rHr@rLrCrVr\rYr`r^rdrcrprvr]rr|r�r�rr�r�r�r�r�r\r�r�r�r�r�r�r�r'r�r�r�rGrMrUrZr^rYr�r�rNr�r�r�rrr�rryr;r�rr�rrrrrr!r#r&r)r,r0r2r4r6rkrOr^rrirkrnrgr
r�r�r�)rr*s00rCr)r)p	sm����D��G�
�F�
�E��E��J��G�
�I�'���N�8�L�*-���L���G��;�5�.�*�.�$(,�T(�(�� �

,�,�.(�#�8�-�8
��2�*����1�f�"�8	5�K��2�.�=�2�9���*�6'�R,�\	0�7�r
O�
O�S�(
R�
T�!�F�8�:
�	��	
�d�k�k�'�"�"�	�L�!�F�..�`
��
3�3� �*�$)��8�8�(:�(A�*=�:B�:9�,;�0A�(?�(?�(
�@
�2X�.T$�l(�Z
����
���X�t�0C�6D�6D�6�4�@����G	s�E*r)c�6�t�}|jd�}|D]�}d|vr�|jdd�\}}	|dk(rd}nt|�}|dk(rd}nt|�}||cxur�t|��|�|dks|�|dkrt|��|xs|}|xs|}|j	||���	|dk(rd}nt|�}|�|dkrt|��|j
|xs|���|S#t
$rt|��wxYw#t
$rt|��wxYw)a�
    Parse a message set search key into a C{MessageSet}.

    @type s: L{bytes}
    @param s: A string description of an id list, for example "1:3, 4:*"

    @type lastMessageId: L{int}
    @param lastMessageId: The last message sequence id or UID, depending on
        whether we are parsing the list in UID or sequence id context. The
        caller should pass in the correct value.

    @rtype: C{MessageSet}
    @return: A C{MessageSet} that contains the ids defined in the list
    �,�:r<r�Nr)rLrYr�rr[rerz)r�r�rwrNr�rirjs       rCr�r�sR���,�C�
�G�G�D�M�E�
�$/���1�9�����a�(�I�C��
0��$�;��C��c�(�C��4�<��D��t�9�D��$�&�0��3�3�'��O��q��d�6F�4�ST�9�0��3�3��,�}���*�]������T�"�

/���9��A��A��A��=�Q�!�V�0��3�3��
�
�1�-�
�.�I$/�J�J���
0�,�Q�/�/�
0���
0�,�Q�/�/�
0�s�A0C+�-%D�+D�D)�ALL�ANSWERED�DELETED�DRAFT�FLAGGED�NEW�OLDr0�SEEN�
UNANSWERED�	UNDELETED�UNDRAFT�	UNFLAGGEDr1)�LARGER�SMALLER�UIDc	��g}|j�}|rt|�}|D�]}||}|j�}|tvr|r|j	|��5|dk(r-|j|t
|d�t
|d�g��g|dk(s|dk(r*tjd|�}|j||g���|tvr@t|t�rd}nt|t�rd}nd}|j|||fzg���t|t�r|j|d	|fzg���|j||�g���"t|�dkDrd
dj|�zdzSdj|�S)
a
    Create a query string

    Among the accepted keywords are::

        all         : If set to a true value, search all messages in the
                      current mailbox

        answered    : If set to a true value, search messages flagged with
                      \Answered

        bcc         : A substring to search the BCC header field for

        before      : Search messages with an internal date before this
                      value.  The given date should be a string in the format
                      of 'DD-Mon-YYYY'.  For example, '03-Mar-2003'.

        body        : A substring to search the body of the messages for

        cc          : A substring to search the CC header field for

        deleted     : If set to a true value, search messages flagged with
                      \Deleted

        draft       : If set to a true value, search messages flagged with
                      \Draft

        flagged     : If set to a true value, search messages flagged with
                      \Flagged

        from        : A substring to search the From header field for

        header      : A two-tuple of a header name and substring to search
                      for in that header

        keyword     : Search for messages with the given keyword set

        larger      : Search for messages larger than this number of octets

        messages    : Search only the given message sequence set.

        new         : If set to a true value, search messages flagged with
                      \Recent but not \Seen

        old         : If set to a true value, search messages not flagged with
                      \Recent

        on          : Search messages with an internal date which is on this
                      date.  The given date should be a string in the format
                      of 'DD-Mon-YYYY'.  For example, '03-Mar-2003'.

        recent      : If set to a true value, search for messages flagged with
                      \Recent

        seen        : If set to a true value, search for messages flagged with
                      \Seen

        sentbefore  : Search for messages with an RFC822 'Date' header before
                      this date.  The given date should be a string in the format
                      of 'DD-Mon-YYYY'.  For example, '03-Mar-2003'.

        senton      : Search for messages with an RFC822 'Date' header which is
                      on this date  The given date should be a string in the format
                      of 'DD-Mon-YYYY'.  For example, '03-Mar-2003'.

        sentsince   : Search for messages with an RFC822 'Date' header which is
                      after this date.  The given date should be a string in the format
                      of 'DD-Mon-YYYY'.  For example, '03-Mar-2003'.

        since       : Search for messages with an internal date that is after
                      this date..  The given date should be a string in the format
                      of 'DD-Mon-YYYY'.  For example, '03-Mar-2003'.

        smaller     : Search for messages smaller than this number of octets

        subject     : A substring to search the 'subject' header for

        text        : A substring to search the entire message for

        to          : A substring to search the 'to' header for

        uid         : Search only the messages in the given message set

        unanswered  : If set to a true value, search for messages not
                      flagged with \Answered

        undeleted   : If set to a true value, search for messages not
                      flagged with \Deleted

        undraft     : If set to a true value, search for messages not
                      flagged with \Draft

        unflagged   : If set to a true value, search for messages not
                      flagged with \Flagged

        unkeyword   : Search for messages without the given keyword set

        unseen      : If set to a true value, search for messages not
                      flagged with \Seen

    @type sorted: C{bool}
    @param sorted: If true, the output will be sorted, alphabetically.
    The standard does not require it, but it makes testing this function
    easier.  The default is zero, and this should be acceptable for any
    application.

    @rtype: L{str}
    @return: The formatted query string
    �HEADERrr<�KEYWORD�	UNKEYWORDr�z"%s"z"%d"z%dr�r�))r#�_sortedr[�_SIMPLE_BOOLrrrzr��
_nonAtomREre�
_NO_QUOTESrXrLr�r�r�)rn�kwargr_r#r�r�rZs       rC�Queryr�SsZ��\
�C��:�:�<�D�
��t�}��
�$���!�H��
�G�G�I�������J�J�q�M�
�(�]��J�J��3�q��t�9�c�!�A�$�i�0�1�
�)�^�q�K�/����r�1�%�A��J�J��1�v��
�j�
 ��!�Z�(����A�s�#������J�J��3�!��:��'�
��3�
��J�J��4�1�$�;�'�(��J�J��a�S�{�#�1$�2�3�x�!�|��S�X�X�c�]�"�S�(�(��x�x��}�rEc��t|�dkrt|��t|�dk(rd|zSd|d�dt|dd��d�S)	z0
    The disjunction of two or more queries
    r�z
(OR %s %s)z(OR rrr<Nr�)r�r�Or)r�s rCr�r��sS���4�y�1�}���%�%�	�T��a���d�"�"��d�1�g�Y�a��D���H�
��a�0�0rEc��d|�d�S)zThe negation of a queryz(NOT r�r?)r�s rC�Notr��s��
�5�'���rEc���|jdd�}|�|jdd�}n(|jddtj|�z�}tj|tj�S)Nr�z(?:.*?)�%z(?:(?:[^%s])*?))�replacer�r�r��I)r�rns  rC�wildcardToRegexpr��sa������Y�/�H��}��#�#�C��3���#�#�C�):�R�Y�Y�u�=M�)M�N��
�:�:�h����%�%rEc��|j�}g}g}dx}}td|�}td|�}td|�}td|�}tt|��D�]\}	}
|
|k(rY|	r-||	dz
|	|k(r"|j	�|j|��;|sd}�@d}|j|j
|��g}�e|s:|s8|
|tjjd�zvrd}|j|
���|rb|s`|
tjjd�vr?|j
|�}||k(r|jd	�n|j|�g}d}��|s|s��|j|
���|rt|��|r:|j
|�}||k(r|jd	�|S|j|�|S)
a�
    Split a string into whitespace delimited tokens

    Tokens that would otherwise be separated but are surrounded by "
    remain as a single token.  Any token that is not quoted and is
    equal to "NIL" is tokenized as L{None}.

    @type s: L{bytes}
    @param s: The string to be split

    @rtype: L{list} of L{bytes}
    @return: A list of the resulting tokens

    @raise MismatchedQuoting: Raised if an odd number of quotes are present
    Fr��\r��NILr<TrTN)r�r8rfr9rIrrr��string�
whitespacerNr!)r�ru�word�inQuote�inWord�qu�escr��nilrhr��ws            rC�splitQuotedr��s��� 	
���	�A�
�F�
�D���G�f�	��a�	 �B�
�&�!�
$�C��B��"�E�
�%��
#�C��)�A�,�'����1���7��Q�q�1�u�q�\�S�(����
����B��������
�
�e�j�j��.�/������"�� 1� 1� 8� 8�� A�B�C��F��K�K��N�
�G��V�->�->�-E�-E�g�-N�(N��
�
�4� �A��C�x��
�
�d�#��
�
�a� ��D��F�
�w��K�K��N�7�:���"�"�
��J�J�t�����8��M�M�$���M�
�M�M�!���MrEc���g}||d�}|dg}|ddD]@}||�}||k7r |j|||��|g}|}�0|j|��B|j|||��|S�Nrr<)rzrr)�sequence�	predicate�transformersrur��tmprSr�s        rC�splitOnr�?s���
�F��X�a�[�!�D��A�;�-�C�
�a�b�\����a�L����9��M�M�,�,�t�,�S�1�2��#�C��D��J�J�q�M���M�M�$�,�t�$�S�)�*��MrEc�6�g}d}d�}d�d�d�}t|�D]Z\}}t|t�r@|�"|jt	|||||��d}|j
t
|���V|��Y|}�\|� |jt	||d||��|S)a{
    Turns a list of length-one strings and lists into a list of longer
    strings and lists.  For example,

    ['a', 'b', ['c', 'd']] is returned as ['ab', ['cd']]

    @type results: L{list} of L{bytes} and L{list}
    @param results: The list to be collapsed

    @rtype: L{list} of L{bytes} and L{list}
    @return: A new list which is the collapsed form of C{results}
    Nc�"�t|t�Srt)rXr��rSs rCrTz!collapseStrings.<locals>.<lambda>_s
��Z��5�)�rEc�6�tdj|��Sr�)r�r�r�s rCrTz!collapseStrings.<locals>.<lambda>as��[����!��-�rEc�R�dj|D�cgc]}|d��	c}�gScc}w)NrEr)r�)rSrhs  rCrTz!collapseStrings.<locals>.<lambda>bs$��c�h�h�a�0���!��0�1�2���0s�$)rr<)rfrXrYrzr�rr�collapseStrings)r`r
�begun�pred�tranrhr�s       rCr�r�Os����D��E�)�D�-�2��D��'�"����1��a���� �����G�G�E�!�$4�d�D�A�B����D�K�K���*�+�
�]��E��
������G�G�E�F�O�T�4�8�9��KrEc�f�|j�}d}gg}	d}t|�}||k�rJ|||dz}|rG|dk(r |dj|||dz�|dz
}�5|dk(r|}|dj|�|dz
}n�|dk(r|dj|�|}|dz
}n�|rb|dk(r]|jd|�}|dk(rt	d	��t||dz|�}|dj||d
z|d
z|zf�|d
z|z}nl|dk(s|dk(r|jg�|dz
}nK|d
k(s|dk(r(|dj|j
��|dz
}n|dj|�|dz
}||kr��Jt|�dk7rt|��t|d�S#t$rt|��wxYw)ak
    Parse an s-exp-like string into a more useful data structure.

    @type s: L{bytes}
    @param s: The s-exp-like string to parse

    @rtype: L{list} of L{bytes} and L{list}
    @return: A list containing the tokens present in the input.

    @raise MismatchedNesting: Raised if the number or placement
    of opening or closing parenthesis is invalid.
    rr<r�r}r�r�r�r�r�rUr�r4r�r[���)
r�r�rrr�rer�rI�
IndexErrorr r�)	r��
handleLiteralr��contentStackrhr�r�r_�literalSizes	         rCr�r�qs��	
���	�A��G��4�L�$#�
����F���!�e��!�a�!�e��A����:� ��$�+�+�A�a�!�a�%�L�9���F�A���$�Y�")�k�G��R� �'�'��*��Q�����9� ��$�+�+�A�.�")�k�G���F�A�"�q�D�y��&�&��q�/�C��b�y�(�)<�=�=�"%�a��A���n�"5�K� ��$�+�+�Q�s�Q�w��q��;�9N�-O�,Q�R��a��+�-�A��$�Y�!�t�)� �'�'��+���F�A��$�Y�!�t�)� ��$�+�+�L�,<�,<�,>�?���F�A� ��$�+�+�A�.���F�A�?�!�e�D�<��A����"�"��<��?�+�+��	�#���"�"�#�s�EF�F0c��td|�}td|�}||j|||z�j|||z�z|zS)Nr�r�)r8r�)r�r�r�s   rCr|r|�sJ��	��a�	 �B�
�&�!�
$�C�
��	�	�#�s�S�y�)�1�1�"�c�B�h�?�?�"�D�DrEr�r�c�"�dt|�|fzS)Ns{%d}
%b)r��r�s rCr�r��s���C��F�A�;�&�&rEc��eZdZd�Zdefd�Zy)rjc��||_yrt)rg)r]rgs  rCr`zDontQuoteMe.__init__�s	����
rEr�c�,�t|j�Srt)r�rgrbs rCr�zDontQuoteMe.__str__�s���4�:�:��rEN)r�r�r�r`r�r�r?rErCrjrj�s�����rErjs(){ %*"c�\�|dk(ryt|�D]}|dks|dkDry|tvs�yy)NrEr<r��r)r9�_ATOM_SPECIALS)r�r�s  rC�_needsQuoter��s?���C�x��
�q�\����w�;�!�g�+������	�

rEc��t|t�r|S	|jd�S#t$r t	j
�t
|��wxYwr�)rXr�r�rHr5r=r�r*s rCr&r&�sH���$�����+��{�{�=�)�)���+����	�$�T�*�*�+�s	�%�)Ac��t|t�s|jd�}|jd�}t	|�rt|�S|S)NrrL)rXr�r�rNr�r|r�s rCr�r��s?���d�C� ��{�{�9�%���;�;�}�%�D��4���d�|���KrEc�f�td|�}td|�}||vxs||vxst|�dkDS)N�
�
i�)r8r�)r��cr�lfs   rC�
_needsLiteralr��s:��	��q�	!�B�	��q�	!�B�
��7�.�b�A�g�.��Q��$��.rEc	�*�g}|D�]x}t|t�r|jd�}|�|jddg��;t|t�r&|jdtt|��g��qt|t�r|jd|jg���t|t�rZt|�r1|jddt|�fztj|g���|jdt|�g���	t|d�rB|j�}|jddt|�fztj|g���W|jddt!|�zdzg���{d	j#|d
d�S)a�
    Turn a nested list structure into an s-exp-like string.

    Strings in C{items} will be sent as literals if they contain CR or LF,
    otherwise they will be quoted.  References to None in C{items} will be
    translated to the atom NIL.  Objects with a 'read' attribute will have
    it called on them with no arguments and the returned string will be
    inserted into the output as a literal.  Integers will be converted to
    strings and inserted into the output unquoted.  Instances of
    C{DontQuoteMe} will be converted to strings and inserted into the output
    unquoted.

    This function used to be much nicer, and only quote things that really
    needed to be quoted (and C{DontQuoteMe} did not exist), however, many
    broken IMAP4 clients were unable to deal with this level of sophistication,
    forcing the current behavior to be adopted for practical reasons.

    @type items: Any iterable

    @rtype: L{str}
    rTNr�sNILs{%d}r@r�r�rEr<)rXr�rNrzr�r;rjrgr�r�r�r�	delimiterr|�hasattrr@rr�)r��piecesrhrs    rCrr�sL��,�F�
�H���a�������!�A��9��M�M�4��.�)�
��3�
��M�M�4��s�1�v�!6�7�8�
��;�
'��M�M�4����/�*�
��5�
!��Q���
�
�t�W��A��y�%8�+�:O�:O�QR�S�T��
�
�t�V�A�Y�/�0�
�Q��
�����A��M�M�4��C��F�9�!4�k�6K�6K�Q�O�P��M�M�4��(;�A�(>�!>��!E�F�G�-H�.�8�8�F�1�2�J��rEc�j�eZdZdZdZdZd�Zd�Zdd�Zd�Z	d�Z
dd�Zd	�Zd
�Z
d�Zd�Zd
�Zd�Zd�Zy)�MemoryAccountWithoutNamespacesNrc�.�||_i|_g|_yrt)r*rl�
subscriptionsr�s  rCr`z'MemoryAccountWithoutNamespaces.__init__s����	������rEc�H�|j}|xjdz
c_|SrQ)�top_id)r]r�s  rC�
allocateIDz)MemoryAccountWithoutNamespaces.allocateID!s��
�[�[�����q����	rEc���t|j��}||jvrt|��|� |j	||j��}||j|<yrQ)r&r[rlr�
_emptyMailboxr�)r]r*r8s   rC�
addMailboxz)MemoryAccountWithoutNamespaces.addMailbox)sX���$�*�*�,�'���4�>�>�!�"�4�(�(��<��%�%�d�D�O�O�,=�>�D�#����t��rEc�p�|jd�D�cgc]}|s�|��	}}tdt|��D]&}	|jdj	|d|���(	|jdj	|��ycc}w#t
$rY�[wxYw#t
$r|j
d�sYyYywxYw)N�/r<FT)rYr�r�r�r�rr�)r]�pathspec�path�paths�accums     rCrGz%MemoryAccountWithoutNamespaces.create2s���"*�.�.��"5�>�$���>��>��1�c�%�j�)�	�E�
��������v��� 7�8�	�
	��O�O�C�H�H�U�O�,���?��$�
��
�� �	��$�$�S�)��*��	�s-�B�B�#B�! B�	B�B�B5�4B5c��t�rt)�NotImplementedError)r]r*r�s   rCr�z,MemoryAccountWithoutNamespaces._emptyMailbox@s��!�!rEc�f�|jjt|j���Srt)rlr�r&r[)r]r*�	readwrites   rCr'z%MemoryAccountWithoutNamespaces.selectCs"���~�~�!�!�*�T�Z�Z�\�":�;�;rEc��t|j��}|jj|�}|st	d��d|j�vrA|jj
�D]$}||k7s�	|j|�s�t	d��|j�t|j|��dkDrt	d|�d���|j|=y)NzNo such mailboxz	\Noselectz<Hierarchically inferior mailboxes exist and \Noselect is setr<zName "z!" has inferior hierarchical names)r&r[rlr�rr5r#r��destroyr��_inferiorNames)r]r*r8�otherss    rCrMz%MemoryAccountWithoutNamespaces.deleteFs����$�*�*�,�'���~�~�!�!�$�'���"�#4�5�5��4�=�=�?�*��.�.�-�-�/�
���T�>�f�&7�&7��&=�*�W���
�
	
�����t�"�"�4�(�)�A�-�"�V�D�6�1R�#S�T�T��N�N�4� rEc	��t|j��}t|j��}||jvrt|��|j	|�}|D�cgc]}||j||d�f��}}|D]\}}||jvs�t
|��|D].\}}|j||j|<|j|=�0ycc}wrQ)r&r[rlr#r�r�r)r]rVrW�	inferiors�o�old�news       rCrUz%MemoryAccountWithoutNamespaces.rename]s����W�]�]�_�-���W�]�]�_�-���$�.�.�(���(�(��'�'��0�	�BK�L�Q�a����7�G�Q�7�8�L�	�L�!�	,�H�C���d�n�n�$�&�s�+�+�	,�"�	$�H�C��"&�.�.��"5�D�N�N�3�����s�#�	$��
Ms�!Cc��g}|jj�D]%}|j|�s�|j|��'|Srt)rlr#r�rr)r]r*r��infnames    rCr�z-MemoryAccountWithoutNamespaces._inferiorNamesnsG���	��~�~�*�*�,�	*�G��!�!�$�'�� � ��)�	*��rEc�L�t|j��|jvSrt)r&r[r�r�s  rCrhz+MemoryAccountWithoutNamespaces.isSubscribedus���$�*�*�,�'�4�+=�+=�=�=rEc��t|j��}||jvr|jj|�yyrt)r&r[r�rrr�s  rCrZz(MemoryAccountWithoutNamespaces.subscribexs9���$�*�*�,�'���t�)�)�)����%�%�d�+�*rEc��t|j��}||jvrtd|����|jj	|�y)NzNot currently subscribed to )r&r[r�r�remover�s  rCr^z*MemoryAccountWithoutNamespaces.unsubscribe}sG���$�*�*�,�'���t�)�)�)�"�%A�$��#H�I�I����!�!�$�'rEc���|jt|j���}t|d�}|D�cgc]%}|j	|�s�||j
|f��'c}Scc}w)Nr�)r�r&r[r�r�rl)r]rdr�rhs    rCraz,MemoryAccountWithoutNamespaces.listMailboxes�sV���!�!�*�S�Y�Y�[�"9�:��#�H�c�2��03�I�1�x�~�~�a�7H��D�N�N�1�%�&�I�I��Is�A&�A&rtrv)r�r�r�rlr�r�r`r�r�rGr�r'rMrUr�rhrZr^rar?rErCr�r�sS���I��M�
�F� �
���"�<�!�.$�"�>�,�
(�JrEr�c�$�eZdZd�Zd�Zd�Zd�Zy)�
MemoryAccountc��ddggS)NrE�/r?rbs rCrz#MemoryAccount.getPersonalNamespaces�s���d��}�rEc��yrtr?rbs rCrz!MemoryAccount.getSharedNamespaces����rEc��yrtr?rbs rC�getOtherNamespacesz MemoryAccount.getOtherNamespaces�rrEc��yrtr?rbs rC�getUserNamespaceszMemoryAccount.getUserNamespaces�s��rEN)r�r�r�rrrrr?rErCr�r��s��
���rEr�r7r8�
getUIDNextr9�getUnseenCountr�c�j�i}|D]+}t|t|j���||<�-|Srt)rN�_statusRequestDictr[)r8r�rrSs    rC�statusRequestHelperr�s<��
�A�
�>��;�w�t�/����	�:�;�=��!��>��HrEc��|�dgStjj|g�}|D��cgc]\}}|xsddg|jd�z��!c}}Scc}}w)Nr��@)r�r��getaddressesrY)�addrrg�addresss   rC�	parseAddrr�s[���|��
�	
��;�;�#�#�T�F�+�D�GK�L���G�R�Z�4������s�!3�3�L�L��Ls�$Ac
� �|jd�}|jd�}|jd�}|jd�}|jd|�}|jd|�}|jd�}|jd�}|jd	�}	|jd
�}
|jd�}||t|�t|�|xrt|�|xrt|�|xrt|�|	xrt|	�|
|f
S)NTr�r>r
�senderzreply-torDr�r�zin-reply-toz
message-id)r�r�r)r��headersr�r>�from_r�reply_torDr�r��in_reply_to�mids            rCrjrj�s����n�n�T�"�G��;�;�v��D��k�k�)�$�G��K�K���E�
�[�[��5�
)�F��{�{�:�u�-�H�	���T�	�B�	���T�	�B�
�+�+�e�
�C��+�+�m�,�K�
�+�+�l�
#�C����%���&���(�Y�x�(�
��y��}�
��y��}���	�#�����rEc�>�d}|j�D]}|dz
}�	|Sr�)r�)r�r�rjs   rC�getLineCountr�s-��
�E�
�_�_�
���
��
����LrEc�:�|d|dcxk(rdk(r	n|S|ddS|S)Nrr}r�r<r?r�s rC�unquoter�s.����t�q��u�����H���2�w���HrEc�L�d}|jdd�jdd�}dj|j��}|rW|j	d�}|dj	dd�}t|�dk(r|d}d}n|\}}t
d	�|ddD��}ndx}}|||fS)
zJ
    Return a two-tuple of the main and subtype of the given message.
    NF�content-typer��;rr�r<c3�xK�|]2}|j�j�jdd����4y�w)�=r<N)r�r6rY)rrSs  rCrz"_getContentType.<locals>.<genexpr>�s*����K��Q�W�W�Y�_�_�&�,�,�S�!�4�K�s�8:)r�r�r��
splitlinesrYr��dict)r��attrs�mm�mimetyper��major�minors       rC�_getContentTyper)�s���
�E�	����~�	.�	2�	2�>�2�	F�B�	�������	!�B�	��8�8�C�=����{� � ��a�(���t�9��>���G�E��E� �L�E�5��K�h�q�r�l�K�K�������%���rEc��t|�\}}}|�|j�}|�|j�}|dk(r
t|||�S||fdk(rt||||�S|dk(rt	||||�St||||�S)aE
    Construct an appropriate type of message structure object for the given
    message object.

    @param message: A L{IMessagePart} provider

    @return: A L{_MessageStructure} instance of the most specific type available
        for the given message, determined by inspecting the MIME type of the
        message.
    �	multipart)r�r%r6)r)r6�_MultipartMessageStructure�_RFC822MessageStructure�_TextMessageStructure�_SinglepartMessageStructure)r��main�subtyper$s    rC�_getMessageStructurer2�s���+�7�3��D�'�5����z�z�|�����-�-�/���{��)�'�7�E�B�B�
���1�	1�&�w��g�u�E�E�	
���$�W�d�G�U�C�C�*�7�D�'�5�I�IrEc�"�eZdZdZd�Zd�Zd�Zy)�_MessageStructurez�
    L{_MessageStructure} is a helper base class for message structure classes
    representing the structure of particular kinds of messages, as defined by
    their MIME type.
    c� �||_||_y)z�
        @param message: An L{IMessagePart} provider which this structure object
            reports on.

        @param attrs: A C{dict} giving the parameters of the I{Content-Type}
            header of the message.
        N)r�r$)r]r�r$s   rCr`z_MessageStructure.__init__s�������
rEc� �|r�|jd�}t|�dk(r|dj�df}|St|�dkDr@|ddD��cgc]}|jdd�D]}|���}}}|dj�|g}|Sycc}}w)a
        Parse a I{Content-Disposition} header into a two-sequence of the
        disposition and a flattened list of its parameters.

        @return: L{None} if there is no disposition header value, a L{list} with
            two elements otherwise.
        z; r<rNr!)rYr�r6)r]�disp�paramrS�paramss     rC�_dispositionz_MessageStructure._disposition s�����:�:�d�#�D��4�y�A�~��Q��
�
���.��
�K�	�T��Q��)-�a�b��O��5�;�;�s�A�;N�O�a�!�O�!�O��O��Q��
�
���0���K���	Ps� B
c���|jrY|jj�D��cgc]\}}|t|�f��}}}t|�D��cgc]}|D]}|���
c}}Sycc}}wcc}}w)z�
        @return: The I{Content-Type} parameters, unquoted, as a flat list with
            each Nth element giving a parameter name and N+1th element giving
            the corresponding parameter value.
        N)r$r�rrn)r]r�r��unquotedrS�ys      rC�_unquotedAttrsz _MessageStructure._unquotedAttrs4se���:�:�6:�j�j�6F�6F�6H�I�F�Q���G�A�J��I�H�I�%�h�/�;�!��;�A�A�;�A�;�;���J��;s�A'�A-N)r�r�r�r�r`r:r>r?rErCr4r4s���	��(	rEr4c�0�eZdZdZgd�Zd�Zd�Zd�Zd�Zy)r/zn
    L{_SinglepartMessageStructure} represents the message structure of a
    non-I{multipart/*} message.
    )�
content-id�content-description�content-transfer-encodingc�\�tj|||�||_||_||_y)a�
        @param message: An L{IMessagePart} provider which this structure object
            reports on.

        @param main: A L{str} giving the main MIME type of the message (for
            example, C{"text"}).

        @param subtype: A L{str} giving the MIME subtype of the message (for
            example, C{"plain"}).

        @param attrs: A C{dict} giving the parameters of the I{Content-Type}
            header of the message.
        N)r4r`r0r1r$)r]r�r0r1r$s     rCr`z$_SinglepartMessageStructure.__init__Hs+��	�"�"�4��%�8���	������
rEc�@�|jjdg|j���}|jj�}|j|j
}}|j
�}||||jd�|jd�|jd�|gS)zN
        Return a list of the basic fields for a single-part message.
        Fr@rArB)r�r��_HEADERSrr0r1r>r�)r]rr�r'r(�
unquotedAttrss      rC�_basicFieldsz(_SinglepartMessageStructure._basicFields[s���*�$�,�,�)�)�%�@�$�-�-�@���|�|�#�#�%���y�y�$�,�,�u���+�+�-�
�
����K�K��%��K�K�-�.��K�K�3�4��
�	
rEc�h�|j�}|r|j|j��|S)z�
        Construct and return a list of the basic and extended fields for a
        single-part message.  The list suitable to be encoded into a BODY or
        BODYSTRUCTURE response.
        )rGrz�	_extended�r]�extendedrus   rCrNz"_SinglepartMessageStructure.encodess-���"�"�$����M�M�$�.�.�*�+��
rEc�f�g}|jjddddd�}|j|jd��|j|j	|jd���|j|jd��|j|jd��|S)aw
        The extension data of a non-multipart body part are in the
        following order:

          1. body MD5

             A string giving the body MD5 value as defined in [MD5].

          2. body disposition

             A parenthesized list with the same content and function as
             the body disposition for a multipart body part.

          3. body language

             A string or parenthesized list giving the body language
             value as defined in [LANGUAGE-TAGS].

          4. body location

             A string list giving the body content URI as defined in
             [LOCATION].

        Fzcontent-md5�content-disposition�content-language�content-location)r�r�rrr�r:�r]rurs   rCrIz%_SinglepartMessageStructure._extended~s���2���,�,�)�)���!���
��	�
�
�g�k�k�-�0�1��
�
�d�'�'����4I�(J�K�L��
�
�g�k�k�"4�5�6��
�
�g�k�k�"4�5�6��
rEN)	r�r�r�r�rEr`rGrNrIr?rErCr/r/@s#���
R�H��&
�0	�'rEr/c��eZdZdZd�Zy)r.z_
    L{_TextMessageStructure} represents the message structure of a I{text/*}
    message.
    c��tj|�}|jt|j��|r|j|j
��|S)a
        A body type of type TEXT contains, immediately after the basic
        fields, the size of the body in text lines.  Note that this
        size is the size in its content transfer encoding and not the
        resulting size after any decoding.
        )r/rGrrrr�rzrIrJs   rCrNz_TextMessageStructure.encode�sD��-�9�9�$�?���
�
�l�4�<�<�0�1���M�M�$�.�.�*�+��
rEN�r�r�r�r�rNr?rErCr.r.�s���
rEr.c��eZdZdZd�Zy)r-zi
    L{_RFC822MessageStructure} represents the message structure of a
    I{message/rfc822} message.
    c��tj||�}|jjd�}|j	t|��|j	t
|d��|j	t|��|S)z�
        A body type of type MESSAGE and subtype RFC822 contains,
        immediately after the basic fields, the envelope structure,
        body structure, and size in text lines of the encapsulated
        message.
        rF)r/rNr�r�rrrjr�r)r]rKru�	containeds    rCrNz_RFC822MessageStructure.encode�se��-�3�3�D�(�C���L�L�+�+�A�.�	��
�
�k�)�,�-��
�
�&�y�%�8�9��
�
�l�9�-�.��
rENrSr?rErCr-r-�s���
rEr-c�(�eZdZdZd�Zd�Zd�Zd�Zy)r,zi
    L{_MultipartMessageStructure} represents the message structure of a
    I{multipart/*} message.
    c�@�tj|||�||_y)aR
        @param message: An L{IMessagePart} provider which this structure object
            reports on.

        @param subtype: A L{str} giving the MIME subtype of the message (for
            example, C{"plain"}).

        @param attrs: A C{dict} giving the parameters of the I{Content-Type}
            header of the message.
        N)r4r`r1)r]r�r1r$s    rCr`z#_MultipartMessageStructure.__init__�s��	�"�"�4��%�8���rEc#�zK�d}		|jj|�}|��|dz
}�&#t$rYywxYw�w)zR
        Return an iterator over all of the sub-messages of this message.
        rr<N)r�r�r�)r]rhr�s   rC�	_getPartsz$_MultipartMessageStructure._getParts�sQ����
���
��|�|�.�.�q�1���
��Q������
��
�s�;�,�
;�	8�;�8�;c���|j�D�cgc]}t|�j|���}}|j|j�|r|j|j
��|Scc}w)zW
        Encode each sub-message and added the additional I{multipart} fields.
        )rZr2rNrrr1rzrI)r]rKr�rus    rCrNz!_MultipartMessageStructure.encode�sb��EI�N�N�DT�U�q�&�q�)�0�0��:�U��U��
�
�d�l�l�#���M�M�$�.�.�*�+��
��	Vs�!A4c�f�g}|jjdddd�}|j|j��|j|j	|jd���|j|jdd��|j|jdd��|S)af
        The extension data of a multipart body part are in the following order:

          1. body parameter parenthesized list
               A parenthesized list of attribute/value pairs [e.g., ("foo"
               "bar" "baz" "rag") where "bar" is the value of "foo", and
               "rag" is the value of "baz"] as defined in [MIME-IMB].

          2. body disposition
               A parenthesized list, consisting of a disposition type
               string, followed by a parenthesized list of disposition
               attribute/value pairs as defined in [DISPOSITION].

          3. body language
               A string or parenthesized list giving the body language
               value as defined in [LANGUAGE-TAGS].

          4. body location
               A string list giving the body content URI as defined in
               [LOCATION].
        FrNrOrMN)r�r�rrr>r:r�rPs   rCrIz$_MultipartMessageStructure._extended�s���,���,�,�)�)��%�'9�;P�
��	�
�
�d�)�)�+�,��
�
�d�'�'����4I�(J�K�L��
�
�g�k�k�"4�d�;�<��
�
�g�k�k�"4�d�;�<��
rEN)r�r�r�r�r`rZrNrIr?rErCr,r,�s���
��� rEr,c�6�t|�j|�S)ar
    RFC 3501, 7.4.2, BODYSTRUCTURE::

      A parenthesized list that describes the [MIME-IMB] body structure of a
      message.  This is computed by the server by parsing the [MIME-IMB] header
      fields, defaulting various fields as necessary.

        For example, a simple text message of 48 lines and 2279 octets can have
        a body structure of: ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL
        "7BIT" 2279 48)

    This is represented as::

        ["TEXT", "PLAIN", ["CHARSET", "US-ASCII"], None, None, "7BIT", 2279, 48]

    These basic fields are documented in the RFC as:

      1. body type

         A string giving the content media type name as defined in
         [MIME-IMB].

      2. body subtype

         A string giving the content subtype name as defined in
         [MIME-IMB].

      3. body parameter parenthesized list

         A parenthesized list of attribute/value pairs [e.g., ("foo"
         "bar" "baz" "rag") where "bar" is the value of "foo" and
         "rag" is the value of "baz"] as defined in [MIME-IMB].

      4. body id

         A string giving the content id as defined in [MIME-IMB].

      5. body description

         A string giving the content description as defined in
         [MIME-IMB].

      6. body encoding

         A string giving the content transfer encoding as defined in
         [MIME-IMB].

      7. body size

         A number giving the size of the body in octets.  Note that this size is
         the size in its transfer encoding and not the resulting size after any
         decoding.

    Put another way, the body structure is a list of seven elements.  The
    semantics of the elements of this list are:

       1. Byte string giving the major MIME type
       2. Byte string giving the minor MIME type
       3. A list giving the Content-Type parameters of the message
       4. A byte string giving the content identifier for the message part, or
          None if it has no content identifier.
       5. A byte string giving the content description for the message part, or
          None if it has no content description.
       6. A byte string giving the Content-Encoding of the message body
       7. An integer giving the number of octets in the message body

    The RFC goes on::

        Multiple parts are indicated by parenthesis nesting.  Instead of a body
        type as the first element of the parenthesized list, there is a sequence
        of one or more nested body structures.  The second element of the
        parenthesized list is the multipart subtype (mixed, digest, parallel,
        alternative, etc.).

        For example, a two part message consisting of a text and a
        BASE64-encoded text attachment can have a body structure of: (("TEXT"
        "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23)("TEXT" "PLAIN"
        ("CHARSET" "US-ASCII" "NAME" "cc.diff")
        "<960723163407.20117h@cac.washington.edu>" "Compiler diff" "BASE64" 4554
        73) "MIXED")

    This is represented as::

        [["TEXT", "PLAIN", ["CHARSET", "US-ASCII"], None, None, "7BIT", 1152,
          23],
         ["TEXT", "PLAIN", ["CHARSET", "US-ASCII", "NAME", "cc.diff"],
          "<960723163407.20117h@cac.washington.edu>", "Compiler diff",
          "BASE64", 4554, 73],
         "MIXED"]

    In other words, a list of N + 1 elements, where N is the number of parts in
    the message.  The first N elements are structures as defined by the previous
    section.  The last element is the minor MIME subtype of the multipart
    message.

    Additionally, the RFC describes extension data::

        Extension data follows the multipart subtype.  Extension data is never
        returned with the BODY fetch, but can be returned with a BODYSTRUCTURE
        fetch.  Extension data, if present, MUST be in the defined order.

    The C{extended} flag controls whether extension data might be returned with
    the normal data.
    )r2rN)r�rKs  rCr�r� s��R ��$�+�+�H�5�5rEc��|j�D��cgc]C\}}dj|j�dj|j��f���E}}}dj|�dz}t	|�Scc}}w)Nz: z
)r�r��titler"r;)rr�r�r�s    rCr�r��ss���m�m�o���Q��	
�	�	�1�7�7�9�f�k�k�!�,�,�.�9�:�;��D���;�;�t��v�%�D������s�AA>c#�bK�d}		|j|���|dz
}�#t$rYywxYw�wr�)r�r�)r�rhs  rC�subpartsra�sB����	�A�
���,�,�q�/�!�
��F�A����
��
�s�/� �	,�/�,�/c�b�����ddlm�tj������fd���d��S)a�
    Consume an interator at most a single iteration per reactor iteration.

    If the iterator produces a Deferred, the next iteration will not occur
    until the Deferred fires, otherwise the next iteration will be taken
    in the next reactor iteration.

    @rtype: C{Deferred}
    @return: A deferred which fires (with None) when the iterator is
    exhausted or whose errback is called if there is an exception.
    rr�c���	t��}t|tj�r|j	��y�jd�|�y#t$r�j|�Yyt$r�j�YywxYwr�)
rarXrr�rmr�rdr�rHrH)rcr
r�gorhr�s  ����rCrdziterateInReactor.<locals>.go�sm���
	,��Q��A��!�U�^�^�,��
�
�b�!��!�!�!�R��+���	�
�J�J�t���	�
�I�I�K�	�s�A�B�*B�BN)r�r�rr�)rhrrdr�s`@@@rCrr�s'���)�
����A�,��t�H��HrEc�H�eZdZdZeej�Zdd�Zd�Z	d�Z
y)r��Nc��||_|�tj�}||_|�t}||_|jj|_y)a
        Produce this message.

        @param msg: The message I am to produce.
        @type msg: L{IMessage}

        @param buffer: A buffer to hold the message in.  If None, I will
            use a L{tempfile.TemporaryFile}.
        @type buffer: file-like
        N)r�r�r�r�rrr�)r]r�r�rs    rCr`zMessageProducer.__init__�sH������>��+�+�-�F������(�I�"����[�[�&�&��
rEc�N�||_|j|j��Srt)�consumerr�_produce)r]ris  rCr�zMessageProducer.beginProducing�s�� ��
��~�~�d�m�m�o�.�.rEc#�j�K��jjd�}d}�jj�r�|jd�}|j	d�ddD�cgc]}|j	dd���}}|D��cic]$\}}|j�j
�|��&}}}|jd�}|�/d�j�j��}|dxxd|�d	�z
cc<n'|jd	�r|jd	�r|dd
}t|�}�jt|���jd��jj�r{t�j�D]K}�jd|zdz�t|�j �j"�j%d����M�jd|zd
z�nZ�jj'�}		|	j)�j*�}
|
r �j j|
�d��nn�?�j,r_�j j/dd�t1�j �j%�j,�j3�fd����yycc}wcc}}w�w)NTrrr<r!�boundaryz----=z; boundary="r�r}r=s
--s--
rc����Srtr?r�s �rCrTz*MessageProducer._produce.<locals>.<lambda>s���$�rE)r�r�r�r�rYr6r��_uuid4�hexr�r�r;r�r�rar�r�rr�r�r@�
CHUNK_SIZErir�r�rm)r]rrlr�rSrNr�r�r�rRr+s`          rCrjzMessageProducer._produce�sF������(�(�%�%�d�+�����8�8���!��k�k�.�1�G�.5�m�m�C�.@���.D�E��Q�W�W�S�!�_�E�E�E�8=�>�f�q�!�Q�W�W�Y�_�_�&��)�>�E�>��y�y��,�H���"�4�;�;�=�#4�#4�"5�6����'�\�(��1�+E�E�'��&�&�s�+��0A�0A�#�0F�'��"�~�H�$�X�.�H��
�
�>�'�*�+��
�
�7���8�8���!��d�h�h�'�
���
�
�9�x�/�'�9�:�%�a����d�n�n�E�T�T����
�

�J�J�y�8�+�i�7�8����$�$�&�A���F�F�4�?�?�+����K�K�%�%�a�(��J��
��=�=��K�K���Q��"��t�{�{�+�:�:�4�=�=�I�U�U���
���=F��>�s�AJ3�"J(�;J3�)J-�+HJ3r�)r�r�r�rp�staticmethod�uuid�uuid4rnr`r�rjr?rErCr�r��s$���J�
�$�*�*�
%�F�'�(/�'rEr�c
���eZdZGd�d�ZGd�d�ZGd�d�ZGd�d�ZGd	�d
�ZGd�d�ZGd
�d�Z	Gd�d�Z
Gd�d�ZGd�d�ZGd�d�Z
Gd�d�ZGd�d�ZdZdefdefdefdefd efd!efd"e	fd#e
fd$efg	Zd%�Zd&�Zd'�Zd(�Zd)�Zd*�Zd+�Zd,�Zd-�Zej8d.�Zd/�Zd0�Zd1�Z d2�Z!d3�Z"y)4r�c��eZdZdZd�Zy)�_FetchParser.Enveloperc��y)Nrr?rbs rCrTz_FetchParser.Envelope.<lambda>r�rEN�r�r�r�r�r�r?rErC�Envelopervs����)�rEryc��eZdZdZd�Zy)�_FetchParser.Flagsr�c��y)Nr�r?rbs rCrTz_FetchParser.Flags.<lambda>r�rENrxr?rErC�Flagsr{s����&�rEr}c��eZdZdZd�Zy)�_FetchParser.InternalDaterc��y)Nrr?rbs rCrTz"_FetchParser.InternalDate.<lambda>r�rENrxr?rErC�InternalDaters����-�rEr�c��eZdZdZd�Zy)�_FetchParser.RFC822Headerr(c��y)Nrbr?rbs rCrTz"_FetchParser.RFC822Header.<lambda> r�rENrxr?rErC�RFC822Headerr�s����.�rEr�c��eZdZdZd�Zy)�_FetchParser.RFC822Textr+c��y)Nr`r?rbs rCrTz _FetchParser.RFC822Text.<lambda>$r�rENrxr?rErC�
RFC822Textr�"�����,�rEr�c��eZdZdZd�Zy)�_FetchParser.RFC822Sizer/c��y)Nrar?rbs rCrTz _FetchParser.RFC822Size.<lambda>(r�rENrxr?rErC�
RFC822Sizer�&r�rEr�c��eZdZdZd�Zy)�_FetchParser.RFC822r%c��y)Nr%r?rbs rCrTz_FetchParser.RFC822.<lambda>,r�rENrxr?rErC�RFC822r�*s����'�rEr�c��eZdZdZd�Zy)�_FetchParser.UIDrfc��y)Nrfr?rbs rCrTz_FetchParser.UID.<lambda>0r�rENrxr?rErCr�r�.s����$�rEr�c�H�eZdZdZdZdZdZdZdZdZ	dZ
dZdefd�Z
defd�Zy)�_FetchParser.Bodyr�FNr?r�c�@�|j�jd�S�NrT�r�r�rbs rCr�z_FetchParser.Body.__str__=����>�>�#�*�*�7�3�3rEc	�.�d}d}d}|jrFdj|jD�cgc]}t|dz�jd���!c}�}d}|jr4|d|z|zt|j�jd�zdzz
}nM|j
r|d|z|zdzz
}n2|jr|d|z|zd	zz
}n|jr|d|zdzz
}|j�|d
|j|jfzz
}|Scc}w)Nr<rE�.r<rTr4r[sTEXT]sMIME]s<%d.%d>)
r�r�r�rNr�r6r�r��partialBegin�
partialLength)r]�baser��	separatorrSs     rCr�z_FetchParser.Body.__bytes__@s���D��D��I��y�y��y�y�d�i�i�!P��#�a�!�e�*�"3�"3�G�"<�!P�Q�� �	��{�{���4�K�)�+�c�$�+�+�.>�.E�.E�g�.N�N�QU�U��������t��i�/�(�:�:�������t��i�/�(�:�:�������t��d�*�*��� � �,��
�d�&7�&7��9K�9K�%L�L�L���K��!"Qs�$D)r�r�r�r�rWr�r�r6r�r�r�r�r�r�r�r�r?rErC�Bodyr�2sG�������������������
�	4�S�	4�	�u�	rEr�c��eZdZdZd�Zy)�_FetchParser.BodyStructurer c��y)Nr r?rbs rCrTz#_FetchParser.BodyStructure.<lambda>Yr�rENrxr?rErC�
BodyStructurer�Ws����.�rEr�c�0�eZdZdZdZdZdefd�Zdefd�Z	y)�_FetchParser.HeaderFNr�c�@�|j�jd�Sr�r�rbs rCr�z_FetchParser.Header.__str__ar�rEc��d}|jrz|dz
}|jr|dz
}g}|jD]9}|j�}t|�rt	|�}|j|��;|ddj
|�zdzz
}|jr@dj
|jD�cgc]}|dzj���c}�dz|z}|Scc}w)	NsHEADERs.FIELDSs.NOTr�r�r�r�r<)	r�r�r_r�r|rrr�r�r�)r]r�r�rRrSs     rCr�z_FetchParser.Header.__bytes__ds����D��{�{��
�"���;�;��G�O�D������%�A����	�A�"�1�~�"�1�I���M�M�!�$�	%�
���	�	�&� 1�1�D�8�8���y�y��y�y�t�y�y�!I�!�1�q�5�"3�"3�"5�!I�J�T�Q�TX�X���K��"Js�.C)
r�r�r�r�r�r�r�r�r�r�r?rErC�Headerr�\s)��������	4�S�	4�	�u�	rEr�c��eZdZy)�_FetchParser.TextN�r�r�r�r?rErC�Textr�w���rEr�c��eZdZy)�_FetchParser.MIMENr�r?rErC�MIMEr�zr�rEr�Nsenvelopesflagssinternaldates
rfc822.headersrfc822.textsrfc822.sizesrfc822suids
bodystructurec�0�dg|_g|_d|_y)N�initialrE)r>ru�	remainingrbs rCr`z_FetchParser.__init__�s���[��
������rEc�p�|j|z}	|s|jr\|jstd��|jj�}	t	|d|z�|�}||d}|r�O|jr�\||_y#t
$r|jj
|��wxYw#||_wxYw)NzInvalid Argument�state_)r�r>rrIrNrHrr)r]r�r>�useds    rCr�z_FetchParser.parseString�s����N�N�Q���	��t�z�z��z�z�/�0B�C�C��
�
���(��!�:�7�4��E�)9�:�1�=�D��$�%��A��t�z�z��D�N��%���J�J�%�%�e�,�����D�N�s)�?B,�B�&B,�.B,�&B)�)B,�,	B5c�6�|dk(ry|j�}|jd�rX|jj|j	�|j�|j
�|j�f�y|jd�rg|jj|j	�|j�|j
�|j�|j�f�y|jd�rI|jj|j	�|j�|j
�f�y|jd�r|jjd	�y
|jjd�y)NrErsallrUsfullr�sfastr�)�close_paren�maybe_fetch_att�	fetch_attr<r�)r6r�rurzr}r�r�ryr�r>rr)r]r�r�s   rC�
state_initialz_FetchParser.state_initial�s;��
��8��
�G�G�I���<�<����K�K�������t�0�0�2�D�O�O�4E�t�}�}��W�
���<�<�� ��K�K����J�J�L��%�%�'��O�O�%��M�M�O��I�I�K��
���<�<�� ��K�K����J�J�L��%�%�'��O�O�%��
���<�<����J�J���M�N���
�
���+�&�rEc�<�|jd�rytd��)Nr�r<z	Missing )�r�rPr�s  rC�state_close_parenz_FetchParser.state_close_paren�s���<�<������$�$rEc��|r|ddj�std��d}tt|��D]}|||dzj�r�|S|S)Nrr<zWhitespace expected, none found)�isspacerPr�r�)r]r�rhs   rC�state_whitespacez_FetchParser.state_whitespace�sa����!�A����(��=�>�>�
���s�1�v��	�A��Q��Q��<�'�'�)����	��rEc�\�|jd�s|jjd�y)Nr�)r�r�r�r�r�r>rzr�s  rC�state_maybe_fetch_attz"_FetchParser.state_maybe_fetch_att�s$���|�|�D�!��J�J���L�M�rEc��|j�}|jD]C\}}|j|�s�|jj	|��t|�cS|j
�}|jd�r
d|_d}n"|jd�rd}ntd|����||_	|jjd�|S)Ns	body.peekTrtsbodyr�z!Nothing recognized in fetch_att: )�got_body�
maybe_partial�
maybe_section)r6�_simple_fetch_attr�rurrr�r�rWrP�pending_bodyr>rz)r]r�r�r*�clsr+r�s       rC�state_fetch_attz_FetchParser.state_fetch_att�s���
�G�G�I���/�/�	!�I�D�#��|�|�D�!����"�"�3�5�)��4�y� �	!�

�I�I�K���<�<��%��A�F��D�
�\�\�'�
"��D��?��s�C�D�D�����
�
���H�I��rEc�R�|jj|j�|`yr�)rurrr�r�s  rC�state_got_bodyz_FetchParser.state_got_body�s#�������4�,�,�-���rEc�^�|jd�sy|jjd�y)Nr4r)�section�part_numberr<r�r�s  rC�state_maybe_sectionz _FetchParser.state_maybe_sections'���|�|�D�!���
�
���4�5�rEs(\d+(?:\.\d+)*)\.?c��|jj|�}|�Q|j�djd�D�cgc]}t	|�dz
��c}|_|j
�Sg|_ycc}w)Nrr�r<)�	_partExprr��groupsrYr�rNr_)r]r�r�r�s    rC�state_part_numberz_FetchParser.state_part_number	se���N�N� � ��#���=�./�h�h�j��m�.A�.A�$�.G�H��#�a�&�1�*�H�D�J��5�5�7�N��D�J���	Is�A7c�4�|j�}d}|jd�rd|j_|dz
}�n1|jd�r5|j	�x}|j_d|_d|_|dz
}n�|jd�r%|j�|j_	|d	z
}n�|jd
�r%|j�|j_|d	z
}n|j	�}|jd�r
d|_|dz
}n%|jd
�r|dz
}ntd|����||j_|jjd�t|j �|j_d|_|S)Nrr[Tr<sheader]r?r�stext]r�smime]sheader.fields.not�s
header.fieldsr=zUnhandled section contents: )�finish_section�header_listr�)r6r�r�r�r�r�r�r�r�r6r�r�rPr>rzr�rNr�)r]r�r�r�r�s     rC�
state_sectionz_FetchParser.state_sectionsZ��

�G�G�I�����<�<���&*�D���#��A�I�D�
�\�\�*�
%�+/�;�;�=�8�A��!�!�(��A�H��A�H��A�I�D�
�\�\�(�
#�%)�Y�Y�[�D���"��A�I�D�
�\�\�(�
#�%)�Y�Y�[�D���"��A�I�D����
�A��|�|�0�1������
�����.�/���
���">�q�e� D�E�E�'(�D���$��J�J���M�N�!&�t�z�z�!2�������
��rEc�<�|jd�std��y)Nr[zsection must end with ]r<r�r�s  rC�state_finish_sectionz!_FetchParser.state_finish_section7s���|�|�D�!��5�6�6�rEc�$�|jd�std��|jd�}|dk(rtd��|d|j�}|D�cgc]}|j	���c}|j
j_|dzScc}w)Nr�zHeader list must begin with (r�r}zHeader list must end with )r<)r�rPr�rYr[r�r�r�)r]r�r_rr�s     rC�state_header_listz_FetchParser.state_header_list<s����|�|�D�!��;�<�<��f�f�T�l���"�9��9�:�:��A�c�(�.�.�"��>E�*F��1�7�7�9�*F���� � �'��Q�w���+Gs�B
c�<�|jd�sy|jd�}|dk(rtd��|d|}|jdd�}t	|�dk7rtd	��tt|�\}}||j_||j_	|dzS)
Nr=rr>r}zFound < but not >r<r�r�z>Partial specification did not include two .-delimited integers)
r�r�rPrYr�rkr�r�r�r�)r]r�r_rprN�beginrYs       rC�state_maybe_partialz _FetchParser.state_maybe_partialGs����|�|�D�!���f�f�T�l���"�9��/�0�0��A�c�(���
�
�d�A�&���u�:��?��P��
��C���
��v�).����&�*0����'��Q�w�rE)#r�r�r�ryr}r�r�r�r�r�r�r�r�r�r�r�rNr�r`r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r?rErCr�r�s8��*�*�'�'�.�.�/�/�-�-�-�-�(�(�%�%�#�#�J/�/�
��6
�
�
�
�
�E�
�h��	�5��	�,�'�	�<�(�	��$�	��$�	�F��	��
�	�=�)�
���
�&'�R%���
�2�
���
�
�1�2�I��#�J�
	�rEr�c�8�eZdZdZdZd�Zd�Zd�Zd�Zd�Z	d�Z
y	)
r�rfTc��||_yrtr�)r]rRs  rCr`zFileProducer.__init__as	����rEc��||_|j|_tj�x}|_|jj
|d�|S)NF)rir��producerr��_onDone�registerProducer)r]rirs   rCr�zFileProducer.beginProducingds@�� ��
��~�~��� �>�>�+�+��D�L��
�
�&�&�t�U�3��rEc�|�d}|jrd|j�fz}d|_|jsy||jj|j�z}|sK|j
j
�|jj|�dx|_x|_|_y|j|�y)NrEs{%d}
F)
�
firstWrite�_sizerRr@rpri�unregisterProducerr�r�r�)r]r+s  rC�resumeProducingzFileProducer.resumeProducingks������?�?��t�z�z�|�o�-�A�#�D�O��v�v��
������D�O�O�,�,����M�M�,�,�.��L�L�!�!�$�'�48�8�D�L�8�4�6�D�M��L�L��OrEc��y)z9
        Pause the producer.  This does nothing.
        Nr?rbs rC�pauseProducingzFileProducer.pauseProducingzr�rEc��y)z8
        Stop the producer.  This does nothing.
        Nr?rbs rC�
stopProducingzFileProducer.stopProducingr�rEc���|jj�}|jjdd�|jj�}|jj|d�||z
S)Nrr�)rRr�r�)r]r+rSs   rCr�zFileProducer._size�sM���F�F�K�K�M�������A�q���F�F�K�K�M�������A�q���1�u�rEN)r�r�r�rpr�r`r�r�r�r�r�r?rErCr�r�\s*���J��J���
��
�
rEr�c��gd�}dddd�}tjd|z|�}|std|����|j�}	d|j	|d	j��d
zz|d	<t
|d�|d<t
|d�|d<tj|d|d	|dd
d
d
dddf	�S#t$rtd|����wxYw)N)�jan�feb�mar�apr�may�jun�jul�aug�sep�oct�nov�dec�january�february�march�aprilr��june�july�august�	september�october�november�decemberz+(?P<day>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])z(?P<mon>\w+)z(?P<year>\d\d\d\d))�day�mon�yearz%(day)s-%(mon)s-%(year)szCannot parse time string r<r�rrrr})	r�r�re�	groupdict�indexr6r�rx�struct_time)r��months�exprr�rs     rCr�r��s����F�6>��%��D�
	���+�d�2�A�6�A���4�Q�E�:�;�;�	���
�A�V�����Q�u�X�^�^�%5�6��;�<��%����&�	�N��&�	��q��x�=��%������6��A�e�H�a��h��1�a��R�QS� T�U�U���<��4�Q�E�:�;�;�<�s
�A
B9�9Cr	c��|dSr�r?rRs rCrTrT�s
��Q�q�T�rEc�N�|jd�}|ddjdd�S)N�utf-7r<r}r�ry)rNr��r��s_utf7s  rC�modified_base64r�s)��
�X�X�g�
�F��!�B�<����d�+�+rEc�T�d|jdd�zdz}|jd�S)Nr�ryr�rur)r�r�rs  rC�modified_unbase64r�s+��
�A�I�I�d�D�)�
)�D�
0�F��=�=��!�!rEc	�"�t�}g}tttt	dd���dhz
}|D]�}||vrE|r(|dtdj
|��zdzz
}|dd�=|jt|���L|dk(r0|r(|dtdj
|��zdzz
}|dd�=|dz
}��|j|���|r/|jdtdj
|��zdz�t|�t|�fS)	ap
    Encode the given C{unicode} string using the IMAP4 specific variation of
    UTF-7.

    @type s: C{unicode}
    @param s: The text to encode.

    @param errors: Policy for handling encoding errors.  Currently ignored.

    @return: L{tuple} of a L{str} giving the encoded bytes and an L{int}
        giving the number of code units consumed from the input.
    � ��&�&r�ruNs&-)�	bytearrayr�rk�chrr�rr�rr�ordrzr�r�)r��errorsr
�_in�valid_charsr�s      rC�encoderr*�s��	��A�
�C��c�#�u�T�4�0�1�2�c�U�:�K�
��������T�O�B�G�G�C�L�9�9�D�@�@����F�
�H�H�S��V��
�#�X���T�O�B�G�G�C�L�9�9�D�@�@����F�
��J�A��J�J�q�M���	����������5�5��<�=��!�H�c�!�f��rEc
��g}g}tt|�d�}|D]�}|dk(r|s|jd��|dk(rQ|rOt|�dk(r|jd�n,|jt	dj|dd���g}�r|r|j|���|j|j
����|r,|jt	dj|dd���dj|�t|�fS)	aw
    Decode the given L{str} using the IMAP4 specific variation of UTF-7.

    @type s: L{str}
    @param s: The bytes to decode.

    @param errors: Policy for handling decoding errors.  Currently ignored.

    @return: a L{tuple} of a C{unicode} string giving the text which was
        decoded and an L{int} giving the number of bytes consumed from the
        input.
    r�r#rur<r"rENr�)�memory_cast�
memoryviewrrr�rr�r�)r�r'r
r�r�s     rC�decoderr.�s���	�A�
�F��J�q�M�3�'�A�
�!����9�V��M�M�$��
�$�Y�6��6�{�a������
����*�3�8�8�F�1�2�J�+?�@�A��F�
��M�M�!��
�H�H�Q�X�X�Z� �!��	���"�3�8�8�F�1�2�J�#7�8�9��G�G�A�J��A���rEc��eZdZdd�Zy)�StreamReaderc��t|�Srt)r.�r]r�r's   rCr�zStreamReader.decode����q�z�rEN��strict)r�r�r�r�r?rErCr0r0���rEr0c��eZdZdd�Zy)�StreamWriterc��t|�Srt)r*r2s   rCrNzStreamWriter.encoder3rENr4)r�r�r�rNr?rErCr8r8r6rEr8c�:�|jdd�dk(rtSy)N�-rj�imap4_utf_7)r��
_codecInfor�s rCr<r<s"���|�|�C����.���/rE)(rr)�IMailboxListenerr(r��IMailboxr1r:�IMailboxInfo�IMessager�r�r��IMessagePartrrrrr&r"r$rrrr r!rrr#r%rrrrrr�r�r�r�rrtr�rvr�)�r�r��codecsr
�email.utilsr�ror�r�r�rxrr�base64rr�ior�	itertoolsr�typingrrr	�zope.interfacer
�twisted.credr�twisted.cred.errorrr
r�rrr�twisted.internet.deferr�twisted.mail._credrrrrr�twisted.mail._exceptrrrrrrrrrr r!r"r#r$r%r&�twisted.mail.interfacesr'r�r(r)r:r*r?r+r@r,r>r-rAr.r�r/r�r0rBr1r2r��twisted.protocolsr3r4�twisted.pythonr5r6�twisted.python.compatr7r8r9r:r;r#r�r�rYrzrDrJrLr�r�r�r��_SPr��_CTLrr��_nativeNonAtomCharsr�r�r��LineReceiver�TimeoutMixinr�TimeoutErrorrPr)r�r�r�rnr�r�r�r�r�r�r�r�r�r|r�rjr�r�r&r�r�rr�r�r	rrrjrrr)r2r4r/r.r-r,r�r�rarr�r�r�r�rNr-r,rrr*r.r0r8�	CodecInfor=r<�register�__all__r?rErC�<module>r\s��
��
����	�
����+���"�"�&�$�F�5�5�0��������(
�
�
�
�.�$������a���G�M�M�O�P���
*��(a�a�H	9�9�8/�/�B��0D"�D"�V���U�5��;��d�E� 2�
3�4����$�t�+�
�$�*�*�9�5��
�R�Z�Z��1�1�C�7�
8�
��H��e��H�
H�
�
�
��TN�%�$�$�h�&;�&;�TN��TN�n8#��"�"�$�
�
�
��P�%�$�$�h�&;�&;�P��P�f<6�r��"*�
�
��N�b	1��
&�>�B
� �D7,�tE�'��'�%�'�����
�+��/�. �b
�X��oJ�oJ��oJ�d
�
 �!��2��"��&"���#����
�M��4�
��,J�4/�/�de�"3�e�P�7��(�9��*L�!2�L�^i6�X�
�
�BC�C�LR�R�j
-�-�`,V�d�j�&�/�:��,�
"�
�D �D�6�&�&��
�6�&�&��
�V�
�
�g�w��l�
K�
�	�������/�rE

Zerion Mini Shell 1.0