AlkantarClanX12

Your IP : 3.142.201.93


Current Path : /opt/alt/python310/lib64/python3.10/__pycache__/
Upload File :
Current File : //opt/alt/python310/lib64/python3.10/__pycache__/imaplib.cpython-310.pyc

o

6��fR��@s$dZdZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZmZm
Z
ddlmZzddlZdZWneyKdZYnwgd�Zd	ZdZd
ZdZdZd
Zidd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�d d�d!d�d"d�d#d�id$d�d%d�d&d�d'd�d(d�d)d�d*d�d+d�d,d�d-d�d.d�d/d�d0d�d1d�d2d�d3d�d4d��ddddddddd5��Ze�d6�Ze�d7�Ze�d8�Ze�d9ej�Z e�d:�Z!e�d;�Z"e�d<�Z#e�d=ej�Z$d9Z%d=Z&Gd>d?�d?�Z'e�rGd@dA�dAe'�Z(e�)dA�GdBdC�dCe'�Z*GdDdE�dE�Z+dF�,dG�Z-dHdI�e.e-dJd��D�Z/dKdL�Z0dMdN�Z1dOdP�Z2dQdR�Z3e4dSk�r�ddl5Z5ddl6Z6ze5�5ej7dJd�dT�\Z8Z9Wne5j:�y}Z;z
dU\Z8Z9WYdZ;[;ndZ;[;wwdZ<e8D]\Z=Z;e=dVk�r�e>e;�Z�q�e=dWk�r�e;Z<e9�s�e<fZ9�q�e9�s�dXZ9e9dZ?e6�@�ZAe6�6dYeAe?�p�dZf�ZBd[eAd\d]�ZCd^eAeBffd_d`dadbdcddeCffdddedfdgdhdidjdkdlfZDdmdndodpdbdddeCffdkdqfZEdrds�ZFz�e<�r�e*e<�ZGne'e?�ZGeGjHdtk�reDdJd�ZDeG�IdueGjJ�eG�IdveGjKf�eDD]
\ZLZ9eFeLe9��qeFdwdx�D]ZMe�NdyeM�ZOeO�r:eO�PdJ�ZQneM�,�dzZQeFd{eQf��q)eED]*\ZLZ9eFeLe9�ZReLe9fdok�r\�qJeRdz�,�ZSeS�sg�qJeFd|dd}eSdzd~f��qJeTd�WdSeTd��e�s�eTd�ej7d��dS)�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�APPEND)�AUTH�SELECTED�AUTHENTICATE)�NONAUTH�
CAPABILITY)rrr�LOGOUT�CHECK)r�CLOSE�COPY�CREATE�DELETE�	DELETEACL�ENABLE)r�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�UNSUBSCRIBE�UNSELECTs\+( (?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�Zded	fd
d�Z	dd
�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zded	fdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Z d:d;�Z!d<d=�Z"d>d?�Z#d@dA�Z$dBdC�Z%dDdE�Z&dFdG�Z'dHdI�Z(d�dLdM�Z)dNdO�Z*dPdQ�Z+dRdS�Z,dTdU�Z-d�dVdW�Z.dXdY�Z/dZd[�Z0d\d]�Z1d^d_�Z2d`da�Z3dbdc�Z4ddde�Z5d�dhdi�Z6djdk�Z7dldm�Z8dndo�Z9dpdq�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��ZId�d�d��ZJd�d��ZKd�d��ZLd�d��ZMd�d��ZNd�d��ZOd�d��ZP	d�d�d��ZQd�d��ZRd�d��ZSd�d��ZTd	S)�ra�IMAP4 client class.

    Instantiate with: IMAP4([host[, port[, timeout=None]]])

            host - host's name (default: localhost);
            port - port number (default: standard IMAP4 port).
            timeout - socket timeout (default: None)
                      If timeout is not given or is None,
                      the global default socket timeout is used

    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@�eZdZdS)zIMAP4.errorN��__name__�
__module__�__qualname__�r?r?�./opt/alt/python310/lib64/python3.10/imaplib.py�error��rAc@r:)zIMAP4.abortNr;r?r?r?r@�abort�rBrCc@r:)zIMAP4.readonlyNr;r?r?r?r@�readonly�rBrD�NcCs�t|_d|_d|_i|_i|_d|_d|_d|_d|_	|�
�|�|||�z|��WdSt
yDz|��W�tyCY�ww)NrrEFr)�Debug�debug�state�literal�tagged_commands�untagged_responses�continuation_response�is_readonly�tagnum�_tls_established�_mode_ascii�open�_connect�	Exception�shutdown�OSError��self�host�port�timeoutr?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�rWr?r?r@rP�szIMAP4._mode_asciicCs(d|_d|_t�t�|_t�t�|_dS)NT�utf-8)r]r^r_r`rarcrdrerfr?r?r@�
_mode_utf8�szIMAP4._mode_utf8cCs�tt�dd��|_t�d|jdtj�|_	d|_d|_	i|_
|jdkr4|�d	t
�|�d
|j�|��|_d|jvrBd|_nd
|jvrKd|_n|�|j��|��	|jdkrd|�d|jf�tD]
}||jvrnqf||_dS|�d��)Nii��s(?P<tag>s"\d+) (?P<type>[A-Z]+) (?P<data>.*)T�
r�zimaplib version %sznew IMAP4 connection, tag=%sZPREAUTHr�OKr�zCAPABILITIES: %rzserver not IMAP4 compliant)r	�randomZrandint�tagprer_r`rb�tagre�_cmd_log_len�_cmd_log_idx�_cmd_logrG�_mesg�__version__�
_get_responseZwelcomerKrHrA�_get_capabilities�capabilities�AllowedVersions�PROTOCOL_VERSION)rW�versionr?r?r@rR�s>���






zIMAP4._connectcCs"|tvrt||���Std|��)NzUnknown IMAP4 command: '%s')�Commands�getattr�lower�AttributeError)rW�attrr?r?r@�__getattr__
szIMAP4.__getattr__cCs|S�Nr?rfr?r?r@�	__enter__szIMAP4.__enter__cGs2|jdkrdSz|��WdStyYdSw)Nr)rH�logoutrU)rW�argsr?r?r@�__exit__s
�zIMAP4.__exit__cCs`|dur
|s
td��|jsdn|j}t�d||j|j�||jf}|dur+t�||�St�|�S)Nz0Non-blocking socket (timeout=0) is not supportedzimaplib.open)�
ValueErrorrX�sys�auditrY�socketZcreate_connection)rWrZrXZaddressr?r?r@�_create_socket#s

zIMAP4._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)rXrYr��sock�makefile�filerVr?r?r@rQ0sz
IMAP4.opencC�|j�|�S�zRead 'size' bytes from remote.)r��read�rW�sizer?r?r@r�<�z
IMAP4.readcCs.|j�td�}t|�tkr|�dt��|S)�Read line from remote.rjzgot more than %d bytes)r��readline�_MAXLINE�lenrA�rW�liner?r?r@r�AszIMAP4.readlinecCst�d||�|j�|�dS)�Send data to remote.zimaplib.sendN)r�r�r�Zsendall�rW�datar?r?r@�sendIsz
IMAP4.sendc
Cs�|j��z;z	|j�tj�Wn!ty0}z|jtjkr&t	|dd�dkr&�WYd}~n
d}~wwW|j��dSW|j��dS|j��w)� Close I/O established in "open".Zwinerrorri&'N)
r��closer�rTr�Z	SHUT_RDWRrU�errnoZENOTCONNr|)rW�excr?r?r@rTOs
���	�zIMAP4.shutdowncCs|jS)zfReturn socket instance used to connect to IMAP4 server.

        socket = <instance>.socket()
        )r�rfr?r?r@r�_sz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.
        ZRECENTrkN���)�_untagged_response�noop�rW�name�typ�datr?r?r@�recentks	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)rW�coder?r?r@�response|szIMAP4.responsecCsxd}|sd}|r|d|dfdkrd|}nd}|r t|�}nd}t�t|�}|jr1d|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�CRLFr]rI�_simple_command)rW�mailbox�flags�	date_time�messager�rIr?r?r@�append�s �
zIMAP4.appendcCsP|��}t|�j|_|�d|�\}}|dkr!|�|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.
        rrkr�rg�replacer)r��_Authenticator�processrIr�rA�decoderH)rWZ	mechanismZ
authobject�mechr�r�r?r?r@�authenticate�szIMAP4.authenticatecC� 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�rfr?r?r@�check�s
zIMAP4.checkcC�(z|�d�\}}Wd|_||fSd|_w)z�Close currently selected mailbox.

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

        (typ, [data]) = <instance>.close()
        rr�r�rH�rWr�r�r?r?r@r��s
�zIMAP4.closecC�|�d||�S)z�Copy 'message_set' messages onto end of 'new_mailbox'.

        (typ, [data]) = <instance>.copy(message_set, new_mailbox)
        rr�)rW�message_setZnew_mailboxr?r?r@�copy��z
IMAP4.copycC�|�d|�S)zPCreate new mailbox.

        (typ, [data]) = <instance>.create(mailbox)
        rr��rWr�r?r?r@�create��zIMAP4.createcCr�)zPDelete old mailbox.

        (typ, [data]) = <instance>.delete(mailbox)
        rr�r�r?r?r@�delete�r�zIMAP4.deletecCr�)z�Delete the ACLs (remove any rights) set for who on mailbox.

        (typ, [data]) = <instance>.deleteacl(mailbox, who)
        rr�)rWr��whor?r?r@�	deleteacl�r�zIMAP4.deleteaclcCsHd|jvr
t�d��|�d|�\}}|dkr d|��vr |��||fS)zlSend an RFC5161 enable string to the server.

        (typ, [data]) = <instance>.enable(capability)
        rzServer does not support ENABLErkzUTF8=ACCEPT)rwrrAr�r�rh)rWr�r�r�r?r?r@�enable�s

zIMAP4.enablecCr�)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.expungecC�$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�)rWr�Z
message_partsr�r�r�r?r?r@�fetchs
zIMAP4.fetchcC�|�d|�\}}|�||d�S)zXGet the ACLs for a mailbox.

        (typ, [data]) = <instance>.getacl(mailbox)
        rZACLr��rWr�r�r�r?r?r@�getacl(�zIMAP4.getaclcCs"|�d|||�\}}|�||d�S)za(typ, [data]) = <instance>.getannotation(mailbox, entry, attribute)
        Retrieve ANNOTATIONs.r �
ANNOTATIONr�)rWr��entryZ	attributer�r�r?r?r@�
getannotation1szIMAP4.getannotationcCr�)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�)rW�rootr�r�r?r?r@�getquota9szIMAP4.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�)rWr�r�r�ZquotaZ	quotarootr?r?r@�getquotarootDszIMAP4.getquotaroot�""�*cCr�)z�List mailbox names in directory matching pattern.

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

        'data' is list of LIST responses.
        r$r��rWZ	directory�patternr�r�r�r?r?r@�listO�z
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%rkr�r)r��_quoterArH)rW�user�passwordr�r�r?r?r@�login[s
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)rWr�r�r?r?r@�login_cram_md5iszIMAP4.login_cram_md5cCsBddl}t|jt�r|j�d�n|j}|jd|�||d���S)z1 Authobject to use with CRAM-MD5 authentication. rNrg� Zmd5)�hmac�
isinstancer��str�encoder�ZHMACZ	hexdigest)rWZ	challenger��pwdr?r?r@r�rs
�zIMAP4._CRAM_MD5_AUTHcCs$d|_|�d�\}}|��||fS)z|Shutdown connection to server.

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

        Returns server 'BYE' response.
        r)rHr�rTr�r?r?r@r�zszIMAP4.logoutcCr�)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�r�z
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�r�zIMAP4.myrightscCr�)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()
        Trlr()rG�_dump_urrKr�rfr?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�)rWZmessage_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�)rWr�r�r?r?r@�	proxyauth�s	zIMAP4.proxyauthcCr�)zkRename old mailbox name to new.

        (typ, [data]) = <instance>.rename(oldmailbox, newmailbox)
        r+r�)rWZ
oldmailboxZ
newmailboxr?r?r@�rename�r�zIMAP4.renamecGsXd}|r|jrt�d��|j|d|g|�R�\}}n|j|g|�R�\}}|�|||�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)r]rrAr�r�)rW�charsetZcriteriar�r�r�r?r?r@�search�s
zIMAP4.searchr�FcCs�i|_||_|rd}nd}|�||�\}}|dkr d|_||fSd|_d|jvr=|s=	|jdkr6|�|j�|�d	|��||j�d
dg�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-rkrr�	READ-ONLYTrjz%s is not writable�EXISTSN)rKrMr�rHrGr�rD�get)rWr�rDr�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�)rWr�r�Zwhatr?r?r@�setaclszIMAP4.setaclcGs$|jdg|�R�\}}|�||d�S)z_(typ, [data]) = <instance>.setannotation(mailbox[, entry, attribute]+)
        Set ANNOTATIONs.r/r�r�)rWr�r�r�r?r?r@�
setannotation
szIMAP4.setannotationcCs |�d||�\}}|�||d�S)ziSet the quota root's resource limits.

        (typ, [data]) = <instance>.setquota(root, limits)
        r0r�r�)rWr�Zlimitsr�r�r?r?r@�setquotaszIMAP4.setquotacGsHd}|d|dfdkrd|}|j|||g|�R�\}}|�|||�S)z�IMAP4rev1 extension SORT command.

        (typ, [data]) = <instance>.sort(sort_criteria, charset, search_criteria, ...)
        r1rr�r�r�r�)rWZ
sort_criteriar�search_criteriar�r�r�r?r?r@�sorts
z
IMAP4.sortcCs�d}ts	|�d��|jr|�d��||jvr|�d��|dur#t��}|�|�\}}|dkrG|j|j	|j
d�|_	|j	�d�|_d|_|�
�n|�d	��|�|||�S)
Nr2zSSL support missingzTLS session already establishedzTLS not supported by serverrk�Zserver_hostnamer�TzCouldn't establish TLS session)�HAVE_SSLrArOrCrw�ssl�_create_stdlib_contextr��wrap_socketr�rXr�r�rvr�)rW�ssl_contextr�r�r�r?r?r@�starttls)s&



�

zIMAP4.starttlscCr�)zpRequest named status conditions for mailbox.

        (typ, [data]) = <instance>.status(mailbox, names)
        r3r�)rWr��namesr�r�r�r?r?r@�status@sz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�)rWr��commandr�r�r�r?r?r@�storeLszIMAP4.storecCr�)zYSubscribe to new mailbox.

        (typ, [data]) = <instance>.subscribe(mailbox)
        r5r�r�r?r?r@�	subscribeWr�zIMAP4.subscribecGs,d}|j|||g|�R�\}}|�|||�S)z�IMAPrev1 extension THREAD command.

        (type, [data]) = <instance>.thread(threading_algorithm, charset, search_criteria, ...)
        r6r�)rWZthreading_algorithmrr	r�r�r�r?r?r@�thread_szIMAP4.threadc	Gs�|��}|tvr|�d|��|jt|vr&|�d||jd�t|�f��d}|j||g|�R�\}}|dvr;|}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�r{rArH�joinr�r�)rWrr�r�r�r�r?r?r@�uidis��z	IMAP4.uidcCr�)z_Unsubscribe from old mailbox.

        (typ, [data]) = <instance>.unsubscribe(mailbox)
        r8r�r�r?r?r@�unsubscribe�r�zIMAP4.unsubscribecCr�)aMFree server's resources associated with the selected mailbox
        and returns the server to the authenticated state.
        This command performs the same actions as CLOSE, except
        that no messages are permanently removed from the currently
        selected mailbox.

        (typ, [data]) = <instance>.unselect()
        r9rr�)rWr�r�r?r?r@�unselect�s
	�zIMAP4.unselectcGs.|��}|tvr|jft|<|j|g|�R�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�r{rHr��rWr�r�r?r?r@�xatom�s
zIMAP4.xatomc	Csf|durd}|j}	|jdkr|�d|t|�|d��|f�||vr,||�|�dS|g||<dS)N�T�z#untagged_responses[%s] %s += ["%r"]rE)rKrGrsr�rr�)rWr�r�Zurr?r?r@�_append_untagged�s
�zIMAP4._append_untaggedcCs,|j�d�}|r|�|d�|jd���dS)N�BYEr�r�)rKrrCr�r^)rW�byer?r?r@�
_check_bye�s�zIMAP4._check_byec

Gs|jt|vrd|_|�d||jd�t|�f��dD]}||jvr'|j|=qd|jvr5|js5|�d��|��}t	||j
�}|d|}|D]}|durNqGt|t�rYt	||j
�}|d|}qG|j}|dur�d|_t
|�t
|j�urv|}nd}|t	dt|�|j
�}	|jd	kr�|�d
|�n|�d
|�z	|�|t�Wnty�}	z|�d|	��d}	~	ww|dur�|S	|��r�|j|r�|S|��s�|r�||j�}	|jd	kr�|�d
t|��z|�|�|�t�Wnt�y}	z|�d|	��d}	~	ww|�s		|Sq�)Nrr�rk�NO�BADrz#mailbox status changed to READ-ONLY� z {%s}T�z> %rzsocket error: %srjzwrite literal size %s)rHr{rIrArrKrMrD�_new_tag�bytesr^r�r��type�_commandr�rGrs�_logr�r�rUrCrurJrL)
rWr�r�r��tagr��argrIZ	literator�valr?r?r@r.�s~��
�
�



��
�


���zIMAP4._commandc
Cs�|dk}|s
|��z|j||d�\}}Wn+|jy+}z	|�d||f��d}~w|jy@}z	|�d||f��d}~ww|sG|��|dkrU|�d|||f��||fS)Nr)�
expect_byezcommand: %s => %sr(z%s command error: %s %s)r%�_get_tagged_responserCrA)rWr�r0r�r�r�r2r?r?r@�_command_completes"���zIMAP4._command_completecCsJ|��\}}|dgkr|�d��t|d|j�}|��}t|���|_dS)Nz"no CAPABILITY response from serverr�)r�rAr�r^r��tuple�splitrwr�r?r?r@rv#s

zIMAP4._get_capabilitiescCs�|��}|�|j|�r8|j�d�}||jvr|�d|��|j�d�}t||j�}|j�d�}||gf|j|<n�d}|�t	|�sM|�|j
|�rM|j�d�}|jdurh|�t|�ra|j�d�|_dS|�d|��|j�d�}t||j�}|j�d�}|dur�d}|r�|d|}|�|j
|�r�t|j�d	��}	|jdkr�|�d|�|�|�}|�|||f�|��}|�|j
|�s�|�||�|d
vr�|�t|�r�|j�d�}t||j�}|�||j�d��	|jdkr�|dvr�|�d||f�|S)Nr0zunexpected tagged response: %rr-r�Zdata2zunexpected response: %rr r)r�Tr*zread literal size %sr&rj)r'r(r#z%s response: %r)�	_get_line�_matchro�mo�grouprJrCr�r^�Untagged_responsere�ContinuationrLrc�intrGrsr�r"�
Response_code)rW�respr0r�r�Zdat2r�r�r?r?r@ru,sR



�zIMAP4._get_responsec
Cs�	|j|}|dur|j|=|S|r#d}|j�|d�}|dur#||fS|��z|��Wn|jyE}z	|jdkr@|���d}~wwq)Nrjr#)rJrK�popr%rurCrG�	print_log)rWr0r3�resultr�r$r2r?r?r@r4|s*

���zIMAP4._get_tagged_responsecCsj|��}|s|�d��|�d�s|�d|��|dd�}	|jdkr,|�d|�|S|�d|�|S)Nzsocket error: EOFrz#socket error: unterminated line: %r���Tr*z< %r)r�rC�endswithrGrsr/r�r?r?r@r8�s


�zIMAP4._get_linecCsF|�|�|_	|jdur|jdkr|�d|j|j��f�|jduS)NTr!z	matched %r => %r)�matchr:rGrsr��groups)rWZcre�sr?r?r@r9�s

zIMAP4._matchcCs2|jtt|j�|j�}|jd|_d|j|<|S)Nrj)rnr,r�rNr^rJ)rWr0r?r?r@r+�s
zIMAP4._new_tagcCs$|�dd�}|�dd�}d|dS)N�\z\\�"z\")r�)rWr1r?r?r@r��szIMAP4._quotecGs|�||j|g|�R��Sr�)r5r.rr?r?r@r��szIMAP4._simple_commandcCsV|dkr||fS||jvr|dgfS|j�|�}	|jdkr'|�d||f�||fS)Nr'Tr!zuntagged_responses[%s] => %s)rKrArGrs)rWr�r�r�r�r?r?r@r��s


zIMAP4._untagged_responseTcCsN|durt��}t�dt�|��}tj�d||dd|f�tj��dS)Nz%M:%Sz
  %s.%02d %s
�d)�time�strftime�	localtimer��stderr�write�flush)rWrHZsecsZtmr?r?r@rs�s
zIMAP4._mesgcCs2|sdSdd�|��D�}|�dd�|��dS)Ncss"�|]\}}|�d|��VqdS)z: Nr?)�.0�key�valuer?r?r@�	<genexpr>�s��z!IMAP4._dump_ur.<locals>.<genexpr>zuntagged responses dump:z
		)�itemsrsr)rWZuntagged_resp_dictrVr?r?r@r��s�zIMAP4._dump_urcCs<|t��f|j|j<|jd7_|j|jkrd|_dSdS)Nrjr)rLrrrqrpr�r?r?r@r/�s

�z
IMAP4._logcCsn|�dt|j��|j|j}}|r5z
|j|j|�WnY|d7}||jkr-d}|d8}|sdSdS)Nzlast %d IMAP4 interactions:rjr)rsr�rrrqrp)rW�i�nr?r?r@rB�s
�zIMAP4.print_log)r�r�)r�Fr�)F)Ur<r=r>�__doc__rSrArCrD�
IMAP4_PORTr[rPrhrRr�r�r�r�rQr�r�r�rTr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrrr
rrrrrrrrrr"r%r.r5rvrur4r8r9r+r�r�r�rsr�r/rBr?r?r?r@r�s�/*

		
	




 	

M	
P!
rc@s>eZdZdZdeddddfdd�Zdd�Zdedfdd	�ZdS)
�	IMAP4_SSLa�IMAP4 client class over SSL connection

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

                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.
                timeout - socket timeout (default: None) If timeout is not given or is None,
                          the global default socket timeout is used

        for more documentation see the docstring of the parent class IMAP4.
        rENcCs�|dur|durtd��|dur|durtd��|dus |dur+ddl}|�dtd�||_||_|dur<tj||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)r��warnings�warn�DeprecationWarningr^r]r
rrrr[)rWrXrYr^r]rrZr_r?r?r@r[s"��zIMAP4_SSL.__init__cCst�||�}|jj||jd�S)Nr)rr�rrrX)rWrZr�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)rrQrVr?r?r@rQ2szIMAP4_SSL.open)r<r=r>rY�IMAP4_SSL_PORTr[r�rQr?r?r?r@r[s
�r[c@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�)rrr[)rWrr?r?r@r[IszIMAP4_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)rXrYr�r��
subprocess�Popenrr�PIPEr�rd�	writefilere�readfilerVr?r?r@rQNs�
zIMAP4_stream.opencCr�r�)rkr�r�r?r?r@r�^r�zIMAP4_stream.readcCs
|j��S)r�)rkr�rfr?r?r@r�cs
zIMAP4_stream.readlinecCs|j�|�|j��dS)r�N)rjrPrQr�r?r?r@r�hszIMAP4_stream.sendcCs"|j��|j��|j��dS)r�N)rkr�rjr��waitrfr?r?r@rTns

zIMAP4_stream.shutdown)NNN)
r<r=r>rYr[rQr�r�r�rTr?r?r?r@r=s

rc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)r�zcPrivate class to provide en/decoding
            for base64-based authentication conversation.
    cCs
||_dSr�)r�)rWZmechinstr?r?r@r[|s
z_Authenticator.__init__cCs&|�|�|��}|durdS|�|�S)N�*)r�r�r�)rWr�Zretr?r?r@r�s
z_Authenticator.processcCspd}t|t�r|�d�}|r6t|�dkr!|dd�}|dd�}n|}d}t�|�}|r4||dd�}|s|S)Nr rg�0r�)r�r�r�r��binasciiZ
b2a_base64)rW�inpZoup�t�er?r?r@r��s	


�
z_Authenticator.encodecCs|sdSt�|�S)Nr )roZ
a2b_base64)rWrpr?r?r@r��s
z_Authenticator.decodeN)r<r=r>rYr[r�r�r�r?r?r?r@r�vsr�z0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Decr�cCsi|]\}}|��|d�qS)rj)r�)rRrXrHr?r?r@�
<dictcomp>�srsrjc	Cs�t�|�}|s	dSt|�d�}|�d�}t|�d��}t|�d��}t|�d��}t|�d��}t|�d��}t|�d	��}	t|�d
��}
|	d|
d}|dkrU|}||||||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�)	�InternalDaterF�Mon2numr;r>�calendarZtimegmrLrN)r@r:rtrurvrwrxryrzr{r|ZzoneZtt�utcr?r?r@r�s$


rcCsBd}d}tt|��}|rt|d�\}}|||d�|}|s|S)z-Convert integer to A-P string representation.r sABCDEFGHIJKLMNOP�rj)r>�abs�divmod)Znumr2ZAP�modr?r?r@r	�s�r	cCs$t�|�}|s	dSt|�d����S)z-Convert IMAP4 flags response to python tuple.r?r�)�FlagsrFr6r;r7)r@r:r?r?r@r
�s
r
cCst|ttf�rt�|tj���}npt|t�r[z|j	}Wn,t
yGtjrA|d}|dkr7t�
t�|��d}tjtjf|}ntj}Ynwt|d�}t|dd�dt|�i�}n&t|t�rl|jduritd��|}nt|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)rJrJzdate_time not of a known typez"%d-{}-%Y %H:%M:%S %z")r�r>�floatrZ
fromtimestamprr�Z
astimezoner6�	tm_gmtoffr~rL�daylightrN�mktime�altzonerr�r�r��format�MonthsZmonthrM)r�ZdtZgmtoffZdstZdeltaZfmtr?r?r@r�s:��

��



r�__main__zd:s:)r?r?z-dz-s)rEzIMAP 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*)rr�)r)NZSUBJECT�test)r�)�1z(FLAGS INTERNALDATE RFC822))r)r��FLAGSz
(\Deleted))r�r?)r�r?)r�r?)r�r?)rr?)r�)ZUIDVALIDITY)r)r,ZALL)r�)r)r�r?cCsLt�d||f�tt|�|�\}}t�d|||f�|dkr$|d�|S)Nz%s %sz%s => %s %sr'r)�Mrsr|)�cmdr�r�r�r?r?r@�runAs
r�rzPROTOCOL_VERSION = %szCAPABILITIES = %rr�)z/tmp/zyy%z.*"([^"]+)"$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
)UrYrtror�rmr_r�rgr�rLr�rrr�iorr
r�ImportError�__all__r�rFrZrbrxr�r{r`r=r�rrbrcr�r?r<rerardrr[r�rr�r7r��	enumerater�rr	r
rr<ZgetoptZgetpass�argvZoptlistr�rAr2Zstream_command�optr>rXZgetuserZUSERZPASSWDZ	test_mesgZ	test_seq1Z	test_seq2r�r�rHrsryrwr�ZmlrFr:r;�pathr�r�printr?r?r?r@�<module>s�H�	�������	�
���
������������������� �!�"�#�$�
0



	
�
69
,#

)��

�

��




���