U e5dfÑã*@sždZdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z m Z m Z ddlmZzddlZdZWnek 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šddl5Z5ddl6Z6ze5 5ej7d+d…d1¡\Z8Z9Wn.e5j:k rpZ;z d2\Z8Z9W5dZ;[;XYnXdZe;ƒZne=d4krze;Zd?d@ddeCffdAdBdCdDdEdFdGdHdIfZDdJdKdLdMd?dddeCffdHdNfZEdOdP„ZFze<rbe*e<ƒZGne'e?ƒZGeGjHdQkr‚eDd+d…ZDeG IdReGjJ¡eG IdSeGjKf¡eDD]\ZLZ9eFeLe9ƒq¨eFdTdUƒ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-d¥dUdV„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Žd„ZIdd‘„ZJd’d“„ZKd”d•„ZLd–d—„ZMd˜d™„ZNdšd›„ZOd©dœd„ZPdždŸ„ZQd d¡„ZRd¢d£„ZSdqS)ª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=ú/usr/lib64/python3.8/imaplib.pyÚerror´sr?c@s eZdZdS)z IMAP4.abortNr9r=r=r=r>Úabortµsr@c@s eZdZdS)zIMAP4.readonlyNr9r=r=r=r>Úreadonly¶srAÚc Cs”t|_d|_d|_i|_i|_d|_d|_d|_d|_ |  ¡|  ||¡z |  ¡Wn8t k rŽz | ¡Wntk r†YnX‚YnXdS)NrrBFr)Ú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©rTr=r=r>rMÑszIMAP4._mode_asciicCs(d|_d|_t t¡|_t t¡|_dS)NTúutf-8)rYrZr[r\r]r_r`rarbr=r=r>Ú _mode_utf8Øs zIMAP4._mode_utf8cCsòtt dd¡ƒ|_t d|jdtj¡|_d|_d|_ i|_ |j dkrf|  dt ¡|  d |j¡| ¡|_d |jkr‚d |_nd |jkr”d |_n | |j¡‚| ¡|j dkrÄ|  d|jf¡tD]}||jkrØqÈ||_dS| d¡‚dS)Niiÿÿs(?Ps"\d+) (?P[A-Z]+) (?P.*)é rézimaplib version %sznew IMAP4 connection, tag=%sZPREAUTHrÚOKrézCAPABILITIES: %rzserver not IMAP4 compliant)r ÚrandomZrandintÚtagprer[r\r^ÚtagreÚ _cmd_log_lenÚ _cmd_log_idxÚ_cmd_logrDÚ_mesgÚ __version__Ú _get_responseZwelcomerHrEr?Ú_get_capabilitiesÚ capabilitiesÚAllowedVersionsÚPROTOCOL_VERSION)rTÚversionr=r=r>rOßs:ÿþþ       zIMAP4._connectcCs&|tkrt|| ¡ƒStd|ƒ‚dS)NzUnknown IMAP4 command: '%s')ÚCommandsÚgetattrÚlowerÚAttributeError)rTÚattrr=r=r>Ú __getattr__ szIMAP4.__getattr__cCs|S©Nr=rbr=r=r>Ú __enter__szIMAP4.__enter__cGs4|jdkrdSz | ¡Wntk r.YnXdS)Nr)rEÚlogoutrR)rTÚargsr=r=r>Ú__exit__s   zIMAP4.__exit__cCs4|js dn|j}t d||j|j¡t ||jf¡S)Nz imaplib.open)rUÚsysÚauditrVÚsocketZcreate_connection)rTrUr=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)rUrVr…ÚsockÚmakefileÚfilerSr=r=r>rN's z IMAP4.opencCs |j |¡S©zRead 'size' bytes from remote.)r‰Úread©rTÚsizer=r=r>r‹3sz IMAP4.readcCs.|j td¡}t|ƒtkr*| dt¡‚|S)úRead line from remote.rfzgot more than %d bytes)r‰ÚreadlineÚ_MAXLINEÚlenr?©rTÚliner=r=r>r8s zIMAP4.readlinecCst d||¡|j |¡dS)úSend data to remote.z imaplib.sendN)r‚rƒr‡Zsendall©rTÚdatar=r=r>Úsend@sz IMAP4.sendc Cst|j ¡zXz|j tj¡Wn@tk r^}z"|jtjkrNt |ddƒdkrN‚W5d}~XYnXW5|j ¡XdS)ú Close I/O established in "open".Zwinerrorri&'N) r‰Úcloser‡rQr„Z SHUT_RDWRrRÚerrnoZENOTCONNrx)rTÚexcr=r=r>rQFs  ÿzIMAP4.shutdowncCs|jS)zfReturn socket instance used to connect to IMAP4 server. socket = .socket() )r‡rbr=r=r>r„Vsz 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. ZRECENTrgNéÿÿÿÿ)Ú_untagged_responseÚnoop©rTÚnameÚtypÚdatr=r=r>Úrecentbs  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)rTÚcoder=r=r>ÚresponsesszIMAP4.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ÚCRLFrYrFÚ_simple_command)rTÚmailboxÚflagsÚ date_timeÚmessager rFr=r=r>Úappends    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. rrgrœrcÚreplacer)r¤Ú_AuthenticatorÚprocessrFr°r?ÚdecoderE)rTZ mechanismZ authobjectÚmechr¡r¢r=r=r>Ú authenticate›s zIMAP4.authenticatecCs d}| |¡\}}| |||¡S)zT(typ, [data]) = .capability() Fetch capabilities list from server.r©r°rrŸr=r=r>Ú capability¸szIMAP4.capabilitycCs | d¡S)zRCheckpoint mailbox on server. (typ, [data]) = .check() r©r°rbr=r=r>ÚcheckÁsz IMAP4.checkcCs$z| d¡\}}W5d|_X||fS)zÌClose currently selected mailbox. Deleted messages are removed from writable mailbox. This is the recommended command before 'LOGOUT'. (typ, [data]) = .close() rr)rEr°©rTr¡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¾)rTÚ message_setZ new_mailboxr=r=r>ÚcopyØsz IMAP4.copycCs | d|¡S)zPCreate new mailbox. (typ, [data]) = .create(mailbox) rr¾©rTr±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¾)rTr±Ú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 ENABLErgz UTF8=ACCEPT)rsrr?r°r¤rd)rTr½r¡r–r=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. 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¼)rTrÁZ message_partsr r¡r¢r=r=r>Úfetchs z IMAP4.fetchcCs| d|¡\}}| ||d¡S)zXGet the ACLs for a mailbox. (typ, [data]) = .getacl(mailbox) rZACLr¼©rTr±r¡r¢r=r=r>Úgetaclsz IMAP4.getaclcCs"| d|||¡\}}| ||d¡S)za(typ, [data]) = .getannotation(mailbox, entry, attribute) Retrieve ANNOTATIONs.r Ú ANNOTATIONr¼)rTr±Ú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¼)rTÚrootr¡r¢r=r=r>Úgetquota0szIMAP4.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¼)rTr±r¡r¢ZquotaZ quotarootr=r=r>Ú getquotaroot;szIMAP4.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¼©rTZ directoryÚpatternr r¡r¢r=r=r>ÚlistFsz IMAP4.listcCs<| d|| |¡¡\}}|dkr.| |d¡‚d|_||fS)z”Identify client using plaintext password. (typ, [data]) = .login(user, password) NB: 'password' will be quoted. r%rgrœr)r°Ú_quoter?rE)rTÚuserÚpasswordr¡r¢r=r=r>ÚloginRs 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)rTrÚ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. rNrcú Zmd5)ÚhmacÚ isinstancerÛÚstrÚencoderÚZHMACZ hexdigest)rTZ challengeràÚpwdr=r=r>rÝis ÿzIMAP4._CRAM_MD5_AUTHcCs$d|_| d¡\}}| ¡||fS)z|Shutdown connection to server. (typ, [data]) = .logout() Returns server 'BYE' response. r)rEr°rQrÀr=r=r>rqsz 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>Ú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Ë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 |jdkr| |j¡| d¡S)zFSend NOOP command. (typ, [data]) = .noop() rhr()rDÚ_dump_urrHr°rbr=r=r>rž›s  z 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¼)rTZ message_numZ message_partÚstartZlengthr 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¾)rTrÚr r=r=r>Ú proxyauth²s zIMAP4.proxyauthcCs| d||¡S)zkRename old mailbox name to new. (typ, [data]) = .rename(oldmailbox, newmailbox) r+r¾)rTZ 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)rYrr?r°r)rTÚcharsetZcriteriar r¡r¢r=r=r>ÚsearchÇs z IMAP4.searchr§FcCsŒi|_||_|rd}nd}| ||¡\}}|dkr@d|_||fSd|_d|jkrx|sx|jdkrj| |j¡| d|¡‚||j d d 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-rgrrú READ-ONLYrfz%s is not writableÚEXISTSN)rHrJr°rErDrèrAÚget)rTr±rAr r¡r¢r=r=r>ÚselectÙs"  ÿ  z IMAP4.selectcCs| d|||¡S)zZSet a mailbox acl. (typ, [data]) = .setacl(mailbox, who, what) r.r¾)rTr±rÆZwhatr=r=r>Úsetaclùsz IMAP4.setaclcGs |jd|žŽ\}}| ||d¡S)z_(typ, [data]) = .setannotation(mailbox[, entry, attribute]+) Set ANNOTATIONs.r/rÍ)r/r¼)rTr€r¡r¢r=r=r>Ú setannotationszIMAP4.setannotationcCs | d||¡\}}| ||d¡S)ziSet the quota root's resource limits. (typ, [data]) = .setquota(root, limits) r0rÐr¼)rTrÑZlimitsr¡r¢r=r=r>Úsetquota szIMAP4.setquotacGsFd}|d|dfdkr d|}|j|||f|žŽ\}}| |||¡S)zIMAP4rev1 extension SORT command. (typ, [data]) = .sort(sort_criteria, charset, search_criteria, ...) r1rrœr¨r«r¼)rTZ 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) Nr2zSSL support missingzTLS session already establishedzTLS not supported by serverrg©Zserver_hostnamer†TzCouldn't establish TLS session)ÚHAVE_SSLr?rLr@rsÚsslÚ_create_stdlib_contextr°Ú wrap_socketr‡rUrˆr‰rrr)rTÚ ssl_contextr r¡r¢r=r=r>Ústarttls s&    ÿ  zIMAP4.starttlscCs$d}| |||¡\}}| |||¡S)zpRequest named status conditions for mailbox. (typ, [data]) = .status(mailbox, names) r3r¼)rTr±Únamesr r¡r¢r=r=r>Ústatus7sz 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¼)rTrÁÚcommandr²r¡r¢r=r=r>ÚstoreCsz IMAP4.storecCs | d|¡S)zYSubscribe to new mailbox. (typ, [data]) = .subscribe(mailbox) r5r¾rÃr=r=r>Ú subscribeNszIMAP4.subscribecGs*d}|j|||f|žŽ\}}| |||¡S)z‹IMAPrev1 extension THREAD command. (type, [data]) = .thread(threading_algorithm, charset, search_criteria, ...) r6r¼)rTZthreading_algorithmrîr÷r r¡r¢r=r=r>ÚthreadVsz 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: %sú9command %s illegal in state %s, only allowed in states %sú, r7)r,r1r6r)r¤rwr?rEÚjoinr°r)rTrr€r r¡r¢r=r=r>Úuid`s ÿþz IMAP4.uidcCs | d|¡S)z_Unsubscribe from old mailbox. (typ, [data]) = .unsubscribe(mailbox) r8r¾rÃr=r=r>Ú unsubscribeyszIMAP4.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'. )r¤rwrEr°©rTr r€r=r=r>Úxatoms  z IMAP4.xatomc Csb|dkr d}|j}|jdkr<| d|t| |d¡ƒ|f¡||krT|| |¡n |g||<dS)Nóéz#untagged_responses[%s] %s += ["%r"]rB)rHrDror‘ròrµ)rTr¡r¢Zurr=r=r>Ú_append_untagged—s ÿzIMAP4._append_untaggedcCs,|j d¡}|r(| |d |jd¡¡‚dS)NÚBYErœr¶)rHròr@r¹rZ)rTÚbyer=r=r>Ú _check_bye¥s zIMAP4._check_byec Gs2|jt|kr4d|_| d||jd t|¡f¡‚dD]}||jkr8|j|=q8d|jkrj|jsj| d¡‚| ¡}t ||j ƒ}|d|}|D]0}|dkrœqŽt |t ƒr²t ||j ƒ}|d|}qŽ|j}|dk r d|_t |ƒt |jƒkrî|}nd}|t dt|ƒ|j ƒ}|jdkr&| d |¡n| d |¡z| |t¡Wn2tk rx} z| d | ¡‚W5d} ~ XYnX|dkrˆ|S| ¡r¦|j|rˆ|Sqˆ|r¶||jƒ}|jdkrÔ| d t|ƒ¡z| |¡| t¡Wn2tk r} z| d | ¡‚W5d} ~ XYnX|sˆq.qˆ|S) Nrr©rgÚNOÚBADrðz#mailbox status changed to READ-ONLYó z {%s}éz> %rzsocket error: %szwrite literal size %s)rErwrFr?rrHrJrAÚ_new_tagÚbytesrZrárâÚtypeÚ_commandr‘rDroÚ_logr—r¯rRr@rqrGrI) rTr r€r¡Útagr–ÚargrFZ literatorÚvalr=r=r>r«sl ÿþ   ÿ               zIMAP4._commandc CsÆ|dk}|s| ¡z|j||d\}}Wnj|jk r`}z| d||f¡‚W5d}~XYn6|jk r”}z| d||f¡‚W5d}~XYnX|s¢| ¡|dkr¾| d|||f¡‚||fS)Nr)Ú expect_byezcommand: %s => %srz%s command error: %s %s)rÚ_get_tagged_responser@r?)rTr rrr¡r–rr=r=r>Ú_command_completeøs"$zIMAP4._command_completecCsJ| ¡\}}|dgkr | d¡‚t|d|jƒ}| ¡}t| ¡ƒ|_dS)Nz"no CAPABILITY response from serverrœ)r½r?rârZr¤ÚtupleÚsplitrsrÀr=r=r>rr s    zIMAP4._get_capabilitiescCsò| ¡}| |j|¡rr|j d¡}||jkr:| d|¡‚|j d¡}t||jƒ}|j d¡}||gf|j|<nd}| t |¡sœ| |j |¡rœ|j d¡}|jdkrÒ| t |¡rÄ|j d¡|_ dS| d|¡‚|j d¡}t||jƒ}|j d¡}|dkrd}|r|d|}| |j |¡rvt|j d ¡ƒ}|jd krP| d |¡| |¡}| |||f¡| ¡}q| ||¡|d krÆ| t|¡rÆ|j d¡}t||jƒ}| ||j d¡¡|jd krî|dkrî| d||f¡|S)Nrzunexpected tagged response: %rrr–Zdata2zunexpected response: %rr rrrzread literal size %srrf)rrrz%s response: %r)Ú _get_lineÚ_matchrkÚmoÚgrouprGr@rârZÚUntagged_responseraÚ ContinuationrIr_ÚintrDror‹rÚ Response_code)rTÚresprr¡r¢Zdat2rr–r=r=r>rqsP                    zIMAP4._get_responsec Cs˜|j|}|dk r|j|=|S|rDd}|j |d¡}|dk rD||fS| ¡z | ¡Wq|jk r}z|jdkr~| ¡‚W5d}~XYqXqdS)Nrrf)rGrHÚpoprrqr@rDÚ print_log)rTrr Úresultr¡rrr=r=r>r!cs    zIMAP4._get_tagged_responsecCsf| ¡}|s| d¡‚| d¡s.| d|¡‚|dd…}|jdkrT| d|¡n| d|¡|S)Nzsocket error: EOFr z#socket error: unterminated line: %réþÿÿÿrz< %r)rr@ÚendswithrDrorr’r=r=r>r%„s    zIMAP4._get_linecCsD| |¡|_|jdk r:|jdkr:| d|j|j ¡f¡|jdk S)Nrz matched %r => %r)Úmatchr'rDror×Úgroups)rTZcreÚsr=r=r>r&—s z IMAP4._matchcCs2|jtt|jƒ|jƒ}|jd|_d|j|<|S)Nrf)rjrrârKrZrG)rTrr=r=r>r£s  zIMAP4._new_tagcCs$| dd¡}| dd¡}d|dS)Nú\z\\ú"z\")r¶)rTrr=r=r>rÙ«s  z IMAP4._quotecGs| ||j|f|žŽ¡Sr})r"rr r=r=r>r°³szIMAP4._simple_commandcCsT|dkr||fS||jkr$|dgfS|j |¡}|jdkrL| d||f¡||fS)Nrrzuntagged_responses[%s] => %s)rHr.rDro)rTr¡r¢r r–r=r=r>r¸s    zIMAP4._untagged_responsecCsN|dkrt ¡}t dt |¡¡}tj d||dd|f¡tj ¡dS)Nz%M:%Sz %s.%02d %s éd)ÚtimeÚstrftimeÚ localtimer‚ÚstderrÚwriteÚflush)rTr5ZsecsZtmr=r=r>roÆs z IMAP4._mesgcCs>| ¡}|sdSd}tdd„|ƒ}| d|| |¡f¡dS)Nz cSs*d|d|ddr"d |d¡p$dfS)Nz%s: "%s"rrfz" "rB)r)Úxr=r=r>ÚÒr z IMAP4._dump_ur..zuntagged responses dump:%s%s)ÚitemsÚmapror)rTÚdictÚlÚtr=r=r>rèÍs zIMAP4._dump_urcCs8|t ¡f|j|j<|jd7_|j|jkr4d|_dS)Nrfr)r9rnrmrlr’r=r=r>rÕs z IMAP4._logcCsl| dt|jƒ¡|j|j}}|rhz|j|j|ŽWn YnX|d7}||jkr^d}|d8}q"dS)Nzlast %d IMAP4 interactions:rfr)ror‘rnrmrl)rTÚiÚnr=r=r>r/Üs zIMAP4.print_log)rÔrÕ)rÔrÕ)r§F)N)F)N)Tr:r;r<Ú__doc__rPr?r@rAÚ IMAP4_PORTrWrMrdrOr|r~rr…rNr‹rr—rQr„r£r¦rµr»r½r¿r™rÂrÄrÅrÇrÈrÉrÊrÌrÏrÒrÓrØrÜrÞrÝrrårærçržrêrërìrïrórôrõrörørÿrrrrr r r rrrr"rrrqr!r%r&rrÙr°rrorèrr/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. rBNcCs|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 insteadé)ÚcertfileÚkeyfile) Ú ValueErrorÚwarningsÚwarnÚDeprecationWarningrMrLrûrürþrrW)rTrUrVrMrLrþrOr=r=r>rWÿs$ÿÿzIMAP4_SSL.__init__cCst |¡}|jj||jdS)Nrù)rr…rþrýrU)rTr‡r=r=r>r…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)rrNrSr=r=r>rNszIMAP4_SSL.open)r:r;r<rHÚIMAP4_SSL_PORTrWr…rNr=r=r=r>rJìsÿ rJc@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 |¡dSr})rrrW)rTrr=r=r>rW/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)rUrVr‡r‰Ú subprocessÚPopenrrÚPIPEr¸rTÚ writefilerUÚreadfilerSr=r=r>rN4sý zIMAP4_stream.opencCs |j |¡SrŠ)r[r‹rŒr=r=r>r‹DszIMAP4_stream.readcCs |j ¡S)rŽ)r[rrbr=r=r>rIszIMAP4_stream.readlinecCs|j |¡|j ¡dS)r”N)rZr=r>r•r=r=r>r—Ns zIMAP4_stream.sendcCs"|j ¡|j ¡|j ¡dS)r˜N)r[r™rZr¸Úwaitrbr=r=r>rQTs  zIMAP4_stream.shutdown)NN) r:r;r<rHrWrNr‹rr—rQr=r=r=r>r#s  c@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) r·zcPrivate class to provide en/decoding for base64-based authentication conversation. cCs ||_dSr})rº)rTZmechinstr=r=r>rWbsz_Authenticator.__init__cCs&| | |¡¡}|dkrdS| |¡S)Nó*)rºr¹rã)rTr–Zretr=r=r>r¸esz_Authenticator.processcCsnd}t|tƒr| d¡}|rjt|ƒdkrB|dd…}|dd…}n|}d}t |¡}|r||dd…}q|S)Nr rcé0rœ)rárârãr‘ÚbinasciiZ b2a_base64)rTÚinpZouprEÚer=r=r>rãks      z_Authenticator.encodecCs|sdSt |¡S)Nr )r_Z a2b_base64)rTr`r=r=r>r¹ƒsz_Authenticator.decodeN)r:r;r<rHrWr¸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)rf)rã)Ú.0rGr5r=r=r>Ú ‰srcrfc 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œ) Ú InternalDater3ÚMon2numr(r+ÚcalendarZtimegmr9r;)r-r'rdrerfrgrhrirjrkrlZzoneZttÚutcr=r=r>r‹s$  cCs@d}d}tt|ƒƒ}|rr ®s cCs$t |¡}|sdSt| d¡ ¡ƒS)z-Convert IMAP4 flags response to python tuple.r=r²)ÚFlagsr3r#r(r$)r-r'r=r=r>r »s cCst|ttfƒr"t |tj¡ ¡}nât|tƒr¸z |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)r7r7zdate_time not of a known typez"%d-{}-%Y %H:%M:%S %z")rár+ÚfloatrZ fromtimestamprrrZ astimezoner#Ú tm_gmtoffrzr9Údaylightr;ÚmktimeÚaltzonerrzrNrâÚformatÚMonthsZmonthr:)r³ZdtZgmtoffZdstZdeltaZfmtr=r=r>r Æs2 ÿ      Ú__main__zd:s:)r=r=z-dz-s)rBzIMAP password for %s on %s: Z 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ï)NZSUBJECTZtest)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ñ)rr=cCsLt d||f¡tt|ƒ|Ž\}}t d|||f¡|dkrH|d‚|S)Nz%s %sz %s => %s %srr)ÚMrorx)Úcmdr€r¡r¢r=r=r>Úrun's r‹rzPROTOCOL_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 )UrHrpr_ršrir[r„rWr‚r9rqrrrÚiorrûrúÚ ImportErrorÚ__all__r¯rCrIrRrtrrwr\r*rwror^r_r­r,r)rar]r`rrJrµrr·r$rÚ enumeraterprr r r r:ZgetoptZgetpassÚargvZoptlistr€r?rZstream_commandÚoptr+rUZgetuserZUSERZPASSWDZ test_mesgZ test_seq1Z test_seq2r‹r‰rErorursrŠZmlr3r'r(Úpathr¢r Úprintr=r=r=r>ÚsZH  ÿ Ö/     ÿl4 9, #  )       òù           ÿ  ý