a
    ze^;                     @   sf  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yF   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rbd 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 )    NTFPOP3error_protoc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   /usr/lib/python3.9/poplib.pyr          n   i        
i   c                   @   s  e 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d/Zd0d1 Z d2d3 Z!d=d4d5Z"d6d7 Z#d8d9 Z$d>d:d;Z%dS )?r   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 )NzUSER %sr<   r   userr   r   r   rD      s    z	POP3.userc                 C   s   |  d| S )Nz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 )NZ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 )NzLIST %sLISTr<   r=   r   whichr   r   r   r9      s    	z	POP3.listc                 C   s   |  d| S )NzRETR %sr=   rL   r   r   r   retr   s    z	POP3.retrc                 C   s   |  d| S )NzDELE %srB   rL   r   r   r   dele   s    z	POP3.delec                 C   s
   |  dS )NZNOOPrB   r>   r   r   r   noop  s    z	POP3.noopc                 C   s
   |  dS )NZRSETrB   r>   r   r   r   rset  s    z	POP3.rsetc                 C   s   |  d}|   |S )NZ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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 )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 )Nz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 )Nz!-ERR APOP not supported by serverr   r    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 )Nz	TOP %s %srN   )r   rM   Zhowmuchr   r   r   topS  s    zPOP3.topc                 C   s    |d ur|  d| S | dS )NzUIDL %sZUIDLrK   rL   r   r   r   uidl\  s    z	POP3.uidlc                 C   s
   |  dS )NZ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 )Nc                 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 )Nz-ERR TLS support missing$-ERR TLS session already establishedZSTLSz!-ERR STLS not supported by server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   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   s@   -





	
c                   @   s4   e Zd ZeddejdfddZdd ZdddZdS )	POP3_SSLNc                 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 )Nro   )r   )r   r}   r|   ru   r   r   r   rv     s    zPOP3_SSL.stls)NNN)	r   r   r   POP3_SSL_PORTr   rx   r   r   rv   r   r   r   r   r{     s
   
r{   __main__r    rF      zMessage %d:z   z-----------------------)&rY   ry   r   r   rr   rq   ImportError__all__	Exceptionr   rw   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>   sJ   
  p0

