a ´iØ×ã+@sœdZdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z m Z m Z ddlmZzddlZdZWney–dZYn0gd¢Zd ZdZd Zd Zd Zd ZdZdddddddddddddddddddddddddddddddddddddddddddœ*Ze d¡Ze d¡Ze d¡Ze dej ¡Z!e d¡Z"e d¡Z#e d¡Z$e dej ¡Z%dZ&dZ'Gdd„dƒZ(er¤Gdd „d e(ƒZ)e *d ¡Gd!d"„d"e(ƒZ+Gd#d$„d$ƒZ,d% -d&¡Z.d'd(„e/e.d)d…ƒDƒZ0d*d+„Z1d,d-„Z2d.d/„Z3d0d1„Z4e5d2kr˜ddl6Z6ddl7Z7ze6 6ej8d)d…d3¡\Z9Z:Wn0e6j;ynZ<zd4\Z9Z:WYdZ<[Zd5kr”e?e<ƒZne>d6krxed?d@dAdBddeDffdCdDdEdFdGdHdIdJdKfZEdLdMdNdOdAdddeDffdJdPfZFdQdR„ZGze=r`e+e=ƒZHne(e@ƒZHeHjIdSkr€eEd)d…ZEeH JdTeHjK¡eH JdUeHjLf¡eED]\ZMZ:eGeMe:ƒq¦eGdVdWƒ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ƒZded fd d „Z d d „Z dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zded fdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Z d:d;„Z!dd?„Z#d@dA„Z$dBdC„Z%dDdE„Z&dFdG„Z'dHdI„Z(dždLdM„Z)dNdO„Z*dPdQ„Z+dRdS„Z,dTdU„Z-dŸdVdW„Z.dXdY„Z/dZd[„Z0d\d]„Z1d^d_„Z2d`da„Z3dbdc„Z4ddde„Z5d dhdi„Z6djdk„Z7dldm„Z8dndo„Z9dpdq„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„ZId¢dd‘„ZJd’d“„ZKd”d•„ZLd–d—„ZMd˜d™„ZNdšd›„ZOdœd„ZPd S)£raªIMAP4 client class. Instantiate with: IMAP4([host[, port[, timeout=None]]]) host - host's name (default: localhost); port - port number (default: standard IMAP4 port). timeout - socket timeout (default: None) If timeout is not given or is None, the global default socket timeout is used 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>ú/usr/lib64/python3.9/imaplib.pyÚerror»ór@c@s eZdZdS)z IMAP4.abortNr:r>r>r>r?Úabort¼rArBc@s eZdZdS)zIMAP4.readonlyNr:r>r>r>r?Úreadonly½rArCÚNc Cs’t|_d|_d|_i|_i|_d|_d|_d|_d|_ |  ¡|  |||¡z |  ¡Wn4t yŒz | ¡Wnty„Yn0‚Yn0dS)NrrDFr)Ú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&    zIMAP4.__init__cCs0d|_d|_t ttj¡|_t ttj¡|_dS)NFÚascii) Ú utf8_enabledÚ _encodingÚreÚcompileÚ_LiteralÚASCIIÚLiteralÚ_Untagged_statusÚUntagged_status©rVr>r>r?rOØszIMAP4._mode_asciicCs(d|_d|_t t¡|_t t¡|_dS)NTúutf-8)r\r]r^r_r`rbrcrdrer>r>r?Ú _mode_utf8ßs zIMAP4._mode_utf8cCsœtt dd¡ƒ|_t d|jdtj¡|_|  ¡|_ d|j vrHd|_ nd|j vrZd|_ n |  |j ¡‚| ¡tD]}||jvr‚qr||_dS|  d ¡‚dS) Niiÿÿs(?Ps"\d+) (?P[A-Z]+) (?P.*)ZPREAUTHrÚOKrzserver not IMAP4 compliant)r ÚrandomÚrandintÚtagprer^r_raÚtagreÚ __version__Ú _get_responseZwelcomerJrGr@Ú_get_capabilitiesÚAllowedVersionsÚ capabilitiesÚPROTOCOL_VERSION)rVÚversionr>r>r?rQæs*ÿþþ     zIMAP4._connectcCs&|tvrt|| ¡ƒStd|ƒ‚dS)NzUnknown IMAP4 command: '%s')ÚCommandsÚgetattrÚlowerÚAttributeError)rVÚattrr>r>r?Ú __getattr__szIMAP4.__getattr__cCs|S©Nr>rer>r>r?Ú __enter__szIMAP4.__enter__cGs2|jdkrdSz | ¡Wnty,Yn0dS)Nr)rGÚlogoutrT)rVÚargsr>r>r?Ú__exit__s    zIMAP4.__exit__cCs`|dur|stdƒ‚|jsdn|j}t d||j|j¡||jf}|durVt ||¡St |¡S)Nz0Non-blocking socket (timeout=0) is not supportedz imaplib.open)Ú ValueErrorrWÚsysÚauditrXÚsocketÚcreate_connection)rVrYrWÚaddressr>r>r?Ú_create_socket&s   zIMAP4._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)rWrXr…ÚsockÚmakefileÚfilerUr>r>r?rP3s z IMAP4.opencCsVt|tƒ}|j |¡}||krRt|ƒ|krRt|||ƒ}||j |¡7}||7}q|S©zRead 'size' bytes from remote.)ÚminÚ_SAFE_BUF_SIZEr‰ÚreadÚlen)rVÚsizeZcursizeÚdataÚdeltar>r>r?r?s   z IMAP4.readcCs.|j td¡}t|ƒtkr*| dt¡‚|S)úRead line from remote.ézgot more than %d bytes)r‰ÚreadlineÚ_MAXLINErŽr@©rVÚliner>r>r?r”Js zIMAP4.readlinecCst d||¡|j |¡dS)úSend data to remote.z imaplib.sendN)r€rr‡Úsendall©rVrr>r>r?ÚsendRsz IMAP4.sendc Cs€|j ¡zdz|j tj¡WnBty`}z*|jtjkrLt |ddƒdkrL‚WYd}~n d}~00W|j ¡n |j ¡0dS)ú Close I/O established in "open".Úwinerrorri&'N) r‰Úcloser‡rSr‚Ú SHUT_RDWRrTÚerrnoZENOTCONNru)rVÚexcr>r>r?rSXs  ÿzIMAP4.shutdowncCs|jS)zfReturn socket instance used to connect to IMAP4 server. socket = .socket() )r‡rer>r>r?r‚hsz 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. ZRECENTrhNéÿÿÿÿ)Ú_untagged_responseÚnoop©rVÚnameÚtypÚdatr>r>r?Úrecentts  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)rVÚcoder>r>r?Úresponse…szIMAP4.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¢©ú(ú)ú(%s)NsUTF8 (ó))r ÚMapCRLFÚsubÚCRLFr\rHÚ_simple_command)rVÚmailboxÚflagsÚ date_timeÚmessager¦rHr>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. rrhr¢rfÚreplacer)rªÚ_AuthenticatorÚprocessrHr¶r@ÚdecoderG)rVZ 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£r¥r>r>r?Ú capabilityÊszIMAP4.capabilitycCs | d¡S)zRCheckpoint mailbox on server. (typ, [data]) = .check() r©r¶rer>r>r?ÚcheckÓsz IMAP4.checkcCs*z| d¡\}}Wd|_nd|_0||fS)zÌClose currently selected mailbox. Deleted messages are removed from writable mailbox. This is the recommended command before 'LOGOUT'. (typ, [data]) = .close() rr©r¶rG©rVr§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) rrÄ)rVÚ message_setZ new_mailboxr>r>r?Úcopyêsz IMAP4.copycCs | d|¡S)zPCreate new mailbox. (typ, [data]) = .create(mailbox) rrÄ©rVr·r>r>r?Úcreateòsz IMAP4.createcCs | d|¡S)zPDelete old mailbox. (typ, [data]) = .delete(mailbox) rrÄrÊ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) rrÄ)rVr·Úwhor>r>r?Ú deleteaclszIMAP4.deleteaclcCsHd|jvrt d¡‚| d|¡\}}|dkr@d| ¡vr@| ¡||fS)zlSend an RFC5161 enable string to the server. (typ, [data]) = .enable(capability) rzServer does not support ENABLErhz UTF8=ACCEPT)rqrr@r¶rªrg)rVrÃr§rr>r>r?Úenables   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. rrÂr¥r>r>r?Úexpunges 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. rrÂ)rVrÈ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) rZACLr©rVr·r§r¨r>r>r?Úgetacl1sz IMAP4.getaclcCs"| d|||¡\}}| ||d¡S)za(typ, [data]) = .getannotation(mailbox, entry, attribute) Retrieve ANNOTATIONs.r Ú ANNOTATIONrÂ)rVr·Ú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!ÚQUOTArÂ)rVÚrootr§r¨r>r>r?ÚgetquotaBszIMAP4.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 QUOTAROOTrÂ)rVr·r§r¨ZquotaZ quotarootr>r>r?Ú getquotarootMszIMAP4.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©rVZ directoryÚpatternr¦r§r¨r>r>r?ÚlistXsz IMAP4.listcCs<| d|| |¡¡\}}|dkr.| |d¡‚d|_||fS)z”Identify client using plaintext password. (typ, [data]) = .login(user, password) NB: 'password' will be quoted. r%rhr¢r)r¶Ú_quoter@rG)rVÚuserÚpasswordr§r¨r>r>r?Úloginds 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)rVrárâr>r>r?Úlogin_cram_md5rszIMAP4.login_cram_md5cCsBddl}t|jtƒr |j d¡n|j}|jd| ||d¡ ¡S)z1 Authobject to use with CRAM-MD5 authentication. rNrfú Úmd5)ÚhmacÚ isinstancerâÚstrÚencoderáÚHMACÚ hexdigest)rVZ challengerèÚpwdr>r>r?rä{s ÿzIMAP4._CRAM_MD5_AUTHcCs$d|_| d¡\}}| ¡||fS)z|Shutdown connection to server. (typ, [data]) = .logout() Returns server 'BYE' response. r)rGr¶rSrÇr>r>r?r|ƒsz 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Ýr>r>r?Úlsubsz 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Òr>r>r?Úmyrights›szIMAP4.myrightscCs d}| |¡\}}| |||¡S)zb Returns IMAP namespaces ala rfc2342 (typ, [data, ...]) = .namespace() r'rÂr¥r>r>r?Ú namespace£szIMAP4.namespacecCs | d¡S)zFSend NOOP command. (typ, [data]) = .noop() r(rÄrer>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)rrÂ)rVZ 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Ä)rVrár¦r>r>r?Ú proxyauthÄs zIMAP4.proxyauthcCs| d||¡S)zkRename old mailbox name to new. (typ, [data]) = .rename(oldmailbox, newmailbox) r+rÄ)rVZ oldmailboxZ newmailboxr>r>r?ÚrenameÑsz IMAP4.renamecGsXd}|r4|jrt d¡‚|j|d|g|¢RŽ\}}n|j|g|¢RŽ\}}| |||¡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)r\rr@r¶r£)rVÚcharsetZcriteriar¦r§r¨r>r>r?ÚsearchÙs z IMAP4.searchr­FcCsvi|_||_|rd}nd}| ||¡\}}|dkr@d|_||fSd|_d|jvrb|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-rhrrú READ-ONLYz%s is not writableÚEXISTSN)rJrLr¶rGrCÚget)rVr·rCr¦r§r¨r>r>r?Úselectës  ÿz IMAP4.selectcCs| d|||¡S)zZSet a mailbox acl. (typ, [data]) = .setacl(mailbox, who, what) r.rÄ)rVr·rÍZwhatr>r>r?Úsetacl sz IMAP4.setaclcGs$|jdg|¢RŽ\}}| ||d¡S)z_(typ, [data]) = .setannotation(mailbox[, entry, attribute]+) Set ANNOTATIONs.r/rÔrÂ)rVr}r§r¨r>r>r?Ú setannotationszIMAP4.setannotationcCs | d||¡\}}| ||d¡S)ziSet the quota root's resource limits. (typ, [data]) = .setquota(root, limits) r0r×rÂ)rVrØZlimitsr§r¨r>r>r?ÚsetquotaszIMAP4.setquotacGsHd}|d|dfdkr d|}|j|||g|¢RŽ\}}| |||¡S)zIMAP4rev1 extension SORT command. (typ, [data]) = .sort(sort_criteria, charset, search_criteria, ...) r1rr¢r®r±rÂ)rVZ sort_criteriarøÚsearch_criteriar¦r§r¨r>r>r?Úsort$s z IMAP4.sortcCs¦d}ts| d¡‚|jr"| d¡‚||jvr6| d¡‚|durFt ¡}| |¡\}}|dkrŽ|j|j |j d|_ |j   d¡|_ d|_|  ¡n | d ¡‚| |||¡S) Nr2zSSL support missingzTLS session already establishedzTLS not supported by serverrh©Zserver_hostnamer†TzCouldn't establish TLS session)ÚHAVE_SSLr@rNrBrqÚsslÚ_create_stdlib_contextr¶Ú wrap_socketr‡rWrˆr‰ror£)rVÚ ssl_contextr¦r§r¨r>r>r?Ústarttls2s&    ÿ  zIMAP4.starttlscCs$d}| |||¡\}}| |||¡S)zpRequest named status conditions for mailbox. (typ, [data]) = .status(mailbox, names) r3rÂ)rVr·Únamesr¦r§r¨r>r>r?ÚstatusIsz IMAP4.statuscCs>|d|dfdkrd|}| d|||¡\}}| ||d¡S)zAlters flag dispositions for messages in mailbox. (typ, [data]) = .store(message_set, command, flags) rr¢r®r±r4rrÂ)rVrÈÚcommandr¸r§r¨r>r>r?ÚstoreUsz IMAP4.storecCs | d|¡S)zYSubscribe to new mailbox. (typ, [data]) = .subscribe(mailbox) r5rÄrÊr>r>r?Ú subscribe`szIMAP4.subscribecGs,d}|j|||g|¢RŽ\}}| |||¡S)z‹IMAPrev1 extension THREAD command. (type, [data]) = .thread(threading_algorithm, charset, search_criteria, ...) r6rÂ)rVZthreading_algorithmrørr¦r§r¨r>r>r?Úthreadhsz IMAP4.threadc Gsˆ| ¡}|tvr| d|¡‚|jt|vrL| d||jd t|¡f¡‚d}|j||g|¢RŽ\}}|dvrv|}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: %sú9command %s illegal in state %s, only allowed in states %sú, r7)r,r1r6r)rªrtr@rGÚjoinr¶r£)rVr r}r¦r§r¨r>r>r?Úuidrs ÿþz IMAP4.uidcCs | d|¡S)z_Unsubscribe from old mailbox. (typ, [data]) = .unsubscribe(mailbox) r8rÄrÊr>r>r?Ú unsubscribe‹szIMAP4.unsubscribecCs*z| d¡\}}Wd|_nd|_0||fS)aMFree server's resources associated with the selected mailbox and returns the server to the authenticated state. This command performs the same actions as CLOSE, except that no messages are permanently removed from the currently selected mailbox. (typ, [data]) = .unselect() r9rrÆ)rVr§rr>r>r?Úunselect“s zIMAP4.unselectcGs.| ¡}|tvr|jft|<|j|g|¢RŽ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'. )rªrtrGr¶©rVr¦r}r>r>r?Úxatom£s  z IMAP4.xatomcCs8|dur d}|j}||vr*|| |¡n |g||<dS©NrA)rJrŽr»)rVr§r¨Úurr>r>r?Ú_append_untagged¹s zIMAP4._append_untaggedcCs,|j d¡}|r(| |d |jd¡¡‚dS)NÚBYEr¢r¼)rJrürBr¿r])rVÚbyer>r>r?Ú _check_byeÇs zIMAP4._check_byec Gsî|jt|vr4d|_| d||jd t|¡f¡‚dD]}||jvr8|j|=q8d|jvrj|jsj| d¡‚| ¡}t ||j ƒ}|d|}|D]0}|durœqŽt |t ƒr²t ||j ƒ}|d|}qŽ|j}|dur d|_t |ƒt |jƒurî|}nd}|t dt|ƒ|j ƒ}z| |t¡Wn4tyP} z| d| ¡‚WYd} ~ n d} ~ 00|dur`|S| ¡r~|j|r`|Sq`|rŽ||jƒ}z| |¡| t¡Wn4tyÚ} z| d| ¡‚WYd} ~ n d} ~ 00|s`qêq`|S) Nrr©rhÚNOÚBADrúz#mailbox status changed to READ-ONLYó z {%s}zsocket error: %s)rGrtrHr@rrJrLrCÚ_new_tagÚbytesr]rérêÚtypeÚ_commandrŽr›rµrTrBrnrIrK) rVr¦r}r§ÚtagrÚargrHZ literatorÚvalr>r>r?r%Ís` ÿþ   ÿ       $     $zIMAP4._commandc CsÊ|dk}|s| ¡z|j||d\}}Wnn|jyb}z| d||f¡‚WYd}~n@d}~0|jy˜}z| d||f¡‚WYd}~n d}~00|s¦| ¡|dkrÂ| d|||f¡‚||fS)Nr)Ú expect_byezcommand: %s => %sr z%s command error: %s %s)rÚ_get_tagged_responserBr@)rVr¦r&r|r§rr(r>r>r?Ú_command_completes&(zIMAP4._command_completecCsJ| ¡\}}|dgkr | d¡‚t|d|jƒ}| ¡}t| ¡ƒ|_dS)Nz"no CAPABILITY response from serverr¢)rÃr@rêr]rªÚtupleÚsplitrqrÇr>r>r?ro,s    zIMAP4._get_capabilitiescCs®| ¡}| |j|¡rp|j d¡}||jvr:| d|¡‚|j d¡}t||jƒ}|j d¡}||gf|j|<nöd}| t |¡sš| |j |¡rš|j d¡}|jdurÐ| t |¡rÂ|j d¡|_ dS| d|¡‚|j d¡}t||jƒ}|j d¡}|durd}|r|d|}| |j |¡rZt|j d ¡ƒ}| |¡}| |||f¡| ¡}q| ||¡|d vrª| t|¡rª|j d¡}t||jƒ}| ||j d¡¡|S) Nr&zunexpected tagged response: %rr$rZdata2zunexpected response: %rrAr!rr)Ú _get_lineÚ_matchrlÚmoÚgrouprIrBrêr]ÚUntagged_responserdÚ ContinuationrKrbÚintrrÚ Response_code)rVÚrespr&r§r¨Zdat2rrr>r>r?rn5sD                 zIMAP4._get_responsec Csˆ|j|}|dur|j|=|S|rDd}|j |d¡}|durD||fS| ¡z | ¡Wq|jy€}z‚WYd}~qd}~00qdS)Nr)rIrJÚpoprrnrB)rVr&r)Úresultr§rr(r>r>r?r*…s  zIMAP4._get_tagged_responsecCs>| ¡}|s| d¡‚| d¡s.| d|¡‚|dd…}|S)Nzsocket error: EOFr z#socket error: unterminated line: %réþÿÿÿ)r”rBÚendswithr–r>r>r?r.¦s   zIMAP4._get_linecCs| |¡|_|jduSrz)Úmatchr0)rVZcreÚsr>r>r?r/¹s z IMAP4._matchcCs2|jtt|jƒ|jƒ}|jd|_d|j|<|S)Nr“)rkr#rêrMr]rI)rVr&r>r>r?r"Ås  zIMAP4._new_tagcCs$| dd¡}| dd¡}d|dS)Nú\z\\ú"z\")r¼)rVr'r>r>r?ràÍs  z IMAP4._quotecGs| ||j|g|¢RŽ¡Srz)r+r%rr>r>r?r¶ÕszIMAP4._simple_commandcCs8|dkr||fS||jvr$|dgfS|j |¡}||fS)Nr)rJr7)rVr§r¨r¦rr>r>r?r£Ús    zIMAP4._untagged_response)rÛrÜ)rÛrÜ)r­F)N)F)Ur;r<r=Ú__doc__rRr@rBrCÚ IMAP4_PORTrZrOrgrQryr{r~r…rPrr”r›rSr‚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ÿrrr r r rrrrrrrrr%r+rornr*r.r/r"ràr¶r£Ú_mesgZ_dump_urÚ_logZ print_logr>r>r>r?rŠsš/*                      M P ! $rc@s>eZdZdZdeddddfdd„Zdd„Zdedfdd „ZdS) Ú IMAP4_SSLaäIMAP4 client class over SSL connection Instantiate with: IMAP4_SSL([host[, port[, keyfile[, certfile[, ssl_context[, timeout=None]]]]]]) 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. timeout - socket timeout (default: None) If timeout is not given or is None, the global default socket timeout is used for more documentation see the docstring of the parent class IMAP4. rDNcCs’|dur|durtdƒ‚|dur0|dur0tdƒ‚|dus@|durVddl}| dtd¡||_||_|durxtj||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 insteadé)ÚcertfileÚkeyfile) rÚwarningsÚwarnÚDeprecationWarningrFrErrrrrZ)rVrWrXrFrErrYrGr>r>r?rZ#s"ÿÿzIMAP4_SSL.__init__cCst ||¡}|jj||jdS)Nr)rr…rrrW)rVrYr‡r>r>r?r…7s ÿ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)rrPrUr>r>r?rP<szIMAP4_SSL.open)r;r<r=r?ÚIMAP4_SSL_PORTrZr…rPr>r>r>r?rCs ÿ rCc@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 |¡dSrz)r rrZ)rVr r>r>r?rZSszIMAP4_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ÚshellÚ close_fds)rWrXr‡r‰Ú subprocessÚPopenr rÚPIPEr¾rLÚ writefilerMÚreadfilerUr>r>r?rPXsý zIMAP4_stream.opencCs |j |¡SrŠ)rTr)rVrr>r>r?rhszIMAP4_stream.readcCs |j ¡S)r’)rTr”rer>r>r?r”mszIMAP4_stream.readlinecCs|j |¡|j ¡dS)r˜N)rSÚwriteÚflushršr>r>r?r›rs zIMAP4_stream.sendcCs"|j ¡|j ¡|j ¡dS)rœN)rTržrSr¾Úwaitrer>r>r?rSxs  zIMAP4_stream.shutdown)NNN) r;r<r=r?rZrPrr”r›rSr>r>r>r?rGs  rc@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) r½zcPrivate class to provide en/decoding for base64-based authentication conversation. cCs ||_dSrz)rÀ)rVZmechinstr>r>r?rZ†sz_Authenticator.__init__cCs&| | |¡¡}|durdS| |¡S)Nó*)rÀr¿rë)rVrÚretr>r>r?r¾‰sz_Authenticator.processcCsnd}t|tƒr| d¡}|rjt|ƒdkrB|dd…}|dd…}n|}d}t |¡}|r||dd…}q|S)NrArfé0r¢)rérêrërŽÚbinasciiZ b2a_base64)rVÚinpZoupÚtÚer>r>r?rës      z_Authenticator.encodecCs|sdSt |¡Sr)r[Z a2b_base64)rVr\r>r>r?r¿§sz_Authenticator.decodeN)r;r<r=r?rZr¾rër¿r>r>r>r?r½€s r½z0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecræcCsi|]\}}| ¡|d“qS)r“)rë)Ú.0Únr<r>r>r?Ú ­rArar“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Úhourr‹ÚsecÚzonehÚzonemé<ó-r¢) Ú InternalDater;ÚMon2numr1r4ÚcalendarZtimegmÚtimeÚ localtime)r6r0rbrcrdrerfr‹rgrhriÚzoneÚttÚutcr>r>r?r¯s$  rcCs@d}d}tt|ƒƒ}|rr>r?r Òs  r cCs$t |¡}|sdSt| d¡ ¡ƒS)z-Convert IMAP4 flags response to python tuple.r>r¸)ÚFlagsr;r,r1r-)r6r0r>r>r?r ßs r cCst|ttfƒr"t |tj¡ ¡}nàt|tƒr¶z |j }WnXt yŽt j r‚|d}|dkrnt   t  |¡¡d}t jt jf| }nt j }Yn0t|d}t|dd…dt|ƒiŽ}nLt|tƒrØ|jdurÒ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)r>r>zdate_time not of a known typez"%d-{}-%Y %H:%M:%S %z")rér4ÚfloatrZ fromtimestamprrsZ astimezoner,Ú tm_gmtoffrwroÚdaylightrpÚmktimeÚaltzonerr|rrêÚformatÚMonthsZmonthÚstrftime)r¹ZdtZgmtoffÚdstr‘Úfmtr>r>r?r ês2 ÿ       r Ú__main__zd:s:)r>r>z-dz-s)rDzIMAP password for %s on %s: Ú localhostzJFrom: %(user)s@localhost%(lf)sSubject: IMAP4 test%(lf)s%(lf)sdata...%(lf)sÚ )ráZlfrã)rË)ú /tmp/xxx 1)rö)rŠz/tmp/yyy)r©ú /tmp/yyz 2r»rŒ)rß)z/tmpzyy*)rýr‹)rù)NZSUBJECTÚ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,ZALL)r¬)rû)r|r>cCsLt d||f¡tt|ƒ|Ž\}}t d|||f¡|dkrH|d‚|S)Nz%s %sz %s => %s %srr)ÚMrAru)Úcmdr}r§r¨r>r>r?ÚrunKs r’rzPROTOCOL_VERSION = %szCAPABILITIES = %rrß)z/tmp/zyy%z .*"([^"]+)"$r¢rÌrrz%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 )Vr?rmr[r rir^r‚rPr€rornrrrÚiorrrÚ ImportErrorÚ__all__rµrEr@rJrpr•rŒrtr_r3ryrlrarbr³r5r2rdr`rcrrCr»rr½r-rƒÚ enumeratermrr r r r;ÚgetoptZgetpassÚargvZoptlistr}r@r(Zstream_commandÚoptr4rWZgetuserZUSERZPASSWDZ test_mesgZ test_seq1Z test_seq2r’rrGrArrrqr‘Úmlr;r0r1Úpathr¨rÚprintr>r>r>r?ÚsNH    Õ0     ÿ 6 9, #  )       òù           ÿ  ý