AlkantarClanX12

Your IP : 18.223.210.249


Current Path : /lib64/python3.8/__pycache__/
Upload File :
Current File : //lib64/python3.8/__pycache__/imaplib.cpython-38.pyc

U

e5df��*@s�dZdZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZmZm
Z
ddlmZzddlZdZWnek
r�dZYnXdd	d
ddd
gZdZdZdZdZdZdZdddddddddddddddddddddddddddddddddddddddddd�)Ze�d�Ze�d�Ze�d�Ze�dej�Z e�d�Z!e�d�Z"e�d�Z#e�d ej�Z$dZ%d Z&Gd!d�d�Z'e�r�Gd"d#�d#e'�Z(e�)d#�Gd$d	�d	e'�Z*Gd%d&�d&�Z+d'�,d(�Z-d)d*�e.e-d+d��D�Z/d,d
�Z0d-d�Z1d.d�Z2d/d
�Z3e4d0k�r�ddl5Z5ddl6Z6ze5�5ej7d+d�d1�\Z8Z9Wn.e5j:k
�rpZ;zd2\Z8Z9W5dZ;[;XYnXdZ<e8D]8\Z=Z;e=d3k�r�e>e;�Zne=d4k�rze;Z<e9�sze<fZ9�qze9�s�d5Z9e9dZ?e6�@�ZAe6�6d6eAe?�p�d7f�ZBd8eAd9d:�ZCd;eAeBffd<d=d>d?d@ddeCffdAdBdCdDdEdFdGdHdIfZDdJdKdLdMd?dddeCffdHdNfZEdOdP�ZF�ze<�rbe*e<�ZGne'e?�ZGeGjHdQk�r�eDd+d�ZDeG�IdReGjJ�eG�IdSeGjKf�eDD]\ZLZ9eFeLe9��q�eFdTdU�D]<ZMe�NdVeM�ZOeO�r�eO�Pd+�ZQneM�,�dWZQeFdXeQf��q�eED]T\ZLZ9eFeLe9�ZReLe9fdLk�r.�q
eRdW�,�ZSeS�sD�q
eFdYdZd[eSdWd\f��q
eTd]�Wn.eTd^�e�s�eTd_ej7d��YnXdS)`z�IMAP4 client.

Based on RFC 2060.

Public class:           IMAP4
Public variable:        Debug
Public functions:       Internaldate2tuple
                        Int2AP
                        ParseFlags
                        Time2Internaldate
z2.58�N)�datetime�timezone�	timedelta)�DEFAULT_BUFFER_SIZETF�IMAP4�IMAP4_stream�Internaldate2tuple�Int2AP�
ParseFlags�Time2Internaldate�
�i�)Z	IMAP4REV1ri@B)�AUTH�SELECTED)�NONAUTH)rrr�LOGOUT)r)r))�APPEND�AUTHENTICATE�
CAPABILITY�CHECK�CLOSE�COPY�CREATE�DELETE�	DELETEACL�ENABLE�EXAMINE�EXPUNGE�FETCH�GETACL�
GETANNOTATION�GETQUOTA�GETQUOTAROOT�MYRIGHTS�LIST�LOGINr�LSUBZMOVE�	NAMESPACE�NOOP�PARTIAL�	PROXYAUTH�RENAME�SEARCH�SELECT�SETACL�
SETANNOTATION�SETQUOTA�SORT�STARTTLS�STATUS�STORE�	SUBSCRIBE�THREAD�UID�UNSUBSCRIBEs\+( (?P<data>.*))?s.*FLAGS \((?P<flags>[^\)]*)\)s�.*INTERNALDATE "(?P<day>[ 0123][0-9])-(?P<mon>[A-Z][a-z][a-z])-(?P<year>[0-9][0-9][0-9][0-9]) (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9]) (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])"s.*{(?P<size>\d+)}$s
\r\n|\r|\ns%\[(?P<type>[A-Z-]+)( (?P<data>.*))?\]s$\* (?P<type>[A-Z-]+)( (?P<data>.*))?s3\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?c@s�eZdZdZGdd�de�ZGdd�de�ZGdd�de�Zdefd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdefdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Z d9d:�Z!d;d<�Z"d=d>�Z#d?d@�Z$dAdB�Z%dCdD�Z&dEdF�Z'dGdH�Z(d�dKdL�Z)dMdN�Z*dOdP�Z+dQdR�Z,dSdT�Z-d�dUdV�Z.dWdX�Z/dYdZ�Z0d[d\�Z1d]d^�Z2d_d`�Z3dadb�Z4dcdd�Z5d�dgdh�Z6didj�Z7dkdl�Z8dmdn�Z9dodp�Z:d�drds�Z;dtdu�Z<dvdw�Z=dxdy�Z>dzd{�Z?d|d}�Z@d~d�ZAd�d��ZBd�d��ZCd�d��ZDd�d��ZEd�d��ZFd�d��ZGd�d��ZHd�d�d��ZId�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d��ZOd�d�d��ZPd�d��ZQd�d��ZRd�d��ZSdqS)�ra�IMAP4 client class.

    Instantiate with: IMAP4([host[, port]])

            host - host's name (default: localhost);
            port - port number (default: standard IMAP4 port).

    All IMAP4rev1 commands are supported by methods of the same
    name (in lower-case).

    All arguments to commands are converted to strings, except for
    AUTHENTICATE, and the last argument to APPEND which is passed as
    an IMAP4 literal.  If necessary (the string contains any
    non-printing characters or white-space and isn't enclosed with
    either parentheses or double quotes) each string is quoted.
    However, the 'password' argument to the LOGIN command is always
    quoted.  If you want to avoid having an argument string quoted
    (eg: the 'flags' argument to STORE) then enclose the string in
    parentheses (eg: "(\Deleted)").

    Each command returns a tuple: (type, [data, ...]) where 'type'
    is usually 'OK' or 'NO', and 'data' is either the text from the
    tagged response, or untagged results from command. Each 'data'
    is either a string, or a tuple. If a tuple, then the first part
    is the header of the response, and the second part contains
    the data (ie: 'literal' value).

    Errors raise the exception class <instance>.error("<reason>").
    IMAP4 server errors raise <instance>.abort("<reason>"),
    which is a sub-class of 'error'. Mailbox status changes
    from READ-WRITE to READ-ONLY raise the exception class
    <instance>.readonly("<reason>"), which is a sub-class of 'abort'.

    "error" exceptions imply a program error.
    "abort" exceptions imply the connection should be reset, and
            the command re-tried.
    "readonly" exceptions imply the command should be re-tried.

    Note: to use this module, you must read the RFCs pertaining to the
    IMAP4 protocol, as the semantics of the arguments to each IMAP4
    command are left to the invoker, not to mention the results. Also,
    most IMAP servers implement a sub-set of the commands available here.
    c@seZdZdS)zIMAP4.errorN��__name__�
__module__�__qualname__�r=r=�/usr/lib64/python3.8/imaplib.py�error�sr?c@seZdZdS)zIMAP4.abortNr9r=r=r=r>�abort�sr@c@seZdZdS)zIMAP4.readonlyNr9r=r=r=r>�readonly�srA�cCs�t|_d|_d|_i|_i|_d|_d|_d|_d|_	|�
�|�||�z|��Wn8t
k
r�z|��Wntk
r�YnX�YnXdS)NrrBFr)�Debug�debug�state�literal�tagged_commands�untagged_responses�continuation_response�is_readonly�tagnum�_tls_established�_mode_ascii�open�_connect�	Exception�shutdown�OSError��self�host�portr=r=r>�__init__�s&zIMAP4.__init__cCs0d|_d|_t�ttj�|_t�ttj�|_dS)NF�ascii)	�utf8_enabled�	_encoding�re�compile�_Literal�ASCII�Literal�_Untagged_status�Untagged_status�rTr=r=r>rM�szIMAP4._mode_asciicCs(d|_d|_t�t�|_t�t�|_dS)NT�utf-8)rYrZr[r\r]r_r`rarbr=r=r>�
_mode_utf8�szIMAP4._mode_utf8cCs�tt�dd��|_t�d|jdtj�|_d|_d|_	i|_
|jdkrf|�dt
�|�d	|j�|��|_d
|jkr�d|_nd|jkr�d
|_n|�|j��|��|jdkr�|�d|jf�tD]}||jkr�q�||_dS|�d��dS)Nii��s(?P<tag>s"\d+) (?P<type>[A-Z]+) (?P<data>.*)�
r�zimaplib version %sznew IMAP4 connection, tag=%sZPREAUTHr�OKr�zCAPABILITIES: %rzserver not IMAP4 compliant)r	�randomZrandint�tagprer[r\r^�tagre�_cmd_log_len�_cmd_log_idx�_cmd_logrD�_mesg�__version__�
_get_responseZwelcomerHrEr?�_get_capabilities�capabilities�AllowedVersions�PROTOCOL_VERSION)rT�versionr=r=r>rO�s:���





zIMAP4._connectcCs&|tkrt||���Std|��dS)NzUnknown IMAP4 command: '%s')�Commands�getattr�lower�AttributeError)rT�attrr=r=r>�__getattr__	szIMAP4.__getattr__cCs|S�Nr=rbr=r=r>�	__enter__szIMAP4.__enter__cGs4|jdkrdSz|��Wntk
r.YnXdS)Nr)rE�logoutrR)rT�argsr=r=r>�__exit__s
zIMAP4.__exit__cCs4|js
dn|j}t�d||j|j�t�||jf�S)Nzimaplib.open)rU�sys�auditrV�socketZcreate_connection)rTrUr=r=r>�_create_socketszIMAP4._create_socketcCs(||_||_|��|_|j�d�|_dS)z�Setup connection to remote server on "host:port"
            (default: localhost:standard IMAP4 port).
        This connection will be used by the routines:
            read, readline, send, shutdown.
        �rbN)rUrVr��sock�makefile�filerSr=r=r>rN's
z
IMAP4.opencCs|j�|�S�zRead 'size' bytes from remote.)r��read�rT�sizer=r=r>r�3sz
IMAP4.readcCs.|j�td�}t|�tkr*|�dt��|S)�Read line from remote.rfzgot more than %d bytes)r��readline�_MAXLINE�lenr?�rT�liner=r=r>r�8szIMAP4.readlinecCst�d||�|j�|�dS)�Send data to remote.zimaplib.sendN)r�r�r�Zsendall�rT�datar=r=r>�send@sz
IMAP4.sendc
Cst|j��zXz|j�tj�Wn@tk
r^}z"|jtjkrNt	|dd�dkrN�W5d}~XYnXW5|j��XdS)� Close I/O established in "open".Zwinerrorri&'N)
r��closer�rQr�Z	SHUT_RDWRrR�errnoZENOTCONNrx)rT�excr=r=r>rQFs
�zIMAP4.shutdowncCs|jS)zfReturn socket instance used to connect to IMAP4 server.

        socket = <instance>.socket()
        )r�rbr=r=r>r�VszIMAP4.socketcCsBd}|�ddg|�\}}|dr(||fS|��\}}|�|||�S)aReturn most recent 'RECENT' responses if any exist,
        else prompt server for an update using the 'NOOP' command.

        (typ, [data]) = <instance>.recent()

        'data' is None if no new messages,
        else list of RECENT responses, most recent last.
        ZRECENTrgN���)�_untagged_response�noop�rT�name�typ�datr=r=r>�recentbs	zIMAP4.recentcCs|�|dg|���S)z�Return data for response 'code' if received, or None.

        Old value for response 'code' is cleared.

        (code, [data]) = <instance>.response(code)
        N)r��upper)rT�coder=r=r>�responsesszIMAP4.responsecCsxd}|sd}|r.|d|dfdkr2d|}nd}|r@t|�}nd}t�t|�}|jrbd|d	}||_|�||||�S)
z�Append message to named mailbox.

        (typ, [data]) = <instance>.append(mailbox, flags, date_time, message)

                All args except `message' can be None.
        r�INBOXrr���(�)�(%s)NsUTF8 (�))r�MapCRLF�sub�CRLFrYrF�_simple_command)rT�mailbox�flags�	date_time�messager�rFr=r=r>�append�s

zIMAP4.appendcCsP|��}t|�j|_|�d|�\}}|dkrB|�|d�dd���d|_||fS)asAuthenticate command - requires response processing.

        'mechanism' specifies which authentication mechanism is to
        be used - it must appear in <instance>.capabilities in the
        form AUTH=<mechanism>.

        'authobject' must be a callable object:

                data = authobject(response)

        It will be called to process server continuation responses; the
        response argument it is passed will be a bytes.  It should return bytes
        data that will be base64 encoded and sent to the server.  It should
        return None if the client abort response '*' should be sent instead.
        rrgr�rc�replacer)r��_Authenticator�processrFr�r?�decoderE)rTZ	mechanismZ
authobject�mechr�r�r=r=r>�authenticate�szIMAP4.authenticatecCs d}|�|�\}}|�|||�S)zT(typ, [data]) = <instance>.capability()
        Fetch capabilities list from server.r�r�r�r�r=r=r>�
capability�szIMAP4.capabilitycCs
|�d�S)zRCheckpoint mailbox on server.

        (typ, [data]) = <instance>.check()
        r�r�rbr=r=r>�check�szIMAP4.checkcCs$z|�d�\}}W5d|_X||fS)z�Close currently selected mailbox.

        Deleted messages are removed from writable mailbox.
        This is the recommended command before 'LOGOUT'.

        (typ, [data]) = <instance>.close()
        rr)rEr��rTr�r�r=r=r>r��szIMAP4.closecCs|�d||�S)z�Copy 'message_set' messages onto end of 'new_mailbox'.

        (typ, [data]) = <instance>.copy(message_set, new_mailbox)
        rr�)rT�message_setZnew_mailboxr=r=r>�copy�sz
IMAP4.copycCs|�d|�S)zPCreate new mailbox.

        (typ, [data]) = <instance>.create(mailbox)
        rr��rTr�r=r=r>�create�szIMAP4.createcCs|�d|�S)zPDelete old mailbox.

        (typ, [data]) = <instance>.delete(mailbox)
        rr�r�r=r=r>�delete�szIMAP4.deletecCs|�d||�S)z�Delete the ACLs (remove any rights) set for who on mailbox.

        (typ, [data]) = <instance>.deleteacl(mailbox, who)
        rr�)rTr��whor=r=r>�	deleteacl�szIMAP4.deleteaclcCsHd|jkrt�d��|�d|�\}}|dkr@d|��kr@|��||fS)zkSend an RFC5161 enable string to the server.

        (typ, [data]) = <intance>.enable(capability)
        rzServer does not support ENABLErgzUTF8=ACCEPT)rsrr?r�r�rd)rTr�r�r�r=r=r>�enable�s

zIMAP4.enablecCs d}|�|�\}}|�|||�S)z�Permanently remove deleted items from selected mailbox.

        Generates 'EXPUNGE' response for each deleted message.

        (typ, [data]) = <instance>.expunge()

        'data' is list of 'EXPUNGE'd message numbers in order received.
        rr�r�r=r=r>�expunges	z
IMAP4.expungecCs$d}|�|||�\}}|�|||�S)a#Fetch (parts of) messages.

        (typ, [data, ...]) = <instance>.fetch(message_set, message_parts)

        'message_parts' should be a string of selected parts
        enclosed in parentheses, eg: "(UID BODY[TEXT])".

        'data' are tuples of message part envelope and data.
        rr�)rTr�Z
message_partsr�r�r�r=r=r>�fetchs
zIMAP4.fetchcCs|�d|�\}}|�||d�S)zXGet the ACLs for a mailbox.

        (typ, [data]) = <instance>.getacl(mailbox)
        rZACLr��rTr�r�r�r=r=r>�getaclszIMAP4.getaclcCs"|�d|||�\}}|�||d�S)za(typ, [data]) = <instance>.getannotation(mailbox, entry, attribute)
        Retrieve ANNOTATIONs.r �
ANNOTATIONr�)rTr��entryZ	attributer�r�r=r=r>�
getannotation(szIMAP4.getannotationcCs|�d|�\}}|�||d�S)z�Get the quota root's resource usage and limits.

        Part of the IMAP4 QUOTA extension defined in rfc2087.

        (typ, [data]) = <instance>.getquota(root)
        r!�QUOTAr�)rT�rootr�r�r=r=r>�getquota0szIMAP4.getquotacCs@|�d|�\}}|�||d�\}}|�||d�\}}|||gfS)z�Get the list of quota roots for the named mailbox.

        (typ, [[QUOTAROOT responses...], [QUOTA responses]]) = <instance>.getquotaroot(mailbox)
        r"r�Z	QUOTAROOTr�)rTr�r�r�ZquotaZ	quotarootr=r=r>�getquotaroot;szIMAP4.getquotaroot�""�*cCs$d}|�|||�\}}|�|||�S)z�List mailbox names in directory matching pattern.

        (typ, [data]) = <instance>.list(directory='""', pattern='*')

        'data' is list of LIST responses.
        r$r��rTZ	directory�patternr�r�r�r=r=r>�listFsz
IMAP4.listcCs<|�d||�|��\}}|dkr.|�|d��d|_||fS)z�Identify client using plaintext password.

        (typ, [data]) = <instance>.login(user, password)

        NB: 'password' will be quoted.
        r%rgr�r)r��_quoter?rE)rT�user�passwordr�r�r=r=r>�loginRs
zIMAP4.logincCs|||_|_|�d|j�S)zr Force use of CRAM-MD5 authentication.

        (typ, [data]) = <instance>.login_cram_md5(user, password)
        zCRAM-MD5)r�r�r��_CRAM_MD5_AUTH)rTr�r�r=r=r>�login_cram_md5`szIMAP4.login_cram_md5cCsBddl}t|jt�r |j�d�n|j}|jd|�||d���S)z1 Authobject to use with CRAM-MD5 authentication. rNrc� Zmd5)�hmac�
isinstancer��str�encoder�ZHMACZ	hexdigest)rTZ	challenger��pwdr=r=r>r�is
�zIMAP4._CRAM_MD5_AUTHcCs$d|_|�d�\}}|��||fS)z|Shutdown connection to server.

        (typ, [data]) = <instance>.logout()

        Returns server 'BYE' response.
        r)rEr�rQr�r=r=r>rqszIMAP4.logoutcCs$d}|�|||�\}}|�|||�S)z�List 'subscribed' mailbox names in directory matching pattern.

        (typ, [data, ...]) = <instance>.lsub(directory='""', pattern='*')

        'data' are tuples of message part envelope and data.
        r&r�r�r=r=r>�lsub~sz
IMAP4.lsubcCs|�d|�\}}|�||d�S)z�Show my ACLs for a mailbox (i.e. the rights that I have on mailbox).

        (typ, [data]) = <instance>.myrights(mailbox)
        r#r�r�r=r=r>�myrights�szIMAP4.myrightscCs d}|�|�\}}|�|||�S)zb Returns IMAP namespaces ala rfc2342

        (typ, [data, ...]) = <instance>.namespace()
        r'r�r�r=r=r>�	namespace�szIMAP4.namespacecCs |jdkr|�|j�|�d�S)zFSend NOOP command.

        (typ, [data]) = <instance>.noop()
        rhr()rD�_dump_urrHr�rbr=r=r>r��s
z
IMAP4.noopcCs(d}|�|||||�\}}|�||d�S)z�Fetch truncated part of a message.

        (typ, [data, ...]) = <instance>.partial(message_num, message_part, start, length)

        'data' is tuple of message part envelope and data.
        r)rr�)rTZmessage_numZmessage_part�startZlengthr�r�r�r=r=r>�partial�sz
IMAP4.partialcCsd}|�d|�S)z�Assume authentication as "user".

        Allows an authorised administrator to proxy into any user's
        mailbox.

        (typ, [data]) = <instance>.proxyauth(user)
        r*r�)rTr�r�r=r=r>�	proxyauth�s	zIMAP4.proxyauthcCs|�d||�S)zkRename old mailbox name to new.

        (typ, [data]) = <instance>.rename(oldmailbox, newmailbox)
        r+r�)rTZ
oldmailboxZ
newmailboxr=r=r>�rename�szIMAP4.renamecGsTd}|r2|jrt�d��|j|d|f|��\}}n|j|f|��\}}|�|||�S)z�Search mailbox for matching messages.

        (typ, [data]) = <instance>.search(charset, criterion, ...)

        'data' is space separated list of matching message numbers.
        If UTF8 is enabled, charset MUST be None.
        r,z'Non-None charset not valid in UTF8 mode�CHARSET)rYrr?r�r�)rT�charsetZcriteriar�r�r�r=r=r>�search�s
zIMAP4.searchr�FcCs�i|_||_|rd}nd}|�||�\}}|dkr@d|_||fSd|_d|jkrx|sx|jdkrj|�|j�|�d|��||j�d	d
g�fS)atSelect a mailbox.

        Flush all untagged responses.

        (typ, [data]) = <instance>.select(mailbox='INBOX', readonly=False)

        'data' is count of messages in mailbox ('EXISTS' response).

        Mandated responses are ('FLAGS', 'EXISTS', 'RECENT', 'UIDVALIDITY'), so
        other responses should be obtained via <instance>.response('FLAGS') etc.
        rr-rgrr�	READ-ONLYrfz%s is not writable�EXISTSN)rHrJr�rErDr�rA�get)rTr�rAr�r�r�r=r=r>�select�s"
�
zIMAP4.selectcCs|�d|||�S)zZSet a mailbox acl.

        (typ, [data]) = <instance>.setacl(mailbox, who, what)
        r.r�)rTr�r�Zwhatr=r=r>�setacl�szIMAP4.setaclcGs |jd|��\}}|�||d�S)z_(typ, [data]) = <instance>.setannotation(mailbox[, entry, attribute]+)
        Set ANNOTATIONs.r/r�)r/r�)rTr�r�r�r=r=r>�
setannotationszIMAP4.setannotationcCs |�d||�\}}|�||d�S)ziSet the quota root's resource limits.

        (typ, [data]) = <instance>.setquota(root, limits)
        r0r�r�)rTr�Zlimitsr�r�r=r=r>�setquota	szIMAP4.setquotacGsFd}|d|dfdkr d|}|j|||f|��\}}|�|||�S)z�IMAP4rev1 extension SORT command.

        (typ, [data]) = <instance>.sort(sort_criteria, charset, search_criteria, ...)
        r1rr�r�r�r�)rTZ
sort_criteriar��search_criteriar�r�r�r=r=r>�sorts
z
IMAP4.sortNcCs�d}ts|�d��|jr"|�d��||jkr6|�d��|dkrFt��}|�|�\}}|dkr�|j|j	|j
d�|_	|j	�d�|_d|_|�
�n
|�d	��|�|||�S)
Nr2zSSL support missingzTLS session already establishedzTLS not supported by serverrg�Zserver_hostnamer�TzCouldn't establish TLS session)�HAVE_SSLr?rLr@rs�ssl�_create_stdlib_contextr��wrap_socketr�rUr�r�rrr�)rT�ssl_contextr�r�r�r=r=r>�starttls s&



�

zIMAP4.starttlscCs$d}|�|||�\}}|�|||�S)zpRequest named status conditions for mailbox.

        (typ, [data]) = <instance>.status(mailbox, names)
        r3r�)rTr��namesr�r�r�r=r=r>�status7szIMAP4.statuscCs>|d|dfdkrd|}|�d|||�\}}|�||d�S)z�Alters flag dispositions for messages in mailbox.

        (typ, [data]) = <instance>.store(message_set, command, flags)
        rr�r�r�r4rr�)rTr��commandr�r�r�r=r=r>�storeCszIMAP4.storecCs|�d|�S)zYSubscribe to new mailbox.

        (typ, [data]) = <instance>.subscribe(mailbox)
        r5r�r�r=r=r>�	subscribeNszIMAP4.subscribecGs*d}|j|||f|��\}}|�|||�S)z�IMAPrev1 extension THREAD command.

        (type, [data]) = <instance>.thread(threading_algorithm, charset, search_criteria, ...)
        r6r�)rTZthreading_algorithmr�r�r�r�r�r=r=r>�threadVszIMAP4.threadc	Gs�|��}|tkr|�d|��|jt|krL|�d||jd�t|�f��d}|j||f|��\}}|dkrt|}nd}|�|||�S)z�Execute "command arg ..." with messages identified by UID,
                rather than message number.

        (typ, [data]) = <instance>.uid(command, arg1, arg2, ...)

        Returns response appropriate to 'command'.
        zUnknown IMAP4 UID command: %s�9command %s illegal in state %s, only allowed in states %s�, r7)r,r1r6r)r�rwr?rE�joinr�r�)rTrr�r�r�r�r=r=r>�uid`s��z	IMAP4.uidcCs|�d|�S)z_Unsubscribe from old mailbox.

        (typ, [data]) = <instance>.unsubscribe(mailbox)
        r8r�r�r=r=r>�unsubscribeyszIMAP4.unsubscribecGs,|��}|tkr|jft|<|j|f|��S)aAllow simple extension commands
                notified by server in CAPABILITY response.

        Assumes command is legal in current state.

        (typ, [data]) = <instance>.xatom(name, arg, ...)

        Returns response appropriate to extension command `name'.
        )r�rwrEr��rTr�r�r=r=r>�xatom�s
zIMAP4.xatomc	Csb|dkrd}|j}|jdkr<|�d|t|�|d��|f�||krT||�|�n
|g||<dS)N��z#untagged_responses[%s] %s += ["%r"]rB)rHrDror�r�r�)rTr�r�Zurr=r=r>�_append_untagged�s
�zIMAP4._append_untaggedcCs,|j�d�}|r(|�|d�|jd���dS)N�BYEr�r�)rHr�r@r�rZ)rT�byer=r=r>�
_check_bye�szIMAP4._check_byec

Gs2|jt|kr4d|_|�d||jd�t|�f��dD]}||jkr8|j|=q8d|jkrj|jsj|�d��|��}t	||j
�}|d|}|D]0}|dkr�q�t|t�r�t	||j
�}|d|}q�|j}|dk	�r
d|_t
|�t
|j�kr�|}nd}|t	dt|�|j
�}|jdk�r&|�d	|�n|�d	|�z|�|t�Wn2tk
�rx}	z|�d
|	��W5d}	~	XYnX|dk�r�|S|���r�|j|�r�|S�q�|�r�||j�}|jdk�r�|�dt|��z|�|�|�t�Wn2tk
�r}	z|�d
|	��W5d}	~	XYnX|�s��q.�q�|S)Nrr�rg�NO�BADr�z#mailbox status changed to READ-ONLY� z {%s}�z> %rzsocket error: %szwrite literal size %s)rErwrFr?rrHrJrA�_new_tag�bytesrZr�r��type�_commandr�rDro�_logr�r�rRr@rqrGrI)
rTr�r�r��tagr��argrFZ	literator�valr=r=r>r�sl��


�


 



 zIMAP4._commandc
Cs�|dk}|s|��z|j||d�\}}Wnj|jk
r`}z|�d||f��W5d}~XYn6|jk
r�}z|�d||f��W5d}~XYnX|s�|��|dkr�|�d|||f��||fS)Nr)�
expect_byezcommand: %s => %srz%s command error: %s %s)r�_get_tagged_responser@r?)rTr�rrr�r�rr=r=r>�_command_complete�s"$zIMAP4._command_completecCsJ|��\}}|dgkr |�d��t|d|j�}|��}t|���|_dS)Nz"no CAPABILITY response from serverr�)r�r?r�rZr��tuple�splitrsr�r=r=r>rr
s

zIMAP4._get_capabilitiescCs�|��}|�|j|�rr|j�d�}||jkr:|�d|��|j�d�}t||j�}|j�d�}||gf|j|<�nd}|�t	|�s�|�|j
|�r�|j�d�}|jdkr�|�t|�r�|j�d�|_dS|�d|��|j�d�}t||j�}|j�d�}|dk�rd}|�r|d|}|�|j
|��rvt|j�d	��}|jd
k�rP|�d|�|�|�}|�|||f�|��}�q|�||�|dk�r�|�t|��r�|j�d�}t||j�}|�||j�d��|jd
k�r�|dk�r�|�d||f�|S)Nrzunexpected tagged response: %rrr�Zdata2zunexpected response: %rr
rr�rzread literal size %srrf)rrrz%s response: %r)�	_get_line�_matchrk�mo�grouprGr@r�rZ�Untagged_responsera�ContinuationrIr_�intrDror�r�
Response_code)rT�resprr�r�Zdat2r�r�r=r=r>rqsP



zIMAP4._get_responsec
Cs�|j|}|dk	r|j|=|S|rDd}|j�|d�}|dk	rD||fS|��z|��Wq|jk
r�}z|jdkr~|���W5d}~XYqXqdS)Nrrf)rGrH�poprrqr@rD�	print_log)rTrr �resultr�rrr=r=r>r!cs 

zIMAP4._get_tagged_responsecCsf|��}|s|�d��|�d�s.|�d|��|dd�}|jdkrT|�d|�n|�d|�|S)Nzsocket error: EOFrz#socket error: unterminated line: %r���rz< %r)r�r@�endswithrDrorr�r=r=r>r%�s


zIMAP4._get_linecCsD|�|�|_|jdk	r:|jdkr:|�d|j|j��f�|jdk	S)Nrz	matched %r => %r)�matchr'rDror��groups)rTZcre�sr=r=r>r&�szIMAP4._matchcCs2|jtt|j�|j�}|jd|_d|j|<|S)Nrf)rjrr�rKrZrG)rTrr=r=r>r�s
zIMAP4._new_tagcCs$|�dd�}|�dd�}d|dS)N�\z\\�"z\")r�)rTrr=r=r>r��szIMAP4._quotecGs|�||j|f|���Sr})r"rrr=r=r>r��szIMAP4._simple_commandcCsT|dkr||fS||jkr$|dgfS|j�|�}|jdkrL|�d||f�||fS)Nrrzuntagged_responses[%s] => %s)rHr.rDro)rTr�r�r�r�r=r=r>r��s


zIMAP4._untagged_responsecCsN|dkrt��}t�dt�|��}tj�d||dd|f�tj��dS)Nz%M:%Sz
  %s.%02d %s
�d)�time�strftime�	localtimer��stderr�write�flush)rTr5ZsecsZtmr=r=r>ro�s
zIMAP4._mesgcCs>|��}|sdSd}tdd�|�}|�d||�|�f�dS)Nz
		cSs*d|d|ddr"d�|d�p$dfS)Nz%s: "%s"rrfz" "rB)r)�xr=r=r>�<lambda>�r
z IMAP4._dump_ur.<locals>.<lambda>zuntagged responses dump:%s%s)�items�mapror)rT�dict�l�tr=r=r>r��szIMAP4._dump_urcCs8|t��f|j|j<|jd7_|j|jkr4d|_dS)Nrfr)r9rnrmrlr�r=r=r>r�sz
IMAP4._logcCsl|�dt|j��|j|j}}|rhz|j|j|�WnYnX|d7}||jkr^d}|d8}q"dS)Nzlast %d IMAP4 interactions:rfr)ror�rnrmrl)rT�i�nr=r=r>r/�s
zIMAP4.print_log)r�r�)r�r�)r�F)N)F)N)Tr:r;r<�__doc__rPr?r@rA�
IMAP4_PORTrWrMrdrOr|r~r�r�rNr�r�r�rQr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr	r
rrrrr"rrrqr!r%r&rr�r�r�ror�rr/r=r=r=r>r�s�,*
		
	



 	

M	P
!
c@s:eZdZdZdedddfdd�Zdd�Zdefdd	�ZdS)
�	IMAP4_SSLa3IMAP4 client class over SSL connection

        Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile[, ssl_context]]]]])

                host - host's name (default: localhost);
                port - port number (default: standard IMAP4 SSL port);
                keyfile - PEM formatted file that contains your private key (default: None);
                certfile - PEM formatted certificate chain file (default: None);
                ssl_context - a SSLContext object that contains your certificate chain
                              and private key (default: None)
                Note: if ssl_context is provided, then parameters keyfile or
                certfile should not be set otherwise ValueError is raised.

        for more documentation see the docstring of the parent class IMAP4.
        rBNcCs�|dk	r|dk	rtd��|dk	r0|dk	r0td��|dk	s@|dk	rVddl}|�dtd�||_||_|dkrxtj||d�}||_t	�
|||�dS)Nz8ssl_context and keyfile arguments are mutually exclusivez9ssl_context and certfile arguments are mutually exclusiverzEkeyfile and certfile are deprecated, use a custom ssl_context instead�)�certfile�keyfile)�
ValueError�warnings�warn�DeprecationWarningrMrLr�r�r�rrW)rTrUrVrMrLr�rOr=r=r>rW�s$��zIMAP4_SSL.__init__cCst�|�}|jj||jd�S)Nr�)rr�r�r�rU)rTr�r=r=r>r�s
�zIMAP4_SSL._create_socketcCst�|||�dS)z�Setup connection to remote server on "host:port".
                (default: localhost:standard IMAP4 SSL port).
            This connection will be used by the routines:
                read, readline, send, shutdown.
            N)rrNrSr=r=r>rNszIMAP4_SSL.open)r:r;r<rH�IMAP4_SSL_PORTrWr�rNr=r=r=r>rJ�s�
rJc@sBeZdZdZdd�Zddd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)rz�IMAP4 client class over a stream

    Instantiate with: IMAP4_stream(command)

            "command" - a string that can be passed to subprocess.Popen()

    for more documentation see the docstring of the parent class IMAP4.
    cCs||_t�|�dSr})rrrW)rTrr=r=r>rW/szIMAP4_stream.__init__NcCsNd|_d|_d|_d|_tj|jttjtjddd�|_	|j	j
|_|j	j|_
dS)z�Setup a stream connection.
        This connection will be used by the routines:
            read, readline, send, shutdown.
        NT)�bufsize�stdin�stdout�shellZ	close_fds)rUrVr�r��
subprocess�Popenrr�PIPEr�rT�	writefilerU�readfilerSr=r=r>rN4s�
zIMAP4_stream.opencCs|j�|�Sr�)r[r�r�r=r=r>r�DszIMAP4_stream.readcCs
|j��S)r�)r[r�rbr=r=r>r�IszIMAP4_stream.readlinecCs|j�|�|j��dS)r�N)rZr=r>r�r=r=r>r�NszIMAP4_stream.sendcCs"|j��|j��|j��dS)r�N)r[r�rZr��waitrbr=r=r>rQTs

zIMAP4_stream.shutdown)NN)
r:r;r<rHrWrNr�r�r�rQr=r=r=r>r#s

c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)r�zcPrivate class to provide en/decoding
            for base64-based authentication conversation.
    cCs
||_dSr})r�)rTZmechinstr=r=r>rWbsz_Authenticator.__init__cCs&|�|�|��}|dkrdS|�|�S)N�*)r�r�r�)rTr�Zretr=r=r>r�esz_Authenticator.processcCsnd}t|t�r|�d�}|rjt|�dkrB|dd�}|dd�}n|}d}t�|�}|r||dd�}q|S)Nr
rc�0r�)r�r�r�r��binasciiZ
b2a_base64)rT�inpZouprE�er=r=r>r�ks	


z_Authenticator.encodecCs|sdSt�|�S)Nr
)r_Z
a2b_base64)rTr`r=r=r>r��sz_Authenticator.decodeN)r:r;r<rHrWr�r�r�r=r=r=r>r�\s
r�z0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Decr�cCsi|]\}}|��|d�qS)rf)r�)�.0rGr5r=r=r>�
<dictcomp>�srcrfc	Cs�t�|�}|sdSt|�d�}|�d�}t|�d��}t|�d��}t|�d��}t|�d��}t|�d��}t|�d	��}	t|�d
��}
|	d|
d}|dkr�|}||||||d
d
d
f	}t�|�|}
t�|
�S)z�Parse an IMAP4 INTERNALDATE string.

    Return corresponding local time.  The return value is a
    time.struct_time tuple or None if the string has wrong format.
    N�mon�zonen�day�year�hour�min�sec�zoneh�zonem�<�-r�)	�InternalDater3�Mon2numr(r+�calendarZtimegmr9r;)r-r'rdrerfrgrhrirjrkrlZzoneZtt�utcr=r=r>r�s$

cCs@d}d}tt|��}|r<t|d�\}}|||d�|}q|S)z-Convert integer to A-P string representation.r
sABCDEFGHIJKLMNOP�rf)r+�abs�divmod)ZnumrZAP�modr=r=r>r	�scCs$t�|�}|sdSt|�d����S)z-Convert IMAP4 flags response to python tuple.r=r�)�Flagsr3r#r(r$)r-r'r=r=r>r
�s
cCst|ttf�r"t�|tj���}n�t|t�r�z
|j	}WnZt
k
r�tjr�|d}|dkrpt�
t�|��d}tjtjf|}ntj}YnXt|d�}t|dd�dt|�i�}nLt|t�r�|jdkr�td��|}n*t|t�r�|d|dfd	kr�|Std
��d�t|j�}|�|�S)a�Convert date_time to IMAP4 INTERNALDATE representation.

    Return string in form: '"DD-Mmm-YYYY HH:MM:SS +HHMM"'.  The
    date_time argument can be a number (int or float) representing
    seconds since epoch (as returned by time.time()), a 9-tuple
    representing local time, an instance of time.struct_time (as
    returned by time.localtime()), an aware datetime instance or a
    double-quoted string.  In the last case, it is assumed to already
    be in the correct format.
    �r�)ZsecondsN��tzinfozdate_time must be awarer)r7r7zdate_time not of a known typez"%d-{}-%Y %H:%M:%S %z")r�r+�floatrZ
fromtimestamprrrZ
astimezoner#�	tm_gmtoffrzr9�daylightr;�mktime�altzonerrzrNr��format�MonthsZmonthr:)r�ZdtZgmtoffZdstZdeltaZfmtr=r=r>r�s2�





�__main__zd:s:)r=r=z-dz-s)rBzIMAP password for %s on %s: Z	localhostzJFrom: %(user)s@localhost%(lf)sSubject: IMAP4 test%(lf)s%(lf)sdata...%(lf)s�
)r�Zlfr�)r�)�
/tmp/xxx 1)r�)r�z/tmp/yyy)r��
/tmp/yyz 2r�r�)r�)z/tmpzyy*)r�r�)r�)NZSUBJECTZtest)r�)�1z(FLAGS INTERNALDATE RFC822))r)r��FLAGSz
(\Deleted))r�r=)r�r=)r�r=)r�r=)r�r=)r�)ZUIDVALIDITY)r	)r,ZALL)r�)r�)rr=cCsLt�d||f�tt|�|�\}}t�d|||f�|dkrH|d�|S)Nz%s %sz%s => %s %srr)�Mrorx)�cmdr�r�r�r=r=r>�run'sr�rzPROTOCOL_VERSION = %szCAPABILITIES = %rr�)z/tmp/zyy%z.*"([^"]+)"$r�r�r	rz%sz:(FLAGS INTERNALDATE RFC822.SIZE RFC822.HEADER RFC822.TEXT)z
All tests OK.z
Tests failed.z8
If you would like to see debugging output,
try: %s -d5
)UrHrpr_r�rir[r�rWr�r9rqrrr�iorr�r��ImportError�__all__r�rCrIrRrtr�rwr\r*rwror^r_r�r,r)rar]r`rrJr�rr�r$r��	enumeraterprr	r
rr:ZgetoptZgetpass�argvZoptlistr�r?rZstream_command�optr+rUZgetuserZUSERZPASSWDZ	test_mesgZ	test_seq1Z	test_seq2r�r�rErorursr�Zmlr3r'r(�pathr�r	�printr=r=r=r>�<module>sZH
�	�/



	

�l4
9,
#
)





��


�
�