<s> """<STR_LIT>""" <EOL> import email . utils <EOL> import logging <EOL> import hashlib <EOL> from cStringIO import StringIO <EOL> from email . Generator import Generator <EOL> from email . Header import Header <EOL> import asimap . utils <EOL> import asimap . constants <EOL> import asimap . exceptions <EOL> class BadSection ( asimap . exceptions . Bad ) : <EOL> def __init__ ( self , value = "<STR_LIT>" ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . value <EOL> class TextGenerator ( Generator ) : <EOL> def __init__ ( self , outfp , headers = False ) : <EOL> """<STR_LIT>""" <EOL> Generator . __init__ ( self , outfp ) <EOL> self . _headers = headers <EOL> def _write ( self , msg ) : <EOL> if self . _headers : <EOL> Generator . _write ( self , msg ) <EOL> else : <EOL> self . _dispatch ( msg ) <EOL> def clone ( self , fp , headers = True ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( fp , headers ) <EOL> def _is8bitstring ( s ) : <EOL> if isinstance ( s , str ) : <EOL> try : <EOL> unicode ( s , '<STR_LIT>' ) <EOL> except UnicodeError : <EOL> return True <EOL> return False <EOL> class HeaderGenerator ( Generator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , outfp , headers = [ ] , skip = True ) : <EOL> self . log = logging . getLogger ( "<STR_LIT>" % ( __name__ , <EOL> self . __class__ . __name__ ) ) <EOL> Generator . __init__ ( self , outfp ) <EOL> self . _headers = [ x . lower ( ) for x in headers ] <EOL> self . _skip = skip <EOL> def clone ( self , fp ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( fp , self . _headers , self . _skip ) <EOL> def _write ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> meth = getattr ( msg , '<STR_LIT>' , None ) <EOL> if meth is None : <EOL> self . _write_headers ( msg ) <EOL> else : <EOL> meth ( self ) <EOL> def _write_headers ( self , msg ) : <EOL> for h , v in msg . items ( ) : <EOL> if ( self . _skip and h . lower ( ) in self . _headers ) or ( not self . _skip and h . lower ( ) not in self . _headers ) : <EOL> continue <EOL> print >> self . _fp , '<STR_LIT>' % h , <EOL> if self . _maxheaderlen == <NUM_LIT:0> : <EOL> print >> self . _fp , v <EOL> elif isinstance ( v , Header ) : <EOL> print >> self . _fp , v . encode ( ) <EOL> elif _is8bitstring ( v ) : <EOL> print >> self . _fp , v <EOL> else : <EOL> print >> self . _fp , Header ( <EOL> v , maxlinelen = self . _maxheaderlen , <EOL> header_name = h , continuation_ws = '<STR_LIT:\t>' ) . encode ( ) <EOL> print >> self . _fp <EOL> class FetchAtt ( object ) : <EOL> """<STR_LIT>""" <EOL> OP_BODY = '<STR_LIT:body>' <EOL> OP_BODYSTRUCTURE = '<STR_LIT>' <EOL> OP_ENVELOPE = '<STR_LIT>' <EOL> OP_FLAGS = '<STR_LIT>' <EOL> OP_INTERNALDATE = '<STR_LIT>' <EOL> OP_RFC822_HEADER = '<STR_LIT>' <EOL> OP_RFC822_SIZE = '<STR_LIT>' <EOL> OP_RFC822_TEXT = '<STR_LIT>' <EOL> OP_UID = '<STR_LIT>' <EOL> def __init__ ( self , attribute , section = None , partial = None , <EOL> peek = False , ext_data = True , actual_command = None ) : <EOL> """<STR_LIT>""" <EOL> self . attribute = attribute <EOL> self . section = section <EOL> self . partial = partial <EOL> self . peek = peek <EOL> self . ext_data = ext_data <EOL> if actual_command is None : <EOL> self . actual_command = self . attribute . upper ( ) <EOL> else : <EOL> self . actual_command = actual_command <EOL> self . log = logging . getLogger ( "<STR_LIT>" % ( __name__ , <EOL> self . __class__ . __name__ , <EOL> actual_command ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . attribute , str ( self . section ) , <EOL> str ( self . partial ) ) <EOL> def __str__ ( self ) : <EOL> return self . dbg ( show_peek = False ) <EOL> def dbg ( self , show_peek = False ) : <EOL> """<STR_LIT>""" <EOL> result = self . actual_command <EOL> if result == "<STR_LIT>" : <EOL> if show_peek and self . peek : <EOL> result = "<STR_LIT>" <EOL> if self . section is not None : <EOL> sects = [ ] <EOL> for s in self . section : <EOL> if isinstance ( s , ( list , tuple ) ) : <EOL> sects . append ( "<STR_LIT>" % <EOL> ( str ( s [ <NUM_LIT:0> ] ) . upper ( ) , <EOL> '<STR_LIT:U+0020>' . join ( x for x in s [ <NUM_LIT:1> ] ) ) ) <EOL> else : <EOL> sects . append ( str ( s ) . upper ( ) ) <EOL> result += '<STR_LIT>' % '<STR_LIT:.>' . join ( sects ) <EOL> if self . partial : <EOL> result += "<STR_LIT>" % ( self . partial [ <NUM_LIT:0> ] , self . partial [ <NUM_LIT:1> ] ) <EOL> return result <EOL> def fetch ( self , ctx ) : <EOL> """<STR_LIT>""" <EOL> self . ctx = ctx <EOL> if self . attribute == "<STR_LIT:body>" : <EOL> result = self . body ( self . ctx . msg , self . section ) <EOL> elif self . attribute == "<STR_LIT>" : <EOL> result = self . bodystructure ( self . ctx . msg ) <EOL> elif self . attribute == "<STR_LIT>" : <EOL> result = self . envelope ( self . ctx . msg ) <EOL> elif self . attribute == "<STR_LIT>" : <EOL> result = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( <EOL> [ asimap . constants . seq_to_flag ( x ) for x in self . ctx . sequences ] ) <EOL> elif self . attribute == "<STR_LIT>" : <EOL> result = '<STR_LIT>' % self . ctx . internal_date . strftime ( "<STR_LIT>" ) <EOL> elif self . attribute == "<STR_LIT>" : <EOL> result = str ( len ( self . ctx . mailbox . mailbox . get_string ( <EOL> self . ctx . msg_key ) ) ) <EOL> elif self . attribute == "<STR_LIT>" : <EOL> result = str ( self . ctx . uid ) <EOL> else : <EOL> raise NotImplemented <EOL> return "<STR_LIT>" % ( str ( self ) , result ) <EOL> def body ( self , msg , section ) : <EOL> """<STR_LIT>""" <EOL> msg_text = None <EOL> g = None <EOL> if len ( section ) == <NUM_LIT:0> : <EOL> msg_text = email . utils . fix_eols ( msg . as_string ( ) ) <EOL> else : <EOL> if len ( section ) == <NUM_LIT:1> : <EOL> fp = StringIO ( ) <EOL> if isinstance ( section [ <NUM_LIT:0> ] , int ) : <EOL> g = TextGenerator ( fp , headers = False ) <EOL> if section [ <NUM_LIT:0> ] == <NUM_LIT:1> and not msg . is_multipart ( ) : <EOL> pass <EOL> else : <EOL> if section [ <NUM_LIT:0> ] != <NUM_LIT:1> and not msg . is_multipart ( ) : <EOL> raise BadSection ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % section [ <NUM_LIT:0> ] ) <EOL> try : <EOL> msg = msg . get_payload ( section [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> except IndexError : <EOL> raise BadSection ( "<STR_LIT>" <EOL> "<STR_LIT>" % section [ <NUM_LIT:0> ] ) <EOL> elif isinstance ( section [ <NUM_LIT:0> ] , str ) and section [ <NUM_LIT:0> ] . upper ( ) == '<STR_LIT>' : <EOL> g = TextGenerator ( fp , headers = False ) <EOL> elif isinstance ( section [ <NUM_LIT:0> ] , ( list , tuple ) ) : <EOL> if section [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . upper ( ) == "<STR_LIT>" : <EOL> g = HeaderGenerator ( fp , headers = section [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <EOL> skip = False ) <EOL> elif section [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . upper ( ) == "<STR_LIT>" : <EOL> g = HeaderGenerator ( fp , headers = section [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <EOL> skip = True ) <EOL> else : <EOL> raise BadSection ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % section [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> else : <EOL> g = HeaderGenerator ( fp ) <EOL> if isinstance ( section [ <NUM_LIT:0> ] , str ) and section [ <NUM_LIT:0> ] . upper ( ) == '<STR_LIT>' : <EOL> pass <EOL> elif isinstance ( section [ <NUM_LIT:0> ] , str ) and section [ <NUM_LIT:0> ] . upper ( ) == '<STR_LIT>' : <EOL> if msg . is_multipart ( ) and msg . get_content_type ( ) == '<STR_LIT>' : <EOL> msg = msg . get_payload ( <NUM_LIT:0> ) <EOL> else : <EOL> self . log . warn ( "<STR_LIT>" % repr ( section ) ) <EOL> raise BadSection ( "<STR_LIT>" % ( str ( self ) , repr ( section [ <NUM_LIT:0> ] ) ) ) <EOL> elif isinstance ( section [ <NUM_LIT:0> ] , int ) : <EOL> if not msg . is_multipart ( ) : <EOL> raise BadSection ( "<STR_LIT>" <EOL> "<STR_LIT>" % section [ <NUM_LIT:0> ] , <EOL> section ) <EOL> try : <EOL> msg = msg . get_payload ( section [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> except TypeError : <EOL> raise BadSection ( "<STR_LIT>" <EOL> "<STR_LIT>" % section [ <NUM_LIT:0> ] , <EOL> section ) <EOL> return self . body ( msg , section [ <NUM_LIT:1> : ] ) <EOL> if g is None : <EOL> raise BadSection ( "<STR_LIT>" % section ) <EOL> g . flatten ( msg ) <EOL> msg_text = fp . getvalue ( ) <EOL> msg_text = email . utils . fix_eols ( msg_text ) <EOL> if self . partial : <EOL> msg_text = msg_text [ self . partial [ <NUM_LIT:0> ] : self . partial [ <NUM_LIT:1> ] ] <EOL> return "<STR_LIT>" % ( len ( msg_text ) , str ( msg_text ) ) <EOL> def envelope ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> from_field = "<STR_LIT>" <EOL> for field in ( '<STR_LIT:date>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if field in ( '<STR_LIT>' , '<STR_LIT>' ) and field not in msg : <EOL> result . append ( from_field ) <EOL> continue <EOL> if field not in msg : <EOL> result . append ( "<STR_LIT>" ) <EOL> continue <EOL> if field in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> addrs = email . Utils . getaddresses ( [ msg [ field ] ] ) <EOL> if len ( addrs ) == <NUM_LIT:0> : <EOL> result . append ( "<STR_LIT>" ) <EOL> continue <EOL> addr_list = [ ] <EOL> for name , paddr in addrs : <EOL> one_addr = [ ] <EOL> if name == "<STR_LIT>" : <EOL> one_addr . append ( "<STR_LIT>" ) <EOL> else : <EOL> one_addr . append ( '<STR_LIT>' % name ) <EOL> one_addr . append ( "<STR_LIT>" ) <EOL> if paddr != "<STR_LIT>" : <EOL> if '<STR_LIT:@>' in paddr : <EOL> mbox_name , host_name = paddr . split ( '<STR_LIT:@>' ) <EOL> one_addr . append ( '<STR_LIT>' % mbox_name ) <EOL> one_addr . append ( '<STR_LIT>' % host_name ) <EOL> else : <EOL> one_addr . append ( '<STR_LIT>' % paddr ) <EOL> one_addr . append ( '<STR_LIT>' ) <EOL> else : <EOL> one_addr . append ( "<STR_LIT>" ) <EOL> addr_list . append ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( one_addr ) ) <EOL> result . append ( "<STR_LIT>" % "<STR_LIT>" . join ( addr_list ) ) <EOL> if field == "<STR_LIT>" : <EOL> from_field = result [ - <NUM_LIT:1> ] <EOL> else : <EOL> result . append ( '<STR_LIT>' % msg [ field ] ) <EOL> return '<STR_LIT>' % "<STR_LIT:U+0020>" . join ( result ) <EOL> def body_languages ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> langs = [ ] <EOL> for hdr , value in msg . items ( ) : <EOL> if hdr [ - <NUM_LIT:9> : ] . lower ( ) != "<STR_LIT>" : <EOL> continue <EOL> if "<STR_LIT:U+002C>" in value : <EOL> langs . extend ( value . split ( "<STR_LIT:U+002C>" ) ) <EOL> elif "<STR_LIT:;>" in value : <EOL> langs . extend ( value . split ( "<STR_LIT:;>" ) ) <EOL> else : <EOL> langs . append ( value ) <EOL> if len ( langs ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> elif len ( langs ) == <NUM_LIT:1> : <EOL> return '<STR_LIT>' % langs [ <NUM_LIT:0> ] <EOL> else : <EOL> return "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( [ str ( x ) . strip ( ) for x in langs ] ) <EOL> def body_location ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in msg : <EOL> return '<STR_LIT>' % msg [ '<STR_LIT>' ] <EOL> return "<STR_LIT>" <EOL> def body_parameters ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if msg . get_params ( ) is None : <EOL> return "<STR_LIT>" <EOL> msg_params = [ ] <EOL> for k , v in msg . get_params ( ) : <EOL> if v == '<STR_LIT>' : <EOL> continue <EOL> msg_params . append ( '<STR_LIT>' % k . upper ( ) ) <EOL> msg_params . append ( '<STR_LIT>' % v ) <EOL> if len ( msg_params ) > <NUM_LIT:0> : <EOL> return "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( msg_params ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def body_disposition ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in msg : <EOL> cd = msg . get_params ( header = '<STR_LIT>' ) <EOL> if len ( cd ) == <NUM_LIT:1> : <EOL> return '<STR_LIT>' % cd [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . upper ( ) <EOL> else : <EOL> cdpl = [ ] <EOL> for k , v in cd [ <NUM_LIT:1> : ] : <EOL> cdpl . extend ( [ '<STR_LIT>' % ( k . upper ( ) , v ) ] ) <EOL> return '<STR_LIT>' % ( cd [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . upper ( ) , "<STR_LIT:U+0020>" . join ( cdpl ) ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> def bodystructure ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> if msg . is_multipart ( ) : <EOL> sub_parts = [ ] <EOL> for sub_part in msg . get_payload ( ) : <EOL> sub_parts . append ( self . bodystructure ( sub_part ) ) <EOL> if not self . ext_data : <EOL> return '<STR_LIT>' % ( '<STR_LIT>' . join ( sub_parts ) , <EOL> msg . get_content_subtype ( ) ) <EOL> return '<STR_LIT>' % ( '<STR_LIT>' . join ( sub_parts ) , <EOL> msg . get_content_subtype ( ) , <EOL> self . body_parameters ( msg ) , <EOL> self . body_disposition ( msg ) , <EOL> self . body_languages ( msg ) , <EOL> self . body_location ( msg ) ) <EOL> result = [ ] <EOL> result . append ( '<STR_LIT>' % msg . get_content_maintype ( ) ) <EOL> result . append ( '<STR_LIT>' % msg . get_content_subtype ( ) ) <EOL> result . append ( self . body_parameters ( msg ) ) <EOL> result . append ( '<STR_LIT>' ) <EOL> result . append ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in msg : <EOL> result . append ( '<STR_LIT>' % msg [ '<STR_LIT>' ] . upper ( ) ) <EOL> else : <EOL> result . append ( '<STR_LIT>' ) <EOL> payload = msg . get_payload ( ) <EOL> result . append ( str ( len ( email . utils . fix_eols ( payload ) ) ) ) <EOL> if msg . get_content_type ( ) == "<STR_LIT>" : <EOL> result . append ( self . envelope ( msg ) ) <EOL> result . append ( self . bodystructure ( payload ) ) <EOL> result . append ( str ( len ( payload . splitlines ( ) ) ) ) <EOL> elif msg . get_content_maintype ( ) == "<STR_LIT:text>" : <EOL> result . append ( str ( len ( payload . splitlines ( ) ) ) ) <EOL> if not self . ext_data : <EOL> return '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( result ) <EOL> result . append ( '<STR_LIT>' ) <EOL> result . append ( self . body_disposition ( msg ) ) <EOL> result . append ( self . body_languages ( msg ) ) <EOL> result . append ( self . body_location ( msg ) ) <EOL> return '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( result ) </s>
