a
    ´i^;  ã                   @   sj  d Z ddlZddlZddlZddlZzddlZdZW n eyJ   dZY n0 ddgZG dd„ de	ƒZ
dZd	Zd
ZdZee ZdZG dd„ dƒZer¬G dd„ deƒZe d¡ edk�rfddlZeejd ƒZee ¡ ƒ e ejd ¡ e ejd ¡ e ¡  e ¡ \ZZeded ƒD ]BZ e !e ¡\Z"Z#Z$ede  ƒ e#D ]Z%ede% ƒ �q>edƒ �qe &¡  dS )z@A POP3 client class.

Based on the J. Myers POP3 draft, Jan. 96
é    NTFÚPOP3Úerror_protoc                   @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r   r   ú/usr/lib64/python3.9/poplib.pyr      ó    én   iã  ó   ó   
i   c                   @   s  e Zd ZdZdZeejf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d„ Zdd„ Zdd„ Zdd„ Zd=d d!„Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Ze d0¡Z d1d2„ Z!d3d4„ Z"d>d5d6„Z#d7d8„ Z$d9d:„ Z%d?d;d<„Z&dS )@r   aP  This class supports both the minimal and optional command sets.
    Arguments can be strings or integers (where appropriate)
    (e.g.: retr(1) and retr('1') both work equally well.

    Minimal Command Set:
            USER name               user(name)
            PASS string             pass_(string)
            STAT                    stat()
            LIST [msg]              list(msg = None)
            RETR msg                retr(msg)
            DELE msg                dele(msg)
            NOOP                    noop()
            RSET                    rset()
            QUIT                    quit()

    Optional Commands (some servers support these):
            RPOP name               rpop(name)
            APOP name digest        apop(name, digest)
            TOP msg n               top(msg, n)
            UIDL [msg]              uidl(msg = None)
            CAPA                    capa()
            STLS                    stls()
            UTF8                    utf8()

    Raises one exception: 'error_proto'.

    Instantiate with:
            POP3(hostname, port=110)

    NB:     the POP protocol locks the mailbox from user
            authorization until QUIT, so be sure to get in, suck
            the messages, and quit, each time you access the
            mailbox.

            POP is a line-based protocol, which means large mail
            messages consume lots of python cycles reading them
            line-by-line.

            If it's available on your mail server, use IMAP4
            instead, it doesn't suffer from the two problems
            above.
    zUTF-8c                 C   sP   || _ || _d| _t d| ||¡ |  |¡| _| j d¡| _d| _	|  
¡ | _d S )NFzpoplib.connectÚrbr   )ÚhostÚportÚ_tls_establishedÚsysÚauditÚ_create_socketÚsockÚmakefileÚfileÚ
_debuggingÚ_getrespÚwelcome)Úselfr   r   Útimeoutr   r   r   Ú__init__b   s    zPOP3.__init__c                 C   s(   |d ur|st dƒ‚t | j| jf|¡S )Nz0Non-blocking socket (timeout=0) is not supported)Ú
ValueErrorÚsocketÚcreate_connectionr   r   )r   r   r   r   r   r   m   s    zPOP3._create_socketc                 C   s:   | j dkrtdt|ƒƒ t d| |¡ | j |t ¡ d S )Né   z*put*zpoplib.putline)r   ÚprintÚreprr   r   r   ÚsendallÚCRLF©r   Úliner   r   r   Ú_putliner   s    zPOP3._putlinec                 C   s.   | j rtdt|ƒƒ t|| jƒ}|  |¡ d S )Nz*cmd*)r   r!   r"   ÚbytesÚencodingr'   r%   r   r   r   Ú_putcmdz   s    zPOP3._putcmdc                 C   s    | j  td ¡}t|ƒtkr$tdƒ‚| jdkr<tdt|ƒƒ |sHtdƒ‚t|ƒ}|dd … tkrp|d d… |fS |d d… t	kr�|dd… |fS |d d… |fS )Nr    zline too longz*get*z-ERR EOFéþÿÿÿéÿÿÿÿ)
r   ÚreadlineÚ_MAXLINEÚlenr   r   r!   r"   r$   ÚCR)r   r&   Úoctetsr   r   r   Ú_getline„   s    zPOP3._getlinec                 C   s:   |   ¡ \}}| jdkr$tdt|ƒƒ | d¡s6t|ƒ‚|S )Nr    z*resp*ó   +)r2   r   r!   r"   Ú
startswithr   )r   ÚrespÚor   r   r   r   ™   s
    
zPOP3._getrespc                 C   sl   |   ¡ }g }d}|  ¡ \}}|dkrb| d¡rB|d }|dd … }|| }| |¡ |  ¡ \}}q|||fS )Nr   ó   .s   ..r    )r   r2   r4   Úappend)r   r5   Úlistr1   r&   r6   r   r   r   Ú_getlongresp£   s    

zPOP3._getlongrespc                 C   s   |   |¡ |  ¡ S ©N)r*   r   r%   r   r   r   Ú	_shortcmd³   s    
zPOP3._shortcmdc                 C   s   |   |¡ |  ¡ S r;   )r*   r:   r%   r   r   r   Ú_longcmdº   s    
zPOP3._longcmdc                 C   s   | j S r;   )r   ©r   r   r   r   Ú
getwelcomeÁ   s    zPOP3.getwelcomec                 C   s
   || _ d S r;   )r   )r   Úlevelr   r   r   Úset_debuglevelÅ   s    zPOP3.set_debuglevelc                 C   s   |   d| ¡S )zVSend user name, return response

        (should indicate password required).
        zUSER %s©r<   ©r   Úuserr   r   r   rD   Ë   s    z	POP3.userc                 C   s   |   d| ¡S )z Send password, return response

        (response includes message count, mailbox size).

        NB: mailbox is locked by server from here to 'quit()'
        zPASS %srB   )r   Zpswdr   r   r   Úpass_Ó   s    z
POP3.pass_c                 C   sF   |   d¡}| ¡ }| jr&tdt|ƒƒ t|d ƒ}t|d ƒ}||fS )z]Get mailbox status.

        Result is tuple of 2 ints (message count, mailbox size)
        ZSTATz*stat*r    é   )r<   Úsplitr   r!   r"   Úint)r   ZretvalZretsZnumMessagesZsizeMessagesr   r   r   ÚstatÝ   s    
z	POP3.statNc                 C   s    |dur|   d| ¡S |  d¡S )a  Request listing, return result.

        Result without a message number argument is in form
        ['response', ['mesg_num octets', ...], octets].

        Result when a message number argument is given is a
        single response: the "scan listing" for that message.
        NzLIST %sÚLIST©r<   r=   ©r   Úwhichr   r   r   r9   ê   s    	z	POP3.listc                 C   s   |   d| ¡S )zoRetrieve whole message number 'which'.

        Result is in form ['response', ['line', ...], octets].
        zRETR %s©r=   rL   r   r   r   Úretrø   s    z	POP3.retrc                 C   s   |   d| ¡S )zFDelete message number 'which'.

        Result is 'response'.
        zDELE %srB   rL   r   r   r   Údele   s    z	POP3.delec                 C   s
   |   d¡S )zXDoes nothing.

        One supposes the response indicates the server is alive.
        ZNOOPrB   r>   r   r   r   Únoop  s    z	POP3.noopc                 C   s
   |   d¡S )z(Unmark all messages marked for deletion.ZRSETrB   r>   r   r   r   Úrset  s    z	POP3.rsetc                 C   s   |   d¡}|  ¡  |S )zDSignoff: commit changes on server, unlock mailbox, close connection.ZQUIT)r<   Úclose)r   r5   r   r   r   Úquit  s    
z	POP3.quitc                 C   s,  z | j }d| _ |dur| ¡  W | j}d| _|dur z`z| tj¡ W nB tyˆ } z*|jtjkrtt	|ddƒdkrt‚ W Y d}~n
d}~0 0 W | ¡  n
| ¡  0 n†| j}d| _|du�r&zbz| tj¡ W nD t�y } z*|jtjkrút	|ddƒdkrú‚ W Y d}~n
d}~0 0 W | ¡  n
| ¡  0 0 dS )z8Close the connection without assuming anything about it.NÚwinerrorr   i&'  )
r   rS   r   Úshutdownr   Ú	SHUT_RDWRÚOSErrorÚerrnoZENOTCONNÚgetattr)r   r   r   Úexcr   r   r   rS     s6    
ÿó
ÿz
POP3.closec                 C   s   |   d| ¡S )zNot sure what this does.zRPOP %srB   rC   r   r   r   Úrpop7  s    z	POP3.rpops   \+OK.[^<]*(<.*>)c                 C   s\   t || jƒ}| j | j¡}|s&tdƒ‚ddl}| d¡| }| |¡ 	¡ }|  
d||f ¡S )a  Authorisation

        - only possible if server has supplied a timestamp in initial greeting.

        Args:
                user     - mailbox user;
                password - mailbox password.

        NB: mailbox is locked by server from here to 'quit()'
        z!-ERR APOP not supported by serverr   Nr    z
APOP %s %s)r(   r)   Ú	timestampÚmatchr   r   ÚhashlibÚgroupÚmd5Ú	hexdigestr<   )r   rD   ÚpasswordZsecretÚmr_   Údigestr   r   r   Úapop>  s    z	POP3.apopc                 C   s   |   d||f ¡S )z­Retrieve message header of message number 'which'
        and first 'howmuch' lines of message body.

        Result is in form ['response', ['line', ...], octets].
        z	TOP %s %srN   )r   rM   Zhowmuchr   r   r   ÚtopS  s    zPOP3.topc                 C   s    |dur|   d| ¡S |  d¡S )zìReturn message digest (unique id) list.

        If 'which', result contains unique id for that message
        in the form 'response mesgnum uid', otherwise result is
        the list ['response', ['mesgnum uid', ...], octets]
        NzUIDL %sZUIDLrK   rL   r   r   r   Úuidl\  s    z	POP3.uidlc                 C   s
   |   d¡S )zITry to enter UTF-8 mode (see RFC 6856). Returns server response.
        ZUTF8rB   r>   r   r   r   Úutf8h  s    z	POP3.utf8c                 C   s`   dd„ }i }z4|   d¡}|d }|D ]}||ƒ\}}|||< q$W n tyZ   tdƒ‚Y n0 |S )a   Return server capabilities (RFC 2449) as a dictionary
        >>> c=poplib.POP3('localhost')
        >>> c.capa()
        {'IMPLEMENTATION': ['Cyrus', 'POP3', 'server', 'v2.2.12'],
         'TOP': [], 'LOGIN-DELAY': ['0'], 'AUTH-RESP-CODE': [],
         'EXPIRE': ['NEVER'], 'USER': [], 'STLS': [], 'PIPELINING': [],
         'UIDL': [], 'RESP-CODES': []}
        >>>

        Really, according to RFC 2449, the cyrus folks should avoid
        having the implementation split into multiple arguments...
        c                 S   s"   |   d¡ ¡ }|d |dd … fS )NÚasciir   r    )ÚdecoderG   )r&   Zlstr   r   r   Ú	_parsecap{  s    zPOP3.capa.<locals>._parsecapZCAPAr    z!-ERR CAPA not supported by server)r=   r   )r   rl   Úcapsr5   ZrawcapsZcaplineZcapnmZcapargsr   r   r   Úcapan  s    
z	POP3.capac                 C   sx   t stdƒ‚| jrtdƒ‚|  ¡ }d|vr2tdƒ‚|du rBt ¡ }|  d¡}|j| j| j	d�| _| j 
d¡| _d| _|S )	z{Start a TLS session on the active connection as specified in RFC 2595.

                context - a ssl.SSLContext
        z-ERR TLS support missingú$-ERR TLS session already establishedZSTLSz!-ERR STLS not supported by serverN©Zserver_hostnamer   T)ÚHAVE_SSLr   r   rn   ÚsslÚ_create_stdlib_contextr<   Úwrap_socketr   r   r   r   )r   Úcontextrm   r5   r   r   r   Ústls‹  s     
ÿz	POP3.stls)N)N)N)'r   r   r   Ú__doc__r)   Ú	POP3_PORTr   Ú_GLOBAL_DEFAULT_TIMEOUTr   r   r'   r*   r2   r   r:   r<   r=   r?   rA   rD   rE   rI   r9   rO   rP   rQ   rR   rT   rS   r\   ÚreÚcompiler]   rf   rg   rh   ri   rn   rv   r   r   r   r   r   3   sB   +ÿ





	
c                   @   s8   e Zd ZdZeddejdfdd„Zdd„ Zd	dd„Z	dS )
ÚPOP3_SSLa  POP3 client class over SSL connection

        Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None,
                                   context=None)

               hostname - the hostname of the pop3 over ssl server
               port - port number
               keyfile - PEM formatted file that contains your private key
               certfile - PEM formatted certificate chain file
               context - a ssl.SSLContext

        See the methods of the parent class POP3 for more documentation.
        Nc                 C   s’   |d ur|d urt dƒ‚|d ur0|d ur0t dƒ‚|d us@|d urVdd l}| dtd¡ || _|| _|d u rxtj||d�}|| _t	 
| |||¡ d S )Nz4context and keyfile arguments are mutually exclusivez5context and certfile arguments are mutually exclusiver   zAkeyfile and certfile are deprecated, use a custom context insteadrF   )ÚcertfileÚkeyfile)r   ÚwarningsÚwarnÚDeprecationWarningr~   r}   rr   rs   ru   r   r   )r   r   r   r~   r}   r   ru   r   r   r   r   r   ²  s"    ÿÿzPOP3_SSL.__init__c                 C   s"   t  | |¡}| jj|| jd�}|S )Nrp   )r   r   ru   rt   r   )r   r   r   r   r   r   r   Æ  s
    ÿzPOP3_SSL._create_socketc                 C   s   t dƒ‚dS )z¬The method unconditionally raises an exception since the
            STLS command doesn't make any sense on an already established
            SSL/TLS session.
            ro   N)r   )r   r~   r}   ru   r   r   r   rv   Ì  s    zPOP3_SSL.stls)NNN)
r   r   r   rw   ÚPOP3_SSL_PORTr   ry   r   r   rv   r   r   r   r   r|   £  s   ÿ
r|   Ú__main__r    rF   é   zMessage %d:z   z-----------------------)'rw   rY   rz   r   r   rr   rq   ÚImportErrorÚ__all__Ú	Exceptionr   rx   r‚   r0   ZLFr$   r.   r   r|   r8   r   ÚargvÚar!   r?   rD   rE   r9   rI   ZnumMsgsZ	totalSizeÚrangeÚirO   ÚheaderÚmsgr1   r&   rT   r   r   r   r   Ú<module>   sL   
  p0

