<s> """<STR_LIT>""" <EOL> import sys <EOL> from twisted . internet import protocol <EOL> from twisted . internet import ssl <EOL> from twisted . internet import defer <EOL> from twisted . internet import stdio <EOL> from twisted . mail import imap4 <EOL> from twisted . protocols import basic <EOL> from twisted . python import util <EOL> from twisted . python import log <EOL> class TrivialPrompter ( basic . LineReceiver ) : <EOL> from os import linesep as delimiter <EOL> promptDeferred = None <EOL> def prompt ( self , msg ) : <EOL> assert self . promptDeferred is None <EOL> self . display ( msg ) <EOL> self . promptDeferred = defer . Deferred ( ) <EOL> return self . promptDeferred <EOL> def display ( self , msg ) : <EOL> self . transport . write ( msg ) <EOL> def lineReceived ( self , line ) : <EOL> if self . promptDeferred is None : <EOL> return <EOL> d , self . promptDeferred = self . promptDeferred , None <EOL> d . callback ( line ) <EOL> class SimpleIMAP4Client ( imap4 . IMAP4Client ) : <EOL> """<STR_LIT>""" <EOL> greetDeferred = None <EOL> def serverGreeting ( self , caps ) : <EOL> self . serverCapabilities = caps <EOL> if self . greetDeferred is not None : <EOL> d , self . greetDeferred = self . greetDeferred , None <EOL> d . callback ( self ) <EOL> class SimpleIMAP4ClientFactory ( protocol . ClientFactory ) : <EOL> usedUp = False <EOL> protocol = SimpleIMAP4Client <EOL> def __init__ ( self , username , onConn ) : <EOL> self . ctx = ssl . ClientContextFactory ( ) <EOL> self . username = username <EOL> self . onConn = onConn <EOL> def buildProtocol ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> assert not self . usedUp <EOL> self . usedUp = True <EOL> p = self . protocol ( self . ctx ) <EOL> p . factory = self <EOL> p . greetDeferred = self . onConn <EOL> p . registerAuthenticator ( imap4 . PLAINAuthenticator ( self . username ) ) <EOL> p . registerAuthenticator ( imap4 . LOGINAuthenticator ( self . username ) ) <EOL> p . registerAuthenticator ( <EOL> imap4 . CramMD5ClientAuthenticator ( self . username ) ) <EOL> return p <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> d , self . onConn = self . onConn , None <EOL> d . errback ( reason ) <EOL> def cbServerGreeting ( proto , username , password ) : <EOL> """<STR_LIT>""" <EOL> tp = TrivialPrompter ( ) <EOL> stdio . StandardIO ( tp ) <EOL> proto . prompt = tp . prompt <EOL> proto . display = tp . display <EOL> return proto . authenticate ( password <EOL> ) . addCallback ( cbAuthentication , proto <EOL> ) . addErrback ( ebAuthentication , proto , username , password <EOL> ) <EOL> def ebConnection ( reason ) : <EOL> """<STR_LIT>""" <EOL> log . startLogging ( sys . stdout ) <EOL> log . err ( reason ) <EOL> return reason <EOL> def cbAuthentication ( result , proto ) : <EOL> """<STR_LIT>""" <EOL> return proto . list ( "<STR_LIT>" , "<STR_LIT:*>" <EOL> ) . addCallback ( cbMailboxList , proto <EOL> ) <EOL> def ebAuthentication ( failure , proto , username , password ) : <EOL> """<STR_LIT>""" <EOL> failure . trap ( imap4 . NoSupportedAuthentication ) <EOL> return proto . prompt ( <EOL> "<STR_LIT>" <EOL> ) . addCallback ( cbInsecureLogin , proto , username , password <EOL> ) <EOL> def cbInsecureLogin ( result , proto , username , password ) : <EOL> """<STR_LIT>""" <EOL> if result . lower ( ) == "<STR_LIT:y>" : <EOL> return proto . login ( username , password <EOL> ) . addCallback ( cbAuthentication , proto <EOL> ) <EOL> return defer . fail ( Exception ( "<STR_LIT>" ) ) <EOL> def cbMailboxList ( result , proto ) : <EOL> """<STR_LIT>""" <EOL> result = [ e [ <NUM_LIT:2> ] for e in result ] <EOL> s = '<STR_LIT:\n>' . join ( [ '<STR_LIT>' % ( n + <NUM_LIT:1> , m ) for ( n , m ) in zip ( range ( len ( result ) ) , result ) ] ) <EOL> if not s : <EOL> return defer . fail ( Exception ( "<STR_LIT>" ) ) <EOL> return proto . prompt ( s + "<STR_LIT>" <EOL> ) . addCallback ( cbPickMailbox , proto , result <EOL> ) <EOL> def cbPickMailbox ( result , proto , mboxes ) : <EOL> """<STR_LIT>""" <EOL> mbox = mboxes [ int ( result or '<STR_LIT:1>' ) - <NUM_LIT:1> ] <EOL> return proto . examine ( mbox <EOL> ) . addCallback ( cbExamineMbox , proto <EOL> ) <EOL> def cbExamineMbox ( result , proto ) : <EOL> """<STR_LIT>""" <EOL> return proto . fetchSpecific ( '<STR_LIT>' , <EOL> headerType = '<STR_LIT>' , <EOL> headerArgs = [ '<STR_LIT>' ] , <EOL> ) . addCallback ( cbFetch , proto <EOL> ) <EOL> def cbFetch ( result , proto ) : <EOL> """<STR_LIT>""" <EOL> if result : <EOL> keys = result . keys ( ) <EOL> keys . sort ( ) <EOL> for k in keys : <EOL> proto . display ( '<STR_LIT>' % ( k , result [ k ] [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> return proto . logout ( ) <EOL> def cbClose ( result ) : <EOL> """<STR_LIT>""" <EOL> from twisted . internet import reactor <EOL> reactor . stop ( ) <EOL> def main ( ) : <EOL> hostname = raw_input ( '<STR_LIT>' ) <EOL> port = raw_input ( '<STR_LIT>' ) <EOL> username = raw_input ( '<STR_LIT>' ) <EOL> password = util . getPassword ( '<STR_LIT>' ) <EOL> onConn = defer . Deferred ( <EOL> ) . addCallback ( cbServerGreeting , username , password <EOL> ) . addErrback ( ebConnection <EOL> ) . addBoth ( cbClose ) <EOL> factory = SimpleIMAP4ClientFactory ( username , onConn ) <EOL> from twisted . internet import reactor <EOL> if port == '<STR_LIT>' : <EOL> reactor . connectSSL ( hostname , int ( port ) , factory , ssl . ClientContextFactory ( ) ) <EOL> else : <EOL> if not port : <EOL> port = <NUM_LIT> <EOL> reactor . connectTCP ( hostname , int ( port ) , factory ) <EOL> reactor . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
