U e5df*@sdZdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z m Z m Z ddlmZzddlZdZWnek rdZYnXdd d d d d gZdZdZdZdZdZdZdddddddddddddddddddddddddddddddddddddddddd)ZedZedZedZedejZ edZ!edZ"edZ#ed ejZ$dZ%d Z&Gd!ddZ'erGd"d#d#e'Z(e)d#Gd$d d e'Z*Gd%d&d&Z+d',d(Z-d)d*e.e-d+dDZ/d,d Z0d-d Z1d.d Z2d/d Z3e4d0krddl5Z5ddl6Z6ze55ej7d+dd1\Z8Z9Wn.e5j:k rpZ;z d2\Z8Z9W5dZ;[;XYnXdZe;Zne=d4krze;Zd?d@ddeCffdAdBdCdDdEdFdGdHdIfZDdJdKdLdMd?dddeCffdHdNfZEdOdPZFze<rbe*e<ZGne'e?ZGeGjHdQkreDd+dZDeGIdReGjJeGIdSeGjKfeDD]\ZLZ9eFeLe9qeFdTdUD].*))?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@seZdZdZGdddeZGdddeZGdddeZdefd d Z d d Z d dZ ddZ ddZ ddZddZddZdefddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Z d9d:Z!d;d<Z"d=d>Z#d?d@Z$dAdBZ%dCdDZ&dEdFZ'dGdHZ(ddKdLZ)dMdNZ*dOdPZ+dQdRZ,dSdTZ-ddUdVZ.dWdXZ/dYdZZ0d[d\Z1d]d^Z2d_d`Z3dadbZ4dcddZ5ddgdhZ6didjZ7dkdlZ8dmdnZ9dodpZ:ddrdsZ;dtduZdzd{Z?d|d}Z@d~dZAddZBddZCddZDddZEddZFddZGddZHdddZIddZJddZKddZLddZMddZNddZOdqS)raIMAP4 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.pyerrorsr?c@s eZdZdS)z IMAP4.abortNr9r=r=r=r>abortsr@c@s eZdZdS)zIMAP4.readonlyNr9r=r=r=r>readonlysrAc Cst|_d|_d|_i|_i|_d|_d|_d|_d|_ | | ||z | Wn8t k rz |Wntk rYnXYnXdS)NrrBFr)Debugdebugstateliteraltagged_commandsuntagged_responsescontinuation_response is_readonlytagnum_tls_established _mode_asciiopen_connect ExceptionshutdownOSErrorselfhostportr=r=r>__init__s&   zIMAP4.__init__cCs0d|_d|_tttj|_tttj|_dS)NFascii) utf8_enabled _encodingrecompile_LiteralASCIILiteral_Untagged_statusUntagged_statusrTr=r=r>rMszIMAP4._mode_asciicCs(d|_d|_tt|_tt|_dS)NTutf-8)rYrZr[r\r]r_r`rarbr=r=r> _mode_utf8s zIMAP4._mode_utf8cCsttdd|_td|jdtj|_| |_ d|j krHd|_ nd|j krZd|_ n | |j |tD]}||jkrqr||_dS| d dS) Niis(?Ps"\d+) (?P[A-Z]+) (?P.*)ZPREAUTHrOKrzserver not IMAP4 compliant)r randomZrandinttagprer[r\r^tagre __version__ _get_responseZwelcomerHrEr?_get_capabilitiesAllowedVersions capabilitiesPROTOCOL_VERSION)rTversionr=r=r>rOs*     zIMAP4._connectcCs&|tkrt||Std|dS)NzUnknown IMAP4 command: '%s')CommandsgetattrlowerAttributeError)rTattrr=r=r> __getattr__ szIMAP4.__getattr__cCs|SNr=rbr=r=r> __enter__szIMAP4.__enter__cGs4|jdkrdSz |Wntk r.YnXdS)Nr)rElogoutrR)rTargsr=r=r>__exit__s   zIMAP4.__exit__cCs4|js dn|j}td||j|jt||jfS)Nz imaplib.open)rUsysauditrVsocketZcreate_connection)rTrUr=r=r>_create_socketszIMAP4._create_socketcCs(||_||_||_|jd|_dS)zSetup 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~sockmakefilefilerSr=r=r>rN's z IMAP4.opencCs |j|SzRead 'size' bytes from remote.)rreadrTsizer=r=r>r3sz IMAP4.readcCs.|jtd}t|tkr*|dt|S)Read line from remote.zgot more than %d bytes)rreadline_MAXLINElenr?rTliner=r=r>r8s zIMAP4.readlinecCstd|||j|dS)Send data to remote.z imaplib.sendN)r{r|rZsendallrTdatar=r=r>send@sz IMAP4.sendc Cst|jzXz|jtjWn@tk r^}z"|jtjkrNt |dddkrNW5d}~XYnXW5|jXdS) Close I/O established in "open".Zwinerrorri&'N) rcloserrQr}Z SHUT_RDWRrRerrnoZENOTCONNrq)rTexcr=r=r>rQFs  zIMAP4.shutdowncCs|jS)zfReturn socket instance used to connect to IMAP4 server. socket = .socket() )rrbr=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. ZRECENTreN)_untagged_responsenooprTnametypdatr=r=r>recentbs  z IMAP4.recentcCs||dg|S)zReturn data for response 'code' if received, or None. Old value for response 'code' is cleared. (code, [data]) = .response(code) N)rupper)rTcoder=r=r>responsesszIMAP4.responsecCsxd}|s d}|r.|d|dfdkr2d|}nd}|r@t|}nd}tt|}|jrbd|d }||_|||||S) zAppend message to named mailbox. (typ, [data]) = .append(mailbox, flags, date_time, message) All args except `message' can be None. rINBOXrr()(%s)NsUTF8 ())r MapCRLFsubCRLFrYrF_simple_command)rTmailboxflags date_timemessagerrFr=r=r>appends    z IMAP4.appendcCsP|}t|j|_|d|\}}|dkrB||dddd|_||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. rrerrcreplacer)r_AuthenticatorprocessrFrr?decoderE)rTZ mechanismZ authobjectmechrrr=r=r> authenticates zIMAP4.authenticatecCs d}||\}}||||S)zT(typ, [data]) = .capability() Fetch capabilities list from server.rrrrr=r=r> capabilityszIMAP4.capabilitycCs |dS)zRCheckpoint mailbox on server. (typ, [data]) = .check() rrrbr=r=r>checksz IMAP4.checkcCs$z|d\}}W5d|_X||fS)zClose currently selected mailbox. Deleted messages are removed from writable mailbox. This is the recommended command before 'LOGOUT'. (typ, [data]) = .close() rr)rErrTrrr=r=r>rsz IMAP4.closecCs|d||S)zCopy 'message_set' messages onto end of 'new_mailbox'. (typ, [data]) = .copy(message_set, new_mailbox) rr)rT message_setZ new_mailboxr=r=r>copysz IMAP4.copycCs |d|S)zPCreate new mailbox. (typ, [data]) = .create(mailbox) rrrTrr=r=r>createsz IMAP4.createcCs |d|S)zPDelete old mailbox. (typ, [data]) = .delete(mailbox) rrrr=r=r>deletesz IMAP4.deletecCs|d||S)zDelete the ACLs (remove any rights) set for who on mailbox. (typ, [data]) = .deleteacl(mailbox, who) rr)rTrwhor=r=r> deleteaclszIMAP4.deleteaclcCsHd|jkrtd|d|\}}|dkr@d|kr@|||fS)zkSend an RFC5161 enable string to the server. (typ, [data]) = .enable(capability) rzServer does not support ENABLErez UTF8=ACCEPT)rmrr?rrrd)rTrrrr=r=r>enables   z IMAP4.enablecCs d}||\}}||||S)zPermanently 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. rrrr=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)rTrZ message_partsrrrr=r=r>fetchs z IMAP4.fetchcCs|d|\}}|||dS)zXGet the ACLs for a mailbox. (typ, [data]) = .getacl(mailbox) rZACLrrTrrrr=r=r>getaclsz IMAP4.getaclcCs"|d|||\}}|||dS)za(typ, [data]) = .getannotation(mailbox, entry, attribute) Retrieve ANNOTATIONs.r ANNOTATIONr)rTrentryZ attributerrr=r=r> getannotation(szIMAP4.getannotationcCs|d|\}}|||dS)zGet the quota root's resource usage and limits. Part of the IMAP4 QUOTA extension defined in rfc2087. (typ, [data]) = .getquota(root) r!QUOTAr)rTrootrrr=r=r>getquota0szIMAP4.getquotacCs@|d|\}}|||d\}}|||d\}}|||gfS)zGet the list of quota roots for the named mailbox. (typ, [[QUOTAROOT responses...], [QUOTA responses]]) = .getquotaroot(mailbox) r"rZ QUOTAROOTr)rTrrrZquotaZ quotarootr=r=r> getquotaroot;szIMAP4.getquotaroot""*cCs$d}||||\}}||||S)zList mailbox names in directory matching pattern. (typ, [data]) = .list(directory='""', pattern='*') 'data' is list of LIST responses. r$rrTZ directorypatternrrrr=r=r>listFsz IMAP4.listcCs<|d|||\}}|dkr.||dd|_||fS)zIdentify client using plaintext password. (typ, [data]) = .login(user, password) NB: 'password' will be quoted. r%rerr)r_quoter?rE)rTuserpasswordrrr=r=r>loginRs z IMAP4.logincCs|||_|_|d|jS)zr Force use of CRAM-MD5 authentication. (typ, [data]) = .login_cram_md5(user, password) zCRAM-MD5)rrr_CRAM_MD5_AUTH)rTrrr=r=r>login_cram_md5`szIMAP4.login_cram_md5cCsBddl}t|jtr |jdn|j}|jd|||dS)z1 Authobject to use with CRAM-MD5 authentication. rNrc Zmd5)hmac isinstancerstrencoderZHMACZ hexdigest)rTZ challengerpwdr=r=r>ris zIMAP4._CRAM_MD5_AUTHcCs$d|_|d\}}|||fS)z|Shutdown connection to server. (typ, [data]) = .logout() Returns server 'BYE' response. r)rErrQrr=r=r>rxqsz IMAP4.logoutcCs$d}||||\}}||||S)zList 'subscribed' mailbox names in directory matching pattern. (typ, [data, ...]) = .lsub(directory='""', pattern='*') 'data' are tuples of message part envelope and data. r&rrr=r=r>lsub~sz IMAP4.lsubcCs|d|\}}|||dS)zShow my ACLs for a mailbox (i.e. the rights that I have on mailbox). (typ, [data]) = .myrights(mailbox) r#rrr=r=r>myrightsszIMAP4.myrightscCs d}||\}}||||S)zb Returns IMAP namespaces ala rfc2342 (typ, [data, ...]) = .namespace() r'rrr=r=r> namespaceszIMAP4.namespacecCs |dS)zFSend NOOP command. (typ, [data]) = .noop() r(rrbr=r=r>rsz IMAP4.noopcCs(d}||||||\}}|||dS)zFetch 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_partstartZlengthrrrr=r=r>partialsz IMAP4.partialcCsd}|d|S)zAssume authentication as "user". Allows an authorised administrator to proxy into any user's mailbox. (typ, [data]) = .proxyauth(user) r*r)rTrrr=r=r> proxyauths zIMAP4.proxyauthcCs|d||S)zkRename old mailbox name to new. (typ, [data]) = .rename(oldmailbox, newmailbox) r+r)rTZ oldmailboxZ newmailboxr=r=r>renamesz IMAP4.renamecGsTd}|r2|jrtd|j|d|f|\}}n|j|f|\}}||||S)zSearch 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 modeCHARSET)rYrr?rr)rTcharsetZcriteriarrrr=r=r>searchs z IMAP4.searchrFcCsvi|_||_|rd}nd}|||\}}|dkr@d|_||fSd|_d|jkrb|sb|d|||jdd gfS) 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-rerr READ-ONLYz%s is not writableEXISTSN)rHrJrrErAget)rTrrArrrr=r=r>selects  z IMAP4.selectcCs|d|||S)zZSet a mailbox acl. (typ, [data]) = .setacl(mailbox, who, what) r.r)rTrrZwhatr=r=r>setaclsz IMAP4.setaclcGs |jd|\}}|||dS)z_(typ, [data]) = .setannotation(mailbox[, entry, attribute]+) Set ANNOTATIONs.r/r)r/r)rTryrrr=r=r> setannotationszIMAP4.setannotationcCs |d||\}}|||dS)ziSet the quota root's resource limits. (typ, [data]) = .setquota(root, limits) r0rr)rTrZlimitsrrr=r=r>setquota szIMAP4.setquotacGsFd}|d|dfdkr d|}|j|||f|\}}||||S)zIMAP4rev1 extension SORT command. (typ, [data]) = .sort(sort_criteria, charset, search_criteria, ...) r1rrrrr)rTZ sort_criteriarsearch_criteriarrrr=r=r>sorts z IMAP4.sortNcCsd}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 serverreZserver_hostnamerTzCouldn't establish TLS session)HAVE_SSLr?rLr@rmssl_create_stdlib_contextr wrap_socketrrUrrrkr)rT ssl_contextrrrr=r=r>starttls s&      zIMAP4.starttlscCs$d}||||\}}||||S)zpRequest named status conditions for mailbox. (typ, [data]) = .status(mailbox, names) r3r)rTrnamesrrrr=r=r>status7sz IMAP4.statuscCs>|d|dfdkrd|}|d|||\}}|||dS)zAlters flag dispositions for messages in mailbox. (typ, [data]) = .store(message_set, command, flags) rrrrr4rr)rTrcommandrrrr=r=r>storeCsz IMAP4.storecCs |d|S)zYSubscribe to new mailbox. (typ, [data]) = .subscribe(mailbox) r5rrr=r=r> subscribeNszIMAP4.subscribecGs*d}|j|||f|\}}||||S)zIMAPrev1 extension THREAD command. (type, [data]) = .thread(threading_algorithm, charset, search_criteria, ...) r6r)rTZthreading_algorithmrrrrrr=r=r>threadVsz IMAP4.threadc Gs|}|tkr|d||jt|krL|d||jdt|fd}|j||f|\}}|dkrt|}nd}||||S)zExecute "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: %s9command %s illegal in state %s, only allowed in states %s, r7)r,r1r6r)rrpr?rEjoinrr)rTrryrrrr=r=r>uid`s z IMAP4.uidcCs |d|S)z_Unsubscribe from old mailbox. (typ, [data]) = .unsubscribe(mailbox) r8rrr=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'. )rrprErrTrryr=r=r>xatoms  z IMAP4.xatomcCs8|dkr d}|j}||kr*|||n |g||<dSN)rHrr)rTrrZurr=r=r>_append_untaggeds zIMAP4._append_untaggedcCs,|jd}|r(||d|jddS)NBYErr)rHrr@rrZ)rTbyer=r=r> _check_byes zIMAP4._check_byec Gs|jt|kr4d|_|d||jdt|fdD]}||jkr8|j|=q8d|jkrj|jsj|d|}t ||j }|d|}|D]0}|dkrqt |t rt ||j }|d|}q|j}|dk r d|_t |t |jkr|}nd}|t dt||j }z||tWn2tk rN} z|d| W5d} ~ XYnX|dkr^|S|r||j|r^|Sq^|r||j}z|||tWn2tk r} z|d| W5d} ~ XYnX|s^qq^|S) NrrreNOBADrz#mailbox status changed to READ-ONLY z {%s}zsocket error: %s)rErprFr?rrHrJrA_new_tagbytesrZrrtype_commandrrrrRr@rjrGrI) rTrryrtagrargrFZ literatorvalr=r=r>rsb                 zIMAP4._commandc Cs|dk}|s|z|j||d\}}Wnj|jk r`}z|d||fW5d}~XYn6|jk r}z|d||fW5d}~XYnX|s||dkr|d|||f||fS)Nr) expect_byezcommand: %s => %srz%s command error: %s %s)r _get_tagged_responser@r?)rTrrrxrrrr=r=r>_command_completes"$zIMAP4._command_completecCsJ|\}}|dgkr |dt|d|j}|}t||_dS)Nz"no CAPABILITY response from serverr)rr?rrZrtuplesplitrmrr=r=r>rk s    zIMAP4._get_capabilitiescCs|}||j|rp|jd}||jkr:|d||jd}t||j}|jd}||gf|j|<nd}|t |s||j |r|jd}|jdkr|t |r|jd|_ dS|d||jd}t||j}|jd}|dkrd}|r|d|}||j |rZt|jd }||}||||f|}q||||d kr|t|r|jd}t||j}|||jd|S) Nrzunexpected tagged response: %rrrZdata2zunexpected response: %rrrrr ) _get_line_matchrhmogrouprGr@rrZUntagged_responsera ContinuationrIr_intrr Response_code)rTresprrrZdat2rrr=r=r>rjsH                   zIMAP4._get_responsec Cs|j|}|dk r|j|=|S|rDd}|j|d}|dk rD||fS|z |Wq|jk r~}zW5d}~XYqXqdS)Nr )rGrHpopr rjr@)rTrrresultrr rr=r=r>rcs  zIMAP4._get_tagged_responsecCs>|}|s|d|ds.|d||dd}|S)Nzsocket error: EOFr z#socket error: unterminated line: %r)rr@endswithrr=r=r>rs   zIMAP4._get_linecCs|||_|jdk Srv)matchr)rTZcresr=r=r>rs z IMAP4._matchcCs2|jtt|j|j}|jd|_d|j|<|S)Nr)rgrrrKrZrG)rTrr=r=r>rs  zIMAP4._new_tagcCs$|dd}|dd}d|dS)N\z\\"z\")r)rTrr=r=r>rs  z IMAP4._quotecGs|||j|f|Srv)rrrr=r=r>rszIMAP4._simple_commandcCs8|dkr||fS||jkr$|dgfS|j|}||fS)Nr )rHr%)rTrrrrr=r=r>rs    zIMAP4._untagged_response)rr)rr)rF)N)F)Tr:r;r<__doc__rPr?r@rA IMAP4_PORTrWrMrdrOrurwrzr~rNrrrrQr}rrrrrrrrrrrrrrrrrrrrrrrxrrrrrrrrrrrrrrrrrrrrrrr rrrkrjrrrrrrr_mesgZ_dump_urZ_logZ print_logr=r=r=r>rs,*                    M P ! $c@s:eZdZdZdedddfddZddZdefdd 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)certfilekeyfile) ValueErrorwarningswarnDeprecationWarningr3r2rrrrrW)rTrUrVr3r2rr5r=r=r>rWs$zIMAP4_SSL.__init__cCst|}|jj||jdS)Nr)rr~rrrU)rTrr=r=r>r~s zIMAP4_SSL._create_socketcCst|||dS)zSetup 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<r-IMAP4_SSL_PORTrWr~rNr=r=r=r>r0s r0c@sBeZdZdZddZdddZddZd d Zd d Zd dZ dS)rzIMAP4 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|dSrv)rrrW)rTrr=r=r>rW/szIMAP4_stream.__init__NcCsNd|_d|_d|_d|_tj|jttjtjddd|_ |j j |_ |j j |_ dS)zSetup a stream connection. This connection will be used by the routines: read, readline, send, shutdown. NT)bufsizestdinstdoutshellZ close_fds)rUrVrr subprocessPopenrrPIPErr: writefiler;readfilerSr=r=r>rN4s zIMAP4_stream.opencCs |j|Sr)rArrr=r=r>rDszIMAP4_stream.readcCs |jS)r)rArrbr=r=r>rIszIMAP4_stream.readlinecCs|j||jdS)rN)r@writeflushrr=r=r>rNs zIMAP4_stream.sendcCs"|j|j|jdS)rN)rArr@rwaitrbr=r=r>rQTs  zIMAP4_stream.shutdown)NN) r:r;r<r-rWrNrrrrQr=r=r=r>r#s  c@s0eZdZdZddZddZddZdd Zd S) rzcPrivate class to provide en/decoding for base64-based authentication conversation. cCs ||_dSrv)r)rTZmechinstr=r=r>rWbsz_Authenticator.__init__cCs&|||}|dkrdS||S)N*)rrr)rTrZretr=r=r>resz_Authenticator.processcCsnd}t|tr|d}|rjt|dkrB|dd}|dd}n|}d}t|}|r||dd}q|S)Nrrc0r)rrrrbinasciiZ b2a_base64)rTinpZoupter=r=r>rks      z_Authenticator.encodecCs|sdSt|Sr)rGZ a2b_base64)rTrHr=r=r>rsz_Authenticator.decodeN)r:r;r<r-rWrrrr=r=r=r>r\s rz0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecrcCsi|]\}}||dqS)r)r).0nr*r=r=r> srMrc Cst|}|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)zParse 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. Nmonzonendayyearhourminseczonehzonem<-r) InternalDater)Mon2numrr"calendarZtimegmtime localtime)r$rrNrOrPrQrRrSrTrUrVZzoneZttutcr=r=r>rs$  cCs@d}d}tt|}|rr s cCs$t|}|sdSt|dS)z-Convert IMAP4 flags response to python tuple.r=r)Flagsr)rrr)r$rr=r=r>r s cCst|ttfr"t|tj}nt|trz |j }WnZt k rt j r|d}|dkrpt t |d}t jt jf| }nt j }YnXt|d}t|dddt|i}nLt|tr|jdkrtd|}n*t|tr|d|dfd kr|Std d t|j}||S) aConvert 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)ZsecondsNtzinfozdate_time must be awarer)r,r,zdate_time not of a known typez"%d-{}-%Y %H:%M:%S %z")rr"floatrZ fromtimestamprr^Z astimezoner tm_gmtoffrsr\daylightr]mktimealtzonerrfr4rformatMonthsZmonthstrftime)rZdtZgmtoffZdstZdeltaZfmtr=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 )rZlfr)r) /tmp/xxx 1)r)rqz/tmp/yyy)r /tmp/yyz 2rrs)r)z/tmpzyy*)rrr)r)NZSUBJECTZtest)r)1z(FLAGS INTERNALDATE RFC822))r)rtFLAGSz (\Deleted))rr=)rr=)rr=)rr=)rr=)r)Z UIDVALIDITY)r)r,ZALL)r)r)rxr=cCsLtd||ftt||\}}td|||f|dkrH|d|S)Nz%s %sz %s => %s %sr r)Mr/rq)cmdryrrr=r=r>run's rxrzPROTOCOL_VERSION = %szCAPABILITIES = %rr)z/tmp/zyy%z .*"([^"]+)"$rrrrz%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-rirGrrfr[r}r=r{r\r[rrriorrr ImportError__all__rrCr.r8rlrrpr\r!rcrYr^r_rr#r rar]r`rr0rrrrrm enumeraterZrr r r r:ZgetoptZgetpassargvZoptlistryr?rZstream_commandoptr"rUZgetuserZUSERZPASSWDZ test_mesgZ test_seq1Z test_seq2rxrvrEr/rnrmrwZmlr)rrpathrrprintr=r=r=r>sZH   /     l4 9, #  )