<s> """<STR_LIT>""" <EOL> __all__ = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> __version__ = "<STR_LIT>" <EOL> __release__ = "<STR_LIT:3>" <EOL> __revision__ = "<STR_LIT>" <EOL> __credits__ = """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __URL__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> import binascii , calendar , errno , os , queue , random , re , select , socket , sys , time , threading , zlib <EOL> select_module = select <EOL> CRLF = b'<STR_LIT:\r\n>' <EOL> IMAP4_PORT = <NUM_LIT> <EOL> IMAP4_SSL_PORT = <NUM_LIT> <EOL> IDLE_TIMEOUT_RESPONSE = b'<STR_LIT>' <EOL> IDLE_TIMEOUT = <NUM_LIT> * <NUM_LIT> <EOL> READ_POLL_TIMEOUT = <NUM_LIT:30> <EOL> READ_SIZE = <NUM_LIT> <EOL> DFLT_DEBUG_BUF_LVL = <NUM_LIT:3> <EOL> TLS_SECURE = "<STR_LIT>" <EOL> TLS_NO_SSL = "<STR_LIT>" <EOL> TLS_COMPAT = "<STR_LIT>" <EOL> AllowedVersions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> CMD_VAL_STATES = <NUM_LIT:0> <EOL> CMD_VAL_ASYNC = <NUM_LIT:1> <EOL> NONAUTH , AUTH , SELECTED , LOGOUT = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> Commands = { <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , False ) , <EOL> '<STR_LIT>' : ( ( NONAUTH , ) , False ) , <EOL> '<STR_LIT>' : ( ( NONAUTH , AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , False ) , <EOL> '<STR_LIT>' : ( ( AUTH , ) , False ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , False ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( NONAUTH , AUTH , LOGOUT , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , False ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( NONAUTH , ) , False ) , <EOL> '<STR_LIT>' : ( ( NONAUTH , AUTH , LOGOUT , SELECTED ) , False ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( NONAUTH , AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , ) , False ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , False ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , False ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , False ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( NONAUTH , ) , False ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , True ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , False ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( SELECTED , ) , True ) , <EOL> '<STR_LIT>' : ( ( AUTH , SELECTED ) , False ) , <EOL> } <EOL> UID_direct = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def Int2AP ( num ) : <EOL> """<STR_LIT>""" <EOL> val = b'<STR_LIT>' ; AP = b'<STR_LIT>' <EOL> num = int ( abs ( num ) ) <EOL> while num : <EOL> num , mod = divmod ( num , <NUM_LIT:16> ) <EOL> val = AP [ mod : mod + <NUM_LIT:1> ] + val <EOL> return val <EOL> class Request ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , name = None , callback = None , cb_arg = None , cb_self = False ) : <EOL> self . parent = parent <EOL> self . name = name <EOL> self . callback = callback <EOL> if not cb_self : <EOL> self . callback_arg = cb_arg <EOL> else : <EOL> self . callback_arg = ( self , cb_arg ) <EOL> self . tag = parent . tagpre + bytes ( str ( parent . tagnum ) , '<STR_LIT>' ) <EOL> parent . tagnum += <NUM_LIT:1> <EOL> self . ready = threading . Event ( ) <EOL> self . response = None <EOL> self . aborted = None <EOL> self . data = None <EOL> def abort ( self , typ , val ) : <EOL> self . aborted = ( typ , val ) <EOL> self . deliver ( None ) <EOL> def get_response ( self , exc_fmt = None ) : <EOL> self . callback = None <EOL> if __debug__ : self . parent . _log ( <NUM_LIT:3> , '<STR_LIT>' % ( self . name , self . tag ) ) <EOL> self . ready . wait ( threading . TIMEOUT_MAX ) <EOL> if self . aborted is not None : <EOL> typ , val = self . aborted <EOL> if exc_fmt is None : <EOL> exc_fmt = '<STR_LIT>' % typ <EOL> raise typ ( exc_fmt % str ( val ) ) <EOL> return self . response <EOL> def deliver ( self , response ) : <EOL> if self . callback is not None : <EOL> self . callback ( ( response , self . callback_arg , self . aborted ) ) <EOL> return <EOL> self . response = response <EOL> self . ready . set ( ) <EOL> if __debug__ : self . parent . _log ( <NUM_LIT:3> , '<STR_LIT>' % ( self . name , self . tag ) ) <EOL> class IMAP4 ( object ) : <EOL> """<STR_LIT>""" <EOL> class error ( Exception ) : pass <EOL> class abort ( error ) : pass <EOL> class readonly ( abort ) : pass <EOL> continuation_cre = re . compile ( br'<STR_LIT>' ) <EOL> literal_cre = re . compile ( br'<STR_LIT>' ) <EOL> mapCRLF_cre = re . compile ( br'<STR_LIT>' ) <EOL> response_code_cre = re . compile ( br'<STR_LIT>' ) <EOL> untagged_response_cre = re . compile ( br'<STR_LIT>' ) <EOL> untagged_status_cre = re . compile ( br'<STR_LIT>' ) <EOL> def __init__ ( self , host = None , port = None , debug = None , debug_file = None , identifier = None , timeout = None , debug_buf_lvl = None ) : <EOL> self . state = NONAUTH <EOL> self . literal = None <EOL> self . tagged_commands = { } <EOL> self . untagged_responses = [ ] <EOL> self . mailbox = None <EOL> self . is_readonly = False <EOL> self . idle_rqb = None <EOL> self . idle_timeout = None <EOL> self . _expecting_data = False <EOL> self . _expecting_data_len = <NUM_LIT:0> <EOL> self . _accumulated_data = [ ] <EOL> self . _literal_expected = None <EOL> self . compressor = None <EOL> self . decompressor = None <EOL> self . _tls_established = False <EOL> self . tagnum = <NUM_LIT:0> <EOL> self . tagpre = Int2AP ( random . randint ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . tagre = re . compile ( br'<STR_LIT>' <EOL> + self . tagpre <EOL> + br'<STR_LIT>' , re . ASCII ) <EOL> if __debug__ : self . _init_debug ( debug , debug_file , debug_buf_lvl ) <EOL> self . resp_timeout = timeout <EOL> if timeout is not None and timeout < READ_POLL_TIMEOUT : <EOL> self . read_poll_timeout = timeout <EOL> else : <EOL> self . read_poll_timeout = READ_POLL_TIMEOUT <EOL> self . read_size = READ_SIZE <EOL> self . open ( host , port ) <EOL> if __debug__ : <EOL> if debug : <EOL> self . _mesg ( '<STR_LIT>' % ( self . host , self . port ) ) <EOL> if identifier is not None : <EOL> self . identifier = identifier <EOL> else : <EOL> self . identifier = self . host <EOL> if self . identifier : <EOL> self . identifier += '<STR_LIT:U+0020>' <EOL> self . Terminate = self . TerminateReader = False <EOL> self . state_change_free = threading . Event ( ) <EOL> self . state_change_pending = threading . Lock ( ) <EOL> self . commands_lock = threading . Lock ( ) <EOL> self . idle_lock = threading . Lock ( ) <EOL> self . ouq = queue . Queue ( <NUM_LIT:10> ) <EOL> self . inq = queue . Queue ( ) <EOL> self . wrth = threading . Thread ( target = self . _writer ) <EOL> self . wrth . setDaemon ( True ) <EOL> self . wrth . start ( ) <EOL> self . rdth = threading . Thread ( target = self . _reader ) <EOL> self . rdth . setDaemon ( True ) <EOL> self . rdth . start ( ) <EOL> self . inth = threading . Thread ( target = self . _handler ) <EOL> self . inth . setDaemon ( True ) <EOL> self . inth . start ( ) <EOL> try : <EOL> self . welcome = self . _request_push ( name = '<STR_LIT>' , tag = '<STR_LIT>' ) . get_response ( '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> if self . _get_untagged_response ( '<STR_LIT>' ) : <EOL> self . state = AUTH <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> elif self . _get_untagged_response ( '<STR_LIT:OK>' ) : <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> else : <EOL> raise self . error ( '<STR_LIT>' % repr ( self . welcome ) ) <EOL> self . _get_capabilities ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' % ( self . capabilities , ) ) <EOL> for version in AllowedVersions : <EOL> if not version in self . capabilities : <EOL> continue <EOL> self . PROTOCOL_VERSION = version <EOL> break <EOL> else : <EOL> raise self . error ( '<STR_LIT>' ) <EOL> except : <EOL> self . _close_threads ( ) <EOL> raise <EOL> def __getattr__ ( self , attr ) : <EOL> if attr in Commands : <EOL> return getattr ( self , attr . lower ( ) ) <EOL> raise AttributeError ( "<STR_LIT>" % attr ) <EOL> def open ( self , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> self . host = self . _choose_nonull_or_dflt ( '<STR_LIT>' , host ) <EOL> self . port = self . _choose_nonull_or_dflt ( IMAP4_PORT , port ) <EOL> self . sock = self . open_socket ( ) <EOL> self . read_fd = self . sock . fileno ( ) <EOL> def open_socket ( self ) : <EOL> """<STR_LIT>""" <EOL> return socket . create_connection ( ( self . host , self . port ) ) <EOL> def ssl_wrap_socket ( self ) : <EOL> try : <EOL> import ssl <EOL> TLS_MAP = { } <EOL> if hasattr ( ssl , "<STR_LIT>" ) : <EOL> TLS_MAP [ TLS_SECURE ] = { <EOL> "<STR_LIT>" : ssl . PROTOCOL_TLSv1_2 , <EOL> "<STR_LIT>" : ssl . PROTOCOL_TLSv1_1 , <EOL> } <EOL> else : <EOL> TLS_MAP [ TLS_SECURE ] = { } <EOL> TLS_MAP [ TLS_NO_SSL ] = TLS_MAP [ TLS_SECURE ] . copy ( ) <EOL> TLS_MAP [ TLS_NO_SSL ] . update ( { <EOL> "<STR_LIT>" : ssl . PROTOCOL_TLSv1 , <EOL> } ) <EOL> TLS_MAP [ TLS_COMPAT ] = TLS_MAP [ TLS_NO_SSL ] . copy ( ) <EOL> TLS_MAP [ TLS_COMPAT ] . update ( { <EOL> "<STR_LIT>" : ssl . PROTOCOL_SSLv23 , <EOL> None : ssl . PROTOCOL_SSLv23 , <EOL> } ) <EOL> if hasattr ( ssl , "<STR_LIT>" ) : <EOL> TLS_MAP [ TLS_COMPAT ] . update ( { <EOL> "<STR_LIT>" : ssl . PROTOCOL_SSLv3 <EOL> } ) <EOL> if self . ca_certs is not None : <EOL> cert_reqs = ssl . CERT_REQUIRED <EOL> else : <EOL> cert_reqs = ssl . CERT_NONE <EOL> if self . tls_level not in TLS_MAP : <EOL> raise RuntimeError ( "<STR_LIT>" % self . tls_level ) <EOL> if self . ssl_version not in TLS_MAP [ self . tls_level ] : <EOL> raise socket . sslerror ( "<STR_LIT>" % ( self . ssl_version , self . tls_level ) ) <EOL> ssl_version = TLS_MAP [ self . tls_level ] [ self . ssl_version ] <EOL> self . sock = ssl . wrap_socket ( self . sock , self . keyfile , self . certfile , ca_certs = self . ca_certs , cert_reqs = cert_reqs , ssl_version = ssl_version ) <EOL> ssl_exc = ssl . SSLError <EOL> self . read_fd = self . sock . fileno ( ) <EOL> except ImportError : <EOL> raise socket . sslerror ( "<STR_LIT>" ) <EOL> if self . cert_verify_cb is not None : <EOL> cert_err = self . cert_verify_cb ( self . sock . getpeercert ( ) , self . host ) <EOL> if cert_err : <EOL> raise ssl_exc ( cert_err ) <EOL> self . sock . setsockopt ( socket . SOL_SOCKET , socket . SO_KEEPALIVE , <NUM_LIT:1> ) <EOL> def start_compressing ( self ) : <EOL> """<STR_LIT>""" <EOL> self . decompressor = zlib . decompressobj ( - <NUM_LIT:15> ) <EOL> self . compressor = zlib . compressobj ( zlib . Z_DEFAULT_COMPRESSION , zlib . DEFLATED , - <NUM_LIT:15> ) <EOL> def read ( self , size ) : <EOL> """<STR_LIT>""" <EOL> if self . decompressor is None : <EOL> return self . sock . recv ( size ) <EOL> if self . decompressor . unconsumed_tail : <EOL> data = self . decompressor . unconsumed_tail <EOL> else : <EOL> data = self . sock . recv ( READ_SIZE ) <EOL> return self . decompressor . decompress ( data , size ) <EOL> def send ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . compressor is not None : <EOL> data = self . compressor . compress ( data ) <EOL> data += self . compressor . flush ( zlib . Z_SYNC_FLUSH ) <EOL> self . sock . sendall ( data ) <EOL> def shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . sock . shutdown ( socket . SHUT_RDWR ) <EOL> except OSError as e : <EOL> if e . errno != errno . ENOTCONN : <EOL> raise <EOL> finally : <EOL> self . sock . close ( ) <EOL> def socket ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sock <EOL> def enable_compression ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if typ == '<STR_LIT:OK>' : <EOL> self . start_compressing ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def pop_untagged_responses ( self ) : <EOL> """<STR_LIT>""" <EOL> while self . untagged_responses : <EOL> self . commands_lock . acquire ( ) <EOL> try : <EOL> yield self . untagged_responses . pop ( <NUM_LIT:0> ) <EOL> finally : <EOL> self . commands_lock . release ( ) <EOL> def recent ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _untagged_response ( None , [ None ] , name ) <EOL> if dat != [ None ] : <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . noop ( ** kw ) <EOL> def response ( self , code , ** kw ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _untagged_response ( code , [ None ] , code . upper ( ) ) <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> def append ( self , mailbox , flags , date_time , message , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> if not mailbox : <EOL> mailbox = '<STR_LIT>' <EOL> if flags : <EOL> if ( flags [ <NUM_LIT:0> ] , flags [ - <NUM_LIT:1> ] ) != ( '<STR_LIT:(>' , '<STR_LIT:)>' ) : <EOL> flags = '<STR_LIT>' % flags <EOL> else : <EOL> flags = None <EOL> if date_time : <EOL> date_time = Time2Internaldate ( date_time ) <EOL> else : <EOL> date_time = None <EOL> if isinstance ( message , str ) : <EOL> message = bytes ( message , '<STR_LIT>' ) <EOL> self . literal = self . mapCRLF_cre . sub ( CRLF , message ) <EOL> try : <EOL> return self . _simple_command ( name , mailbox , flags , date_time , ** kw ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def authenticate ( self , mechanism , authobject , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . literal = _Authenticator ( authobject ) . process <EOL> try : <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , mechanism . upper ( ) ) <EOL> if typ != '<STR_LIT:OK>' : <EOL> self . _deliver_exc ( self . error , dat [ - <NUM_LIT:1> ] , kw ) <EOL> self . state = AUTH <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> def capability ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , ** kw ) <EOL> def check ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , ** kw ) <EOL> def close ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if self . state != '<STR_LIT>' : <EOL> raise self . error ( '<STR_LIT>' ) <EOL> try : <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' ) <EOL> finally : <EOL> self . state = AUTH <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> self . _release_state_change ( ) <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> def copy ( self , message_set , new_mailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , message_set , new_mailbox , ** kw ) <EOL> def create ( self , mailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , ** kw ) <EOL> def delete ( self , mailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , ** kw ) <EOL> def deleteacl ( self , mailbox , who , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , who , ** kw ) <EOL> def examine ( self , mailbox = '<STR_LIT>' , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . select ( mailbox = mailbox , readonly = True , ** kw ) <EOL> def expunge ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , ** kw ) <EOL> def fetch ( self , message_set , message_parts , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , message_set , message_parts , ** kw ) <EOL> def getacl ( self , mailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , ** kw ) <EOL> def getannotation ( self , mailbox , entry , attribute , ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , entry , attribute , ** kw ) <EOL> def getquota ( self , root , ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . _simple_command ( '<STR_LIT>' , root , ** kw ) <EOL> def getquotaroot ( self , mailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , mailbox ) <EOL> typ , quota = self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> typ , quotaroot = self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> return self . _deliver_dat ( typ , [ quotaroot , quota ] , kw ) <EOL> def id ( self , * kv_pairs , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> if not kv_pairs : <EOL> data = '<STR_LIT>' <EOL> elif len ( kv_pairs ) == <NUM_LIT:1> : <EOL> data = kv_pairs [ <NUM_LIT:0> ] <EOL> else : <EOL> data = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( [ ( arg and self . _quote ( arg ) or '<STR_LIT>' ) for arg in kv_pairs ] ) <EOL> return self . _simple_command ( name , data , ** kw ) <EOL> def idle ( self , timeout = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> self . literal = _IdleCont ( self , timeout ) . process <EOL> try : <EOL> return self . _simple_command ( name , ** kw ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def list ( self , directory = '<STR_LIT>' , pattern = '<STR_LIT:*>' , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , directory , pattern , ** kw ) <EOL> def login ( self , user , password , ** kw ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , user , self . _quote ( password ) ) <EOL> if typ != '<STR_LIT:OK>' : <EOL> self . _deliver_exc ( self . error , dat [ - <NUM_LIT:1> ] , kw ) <EOL> self . state = AUTH <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> def login_cram_md5 ( self , user , password , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . user , self . password = user , password <EOL> return self . authenticate ( '<STR_LIT>' , self . _CRAM_MD5_AUTH , ** kw ) <EOL> def _CRAM_MD5_AUTH ( self , challenge ) : <EOL> """<STR_LIT>""" <EOL> import hmac <EOL> pwd = ( self . password . encode ( '<STR_LIT>' ) if isinstance ( self . password , str ) <EOL> else self . password ) <EOL> return self . user + "<STR_LIT:U+0020>" + hmac . HMAC ( pwd , challenge , '<STR_LIT>' ) . hexdigest ( ) <EOL> def logout ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . state = LOGOUT <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> try : <EOL> try : <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' ) <EOL> except : <EOL> typ , dat = '<STR_LIT>' , [ '<STR_LIT>' % sys . exc_info ( ) [ : <NUM_LIT:2> ] ] <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , dat ) <EOL> self . _close_threads ( ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> bye = self . _get_untagged_response ( '<STR_LIT>' , leave = True ) <EOL> if bye : <EOL> typ , dat = '<STR_LIT>' , bye <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> def lsub ( self , directory = '<STR_LIT>' , pattern = '<STR_LIT:*>' , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , directory , pattern , ** kw ) <EOL> def myrights ( self , mailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , mailbox , ** kw ) <EOL> def namespace ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , ** kw ) <EOL> def noop ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if __debug__ : self . _dump_ur ( <NUM_LIT:3> ) <EOL> return self . _simple_command ( '<STR_LIT>' , ** kw ) <EOL> def partial ( self , message_num , message_part , start , length , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . _simple_command ( name , message_num , message_part , start , length , ** kw ) <EOL> def proxyauth ( self , user , ** kw ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _simple_command ( '<STR_LIT>' , user , ** kw ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def rename ( self , oldmailbox , newmailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , oldmailbox , newmailbox , ** kw ) <EOL> def search ( self , charset , * criteria , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> if charset : <EOL> return self . _simple_command ( name , '<STR_LIT>' , charset , * criteria , ** kw ) <EOL> return self . _simple_command ( name , * criteria , ** kw ) <EOL> def select ( self , mailbox = '<STR_LIT>' , readonly = False , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . mailbox = mailbox <EOL> self . is_readonly = bool ( readonly ) <EOL> if readonly : <EOL> name = '<STR_LIT>' <EOL> else : <EOL> name = '<STR_LIT>' <EOL> try : <EOL> rqb = self . _command ( name , mailbox ) <EOL> typ , dat = rqb . get_response ( '<STR_LIT>' % rqb . name ) <EOL> if typ != '<STR_LIT:OK>' : <EOL> if self . state == SELECTED : <EOL> self . state = AUTH <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> if typ == '<STR_LIT>' : <EOL> self . _deliver_exc ( self . error , '<STR_LIT>' % ( name , typ , dat , mailbox ) , kw ) <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> self . state = SELECTED <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> if self . _get_untagged_response ( '<STR_LIT>' , leave = True ) and not readonly : <EOL> if __debug__ : self . _dump_ur ( <NUM_LIT:1> ) <EOL> self . _deliver_exc ( self . readonly , '<STR_LIT>' % mailbox , kw ) <EOL> typ , dat = self . _untagged_response ( typ , [ None ] , '<STR_LIT>' ) <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> def setacl ( self , mailbox , who , what , ** kw ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , who , what , ** kw ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def setannotation ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . _simple_command ( '<STR_LIT>' , * args , ** kw ) <EOL> def setquota ( self , root , limits , ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> return self . _simple_command ( '<STR_LIT>' , root , limits , ** kw ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def sort ( self , sort_criteria , charset , * search_criteria , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> if ( sort_criteria [ <NUM_LIT:0> ] , sort_criteria [ - <NUM_LIT:1> ] ) != ( '<STR_LIT:(>' , '<STR_LIT:)>' ) : <EOL> sort_criteria = '<STR_LIT>' % sort_criteria <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , sort_criteria , charset , * search_criteria , ** kw ) <EOL> def starttls ( self , keyfile = None , certfile = None , ca_certs = None , cert_verify_cb = None , ssl_version = "<STR_LIT>" , tls_level = TLS_COMPAT , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> if name not in self . capabilities : <EOL> raise self . abort ( '<STR_LIT>' ) <EOL> if self . _tls_established : <EOL> raise self . abort ( '<STR_LIT>' ) <EOL> self . read_size = <NUM_LIT:1> <EOL> self . TerminateReader = True <EOL> try : <EOL> typ , dat = self . _simple_command ( name ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> self . rdth . join ( ) <EOL> self . TerminateReader = False <EOL> self . read_size = READ_SIZE <EOL> if typ != '<STR_LIT:OK>' : <EOL> self . rdth = threading . Thread ( target = self . _reader ) <EOL> self . rdth . setDaemon ( True ) <EOL> self . rdth . start ( ) <EOL> raise self . error ( "<STR_LIT>" % dat ) <EOL> self . keyfile = keyfile <EOL> self . certfile = certfile <EOL> self . ca_certs = ca_certs <EOL> self . cert_verify_cb = cert_verify_cb <EOL> self . ssl_version = ssl_version <EOL> self . tls_level = tls_level <EOL> try : <EOL> self . ssl_wrap_socket ( ) <EOL> finally : <EOL> self . rdth = threading . Thread ( target = self . _reader ) <EOL> self . rdth . setDaemon ( True ) <EOL> self . rdth . start ( ) <EOL> self . _get_capabilities ( ) <EOL> self . _tls_established = True <EOL> typ , dat = self . _untagged_response ( typ , dat , name ) <EOL> return self . _deliver_dat ( typ , dat , kw ) <EOL> def status ( self , mailbox , names , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , mailbox , names , ** kw ) <EOL> def store ( self , message_set , command , flags , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if ( flags [ <NUM_LIT:0> ] , flags [ - <NUM_LIT:1> ] ) != ( '<STR_LIT:(>' , '<STR_LIT:)>' ) : <EOL> flags = '<STR_LIT>' % flags <EOL> kw [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . _simple_command ( '<STR_LIT>' , message_set , command , flags , ** kw ) <EOL> def subscribe ( self , mailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , ** kw ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def thread ( self , threading_algorithm , charset , * search_criteria , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = name <EOL> return self . _simple_command ( name , threading_algorithm , charset , * search_criteria , ** kw ) <EOL> def uid ( self , command , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> command = command . upper ( ) <EOL> if command in UID_direct : <EOL> resp = command <EOL> else : <EOL> resp = '<STR_LIT>' <EOL> kw [ '<STR_LIT>' ] = resp <EOL> return self . _simple_command ( '<STR_LIT>' , command , * args , ** kw ) <EOL> def unsubscribe ( self , mailbox , ** kw ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , ** kw ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def xatom ( self , name , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> name = name . upper ( ) <EOL> if not name in Commands : <EOL> Commands [ name ] = ( ( self . state , ) , False ) <EOL> try : <EOL> return self . _simple_command ( name , * args , ** kw ) <EOL> finally : <EOL> self . _release_state_change ( ) <EOL> def _append_untagged ( self , typ , dat ) : <EOL> if dat is None : dat = b'<STR_LIT>' <EOL> self . commands_lock . acquire ( ) <EOL> if self . untagged_responses : <EOL> urn , urd = self . untagged_responses [ - <NUM_LIT:1> ] <EOL> if urn != typ : <EOL> urd = None <EOL> else : <EOL> urd = None <EOL> if urd is None : <EOL> urd = [ ] <EOL> self . untagged_responses . append ( [ typ , urd ] ) <EOL> urd . append ( dat ) <EOL> self . commands_lock . release ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:5> , '<STR_LIT>' % ( typ , len ( urd ) - <NUM_LIT:1> , dat ) ) <EOL> def _check_bye ( self ) : <EOL> bye = self . _get_untagged_response ( '<STR_LIT>' , leave = True ) <EOL> if bye : <EOL> raise self . abort ( bye [ - <NUM_LIT:1> ] . decode ( '<STR_LIT>' , '<STR_LIT:replace>' ) ) <EOL> def _choose_nonull_or_dflt ( self , dflt , * args ) : <EOL> if isinstance ( dflt , str ) : <EOL> dflttyp = str <EOL> else : <EOL> dflttyp = type ( dflt ) <EOL> for arg in args : <EOL> if arg is not None : <EOL> if isinstance ( arg , dflttyp ) : <EOL> return arg <EOL> if __debug__ : self . _log ( <NUM_LIT:0> , '<STR_LIT>' % ( type ( arg ) , dflttyp ) ) <EOL> return dflt <EOL> def _command ( self , name , * args , ** kw ) : <EOL> if Commands [ name ] [ CMD_VAL_ASYNC ] : <EOL> cmdtyp = '<STR_LIT>' <EOL> else : <EOL> cmdtyp = '<STR_LIT>' <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' % ( cmdtyp , name , args ) ) <EOL> if __debug__ : self . _log ( <NUM_LIT:3> , '<STR_LIT>' ) <EOL> self . state_change_pending . acquire ( ) <EOL> self . _end_idle ( ) <EOL> if cmdtyp == '<STR_LIT>' : <EOL> self . state_change_pending . release ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:3> , '<STR_LIT>' ) <EOL> else : <EOL> self . _check_bye ( ) <EOL> self . commands_lock . acquire ( ) <EOL> if self . tagged_commands : <EOL> self . state_change_free . clear ( ) <EOL> need_event = True <EOL> else : <EOL> need_event = False <EOL> self . commands_lock . release ( ) <EOL> if need_event : <EOL> if __debug__ : self . _log ( <NUM_LIT:3> , '<STR_LIT>' % name ) <EOL> self . state_change_free . wait ( threading . TIMEOUT_MAX ) <EOL> if __debug__ : self . _log ( <NUM_LIT:3> , '<STR_LIT>' % name ) <EOL> if self . state not in Commands [ name ] [ CMD_VAL_STATES ] : <EOL> self . literal = None <EOL> raise self . error ( '<STR_LIT>' <EOL> % ( name , self . state ) ) <EOL> self . _check_bye ( ) <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . commands_lock . acquire ( ) <EOL> self . untagged_responses = [ ] <EOL> self . commands_lock . release ( ) <EOL> else : <EOL> for typ in ( '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> while self . _get_untagged_response ( typ ) : <EOL> continue <EOL> if not self . is_readonly and self . _get_untagged_response ( '<STR_LIT>' , leave = True ) : <EOL> self . literal = None <EOL> raise self . readonly ( '<STR_LIT>' ) <EOL> if self . Terminate : <EOL> raise self . abort ( '<STR_LIT>' ) <EOL> rqb = self . _request_push ( name = name , ** kw ) <EOL> name = bytes ( name , '<STR_LIT>' ) <EOL> data = rqb . tag + b'<STR_LIT:U+0020>' + name <EOL> for arg in args : <EOL> if arg is None : continue <EOL> if isinstance ( arg , str ) : <EOL> arg = bytes ( arg , "<STR_LIT>" ) <EOL> data = data + b'<STR_LIT:U+0020>' + arg <EOL> literal = self . literal <EOL> if literal is not None : <EOL> self . literal = None <EOL> if type ( literal ) is type ( self . _command ) : <EOL> literator = literal <EOL> else : <EOL> literator = None <EOL> data = data + bytes ( '<STR_LIT>' % len ( literal ) , '<STR_LIT>' ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % data ) <EOL> rqb . data = data + CRLF <EOL> if literal is None : <EOL> self . ouq . put ( rqb ) <EOL> return rqb <EOL> crqb = self . _request_push ( name = name , tag = '<STR_LIT>' ) <EOL> self . ouq . put ( rqb ) <EOL> while True : <EOL> ok , data = crqb . get_response ( '<STR_LIT>' % name ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % ( ok , data ) ) <EOL> if not ok : <EOL> break <EOL> if literator is not None : <EOL> literal = literator ( data , rqb ) <EOL> if literal is None : <EOL> break <EOL> if literator is not None : <EOL> crqb = self . _request_push ( name = name , tag = '<STR_LIT>' ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % len ( literal ) ) <EOL> crqb . data = literal + CRLF <EOL> self . ouq . put ( crqb ) <EOL> if literator is None : <EOL> break <EOL> return rqb <EOL> def _command_complete ( self , rqb , kw ) : <EOL> self . _check_bye ( ) <EOL> typ , dat = rqb . get_response ( '<STR_LIT>' % rqb . name ) <EOL> if typ == '<STR_LIT>' : <EOL> if __debug__ : self . _print_log ( ) <EOL> raise self . error ( '<STR_LIT>' % ( rqb . name , typ , dat , rqb . data ) ) <EOL> if '<STR_LIT>' in kw : <EOL> return self . _untagged_response ( typ , dat , kw [ '<STR_LIT>' ] ) <EOL> return typ , dat <EOL> def _command_completer ( self , cb_arg_list ) : <EOL> response , cb_arg , error = cb_arg_list <EOL> rqb , kw = cb_arg <EOL> rqb . callback = kw [ '<STR_LIT>' ] <EOL> rqb . callback_arg = kw . get ( '<STR_LIT>' ) <EOL> if error is not None : <EOL> if __debug__ : self . _print_log ( ) <EOL> typ , val = error <EOL> rqb . abort ( typ , val ) <EOL> return <EOL> bye = self . _get_untagged_response ( '<STR_LIT>' , leave = True ) <EOL> if bye : <EOL> rqb . abort ( self . abort , bye [ - <NUM_LIT:1> ] . decode ( '<STR_LIT>' , '<STR_LIT:replace>' ) ) <EOL> return <EOL> typ , dat = response <EOL> if typ == '<STR_LIT>' : <EOL> if __debug__ : self . _print_log ( ) <EOL> rqb . abort ( self . error , '<STR_LIT>' % ( rqb . name , typ , dat , rqb . data ) ) <EOL> return <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % ( response , cb_arg , rqb . tag ) ) <EOL> if '<STR_LIT>' in kw : <EOL> response = self . _untagged_response ( typ , dat , kw [ '<STR_LIT>' ] ) <EOL> rqb . deliver ( response ) <EOL> def _deliver_dat ( self , typ , dat , kw ) : <EOL> if '<STR_LIT>' in kw : <EOL> kw [ '<STR_LIT>' ] ( ( ( typ , dat ) , kw . get ( '<STR_LIT>' ) , None ) ) <EOL> return typ , dat <EOL> def _deliver_exc ( self , exc , dat , kw ) : <EOL> if '<STR_LIT>' in kw : <EOL> kw [ '<STR_LIT>' ] ( ( None , kw . get ( '<STR_LIT>' ) , ( exc , dat ) ) ) <EOL> raise exc ( dat ) <EOL> def _end_idle ( self ) : <EOL> self . idle_lock . acquire ( ) <EOL> irqb = self . idle_rqb <EOL> if irqb is None : <EOL> self . idle_lock . release ( ) <EOL> return <EOL> self . idle_rqb = None <EOL> self . idle_timeout = None <EOL> self . idle_lock . release ( ) <EOL> irqb . data = bytes ( '<STR_LIT>' , '<STR_LIT>' ) + CRLF <EOL> self . ouq . put ( irqb ) <EOL> if __debug__ : self . _log ( <NUM_LIT:2> , '<STR_LIT>' ) <EOL> def _get_capabilities ( self ) : <EOL> typ , dat = self . capability ( ) <EOL> if dat == [ None ] : <EOL> raise self . error ( '<STR_LIT>' ) <EOL> dat = str ( dat [ - <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> dat = dat . upper ( ) <EOL> self . capabilities = tuple ( dat . split ( ) ) <EOL> def _get_untagged_response ( self , name , leave = False ) : <EOL> self . commands_lock . acquire ( ) <EOL> for i , ( typ , dat ) in enumerate ( self . untagged_responses ) : <EOL> if typ == name : <EOL> if not leave : <EOL> del self . untagged_responses [ i ] <EOL> self . commands_lock . release ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:5> , '<STR_LIT>' % ( name , dat ) ) <EOL> return dat <EOL> self . commands_lock . release ( ) <EOL> return None <EOL> def _match ( self , cre , s ) : <EOL> self . mo = cre . match ( s ) <EOL> return self . mo is not None <EOL> def _put_response ( self , resp ) : <EOL> if self . _expecting_data : <EOL> rlen = len ( resp ) <EOL> dlen = min ( self . _expecting_data_len , rlen ) <EOL> if __debug__ : self . _log ( <NUM_LIT:5> , '<STR_LIT>' % ( self . _expecting_data_len , rlen ) ) <EOL> self . _expecting_data_len -= dlen <EOL> self . _expecting_data = ( self . _expecting_data_len != <NUM_LIT:0> ) <EOL> if rlen <= dlen : <EOL> self . _accumulated_data . append ( resp ) <EOL> return <EOL> self . _accumulated_data . append ( resp [ : dlen ] ) <EOL> resp = resp [ dlen : ] <EOL> if self . _accumulated_data : <EOL> typ , dat = self . _literal_expected <EOL> self . _append_untagged ( typ , ( dat , b'<STR_LIT>' . join ( self . _accumulated_data ) ) ) <EOL> self . _accumulated_data = [ ] <EOL> resp = resp [ : - <NUM_LIT:2> ] <EOL> if __debug__ : self . _log ( <NUM_LIT:5> , '<STR_LIT>' % resp ) <EOL> if '<STR_LIT>' in self . tagged_commands : <EOL> continuation_expected = True <EOL> else : <EOL> continuation_expected = False <EOL> if self . _literal_expected is not None : <EOL> dat = resp <EOL> if self . _match ( self . literal_cre , dat ) : <EOL> self . _literal_expected [ <NUM_LIT:1> ] = dat <EOL> self . _expecting_data = True <EOL> self . _expecting_data_len = int ( self . mo . group ( '<STR_LIT:size>' ) ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % self . _expecting_data_len ) <EOL> return <EOL> typ = self . _literal_expected [ <NUM_LIT:0> ] <EOL> self . _literal_expected = None <EOL> if dat : <EOL> self . _append_untagged ( typ , dat ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' ) <EOL> else : <EOL> if self . _match ( self . tagre , resp ) : <EOL> tag = self . mo . group ( '<STR_LIT>' ) <EOL> typ = str ( self . mo . group ( '<STR_LIT:type>' ) , '<STR_LIT>' ) <EOL> dat = self . mo . group ( '<STR_LIT:data>' ) <EOL> if typ in ( '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' ) and self . _match ( self . response_code_cre , dat ) : <EOL> self . _append_untagged ( str ( self . mo . group ( '<STR_LIT:type>' ) , '<STR_LIT>' ) , self . mo . group ( '<STR_LIT:data>' ) ) <EOL> if not tag in self . tagged_commands : <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' % resp ) <EOL> else : <EOL> self . _request_pop ( tag , ( typ , [ dat ] ) ) <EOL> else : <EOL> dat2 = None <EOL> if not self . _match ( self . untagged_response_cre , resp ) : <EOL> if self . _match ( self . untagged_status_cre , resp ) : <EOL> dat2 = self . mo . group ( '<STR_LIT>' ) <EOL> if self . mo is None : <EOL> if self . _match ( self . continuation_cre , resp ) : <EOL> if not continuation_expected : <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , "<STR_LIT>" % resp ) <EOL> return <EOL> self . _request_pop ( '<STR_LIT>' , ( True , self . mo . group ( '<STR_LIT:data>' ) ) ) <EOL> return <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , "<STR_LIT>" % resp ) <EOL> return <EOL> typ = str ( self . mo . group ( '<STR_LIT:type>' ) , '<STR_LIT>' ) <EOL> dat = self . mo . group ( '<STR_LIT:data>' ) <EOL> if dat is None : dat = b'<STR_LIT>' <EOL> if dat2 : dat = dat + b'<STR_LIT:U+0020>' + dat2 <EOL> if self . _match ( self . literal_cre , dat ) : <EOL> self . _expecting_data = True <EOL> self . _expecting_data_len = int ( self . mo . group ( '<STR_LIT:size>' ) ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % self . _expecting_data_len ) <EOL> self . _literal_expected = [ typ , dat ] <EOL> return <EOL> self . _append_untagged ( typ , dat ) <EOL> if typ in ( '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' ) and self . _match ( self . response_code_cre , dat ) : <EOL> self . _append_untagged ( str ( self . mo . group ( '<STR_LIT:type>' ) , '<STR_LIT>' ) , self . mo . group ( '<STR_LIT:data>' ) ) <EOL> if typ != '<STR_LIT:OK>' : <EOL> self . _end_idle ( ) <EOL> if continuation_expected : <EOL> self . _request_pop ( '<STR_LIT>' , ( False , resp ) ) <EOL> if typ in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if typ == '<STR_LIT>' : <EOL> self . Terminate = True <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' % ( typ , dat ) ) <EOL> def _quote ( self , arg ) : <EOL> return '<STR_LIT>' % arg . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> def _release_state_change ( self ) : <EOL> if self . state_change_pending . locked ( ) : <EOL> self . state_change_pending . release ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:3> , '<STR_LIT>' ) <EOL> def _request_pop ( self , name , data ) : <EOL> self . commands_lock . acquire ( ) <EOL> rqb = self . tagged_commands . pop ( name ) <EOL> if not self . tagged_commands : <EOL> need_event = True <EOL> else : <EOL> need_event = False <EOL> self . commands_lock . release ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % ( name , data , len ( self . tagged_commands ) , rqb . tag ) ) <EOL> rqb . deliver ( data ) <EOL> if need_event : <EOL> if __debug__ : self . _log ( <NUM_LIT:3> , '<STR_LIT>' ) <EOL> self . state_change_free . set ( ) <EOL> def _request_push ( self , tag = None , name = None , ** kw ) : <EOL> self . commands_lock . acquire ( ) <EOL> rqb = Request ( self , name = name , ** kw ) <EOL> if tag is None : <EOL> tag = rqb . tag <EOL> self . tagged_commands [ tag ] = rqb <EOL> self . commands_lock . release ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % ( tag , name , repr ( kw ) , rqb . tag ) ) <EOL> return rqb <EOL> def _simple_command ( self , name , * args , ** kw ) : <EOL> if '<STR_LIT>' in kw : <EOL> self . _command ( name , callback = self . _command_completer , cb_arg = kw , cb_self = True , * args ) <EOL> return ( None , None ) <EOL> return self . _command_complete ( self . _command ( name , * args ) , kw ) <EOL> def _untagged_response ( self , typ , dat , name ) : <EOL> if typ == '<STR_LIT>' : <EOL> return typ , dat <EOL> data = self . _get_untagged_response ( name ) <EOL> if not data : <EOL> return typ , [ None ] <EOL> while True : <EOL> dat = self . _get_untagged_response ( name ) <EOL> if not dat : <EOL> break <EOL> data += dat <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % ( typ , name , data ) ) <EOL> return typ , data <EOL> def _close_threads ( self ) : <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> self . ouq . put ( None ) <EOL> self . wrth . join ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> self . shutdown ( ) <EOL> self . rdth . join ( ) <EOL> self . inth . join ( ) <EOL> def _handler ( self ) : <EOL> resp_timeout = self . resp_timeout <EOL> threading . currentThread ( ) . setName ( self . identifier + '<STR_LIT>' ) <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> typ , val = self . abort , '<STR_LIT>' <EOL> while not self . Terminate : <EOL> self . idle_lock . acquire ( ) <EOL> if self . idle_timeout is not None : <EOL> timeout = self . idle_timeout - time . time ( ) <EOL> if timeout <= <NUM_LIT:0> : <EOL> timeout = <NUM_LIT:1> <EOL> if __debug__ : <EOL> if self . idle_rqb is not None : <EOL> self . _log ( <NUM_LIT:5> , '<STR_LIT>' % timeout ) <EOL> else : <EOL> timeout = resp_timeout <EOL> self . idle_lock . release ( ) <EOL> try : <EOL> line = self . inq . get ( True , timeout ) <EOL> except queue . Empty : <EOL> if self . idle_rqb is None : <EOL> if resp_timeout is not None and self . tagged_commands : <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> typ , val = self . abort , '<STR_LIT>' % resp_timeout <EOL> break <EOL> continue <EOL> if self . idle_timeout > time . time ( ) : <EOL> continue <EOL> if __debug__ : self . _log ( <NUM_LIT:2> , '<STR_LIT>' ) <EOL> line = IDLE_TIMEOUT_RESPONSE <EOL> if line is None : <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> break <EOL> if not isinstance ( line , bytes ) : <EOL> typ , val = line <EOL> break <EOL> try : <EOL> self . _put_response ( line ) <EOL> except : <EOL> typ , val = self . error , '<STR_LIT>' % sys . exc_info ( ) [ : <NUM_LIT:2> ] <EOL> break <EOL> self . Terminate = True <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' % repr ( val ) ) <EOL> while not self . ouq . empty ( ) : <EOL> try : <EOL> qel = self . ouq . get_nowait ( ) <EOL> if qel is not None : <EOL> qel . abort ( typ , val ) <EOL> except queue . Empty : <EOL> break <EOL> self . ouq . put ( None ) <EOL> self . commands_lock . acquire ( ) <EOL> for name in list ( self . tagged_commands . keys ( ) ) : <EOL> rqb = self . tagged_commands . pop ( name ) <EOL> rqb . abort ( typ , val ) <EOL> self . state_change_free . set ( ) <EOL> self . commands_lock . release ( ) <EOL> if __debug__ : self . _log ( <NUM_LIT:3> , '<STR_LIT>' ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> if hasattr ( select_module , "<STR_LIT>" ) : <EOL> def _reader ( self ) : <EOL> threading . currentThread ( ) . setName ( self . identifier + '<STR_LIT>' ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def poll_error ( state ) : <EOL> PollErrors = { <EOL> select . POLLERR : '<STR_LIT>' , <EOL> select . POLLHUP : '<STR_LIT>' , <EOL> select . POLLNVAL : '<STR_LIT>' , <EOL> } <EOL> return '<STR_LIT:U+0020>' . join ( [ PollErrors [ s ] for s in PollErrors . keys ( ) if ( s & state ) ] ) <EOL> line_part = b'<STR_LIT>' <EOL> poll = select . poll ( ) <EOL> poll . register ( self . read_fd , select . POLLIN ) <EOL> rxzero = <NUM_LIT:0> <EOL> terminate = False <EOL> read_poll_timeout = self . read_poll_timeout * <NUM_LIT:1000> <EOL> while not ( terminate or self . Terminate ) : <EOL> if self . state == LOGOUT : <EOL> timeout = <NUM_LIT:10> <EOL> else : <EOL> timeout = read_poll_timeout <EOL> try : <EOL> r = poll . poll ( timeout ) <EOL> if __debug__ : self . _log ( <NUM_LIT:5> , '<STR_LIT>' % repr ( r ) ) <EOL> if not r : <EOL> continue <EOL> fd , state = r [ <NUM_LIT:0> ] <EOL> if state & select . POLLIN : <EOL> data = self . read ( self . read_size ) <EOL> start = <NUM_LIT:0> <EOL> dlen = len ( data ) <EOL> if __debug__ : self . _log ( <NUM_LIT:5> , '<STR_LIT>' % dlen ) <EOL> if dlen == <NUM_LIT:0> : <EOL> rxzero += <NUM_LIT:1> <EOL> if rxzero > <NUM_LIT:5> : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> continue <EOL> rxzero = <NUM_LIT:0> <EOL> while True : <EOL> stop = data . find ( b'<STR_LIT:\n>' , start ) <EOL> if stop < <NUM_LIT:0> : <EOL> line_part += data [ start : ] <EOL> break <EOL> stop += <NUM_LIT:1> <EOL> line_part , start , line = b'<STR_LIT>' , stop , line_part + data [ start : stop ] <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % line ) <EOL> self . inq . put ( line ) <EOL> if self . TerminateReader : <EOL> terminate = True <EOL> if state & ~ ( select . POLLIN ) : <EOL> raise IOError ( poll_error ( state ) ) <EOL> except : <EOL> reason = '<STR_LIT>' % sys . exc_info ( ) [ : <NUM_LIT:2> ] <EOL> if __debug__ : <EOL> if not self . Terminate : <EOL> self . _print_log ( ) <EOL> if self . debug : self . debug += <NUM_LIT:4> <EOL> self . _log ( <NUM_LIT:1> , reason ) <EOL> self . inq . put ( ( self . abort , reason ) ) <EOL> break <EOL> poll . unregister ( self . read_fd ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> else : <EOL> def _reader ( self ) : <EOL> threading . currentThread ( ) . setName ( self . identifier + '<STR_LIT>' ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> line_part = b'<STR_LIT>' <EOL> rxzero = <NUM_LIT:0> <EOL> terminate = False <EOL> while not ( terminate or self . Terminate ) : <EOL> if self . state == LOGOUT : <EOL> timeout = <NUM_LIT:1> <EOL> else : <EOL> timeout = self . read_poll_timeout <EOL> try : <EOL> r , w , e = select . select ( [ self . read_fd ] , [ ] , [ ] , timeout ) <EOL> if __debug__ : self . _log ( <NUM_LIT:5> , '<STR_LIT>' % ( r , w , e ) ) <EOL> if not r : <EOL> continue <EOL> data = self . read ( self . read_size ) <EOL> start = <NUM_LIT:0> <EOL> dlen = len ( data ) <EOL> if __debug__ : self . _log ( <NUM_LIT:5> , '<STR_LIT>' % dlen ) <EOL> if dlen == <NUM_LIT:0> : <EOL> rxzero += <NUM_LIT:1> <EOL> if rxzero > <NUM_LIT:5> : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> continue <EOL> rxzero = <NUM_LIT:0> <EOL> while True : <EOL> stop = data . find ( b'<STR_LIT:\n>' , start ) <EOL> if stop < <NUM_LIT:0> : <EOL> line_part += data [ start : ] <EOL> break <EOL> stop += <NUM_LIT:1> <EOL> line_part , start , line = b'<STR_LIT>' , stop , ( line_part + data [ start : stop ] ) . decode ( errors = '<STR_LIT:ignore>' ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % line ) <EOL> self . inq . put ( line ) <EOL> if self . TerminateReader : <EOL> terminate = True <EOL> except : <EOL> reason = '<STR_LIT>' % sys . exc_info ( ) [ : <NUM_LIT:2> ] <EOL> if __debug__ : <EOL> if not self . Terminate : <EOL> self . _print_log ( ) <EOL> if self . debug : self . debug += <NUM_LIT:4> <EOL> self . _log ( <NUM_LIT:1> , reason ) <EOL> self . inq . put ( ( self . abort , reason ) ) <EOL> break <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def _writer ( self ) : <EOL> threading . currentThread ( ) . setName ( self . identifier + '<STR_LIT>' ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> reason = '<STR_LIT>' <EOL> while not self . Terminate : <EOL> rqb = self . ouq . get ( ) <EOL> if rqb is None : <EOL> break <EOL> try : <EOL> self . send ( rqb . data ) <EOL> if __debug__ : self . _log ( <NUM_LIT:4> , '<STR_LIT>' % rqb . data ) <EOL> except : <EOL> reason = '<STR_LIT>' % sys . exc_info ( ) [ : <NUM_LIT:2> ] <EOL> if __debug__ : <EOL> if not self . Terminate : <EOL> self . _print_log ( ) <EOL> if self . debug : self . debug += <NUM_LIT:4> <EOL> self . _log ( <NUM_LIT:1> , reason ) <EOL> rqb . abort ( self . abort , reason ) <EOL> break <EOL> self . inq . put ( ( self . abort , reason ) ) <EOL> if __debug__ : self . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> if __debug__ : <EOL> def _init_debug ( self , debug = None , debug_file = None , debug_buf_lvl = None ) : <EOL> self . debug_lock = threading . Lock ( ) <EOL> self . debug = self . _choose_nonull_or_dflt ( <NUM_LIT:0> , debug ) <EOL> self . debug_file = self . _choose_nonull_or_dflt ( sys . stderr , debug_file ) <EOL> self . debug_buf_lvl = self . _choose_nonull_or_dflt ( DFLT_DEBUG_BUF_LVL , debug_buf_lvl ) <EOL> self . _cmd_log_len = <NUM_LIT:20> <EOL> self . _cmd_log_idx = <NUM_LIT:0> <EOL> self . _cmd_log = { } <EOL> if self . debug : <EOL> self . _mesg ( '<STR_LIT>' % __version__ ) <EOL> self . _mesg ( '<STR_LIT>' % ( self . debug , self . debug_buf_lvl ) ) <EOL> def _dump_ur ( self , lvl ) : <EOL> if lvl > self . debug : <EOL> return <EOL> l = self . untagged_responses <EOL> if not l : <EOL> return <EOL> t = '<STR_LIT>' <EOL> l = [ '<STR_LIT>' % ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] and b'<STR_LIT>' . join ( x [ <NUM_LIT:1> ] ) or '<STR_LIT>' ) for x in l ] <EOL> self . debug_lock . acquire ( ) <EOL> self . _mesg ( '<STR_LIT>' % ( t , t . join ( l ) ) ) <EOL> self . debug_lock . release ( ) <EOL> def _log ( self , lvl , line ) : <EOL> if lvl > self . debug : <EOL> return <EOL> if line [ - <NUM_LIT:2> : ] == CRLF : <EOL> line = line [ : - <NUM_LIT:2> ] + '<STR_LIT>' <EOL> tn = threading . currentThread ( ) . getName ( ) <EOL> if lvl <= <NUM_LIT:1> or self . debug > self . debug_buf_lvl : <EOL> self . debug_lock . acquire ( ) <EOL> self . _mesg ( line , tn ) <EOL> self . debug_lock . release ( ) <EOL> if lvl != <NUM_LIT:1> : <EOL> return <EOL> self . debug_lock . acquire ( ) <EOL> self . _cmd_log [ self . _cmd_log_idx ] = ( line , tn , time . time ( ) ) <EOL> self . _cmd_log_idx += <NUM_LIT:1> <EOL> if self . _cmd_log_idx >= self . _cmd_log_len : <EOL> self . _cmd_log_idx = <NUM_LIT:0> <EOL> self . debug_lock . release ( ) <EOL> def _mesg ( self , s , tn = None , secs = None ) : <EOL> if secs is None : <EOL> secs = time . time ( ) <EOL> if tn is None : <EOL> tn = threading . currentThread ( ) . getName ( ) <EOL> tm = time . strftime ( '<STR_LIT>' , time . localtime ( secs ) ) <EOL> try : <EOL> self . debug_file . write ( '<STR_LIT>' % ( tm , ( secs * <NUM_LIT:100> ) % <NUM_LIT:100> , tn , s ) ) <EOL> self . debug_file . flush ( ) <EOL> finally : <EOL> pass <EOL> def _print_log ( self ) : <EOL> self . debug_lock . acquire ( ) <EOL> i , n = self . _cmd_log_idx , self . _cmd_log_len <EOL> if n : self . _mesg ( '<STR_LIT>' % n ) <EOL> while n : <EOL> try : <EOL> self . _mesg ( * self . _cmd_log [ i ] ) <EOL> except : <EOL> pass <EOL> i += <NUM_LIT:1> <EOL> if i >= self . _cmd_log_len : <EOL> i = <NUM_LIT:0> <EOL> n -= <NUM_LIT:1> <EOL> self . debug_lock . release ( ) <EOL> class IMAP4_SSL ( IMAP4 ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host = None , port = None , keyfile = None , certfile = None , ca_certs = None , cert_verify_cb = None , ssl_version = "<STR_LIT>" , debug = None , debug_file = None , identifier = None , timeout = None , debug_buf_lvl = None , tls_level = TLS_COMPAT ) : <EOL> self . keyfile = keyfile <EOL> self . certfile = certfile <EOL> self . ca_certs = ca_certs <EOL> self . cert_verify_cb = cert_verify_cb <EOL> self . ssl_version = ssl_version <EOL> self . tls_level = tls_level <EOL> IMAP4 . __init__ ( self , host , port , debug , debug_file , identifier , timeout , debug_buf_lvl ) <EOL> def open ( self , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> self . host = self . _choose_nonull_or_dflt ( '<STR_LIT>' , host ) <EOL> self . port = self . _choose_nonull_or_dflt ( IMAP4_SSL_PORT , port ) <EOL> self . sock = self . open_socket ( ) <EOL> self . ssl_wrap_socket ( ) <EOL> def read ( self , size ) : <EOL> """<STR_LIT>""" <EOL> if self . decompressor is None : <EOL> return self . sock . read ( size ) <EOL> if self . decompressor . unconsumed_tail : <EOL> data = self . decompressor . unconsumed_tail <EOL> else : <EOL> data = self . sock . read ( READ_SIZE ) <EOL> return self . decompressor . decompress ( data , size ) <EOL> def send ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . compressor is not None : <EOL> data = self . compressor . compress ( data ) <EOL> data += self . compressor . flush ( zlib . Z_SYNC_FLUSH ) <EOL> if hasattr ( self . sock , "<STR_LIT>" ) : <EOL> self . sock . sendall ( data ) <EOL> else : <EOL> dlen = len ( data ) <EOL> while dlen > <NUM_LIT:0> : <EOL> sent = self . sock . write ( data ) <EOL> if sent == dlen : <EOL> break <EOL> data = data [ sent : ] <EOL> dlen = dlen - sent <EOL> def ssl ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sock <EOL> class IMAP4_stream ( IMAP4 ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , command , debug = None , debug_file = None , identifier = None , timeout = None , debug_buf_lvl = None ) : <EOL> self . command = command <EOL> self . host = command <EOL> self . port = None <EOL> self . sock = None <EOL> self . writefile , self . readfile = None , None <EOL> self . read_fd = None <EOL> IMAP4 . __init__ ( self , None , None , debug , debug_file , identifier , timeout , debug_buf_lvl ) <EOL> def open ( self , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> from subprocess import Popen , PIPE <EOL> from io import DEFAULT_BUFFER_SIZE <EOL> if __debug__ : self . _log ( <NUM_LIT:0> , '<STR_LIT>' % self . command ) <EOL> self . _P = Popen ( self . command , shell = True , stdin = PIPE , stdout = PIPE , close_fds = True , bufsize = DEFAULT_BUFFER_SIZE ) <EOL> self . writefile , self . readfile = self . _P . stdin , self . _P . stdout <EOL> self . read_fd = self . readfile . fileno ( ) <EOL> def read ( self , size ) : <EOL> """<STR_LIT>""" <EOL> if self . decompressor is None : <EOL> return os . read ( self . read_fd , size ) <EOL> if self . decompressor . unconsumed_tail : <EOL> data = self . decompressor . unconsumed_tail <EOL> else : <EOL> data = os . read ( self . read_fd , READ_SIZE ) <EOL> return self . decompressor . decompress ( data , size ) <EOL> def send ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if self . compressor is not None : <EOL> data = self . compressor . compress ( data ) <EOL> data += self . compressor . flush ( zlib . Z_SYNC_FLUSH ) <EOL> self . writefile . write ( data ) <EOL> self . writefile . flush ( ) <EOL> def shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . readfile . close ( ) <EOL> self . writefile . close ( ) <EOL> self . _P . wait ( ) <EOL> class _Authenticator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mechinst ) : <EOL> self . mech = mechinst <EOL> def process ( self , data , rqb ) : <EOL> ret = self . mech ( self . decode ( data ) ) <EOL> if ret is None : <EOL> return b'<STR_LIT:*>' <EOL> return self . encode ( ret ) <EOL> def encode ( self , inp ) : <EOL> oup = b'<STR_LIT>' <EOL> if isinstance ( inp , str ) : <EOL> inp = inp . encode ( '<STR_LIT>' ) <EOL> while inp : <EOL> if len ( inp ) > <NUM_LIT> : <EOL> t = inp [ : <NUM_LIT> ] <EOL> inp = inp [ <NUM_LIT> : ] <EOL> else : <EOL> t = inp <EOL> inp = b'<STR_LIT>' <EOL> e = binascii . b2a_base64 ( t ) <EOL> if e : <EOL> oup = oup + e [ : - <NUM_LIT:1> ] <EOL> return oup <EOL> def decode ( self , inp ) : <EOL> if not inp : <EOL> return b'<STR_LIT>' <EOL> return binascii . a2b_base64 ( inp ) <EOL> class _IdleCont ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , timeout ) : <EOL> self . parent = parent <EOL> self . timeout = parent . _choose_nonull_or_dflt ( IDLE_TIMEOUT , timeout ) <EOL> self . parent . idle_timeout = self . timeout + time . time ( ) <EOL> def process ( self , data , rqb ) : <EOL> self . parent . idle_lock . acquire ( ) <EOL> self . parent . idle_rqb = rqb <EOL> self . parent . idle_timeout = self . timeout + time . time ( ) <EOL> self . parent . idle_lock . release ( ) <EOL> if __debug__ : self . parent . _log ( <NUM_LIT:2> , '<STR_LIT>' % self . timeout ) <EOL> return None <EOL> MonthNames = [ None , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> Mon2num = { s . encode ( ) : n + <NUM_LIT:1> for n , s in enumerate ( MonthNames [ <NUM_LIT:1> : ] ) } <EOL> InternalDate = re . compile ( br'<STR_LIT>' <EOL> br'<STR_LIT>' <EOL> br'<STR_LIT>' <EOL> br'<STR_LIT>' <EOL> br'<STR_LIT:">' ) <EOL> def Internaldate2Time ( resp ) : <EOL> """<STR_LIT>""" <EOL> mo = InternalDate . match ( resp ) <EOL> if not mo : <EOL> return None <EOL> mon = Mon2num [ mo . group ( '<STR_LIT>' ) ] <EOL> zonen = mo . group ( '<STR_LIT>' ) <EOL> day = int ( mo . group ( '<STR_LIT>' ) ) <EOL> year = int ( mo . group ( '<STR_LIT>' ) ) <EOL> hour = int ( mo . group ( '<STR_LIT>' ) ) <EOL> min = int ( mo . group ( '<STR_LIT>' ) ) <EOL> sec = int ( mo . group ( '<STR_LIT>' ) ) <EOL> zoneh = int ( mo . group ( '<STR_LIT>' ) ) <EOL> zonem = int ( mo . group ( '<STR_LIT>' ) ) <EOL> zone = ( zoneh * <NUM_LIT> + zonem ) * <NUM_LIT> <EOL> if zonen == b'<STR_LIT:->' : <EOL> zone = - zone <EOL> tt = ( year , mon , day , hour , min , sec , - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> return time . localtime ( calendar . timegm ( tt ) - zone ) <EOL> Internaldate2tuple = Internaldate2Time <EOL> def Time2Internaldate ( date_time ) : <EOL> """<STR_LIT>""" <EOL> from datetime import datetime , timezone , timedelta <EOL> if isinstance ( date_time , ( int , float ) ) : <EOL> tt = time . localtime ( date_time ) <EOL> elif isinstance ( date_time , tuple ) : <EOL> try : <EOL> gmtoff = date_time . tm_gmtoff <EOL> except AttributeError : <EOL> if time . daylight : <EOL> dst = date_time [ <NUM_LIT:8> ] <EOL> if dst == - <NUM_LIT:1> : <EOL> dst = time . localtime ( time . mktime ( date_time ) ) [ <NUM_LIT:8> ] <EOL> gmtoff = - ( time . timezone , time . altzone ) [ dst ] <EOL> else : <EOL> gmtoff = - time . timezone <EOL> delta = timedelta ( seconds = gmtoff ) <EOL> dt = datetime ( * date_time [ : <NUM_LIT:6> ] , tzinfo = timezone ( delta ) ) <EOL> elif isinstance ( date_time , datetime ) : <EOL> if date_time . tzinfo is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> dt = date_time <EOL> elif isinstance ( date_time , str ) and ( date_time [ <NUM_LIT:0> ] , date_time [ - <NUM_LIT:1> ] ) == ( '<STR_LIT:">' , '<STR_LIT:">' ) : <EOL> return date_time <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> fmt = '<STR_LIT>' . format ( MonthNames [ dt . month ] ) <EOL> return dt . strftime ( fmt ) <EOL> FLAGS_cre = re . compile ( br'<STR_LIT>' ) <EOL> def ParseFlags ( resp ) : <EOL> """<STR_LIT>""" <EOL> mo = FLAGS_cre . match ( resp ) <EOL> if not mo : <EOL> return ( ) <EOL> return tuple ( mo . group ( '<STR_LIT>' ) . split ( ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if not __debug__ : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> import getopt , getpass <EOL> try : <EOL> optlist , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' ) <EOL> except getopt . error as val : <EOL> optlist , args = ( ) , ( ) <EOL> debug , debug_buf_lvl , port , stream_command , keyfile , certfile , idle_intr = ( None , ) * <NUM_LIT:7> <EOL> for opt , val in optlist : <EOL> if opt == '<STR_LIT>' : <EOL> debug = int ( val ) <EOL> debug_buf_lvl = debug - <NUM_LIT:1> <EOL> elif opt == '<STR_LIT>' : <EOL> idle_intr = <NUM_LIT:1> <EOL> elif opt == '<STR_LIT>' : <EOL> try : <EOL> keyfile , certfile = val . split ( '<STR_LIT::>' ) <EOL> except ValueError : <EOL> keyfile , certfile = val , val <EOL> elif opt == '<STR_LIT>' : <EOL> port = int ( val ) <EOL> elif opt == '<STR_LIT>' : <EOL> stream_command = val <EOL> if not args : args = ( stream_command , ) <EOL> if not args : args = ( '<STR_LIT>' , ) <EOL> if not port : port = ( keyfile is not None ) and IMAP4_SSL_PORT or IMAP4_PORT <EOL> host = args [ <NUM_LIT:0> ] <EOL> USER = getpass . getuser ( ) <EOL> data = open ( os . path . exists ( "<STR_LIT>" ) and "<STR_LIT>" or __file__ ) . read ( <NUM_LIT:1000> ) <EOL> test_mesg = '<STR_LIT>' % { '<STR_LIT:user>' : USER , '<STR_LIT>' : '<STR_LIT:\n>' , '<STR_LIT:data>' : data } <EOL> test_seq1 = [ <EOL> ( '<STR_LIT:list>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:list>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , None , None , test_mesg ) ) , <EOL> ( '<STR_LIT:list>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , ) ) , <EOL> ( '<STR_LIT>' , ( None , '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT:store>' , ( '<STR_LIT:1>' , '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ] <EOL> test_seq2 = ( <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , ) ) , <EOL> ( '<STR_LIT>' , ( None , None , None , test_mesg ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ) <EOL> AsyncError , M = None , None <EOL> def responder ( cb_arg_list ) : <EOL> response , cb_arg , error = cb_arg_list <EOL> global AsyncError <EOL> cmd , args = cb_arg <EOL> if error is not None : <EOL> AsyncError = error <EOL> M . _log ( <NUM_LIT:0> , '<STR_LIT>' % ( cmd , args , error ) ) <EOL> return <EOL> typ , dat = response <EOL> M . _log ( <NUM_LIT:0> , '<STR_LIT>' % ( cmd , args , typ , dat ) ) <EOL> if typ == '<STR_LIT>' : <EOL> AsyncError = ( Exception , dat [ <NUM_LIT:0> ] ) <EOL> def run ( cmd , args , cb = True ) : <EOL> if AsyncError : <EOL> M . _log ( <NUM_LIT:1> , '<STR_LIT>' % repr ( AsyncError ) ) <EOL> M . logout ( ) <EOL> typ , val = AsyncError <EOL> raise typ ( val ) <EOL> if not M . debug : M . _log ( <NUM_LIT:0> , '<STR_LIT>' % ( cmd , args ) ) <EOL> try : <EOL> if cb : <EOL> typ , dat = getattr ( M , cmd ) ( callback = responder , cb_arg = ( cmd , args ) , * args ) <EOL> M . _log ( <NUM_LIT:1> , '<STR_LIT>' % ( cmd , args , typ , dat ) ) <EOL> else : <EOL> typ , dat = getattr ( M , cmd ) ( * args ) <EOL> M . _log ( <NUM_LIT:1> , '<STR_LIT>' % ( cmd , args , typ , dat ) ) <EOL> except : <EOL> M . _log ( <NUM_LIT:1> , '<STR_LIT>' % sys . exc_info ( ) [ : <NUM_LIT:2> ] ) <EOL> M . logout ( ) <EOL> raise <EOL> if typ == '<STR_LIT>' : <EOL> M . _log ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> M . logout ( ) <EOL> raise Exception ( dat [ <NUM_LIT:0> ] ) <EOL> return dat <EOL> try : <EOL> threading . currentThread ( ) . setName ( '<STR_LIT>' ) <EOL> if keyfile is not None : <EOL> if not keyfile : keyfile = None <EOL> if not certfile : certfile = None <EOL> M = IMAP4_SSL ( host = host , port = port , keyfile = keyfile , certfile = certfile , ssl_version = "<STR_LIT>" , debug = debug , identifier = '<STR_LIT>' , timeout = <NUM_LIT:10> , debug_buf_lvl = debug_buf_lvl , tls_level = "<STR_LIT>" ) <EOL> elif stream_command : <EOL> M = IMAP4_stream ( stream_command , debug = debug , identifier = '<STR_LIT>' , timeout = <NUM_LIT:10> , debug_buf_lvl = debug_buf_lvl ) <EOL> else : <EOL> M = IMAP4 ( host = host , port = port , debug = debug , identifier = '<STR_LIT>' , timeout = <NUM_LIT:10> , debug_buf_lvl = debug_buf_lvl ) <EOL> if M . state != '<STR_LIT>' : <EOL> PASSWD = getpass . getpass ( "<STR_LIT>" % ( USER , host or "<STR_LIT:localhost>" ) ) <EOL> test_seq1 . insert ( <NUM_LIT:0> , ( '<STR_LIT>' , ( USER , PASSWD ) ) ) <EOL> M . _log ( <NUM_LIT:0> , '<STR_LIT>' % M . PROTOCOL_VERSION ) <EOL> if '<STR_LIT>' in M . capabilities : <EOL> M . enable_compression ( ) <EOL> for cmd , args in test_seq1 : <EOL> run ( cmd , args ) <EOL> for ml in run ( '<STR_LIT:list>' , ( '<STR_LIT>' , '<STR_LIT>' ) , cb = False ) : <EOL> mo = re . match ( br'<STR_LIT>' , ml ) <EOL> if mo : path = mo . group ( <NUM_LIT:1> ) <EOL> else : path = ml . split ( ) [ - <NUM_LIT:1> ] <EOL> run ( '<STR_LIT>' , ( path , ) ) <EOL> if '<STR_LIT>' in M . capabilities : <EOL> run ( '<STR_LIT:id>' , ( ) ) <EOL> run ( '<STR_LIT:id>' , ( "<STR_LIT>" , ) ) <EOL> run ( '<STR_LIT:id>' , ( "<STR_LIT:version>" , __version__ , "<STR_LIT>" , os . uname ( ) [ <NUM_LIT:0> ] ) ) <EOL> for cmd , args in test_seq2 : <EOL> if ( cmd , args ) != ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> run ( cmd , args ) <EOL> continue <EOL> dat = run ( cmd , args , cb = False ) <EOL> uid = dat [ - <NUM_LIT:1> ] . split ( ) <EOL> if not uid : continue <EOL> run ( '<STR_LIT>' , ( '<STR_LIT>' , uid [ - <NUM_LIT:1> ] , <EOL> '<STR_LIT>' ) ) <EOL> run ( '<STR_LIT>' , ( '<STR_LIT>' , uid [ - <NUM_LIT:1> ] , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> run ( '<STR_LIT>' , ( ) ) <EOL> if '<STR_LIT>' in M . capabilities : <EOL> run ( '<STR_LIT>' , ( <NUM_LIT:2> , ) , cb = False ) <EOL> run ( '<STR_LIT>' , ( <NUM_LIT> , ) ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> run ( '<STR_LIT>' , ( ) , cb = False ) <EOL> run ( '<STR_LIT>' , ( None , None , None , test_mesg ) , cb = False ) <EOL> num = run ( '<STR_LIT>' , ( None , '<STR_LIT>' ) , cb = False ) [ <NUM_LIT:0> ] . split ( ) [ <NUM_LIT:0> ] <EOL> dat = run ( '<STR_LIT>' , ( num , '<STR_LIT>' ) , cb = False ) <EOL> M . _mesg ( '<STR_LIT>' % ( num , repr ( dat ) ) ) <EOL> run ( '<STR_LIT>' , ( <NUM_LIT:2> , ) ) <EOL> run ( '<STR_LIT:store>' , ( num , '<STR_LIT>' , '<STR_LIT>' ) , cb = False ) , <EOL> dat = run ( '<STR_LIT>' , ( num , '<STR_LIT>' ) , cb = False ) <EOL> M . _mesg ( '<STR_LIT>' % ( num , repr ( dat ) ) ) <EOL> run ( '<STR_LIT>' , ( '<STR_LIT>' , num , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> run ( '<STR_LIT>' , ( ) ) <EOL> if idle_intr : <EOL> M . _mesg ( '<STR_LIT>' ) <EOL> try : <EOL> run ( '<STR_LIT>' , ( <NUM_LIT> , ) , cb = False ) <EOL> except KeyboardInterrupt : <EOL> M . _mesg ( '<STR_LIT>' ) <EOL> M . _mesg ( '<STR_LIT>' ) <EOL> raise <EOL> elif idle_intr : <EOL> M . _mesg ( '<STR_LIT>' ) <EOL> run ( '<STR_LIT>' , ( ) , cb = False ) <EOL> if debug : <EOL> M . _mesg ( '<STR_LIT>' ) <EOL> M . _print_log ( ) <EOL> M . _mesg ( '<STR_LIT>' ) <EOL> M . _mesg ( '<STR_LIT>' ) <EOL> for typ , dat in M . pop_untagged_responses ( ) : M . _mesg ( '<STR_LIT>' % ( typ , dat ) ) <EOL> print ( '<STR_LIT>' ) <EOL> except : <EOL> if not idle_intr or M is None or not '<STR_LIT>' in M . capabilities : <EOL> print ( '<STR_LIT>' ) <EOL> if not debug : <EOL> print ( '''<STR_LIT>''' % sys . argv [ <NUM_LIT:0> ] ) <EOL> raise </s>
