a
    If^;                     @   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rG 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   lib/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socketZ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   Z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*   +)r0   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   r0   r2   append)r   r3   listr/   r$   r4   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 r9   )r(   r8   r#   r   r   r   _longcmd   s    
zPOP3._longcmdc                 C   s   | j S r9   )r   r   r   r   r   
getwelcome   s    zPOP3.getwelcomec                 C   s
   || _ d S r9   )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   rB      s    z	POP3.userc                 C   s   |  d| S )zSend password, return response

        (response includes message count, mailbox size).

        NB: mailbox is locked by server from here to 'quit()'
        zPASS %sr@   )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 %sZLISTr:   r;   r   whichr   r   r   r7      s    	z	POP3.listc                 C   s   |  d| S )zoRetrieve whole message number 'which'.

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

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

        One supposes the response indicates the server is alive.
        ZNOOPr@   r<   r   r   r   noop  s    z	POP3.noopc                 C   s
   |  dS )z(Unmark all messages marked for deletion.ZRSETr@   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   r3   r   r   r   quit  s    
z	POP3.quitc                 C   s,  z| j }d| _ |dur|  W | j}d| _|durz`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rt	|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.NZwinerrorr   i&'  )
r   rP   r   Zshutdownr   Z	SHUT_RDWROSErrorerrnoZENOTCONNgetattr)r   r   r   excr   r   r   rP     s6    

z
POP3.closec                 C   s   |  d| S )zNot sure what this does.zRPOP %sr@   rA   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groupZmd5Z	hexdigestr:   )r   rB   ZpasswordZsecretmrY   Zdigestr   r   r   apop>  s    z	POP3.apopc                 C   s   |  d||f S )zRetrieve message header of message number 'which'
        and first 'howmuch' lines of message body.

        Result is in form ['response', ['line', ...], octets].
        z	TOP %s %srK   )r   rJ   Zhowmuchr   r   r   topS  s    zPOP3.topc                 C   s    |dur|  d| S | dS )zReturn 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UIDLrH   rI   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UTF8r@   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   )decoderE   )r$   Zlstr   r   r   	_parsecap{  s    zPOP3.capa.<locals>._parsecapZCAPAr   z!-ERR CAPA not supported by server)r;   r   )r   rb   capsr3   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   rd   ssl_create_stdlib_contextr:   wrap_socketr   r   r   r   )r   contextrc   r3   r   r   r   stls  s     
z	POP3.stls)N)N)N)'r   r   r   __doc__r'   	POP3_PORTr   _GLOBAL_DEFAULT_TIMEOUTr   r   r%   r(   r0   r   r8   r:   r;   r=   r?   rB   rC   rG   r7   rL   rM   rN   rO   rQ   rP   rV   recompilerW   r\   r]   r^   r_   rd   rl   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 insteadrD   )certfilekeyfile)r   warningswarnDeprecationWarningrt   rs   rh   ri   rk   r   r   )r   r   r   rt   rs   r   rk   ru   r   r   r   r     s"    zPOP3_SSL.__init__c                 C   s"   t | |}| jj|| jd}|S )Nrf   )r   r   rk   rj   r   )r   r   r   r   r   r   r     s
    zPOP3_SSL._create_socketc                 C   s   t ddS )zThe method unconditionally raises an exception since the
            STLS command doesn't make any sense on an already established
            SSL/TLS session.
            re   N)r   )r   rt   rs   rk   r   r   r   rl     s    zPOP3_SSL.stls)NNN)
r   r   r   rm   POP3_SSL_PORTr   ro   r   r   rl   r   r   r   r   rr     s   
rr   __main__r   rD      zMessage %d:z   z-----------------------)'rm   rS   rp   r   r   rh   rg   ImportError__all__	Exceptionr   rn   rx   r.   ZLFr"   r,   r   rr   r6   r   argvar    r=   rB   rC   r7   rG   ZnumMsgsZ	totalSizerangeirL   headermsgr/   r$   rQ   r   r   r   r   <module>   sL   
  p0

