B
    u9a,Ð  ã            *   @   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 yddlZdZW n ek
r”   dZY nX ddd	d
ddg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œ)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„ Z2e3d/kr¦ddl4Z4ddl5Z5ye4 4ej6d*d… d0¡\Z7Z8W n. e4j9k
rl Z: zd1\Z7Z8W ddZ:[:X Y nX dZ;x@e7D ]8\Z<Z:e<d2kr”e=e:ƒZne<d3krxe:Z;e8sxe;fZ8qxW e8s¾d4Z8e8d Z>e5 ?¡ Z@e5 5d5e@e>pÞd6f ¡ZAd7e@d8d9œ ZBd:e@eAffd;d<d=d>d?ddeBffd@dAdBdCdDdEdFdGdHfZCdIdJdKdLd>dddeBffdGdMfZDdNdO„ ZEy&e;rbe)e;ƒZFne&e>ƒZFeFjGdPkr‚eCd*d… ZCeF HdQeFjI ¡ eF HdReFjJf ¡ xeCD ]\ZKZ8eEeKe8ƒ qªW xJeEdSdTƒD ]<ZLe MdUeL¡ZNeNrðeN Od*¡ZPneL +¡ dV ZPeEdWePfƒ qÎW x\eDD ]T\ZKZ8eEeKe8ƒZQeKe8fdKkr8qeQdV  +¡ ZReRsNqeEdXdYdZeRdV  d[fƒ qW eSd\ƒ W n.   eSd]ƒ esžeSd^ej6d  ƒ ‚ Y nX dS )_z2.58é    N)ÚdatetimeÚtimezoneÚ	timedelta)ÚDEFAULT_BUFFER_SIZETFÚIMAP4ÚIMAP4_streamÚInternaldate2tupleÚInt2APÚ
ParseFlagsÚTime2Internaldates   
é   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ÚUNSUBSCRIBEs   \+( (?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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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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Z d:d;„ Z!d<d=„ Z"d>d?„ Z#d@dA„ Z$dBdC„ Z%dDdE„ Z&dFdG„ Z'd›dJdK„Z(dLdM„ Z)dNdO„ Z*dPdQ„ Z+dRdS„ Z,dœdTdU„Z-dVdW„ Z.dXdY„ Z/dZd[„ Z0d\d]„ Z1d^d_„ Z2d`da„ Z3dbdc„ Z4ddfdg„Z5dhdi„ Z6djdk„ Z7dldm„ Z8dndo„ 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Ž„ ZHdd„ ZId‘d’„ ZJd“d”„ ZKd•d–„ ZLd—d˜„ ZMd™dš„ ZNdpS )Ÿr   c               @   s   e Zd ZdS )zIMAP4.errorN)Ú__name__Ú
__module__Ú__qualname__© r;   r;   ú/usr/lib/python3.7/imaplib.pyÚerror´   s    r=   c               @   s   e Zd ZdS )zIMAP4.abortN)r8   r9   r:   r;   r;   r;   r<   Úabortµ   s    r>   c               @   s   e Zd ZdS )zIMAP4.readonlyN)r8   r9   r:   r;   r;   r;   r<   Úreadonly¶   s    r?   Ú c             C   s”   t | _d| _d | _i | _i | _d| _d| _d| _d| _	|  
¡  |  ||¡ y|  ¡  W n8 tk
rŽ   y|  ¡  W n tk
r†   Y nX ‚ Y nX d S )Nr   r@   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Úportr;   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)rQ   r;   r;   r<   rK   Ñ   s    zIMAP4._mode_asciic             C   s(   d| _ d| _t t¡| _t t¡| _d S )NTzutf-8)rV   rW   rX   rY   rZ   r\   r]   r^   )rQ   r;   r;   r<   Ú
_mode_utf8Ø   s    zIMAP4._mode_utf8c             C   sž   t t dd¡ƒ| _t d| j d tj¡| _|  ¡ | _	d| j
krHd| _nd| j
krZd| _n|  | j	¡‚|  ¡  x tD ]}|| jkr„qt|| _d S W |  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	   ÚrandomZrandintÚtagprerX   rY   r[   ÚtagreÚ_get_responseZwelcomerF   rC   r=   Ú_get_capabilitiesÚAllowedVersionsÚcapabilitiesÚPROTOCOL_VERSION)rQ   Úversionr;   r;   r<   rM   ß   s     




zIMAP4._connectc             C   s&   |t krt| | ¡ ƒS td| ƒ‚d S )NzUnknown IMAP4 command: '%s')ÚCommandsÚgetattrÚlowerÚAttributeError)rQ   Úattrr;   r;   r<   Ú__getattr__	  s    zIMAP4.__getattr__c             C   s   | S )Nr;   )rQ   r;   r;   r<   Ú	__enter__  s    zIMAP4.__enter__c             G   s&   y|   ¡  W n tk
r    Y nX d S )N)ÚlogoutrP   )rQ   Úargsr;   r;   r<   Ú__exit__  s    zIMAP4.__exit__c             C   s    | j s
d n| j }t || jf¡S )N)rR   ÚsocketZcreate_connectionrS   )rQ   rR   r;   r;   r<   Ú_create_socket  s    zIMAP4._create_socketc             C   s(   || _ || _|  ¡ | _| j d¡| _d S )NÚrb)rR   rS   ru   ÚsockÚmakefileÚfile)rQ   rR   rS   r;   r;   r<   rL   #  s    
z
IMAP4.openc             C   s   | j  |¡S )N)ry   Úread)rQ   Úsizer;   r;   r<   rz   /  s    z
IMAP4.readc             C   s.   | j  td ¡}t|ƒtkr*|  dt ¡‚|S )Né   zgot more than %d bytes)ry   ÚreadlineÚ_MAXLINEÚlenr=   )rQ   Úliner;   r;   r<   r}   4  s    zIMAP4.readlinec             C   s   | j  |¡ d S )N)rw   Zsendall)rQ   Údatar;   r;   r<   Úsend<  s    z
IMAP4.sendc          
   C   st   | j  ¡  zXy| j tj¡ W n@ tk
r^ } z"|jtjkrNt	|ddƒdkrN‚ W d d }~X Y nX W d | j ¡  X d S )NZwinerrorr   i&'  )
ry   Úcloserw   rO   rt   Z	SHUT_RDWRrP   ÚerrnoZENOTCONNrk   )rQ   Úexcr;   r;   r<   rO   A  s    
zIMAP4.shutdownc             C   s   | j S )N)rw   )rQ   r;   r;   r<   rt   Q  s    zIMAP4.socketc             C   sB   d}|   dd g|¡\}}|d r(||fS |  ¡ \}}|   |||¡S )NZRECENTr`   éÿÿÿÿ)Ú_untagged_responseÚnoop)rQ   ÚnameÚtypÚdatr;   r;   r<   Úrecent]  s    	zIMAP4.recentc             C   s   |   |d g| ¡ ¡S )N)r‡   Úupper)rQ   Úcoder;   r;   r<   Úresponsen  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†   )ú(ú)z(%s)s   UTF8 (ó   ))r   ÚMapCRLFÚsubÚCRLFrV   rD   Ú_simple_command)rQ   ÚmailboxÚflagsÚ	date_timeÚmessager‰   rD   r;   r;   r<   Úappend|  s    

zIMAP4.appendc             C   sP   |  ¡ }t|ƒj| _|  d|¡\}}|dkrB|  |d  dd¡¡‚d| _||fS )Nr   r`   r†   zutf-8Úreplacer   )r   Ú_AuthenticatorÚprocessrD   r—   r=   ÚdecoderC   )rQ   Z	mechanismZ
authobjectÚmechrŠ   r‹   r;   r;   r<   Úauthenticate–  s    zIMAP4.authenticatec             C   s    d}|   |¡\}}|  |||¡S )Nr   )r—   r‡   )rQ   r‰   rŠ   r‹   r;   r;   r<   Ú
capability³  s    zIMAP4.capabilityc             C   s
   |   d¡S )Nr   )r—   )rQ   r;   r;   r<   Úcheck¼  s    zIMAP4.checkc             C   s$   z|   d¡\}}W d d| _X ||fS )Nr   r   )r—   rC   )rQ   rŠ   r‹   r;   r;   r<   rƒ   Ä  s    zIMAP4.closec             C   s   |   d||¡S )Nr   )r—   )rQ   Úmessage_setZnew_mailboxr;   r;   r<   ÚcopyÓ  s    z
IMAP4.copyc             C   s   |   d|¡S )Nr   )r—   )rQ   r˜   r;   r;   r<   ÚcreateÛ  s    zIMAP4.createc             C   s   |   d|¡S )Nr   )r—   )rQ   r˜   r;   r;   r<   Údeleteã  s    zIMAP4.deletec             C   s   |   d||¡S )Nr   )r—   )rQ   r˜   Úwhor;   r;   r<   Ú	deleteaclê  s    zIMAP4.deleteaclc             C   sH   d| j krt d¡‚|  d|¡\}}|dkr@d| ¡ kr@|  ¡  ||fS )Nr   zServer does not support ENABLEr`   zUTF8=ACCEPT)rg   r   r=   r—   r   r_   )rQ   r£   rŠ   r   r;   r;   r<   Úenableñ  s    

zIMAP4.enablec             C   s    d}|   |¡\}}|  |||¡S )Nr   )r—   r‡   )rQ   r‰   rŠ   r‹   r;   r;   r<   Úexpungeý  s    	zIMAP4.expungec             C   s$   d}|   |||¡\}}|  |||¡S )Nr   )r—   r‡   )rQ   r¥   Zmessage_partsr‰   rŠ   r‹   r;   r;   r<   Úfetch  s    
zIMAP4.fetchc             C   s   |   d|¡\}}|  ||d¡S )Nr   ZACL)r—   r‡   )rQ   r˜   rŠ   r‹   r;   r;   r<   Úgetacl  s    zIMAP4.getaclc             C   s"   |   d|||¡\}}|  ||d¡S )Nr   Ú
ANNOTATION)r—   r‡   )rQ   r˜   ÚentryZ	attributerŠ   r‹   r;   r;   r<   Úgetannotation#  s    zIMAP4.getannotationc             C   s   |   d|¡\}}|  ||d¡S )Nr    ÚQUOTA)r—   r‡   )rQ   ÚrootrŠ   r‹   r;   r;   r<   Úgetquota+  s    zIMAP4.getquotac             C   s@   |   d|¡\}}|  ||d¡\}}|  ||d¡\}}|||gfS )Nr!   r²   Z	QUOTAROOT)r—   r‡   )rQ   r˜   rŠ   r‹   ZquotaZ	quotarootr;   r;   r<   Úgetquotaroot6  s    zIMAP4.getquotarootú""Ú*c             C   s$   d}|   |||¡\}}|  |||¡S )Nr#   )r—   r‡   )rQ   Ú	directoryÚpatternr‰   rŠ   r‹   r;   r;   r<   ÚlistA  s    z
IMAP4.listc             C   s<   |   d||  |¡¡\}}|dkr.|  |d ¡‚d| _||fS )Nr$   r`   r†   r   )r—   Ú_quoter=   rC   )rQ   ÚuserÚpasswordrŠ   r‹   r;   r;   r<   ÚloginM  s
    zIMAP4.loginc             C   s   || | _ | _|  d| j¡S )NzCRAM-MD5)r¼   r½   r¢   Ú_CRAM_MD5_AUTH)rQ   r¼   r½   r;   r;   r<   Úlogin_cram_md5[  s    zIMAP4.login_cram_md5c             C   sB   dd l }t| jtƒr | j d¡n| j}| jd | ||d¡ ¡  S )Nr   zutf-8ú Zmd5)ÚhmacÚ
isinstancer½   ÚstrÚencoder¼   ZHMACZ	hexdigest)rQ   Z	challengerÂ   Úpwdr;   r;   r<   r¿   d  s    zIMAP4._CRAM_MD5_AUTHc             C   sj   d| _ y|  d¡\}}W n(   ddt ¡ d d…  g }}Y nX |  ¡  d| jkrbd| jd fS ||fS )Nr   ÚNOz%s: %sé   ÚBYE)rC   r—   ÚsysÚexc_inforO   rF   )rQ   rŠ   r‹   r;   r;   r<   rq   l  s      "
zIMAP4.logoutc             C   s$   d}|   |||¡\}}|  |||¡S )Nr%   )r—   r‡   )rQ   r¸   r¹   r‰   rŠ   r‹   r;   r;   r<   Úlsub|  s    z
IMAP4.lsubc             C   s   |   d|¡\}}|  ||d¡S )Nr"   )r—   r‡   )rQ   r˜   rŠ   r‹   r;   r;   r<   Úmyrights‡  s    zIMAP4.myrightsc             C   s    d}|   |¡\}}|  |||¡S )Nr&   )r—   r‡   )rQ   r‰   rŠ   r‹   r;   r;   r<   Ú	namespace  s    zIMAP4.namespacec             C   s
   |   d¡S )Nr'   )r—   )rQ   r;   r;   r<   rˆ   ™  s    z
IMAP4.noopc             C   s(   d}|   |||||¡\}}|  ||d¡S )Nr(   r   )r—   r‡   )rQ   Zmessage_numZmessage_partÚstartÚlengthr‰   rŠ   r‹   r;   r;   r<   Úpartial¤  s    zIMAP4.partialc             C   s   d}|   d|¡S )Nr)   )r—   )rQ   r¼   r‰   r;   r;   r<   Ú	proxyauth°  s    	zIMAP4.proxyauthc             C   s   |   d||¡S )Nr*   )r—   )rQ   Z
oldmailboxZ
newmailboxr;   r;   r<   Úrename½  s    zIMAP4.renamec             G   sT   d}|r2| j rt d¡‚| j|d|f|žŽ \}}n| j|f|žŽ \}}|  |||¡S )Nr+   z'Non-None charset not valid in UTF8 modeÚCHARSET)rV   r   r=   r—   r‡   )rQ   Ú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 krb|sb|  d| ¡‚|| j  dd g¡fS )	Nr   r,   r`   r   r   z	READ-ONLYz%s is not writableÚEXISTS)rF   rH   r—   rC   r?   Úget)rQ   r˜   r?   r‰   rŠ   r‹   r;   r;   r<   Úselect×  s    
zIMAP4.selectc             C   s   |   d|||¡S )Nr-   )r—   )rQ   r˜   r©   Zwhatr;   r;   r<   Úsetacl÷  s    zIMAP4.setaclc             G   s    | j d|žŽ \}}|  ||d¡S )Nr.   r¯   )r.   )r—   r‡   )rQ   rr   rŠ   r‹   r;   r;   r<   Úsetannotationÿ  s    zIMAP4.setannotationc             C   s    |   d||¡\}}|  ||d¡S )Nr/   r²   )r—   r‡   )rQ   r³   ZlimitsrŠ   r‹   r;   r;   r<   Úsetquota  s    zIMAP4.setquotac             G   sF   d}|d |d fdkr d| }| j |||f|žŽ \}}|  |||¡S )Nr0   r   r†   )r‘   r’   z(%s))r—   r‡   )rQ   Zsort_criteriarÕ   Úsearch_criteriar‰   rŠ   r‹   r;   r;   r<   Úsort  s
    z
IMAP4.sortNc             C   s¦   d}t s|  d¡‚| jr"|  d¡‚|| jkr6|  d¡‚|d krFt ¡ }|  |¡\}}|dkrŽ|j| j	| j
d| _	| j	 d¡| _d| _|  ¡  n
|  d	¡‚|  |||¡S )
Nr1   zSSL support missingzTLS session already establishedzTLS not supported by serverr`   )Úserver_hostnamerv   TzCouldn't establish TLS session)ÚHAVE_SSLr=   rJ   r>   rg   ÚsslÚ_create_stdlib_contextr—   Úwrap_socketrw   rR   rx   ry   re   r‡   )rQ   Ússl_contextr‰   rŠ   r‹   r;   r;   r<   Ústarttls  s$    





zIMAP4.starttlsc             C   s$   d}|   |||¡\}}|  |||¡S )Nr2   )r—   r‡   )rQ   r˜   Únamesr‰   rŠ   r‹   r;   r;   r<   Ústatus5  s    zIMAP4.statusc             C   s>   |d |d fdkrd| }|   d|||¡\}}|  ||d¡S )Nr   r†   )r‘   r’   z(%s)r3   r   )r—   r‡   )rQ   r¥   Úcommandr™   rŠ   r‹   r;   r;   r<   ÚstoreA  s    zIMAP4.storec             C   s   |   d|¡S )Nr4   )r—   )rQ   r˜   r;   r;   r<   Ú	subscribeL  s    zIMAP4.subscribec             G   s*   d}| j |||f|žŽ \}}|  |||¡S )Nr5   )r—   r‡   )rQ   Zthreading_algorithmrÕ   rÝ   r‰   rŠ   r‹   r;   r;   r<   ÚthreadT  s    zIMAP4.threadc          	   G   s†   |  ¡ }|tkr|  d| ¡‚| jt| krL|  d|| jd t| ¡f ¡‚d}| j||f|žŽ \}}|dkrt|}nd}|  |||¡S )NzUnknown IMAP4 UID command: %sz9command %s illegal in state %s, only allowed in states %sz, r6   )r+   r0   r5   r   )r   rj   r=   rC   Újoinr—   r‡   )rQ   rè   rr   r‰   rŠ   r‹   r;   r;   r<   Úuid^  s    z	IMAP4.uidc             C   s   |   d|¡S )Nr7   )r—   )rQ   r˜   r;   r;   r<   Úunsubscribew  s    zIMAP4.unsubscribec             G   s,   |  ¡ }|tkr| jft|< | j|f|žŽ S )N)r   rj   rC   r—   )rQ   r‰   rr   r;   r;   r<   Úxatom  s    
zIMAP4.xatomc             C   s8   |d krd}| j }||kr*||  |¡ n
|g||< d S )Nó    )rF   rœ   )rQ   rŠ   r‹   Úurr;   r;   r<   Ú_append_untagged•  s    zIMAP4._append_untaggedc             C   s,   | j  d¡}|r(|  |d  | jd¡¡‚d S )NrÉ   r†   r   )rF   rØ   r>   r    rW   )rQ   Zbyer;   r;   r<   Ú
_check_bye£  s    zIMAP4._check_byec       
   
   G   sø  | j t| kr4d | _|  d|| j d t| ¡f ¡‚xdD ]}|| jkr:| j|= q:W d| jkrn| jsn|  d¡‚|  ¡ }t	|| j
ƒ}|d | }x8|D ]0}|d kr¢q”t|tƒr¸t	|| j
ƒ}|d | }q”W | j}|d k	rd | _t|ƒt| jƒkrö|}nd }|t	dt|ƒ | j
ƒ }y|  |t ¡ W n2 tk
rV }	 z|  d|	 ¡‚W d d }	~	X Y nX |d krf|S xŒx |  ¡ rˆ| j| rj|S qjW |rš|| jƒ}y|  |¡ |  t¡ W n2 tk
rä }	 z|  d|	 ¡‚W d d }	~	X Y nX |shP qhW |S )	Nz9command %s illegal in state %s, only allowed in states %sz, )r`   rÇ   ÚBADz	READ-ONLYz#mailbox status changed to READ-ONLYó    z {%s}zsocket error: %s)rC   rj   rD   r=   rì   rF   rH   r?   Ú_new_tagÚbytesrW   rÃ   rÄ   ÚtypeÚ_commandr   r‚   r–   rP   r>   rd   rE   rG   )
rQ   r‰   rr   rŠ   Útagr   ÚargrD   Z	literatorÚvalr;   r;   r<   rù   ©  s^    




 

 



 zIMAP4._commandc          
   C   sÂ   |dkr|   ¡  y|  |¡\}}W nj | jk
rX } z|  d||f ¡‚W d d }~X Y n6 | jk
rŒ } z|  d||f ¡‚W d d }~X Y nX |dkrž|   ¡  |dkrº|  d|||f ¡‚||fS )Nr   zcommand: %s => %srô   z%s command error: %s %s)ró   Ú_get_tagged_responser>   r=   )rQ   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Ä   rW   r   ÚtupleÚsplitrg   )rQ   rŠ   r‹   r;   r;   r<   re     s    

zIMAP4._get_capabilitiesc             C   s²  |   ¡ }|  | j|¡rp| j d¡}|| jkr:|  d| ¡‚| j d¡}t|| jƒ}| j d¡}||gf| j|< núd }|  t	|¡sš|  | j
|¡rš| j d¡}| jd krÐ|  t|¡rÂ| j d¡| _d S |  d| ¡‚| j d¡}t|| jƒ}| j d¡}|d krd}|r|d | }xH|  | j|¡r\t| j d	¡ƒ}|  |¡}|  |||f¡ |   ¡ }qW |  ||¡ |d
kr®|  t|¡r®| j d¡}t|| jƒ}|  || j d¡¡ |S )Nrú   zunexpected tagged response: %rrø   r   Zdata2zunexpected response: %rrð   rõ   r{   )r`   rÇ   rô   )Ú	_get_lineÚ_matchrc   ÚmoÚgrouprE   r>   rÄ   rW   ÚUntagged_responser^   ÚContinuationrG   r\   Úintrz   rò   ÚResponse_code)rQ   Úresprú   rŠ   r‹   Zdat2r{   r   r;   r;   r<   rd     sH    


  
zIMAP4._get_responsec          
   C   sd   x^| j | }|d k	r | j |= |S |  ¡  y|  ¡  W q | jk
rZ } z‚ W d d }~X Y qX qW d S )N)rE   ró   rd   r>   )rQ   rú   Úresultrü   r;   r;   r<   rý   `  s    
zIMAP4._get_tagged_responsec             C   s>   |   ¡ }|s|  d¡‚| d¡s.|  d| ¡‚|d d… }|S )Nzsocket error: EOFs   
z#socket error: unterminated line: %réþÿÿÿ)r}   r>   Úendswith)rQ   r€   r;   r;   r<   r  {  s    

zIMAP4._get_linec             C   s   |  |¡| _| jd k	S )N)Úmatchr  )rQ   ZcreÚsr;   r;   r<   r  Ž  s    zIMAP4._matchc             C   s2   | j tt| jƒ| jƒ }| jd | _d | j|< |S )Nr|   )rb   r÷   rÄ   rI   rW   rE   )rQ   rú   r;   r;   r<   rö   š  s    
zIMAP4._new_tagc             C   s$   |  dd¡}|  dd¡}d| d S )Nú\z\\ú"z\")r   )rQ   rû   r;   r;   r<   r»   ¢  s    zIMAP4._quotec             G   s   |   || j|f|žŽ ¡S )N)rþ   rù   )rQ   r‰   rr   r;   r;   r<   r—   ª  s    zIMAP4._simple_commandc             C   s8   |dkr||fS || j kr$|d gfS | j  |¡}||fS )NrÇ   )rF   Úpop)rQ   rŠ   r‹   r‰   r   r;   r;   r<   r‡   ¯  s    

zIMAP4._untagged_response)r¶   r·   )r¶   r·   )r   F)N)Or8   r9   r:   rN   r=   r>   r?   Ú
IMAP4_PORTrT   rK   r_   rM   ro   rp   rs   ru   rL   rz   r}   r‚   rO   rt   rŒ   r   rœ   r¢   r£   r¤   rƒ   r¦   r§   r¨   rª   r«   r¬   r­   r®   r±   r´   rµ   rº   r¾   rÀ   r¿   rq   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þ   re   rd   rý   r  r  rö   r»   r—   r‡   r;   r;   r;   r<   r   †   s–   .*
		
	


 	

M	Pc               @   s6   e Zd Zdedddfdd„Zdd„ Zdefdd„ZdS )	Ú	IMAP4_SSLr@   Nc             C   s   |d k	r|d k	rt dƒ‚|d k	r0|d k	r0t dƒ‚|d k	s@|d k	rVdd l}| dtd¡ || _|| _|d k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 insteadrÈ   )ÚcertfileÚkeyfile)Ú
ValueErrorÚwarningsÚwarnÚDeprecationWarningr  r  rá   râ   rä   r   rT   )rQ   rR   rS   r  r  rä   r  r;   r;   r<   rT   ö  s    zIMAP4_SSL.__init__c             C   s   t  | ¡}| jj|| jdS )N)rß   )r   ru   rä   rã   rR   )rQ   rw   r;   r;   r<   ru   
  s    
zIMAP4_SSL._create_socketc             C   s   t  | ||¡ d S )N)r   rL   )rQ   rR   rS   r;   r;   r<   rL     s    zIMAP4_SSL.open)r8   r9   r:   ÚIMAP4_SSL_PORTrT   ru   rL   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 )N)rè   r   rT   )rQ   rè   r;   r;   r<   rT   &  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ÚshellZ	close_fds)rR   rS   rw   ry   Ú
subprocessÚPopenrè   r   ÚPIPErŸ   r  Ú	writefiler  Úreadfile)rQ   rR   rS   r;   r;   r<   rL   +  s    
zIMAP4_stream.openc             C   s   | j  |¡S )N)r#  rz   )rQ   r{   r;   r;   r<   rz   ;  s    zIMAP4_stream.readc             C   s
   | j  ¡ S )N)r#  r}   )rQ   r;   r;   r<   r}   @  s    zIMAP4_stream.readlinec             C   s   | j  |¡ | j  ¡  d S )N)r"  ÚwriteÚflush)rQ   r   r;   r;   r<   r‚   E  s    zIMAP4_stream.sendc             C   s"   | j  ¡  | j ¡  | j ¡  d S )N)r#  rƒ   r"  rŸ   Úwait)rQ   r;   r;   r<   rO   K  s    

zIMAP4_stream.shutdown)NN)	r8   r9   r:   rT   rL   rz   r}   r‚   rO   r;   r;   r;   r<   r     s   
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 )N)r¡   )rQ   Zmechinstr;   r;   r<   rT   Y  s    z_Authenticator.__init__c             C   s&   |   |  |¡¡}|d krdS |  |¡S )Nó   *)r¡   r    rÅ   )rQ   r   Úretr;   r;   r<   rŸ   \  s    z_Authenticator.processc             C   sr   d}t |tƒr| d¡}xT|rlt|ƒdkrD|d d… }|dd … }n|}d}t |¡}|r||d d…  }qW |S )Nrð   zutf-8é0   r†   )rÃ   rÄ   rÅ   r   ÚbinasciiZ
b2a_base64)rQ   ÚinpZoupÚtÚer;   r;   r<   rÅ   b  s    	


z_Authenticator.encodec             C   s   |sdS t  |¡S )Nrð   )r*  Z
a2b_base64)rQ   r+  r;   r;   r<   r    z  s    z_Authenticator.decodeN)r8   r9   r:   rT   rŸ   rÅ   r    r;   r;   r;   r<   rž   S  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Únr  r;   r;   r<   ú
<dictcomp>€  s    r0  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†   )	ÚInternalDater  ÚMon2numr  r  ÚcalendarZtimegmÚtimeÚ	localtime)r	  r  r1  r2  r3  r4  r5  r6  r7  r8  r9  ZzoneÚttÚutcr;   r;   r<   r   ‚  s$    

c             C   sD   d}d}t t| ƒƒ} x*| r>t| dƒ\} }|||d … | }qW |S )Nrð   s   ABCDEFGHIJKLMNOPé   r|   )r  ÚabsÚdivmod)Znumrü   ZAPÚmodr;   r;   r<   r	   ¥  s     c             C   s$   t  | ¡}|sdS t| d¡ ¡ ƒS )Nr;   r™   )ÚFlagsr  rÿ   r  r   )r	  r  r;   r;   r<   r
   ²  s    
c             C   s  t | ttfƒr"t | tj¡ ¡ }nât | tƒr¸y
| j	}W nZ t
k
r   tjr„| d }|dkrpt t | ¡¡d }tjtjf|  }ntj }Y nX t|d}t| d d… dt|ƒiŽ}nLt | tƒrÚ| jd k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   )r  r  zdate_time not of a known typez"%d-{}-%Y %H:%M:%S %z")rÃ   r  Úfloatr   Zfromtimestampr   rB  Z
astimezonerÿ   Z	tm_gmtoffrm   r?  Zdaylightr@  ZmktimeZaltzoner   rJ  r  rÄ   ÚformatÚMonthsZmonthZstrftime)rš   ZdtZgmtoffÚdstZdeltaZfmtr;   r;   r<   r   ½  s0    




Ú__main__zd:s:)r;   r;   z-dz-s)r@   zIMAP password for %s on %s: Z	localhostzJFrom: %(user)s@localhost%(lf)sSubject: IMAP4 test%(lf)s%(lf)sdata...%(lf)sÚ
)r¼   Zlfr¾   )r§   )z
/tmp/xxx 1)rÓ   )z
/tmp/xxx 1z/tmp/yyy)r   )z
/tmp/yyz 2rœ   z
/tmp/yyz 2)rº   )z/tmpzyy*)rÙ   )z
/tmp/yyz 2)rÖ   )NZSUBJECTZtest)r­   )Ú1z(FLAGS INTERNALDATE RFC822))ré   )rQ  ÚFLAGSz
(\Deleted))rÎ   r;   )r¬   r;   )rŒ   r;   )rƒ   r;   )rÙ   r;   )r   )ZUIDVALIDITY)rí   )r+   ZALL)r   )r×   )rq   r;   c             C   sL   t  d| |f ¡ tt | ƒ|Ž \}}t  d| ||f ¡ |dkrH|d ‚|S )Nz%s %sz%s => %s %srÇ   r   )ÚMÚ_mesgrk   )Úcmdrr   rŠ   r‹   r;   r;   r<   Úrun  s     rV  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
)TÚ__version__r*  r„   ra   rX   rt   r  rÊ   r?  r>  r   r   r   Úior   rá   rà   ÚImportErrorÚ__all__r–   rA   r  r  rf   r~   rj   rY   r  rG  r<  r[   r\   r”   r  r  r^   rZ   r]   r   r  rœ   r   rž   r   rM  Ú	enumerater=  r   r	   r
   r   r8   ZgetoptZgetpassÚargvZoptlistrr   r=   rü   Zstream_commandÚoptr  rR   ZgetuserZUSERZPASSWDZ	test_mesgZ	test_seq1Z	test_seq2rV  rS  rC   rT  rh   rg   rU  Úmlr  r  r  Úpathr‹   rí   Úprintr;   r;   r;   r<   Ú<module>   sD  H

	



	


        c4
9,
#)



  

 
 