a
    ãzexÖ  ã                +   @   s”  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
m
Z
mZmZ ddlmZ zddlZdZW n ey’   dZY n0 g d¢ZdZdZd	Zd
ZdZdZdddddddddddddddddddddddddddddddddddddddddddœ*Ze d¡Ze d¡Ze d¡Ze dej¡Ze d¡Z e d¡Z!e d¡Z"e dej¡Z#dZ$dZ%G dd„ dƒZ&erœG dd„ de&ƒZ'e (d¡ G dd „ d e&ƒZ)G d!d"„ d"ƒZ*d# +d$¡Z,d%d&„ e-e,d'd… ƒD ƒZ.d(d)„ Z/d*d+„ Z0d,d-„ Z1d.d/„ Z2e3d0krddl4Z4ddl5Z5ze4 4ej6d'd… d1¡\Z7Z8W n0 e4j9yf Z: zd2\Z7Z8W Y dZ:[:n
dZ:[:0 0 dZ;e7D ]8\Z<Z:e<d3krŒe=e:ƒZne<d4krpe:Z;e8spe;fZ8qpe8s´d5Z8e8d Z>e5 ?¡ Z@e5 5d6e@e>pÔd7f ¡ZAd8e@d9d:œ ZBd;e@eAffd<d=d>d?d@ddeBffdAdBdCdDdEdFdGdHdIfZCdJdKdLdMd?dddeBffdHdNfZDdOdP„ ZEze;rXe)e;ƒZFne&e>ƒZFeFjGdQkrxeCd'd… ZCeF HdReFjI ¡ eF HdSeFjJf ¡ eCD ]\ZKZ8eEeKe8ƒ qžeEdTdUƒD ]<ZLe MdVeL¡ZNeNràeN Od'¡ZPneL +¡ dW ZPeEdXePfƒ q¾eDD ]T\ZKZ8eEeKe8ƒZQeKe8fdLkr$q eQdW  +¡ ZReRs:q eEdYdZd[eRdW  d\fƒ q eSd]ƒ W n.   eSd^ƒ esˆeSd_ej6d  ƒ ‚ Y n0 dS )`z2.58é    N)ÚdatetimeÚtimezoneÚ	timedelta)ÚDEFAULT_BUFFER_SIZETF)ÚIMAP4ÚIMAP4_streamÚInternaldate2tupleÚInt2APÚ
ParseFlagsÚTime2Internaldateó   
é   iá  )Z	IMAP4REV1r   i@B )ÚAUTHÚSELECTED)ÚNONAUTH)r   r   r   ÚLOGOUT)r   )r   )*ÚAPPENDÚAUTHENTICATEÚ
CAPABILITYÚCHECKÚCLOSEÚCOPYÚCREATEÚDELETEÚ	DELETEACLÚENABLEÚEXAMINEÚEXPUNGEÚFETCHÚGETACLÚGETANNOTATIONÚGETQUOTAÚGETQUOTAROOTÚMYRIGHTSÚLISTÚLOGINr   ÚLSUBZMOVEÚ	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–  e Zd ZG dd„ deƒZG dd„ deƒZG dd„ deƒZded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ed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/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Z d;d<„ Z!d=d>„ Z"d?d@„ Z#dAdB„ Z$dCdD„ Z%dEdF„ Z&dGdH„ Z'ddKdL„Z(dMdN„ Z)dOdP„ Z*dQdR„ Z+dSdT„ Z,dždUdV„Z-dWdX„ Z.dYdZ„ Z/d[d\„ Z0d]d^„ Z1d_d`„ Z2dadb„ Z3dcdd„ Z4dŸdgdh„Z5didj„ Z6dkdl„ Z7dmdn„ Z8dodp„ Z9d dqdr„Z:dsdt„ Z;dudv„ Z<dwdx„ Z=dydz„ Z>d{d|„ Z?d}d~„ Z@dd€„ ZAdd‚„ ZBdƒd„„ ZCd…d†„ ZDd‡dˆ„ ZEd‰dŠ„ ZFd‹dŒ„ ZGddŽ„ ZHd¡dd„ZId‘d’„ ZJd“d”„ ZKd•d–„ ZLd—d˜„ ZMd™dš„ ZNd›dœ„ ZOdS )¢r   c                   @   s   e Zd ZdS )zIMAP4.errorN©Ú__name__Ú
__module__Ú__qualname__© r>   r>   ú/usr/lib/python3.9/imaplib.pyÚerror¸   ó    r@   c                   @   s   e Zd ZdS )zIMAP4.abortNr:   r>   r>   r>   r?   Úabort¹   rA   rB   c                   @   s   e Zd ZdS )zIMAP4.readonlyNr:   r>   r>   r>   r?   Úreadonlyº   rA   rC   Ú Nc                 C   s’   t | _d| _d | _i | _i | _d| _d| _d| _d| _	|  
¡  |  |||¡ z|  ¡  W n4 tyŒ   z|  ¡  W n ty„   Y n0 ‚ Y n0 d S )Nr   rD   Fr   )Ú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&    zIMAP4.__init__c                 C   s0   d| _ d| _t ttj¡| _t ttj¡| _d S )NFÚascii)	Úutf8_enabledÚ	_encodingÚreÚcompileÚ_LiteralÚASCIIÚLiteralÚ_Untagged_statusÚUntagged_status©rV   r>   r>   r?   rO   Õ   s    zIMAP4._mode_asciic                 C   s(   d| _ d| _t t¡| _t t¡| _d S )NTúutf-8)r\   r]   r^   r_   r`   rb   rc   rd   re   r>   r>   r?   Ú
_mode_utf8Ü   s    zIMAP4._mode_utf8c                 C   sœ   t t dd¡ƒ| _t d| j d tj¡| _|  	¡ | _
d| jv rHd| _nd| jv rZd| _n|  | j
¡‚|  ¡  tD ]}|| jvr‚qr|| _ d S |  d	¡‚d S )
Ni   iÿÿ  s   (?P<tag>s"   \d+) (?P<type>[A-Z]+) (?P<data>.*)ZPREAUTHr   ÚOKr   zserver not IMAP4 compliant)r	   ÚrandomÚrandintÚtagprer^   r_   ra   ÚtagreÚ__version__Ú_get_responseZwelcomerJ   rG   r@   Ú_get_capabilitiesÚAllowedVersionsÚcapabilitiesÚPROTOCOL_VERSION)rV   Úversionr>   r>   r?   rQ   ã   s*    ÿþþ



zIMAP4._connectc                 C   s&   |t v rt| | ¡ ƒS td| ƒ‚d S )NzUnknown IMAP4 command: '%s')ÚCommandsÚgetattrÚlowerÚAttributeError)rV   Úattrr>   r>   r?   Ú__getattr__  s    zIMAP4.__getattr__c                 C   s   | S ©Nr>   re   r>   r>   r?   Ú	__enter__  s    zIMAP4.__enter__c                 G   s2   | j dkrd S z|  ¡  W n ty,   Y n0 d S ©Nr   )rG   ÚlogoutrT   )rV   Úargsr>   r>   r?   Ú__exit__  s    
zIMAP4.__exit__c                 C   s`   |d ur|st dƒ‚| jsd n| j}t d| | j| j¡ || jf}|d urVt ||¡S t |¡S )Nz0Non-blocking socket (timeout=0) is not supportedzimaplib.open)Ú
ValueErrorrW   ÚsysÚauditrX   ÚsocketÚcreate_connection)rV   rY   rW   Úaddressr>   r>   r?   Ú_create_socket#  s    
zIMAP4._create_socketc                 C   s*   || _ || _|  |¡| _| j d¡| _d S )NÚrb)rW   rX   r†   ÚsockÚmakefileÚfilerU   r>   r>   r?   rP   0  s    z
IMAP4.openc                 C   s   | j  |¡S rz   )rŠ   Úread©rV   Úsizer>   r>   r?   r‹   <  s    z
IMAP4.readc                 C   s.   | j  td ¡}t|ƒtkr*|  dt ¡‚|S )Né   zgot more than %d bytes)rŠ   ÚreadlineÚ_MAXLINEÚlenr@   ©rV   Úliner>   r>   r?   r   A  s    zIMAP4.readlinec                 C   s   t  d| |¡ | j |¡ d S )Nzimaplib.send)r   r‚   rˆ   Úsendall©rV   Údatar>   r>   r?   ÚsendI  s    z
IMAP4.sendc              
   C   s€   | j  ¡  zdz| j tj¡ W nB ty` } z*|jtjkrLt	|ddƒdkrL‚ W Y d }~n
d }~0 0 W | j ¡  n| j ¡  0 d S )NÚwinerrorr   i&'  )
rŠ   Úcloserˆ   rS   rƒ   Ú	SHUT_RDWRrT   ÚerrnoZENOTCONNru   )rV   Úexcr>   r>   r?   rS   O  s    
ÿzIMAP4.shutdownc                 C   s   | j S rz   )rˆ   re   r>   r>   r?   rƒ   _  s    zIMAP4.socketc                 C   sB   d}|   dd g|¡\}}|d r(||fS |  ¡ \}}|   |||¡S )NZRECENTrh   éÿÿÿÿ)Ú_untagged_responseÚnoop©rV   ÚnameÚtypÚdatr>   r>   r?   Úrecentk  s    	zIMAP4.recentc                 C   s   |   |d g| ¡ ¡S rz   )rž   Úupper)rV   Úcoder>   r>   r?   Úresponse|  s    zIMAP4.responsec                 C   sx   d}|sd}|r.|d |d fdkr2d| }nd }|r@t |ƒ}nd }t t|¡}| jrbd| d }|| _|  ||||¡S )	Nr   ÚINBOXr   r   ©ú(ú)ú(%s)s   UTF8 (ó   ))r   ÚMapCRLFÚsubÚCRLFr\   rH   Ú_simple_command)rV   ÚmailboxÚflagsÚ	date_timeÚmessager¡   rH   r>   r>   r?   ÚappendŠ  s    

zIMAP4.appendc                 C   sP   |  ¡ }t|ƒj| _|  d|¡\}}|dkrB|  |d  dd¡¡‚d| _||fS )Nr   rh   r   rf   Úreplacer   )r¥   Ú_AuthenticatorÚprocessrH   r±   r@   ÚdecoderG   )rV   Z	mechanismZ
authobjectÚmechr¢   r£   r>   r>   r?   Úauthenticate¤  s    zIMAP4.authenticatec                 C   s    d}|   |¡\}}|  |||¡S )Nr   ©r±   rž   r    r>   r>   r?   Ú
capabilityÁ  s    zIMAP4.capabilityc                 C   s
   |   d¡S )Nr   ©r±   re   r>   r>   r?   ÚcheckÊ  s    zIMAP4.checkc                 C   s*   z|   d¡\}}W d| _nd| _0 ||fS )Nr   r   ©r±   rG   ©rV   r¢   r£   r>   r>   r?   r™   Ò  s    zIMAP4.closec                 C   s   |   d||¡S )Nr   r¿   )rV   Úmessage_setZnew_mailboxr>   r>   r?   Úcopyá  s    z
IMAP4.copyc                 C   s   |   d|¡S )Nr   r¿   ©rV   r²   r>   r>   r?   Úcreateé  s    zIMAP4.createc                 C   s   |   d|¡S )Nr   r¿   rÅ   r>   r>   r?   Údeleteñ  s    zIMAP4.deletec                 C   s   |   d||¡S )Nr   r¿   )rV   r²   Úwhor>   r>   r?   Ú	deleteaclø  s    zIMAP4.deleteaclc                 C   sH   d| j vrt d¡‚|  d|¡\}}|dkr@d| ¡ v r@|  ¡  ||fS )Nr   zServer does not support ENABLErh   zUTF8=ACCEPT)rq   r   r@   r±   r¥   rg   )rV   r¾   r¢   r–   r>   r>   r?   Úenableÿ  s    

zIMAP4.enablec                 C   s    d}|   |¡\}}|  |||¡S )Nr   r½   r    r>   r>   r?   Úexpunge  s    	zIMAP4.expungec                 C   s$   d}|   |||¡\}}|  |||¡S )Nr   r½   )rV   rÃ   Zmessage_partsr¡   r¢   r£   r>   r>   r?   Úfetch  s    
zIMAP4.fetchc                 C   s   |   d|¡\}}|  ||d¡S )Nr   ZACLr½   ©rV   r²   r¢   r£   r>   r>   r?   Úgetacl(  s    zIMAP4.getaclc                 C   s"   |   d|||¡\}}|  ||d¡S )Nr    Ú
ANNOTATIONr½   )rV   r²   ÚentryZ	attributer¢   r£   r>   r>   r?   Úgetannotation1  s    zIMAP4.getannotationc                 C   s   |   d|¡\}}|  ||d¡S )Nr!   ÚQUOTAr½   )rV   Úrootr¢   r£   r>   r>   r?   Úgetquota9  s    zIMAP4.getquotac                 C   s@   |   d|¡\}}|  ||d¡\}}|  ||d¡\}}|||gfS )Nr"   rÒ   Z	QUOTAROOTr½   )rV   r²   r¢   r£   ZquotaZ	quotarootr>   r>   r?   ÚgetquotarootD  s    zIMAP4.getquotarootú""Ú*c                 C   s$   d}|   |||¡\}}|  |||¡S )Nr$   r½   ©rV   Z	directoryÚpatternr¡   r¢   r£   r>   r>   r?   ÚlistO  s    z
IMAP4.listc                 C   s<   |   d||  |¡¡\}}|dkr.|  |d ¡‚d| _||fS )Nr%   rh   r   r   )r±   Ú_quoter@   rG   )rV   ÚuserÚpasswordr¢   r£   r>   r>   r?   Úlogin[  s
    zIMAP4.loginc                 C   s   || | _ | _|  d| j¡S )NzCRAM-MD5)rÜ   rÝ   r¼   Ú_CRAM_MD5_AUTH)rV   rÜ   rÝ   r>   r>   r?   Úlogin_cram_md5i  s    zIMAP4.login_cram_md5c                 C   sB   dd l }t| jtƒr | j d¡n| j}| jd | ||d¡ ¡  S )Nr   rf   ú Úmd5)ÚhmacÚ
isinstancerÝ   ÚstrÚencoderÜ   ZHMACÚ	hexdigest)rV   Z	challengerã   Úpwdr>   r>   r?   rß   r  s
    ÿzIMAP4._CRAM_MD5_AUTHc                 C   s$   d| _ |  d¡\}}|  ¡  ||fS r|   )rG   r±   rS   rÂ   r>   r>   r?   r}   z  s    zIMAP4.logoutc                 C   s$   d}|   |||¡\}}|  |||¡S )Nr&   r½   rØ   r>   r>   r?   Úlsub‡  s    z
IMAP4.lsubc                 C   s   |   d|¡\}}|  ||d¡S )Nr#   r½   rÍ   r>   r>   r?   Úmyrights’  s    zIMAP4.myrightsc                 C   s    d}|   |¡\}}|  |||¡S )Nr'   r½   r    r>   r>   r?   Ú	namespaceš  s    zIMAP4.namespacec                 C   s
   |   d¡S )Nr(   r¿   re   r>   r>   r?   rŸ   ¤  s    z
IMAP4.noopc                 C   s(   d}|   |||||¡\}}|  ||d¡S )Nr)   r   r½   )rV   Zmessage_numZmessage_partÚstartÚlengthr¡   r¢   r£   r>   r>   r?   Úpartial¯  s    zIMAP4.partialc                 C   s   d}|   d|¡S )Nr*   r¿   )rV   rÜ   r¡   r>   r>   r?   Ú	proxyauth»  s    	zIMAP4.proxyauthc                 C   s   |   d||¡S )Nr+   r¿   )rV   Z
oldmailboxZ
newmailboxr>   r>   r?   ÚrenameÈ  s    zIMAP4.renamec                 G   sX   d}|r4| j rt d¡‚| j|d|g|¢R Ž \}}n| j|g|¢R Ž \}}|  |||¡S )Nr,   z'Non-None charset not valid in UTF8 modeÚCHARSET)r\   r   r@   r±   rž   )rV   ÚcharsetZcriteriar¡   r¢   r£   r>   r>   r?   ÚsearchÐ  s    
zIMAP4.searchr¨   Fc                 C   sv   i | _ || _|rd}nd}|  ||¡\}}|dkr@d| _||fS d| _d| j v rb|sb|  d| ¡‚|| j  dd g¡fS )	Nr   r-   rh   r   r   ú	READ-ONLYz%s is not writableÚEXISTS)rJ   rL   r±   rG   rC   Úget)rV   r²   rC   r¡   r¢   r£   r>   r>   r?   Úselectâ  s    
ÿzIMAP4.selectc                 C   s   |   d|||¡S )Nr.   r¿   )rV   r²   rÈ   Zwhatr>   r>   r?   Úsetacl  s    zIMAP4.setaclc                 G   s$   | j dg|¢R Ž \}}|  ||d¡S )Nr/   rÏ   r½   )rV   r~   r¢   r£   r>   r>   r?   Úsetannotation
  s    zIMAP4.setannotationc                 C   s    |   d||¡\}}|  ||d¡S )Nr0   rÒ   r½   )rV   rÓ   Zlimitsr¢   r£   r>   r>   r?   Úsetquota  s    zIMAP4.setquotac                 G   sH   d}|d |d fdkr d| }| j |||g|¢R Ž \}}|  |||¡S )Nr1   r   r   r©   r¬   r½   )rV   Zsort_criteriarò   Úsearch_criteriar¡   r¢   r£   r>   r>   r?   Úsort  s
    z
IMAP4.sortc                 C   s¦   d}t s|  d¡‚| jr"|  d¡‚|| jvr6|  d¡‚|d u rFt ¡ }|  |¡\}}|dkrŽ|j| j	| j
d| _	| j	 d¡| _d| _|  ¡  n
|  d	¡‚|  |||¡S )
Nr2   zSSL support missingzTLS session already establishedzTLS not supported by serverrh   ©Zserver_hostnamer‡   TzCouldn't establish TLS session)ÚHAVE_SSLr@   rN   rB   rq   ÚsslÚ_create_stdlib_contextr±   Úwrap_socketrˆ   rW   r‰   rŠ   ro   rž   )rV   Ússl_contextr¡   r¢   r£   r>   r>   r?   Ústarttls)  s&    



ÿ

zIMAP4.starttlsc                 C   s$   d}|   |||¡\}}|  |||¡S )Nr3   r½   )rV   r²   Únamesr¡   r¢   r£   r>   r>   r?   Ústatus@  s    zIMAP4.statusc                 C   s>   |d |d fdkrd| }|   d|||¡\}}|  ||d¡S )Nr   r   r©   r¬   r4   r   r½   )rV   rÃ   Úcommandr³   r¢   r£   r>   r>   r?   ÚstoreL  s    zIMAP4.storec                 C   s   |   d|¡S )Nr5   r¿   rÅ   r>   r>   r?   Ú	subscribeW  s    zIMAP4.subscribec                 G   s,   d}| j |||g|¢R Ž \}}|  |||¡S )Nr6   r½   )rV   Zthreading_algorithmrò   rû   r¡   r¢   r£   r>   r>   r?   Úthread_  s    zIMAP4.threadc              	   G   sˆ   |  ¡ }|tvr|  d| ¡‚| jt| vrL|  d|| jd t| ¡f ¡‚d}| j||g|¢R Ž \}}|dv rv|}nd}|  |||¡S )NzUnknown IMAP4 UID command: %sú9command %s illegal in state %s, only allowed in states %sú, r7   )r,   r1   r6   r   )r¥   rt   r@   rG   Újoinr±   rž   )rV   r  r~   r¡   r¢   r£   r>   r>   r?   Úuidi  s    ÿþz	IMAP4.uidc                 C   s   |   d|¡S )Nr8   r¿   rÅ   r>   r>   r?   Úunsubscribe‚  s    zIMAP4.unsubscribec                 C   s*   z|   d¡\}}W d| _nd| _0 ||fS )Nr9   r   rÁ   )rV   r¢   r–   r>   r>   r?   ÚunselectŠ  s    	zIMAP4.unselectc                 G   s.   |  ¡ }|tvr| jft|< | j|g|¢R Ž S rz   )r¥   rt   rG   r±   ©rV   r¡   r~   r>   r>   r?   Úxatomš  s    
zIMAP4.xatomc                 C   s8   |d u rd}| j }||v r*||  |¡ n
|g||< d S ©NrA   )rJ   r‘   r¶   )rV   r¢   r£   Úurr>   r>   r?   Ú_append_untagged°  s    zIMAP4._append_untaggedc                 C   s,   | j  d¡}|r(|  |d  | jd¡¡‚d S )NÚBYEr   r·   )rJ   rö   rB   rº   r]   )rV   Úbyer>   r>   r?   Ú
_check_bye¾  s    zIMAP4._check_byec           
   
   G   sî  | j t| vr4d | _|  d|| j d t| ¡f ¡‚dD ]}|| jv r8| j|= q8d| jv rj| jsj|  d¡‚|  ¡ }t	|| j
ƒ}|d | }|D ]0}|d u rœqŽt|tƒr²t	|| j
ƒ}|d | }qŽ| j}|d ur
d | _t|ƒt| jƒu rî|}nd }|t	dt|ƒ | j
ƒ }z|  |t ¡ W n4 tyP }	 z|  d|	 ¡‚W Y d }	~	n
d }	~	0 0 |d u r`|S |  ¡ r~| j| r`|S q`|rŽ|| jƒ}z|  |¡ |  t¡ W n4 tyÚ }	 z|  d|	 ¡‚W Y d }	~	n
d }	~	0 0 |s`qêq`|S )	Nr
  r  ©rh   ÚNOÚBADrô   z#mailbox status changed to READ-ONLYó    z {%s}zsocket error: %s)rG   rt   rH   r@   r  rJ   rL   rC   Ú_new_tagÚbytesr]   rä   rå   ÚtypeÚ_commandr‘   r—   r°   rT   rB   rn   rI   rK   )
rV   r¡   r~   r¢   Útagr–   ÚargrH   Z	literatorÚvalr>   r>   r?   r  Ä  s`    ÿþ


ÿ



$



$zIMAP4._commandc              
   C   sÊ   |dk}|s|   ¡  z| j||d\}}W nn | jyb } z|  d||f ¡‚W Y d }~n@d }~0  | jy˜ } z|  d||f ¡‚W Y d }~n
d }~0 0 |s¦|   ¡  |dkrÂ|  d|||f ¡‚||fS )Nr   )Ú
expect_byezcommand: %s => %sr  z%s command error: %s %s)r  Ú_get_tagged_responserB   r@   )rV   r¡   r   r}   r¢   r–   r"  r>   r>   r?   Ú_command_complete  s    &(zIMAP4._command_completec                 C   sJ   |   ¡ \}}|d gkr |  d¡‚t|d | jƒ}| ¡ }t| ¡ ƒ| _d S )Nz"no CAPABILITY response from serverr   )r¾   r@   rå   r]   r¥   ÚtupleÚsplitrq   rÂ   r>   r>   r?   ro   #  s    

zIMAP4._get_capabilitiesc                 C   s®  |   ¡ }|  | j|¡rp| j d¡}|| jvr:|  d| ¡‚| j d¡}t|| jƒ}| j d¡}||gf| j|< nöd }|  t	|¡sš|  | j
|¡rš| j d¡}| jd u rÐ|  t|¡rÂ| j d¡| _d S |  d| ¡‚| j d¡}t|| jƒ}| j d¡}|d u rd}|r|d | }|  | j|¡rZt| j d	¡ƒ}|  |¡}|  |||f¡ |   ¡ }q|  ||¡ |d
v rª|  t|¡rª| j d¡}t|| jƒ}|  || j d¡¡ |S )Nr   zunexpected tagged response: %rr  r–   Zdata2zunexpected response: %rrA   r  r   r  )Ú	_get_lineÚ_matchrl   ÚmoÚgrouprI   rB   rå   r]   ÚUntagged_responserd   ÚContinuationrK   rb   Úintr‹   r  ÚResponse_code)rV   Úrespr   r¢   r£   Zdat2r   r–   r>   r>   r?   rn   ,  sD    


zIMAP4._get_responsec              
   C   sˆ   | j | }|d ur| j |= |S |rDd}| j |d ¡}|d urD||fS |  ¡  z|  ¡  W q  | jy€ } z‚ W Y d }~q d }~0 0 q d S )Nr  )rI   rJ   Úpopr  rn   rB   )rV   r   r#  Úresultr¢   r  r"  r>   r>   r?   r$  |  s    
zIMAP4._get_tagged_responsec                 C   s>   |   ¡ }|s|  d¡‚| d¡s.|  d| ¡‚|d d… }|S )Nzsocket error: EOFr   z#socket error: unterminated line: %réþÿÿÿ)r   rB   Úendswithr’   r>   r>   r?   r(    s    

zIMAP4._get_linec                 C   s   |  |¡| _| jd uS rz   )Úmatchr*  )rV   ZcreÚsr>   r>   r?   r)  °  s    zIMAP4._matchc                 C   s2   | j tt| jƒ| jƒ }| jd | _d | j|< |S )NrŽ   )rk   r  rå   rM   r]   rI   )rV   r   r>   r>   r?   r  ¼  s    
zIMAP4._new_tagc                 C   s$   |  dd¡}|  dd¡}d| d S )Nú\z\\ú"z\")r·   )rV   r!  r>   r>   r?   rÛ   Ä  s    zIMAP4._quotec                 G   s   |   || j|g|¢R Ž ¡S rz   )r%  r  r  r>   r>   r?   r±   Ì  s    zIMAP4._simple_commandc                 C   s8   |dkr||fS || j vr$|d gfS | j  |¡}||fS )Nr  )rJ   r1  )rV   r¢   r£   r¡   r–   r>   r>   r?   rž   Ñ  s    

zIMAP4._untagged_response)rÖ   r×   )rÖ   r×   )r¨   F)N)F)Tr;   r<   r=   rR   r@   rB   rC   Ú
IMAP4_PORTrZ   rO   rg   rQ   ry   r{   r   r†   rP   r‹   r   r—   rS   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ï   rð   ró   r÷   rø   rù   rú   rü   r  r  r  r  r	  r  r  r  r  r  r  r  r%  ro   rn   r$  r(  r)  r  rÛ   r±   rž   Ú_mesgZ_dump_urÚ_logZ	print_logr>   r>   r>   r?   r   ‡   s˜   1*		
	


 	

M	P
!$r   c                   @   s:   e Zd Zdeddddfdd„Zdd„ Zdedfdd„ZdS )	Ú	IMAP4_SSLrD   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 )Nz8ssl_context and keyfile arguments are mutually exclusivez9ssl_context and certfile arguments are mutually exclusiver   zEkeyfile and certfile are deprecated, use a custom ssl_context insteadé   )ÚcertfileÚkeyfile)r€   ÚwarningsÚwarnÚDeprecationWarningr?  r>  rÿ   r   r  r   rZ   )rV   rW   rX   r?  r>  r  rY   r@  r>   r>   r?   rZ     s"    ÿÿzIMAP4_SSL.__init__c                 C   s   t  | |¡}| jj|| jdS )Nrý   )r   r†   r  r  rW   )rV   rY   rˆ   r>   r>   r?   r†   .  s    ÿzIMAP4_SSL._create_socketc                 C   s   t  | |||¡ d S rz   )r   rP   rU   r>   r>   r?   rP   3  s    zIMAP4_SSL.open)r;   r<   r=   ÚIMAP4_SSL_PORTrZ   r†   rP   r>   r>   r>   r?   r<    s
   ÿ
r<  c                   @   s>   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )r   c                 C   s   || _ t | ¡ d S rz   )r  r   rZ   )rV   r  r>   r>   r?   rZ   J  s    zIMAP4_stream.__init__Nc                 C   sN   d | _ d | _d | _d | _tj| jttjtjddd| _	| j	j
| _| j	j| _d S )NT)ÚbufsizeÚstdinÚstdoutÚshellÚ	close_fds)rW   rX   rˆ   rŠ   Ú
subprocessÚPopenr  r   ÚPIPEr¹   rE  Ú	writefilerF  ÚreadfilerU   r>   r>   r?   rP   O  s    ý
zIMAP4_stream.openc                 C   s   | j  |¡S rz   )rM  r‹   rŒ   r>   r>   r?   r‹   _  s    zIMAP4_stream.readc                 C   s
   | j  ¡ S rz   )rM  r   re   r>   r>   r?   r   d  s    zIMAP4_stream.readlinec                 C   s   | j  |¡ | j  ¡  d S rz   )rL  ÚwriteÚflushr•   r>   r>   r?   r—   i  s    zIMAP4_stream.sendc                 C   s"   | j  ¡  | j ¡  | j ¡  d S rz   )rM  r™   rL  r¹   Úwaitre   r>   r>   r?   rS   o  s    

zIMAP4_stream.shutdown)NNN)	r;   r<   r=   rZ   rP   r‹   r   r—   rS   r>   r>   r>   r?   r   >  s   
r   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
r¸   c                 C   s
   || _ d S rz   )r»   )rV   Zmechinstr>   r>   r?   rZ   }  s    z_Authenticator.__init__c                 C   s&   |   |  |¡¡}|d u rdS |  |¡S )Nó   *)r»   rº   ræ   )rV   r–   Úretr>   r>   r?   r¹   €  s    z_Authenticator.processc                 C   sn   d}t |tƒr| d¡}|rjt|ƒdkrB|d d… }|dd … }n|}d}t |¡}|r||d d…  }q|S )NrA   rf   é0   r   )rä   rå   ræ   r‘   ÚbinasciiZ
b2a_base64)rV   ÚinpZoupÚtÚer>   r>   r?   ræ   †  s    	


z_Authenticator.encodec                 C   s   |sdS t  |¡S r  )rT  Z
a2b_base64)rV   rU  r>   r>   r?   rº   ž  s    z_Authenticator.decodeN)r;   r<   r=   rZ   r¹   ræ   rº   r>   r>   r>   r?   r¸   w  s   r¸   z0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Decrá   c                 C   s   i | ]\}}|  ¡ |d  “qS )rŽ   )ræ   )Ú.0Únr6  r>   r>   r?   Ú
<dictcomp>¤  rA   rZ  rŽ   c              	   C   sØ   t  | ¡}|sd S t| d¡ }| d¡}t| d¡ƒ}t| d¡ƒ}t| d¡ƒ}t| d¡ƒ}t| d¡ƒ}t| d¡ƒ}	t| d	¡ƒ}
|	d
 |
 d
 }|dkrª| }||||||dddf	}t |¡| }t |¡S )NÚmonÚzonenÚdayÚyearÚhourÚminÚsecÚzonehÚzonemé<   ó   -r   )	ÚInternalDater5  ÚMon2numr+  r.  ÚcalendarZtimegmÚtimeÚ	localtime)r0  r*  r[  r\  r]  r^  r_  r`  ra  rb  rc  ÚzoneÚttÚutcr>   r>   r?   r   ¦  s$    

r   c                 C   s@   d}d}t t| ƒƒ} | r<t| dƒ\} }|||d … | }q|S )NrA   s   ABCDEFGHIJKLMNOPé   rŽ   )r.  ÚabsÚdivmod)Únumr"  ZAPÚmodr>   r>   r?   r	   É  s    r	   c                 C   s$   t  | ¡}|sdS t| d¡ ¡ ƒS )Nr>   r³   )ÚFlagsr5  r&  r+  r'  )r0  r*  r>   r>   r?   r
   Ö  s    
r
   c                 C   s  t | ttfƒr"t | tj¡ ¡ }nàt | tƒr¶z
| j	}W nX t
yŽ   tjr‚| d }|dkrnt t | ¡¡d }tjtjf|  }ntj }Y n0 t|d}t| d d… dt|ƒiŽ}nLt | tƒrØ| jd u rÒtdƒ‚| }n*t | tƒrú| d | d fdkrú| S td	ƒ‚d
 t|j ¡}| |¡S )Né   r   )Zsecondsé   Útzinfozdate_time must be awarer   )r8  r8  zdate_time not of a known typez"%d-{}-%Y %H:%M:%S %z")rä   r.  Úfloatr   Zfromtimestampr   rm  Z
astimezoner&  Ú	tm_gmtoffrw   ri  Údaylightrj  ÚmktimeÚaltzoner   rv  r€   rå   ÚformatÚMonthsZmonthÚstrftime)r´   ZdtZgmtoffÚdstZdeltaÚfmtr>   r>   r?   r   á  s2    ÿ





r   Ú__main__zd:s:)r>   r>   z-dz-s)rD   zIMAP password for %s on %s: Ú	localhostzJFrom: %(user)s@localhost%(lf)sSubject: IMAP4 test%(lf)s%(lf)sdata...%(lf)sÚ
)rÜ   ZlfrÞ   )rÆ   )ú
/tmp/xxx 1)rð   )r„  z/tmp/yyy)r   ©ú
/tmp/yyz 2r¶   r†  )rÚ   )z/tmpzyy*)r÷   r…  )ró   )NZSUBJECTÚtest)rÌ   )Ú1z(FLAGS INTERNALDATE RFC822))r  )rˆ  ÚFLAGSz
(\Deleted))rë   r>   )rË   r>   )r¤   r>   )r™   r>   )r÷   r>   )r§   )ZUIDVALIDITY)r  )r,   ZALL)r§   )rõ   )r}   r>   c                 C   sL   t  d| |f ¡ tt | ƒ|Ž \}}t  d| ||f ¡ |dkrH|d ‚|S )Nz%s %sz%s => %s %sr  r   )ÚMr:  ru   )Úcmdr~   r¢   r£   r>   r>   r?   ÚrunB  s
    rŒ  r   zPROTOCOL_VERSION = %szCAPABILITIES = %rrÚ   )z/tmp/zyy%z.*"([^"]+)"$r   rÇ   r  r   z%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
)Trm   rT  r›   ri   r^   rƒ   rI  r   ri  rh  r   r   r   Úior   rÿ   rþ   ÚImportErrorÚ__all__r°   rE   r9  rC  rp   r   rt   r_   r-  rs  rf  ra   rb   r®   r/  r,  rd   r`   rc   r   r<  r¶   r   r¸   r'  r}  Ú	enumeraterg  r   r	   r
   r   r;   ÚgetoptZgetpassÚargvZoptlistr~   r@   r"  Zstream_commandÚoptr.  rW   ZgetuserZUSERZPASSWDZ	test_mesgZ	test_seq1Z	test_seq2rŒ  rŠ  rG   r:  rr   rq   r‹  Úmlr5  r*  r+  Úpathr£   r  Úprintr>   r>   r>   r?   Ú<module>   sJ  H
	Õ0



	

ÿ         6
9,
#)





òù



ÿ
ý