<s> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> import binascii , os , random , re , socket , sys , time <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> CRLF = '<STR_LIT:\r\n>' <EOL> Debug = <NUM_LIT:0> <EOL> IMAP4_PORT = <NUM_LIT> <EOL> IMAP4_SSL_PORT = <NUM_LIT> <EOL> AllowedVersions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Commands = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<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>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<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>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> Continuation = re . compile ( r'<STR_LIT>' ) <EOL> Flags = re . compile ( r'<STR_LIT>' ) <EOL> InternalDate = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT:">' ) <EOL> Literal = re . compile ( r'<STR_LIT>' ) <EOL> MapCRLF = re . compile ( r'<STR_LIT>' ) <EOL> Response_code = re . compile ( r'<STR_LIT>' ) <EOL> Untagged_response = re . compile ( r'<STR_LIT>' ) <EOL> Untagged_status = re . compile ( r'<STR_LIT>' ) <EOL> class IMAP4 : <EOL> """<STR_LIT>""" <EOL> class error ( Exception ) : pass <EOL> class abort ( error ) : pass <EOL> class readonly ( abort ) : pass <EOL> mustquote = re . compile ( r"<STR_LIT>" ) <EOL> def __init__ ( self , host = '<STR_LIT>' , port = IMAP4_PORT ) : <EOL> self . debug = Debug <EOL> self . state = '<STR_LIT>' <EOL> self . literal = None <EOL> self . tagged_commands = { } <EOL> self . untagged_responses = { } <EOL> self . continuation_response = '<STR_LIT>' <EOL> self . is_readonly = False <EOL> self . tagnum = <NUM_LIT:0> <EOL> self . open ( host , port ) <EOL> self . tagpre = Int2AP ( random . randint ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . tagre = re . compile ( r'<STR_LIT>' <EOL> + self . tagpre <EOL> + r'<STR_LIT>' ) <EOL> if __debug__ : <EOL> self . _cmd_log_len = <NUM_LIT:10> <EOL> self . _cmd_log_idx = <NUM_LIT:0> <EOL> self . _cmd_log = { } <EOL> if self . debug >= <NUM_LIT:1> : <EOL> self . _mesg ( '<STR_LIT>' % __version__ ) <EOL> self . _mesg ( '<STR_LIT>' % self . tagpre ) <EOL> self . welcome = self . _get_response ( ) <EOL> if '<STR_LIT>' in self . untagged_responses : <EOL> self . state = '<STR_LIT>' <EOL> elif '<STR_LIT:OK>' in self . untagged_responses : <EOL> self . state = '<STR_LIT>' <EOL> else : <EOL> raise self . error ( self . welcome ) <EOL> typ , dat = self . capability ( ) <EOL> if dat == [ None ] : <EOL> raise self . error ( '<STR_LIT>' ) <EOL> self . capabilities = tuple ( dat [ - <NUM_LIT:1> ] . upper ( ) . split ( ) ) <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:3> : <EOL> self . _mesg ( '<STR_LIT>' % ( self . capabilities , ) ) <EOL> for version in AllowedVersions : <EOL> if not version in self . capabilities : <EOL> continue <EOL> self . PROTOCOL_VERSION = version <EOL> return <EOL> raise self . error ( '<STR_LIT>' ) <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 = '<STR_LIT>' , port = IMAP4_PORT ) : <EOL> """<STR_LIT>""" <EOL> self . host = host <EOL> self . port = port <EOL> self . sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . sock . connect ( ( host , port ) ) <EOL> self . file = self . sock . makefile ( '<STR_LIT:rb>' ) <EOL> def read ( self , size ) : <EOL> """<STR_LIT>""" <EOL> return self . file . read ( size ) <EOL> def readline ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . file . readline ( ) <EOL> def send ( self , data ) : <EOL> """<STR_LIT>""" <EOL> self . sock . sendall ( data ) <EOL> def shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . file . close ( ) <EOL> self . sock . close ( ) <EOL> def socket ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sock <EOL> def recent ( self ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _untagged_response ( '<STR_LIT:OK>' , [ None ] , name ) <EOL> if dat [ - <NUM_LIT:1> ] : <EOL> return typ , dat <EOL> typ , dat = self . noop ( ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def response ( self , code ) : <EOL> """<STR_LIT>""" <EOL> return self . _untagged_response ( code , [ None ] , code . upper ( ) ) <EOL> def append ( self , mailbox , flags , date_time , message ) : <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> self . literal = MapCRLF . sub ( CRLF , message ) <EOL> return self . _simple_command ( name , mailbox , flags , date_time ) <EOL> def authenticate ( self , mechanism , authobject ) : <EOL> """<STR_LIT>""" <EOL> mech = mechanism . upper ( ) <EOL> self . literal = _Authenticator ( authobject ) . process <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , mech ) <EOL> if typ != '<STR_LIT:OK>' : <EOL> raise self . error ( dat [ - <NUM_LIT:1> ] ) <EOL> self . state = '<STR_LIT>' <EOL> return typ , dat <EOL> def capability ( self ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def check ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' ) <EOL> finally : <EOL> self . state = '<STR_LIT>' <EOL> return typ , dat <EOL> def copy ( self , message_set , new_mailbox ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , message_set , new_mailbox ) <EOL> def create ( self , mailbox ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox ) <EOL> def delete ( self , mailbox ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox ) <EOL> def deleteacl ( self , mailbox , who ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , who ) <EOL> def expunge ( self ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def fetch ( self , message_set , message_parts ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name , message_set , message_parts ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def getacl ( self , mailbox ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , mailbox ) <EOL> return self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> def getannotation ( self , mailbox , entry , attribute ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , mailbox , entry , attribute ) <EOL> return self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> def getquota ( self , root ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , root ) <EOL> return self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> def getquotaroot ( self , mailbox ) : <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 typ , [ quotaroot , quota ] <EOL> def list ( self , directory = '<STR_LIT>' , pattern = '<STR_LIT:*>' ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name , directory , pattern ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def login ( self , user , password ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , user , self . _quote ( password ) ) <EOL> if typ != '<STR_LIT:OK>' : <EOL> raise self . error ( dat [ - <NUM_LIT:1> ] ) <EOL> self . state = '<STR_LIT>' <EOL> return typ , dat <EOL> def login_cram_md5 ( self , user , password ) : <EOL> """<STR_LIT>""" <EOL> self . user , self . password = user , password <EOL> return self . authenticate ( '<STR_LIT>' , self . _CRAM_MD5_AUTH ) <EOL> def _CRAM_MD5_AUTH ( self , challenge ) : <EOL> """<STR_LIT>""" <EOL> import hmac <EOL> return self . user + "<STR_LIT:U+0020>" + hmac . HMAC ( self . password , challenge ) . hexdigest ( ) <EOL> def logout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . state = '<STR_LIT>' <EOL> try : typ , dat = self . _simple_command ( '<STR_LIT>' ) <EOL> except : typ , dat = '<STR_LIT>' , [ '<STR_LIT>' % sys . exc_info ( ) [ : <NUM_LIT:2> ] ] <EOL> self . shutdown ( ) <EOL> if '<STR_LIT>' in self . untagged_responses : <EOL> return '<STR_LIT>' , self . untagged_responses [ '<STR_LIT>' ] <EOL> return typ , dat <EOL> def lsub ( self , directory = '<STR_LIT>' , pattern = '<STR_LIT:*>' ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name , directory , pattern ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def myrights ( self , mailbox ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , mailbox ) <EOL> return self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> def namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def noop ( self ) : <EOL> """<STR_LIT>""" <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:3> : <EOL> self . _dump_ur ( self . untagged_responses ) <EOL> return self . _simple_command ( '<STR_LIT>' ) <EOL> def partial ( self , message_num , message_part , start , length ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name , message_num , message_part , start , length ) <EOL> return self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> def proxyauth ( self , user ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> return self . _simple_command ( '<STR_LIT>' , user ) <EOL> def rename ( self , oldmailbox , newmailbox ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , oldmailbox , newmailbox ) <EOL> def search ( self , charset , * criteria ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> if charset : <EOL> typ , dat = self . _simple_command ( name , '<STR_LIT>' , charset , * criteria ) <EOL> else : <EOL> typ , dat = self . _simple_command ( name , * criteria ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def select ( self , mailbox = '<STR_LIT>' , readonly = False ) : <EOL> """<STR_LIT>""" <EOL> self . untagged_responses = { } <EOL> self . is_readonly = readonly <EOL> if readonly : <EOL> name = '<STR_LIT>' <EOL> else : <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name , mailbox ) <EOL> if typ != '<STR_LIT:OK>' : <EOL> self . state = '<STR_LIT>' <EOL> return typ , dat <EOL> self . state = '<STR_LIT>' <EOL> if '<STR_LIT>' in self . untagged_responses and not readonly : <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:1> : <EOL> self . _dump_ur ( self . untagged_responses ) <EOL> raise self . readonly ( '<STR_LIT>' % mailbox ) <EOL> return typ , self . untagged_responses . get ( '<STR_LIT>' , [ None ] ) <EOL> def setacl ( self , mailbox , who , what ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox , who , what ) <EOL> def setannotation ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , * args ) <EOL> return self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> def setquota ( self , root , limits ) : <EOL> """<STR_LIT>""" <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , root , limits ) <EOL> return self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> def sort ( self , sort_criteria , charset , * search_criteria ) : <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> typ , dat = self . _simple_command ( name , sort_criteria , charset , * search_criteria ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def status ( self , mailbox , names ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name , mailbox , names ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def store ( self , message_set , command , flags ) : <EOL> """<STR_LIT>""" <EOL> if ( flags [ <NUM_LIT:0> ] , flags [ - <NUM_LIT:1> ] ) != ( '<STR_LIT:(>' , '<STR_LIT:)>' ) : <EOL> flags = '<STR_LIT>' % flags <EOL> typ , dat = self . _simple_command ( '<STR_LIT>' , message_set , command , flags ) <EOL> return self . _untagged_response ( typ , dat , '<STR_LIT>' ) <EOL> def subscribe ( self , mailbox ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox ) <EOL> def thread ( self , threading_algorithm , charset , * search_criteria ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name , threading_algorithm , charset , * search_criteria ) <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def uid ( self , command , * args ) : <EOL> """<STR_LIT>""" <EOL> command = command . upper ( ) <EOL> if not command in Commands : <EOL> raise self . error ( "<STR_LIT>" % command ) <EOL> if self . state not in Commands [ command ] : <EOL> raise self . error ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( command , self . state , <EOL> '<STR_LIT:U+002CU+0020>' . join ( Commands [ command ] ) ) ) <EOL> name = '<STR_LIT>' <EOL> typ , dat = self . _simple_command ( name , command , * args ) <EOL> if command in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> name = command <EOL> else : <EOL> name = '<STR_LIT>' <EOL> return self . _untagged_response ( typ , dat , name ) <EOL> def unsubscribe ( self , mailbox ) : <EOL> """<STR_LIT>""" <EOL> return self . _simple_command ( '<STR_LIT>' , mailbox ) <EOL> def xatom ( self , name , * args ) : <EOL> """<STR_LIT>""" <EOL> name = name . upper ( ) <EOL> if not name in Commands : <EOL> Commands [ name ] = ( self . state , ) <EOL> return self . _simple_command ( name , * args ) <EOL> def _append_untagged ( self , typ , dat ) : <EOL> if dat is None : dat = '<STR_LIT>' <EOL> ur = self . untagged_responses <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:5> : <EOL> self . _mesg ( '<STR_LIT>' % <EOL> ( typ , len ( ur . get ( typ , '<STR_LIT>' ) ) , dat ) ) <EOL> if typ in ur : <EOL> ur [ typ ] . append ( dat ) <EOL> else : <EOL> ur [ typ ] = [ dat ] <EOL> def _check_bye ( self ) : <EOL> bye = self . untagged_responses . get ( '<STR_LIT>' ) <EOL> if bye : <EOL> raise self . abort ( bye [ - <NUM_LIT:1> ] ) <EOL> def _command ( self , name , * args ) : <EOL> if self . state not in Commands [ name ] : <EOL> self . literal = None <EOL> raise self . error ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( name , self . state , <EOL> '<STR_LIT:U+002CU+0020>' . join ( Commands [ name ] ) ) ) <EOL> for typ in ( '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if typ in self . untagged_responses : <EOL> del self . untagged_responses [ typ ] <EOL> if '<STR_LIT>' in self . untagged_responses and not self . is_readonly : <EOL> raise self . readonly ( '<STR_LIT>' ) <EOL> tag = self . _new_tag ( ) <EOL> data = '<STR_LIT>' % ( tag , name ) <EOL> for arg in args : <EOL> if arg is None : continue <EOL> data = '<STR_LIT>' % ( data , self . _checkquote ( 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 = '<STR_LIT>' % ( data , len ( literal ) ) <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:4> : <EOL> self . _mesg ( '<STR_LIT>' % data ) <EOL> else : <EOL> self . _log ( '<STR_LIT>' % data ) <EOL> try : <EOL> self . send ( '<STR_LIT>' % ( data , CRLF ) ) <EOL> except ( socket . error , OSError ) , val : <EOL> raise self . abort ( '<STR_LIT>' % val ) <EOL> if literal is None : <EOL> return tag <EOL> while <NUM_LIT:1> : <EOL> while self . _get_response ( ) : <EOL> if self . tagged_commands [ tag ] : <EOL> return tag <EOL> if literator : <EOL> literal = literator ( self . continuation_response ) <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:4> : <EOL> self . _mesg ( '<STR_LIT>' % len ( literal ) ) <EOL> try : <EOL> self . send ( literal ) <EOL> self . send ( CRLF ) <EOL> except ( socket . error , OSError ) , val : <EOL> raise self . abort ( '<STR_LIT>' % val ) <EOL> if not literator : <EOL> break <EOL> return tag <EOL> def _command_complete ( self , name , tag ) : <EOL> self . _check_bye ( ) <EOL> try : <EOL> typ , data = self . _get_tagged_response ( tag ) <EOL> except self . abort , val : <EOL> raise self . abort ( '<STR_LIT>' % ( name , val ) ) <EOL> except self . error , val : <EOL> raise self . error ( '<STR_LIT>' % ( name , val ) ) <EOL> self . _check_bye ( ) <EOL> if typ == '<STR_LIT>' : <EOL> raise self . error ( '<STR_LIT>' % ( name , typ , data ) ) <EOL> return typ , data <EOL> def _get_response ( self ) : <EOL> resp = self . _get_line ( ) <EOL> if self . _match ( self . tagre , resp ) : <EOL> tag = self . mo . group ( '<STR_LIT>' ) <EOL> if not tag in self . tagged_commands : <EOL> raise self . abort ( '<STR_LIT>' % resp ) <EOL> typ = self . mo . group ( '<STR_LIT:type>' ) <EOL> dat = self . mo . group ( '<STR_LIT:data>' ) <EOL> self . tagged_commands [ tag ] = ( typ , [ dat ] ) <EOL> else : <EOL> dat2 = None <EOL> if not self . _match ( Untagged_response , resp ) : <EOL> if self . _match ( Untagged_status , resp ) : <EOL> dat2 = self . mo . group ( '<STR_LIT>' ) <EOL> if self . mo is None : <EOL> if self . _match ( Continuation , resp ) : <EOL> self . continuation_response = self . mo . group ( '<STR_LIT:data>' ) <EOL> return None <EOL> raise self . abort ( "<STR_LIT>" % resp ) <EOL> typ = self . mo . group ( '<STR_LIT:type>' ) <EOL> dat = self . mo . group ( '<STR_LIT:data>' ) <EOL> if dat is None : dat = '<STR_LIT>' <EOL> if dat2 : dat = dat + '<STR_LIT:U+0020>' + dat2 <EOL> while self . _match ( Literal , dat ) : <EOL> size = int ( self . mo . group ( '<STR_LIT:size>' ) ) <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:4> : <EOL> self . _mesg ( '<STR_LIT>' % size ) <EOL> data = self . read ( size ) <EOL> self . _append_untagged ( typ , ( dat , data ) ) <EOL> dat = self . _get_line ( ) <EOL> self . _append_untagged ( typ , dat ) <EOL> if typ in ( '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' ) and self . _match ( Response_code , dat ) : <EOL> self . _append_untagged ( self . mo . group ( '<STR_LIT:type>' ) , self . mo . group ( '<STR_LIT:data>' ) ) <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:1> and typ in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . _mesg ( '<STR_LIT>' % ( typ , dat ) ) <EOL> return resp <EOL> def _get_tagged_response ( self , tag ) : <EOL> while <NUM_LIT:1> : <EOL> result = self . tagged_commands [ tag ] <EOL> if result is not None : <EOL> del self . tagged_commands [ tag ] <EOL> return result <EOL> try : <EOL> self . _get_response ( ) <EOL> except self . abort , val : <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:1> : <EOL> self . print_log ( ) <EOL> raise <EOL> def _get_line ( self ) : <EOL> line = self . readline ( ) <EOL> if not line : <EOL> raise self . abort ( '<STR_LIT>' ) <EOL> line = line [ : - <NUM_LIT:2> ] <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:4> : <EOL> self . _mesg ( '<STR_LIT>' % line ) <EOL> else : <EOL> self . _log ( '<STR_LIT>' % line ) <EOL> return line <EOL> def _match ( self , cre , s ) : <EOL> self . mo = cre . match ( s ) <EOL> if __debug__ : <EOL> if self . mo is not None and self . debug >= <NUM_LIT:5> : <EOL> self . _mesg ( "<STR_LIT>" % ( cre . pattern , self . mo . groups ( ) ) ) <EOL> return self . mo is not None <EOL> def _new_tag ( self ) : <EOL> tag = '<STR_LIT>' % ( self . tagpre , self . tagnum ) <EOL> self . tagnum = self . tagnum + <NUM_LIT:1> <EOL> self . tagged_commands [ tag ] = None <EOL> return tag <EOL> def _checkquote ( self , arg ) : <EOL> if type ( arg ) is not type ( '<STR_LIT>' ) : <EOL> return arg <EOL> if len ( arg ) >= <NUM_LIT:2> and ( arg [ <NUM_LIT:0> ] , arg [ - <NUM_LIT:1> ] ) in ( ( '<STR_LIT:(>' , '<STR_LIT:)>' ) , ( '<STR_LIT:">' , '<STR_LIT:">' ) ) : <EOL> return arg <EOL> if arg and self . mustquote . search ( arg ) is None : <EOL> return arg <EOL> return self . _quote ( arg ) <EOL> def _quote ( self , arg ) : <EOL> arg = arg . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) <EOL> arg = arg . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> return '<STR_LIT>' % arg <EOL> def _simple_command ( self , name , * args ) : <EOL> return self . _command_complete ( name , self . _command ( name , * args ) ) <EOL> def _untagged_response ( self , typ , dat , name ) : <EOL> if typ == '<STR_LIT>' : <EOL> return typ , dat <EOL> if not name in self . untagged_responses : <EOL> return typ , [ None ] <EOL> data = self . untagged_responses . pop ( name ) <EOL> if __debug__ : <EOL> if self . debug >= <NUM_LIT:5> : <EOL> self . _mesg ( '<STR_LIT>' % ( name , data ) ) <EOL> return typ , data <EOL> if __debug__ : <EOL> def _mesg ( self , s , secs = None ) : <EOL> if secs is None : <EOL> secs = time . time ( ) <EOL> tm = time . strftime ( '<STR_LIT>' , time . localtime ( secs ) ) <EOL> sys . stderr . write ( '<STR_LIT>' % ( tm , ( secs * <NUM_LIT:100> ) % <NUM_LIT:100> , s ) ) <EOL> sys . stderr . flush ( ) <EOL> def _dump_ur ( self , dict ) : <EOL> l = dict . items ( ) <EOL> if not l : return <EOL> t = '<STR_LIT>' <EOL> l = map ( lambda x : '<STR_LIT>' % ( x [ <NUM_LIT:0> ] , x [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] and '<STR_LIT>' . join ( x [ <NUM_LIT:1> ] ) or '<STR_LIT>' ) , l ) <EOL> self . _mesg ( '<STR_LIT>' % ( t , t . join ( l ) ) ) <EOL> def _log ( self , line ) : <EOL> self . _cmd_log [ self . _cmd_log_idx ] = ( line , 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> def print_log ( self ) : <EOL> self . _mesg ( '<STR_LIT>' % len ( self . _cmd_log ) ) <EOL> i , n = self . _cmd_log_idx , self . _cmd_log_len <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> try : <EOL> import ssl <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> class IMAP4_SSL ( IMAP4 ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host = '<STR_LIT>' , port = IMAP4_SSL_PORT , keyfile = None , certfile = None ) : <EOL> self . keyfile = keyfile <EOL> self . certfile = certfile <EOL> IMAP4 . __init__ ( self , host , port ) <EOL> def open ( self , host = '<STR_LIT>' , port = IMAP4_SSL_PORT ) : <EOL> """<STR_LIT>""" <EOL> self . host = host <EOL> self . port = port <EOL> self . sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> self . sock . connect ( ( host , port ) ) <EOL> self . sslobj = ssl . wrap_socket ( self . sock , self . keyfile , self . certfile ) <EOL> def read ( self , size ) : <EOL> """<STR_LIT>""" <EOL> chunks = [ ] <EOL> read = <NUM_LIT:0> <EOL> while read < size : <EOL> data = self . sslobj . read ( min ( size - read , <NUM_LIT> ) ) <EOL> read += len ( data ) <EOL> chunks . append ( data ) <EOL> return '<STR_LIT>' . join ( chunks ) <EOL> def readline ( self ) : <EOL> """<STR_LIT>""" <EOL> line = [ ] <EOL> while <NUM_LIT:1> : <EOL> char = self . sslobj . read ( <NUM_LIT:1> ) <EOL> line . append ( char ) <EOL> if char == "<STR_LIT:\n>" : return '<STR_LIT>' . join ( line ) <EOL> def send ( self , data ) : <EOL> """<STR_LIT>""" <EOL> bytes = len ( data ) <EOL> while bytes > <NUM_LIT:0> : <EOL> sent = self . sslobj . write ( data ) <EOL> if sent == bytes : <EOL> break <EOL> data = data [ sent : ] <EOL> bytes = bytes - sent <EOL> def shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . sock . close ( ) <EOL> def socket ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sock <EOL> def ssl ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sslobj <EOL> __all__ . append ( "<STR_LIT>" ) <EOL> class IMAP4_stream ( IMAP4 ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , command ) : <EOL> self . command = command <EOL> IMAP4 . __init__ ( self ) <EOL> def open ( self , host = None , port = None ) : <EOL> """<STR_LIT>""" <EOL> self . host = None <EOL> self . port = None <EOL> self . sock = None <EOL> self . file = None <EOL> self . writefile , self . readfile = os . popen2 ( self . command ) <EOL> def read ( self , size ) : <EOL> """<STR_LIT>""" <EOL> return self . readfile . read ( size ) <EOL> def readline ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . readfile . readline ( ) <EOL> def send ( self , data ) : <EOL> """<STR_LIT>""" <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> class _Authenticator : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mechinst ) : <EOL> self . mech = mechinst <EOL> def process ( self , data ) : <EOL> ret = self . mech ( self . decode ( data ) ) <EOL> if ret is None : <EOL> return '<STR_LIT:*>' <EOL> return self . encode ( ret ) <EOL> def encode ( self , inp ) : <EOL> oup = '<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 = '<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 '<STR_LIT>' <EOL> return binascii . a2b_base64 ( inp ) <EOL> Mon2num = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT>' : <NUM_LIT:7> , '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:9> , '<STR_LIT>' : <NUM_LIT:10> , '<STR_LIT>' : <NUM_LIT:11> , '<STR_LIT>' : <NUM_LIT:12> } <EOL> def Internaldate2tuple ( 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 == '<STR_LIT:->' : <EOL> zone = - zone <EOL> tt = ( year , mon , day , hour , min , sec , - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> utc = time . mktime ( tt ) <EOL> lt = time . localtime ( utc ) <EOL> if time . daylight and lt [ - <NUM_LIT:1> ] : <EOL> zone = zone + time . altzone <EOL> else : <EOL> zone = zone + time . timezone <EOL> return time . localtime ( utc - zone ) <EOL> def Int2AP ( num ) : <EOL> """<STR_LIT>""" <EOL> val = '<STR_LIT>' ; AP = '<STR_LIT>' <EOL> num = int ( abs ( num ) ) <EOL> while num : <EOL> num , mod = divmod ( num , <NUM_LIT:16> ) <EOL> val = AP [ mod ] + val <EOL> return val <EOL> def ParseFlags ( resp ) : <EOL> """<STR_LIT>""" <EOL> mo = Flags . match ( resp ) <EOL> if not mo : <EOL> return ( ) <EOL> return tuple ( mo . group ( '<STR_LIT>' ) . split ( ) ) <EOL> def Time2Internaldate ( date_time ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( date_time , ( int , float ) ) : <EOL> tt = time . localtime ( date_time ) <EOL> elif isinstance ( date_time , ( tuple , time . struct_time ) ) : <EOL> tt = 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> dt = time . strftime ( "<STR_LIT>" , tt ) <EOL> if dt [ <NUM_LIT:0> ] == '<STR_LIT:0>' : <EOL> dt = '<STR_LIT:U+0020>' + dt [ <NUM_LIT:1> : ] <EOL> if time . daylight and tt [ - <NUM_LIT:1> ] : <EOL> zone = - time . altzone <EOL> else : <EOL> zone = - time . timezone <EOL> return '<STR_LIT:">' + dt + "<STR_LIT>" % divmod ( zone // <NUM_LIT> , <NUM_LIT> ) + '<STR_LIT:">' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import getopt , getpass <EOL> try : <EOL> optlist , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , '<STR_LIT>' ) <EOL> except getopt . error , val : <EOL> optlist , args = ( ) , ( ) <EOL> stream_command = None <EOL> for opt , val in optlist : <EOL> if opt == '<STR_LIT>' : <EOL> Debug = 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> host = args [ <NUM_LIT:0> ] <EOL> USER = getpass . getuser ( ) <EOL> PASSWD = getpass . getpass ( "<STR_LIT>" % ( USER , host or "<STR_LIT:localhost>" ) ) <EOL> test_mesg = '<STR_LIT>' % { '<STR_LIT:user>' : USER , '<STR_LIT>' : '<STR_LIT:\n>' } <EOL> test_seq1 = ( <EOL> ( '<STR_LIT>' , ( USER , PASSWD ) ) , <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:test>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT:1>' , '<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>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , ) ) , <EOL> ( '<STR_LIT>' , ( None , None , None , test_mesg ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ( '<STR_LIT>' , ( ) ) , <EOL> ) <EOL> def run ( cmd , args ) : <EOL> M . _mesg ( '<STR_LIT>' % ( cmd , args ) ) <EOL> typ , dat = getattr ( M , cmd ) ( * args ) <EOL> M . _mesg ( '<STR_LIT>' % ( cmd , typ , dat ) ) <EOL> if typ == '<STR_LIT>' : raise dat [ <NUM_LIT:0> ] <EOL> return dat <EOL> try : <EOL> if stream_command : <EOL> M = IMAP4_stream ( stream_command ) <EOL> else : <EOL> M = IMAP4 ( host ) <EOL> if M . state == '<STR_LIT>' : <EOL> test_seq1 = test_seq1 [ <NUM_LIT:1> : ] <EOL> M . _mesg ( '<STR_LIT>' % M . PROTOCOL_VERSION ) <EOL> M . _mesg ( '<STR_LIT>' % ( M . capabilities , ) ) <EOL> for cmd , args in test_seq1 : <EOL> run ( cmd , args ) <EOL> for ml in run ( '<STR_LIT:list>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> mo = re . match ( r'<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> for cmd , args in test_seq2 : <EOL> dat = run ( cmd , args ) <EOL> if ( cmd , args ) != ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> continue <EOL> uid = dat [ - <NUM_LIT:1> ] . split ( ) <EOL> if not uid : continue <EOL> run ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT:%s>' % uid [ - <NUM_LIT:1> ] , <EOL> '<STR_LIT>' ) ) <EOL> print '<STR_LIT>' <EOL> except : <EOL> print '<STR_LIT>' <EOL> if not Debug : <EOL> print '''<STR_LIT>''' % sys . argv [ <NUM_LIT:0> ] <EOL> raise </s>
