B &=™'×¼þã*@s®dZdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z m Z m Z ddlmZyddlZdZWnek r˜dZYnXdd d d d d gZdZdZdZdZdZdZddddddddddddddddddddddddddddddddddddddddddœ)Ze d¡Ze d¡Ze d¡Ze dej¡Z e d¡Z!e d¡Z"e d¡Z#e d ej¡Z$dZ%d Z&Gd!d„dƒZ'er¨Gd"d#„d#e'ƒZ(e )d#¡Gd$d „d e'ƒZ*Gd%d&„d&ƒZ+d' ,d(¡Z-d)d*„e.e-d+d…ƒDƒZ/d,d „Z0d-d „Z1d.d „Z2d/d „Z3e4d0krªddl5Z5ddl6Z6ye5 5ej7d+d…d1¡\Z8Z9Wn.e5j:k rpZ;z d2\Z8Z9WddZ;[;XYnXdZe;ƒZne=d4kr|e;Zd?d@ddeCffdAdBdCdDdEdFdGdHdIfZDdJdKdLdMd?dddeCffdHdNfZEdOdP„ZFy&e<rfe*e<ƒZGne'e?ƒZGeGjHdQkr†eDd+d…ZDeG IdReGjJ¡eG IdSeGjKf¡xeDD]\ZLZ9eFeLe9ƒq®WxJeFdTdUƒD].*))?s.*FLAGS \((?P[^\)]*)\)sÕ.*INTERNALDATE "(?P[ 0123][0-9])-(?P[A-Z][a-z][a-z])-(?P[0-9][0-9][0-9][0-9]) (?P[0-9][0-9]):(?P[0-9][0-9]):(?P[0-9][0-9]) (?P[-+])(?P[0-9][0-9])(?P[0-9][0-9])"s.*{(?P\d+)}$s \r\n|\r|\ns%\[(?P[A-Z-]+)( (?P.*))?\]s$\* (?P[A-Z-]+)( (?P.*))?s3\* (?P\d+) (?P[A-Z-]+)( (?P.*))?c@sŒeZdZdZGdd„deƒZGdd„deƒZGdd„deƒZdefd d „Z d d „Z d d„Z dd„Z dd„Z dd„Zdd„Zdd„Zdefdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Z d9d:„Z!d;d<„Z"d=d>„Z#d?d@„Z$dAdB„Z%dCdD„Z&dEdF„Z'dGdH„Z(dœdKdL„Z)dMdN„Z*dOdP„Z+dQdR„Z,dSdT„Z-ddUdV„Z.dWdX„Z/dYdZ„Z0d[d\„Z1d]d^„Z2d_d`„Z3dadb„Z4dcdd„Z5dždgdh„Z6didj„Z7dkdl„Z8dmdn„Z9dodp„Z:dŸdrds„Z;dtdu„Zdzd{„Z?d|d}„Z@d~d„ZAd€d„ZBd‚dƒ„ZCd„d…„ZDd†d‡„ZEdˆd‰„ZFdŠd‹„ZGdŒd„ZHdŽd„ZIdd‘„ZJd’d“„ZKd”d•„ZLd–d—„ZMd˜d™„ZNdšd›„ZOdqS) raëIMAP4 client class. Instantiate with: IMAP4([host[, port]]) host - host's name (default: localhost); port - port number (default: standard IMAP4 port). All IMAP4rev1 commands are supported by methods of the same name (in lower-case). All arguments to commands are converted to strings, except for AUTHENTICATE, and the last argument to APPEND which is passed as an IMAP4 literal. If necessary (the string contains any non-printing characters or white-space and isn't enclosed with either parentheses or double quotes) each string is quoted. However, the 'password' argument to the LOGIN command is always quoted. If you want to avoid having an argument string quoted (eg: the 'flags' argument to STORE) then enclose the string in parentheses (eg: "(\Deleted)"). Each command returns a tuple: (type, [data, ...]) where 'type' is usually 'OK' or 'NO', and 'data' is either the text from the tagged response, or untagged results from command. Each 'data' is either a string, or a tuple. If a tuple, then the first part is the header of the response, and the second part contains the data (ie: 'literal' value). Errors raise the exception class .error(""). IMAP4 server errors raise .abort(""), which is a sub-class of 'error'. Mailbox status changes from READ-WRITE to READ-ONLY raise the exception class .readonly(""), which is a sub-class of 'abort'. "error" exceptions imply a program error. "abort" exceptions imply the connection should be reset, and the command re-tried. "readonly" exceptions imply the command should be re-tried. Note: to use this module, you must read the RFCs pertaining to the IMAP4 protocol, as the semantics of the arguments to each IMAP4 command are left to the invoker, not to mention the results. Also, most IMAP servers implement a sub-set of the commands available here. c@s eZdZdS)z IMAP4.errorN)Ú__name__Ú __module__Ú __qualname__©r;r;ú*/opt/alt/python37/lib/python3.7/imaplib.pyÚerror´sr=c@s eZdZdS)z IMAP4.abortN)r8r9r:r;r;r;r<Úabortµsr>c@s eZdZdS)zIMAP4.readonlyN)r8r9r:r;r;r;r<Úreadonly¶sr?Úc Cs”t|_d|_d|_i|_i|_d|_d|_d|_d|_ |  ¡|  ||¡y |  ¡Wn8t k rŽy | ¡Wntk r†YnX‚YnXdS)Nrr@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&   zIMAP4.__init__cCs0d|_d|_t ttj¡|_t ttj¡|_dS)NFÚascii) Ú utf8_enabledÚ _encodingÚreÚcompileÚ_LiteralÚASCIIÚLiteralÚ_Untagged_statusÚUntagged_status)rQr;r;r<rKÑszIMAP4._mode_asciicCs(d|_d|_t t¡|_t t¡|_dS)NTzutf-8)rVrWrXrYrZr\r]r^)rQr;r;r<Ú _mode_utf8Øs zIMAP4._mode_utf8cCsžtt dd¡ƒ|_t d|jdtj¡|_| ¡|_ d|j krHd|_ nd|j krZd|_ n |  |j ¡‚|  ¡x tD]}||jkr„qt||_dSW|  d ¡‚dS) Niiÿÿs(?Ps"\d+) (?P[A-Z]+) (?P.*)ZPREAUTHr ÚOKrzserver not IMAP4 compliant)r ÚrandomZrandintÚtagprerXrYr[ÚtagreÚ _get_responseZwelcomerFrCr=Ú_get_capabilitiesÚAllowedVersionsÚ capabilitiesÚPROTOCOL_VERSION)rQÚversionr;r;r<rMßs       zIMAP4._connectcCs&|tkrt|| ¡ƒStd|ƒ‚dS)NzUnknown IMAP4 command: '%s')ÚCommandsÚgetattrÚlowerÚAttributeError)rQÚattrr;r;r<Ú __getattr__ szIMAP4.__getattr__cCs|S)Nr;)rQr;r;r<Ú __enter__szIMAP4.__enter__cGs&y | ¡Wntk r YnXdS)N)ÚlogoutrP)rQÚargsr;r;r<Ú__exit__s zIMAP4.__exit__cCs |js dn|j}t ||jf¡S)N)rRÚsocketZcreate_connectionrS)rQrRr;r;r<Ú_create_socketszIMAP4._create_socketcCs(||_||_| ¡|_|j d¡|_dS)zÑSetup connection to remote server on "host:port" (default: localhost:standard IMAP4 port). This connection will be used by the routines: read, readline, send, shutdown. ÚrbN)rRrSruÚsockÚmakefileÚfile)rQrRrSr;r;r<rL#s z IMAP4.opencCs |j |¡S)zRead 'size' bytes from remote.)ryÚread)rQÚsizer;r;r<rz/sz IMAP4.readcCs.|j td¡}t|ƒtkr*| dt¡‚|S)zRead line from remote.ézgot more than %d bytes)ryÚreadlineÚ_MAXLINEÚlenr=)rQÚliner;r;r<r}4s zIMAP4.readlinecCs|j |¡dS)zSend data to remote.N)rwZsendall)rQÚdatar;r;r<Úsend<sz IMAP4.sendc Cst|j ¡zXy|j tj¡Wn@tk r^}z"|jtjkrNt |ddƒdkrN‚Wdd}~XYnXWd|j ¡XdS)z Close I/O established in "open".Zwinerrorri&'N) ryÚcloserwrOrtZ SHUT_RDWRrPÚerrnoZENOTCONNrk)rQÚexcr;r;r<rOAs  zIMAP4.shutdowncCs|jS)zfReturn socket instance used to connect to IMAP4 server. socket = .socket() )rw)rQr;r;r<rtQsz IMAP4.socketcCsBd}| ddg|¡\}}|dr(||fS| ¡\}}| |||¡S)aReturn most recent 'RECENT' responses if any exist, else prompt server for an update using the 'NOOP' command. (typ, [data]) = .recent() 'data' is None if no new messages, else list of RECENT responses, most recent last. ZRECENTr`Néÿÿÿÿ)Ú_untagged_responseÚnoop)rQÚnameÚtypÚdatr;r;r<Úrecent]s  z IMAP4.recentcCs| |dg| ¡¡S)z¥Return data for response 'code' if received, or None. Old value for response 'code' is cleared. (code, [data]) = .response(code) N)r‡Úupper)rQÚcoder;r;r<ÚresponsenszIMAP4.responsecCsxd}|s d}|r.|d|dfdkr2d|}nd}|r@t|ƒ}nd}t t|¡}|jrbd|d }||_| ||||¡S) z°Append message to named mailbox. (typ, [data]) = .append(mailbox, flags, date_time, message) All args except `message' can be None. rÚINBOXrr†)ú(ú)z(%s)NsUTF8 (ó))r ÚMapCRLFÚsubÚCRLFrVrDÚ_simple_command)rQÚmailboxÚflagsÚ date_timeÚmessager‰rDr;r;r<Úappend|s    z IMAP4.appendcCsP| ¡}t|ƒj|_| d|¡\}}|dkrB| |d dd¡¡‚d|_||fS)asAuthenticate command - requires response processing. 'mechanism' specifies which authentication mechanism is to be used - it must appear in .capabilities in the form AUTH=. 'authobject' must be a callable object: data = authobject(response) It will be called to process server continuation responses; the response argument it is passed will be a bytes. It should return bytes data that will be base64 encoded and sent to the server. It should return None if the client abort response '*' should be sent instead. rr`r†zutf-8Úreplacer )rÚ_AuthenticatorÚprocessrDr—r=ÚdecoderC)rQZ mechanismZ authobjectÚmechrŠr‹r;r;r<Ú authenticate–s zIMAP4.authenticatecCs d}| |¡\}}| |||¡S)zT(typ, [data]) = .capability() Fetch capabilities list from server.r)r—r‡)rQr‰rŠr‹r;r;r<Ú capability³szIMAP4.capabilitycCs | d¡S)zRCheckpoint mailbox on server. (typ, [data]) = .check() r)r—)rQr;r;r<Úcheck¼sz IMAP4.checkcCs$z| d¡\}}Wdd|_X||fS)zÌClose currently selected mailbox. Deleted messages are removed from writable mailbox. This is the recommended command before 'LOGOUT'. (typ, [data]) = .close() rNr )r—rC)rQrŠr‹r;r;r<rƒÄsz IMAP4.closecCs| d||¡S)z‚Copy 'message_set' messages onto end of 'new_mailbox'. (typ, [data]) = .copy(message_set, new_mailbox) r)r—)rQÚ message_setZ new_mailboxr;r;r<ÚcopyÓsz IMAP4.copycCs | d|¡S)zPCreate new mailbox. (typ, [data]) = .create(mailbox) r)r—)rQr˜r;r;r<ÚcreateÛsz IMAP4.createcCs | d|¡S)zPDelete old mailbox. (typ, [data]) = .delete(mailbox) r)r—)rQr˜r;r;r<Údeleteãsz IMAP4.deletecCs| d||¡S)z€Delete the ACLs (remove any rights) set for who on mailbox. (typ, [data]) = .deleteacl(mailbox, who) r)r—)rQr˜Úwhor;r;r<Ú deleteaclêszIMAP4.deleteaclcCsHd|jkrt d¡‚| d|¡\}}|dkr@d| ¡kr@| ¡||fS)zkSend an RFC5161 enable string to the server. (typ, [data]) = .enable(capability) rzServer does not support ENABLEr`z UTF8=ACCEPT)rgrr=r—rr_)rQr£rŠrr;r;r<Úenableñs   z IMAP4.enablecCs d}| |¡\}}| |||¡S)z÷Permanently remove deleted items from selected mailbox. Generates 'EXPUNGE' response for each deleted message. (typ, [data]) = .expunge() 'data' is list of 'EXPUNGE'd message numbers in order received. r)r—r‡)rQr‰rŠr‹r;r;r<Úexpungeýs z IMAP4.expungecCs$d}| |||¡\}}| |||¡S)a#Fetch (parts of) messages. (typ, [data, ...]) = .fetch(message_set, message_parts) 'message_parts' should be a string of selected parts enclosed in parentheses, eg: "(UID BODY[TEXT])". 'data' are tuples of message part envelope and data. r)r—r‡)rQr¥Z message_partsr‰rŠr‹r;r;r<Úfetch s z IMAP4.fetchcCs| d|¡\}}| ||d¡S)zXGet the ACLs for a mailbox. (typ, [data]) = .getacl(mailbox) rZACL)r—r‡)rQr˜rŠr‹r;r;r<Úgetaclsz IMAP4.getaclcCs"| d|||¡\}}| ||d¡S)za(typ, [data]) = .getannotation(mailbox, entry, attribute) Retrieve ANNOTATIONs.rÚ ANNOTATION)r—r‡)rQr˜ÚentryZ attributerŠr‹r;r;r<Ú getannotation#szIMAP4.getannotationcCs| d|¡\}}| ||d¡S)zªGet the quota root's resource usage and limits. Part of the IMAP4 QUOTA extension defined in rfc2087. (typ, [data]) = .getquota(root) r ÚQUOTA)r—r‡)rQÚrootrŠr‹r;r;r<Úgetquota+szIMAP4.getquotacCs@| d|¡\}}| ||d¡\}}| ||d¡\}}|||gfS)zœGet the list of quota roots for the named mailbox. (typ, [[QUOTAROOT responses...], [QUOTA responses]]) = .getquotaroot(mailbox) r!r²Z QUOTAROOT)r—r‡)rQr˜rŠr‹ZquotaZ quotarootr;r;r<Ú getquotaroot6szIMAP4.getquotarootú""Ú*cCs$d}| |||¡\}}| |||¡S)z«List mailbox names in directory matching pattern. (typ, [data]) = .list(directory='""', pattern='*') 'data' is list of LIST responses. r#)r—r‡)rQÚ directoryÚpatternr‰rŠr‹r;r;r<ÚlistAsz IMAP4.listcCs<| d|| |¡¡\}}|dkr.| |d¡‚d|_||fS)z”Identify client using plaintext password. (typ, [data]) = .login(user, password) NB: 'password' will be quoted. r$r`r†r )r—Ú_quoter=rC)rQÚuserÚpasswordrŠr‹r;r;r<ÚloginMs z IMAP4.logincCs|||_|_| d|j¡S)zr Force use of CRAM-MD5 authentication. (typ, [data]) = .login_cram_md5(user, password) zCRAM-MD5)r¼r½r¢Ú_CRAM_MD5_AUTH)rQr¼r½r;r;r<Úlogin_cram_md5[szIMAP4.login_cram_md5cCsBddl}t|jtƒr |j d¡n|j}|jd| ||d¡ ¡S)z1 Authobject to use with CRAM-MD5 authentication. rNzutf-8ú Zmd5)ÚhmacÚ isinstancer½ÚstrÚencoder¼ZHMACZ hexdigest)rQZ challengerÂÚpwdr;r;r<r¿dszIMAP4._CRAM_MD5_AUTHcCsjd|_y| d¡\}}Wn(ddt ¡dd…g}}YnX| ¡d|jkrbd|jdfS||fS)z|Shutdown connection to server. (typ, [data]) = .logout() Returns server 'BYE' response. rÚNOz%s: %sNéÚBYE)rCr—ÚsysÚexc_inforOrF)rQrŠr‹r;r;r<rqls" z IMAP4.logoutcCs$d}| |||¡\}}| |||¡S)zÐList 'subscribed' mailbox names in directory matching pattern. (typ, [data, ...]) = .lsub(directory='""', pattern='*') 'data' are tuples of message part envelope and data. r%)r—r‡)rQr¸r¹r‰rŠr‹r;r;r<Úlsub|sz IMAP4.lsubcCs| d|¡\}}| ||d¡S)zƒShow my ACLs for a mailbox (i.e. the rights that I have on mailbox). (typ, [data]) = .myrights(mailbox) r")r—r‡)rQr˜rŠr‹r;r;r<Úmyrights‡szIMAP4.myrightscCs d}| |¡\}}| |||¡S)zb Returns IMAP namespaces ala rfc2342 (typ, [data, ...]) = .namespace() r&)r—r‡)rQr‰rŠr‹r;r;r<Ú namespaceszIMAP4.namespacecCs | d¡S)zFSend NOOP command. (typ, [data]) = .noop() r')r—)rQr;r;r<rˆ™sz IMAP4.noopcCs(d}| |||||¡\}}| ||d¡S)zÂFetch truncated part of a message. (typ, [data, ...]) = .partial(message_num, message_part, start, length) 'data' is tuple of message part envelope and data. r(r)r—r‡)rQZ message_numZ message_partÚstartÚlengthr‰rŠr‹r;r;r<Úpartial¤sz IMAP4.partialcCsd}| d|¡S)z³Assume authentication as "user". Allows an authorised administrator to proxy into any user's mailbox. (typ, [data]) = .proxyauth(user) r))r—)rQr¼r‰r;r;r<Ú proxyauth°s zIMAP4.proxyauthcCs| d||¡S)zkRename old mailbox name to new. (typ, [data]) = .rename(oldmailbox, newmailbox) r*)r—)rQZ oldmailboxZ newmailboxr;r;r<Úrename½sz IMAP4.renamecGsTd}|r2|jrt d¡‚|j|d|f|žŽ\}}n|j|f|žŽ\}}| |||¡S)zéSearch mailbox for matching messages. (typ, [data]) = .search(charset, criterion, ...) 'data' is space separated list of matching message numbers. If UTF8 is enabled, charset MUST be None. r+z'Non-None charset not valid in UTF8 modeÚCHARSET)rVrr=r—r‡)rQÚcharsetZcriteriar‰rŠr‹r;r;r<ÚsearchÅs z IMAP4.searchrFcCsvi|_||_|rd}nd}| ||¡\}}|dkr@d|_||fSd|_d|jkrb|sb| d|¡‚||j dd g¡fS) atSelect a mailbox. Flush all untagged responses. (typ, [data]) = .select(mailbox='INBOX', readonly=False) 'data' is count of messages in mailbox ('EXISTS' response). Mandated responses are ('FLAGS', 'EXISTS', 'RECENT', 'UIDVALIDITY'), so other responses should be obtained via .response('FLAGS') etc. rr,r`r rz READ-ONLYz%s is not writableÚEXISTSN)rFrHr—rCr?Úget)rQr˜r?r‰rŠr‹r;r;r<Úselect×s  z IMAP4.selectcCs| d|||¡S)zZSet a mailbox acl. (typ, [data]) = .setacl(mailbox, who, what) r-)r—)rQr˜r©Zwhatr;r;r<Úsetacl÷sz IMAP4.setaclcGs |jd|žŽ\}}| ||d¡S)z_(typ, [data]) = .setannotation(mailbox[, entry, attribute]+) Set ANNOTATIONs.r.r¯)r.)r—r‡)rQrrrŠr‹r;r;r<Ú setannotationÿszIMAP4.setannotationcCs | d||¡\}}| ||d¡S)ziSet the quota root's resource limits. (typ, [data]) = .setquota(root, limits) r/r²)r—r‡)rQr³ZlimitsrŠr‹r;r;r<ÚsetquotaszIMAP4.setquotacGsFd}|d|dfdkr d|}|j|||f|žŽ\}}| |||¡S)zIMAP4rev1 extension SORT command. (typ, [data]) = .sort(sort_criteria, charset, search_criteria, ...) r0rr†)r‘r’z(%s))r—r‡)rQZ sort_criteriarÕÚsearch_criteriar‰rŠr‹r;r;r<Úsorts z IMAP4.sortNcCs¦d}ts| d¡‚|jr"| d¡‚||jkr6| d¡‚|dkrFt ¡}| |¡\}}|dkrŽ|j|j |j d|_ |j   d¡|_ d|_|  ¡n | d ¡‚| |||¡S) Nr1zSSL support missingzTLS session already establishedzTLS not supported by serverr`)Úserver_hostnamervTzCouldn't establish TLS session)ÚHAVE_SSLr=rJr>rgÚsslÚ_create_stdlib_contextr—Ú wrap_socketrwrRrxryrer‡)rQÚ ssl_contextr‰rŠr‹r;r;r<Ústarttlss$       zIMAP4.starttlscCs$d}| |||¡\}}| |||¡S)zpRequest named status conditions for mailbox. (typ, [data]) = .status(mailbox, names) r2)r—r‡)rQr˜Únamesr‰rŠr‹r;r;r<Ústatus5sz IMAP4.statuscCs>|d|dfdkrd|}| d|||¡\}}| ||d¡S)zAlters flag dispositions for messages in mailbox. (typ, [data]) = .store(message_set, command, flags) rr†)r‘r’z(%s)r3r)r—r‡)rQr¥Úcommandr™rŠr‹r;r;r<ÚstoreAsz IMAP4.storecCs | d|¡S)zYSubscribe to new mailbox. (typ, [data]) = .subscribe(mailbox) r4)r—)rQr˜r;r;r<Ú subscribeLszIMAP4.subscribecGs*d}|j|||f|žŽ\}}| |||¡S)z‹IMAPrev1 extension THREAD command. (type, [data]) = .thread(threading_algorithm, charset, search_criteria, ...) r5)r—r‡)rQZthreading_algorithmrÕrÝr‰rŠr‹r;r;r<ÚthreadTsz IMAP4.threadc Gs†| ¡}|tkr| d|¡‚|jt|krL| d||jd t|¡f¡‚d}|j||f|žŽ\}}|dkrt|}nd}| |||¡S)zåExecute "command arg ..." with messages identified by UID, rather than message number. (typ, [data]) = .uid(command, arg1, arg2, ...) Returns response appropriate to 'command'. zUnknown IMAP4 UID command: %sz9command %s illegal in state %s, only allowed in states %sz, r6)r+r0r5r)rrjr=rCÚjoinr—r‡)rQrèrrr‰rŠr‹r;r;r<Úuid^sz IMAP4.uidcCs | d|¡S)z_Unsubscribe from old mailbox. (typ, [data]) = .unsubscribe(mailbox) r7)r—)rQr˜r;r;r<Ú unsubscribewszIMAP4.unsubscribecGs,| ¡}|tkr|jft|<|j|f|žŽS)aAllow simple extension commands notified by server in CAPABILITY response. Assumes command is legal in current state. (typ, [data]) = .xatom(name, arg, ...) Returns response appropriate to extension command `name'. )rrjrCr—)rQr‰rrr;r;r<Úxatoms  z IMAP4.xatomcCs8|dkr d}|j}||kr*|| |¡n |g||<dS)Nó)rFrœ)rQrŠr‹Zurr;r;r<Ú_append_untagged•s zIMAP4._append_untaggedcCs,|j d¡}|r(| |d |jd¡¡‚dS)NrÉr†r)rFrØr>r rW)rQZbyer;r;r<Ú _check_bye£s zIMAP4._check_byec Gsø|jt|kr4d|_| d||jd t|¡f¡‚xdD]}||jkr:|j|=q:Wd|jkrn|jsn| d¡‚| ¡}t ||j ƒ}|d|}x8|D]0}|dkr¢q”t |t ƒr¸t ||j ƒ}|d|}q”W|j}|dk rd|_t |ƒt |jƒkrö|}nd}|t dt|ƒ|j ƒ}y| |t¡Wn2tk rV} z| d| ¡‚Wdd} ~ XYnX|dkrf|SxŒx | ¡rˆ|j|rj|SqjW|rš||jƒ}y| |¡| t¡Wn2tk rä} z| d| ¡‚Wdd} ~ XYnX|shPqhW|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}zsocket error: %s)rCrjrDr=rìrFrHr?Ú_new_tagÚbytesrWrÃrÄÚtypeÚ_commandrr‚r–rPr>rdrErG) rQr‰rrrŠÚtagrÚargrDZ literatorÚvalr;r;r<rø©s^                   zIMAP4._commandc CsÂ|dkr| ¡y| |¡\}}Wnj|jk rX}z| d||f¡‚Wdd}~XYn6|jk rŒ}z| d||f¡‚Wdd}~XYnX|dkrž| ¡|dkrº| d|||f¡‚||fS)Nrzcommand: %s => %sróz%s command error: %s %s)ròÚ_get_tagged_responser>r=)rQr‰rùrŠrrûr;r;r<Ú_command_completeös"$zIMAP4._command_completecCsJ| ¡\}}|dgkr | d¡‚t|d|jƒ}| ¡}t| ¡ƒ|_dS)Nz"no CAPABILITY response from serverr†)r£r=rÄrWrÚtupleÚsplitrg)rQrŠr‹r;r;r<res    zIMAP4._get_capabilitiescCs²| ¡}| |j|¡rp|j d¡}||jkr:| d|¡‚|j d¡}t||jƒ}|j d¡}||gf|j|<núd}| t |¡sš| |j |¡rš|j d¡}|jdkrÐ| t |¡rÂ|j d¡|_ dS| d|¡‚|j d¡}t||jƒ}|j d¡}|dkrd}|r|d|}xH| |j |¡r\t|j d ¡ƒ}| |¡}| |||f¡| ¡}qW| ||¡|d kr®| t|¡r®|j d¡}t||jƒ}| ||j d¡¡|S) Nrùzunexpected tagged response: %rr÷rZdata2zunexpected response: %rrðrôr{)r`rÇró)Ú _get_lineÚ_matchrcÚmoÚgrouprEr>rÄrWÚUntagged_responser^Ú ContinuationrGr\ÚintrzrñÚ Response_code)rQÚresprùrŠr‹Zdat2r{rr;r;r<rdsH                  zIMAP4._get_responsec Csdx^|j|}|dk r |j|=|S| ¡y | ¡Wq|jk rZ}z‚Wdd}~XYqXqWdS)N)rEròrdr>)rQrùÚresultrûr;r;r<rü`s  zIMAP4._get_tagged_responsecCs>| ¡}|s| d¡‚| d¡s.| d|¡‚|dd…}|S)Nzsocket error: EOFs z#socket error: unterminated line: %réþÿÿÿ)r}r>Úendswith)rQr€r;r;r<r{s   zIMAP4._get_linecCs| |¡|_|jdk S)N)Úmatchr)rQZcreÚsr;r;r<rŽs z IMAP4._matchcCs2|jtt|jƒ|jƒ}|jd|_d|j|<|S)Nr|)rbrörÄrIrWrE)rQrùr;r;r<rõšs  zIMAP4._new_tagcCs$| dd¡}| dd¡}d|dS)Nú\z\\ú"z\")r)rQrúr;r;r<r»¢s  z IMAP4._quotecGs| ||j|f|žŽ¡S)N)rýrø)rQr‰rrr;r;r<r—ªszIMAP4._simple_commandcCs8|dkr||fS||jkr$|dgfS|j |¡}||fS)NrÇ)rFÚpop)rQrŠr‹r‰rr;r;r<r‡¯s    zIMAP4._untagged_response)r¶r·)r¶r·)rF)N)Pr8r9r:Ú__doc__rNr=r>r?Ú IMAP4_PORTrTrKr_rMrorprsrurLrzr}r‚rOrtrŒrrœr¢r£r¤rƒr¦r§r¨rªr«r¬r­r®r±r´rµrºr¾rÀr¿rqrÌrÍrÎrˆrÑrÒrÓrÖrÙrÚrÛrÜrÞrårçrérêrërírîrïrñròrørýrerdrürrrõr»r—r‡r;r;r;r<r†s˜,*                    M P  c@s:eZdZdZdedddfdd„Zdd„Zdefdd „ZdS) Ú IMAP4_SSLa3IMAP4 client class over SSL connection Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile[, ssl_context]]]]]) host - host's name (default: localhost); port - port number (default: standard IMAP4 SSL port); keyfile - PEM formatted file that contains your private key (default: None); certfile - PEM formatted certificate chain file (default: None); ssl_context - a SSLContext object that contains your certificate chain and private key (default: None) Note: if ssl_context is provided, then parameters keyfile or certfile should not be set otherwise ValueError is raised. for more documentation see the docstring of the parent class IMAP4. r@NcCs|dk r|dk rtdƒ‚|dk r0|dk r0tdƒ‚|dk s@|dk rVddl}| dtd¡||_||_|dkrxtj||d}||_t   |||¡dS)Nz8ssl_context and keyfile arguments are mutually exclusivez9ssl_context and certfile arguments are mutually exclusiverzEkeyfile and certfile are deprecated, use a custom ssl_context insteadrÈ)ÚcertfileÚkeyfile) Ú ValueErrorÚwarningsÚwarnÚDeprecationWarningrrrárârärrT)rQrRrSrrrärr;r;r<rTöszIMAP4_SSL.__init__cCst |¡}|jj||jdS)N)rß)rrurärãrR)rQrwr;r;r<ru s zIMAP4_SSL._create_socketcCst |||¡dS)zæSetup connection to remote server on "host:port". (default: localhost:standard IMAP4 SSL port). This connection will be used by the routines: read, readline, send, shutdown. N)rrL)rQrRrSr;r;r<rLszIMAP4_SSL.open)r8r9r:rÚIMAP4_SSL_PORTrTrurLr;r;r;r<rãs rc@sBeZdZdZdd„Zddd„Zdd„Zd d „Zd d „Zd d„Z dS)rzæIMAP4 client class over a stream Instantiate with: IMAP4_stream(command) "command" - a string that can be passed to subprocess.Popen() for more documentation see the docstring of the parent class IMAP4. cCs||_t |¡dS)N)rèrrT)rQrèr;r;r<rT&szIMAP4_stream.__init__NcCsNd|_d|_d|_d|_tj|jttjtjddd|_ |j j |_ |j j |_ dS)z…Setup a stream connection. This connection will be used by the routines: read, readline, send, shutdown. NT)ÚbufsizeÚstdinÚstdoutÚshellZ close_fds)rRrSrwryÚ subprocessÚPopenrèrÚPIPErŸrÚ writefilerÚreadfile)rQrRrSr;r;r<rL+s  zIMAP4_stream.opencCs |j |¡S)zRead 'size' bytes from remote.)r#rz)rQr{r;r;r<rz;szIMAP4_stream.readcCs |j ¡S)zRead line from remote.)r#r})rQr;r;r<r}@szIMAP4_stream.readlinecCs|j |¡|j ¡dS)zSend data to remote.N)r"ÚwriteÚflush)rQrr;r;r<r‚Es zIMAP4_stream.sendcCs"|j ¡|j ¡|j ¡dS)z Close I/O established in "open".N)r#rƒr"rŸÚwait)rQr;r;r<rOKs  zIMAP4_stream.shutdown)NN) r8r9r:rrTrLrzr}r‚rOr;r;r;r<rs  c@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) ržzcPrivate class to provide en/decoding for base64-based authentication conversation. cCs ||_dS)N)r¡)rQZmechinstr;r;r<rTYsz_Authenticator.__init__cCs&| | |¡¡}|dkrdS| |¡S)Nó*)r¡r rÅ)rQrZretr;r;r<rŸ\sz_Authenticator.processcCsrd}t|tƒr| d¡}xT|rlt|ƒdkrD|dd…}|dd…}n|}d}t |¡}|r||dd…}qW|S)Nrðzutf-8é0r†)rÃrÄrÅrÚbinasciiZ b2a_base64)rQÚinpZoupÚtÚer;r;r<rÅbs      z_Authenticator.encodecCs|sdSt |¡S)Nrð)r)Z a2b_base64)rQr*r;r;r<r zsz_Authenticator.decodeN)r8r9r:rrTrŸrÅr r;r;r;r<ržSs ržz0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecrÁcCsi|]\}}|d| ¡“qS)r|)rÅ)Ú.0Únr r;r;r<ú €sr/r|c CsØt |¡}|sdSt| d¡}| d¡}t| d¡ƒ}t| d¡ƒ}t| d¡ƒ}t| d¡ƒ}t| d¡ƒ}t| d ¡ƒ} t| d ¡ƒ} | d | d } |d krª| } ||||||d d d f } t | ¡| } t | ¡S)z¨Parse an IMAP4 INTERNALDATE string. Return corresponding local time. The return value is a time.struct_time tuple or None if the string has wrong format. NÚmonÚzonenÚdayÚyearÚhourÚminÚsecÚzonehÚzonemé<ó-r†) Ú InternalDater ÚMon2numrrÚcalendarZtimegmÚtimeÚ localtime)rrr0r1r2r3r4r5r6r7r8ZzoneZttÚutcr;r;r<r‚s$  cCsDd}d}tt|ƒƒ}x*|r>t|dƒ\}}|||d…|}qW|S)z-Convert integer to A-P string representation.rðsABCDEFGHIJKLMNOPér|)rÚabsÚdivmod)ZnumrûZAPÚmodr;r;r<r ¥s cCs$t |¡}|sdSt| d¡ ¡ƒS)z-Convert IMAP4 flags response to python tuple.r;r™)ÚFlagsr rþrrÿ)rrr;r;r<r ²s cCst|ttfƒr"t |tj¡ ¡}nât|tƒr¸y |j }WnZt k rt j r„|d}|dkrpt   t  |¡¡d}t jt jf| }nt j }YnXt|d}t|dd…dt|ƒiŽ}nLt|tƒrÚ|jdkrÔtdƒ‚|}n*t|tƒrü|d|dfd krü|Std ƒ‚d  t|j¡}| |¡S) aèConvert date_time to IMAP4 INTERNALDATE representation. Return string in form: '"DD-Mmm-YYYY HH:MM:SS +HHMM"'. The date_time argument can be a number (int or float) representing seconds since epoch (as returned by time.time()), a 9-tuple representing local time, an instance of time.struct_time (as returned by time.localtime()), an aware datetime instance or a double-quoted string. In the last case, it is assumed to already be in the correct format. ér†)ZsecondsNéÚtzinfozdate_time must be awarer)rrzdate_time not of a known typez"%d-{}-%Y %H:%M:%S %z")rÃrÚfloatrZ fromtimestamprr@Z astimezonerþZ tm_gmtoffrmr>Zdaylightr?ZmktimeZaltzonerrHrrÄÚformatÚMonthsZmonthZstrftime)ršZdtZgmtoffÚdstZdeltaZfmtr;r;r<r ½s0      Ú__main__zd:s:)r;r;z-dz-s)r@zIMAP password for %s on %s: Z localhostzJFrom: %(user)s@localhost%(lf)sSubject: IMAP4 test%(lf)s%(lf)sdata...%(lf)sÚ )r¼Zlfr¾)r§)z /tmp/xxx 1)rÓ)z /tmp/xxx 1z/tmp/yyy)r)z /tmp/yyz 2rœz /tmp/yyz 2)rº)z/tmpzyy*)rÙ)z /tmp/yyz 2)rÖ)NZSUBJECTZtest)r­)Ú1z(FLAGS INTERNALDATE RFC822))ré)rOÚFLAGSz (\Deleted))rÎr;)r¬r;)rŒr;)rƒr;)rÙr;)r)Z UIDVALIDITY)rí)r+ZALL)r)r×)rqr;cCsLt d||f¡tt|ƒ|Ž\}}t d|||f¡|dkrH|d‚|S)Nz%s %sz %s => %s %srÇr)ÚMÚ_mesgrk)ÚcmdrrrŠr‹r;r;r<Úruns rTr zPROTOCOL_VERSION = %szCAPABILITIES = %rrº)z/tmp/zyy%z .*"([^"]+)"$r†r¨rírz%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 )UrÚ __version__r)r„rarXrtrrÊr>r=rrrÚiorráràÚ ImportErrorÚ__all__r–rArrrfr~rjrYrrEr;r[r\r”rrr^rZr]rrrœrržrÿrKÚ enumerater<rr r r r8ZgetoptZgetpassÚargvZoptlistrrr=rûZstream_commandZoptrrRZgetuserZUSERZPASSWDZ test_mesgZ test_seq1Z test_seq2rTrQrCrRrhrgrSZmlr rrÚpathr‹ríÚprintr;r;r;r<Ú sF H          c4 9, #  )