<s> """<STR_LIT>""" <EOL> import rfc822 <EOL> import base64 <EOL> import binascii <EOL> import hmac <EOL> import re <EOL> import copy <EOL> import tempfile <EOL> import string <EOL> import time <EOL> import random <EOL> import types <EOL> import email . Utils <EOL> try : <EOL> import cStringIO as StringIO <EOL> except : <EOL> import StringIO <EOL> from zope . interface import implements , Interface <EOL> from twisted . protocols import basic <EOL> from twisted . protocols import policies <EOL> from twisted . internet import defer <EOL> from twisted . internet import error <EOL> from twisted . internet . defer import maybeDeferred <EOL> from twisted . python import log , text <EOL> from twisted . internet import interfaces <EOL> from twisted import cred <EOL> import twisted . cred . error <EOL> import twisted . cred . credentials <EOL> _MONTH_NAMES = dict ( zip ( <EOL> range ( <NUM_LIT:1> , <NUM_LIT> ) , <EOL> "<STR_LIT>" . split ( ) ) ) <EOL> class MessageSet ( object ) : <EOL> """<STR_LIT>""" <EOL> _empty = [ ] <EOL> def __init__ ( self , start = _empty , end = _empty ) : <EOL> """<STR_LIT>""" <EOL> self . _last = self . _empty <EOL> self . ranges = [ ] <EOL> self . getnext = lambda x : x + <NUM_LIT:1> <EOL> if start is self . _empty : <EOL> return <EOL> if isinstance ( start , types . ListType ) : <EOL> self . ranges = start [ : ] <EOL> self . clean ( ) <EOL> else : <EOL> self . add ( start , end ) <EOL> def last ( ) : <EOL> def _setLast ( self , value ) : <EOL> if self . _last is not self . _empty : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _last = value <EOL> for i , ( l , h ) in enumerate ( self . ranges ) : <EOL> if l is not None : <EOL> break <EOL> l = value <EOL> if h is None : <EOL> h = value <EOL> if l > h : <EOL> l , h = h , l <EOL> self . ranges [ i ] = ( l , h ) <EOL> self . clean ( ) <EOL> def _getLast ( self ) : <EOL> return self . _last <EOL> doc = '''<STR_LIT>''' <EOL> return _getLast , _setLast , None , doc <EOL> last = property ( * last ( ) ) <EOL> def add ( self , start , end = _empty ) : <EOL> """<STR_LIT>""" <EOL> if end is self . _empty : <EOL> end = start <EOL> if self . _last is not self . _empty : <EOL> if start is None : <EOL> start = self . last <EOL> if end is None : <EOL> end = self . last <EOL> if start > end : <EOL> start , end = end , start <EOL> self . ranges . append ( ( start , end ) ) <EOL> self . clean ( ) <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , MessageSet ) : <EOL> ranges = self . ranges + other . ranges <EOL> return MessageSet ( ranges ) <EOL> else : <EOL> res = MessageSet ( self . ranges ) <EOL> try : <EOL> res . add ( * other ) <EOL> except TypeError : <EOL> res . add ( other ) <EOL> return res <EOL> def extend ( self , other ) : <EOL> if isinstance ( other , MessageSet ) : <EOL> self . ranges . extend ( other . ranges ) <EOL> self . clean ( ) <EOL> else : <EOL> try : <EOL> self . add ( * other ) <EOL> except TypeError : <EOL> self . add ( other ) <EOL> return self <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> self . ranges . sort ( ) <EOL> oldl , oldh = None , None <EOL> for i , ( l , h ) in enumerate ( self . ranges ) : <EOL> if l is None : <EOL> continue <EOL> if oldl is not None and l <= oldh + <NUM_LIT:1> : <EOL> l = oldl <EOL> h = max ( oldh , h ) <EOL> self . ranges [ i - <NUM_LIT:1> ] = None <EOL> self . ranges [ i ] = ( l , h ) <EOL> oldl , oldh = l , h <EOL> self . ranges = filter ( None , self . ranges ) <EOL> def __contains__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> for l , h in self . ranges : <EOL> if l is None : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> if l <= value <= h : <EOL> return True <EOL> return False <EOL> def _iterator ( self ) : <EOL> for l , h in self . ranges : <EOL> l = self . getnext ( l - <NUM_LIT:1> ) <EOL> while l <= h : <EOL> yield l <EOL> l = self . getnext ( l ) <EOL> if l is None : <EOL> break <EOL> def __iter__ ( self ) : <EOL> if self . ranges and self . ranges [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return self . _iterator ( ) <EOL> def __len__ ( self ) : <EOL> res = <NUM_LIT:0> <EOL> for l , h in self . ranges : <EOL> if l is None : <EOL> if h is None : <EOL> res += <NUM_LIT:1> <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> else : <EOL> res += ( h - l ) + <NUM_LIT:1> <EOL> return res <EOL> def __str__ ( self ) : <EOL> p = [ ] <EOL> for low , high in self . ranges : <EOL> if low == high : <EOL> if low is None : <EOL> p . append ( '<STR_LIT:*>' ) <EOL> else : <EOL> p . append ( str ( low ) ) <EOL> elif low is None : <EOL> p . append ( '<STR_LIT>' % ( high , ) ) <EOL> else : <EOL> p . append ( '<STR_LIT>' % ( low , high ) ) <EOL> return '<STR_LIT:U+002C>' . join ( p ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( str ( self ) , ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , MessageSet ) : <EOL> return self . ranges == other . ranges <EOL> return False <EOL> class LiteralString : <EOL> def __init__ ( self , size , defered ) : <EOL> self . size = size <EOL> self . data = [ ] <EOL> self . defer = defered <EOL> def write ( self , data ) : <EOL> self . size -= len ( data ) <EOL> passon = None <EOL> if self . size > <NUM_LIT:0> : <EOL> self . data . append ( data ) <EOL> else : <EOL> if self . size : <EOL> data , passon = data [ : self . size ] , data [ self . size : ] <EOL> else : <EOL> passon = '<STR_LIT>' <EOL> if data : <EOL> self . data . append ( data ) <EOL> return passon <EOL> def callback ( self , line ) : <EOL> """<STR_LIT>""" <EOL> self . defer . callback ( ( '<STR_LIT>' . join ( self . data ) , line ) ) <EOL> class LiteralFile : <EOL> _memoryFileLimit = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:10> <EOL> def __init__ ( self , size , defered ) : <EOL> self . size = size <EOL> self . defer = defered <EOL> if size > self . _memoryFileLimit : <EOL> self . data = tempfile . TemporaryFile ( ) <EOL> else : <EOL> self . data = StringIO . StringIO ( ) <EOL> def write ( self , data ) : <EOL> self . size -= len ( data ) <EOL> passon = None <EOL> if self . size > <NUM_LIT:0> : <EOL> self . data . write ( data ) <EOL> else : <EOL> if self . size : <EOL> data , passon = data [ : self . size ] , data [ self . size : ] <EOL> else : <EOL> passon = '<STR_LIT>' <EOL> if data : <EOL> self . data . write ( data ) <EOL> return passon <EOL> def callback ( self , line ) : <EOL> """<STR_LIT>""" <EOL> self . data . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . defer . callback ( ( self . data , line ) ) <EOL> class WriteBuffer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , transport , size = <NUM_LIT> ) : <EOL> self . bufferSize = size <EOL> self . transport = transport <EOL> self . _length = <NUM_LIT:0> <EOL> self . _writes = [ ] <EOL> def write ( self , s ) : <EOL> self . _length += len ( s ) <EOL> self . _writes . append ( s ) <EOL> if self . _length > self . bufferSize : <EOL> self . flush ( ) <EOL> def flush ( self ) : <EOL> if self . _writes : <EOL> self . transport . writeSequence ( self . _writes ) <EOL> self . _writes = [ ] <EOL> self . _length = <NUM_LIT:0> <EOL> class Command : <EOL> _1_RESPONSES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> _2_RESPONSES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> _OK_RESPONSES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> defer = None <EOL> def __init__ ( self , command , args = None , wantResponse = ( ) , <EOL> continuation = None , * contArgs , ** contKw ) : <EOL> self . command = command <EOL> self . args = args <EOL> self . wantResponse = wantResponse <EOL> self . continuation = lambda x : continuation ( x , * contArgs , ** contKw ) <EOL> self . lines = [ ] <EOL> def format ( self , tag ) : <EOL> if self . args is None : <EOL> return '<STR_LIT:U+0020>' . join ( ( tag , self . command ) ) <EOL> return '<STR_LIT:U+0020>' . join ( ( tag , self . command , self . args ) ) <EOL> def finish ( self , lastLine , unusedCallback ) : <EOL> send = [ ] <EOL> unuse = [ ] <EOL> for L in self . lines : <EOL> names = parseNestedParens ( L ) <EOL> N = len ( names ) <EOL> if ( N >= <NUM_LIT:1> and names [ <NUM_LIT:0> ] in self . _1_RESPONSES or <EOL> N >= <NUM_LIT:2> and names [ <NUM_LIT:1> ] in self . _2_RESPONSES or <EOL> N >= <NUM_LIT:2> and names [ <NUM_LIT:0> ] == '<STR_LIT:OK>' and isinstance ( names [ <NUM_LIT:1> ] , types . ListType ) and names [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] in self . _OK_RESPONSES ) : <EOL> send . append ( names ) <EOL> else : <EOL> unuse . append ( names ) <EOL> d , self . defer = self . defer , None <EOL> d . callback ( ( send , lastLine ) ) <EOL> if unuse : <EOL> unusedCallback ( unuse ) <EOL> class LOGINCredentials ( cred . credentials . UsernamePassword ) : <EOL> def __init__ ( self ) : <EOL> self . challenges = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . responses = [ '<STR_LIT:password>' , '<STR_LIT:username>' ] <EOL> cred . credentials . UsernamePassword . __init__ ( self , None , None ) <EOL> def getChallenge ( self ) : <EOL> return self . challenges . pop ( ) <EOL> def setResponse ( self , response ) : <EOL> setattr ( self , self . responses . pop ( ) , response ) <EOL> def moreChallenges ( self ) : <EOL> return bool ( self . challenges ) <EOL> class PLAINCredentials ( cred . credentials . UsernamePassword ) : <EOL> def __init__ ( self ) : <EOL> cred . credentials . UsernamePassword . __init__ ( self , None , None ) <EOL> def getChallenge ( self ) : <EOL> return '<STR_LIT>' <EOL> def setResponse ( self , response ) : <EOL> parts = response . split ( '<STR_LIT>' ) <EOL> if len ( parts ) != <NUM_LIT:3> : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> useless , self . username , self . password = parts <EOL> def moreChallenges ( self ) : <EOL> return False <EOL> class IMAP4Exception ( Exception ) : <EOL> def __init__ ( self , * args ) : <EOL> Exception . __init__ ( self , * args ) <EOL> class IllegalClientResponse ( IMAP4Exception ) : pass <EOL> class IllegalOperation ( IMAP4Exception ) : pass <EOL> class IllegalMailboxEncoding ( IMAP4Exception ) : pass <EOL> class IMailboxListener ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def modeChanged ( writeable ) : <EOL> """<STR_LIT>""" <EOL> def flagsChanged ( newFlags ) : <EOL> """<STR_LIT>""" <EOL> def newMessages ( exists , recent ) : <EOL> """<STR_LIT>""" <EOL> class IMAP4Server ( basic . LineReceiver , policies . TimeoutMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IMailboxListener ) <EOL> IDENT = '<STR_LIT>' <EOL> timeOut = <NUM_LIT> <EOL> POSTAUTH_TIMEOUT = <NUM_LIT> * <NUM_LIT:30> <EOL> startedTLS = False <EOL> canStartTLS = False <EOL> tags = None <EOL> portal = None <EOL> account = None <EOL> _onLogout = None <EOL> mbox = None <EOL> _pendingLiteral = None <EOL> _literalStringLimit = <NUM_LIT> <EOL> challengers = None <EOL> _requiresLastMessageInfo = set ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> state = '<STR_LIT>' <EOL> parseState = '<STR_LIT>' <EOL> def __init__ ( self , chal = None , contextFactory = None , scheduler = None ) : <EOL> if chal is None : <EOL> chal = { } <EOL> self . challengers = chal <EOL> self . ctx = contextFactory <EOL> if scheduler is None : <EOL> scheduler = iterateInReactor <EOL> self . _scheduler = scheduler <EOL> self . _queuedAsync = [ ] <EOL> def capabilities ( self ) : <EOL> cap = { '<STR_LIT>' : self . challengers . keys ( ) } <EOL> if self . ctx and self . canStartTLS : <EOL> if not self . startedTLS and interfaces . ISSLTransport ( self . transport , None ) is None : <EOL> cap [ '<STR_LIT>' ] = None <EOL> cap [ '<STR_LIT>' ] = None <EOL> cap [ '<STR_LIT>' ] = None <EOL> cap [ '<STR_LIT>' ] = None <EOL> return cap <EOL> def connectionMade ( self ) : <EOL> self . tags = { } <EOL> self . canStartTLS = interfaces . ITLSTransport ( self . transport , None ) is not None <EOL> self . setTimeout ( self . timeOut ) <EOL> self . sendServerGreeting ( ) <EOL> def connectionLost ( self , reason ) : <EOL> self . setTimeout ( None ) <EOL> if self . _onLogout : <EOL> self . _onLogout ( ) <EOL> self . _onLogout = None <EOL> def timeoutConnection ( self ) : <EOL> self . sendLine ( '<STR_LIT>' ) <EOL> self . transport . loseConnection ( ) <EOL> if self . mbox : <EOL> self . mbox . removeListener ( self ) <EOL> cmbx = ICloseableMailbox ( self . mbox , None ) <EOL> if cmbx is not None : <EOL> maybeDeferred ( cmbx . close ) . addErrback ( log . err ) <EOL> self . mbox = None <EOL> self . state = '<STR_LIT>' <EOL> def rawDataReceived ( self , data ) : <EOL> self . resetTimeout ( ) <EOL> passon = self . _pendingLiteral . write ( data ) <EOL> if passon is not None : <EOL> self . setLineMode ( passon ) <EOL> blocked = None <EOL> def _unblock ( self ) : <EOL> commands = self . blocked <EOL> self . blocked = None <EOL> while commands and self . blocked is None : <EOL> self . lineReceived ( commands . pop ( <NUM_LIT:0> ) ) <EOL> if self . blocked is not None : <EOL> self . blocked . extend ( commands ) <EOL> def lineReceived ( self , line ) : <EOL> if self . blocked is not None : <EOL> self . blocked . append ( line ) <EOL> return <EOL> self . resetTimeout ( ) <EOL> f = getattr ( self , '<STR_LIT>' + self . parseState ) <EOL> try : <EOL> f ( line ) <EOL> except Exception , e : <EOL> self . sendUntaggedResponse ( '<STR_LIT>' + str ( e ) ) <EOL> log . err ( ) <EOL> def parse_command ( self , line ) : <EOL> args = line . split ( None , <NUM_LIT:2> ) <EOL> rest = None <EOL> if len ( args ) == <NUM_LIT:3> : <EOL> tag , cmd , rest = args <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> tag , cmd = args <EOL> elif len ( args ) == <NUM_LIT:1> : <EOL> tag = args [ <NUM_LIT:0> ] <EOL> self . sendBadResponse ( tag , '<STR_LIT>' ) <EOL> return None <EOL> else : <EOL> self . sendBadResponse ( None , '<STR_LIT>' ) <EOL> return None <EOL> cmd = cmd . upper ( ) <EOL> try : <EOL> return self . dispatchCommand ( tag , cmd , rest ) <EOL> except IllegalClientResponse , e : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( e ) ) <EOL> except IllegalOperation , e : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' + str ( e ) ) <EOL> except IllegalMailboxEncoding , e : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' + str ( e ) ) <EOL> def parse_pending ( self , line ) : <EOL> d = self . _pendingLiteral <EOL> self . _pendingLiteral = None <EOL> self . parseState = '<STR_LIT>' <EOL> d . callback ( line ) <EOL> def dispatchCommand ( self , tag , cmd , rest , uid = None ) : <EOL> f = self . lookupCommand ( cmd ) <EOL> if f : <EOL> fn = f [ <NUM_LIT:0> ] <EOL> parseargs = f [ <NUM_LIT:1> : ] <EOL> self . __doCommand ( tag , fn , [ self , tag ] , parseargs , rest , uid ) <EOL> else : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' ) <EOL> def lookupCommand ( self , cmd ) : <EOL> return getattr ( self , '<STR_LIT:_>' . join ( ( self . state , cmd . upper ( ) ) ) , None ) <EOL> def __doCommand ( self , tag , handler , args , parseargs , line , uid ) : <EOL> for ( i , arg ) in enumerate ( parseargs ) : <EOL> if callable ( arg ) : <EOL> parseargs = parseargs [ i + <NUM_LIT:1> : ] <EOL> maybeDeferred ( arg , self , line ) . addCallback ( <EOL> self . __cbDispatch , tag , handler , args , <EOL> parseargs , uid ) . addErrback ( self . __ebDispatch , tag ) <EOL> return <EOL> else : <EOL> args . append ( arg ) <EOL> if line : <EOL> raise IllegalClientResponse ( "<STR_LIT>" + repr ( line ) ) <EOL> if uid is not None : <EOL> handler ( uid = uid , * args ) <EOL> else : <EOL> handler ( * args ) <EOL> def __cbDispatch ( self , ( arg , rest ) , tag , fn , args , parseargs , uid ) : <EOL> args . append ( arg ) <EOL> self . __doCommand ( tag , fn , args , parseargs , rest , uid ) <EOL> def __ebDispatch ( self , failure , tag ) : <EOL> if failure . check ( IllegalClientResponse ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> elif failure . check ( IllegalOperation ) : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' + <EOL> str ( failure . value ) ) <EOL> elif failure . check ( IllegalMailboxEncoding ) : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' + <EOL> str ( failure . value ) ) <EOL> else : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> log . err ( failure ) <EOL> def _stringLiteral ( self , size ) : <EOL> if size > self . _literalStringLimit : <EOL> raise IllegalClientResponse ( <EOL> "<STR_LIT>" % <EOL> ( self . _literalStringLimit , ) ) <EOL> d = defer . Deferred ( ) <EOL> self . parseState = '<STR_LIT>' <EOL> self . _pendingLiteral = LiteralString ( size , d ) <EOL> self . sendContinuationRequest ( '<STR_LIT>' % size ) <EOL> self . setRawMode ( ) <EOL> return d <EOL> def _fileLiteral ( self , size ) : <EOL> d = defer . Deferred ( ) <EOL> self . parseState = '<STR_LIT>' <EOL> self . _pendingLiteral = LiteralFile ( size , d ) <EOL> self . sendContinuationRequest ( '<STR_LIT>' % size ) <EOL> self . setRawMode ( ) <EOL> return d <EOL> def arg_astring ( self , line ) : <EOL> """<STR_LIT>""" <EOL> line = line . strip ( ) <EOL> if not line : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> d = None <EOL> arg , rest = None , None <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT:">' : <EOL> try : <EOL> spam , arg , rest = line . split ( '<STR_LIT:">' , <NUM_LIT:2> ) <EOL> rest = rest [ <NUM_LIT:1> : ] <EOL> except ValueError : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> elif line [ <NUM_LIT:0> ] == '<STR_LIT:{>' : <EOL> if line [ - <NUM_LIT:1> ] != '<STR_LIT:}>' : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> try : <EOL> size = int ( line [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> raise IllegalClientResponse ( "<STR_LIT>" + line [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> d = self . _stringLiteral ( size ) <EOL> else : <EOL> arg = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if len ( arg ) == <NUM_LIT:1> : <EOL> arg . append ( '<STR_LIT>' ) <EOL> arg , rest = arg <EOL> return d or ( arg , rest ) <EOL> atomre = re . compile ( r'<STR_LIT>' ) <EOL> def arg_atom ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if not line : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> m = self . atomre . match ( line ) <EOL> if m : <EOL> return m . group ( '<STR_LIT>' ) , m . group ( '<STR_LIT>' ) <EOL> else : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> def arg_plist ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if not line : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> if line [ <NUM_LIT:0> ] != "<STR_LIT:(>" : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> i = line . find ( "<STR_LIT:)>" ) <EOL> if i == - <NUM_LIT:1> : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> return ( parseNestedParens ( line [ <NUM_LIT:1> : i ] , <NUM_LIT:0> ) , line [ i + <NUM_LIT:2> : ] ) <EOL> def arg_literal ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if not line : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> if line [ <NUM_LIT:0> ] != '<STR_LIT:{>' : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> if line [ - <NUM_LIT:1> ] != '<STR_LIT:}>' : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> try : <EOL> size = int ( line [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> raise IllegalClientResponse ( "<STR_LIT>" + line [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> return self . _fileLiteral ( size ) <EOL> def arg_searchkeys ( self , line ) : <EOL> """<STR_LIT>""" <EOL> query = parseNestedParens ( line ) <EOL> return ( query , '<STR_LIT>' ) <EOL> def arg_seqset ( self , line ) : <EOL> """<STR_LIT>""" <EOL> rest = '<STR_LIT>' <EOL> arg = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> if len ( arg ) == <NUM_LIT:2> : <EOL> rest = arg [ <NUM_LIT:1> ] <EOL> arg = arg [ <NUM_LIT:0> ] <EOL> try : <EOL> return ( parseIdList ( arg ) , rest ) <EOL> except IllegalIdentifierError , e : <EOL> raise IllegalClientResponse ( "<STR_LIT>" + str ( e ) ) <EOL> def arg_fetchatt ( self , line ) : <EOL> """<STR_LIT>""" <EOL> p = _FetchParser ( ) <EOL> p . parseString ( line ) <EOL> return ( p . result , '<STR_LIT>' ) <EOL> def arg_flaglist ( self , line ) : <EOL> """<STR_LIT>""" <EOL> flags = [ ] <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT:(>' : <EOL> if line [ - <NUM_LIT:1> ] != '<STR_LIT:)>' : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> line = line [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> while line : <EOL> m = self . atomre . search ( line ) <EOL> if not m : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT:\\>' and m . start ( ) == <NUM_LIT:1> : <EOL> flags . append ( '<STR_LIT:\\>' + m . group ( '<STR_LIT>' ) ) <EOL> elif m . start ( ) == <NUM_LIT:0> : <EOL> flags . append ( m . group ( '<STR_LIT>' ) ) <EOL> else : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> line = m . group ( '<STR_LIT>' ) <EOL> return ( flags , '<STR_LIT>' ) <EOL> def arg_line ( self , line ) : <EOL> """<STR_LIT>""" <EOL> return ( line , '<STR_LIT>' ) <EOL> def opt_plist ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if line . startswith ( '<STR_LIT:(>' ) : <EOL> return self . arg_plist ( line ) <EOL> else : <EOL> return ( None , line ) <EOL> def opt_datetime ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if line . startswith ( '<STR_LIT:">' ) : <EOL> try : <EOL> spam , date , rest = line . split ( '<STR_LIT:">' , <NUM_LIT:2> ) <EOL> except IndexError : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> return ( date , rest [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> return ( None , line ) <EOL> def opt_charset ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if line [ : <NUM_LIT:7> ] . upper ( ) == '<STR_LIT>' : <EOL> arg = line . split ( '<STR_LIT:U+0020>' , <NUM_LIT:2> ) <EOL> if len ( arg ) == <NUM_LIT:1> : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> if len ( arg ) == <NUM_LIT:2> : <EOL> arg . append ( '<STR_LIT>' ) <EOL> spam , arg , rest = arg <EOL> return ( arg , rest ) <EOL> else : <EOL> return ( None , line ) <EOL> def sendServerGreeting ( self ) : <EOL> msg = '<STR_LIT>' % ( '<STR_LIT:U+0020>' . join ( self . listCapabilities ( ) ) , self . IDENT ) <EOL> self . sendPositiveResponse ( message = msg ) <EOL> def sendBadResponse ( self , tag = None , message = '<STR_LIT>' ) : <EOL> self . _respond ( '<STR_LIT>' , tag , message ) <EOL> def sendPositiveResponse ( self , tag = None , message = '<STR_LIT>' ) : <EOL> self . _respond ( '<STR_LIT:OK>' , tag , message ) <EOL> def sendNegativeResponse ( self , tag = None , message = '<STR_LIT>' ) : <EOL> self . _respond ( '<STR_LIT>' , tag , message ) <EOL> def sendUntaggedResponse ( self , message , async = False ) : <EOL> if not async or ( self . blocked is None ) : <EOL> self . _respond ( message , None , None ) <EOL> else : <EOL> self . _queuedAsync . append ( message ) <EOL> def sendContinuationRequest ( self , msg = '<STR_LIT>' ) : <EOL> if msg : <EOL> self . sendLine ( '<STR_LIT>' + msg ) <EOL> else : <EOL> self . sendLine ( '<STR_LIT:+>' ) <EOL> def _respond ( self , state , tag , message ) : <EOL> if state in ( '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' ) and self . _queuedAsync : <EOL> lines = self . _queuedAsync <EOL> self . _queuedAsync = [ ] <EOL> for msg in lines : <EOL> self . _respond ( msg , None , None ) <EOL> if not tag : <EOL> tag = '<STR_LIT:*>' <EOL> if message : <EOL> self . sendLine ( '<STR_LIT:U+0020>' . join ( ( tag , state , message ) ) ) <EOL> else : <EOL> self . sendLine ( '<STR_LIT:U+0020>' . join ( ( tag , state ) ) ) <EOL> def listCapabilities ( self ) : <EOL> caps = [ '<STR_LIT>' ] <EOL> for c , v in self . capabilities ( ) . iteritems ( ) : <EOL> if v is None : <EOL> caps . append ( c ) <EOL> elif len ( v ) : <EOL> caps . extend ( [ ( '<STR_LIT>' % ( c , cap ) ) for cap in v ] ) <EOL> return caps <EOL> def do_CAPABILITY ( self , tag ) : <EOL> self . sendUntaggedResponse ( '<STR_LIT>' + '<STR_LIT:U+0020>' . join ( self . listCapabilities ( ) ) ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> unauth_CAPABILITY = ( do_CAPABILITY , ) <EOL> auth_CAPABILITY = unauth_CAPABILITY <EOL> select_CAPABILITY = unauth_CAPABILITY <EOL> logout_CAPABILITY = unauth_CAPABILITY <EOL> def do_LOGOUT ( self , tag ) : <EOL> self . sendUntaggedResponse ( '<STR_LIT>' ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> self . transport . loseConnection ( ) <EOL> unauth_LOGOUT = ( do_LOGOUT , ) <EOL> auth_LOGOUT = unauth_LOGOUT <EOL> select_LOGOUT = unauth_LOGOUT <EOL> logout_LOGOUT = unauth_LOGOUT <EOL> def do_NOOP ( self , tag ) : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> unauth_NOOP = ( do_NOOP , ) <EOL> auth_NOOP = unauth_NOOP <EOL> select_NOOP = unauth_NOOP <EOL> logout_NOOP = unauth_NOOP <EOL> def do_AUTHENTICATE ( self , tag , args ) : <EOL> args = args . upper ( ) . strip ( ) <EOL> if args not in self . challengers : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> else : <EOL> self . authenticate ( self . challengers [ args ] ( ) , tag ) <EOL> unauth_AUTHENTICATE = ( do_AUTHENTICATE , arg_atom ) <EOL> def authenticate ( self , chal , tag ) : <EOL> if self . portal is None : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> return <EOL> self . _setupChallenge ( chal , tag ) <EOL> def _setupChallenge ( self , chal , tag ) : <EOL> try : <EOL> challenge = chal . getChallenge ( ) <EOL> except Exception , e : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( e ) ) <EOL> else : <EOL> coded = base64 . encodestring ( challenge ) [ : - <NUM_LIT:1> ] <EOL> self . parseState = '<STR_LIT>' <EOL> self . _pendingLiteral = defer . Deferred ( ) <EOL> self . sendContinuationRequest ( coded ) <EOL> self . _pendingLiteral . addCallback ( self . __cbAuthChunk , chal , tag ) <EOL> self . _pendingLiteral . addErrback ( self . __ebAuthChunk , tag ) <EOL> def __cbAuthChunk ( self , result , chal , tag ) : <EOL> try : <EOL> uncoded = base64 . decodestring ( result ) <EOL> except binascii . Error : <EOL> raise IllegalClientResponse ( "<STR_LIT>" ) <EOL> chal . setResponse ( uncoded ) <EOL> if chal . moreChallenges ( ) : <EOL> self . _setupChallenge ( chal , tag ) <EOL> else : <EOL> self . portal . login ( chal , None , IAccount ) . addCallbacks ( <EOL> self . __cbAuthResp , <EOL> self . __ebAuthResp , <EOL> ( tag , ) , None , ( tag , ) , None <EOL> ) <EOL> def __cbAuthResp ( self , ( iface , avatar , logout ) , tag ) : <EOL> assert iface is IAccount , "<STR_LIT>" <EOL> self . account = avatar <EOL> self . state = '<STR_LIT>' <EOL> self . _onLogout = logout <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> self . setTimeout ( self . POSTAUTH_TIMEOUT ) <EOL> def __ebAuthResp ( self , failure , tag ) : <EOL> if failure . check ( cred . error . UnauthorizedLogin ) : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> elif failure . check ( cred . error . UnhandledCredentials ) : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> else : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' ) <EOL> log . err ( failure ) <EOL> def __ebAuthChunk ( self , failure , tag ) : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> def do_STARTTLS ( self , tag ) : <EOL> if self . startedTLS : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> elif self . ctx and self . canStartTLS : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> self . transport . startTLS ( self . ctx ) <EOL> self . startedTLS = True <EOL> self . challengers = self . challengers . copy ( ) <EOL> if '<STR_LIT>' not in self . challengers : <EOL> self . challengers [ '<STR_LIT>' ] = LOGINCredentials <EOL> if '<STR_LIT>' not in self . challengers : <EOL> self . challengers [ '<STR_LIT>' ] = PLAINCredentials <EOL> else : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> unauth_STARTTLS = ( do_STARTTLS , ) <EOL> def do_LOGIN ( self , tag , user , passwd ) : <EOL> if '<STR_LIT>' in self . capabilities ( ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' ) <EOL> return <EOL> maybeDeferred ( self . authenticateLogin , user , passwd <EOL> ) . addCallback ( self . __cbLogin , tag <EOL> ) . addErrback ( self . __ebLogin , tag <EOL> ) <EOL> unauth_LOGIN = ( do_LOGIN , arg_astring , arg_astring ) <EOL> def authenticateLogin ( self , user , passwd ) : <EOL> """<STR_LIT>""" <EOL> if self . portal : <EOL> return self . portal . login ( <EOL> cred . credentials . UsernamePassword ( user , passwd ) , <EOL> None , IAccount <EOL> ) <EOL> raise cred . error . UnauthorizedLogin ( ) <EOL> def __cbLogin ( self , ( iface , avatar , logout ) , tag ) : <EOL> if iface is not IAccount : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' ) <EOL> log . err ( "<STR_LIT>" % ( iface , ) ) <EOL> else : <EOL> self . account = avatar <EOL> self . _onLogout = logout <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> self . state = '<STR_LIT>' <EOL> self . setTimeout ( self . POSTAUTH_TIMEOUT ) <EOL> def __ebLogin ( self , failure , tag ) : <EOL> if failure . check ( cred . error . UnauthorizedLogin ) : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> else : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> log . err ( failure ) <EOL> def do_NAMESPACE ( self , tag ) : <EOL> personal = public = shared = None <EOL> np = INamespacePresenter ( self . account , None ) <EOL> if np is not None : <EOL> personal = np . getPersonalNamespaces ( ) <EOL> public = np . getSharedNamespaces ( ) <EOL> shared = np . getSharedNamespaces ( ) <EOL> self . sendUntaggedResponse ( '<STR_LIT>' + collapseNestedLists ( [ personal , public , shared ] ) ) <EOL> self . sendPositiveResponse ( tag , "<STR_LIT>" ) <EOL> auth_NAMESPACE = ( do_NAMESPACE , ) <EOL> select_NAMESPACE = auth_NAMESPACE <EOL> def _parseMbox ( self , name ) : <EOL> if isinstance ( name , unicode ) : <EOL> return name <EOL> try : <EOL> return name . decode ( '<STR_LIT>' ) <EOL> except : <EOL> log . err ( ) <EOL> raise IllegalMailboxEncoding ( name ) <EOL> def _selectWork ( self , tag , name , rw , cmdName ) : <EOL> if self . mbox : <EOL> self . mbox . removeListener ( self ) <EOL> cmbx = ICloseableMailbox ( self . mbox , None ) <EOL> if cmbx is not None : <EOL> maybeDeferred ( cmbx . close ) . addErrback ( log . err ) <EOL> self . mbox = None <EOL> self . state = '<STR_LIT>' <EOL> name = self . _parseMbox ( name ) <EOL> maybeDeferred ( self . account . select , self . _parseMbox ( name ) , rw <EOL> ) . addCallback ( self . _cbSelectWork , cmdName , tag <EOL> ) . addErrback ( self . _ebSelectWork , cmdName , tag <EOL> ) <EOL> def _ebSelectWork ( self , failure , cmdName , tag ) : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" % ( cmdName , ) ) <EOL> log . err ( failure ) <EOL> def _cbSelectWork ( self , mbox , cmdName , tag ) : <EOL> if mbox is None : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> return <EOL> if '<STR_LIT>' in [ s . lower ( ) for s in mbox . getFlags ( ) ] : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> return <EOL> flags = mbox . getFlags ( ) <EOL> self . sendUntaggedResponse ( str ( mbox . getMessageCount ( ) ) + '<STR_LIT>' ) <EOL> self . sendUntaggedResponse ( str ( mbox . getRecentCount ( ) ) + '<STR_LIT>' ) <EOL> self . sendUntaggedResponse ( '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( flags ) ) <EOL> self . sendPositiveResponse ( None , '<STR_LIT>' % mbox . getUIDValidity ( ) ) <EOL> s = mbox . isWriteable ( ) and '<STR_LIT>' or '<STR_LIT>' <EOL> mbox . addListener ( self ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' % ( s , cmdName ) ) <EOL> self . state = '<STR_LIT>' <EOL> self . mbox = mbox <EOL> auth_SELECT = ( _selectWork , arg_astring , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> select_SELECT = auth_SELECT <EOL> auth_EXAMINE = ( _selectWork , arg_astring , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> select_EXAMINE = auth_EXAMINE <EOL> def do_IDLE ( self , tag ) : <EOL> self . sendContinuationRequest ( None ) <EOL> self . parseTag = tag <EOL> self . lastState = self . parseState <EOL> self . parseState = '<STR_LIT>' <EOL> def parse_idle ( self , * args ) : <EOL> self . parseState = self . lastState <EOL> del self . lastState <EOL> self . sendPositiveResponse ( self . parseTag , "<STR_LIT>" ) <EOL> del self . parseTag <EOL> select_IDLE = ( do_IDLE , ) <EOL> auth_IDLE = select_IDLE <EOL> def do_CREATE ( self , tag , name ) : <EOL> name = self . _parseMbox ( name ) <EOL> try : <EOL> result = self . account . create ( name ) <EOL> except MailboxException , c : <EOL> self . sendNegativeResponse ( tag , str ( c ) ) <EOL> except : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" ) <EOL> log . err ( ) <EOL> else : <EOL> if result : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> else : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> auth_CREATE = ( do_CREATE , arg_astring ) <EOL> select_CREATE = auth_CREATE <EOL> def do_DELETE ( self , tag , name ) : <EOL> name = self . _parseMbox ( name ) <EOL> if name . lower ( ) == '<STR_LIT>' : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> return <EOL> try : <EOL> self . account . delete ( name ) <EOL> except MailboxException , m : <EOL> self . sendNegativeResponse ( tag , str ( m ) ) <EOL> except : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" ) <EOL> log . err ( ) <EOL> else : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> auth_DELETE = ( do_DELETE , arg_astring ) <EOL> select_DELETE = auth_DELETE <EOL> def do_RENAME ( self , tag , oldname , newname ) : <EOL> oldname , newname = [ self . _parseMbox ( n ) for n in oldname , newname ] <EOL> if oldname . lower ( ) == '<STR_LIT>' or newname . lower ( ) == '<STR_LIT>' : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> return <EOL> try : <EOL> self . account . rename ( oldname , newname ) <EOL> except TypeError : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' ) <EOL> except MailboxException , m : <EOL> self . sendNegativeResponse ( tag , str ( m ) ) <EOL> except : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" ) <EOL> log . err ( ) <EOL> else : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> auth_RENAME = ( do_RENAME , arg_astring , arg_astring ) <EOL> select_RENAME = auth_RENAME <EOL> def do_SUBSCRIBE ( self , tag , name ) : <EOL> name = self . _parseMbox ( name ) <EOL> try : <EOL> self . account . subscribe ( name ) <EOL> except MailboxException , m : <EOL> self . sendNegativeResponse ( tag , str ( m ) ) <EOL> except : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" ) <EOL> log . err ( ) <EOL> else : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> auth_SUBSCRIBE = ( do_SUBSCRIBE , arg_astring ) <EOL> select_SUBSCRIBE = auth_SUBSCRIBE <EOL> def do_UNSUBSCRIBE ( self , tag , name ) : <EOL> name = self . _parseMbox ( name ) <EOL> try : <EOL> self . account . unsubscribe ( name ) <EOL> except MailboxException , m : <EOL> self . sendNegativeResponse ( tag , str ( m ) ) <EOL> except : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" ) <EOL> log . err ( ) <EOL> else : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> auth_UNSUBSCRIBE = ( do_UNSUBSCRIBE , arg_astring ) <EOL> select_UNSUBSCRIBE = auth_UNSUBSCRIBE <EOL> def _listWork ( self , tag , ref , mbox , sub , cmdName ) : <EOL> mbox = self . _parseMbox ( mbox ) <EOL> maybeDeferred ( self . account . listMailboxes , ref , mbox <EOL> ) . addCallback ( self . _cbListWork , tag , sub , cmdName <EOL> ) . addErrback ( self . _ebListWork , tag <EOL> ) <EOL> def _cbListWork ( self , mailboxes , tag , sub , cmdName ) : <EOL> for ( name , box ) in mailboxes : <EOL> if not sub or self . account . isSubscribed ( name ) : <EOL> flags = box . getFlags ( ) <EOL> delim = box . getHierarchicalDelimiter ( ) <EOL> resp = ( DontQuoteMe ( cmdName ) , map ( DontQuoteMe , flags ) , delim , name . encode ( '<STR_LIT>' ) ) <EOL> self . sendUntaggedResponse ( collapseNestedLists ( resp ) ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' % ( cmdName , ) ) <EOL> def _ebListWork ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" ) <EOL> log . err ( failure ) <EOL> auth_LIST = ( _listWork , arg_astring , arg_astring , <NUM_LIT:0> , '<STR_LIT>' ) <EOL> select_LIST = auth_LIST <EOL> auth_LSUB = ( _listWork , arg_astring , arg_astring , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> select_LSUB = auth_LSUB <EOL> def do_STATUS ( self , tag , mailbox , names ) : <EOL> mailbox = self . _parseMbox ( mailbox ) <EOL> maybeDeferred ( self . account . select , mailbox , <NUM_LIT:0> <EOL> ) . addCallback ( self . _cbStatusGotMailbox , tag , mailbox , names <EOL> ) . addErrback ( self . _ebStatusGotMailbox , tag <EOL> ) <EOL> def _cbStatusGotMailbox ( self , mbox , tag , mailbox , names ) : <EOL> if mbox : <EOL> maybeDeferred ( mbox . requestStatus , names ) . addCallbacks ( <EOL> self . __cbStatus , self . __ebStatus , <EOL> ( tag , mailbox ) , None , ( tag , mailbox ) , None <EOL> ) <EOL> else : <EOL> self . sendNegativeResponse ( tag , "<STR_LIT>" ) <EOL> def _ebStatusGotMailbox ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" ) <EOL> log . err ( failure ) <EOL> auth_STATUS = ( do_STATUS , arg_astring , arg_plist ) <EOL> select_STATUS = auth_STATUS <EOL> def __cbStatus ( self , status , tag , box ) : <EOL> line = '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' % x for x in status . iteritems ( ) ] ) <EOL> self . sendUntaggedResponse ( '<STR_LIT>' % ( box , line ) ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> def __ebStatus ( self , failure , tag , box ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' % ( box , str ( failure . value ) ) ) <EOL> def do_APPEND ( self , tag , mailbox , flags , date , message ) : <EOL> mailbox = self . _parseMbox ( mailbox ) <EOL> maybeDeferred ( self . account . select , mailbox <EOL> ) . addCallback ( self . _cbAppendGotMailbox , tag , flags , date , message <EOL> ) . addErrback ( self . _ebAppendGotMailbox , tag <EOL> ) <EOL> def _cbAppendGotMailbox ( self , mbox , tag , flags , date , message ) : <EOL> if not mbox : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> return <EOL> d = mbox . addMessage ( message , flags , date ) <EOL> d . addCallback ( self . __cbAppend , tag , mbox ) <EOL> d . addErrback ( self . __ebAppend , tag ) <EOL> def _ebAppendGotMailbox ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , "<STR_LIT>" ) <EOL> log . err ( failure ) <EOL> auth_APPEND = ( do_APPEND , arg_astring , opt_plist , opt_datetime , <EOL> arg_literal ) <EOL> select_APPEND = auth_APPEND <EOL> def __cbAppend ( self , result , tag , mbox ) : <EOL> self . sendUntaggedResponse ( '<STR_LIT>' % mbox . getMessageCount ( ) ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> def __ebAppend ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> def do_CHECK ( self , tag ) : <EOL> d = self . checkpoint ( ) <EOL> if d is None : <EOL> self . __cbCheck ( None , tag ) <EOL> else : <EOL> d . addCallbacks ( <EOL> self . __cbCheck , <EOL> self . __ebCheck , <EOL> callbackArgs = ( tag , ) , <EOL> errbackArgs = ( tag , ) <EOL> ) <EOL> select_CHECK = ( do_CHECK , ) <EOL> def __cbCheck ( self , result , tag ) : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> def __ebCheck ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> def checkpoint ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def do_CLOSE ( self , tag ) : <EOL> d = None <EOL> if self . mbox . isWriteable ( ) : <EOL> d = maybeDeferred ( self . mbox . expunge ) <EOL> cmbx = ICloseableMailbox ( self . mbox , None ) <EOL> if cmbx is not None : <EOL> if d is not None : <EOL> d . addCallback ( lambda result : cmbx . close ( ) ) <EOL> else : <EOL> d = maybeDeferred ( cmbx . close ) <EOL> if d is not None : <EOL> d . addCallbacks ( self . __cbClose , self . __ebClose , ( tag , ) , None , ( tag , ) , None ) <EOL> else : <EOL> self . __cbClose ( None , tag ) <EOL> select_CLOSE = ( do_CLOSE , ) <EOL> def __cbClose ( self , result , tag ) : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> self . mbox . removeListener ( self ) <EOL> self . mbox = None <EOL> self . state = '<STR_LIT>' <EOL> def __ebClose ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> def do_EXPUNGE ( self , tag ) : <EOL> if self . mbox . isWriteable ( ) : <EOL> maybeDeferred ( self . mbox . expunge ) . addCallbacks ( <EOL> self . __cbExpunge , self . __ebExpunge , ( tag , ) , None , ( tag , ) , None <EOL> ) <EOL> else : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> select_EXPUNGE = ( do_EXPUNGE , ) <EOL> def __cbExpunge ( self , result , tag ) : <EOL> for e in result : <EOL> self . sendUntaggedResponse ( '<STR_LIT>' % e ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> def __ebExpunge ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> log . err ( failure ) <EOL> def do_SEARCH ( self , tag , charset , query , uid = <NUM_LIT:0> ) : <EOL> sm = ISearchableMailbox ( self . mbox , None ) <EOL> if sm is not None : <EOL> maybeDeferred ( sm . search , query , uid = uid <EOL> ) . addCallback ( self . __cbSearch , tag , self . mbox , uid <EOL> ) . addErrback ( self . __ebSearch , tag ) <EOL> else : <EOL> s = parseIdList ( '<STR_LIT>' ) <EOL> maybeDeferred ( self . mbox . fetch , s , uid = uid <EOL> ) . addCallback ( self . __cbManualSearch , <EOL> tag , self . mbox , query , uid <EOL> ) . addErrback ( self . __ebSearch , tag ) <EOL> select_SEARCH = ( do_SEARCH , opt_charset , arg_searchkeys ) <EOL> def __cbSearch ( self , result , tag , mbox , uid ) : <EOL> if uid : <EOL> result = map ( mbox . getUID , result ) <EOL> ids = '<STR_LIT:U+0020>' . join ( [ str ( i ) for i in result ] ) <EOL> self . sendUntaggedResponse ( '<STR_LIT>' + ids ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> def __cbManualSearch ( self , result , tag , mbox , query , uid , <EOL> searchResults = None ) : <EOL> """<STR_LIT>""" <EOL> if searchResults is None : <EOL> searchResults = [ ] <EOL> i = <NUM_LIT:0> <EOL> lastSequenceId = result and result [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> lastMessageId = result and result [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] . getUID ( ) <EOL> for ( i , ( id , msg ) ) in zip ( range ( <NUM_LIT:5> ) , result ) : <EOL> if self . _searchFilter ( copy . deepcopy ( query ) , id , msg , <EOL> lastSequenceId , lastMessageId ) : <EOL> if uid : <EOL> searchResults . append ( str ( msg . getUID ( ) ) ) <EOL> else : <EOL> searchResults . append ( str ( id ) ) <EOL> if i == <NUM_LIT:4> : <EOL> from twisted . internet import reactor <EOL> reactor . callLater ( <EOL> <NUM_LIT:0> , self . __cbManualSearch , result [ <NUM_LIT:5> : ] , tag , mbox , query , uid , <EOL> searchResults ) <EOL> else : <EOL> if searchResults : <EOL> self . sendUntaggedResponse ( '<STR_LIT>' + '<STR_LIT:U+0020>' . join ( searchResults ) ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> def _searchFilter ( self , query , id , msg , lastSequenceId , lastMessageId ) : <EOL> """<STR_LIT>""" <EOL> while query : <EOL> if not self . _singleSearchStep ( query , id , msg , <EOL> lastSequenceId , lastMessageId ) : <EOL> return False <EOL> return True <EOL> def _singleSearchStep ( self , query , id , msg , lastSequenceId , lastMessageId ) : <EOL> """<STR_LIT>""" <EOL> q = query . pop ( <NUM_LIT:0> ) <EOL> if isinstance ( q , list ) : <EOL> if not self . _searchFilter ( q , id , msg , <EOL> lastSequenceId , lastMessageId ) : <EOL> return False <EOL> else : <EOL> c = q . upper ( ) <EOL> if not c [ : <NUM_LIT:1> ] . isalpha ( ) : <EOL> messageSet = parseIdList ( c , lastSequenceId ) <EOL> return id in messageSet <EOL> else : <EOL> f = getattr ( self , '<STR_LIT>' + c , None ) <EOL> if f is None : <EOL> raise IllegalQueryError ( "<STR_LIT>" % c ) <EOL> if c in self . _requiresLastMessageInfo : <EOL> result = f ( query , id , msg , ( lastSequenceId , <EOL> lastMessageId ) ) <EOL> else : <EOL> result = f ( query , id , msg ) <EOL> if not result : <EOL> return False <EOL> return True <EOL> def search_ALL ( self , query , id , msg ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def search_ANSWERED ( self , query , id , msg ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' in msg . getFlags ( ) <EOL> def search_BCC ( self , query , id , msg ) : <EOL> """<STR_LIT>""" <EOL> bcc = msg . getHeaders ( False , '<STR_LIT>' ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return bcc . lower ( ) . find ( query . pop ( <NUM_LIT:0> ) . lower ( ) ) != - <NUM_LIT:1> <EOL> def search_BEFORE ( self , query , id , msg ) : <EOL> date = parseTime ( query . pop ( <NUM_LIT:0> ) ) <EOL> return rfc822 . parsedate ( msg . getInternalDate ( ) ) < date <EOL> def search_BODY ( self , query , id , msg ) : <EOL> body = query . pop ( <NUM_LIT:0> ) . lower ( ) <EOL> return text . strFile ( body , msg . getBodyFile ( ) , False ) <EOL> def search_CC ( self , query , id , msg ) : <EOL> cc = msg . getHeaders ( False , '<STR_LIT>' ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return cc . lower ( ) . find ( query . pop ( <NUM_LIT:0> ) . lower ( ) ) != - <NUM_LIT:1> <EOL> def search_DELETED ( self , query , id , msg ) : <EOL> return '<STR_LIT>' in msg . getFlags ( ) <EOL> def search_DRAFT ( self , query , id , msg ) : <EOL> return '<STR_LIT>' in msg . getFlags ( ) <EOL> def search_FLAGGED ( self , query , id , msg ) : <EOL> return '<STR_LIT>' in msg . getFlags ( ) <EOL> def search_FROM ( self , query , id , msg ) : <EOL> fm = msg . getHeaders ( False , '<STR_LIT>' ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return fm . lower ( ) . find ( query . pop ( <NUM_LIT:0> ) . lower ( ) ) != - <NUM_LIT:1> <EOL> def search_HEADER ( self , query , id , msg ) : <EOL> hdr = query . pop ( <NUM_LIT:0> ) . lower ( ) <EOL> hdr = msg . getHeaders ( False , hdr ) . get ( hdr , '<STR_LIT>' ) <EOL> return hdr . lower ( ) . find ( query . pop ( <NUM_LIT:0> ) . lower ( ) ) != - <NUM_LIT:1> <EOL> def search_KEYWORD ( self , query , id , msg ) : <EOL> query . pop ( <NUM_LIT:0> ) <EOL> return False <EOL> def search_LARGER ( self , query , id , msg ) : <EOL> return int ( query . pop ( <NUM_LIT:0> ) ) < msg . getSize ( ) <EOL> def search_NEW ( self , query , id , msg ) : <EOL> return '<STR_LIT>' in msg . getFlags ( ) and '<STR_LIT>' not in msg . getFlags ( ) <EOL> def search_NOT ( self , query , id , msg , ( lastSequenceId , lastMessageId ) ) : <EOL> """<STR_LIT>""" <EOL> return not self . _singleSearchStep ( query , id , msg , <EOL> lastSequenceId , lastMessageId ) <EOL> def search_OLD ( self , query , id , msg ) : <EOL> return '<STR_LIT>' not in msg . getFlags ( ) <EOL> def search_ON ( self , query , id , msg ) : <EOL> date = parseTime ( query . pop ( <NUM_LIT:0> ) ) <EOL> return rfc822 . parsedate ( msg . getInternalDate ( ) ) == date <EOL> def search_OR ( self , query , id , msg , ( lastSequenceId , lastMessageId ) ) : <EOL> """<STR_LIT>""" <EOL> a = self . _singleSearchStep ( query , id , msg , <EOL> lastSequenceId , lastMessageId ) <EOL> b = self . _singleSearchStep ( query , id , msg , <EOL> lastSequenceId , lastMessageId ) <EOL> return a or b <EOL> def search_RECENT ( self , query , id , msg ) : <EOL> return '<STR_LIT>' in msg . getFlags ( ) <EOL> def search_SEEN ( self , query , id , msg ) : <EOL> return '<STR_LIT>' in msg . getFlags ( ) <EOL> def search_SENTBEFORE ( self , query , id , msg ) : <EOL> """<STR_LIT>""" <EOL> date = msg . getHeaders ( False , '<STR_LIT:date>' ) . get ( '<STR_LIT:date>' , '<STR_LIT>' ) <EOL> date = rfc822 . parsedate ( date ) <EOL> return date < parseTime ( query . pop ( <NUM_LIT:0> ) ) <EOL> def search_SENTON ( self , query , id , msg ) : <EOL> """<STR_LIT>""" <EOL> date = msg . getHeaders ( False , '<STR_LIT:date>' ) . get ( '<STR_LIT:date>' , '<STR_LIT>' ) <EOL> date = rfc822 . parsedate ( date ) <EOL> return date [ : <NUM_LIT:3> ] == parseTime ( query . pop ( <NUM_LIT:0> ) ) [ : <NUM_LIT:3> ] <EOL> def search_SENTSINCE ( self , query , id , msg ) : <EOL> """<STR_LIT>""" <EOL> date = msg . getHeaders ( False , '<STR_LIT:date>' ) . get ( '<STR_LIT:date>' , '<STR_LIT>' ) <EOL> date = rfc822 . parsedate ( date ) <EOL> return date > parseTime ( query . pop ( <NUM_LIT:0> ) ) <EOL> def search_SINCE ( self , query , id , msg ) : <EOL> date = parseTime ( query . pop ( <NUM_LIT:0> ) ) <EOL> return rfc822 . parsedate ( msg . getInternalDate ( ) ) > date <EOL> def search_SMALLER ( self , query , id , msg ) : <EOL> return int ( query . pop ( <NUM_LIT:0> ) ) > msg . getSize ( ) <EOL> def search_SUBJECT ( self , query , id , msg ) : <EOL> subj = msg . getHeaders ( False , '<STR_LIT>' ) . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return subj . lower ( ) . find ( query . pop ( <NUM_LIT:0> ) . lower ( ) ) != - <NUM_LIT:1> <EOL> def search_TEXT ( self , query , id , msg ) : <EOL> body = query . pop ( <NUM_LIT:0> ) . lower ( ) <EOL> return text . strFile ( body , msg . getBodyFile ( ) , False ) <EOL> def search_TO ( self , query , id , msg ) : <EOL> to = msg . getHeaders ( False , '<STR_LIT:to>' ) . get ( '<STR_LIT:to>' , '<STR_LIT>' ) <EOL> return to . lower ( ) . find ( query . pop ( <NUM_LIT:0> ) . lower ( ) ) != - <NUM_LIT:1> <EOL> def search_UID ( self , query , id , msg , ( lastSequenceId , lastMessageId ) ) : <EOL> """<STR_LIT>""" <EOL> c = query . pop ( <NUM_LIT:0> ) <EOL> m = parseIdList ( c , lastMessageId ) <EOL> return msg . getUID ( ) in m <EOL> def search_UNANSWERED ( self , query , id , msg ) : <EOL> return '<STR_LIT>' not in msg . getFlags ( ) <EOL> def search_UNDELETED ( self , query , id , msg ) : <EOL> return '<STR_LIT>' not in msg . getFlags ( ) <EOL> def search_UNDRAFT ( self , query , id , msg ) : <EOL> return '<STR_LIT>' not in msg . getFlags ( ) <EOL> def search_UNFLAGGED ( self , query , id , msg ) : <EOL> return '<STR_LIT>' not in msg . getFlags ( ) <EOL> def search_UNKEYWORD ( self , query , id , msg ) : <EOL> query . pop ( <NUM_LIT:0> ) <EOL> return False <EOL> def search_UNSEEN ( self , query , id , msg ) : <EOL> return '<STR_LIT>' not in msg . getFlags ( ) <EOL> def __ebSearch ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> log . err ( failure ) <EOL> def do_FETCH ( self , tag , messages , query , uid = <NUM_LIT:0> ) : <EOL> if query : <EOL> self . _oldTimeout = self . setTimeout ( None ) <EOL> maybeDeferred ( self . mbox . fetch , messages , uid = uid <EOL> ) . addCallback ( iter <EOL> ) . addCallback ( self . __cbFetch , tag , query , uid <EOL> ) . addErrback ( self . __ebFetch , tag <EOL> ) <EOL> else : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> select_FETCH = ( do_FETCH , arg_seqset , arg_fetchatt ) <EOL> def __cbFetch ( self , results , tag , query , uid ) : <EOL> if self . blocked is None : <EOL> self . blocked = [ ] <EOL> try : <EOL> id , msg = results . next ( ) <EOL> except StopIteration : <EOL> self . setTimeout ( self . _oldTimeout ) <EOL> del self . _oldTimeout <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> self . _unblock ( ) <EOL> else : <EOL> self . spewMessage ( id , msg , query , uid <EOL> ) . addCallback ( lambda _ : self . __cbFetch ( results , tag , query , uid ) <EOL> ) . addErrback ( self . __ebSpewMessage <EOL> ) <EOL> def __ebSpewMessage ( self , failure ) : <EOL> log . err ( failure ) <EOL> self . transport . loseConnection ( ) <EOL> def spew_envelope ( self , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> _w ( '<STR_LIT>' + collapseNestedLists ( [ getEnvelope ( msg ) ] ) ) <EOL> def spew_flags ( self , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> _w ( '<STR_LIT>' + '<STR_LIT>' % ( '<STR_LIT:U+0020>' . join ( msg . getFlags ( ) ) ) ) <EOL> def spew_internaldate ( self , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> idate = msg . getInternalDate ( ) <EOL> ttup = rfc822 . parsedate_tz ( idate ) <EOL> if ttup is None : <EOL> log . msg ( "<STR_LIT>" % ( id , msg , idate ) ) <EOL> raise IMAP4Exception ( "<STR_LIT>" ) <EOL> strdate = time . strftime ( "<STR_LIT>" , ttup [ : <NUM_LIT:9> ] ) <EOL> odate = strdate % ( _MONTH_NAMES [ ttup [ <NUM_LIT:1> ] ] , ) <EOL> if ttup [ <NUM_LIT:9> ] is None : <EOL> odate = odate + "<STR_LIT>" <EOL> else : <EOL> if ttup [ <NUM_LIT:9> ] >= <NUM_LIT:0> : <EOL> sign = "<STR_LIT:+>" <EOL> else : <EOL> sign = "<STR_LIT:->" <EOL> odate = odate + sign + str ( ( ( abs ( ttup [ <NUM_LIT:9> ] ) // <NUM_LIT> ) * <NUM_LIT:100> + ( abs ( ttup [ <NUM_LIT:9> ] ) % <NUM_LIT> ) // <NUM_LIT> ) ) . zfill ( <NUM_LIT:4> ) <EOL> _w ( '<STR_LIT>' + _quote ( odate ) ) <EOL> def spew_rfc822header ( self , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> hdrs = _formatHeaders ( msg . getHeaders ( True ) ) <EOL> _w ( '<STR_LIT>' + _literal ( hdrs ) ) <EOL> def spew_rfc822text ( self , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> _w ( '<STR_LIT>' ) <EOL> _f ( ) <EOL> return FileProducer ( msg . getBodyFile ( ) <EOL> ) . beginProducing ( self . transport <EOL> ) <EOL> def spew_rfc822size ( self , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> _w ( '<STR_LIT>' + str ( msg . getSize ( ) ) ) <EOL> def spew_rfc822 ( self , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> _w ( '<STR_LIT>' ) <EOL> _f ( ) <EOL> mf = IMessageFile ( msg , None ) <EOL> if mf is not None : <EOL> return FileProducer ( mf . open ( ) <EOL> ) . beginProducing ( self . transport <EOL> ) <EOL> return MessageProducer ( msg , None , self . _scheduler <EOL> ) . beginProducing ( self . transport <EOL> ) <EOL> def spew_uid ( self , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> _w ( '<STR_LIT>' + str ( msg . getUID ( ) ) ) <EOL> def spew_bodystructure ( self , id , msg , _w = None , _f = None ) : <EOL> _w ( '<STR_LIT>' + collapseNestedLists ( [ getBodyStructure ( msg , True ) ] ) ) <EOL> def spew_body ( self , part , id , msg , _w = None , _f = None ) : <EOL> if _w is None : <EOL> _w = self . transport . write <EOL> for p in part . part : <EOL> if msg . isMultipart ( ) : <EOL> msg = msg . getSubPart ( p ) <EOL> elif p > <NUM_LIT:0> : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if part . header : <EOL> hdrs = msg . getHeaders ( part . header . negate , * part . header . fields ) <EOL> hdrs = _formatHeaders ( hdrs ) <EOL> _w ( str ( part ) + '<STR_LIT:U+0020>' + _literal ( hdrs ) ) <EOL> elif part . text : <EOL> _w ( str ( part ) + '<STR_LIT:U+0020>' ) <EOL> _f ( ) <EOL> return FileProducer ( msg . getBodyFile ( ) <EOL> ) . beginProducing ( self . transport <EOL> ) <EOL> elif part . mime : <EOL> hdrs = _formatHeaders ( msg . getHeaders ( True ) ) <EOL> _w ( str ( part ) + '<STR_LIT:U+0020>' + _literal ( hdrs ) ) <EOL> elif part . empty : <EOL> _w ( str ( part ) + '<STR_LIT:U+0020>' ) <EOL> _f ( ) <EOL> if part . part : <EOL> return FileProducer ( msg . getBodyFile ( ) <EOL> ) . beginProducing ( self . transport <EOL> ) <EOL> else : <EOL> mf = IMessageFile ( msg , None ) <EOL> if mf is not None : <EOL> return FileProducer ( mf . open ( ) ) . beginProducing ( self . transport ) <EOL> return MessageProducer ( msg , None , self . _scheduler ) . beginProducing ( self . transport ) <EOL> else : <EOL> _w ( '<STR_LIT>' + collapseNestedLists ( [ getBodyStructure ( msg ) ] ) ) <EOL> def spewMessage ( self , id , msg , query , uid ) : <EOL> wbuf = WriteBuffer ( self . transport ) <EOL> write = wbuf . write <EOL> flush = wbuf . flush <EOL> def start ( ) : <EOL> write ( '<STR_LIT>' % ( id , ) ) <EOL> def finish ( ) : <EOL> write ( '<STR_LIT>' ) <EOL> def space ( ) : <EOL> write ( '<STR_LIT:U+0020>' ) <EOL> def spew ( ) : <EOL> seenUID = False <EOL> start ( ) <EOL> for part in query : <EOL> if part . type == '<STR_LIT>' : <EOL> seenUID = True <EOL> if part . type == '<STR_LIT:body>' : <EOL> yield self . spew_body ( part , id , msg , write , flush ) <EOL> else : <EOL> f = getattr ( self , '<STR_LIT>' + part . type ) <EOL> yield f ( id , msg , write , flush ) <EOL> if part is not query [ - <NUM_LIT:1> ] : <EOL> space ( ) <EOL> if uid and not seenUID : <EOL> space ( ) <EOL> yield self . spew_uid ( id , msg , write , flush ) <EOL> finish ( ) <EOL> flush ( ) <EOL> return self . _scheduler ( spew ( ) ) <EOL> def __ebFetch ( self , failure , tag ) : <EOL> self . setTimeout ( self . _oldTimeout ) <EOL> del self . _oldTimeout <EOL> log . err ( failure ) <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> def do_STORE ( self , tag , messages , mode , flags , uid = <NUM_LIT:0> ) : <EOL> mode = mode . upper ( ) <EOL> silent = mode . endswith ( '<STR_LIT>' ) <EOL> if mode . startswith ( '<STR_LIT:+>' ) : <EOL> mode = <NUM_LIT:1> <EOL> elif mode . startswith ( '<STR_LIT:->' ) : <EOL> mode = - <NUM_LIT:1> <EOL> else : <EOL> mode = <NUM_LIT:0> <EOL> maybeDeferred ( self . mbox . store , messages , flags , mode , uid = uid ) . addCallbacks ( <EOL> self . __cbStore , self . __ebStore , ( tag , self . mbox , uid , silent ) , None , ( tag , ) , None <EOL> ) <EOL> select_STORE = ( do_STORE , arg_seqset , arg_atom , arg_flaglist ) <EOL> def __cbStore ( self , result , tag , mbox , uid , silent ) : <EOL> if result and not silent : <EOL> for ( k , v ) in result . iteritems ( ) : <EOL> if uid : <EOL> uidstr = '<STR_LIT>' % mbox . getUID ( k ) <EOL> else : <EOL> uidstr = '<STR_LIT>' <EOL> self . sendUntaggedResponse ( '<STR_LIT>' % <EOL> ( k , '<STR_LIT:U+0020>' . join ( v ) , uidstr ) ) <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> def __ebStore ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> def do_COPY ( self , tag , messages , mailbox , uid = <NUM_LIT:0> ) : <EOL> mailbox = self . _parseMbox ( mailbox ) <EOL> maybeDeferred ( self . account . select , mailbox <EOL> ) . addCallback ( self . _cbCopySelectedMailbox , tag , messages , mailbox , uid <EOL> ) . addErrback ( self . _ebCopySelectedMailbox , tag <EOL> ) <EOL> select_COPY = ( do_COPY , arg_seqset , arg_astring ) <EOL> def _cbCopySelectedMailbox ( self , mbox , tag , messages , mailbox , uid ) : <EOL> if not mbox : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' + mailbox ) <EOL> else : <EOL> maybeDeferred ( self . mbox . fetch , messages , uid <EOL> ) . addCallback ( self . __cbCopy , tag , mbox <EOL> ) . addCallback ( self . __cbCopied , tag , mbox <EOL> ) . addErrback ( self . __ebCopy , tag <EOL> ) <EOL> def _ebCopySelectedMailbox ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> def __cbCopy ( self , messages , tag , mbox ) : <EOL> addedDeferreds = [ ] <EOL> addedIDs = [ ] <EOL> failures = [ ] <EOL> fastCopyMbox = IMessageCopier ( mbox , None ) <EOL> for ( id , msg ) in messages : <EOL> if fastCopyMbox is not None : <EOL> d = maybeDeferred ( fastCopyMbox . copy , msg ) <EOL> addedDeferreds . append ( d ) <EOL> continue <EOL> flags = msg . getFlags ( ) <EOL> date = msg . getInternalDate ( ) <EOL> body = IMessageFile ( msg , None ) <EOL> if body is not None : <EOL> bodyFile = body . open ( ) <EOL> d = maybeDeferred ( mbox . addMessage , bodyFile , flags , date ) <EOL> else : <EOL> def rewind ( f ) : <EOL> f . seek ( <NUM_LIT:0> ) <EOL> return f <EOL> buffer = tempfile . TemporaryFile ( ) <EOL> d = MessageProducer ( msg , buffer , self . _scheduler <EOL> ) . beginProducing ( None <EOL> ) . addCallback ( lambda _ , b = buffer , f = flags , d = date : mbox . addMessage ( rewind ( b ) , f , d ) <EOL> ) <EOL> addedDeferreds . append ( d ) <EOL> return defer . DeferredList ( addedDeferreds ) <EOL> def __cbCopied ( self , deferredIds , tag , mbox ) : <EOL> ids = [ ] <EOL> failures = [ ] <EOL> for ( status , result ) in deferredIds : <EOL> if status : <EOL> ids . append ( result ) <EOL> else : <EOL> failures . append ( result . value ) <EOL> if failures : <EOL> self . sendNegativeResponse ( tag , '<STR_LIT>' ) <EOL> else : <EOL> self . sendPositiveResponse ( tag , '<STR_LIT>' ) <EOL> def __ebCopy ( self , failure , tag ) : <EOL> self . sendBadResponse ( tag , '<STR_LIT>' + str ( failure . value ) ) <EOL> log . err ( failure ) <EOL> def do_UID ( self , tag , command , line ) : <EOL> command = command . upper ( ) <EOL> if command not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise IllegalClientResponse ( command ) <EOL> self . dispatchCommand ( tag , command , line , uid = <NUM_LIT:1> ) <EOL> select_UID = ( do_UID , arg_atom , arg_line ) <EOL> def modeChanged ( self , writeable ) : <EOL> if writeable : <EOL> self . sendUntaggedResponse ( message = '<STR_LIT>' , async = True ) <EOL> else : <EOL> self . sendUntaggedResponse ( message = '<STR_LIT>' , async = True ) <EOL> def flagsChanged ( self , newFlags ) : <EOL> for ( mId , flags ) in newFlags . iteritems ( ) : <EOL> msg = '<STR_LIT>' % ( mId , '<STR_LIT:U+0020>' . join ( flags ) ) <EOL> self . sendUntaggedResponse ( msg , async = True ) <EOL> def newMessages ( self , exists , recent ) : <EOL> if exists is not None : <EOL> self . sendUntaggedResponse ( '<STR_LIT>' % exists , async = True ) <EOL> if recent is not None : <EOL> self . sendUntaggedResponse ( '<STR_LIT>' % recent , async = True ) <EOL> class UnhandledResponse ( IMAP4Exception ) : pass <EOL> class NegativeResponse ( IMAP4Exception ) : pass <EOL> class NoSupportedAuthentication ( IMAP4Exception ) : <EOL> def __init__ ( self , serverSupports , clientSupports ) : <EOL> IMAP4Exception . __init__ ( self , '<STR_LIT>' ) <EOL> self . serverSupports = serverSupports <EOL> self . clientSupports = clientSupports <EOL> def __str__ ( self ) : <EOL> return ( IMAP4Exception . __str__ ( self ) <EOL> + '<STR_LIT>' <EOL> % ( self . serverSupports , self . clientSupports ) ) <EOL> class IllegalServerResponse ( IMAP4Exception ) : pass <EOL> TIMEOUT_ERROR = error . TimeoutError ( ) <EOL> class IMAP4Client ( basic . LineReceiver , policies . TimeoutMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IMailboxListener ) <EOL> tags = None <EOL> waiting = None <EOL> queued = None <EOL> tagID = <NUM_LIT:1> <EOL> state = None <EOL> startedTLS = False <EOL> timeout = <NUM_LIT:0> <EOL> _capCache = None <EOL> _memoryFileLimit = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:10> <EOL> authenticators = None <EOL> STATUS_CODES = ( '<STR_LIT:OK>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> STATUS_TRANSFORMATIONS = { <EOL> '<STR_LIT>' : int , '<STR_LIT>' : int , '<STR_LIT>' : int <EOL> } <EOL> context = None <EOL> def __init__ ( self , contextFactory = None ) : <EOL> self . tags = { } <EOL> self . queued = [ ] <EOL> self . authenticators = { } <EOL> self . context = contextFactory <EOL> self . _tag = None <EOL> self . _parts = None <EOL> self . _lastCmd = None <EOL> def registerAuthenticator ( self , auth ) : <EOL> """<STR_LIT>""" <EOL> self . authenticators [ auth . getName ( ) . upper ( ) ] = auth <EOL> def rawDataReceived ( self , data ) : <EOL> if self . timeout > <NUM_LIT:0> : <EOL> self . resetTimeout ( ) <EOL> self . _pendingSize -= len ( data ) <EOL> if self . _pendingSize > <NUM_LIT:0> : <EOL> self . _pendingBuffer . write ( data ) <EOL> else : <EOL> passon = '<STR_LIT>' <EOL> if self . _pendingSize < <NUM_LIT:0> : <EOL> data , passon = data [ : self . _pendingSize ] , data [ self . _pendingSize : ] <EOL> self . _pendingBuffer . write ( data ) <EOL> rest = self . _pendingBuffer <EOL> self . _pendingBuffer = None <EOL> self . _pendingSize = None <EOL> rest . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . _parts . append ( rest . read ( ) ) <EOL> self . setLineMode ( passon . lstrip ( '<STR_LIT:\r\n>' ) ) <EOL> def _setupForLiteral ( self , rest , octets ) : <EOL> self . _pendingBuffer = self . messageFile ( octets ) <EOL> self . _pendingSize = octets <EOL> if self . _parts is None : <EOL> self . _parts = [ rest , '<STR_LIT:\r\n>' ] <EOL> else : <EOL> self . _parts . extend ( [ rest , '<STR_LIT:\r\n>' ] ) <EOL> self . setRawMode ( ) <EOL> def connectionMade ( self ) : <EOL> if self . timeout > <NUM_LIT:0> : <EOL> self . setTimeout ( self . timeout ) <EOL> def connectionLost ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> if self . timeout > <NUM_LIT:0> : <EOL> self . setTimeout ( None ) <EOL> if self . queued is not None : <EOL> queued = self . queued <EOL> self . queued = None <EOL> for cmd in queued : <EOL> cmd . defer . errback ( reason ) <EOL> if self . tags is not None : <EOL> tags = self . tags <EOL> self . tags = None <EOL> for cmd in tags . itervalues ( ) : <EOL> if cmd is not None and cmd . defer is not None : <EOL> cmd . defer . errback ( reason ) <EOL> def lineReceived ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if self . timeout > <NUM_LIT:0> : <EOL> self . resetTimeout ( ) <EOL> lastPart = line . rfind ( '<STR_LIT:{>' ) <EOL> if lastPart != - <NUM_LIT:1> : <EOL> lastPart = line [ lastPart + <NUM_LIT:1> : ] <EOL> if lastPart . endswith ( '<STR_LIT:}>' ) : <EOL> try : <EOL> octets = int ( lastPart [ : - <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> raise IllegalServerResponse ( line ) <EOL> if self . _parts is None : <EOL> self . _tag , parts = line . split ( None , <NUM_LIT:1> ) <EOL> else : <EOL> parts = line <EOL> self . _setupForLiteral ( parts , octets ) <EOL> return <EOL> if self . _parts is None : <EOL> self . _regularDispatch ( line ) <EOL> else : <EOL> self . _parts . append ( line ) <EOL> tag , rest = self . _tag , '<STR_LIT>' . join ( self . _parts ) <EOL> self . _tag = self . _parts = None <EOL> self . dispatchCommand ( tag , rest ) <EOL> def timeoutConnection ( self ) : <EOL> if self . _lastCmd and self . _lastCmd . defer is not None : <EOL> d , self . _lastCmd . defer = self . _lastCmd . defer , None <EOL> d . errback ( TIMEOUT_ERROR ) <EOL> if self . queued : <EOL> for cmd in self . queued : <EOL> if cmd . defer is not None : <EOL> d , cmd . defer = cmd . defer , d <EOL> d . errback ( TIMEOUT_ERROR ) <EOL> self . transport . loseConnection ( ) <EOL> def _regularDispatch ( self , line ) : <EOL> parts = line . split ( None , <NUM_LIT:1> ) <EOL> if len ( parts ) != <NUM_LIT:2> : <EOL> parts . append ( '<STR_LIT>' ) <EOL> tag , rest = parts <EOL> self . dispatchCommand ( tag , rest ) <EOL> def messageFile ( self , octets ) : <EOL> """<STR_LIT>""" <EOL> if octets > self . _memoryFileLimit : <EOL> return tempfile . TemporaryFile ( ) <EOL> else : <EOL> return StringIO . StringIO ( ) <EOL> def makeTag ( self ) : <EOL> tag = '<STR_LIT>' % self . tagID <EOL> self . tagID += <NUM_LIT:1> <EOL> return tag <EOL> def dispatchCommand ( self , tag , rest ) : <EOL> if self . state is None : <EOL> f = self . response_UNAUTH <EOL> else : <EOL> f = getattr ( self , '<STR_LIT>' + self . state . upper ( ) , None ) <EOL> if f : <EOL> try : <EOL> f ( tag , rest ) <EOL> except : <EOL> log . err ( ) <EOL> self . transport . loseConnection ( ) <EOL> else : <EOL> log . err ( "<STR_LIT>" % ( self . state , tag , rest ) ) <EOL> self . transport . loseConnection ( ) <EOL> def response_UNAUTH ( self , tag , rest ) : <EOL> if self . state is None : <EOL> status , rest = rest . split ( None , <NUM_LIT:1> ) <EOL> if status . upper ( ) == '<STR_LIT:OK>' : <EOL> self . state = '<STR_LIT>' <EOL> elif status . upper ( ) == '<STR_LIT>' : <EOL> self . state = '<STR_LIT>' <EOL> else : <EOL> self . transport . loseConnection ( ) <EOL> raise IllegalServerResponse ( tag + '<STR_LIT:U+0020>' + rest ) <EOL> b , e = rest . find ( '<STR_LIT:[>' ) , rest . find ( '<STR_LIT:]>' ) <EOL> if b != - <NUM_LIT:1> and e != - <NUM_LIT:1> : <EOL> self . serverGreeting ( <EOL> self . __cbCapabilities ( <EOL> ( [ parseNestedParens ( rest [ b + <NUM_LIT:1> : e ] ) ] , None ) ) ) <EOL> else : <EOL> self . serverGreeting ( None ) <EOL> else : <EOL> self . _defaultHandler ( tag , rest ) <EOL> def response_AUTH ( self , tag , rest ) : <EOL> self . _defaultHandler ( tag , rest ) <EOL> def _defaultHandler ( self , tag , rest ) : <EOL> if tag == '<STR_LIT:*>' or tag == '<STR_LIT:+>' : <EOL> if not self . waiting : <EOL> self . _extraInfo ( [ parseNestedParens ( rest ) ] ) <EOL> else : <EOL> cmd = self . tags [ self . waiting ] <EOL> if tag == '<STR_LIT:+>' : <EOL> cmd . continuation ( rest ) <EOL> else : <EOL> cmd . lines . append ( rest ) <EOL> else : <EOL> try : <EOL> cmd = self . tags [ tag ] <EOL> except KeyError : <EOL> self . transport . loseConnection ( ) <EOL> raise IllegalServerResponse ( tag + '<STR_LIT:U+0020>' + rest ) <EOL> else : <EOL> status , line = rest . split ( None , <NUM_LIT:1> ) <EOL> if status == '<STR_LIT:OK>' : <EOL> cmd . finish ( rest , self . _extraInfo ) <EOL> else : <EOL> cmd . defer . errback ( IMAP4Exception ( line ) ) <EOL> del self . tags [ tag ] <EOL> self . waiting = None <EOL> self . _flushQueue ( ) <EOL> def _flushQueue ( self ) : <EOL> if self . queued : <EOL> cmd = self . queued . pop ( <NUM_LIT:0> ) <EOL> t = self . makeTag ( ) <EOL> self . tags [ t ] = cmd <EOL> self . sendLine ( cmd . format ( t ) ) <EOL> self . waiting = t <EOL> def _extraInfo ( self , lines ) : <EOL> flags = { } <EOL> recent = exists = None <EOL> for response in lines : <EOL> elements = len ( response ) <EOL> if elements == <NUM_LIT:1> and response [ <NUM_LIT:0> ] == [ '<STR_LIT>' ] : <EOL> self . modeChanged ( False ) <EOL> elif elements == <NUM_LIT:1> and response [ <NUM_LIT:0> ] == [ '<STR_LIT>' ] : <EOL> self . modeChanged ( True ) <EOL> elif elements == <NUM_LIT:2> and response [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> exists = int ( response [ <NUM_LIT:0> ] ) <EOL> elif elements == <NUM_LIT:2> and response [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> recent = int ( response [ <NUM_LIT:0> ] ) <EOL> elif elements == <NUM_LIT:3> and response [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> mId = int ( response [ <NUM_LIT:0> ] ) <EOL> values = self . _parseFetchPairs ( response [ <NUM_LIT:2> ] ) <EOL> flags . setdefault ( mId , [ ] ) . extend ( values . get ( '<STR_LIT>' , ( ) ) ) <EOL> else : <EOL> log . msg ( '<STR_LIT>' % ( response , ) ) <EOL> if flags : <EOL> self . flagsChanged ( flags ) <EOL> if recent is not None or exists is not None : <EOL> self . newMessages ( exists , recent ) <EOL> def sendCommand ( self , cmd ) : <EOL> cmd . defer = defer . Deferred ( ) <EOL> if self . waiting : <EOL> self . queued . append ( cmd ) <EOL> return cmd . defer <EOL> t = self . makeTag ( ) <EOL> self . tags [ t ] = cmd <EOL> self . sendLine ( cmd . format ( t ) ) <EOL> self . waiting = t <EOL> self . _lastCmd = cmd <EOL> return cmd . defer <EOL> def getCapabilities ( self , useCache = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if useCache and self . _capCache is not None : <EOL> return defer . succeed ( self . _capCache ) <EOL> cmd = '<STR_LIT>' <EOL> resp = ( '<STR_LIT>' , ) <EOL> d = self . sendCommand ( Command ( cmd , wantResponse = resp ) ) <EOL> d . addCallback ( self . __cbCapabilities ) <EOL> return d <EOL> def __cbCapabilities ( self , ( lines , tagline ) ) : <EOL> caps = { } <EOL> for rest in lines : <EOL> for cap in rest [ <NUM_LIT:1> : ] : <EOL> parts = cap . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> category , value = parts [ <NUM_LIT:0> ] , None <EOL> else : <EOL> category , value = parts <EOL> caps . setdefault ( category , [ ] ) . append ( value ) <EOL> for category in caps : <EOL> if caps [ category ] == [ None ] : <EOL> caps [ category ] = None <EOL> self . _capCache = caps <EOL> return caps <EOL> def logout ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . sendCommand ( Command ( '<STR_LIT>' , wantResponse = ( '<STR_LIT>' , ) ) ) <EOL> d . addCallback ( self . __cbLogout ) <EOL> return d <EOL> def __cbLogout ( self , ( lines , tagline ) ) : <EOL> self . transport . loseConnection ( ) <EOL> return None <EOL> def noop ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . sendCommand ( Command ( '<STR_LIT>' ) ) <EOL> d . addCallback ( self . __cbNoop ) <EOL> return d <EOL> def __cbNoop ( self , ( lines , tagline ) ) : <EOL> return lines <EOL> def startTLS ( self , contextFactory = None ) : <EOL> """<STR_LIT>""" <EOL> assert not self . startedTLS , "<STR_LIT>" <EOL> if contextFactory is None : <EOL> contextFactory = self . _getContextFactory ( ) <EOL> if contextFactory is None : <EOL> return defer . fail ( IMAP4Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if '<STR_LIT>' not in self . _capCache : <EOL> return defer . fail ( IMAP4Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> tls = interfaces . ITLSTransport ( self . transport , None ) <EOL> if tls is None : <EOL> return defer . fail ( IMAP4Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> d = self . sendCommand ( Command ( '<STR_LIT>' ) ) <EOL> d . addCallback ( self . _startedTLS , contextFactory ) <EOL> d . addCallback ( lambda _ : self . getCapabilities ( ) ) <EOL> return d <EOL> def authenticate ( self , secret ) : <EOL> """<STR_LIT>""" <EOL> if self . _capCache is None : <EOL> d = self . getCapabilities ( ) <EOL> else : <EOL> d = defer . succeed ( self . _capCache ) <EOL> d . addCallback ( self . __cbAuthenticate , secret ) <EOL> return d <EOL> def __cbAuthenticate ( self , caps , secret ) : <EOL> auths = caps . get ( '<STR_LIT>' , ( ) ) <EOL> for scheme in auths : <EOL> if scheme . upper ( ) in self . authenticators : <EOL> cmd = Command ( '<STR_LIT>' , scheme , ( ) , <EOL> self . __cbContinueAuth , scheme , <EOL> secret ) <EOL> return self . sendCommand ( cmd ) <EOL> if self . startedTLS : <EOL> return defer . fail ( NoSupportedAuthentication ( <EOL> auths , self . authenticators . keys ( ) ) ) <EOL> else : <EOL> def ebStartTLS ( err ) : <EOL> err . trap ( IMAP4Exception ) <EOL> return defer . fail ( NoSupportedAuthentication ( <EOL> auths , self . authenticators . keys ( ) ) ) <EOL> d = self . startTLS ( ) <EOL> d . addErrback ( ebStartTLS ) <EOL> d . addCallback ( lambda _ : self . getCapabilities ( ) ) <EOL> d . addCallback ( self . __cbAuthTLS , secret ) <EOL> return d <EOL> def __cbContinueAuth ( self , rest , scheme , secret ) : <EOL> try : <EOL> chal = base64 . decodestring ( rest + '<STR_LIT:\n>' ) <EOL> except binascii . Error : <EOL> self . sendLine ( '<STR_LIT:*>' ) <EOL> raise IllegalServerResponse ( rest ) <EOL> self . transport . loseConnection ( ) <EOL> else : <EOL> auth = self . authenticators [ scheme ] <EOL> chal = auth . challengeResponse ( secret , chal ) <EOL> self . sendLine ( base64 . encodestring ( chal ) . strip ( ) ) <EOL> def __cbAuthTLS ( self , caps , secret ) : <EOL> auths = caps . get ( '<STR_LIT>' , ( ) ) <EOL> for scheme in auths : <EOL> if scheme . upper ( ) in self . authenticators : <EOL> cmd = Command ( '<STR_LIT>' , scheme , ( ) , <EOL> self . __cbContinueAuth , scheme , <EOL> secret ) <EOL> return self . sendCommand ( cmd ) <EOL> raise NoSupportedAuthentication ( auths , self . authenticators . keys ( ) ) <EOL> def login ( self , username , password ) : <EOL> """<STR_LIT>""" <EOL> d = maybeDeferred ( self . getCapabilities ) <EOL> d . addCallback ( self . __cbLoginCaps , username , password ) <EOL> return d <EOL> def serverGreeting ( self , caps ) : <EOL> """<STR_LIT>""" <EOL> def _getContextFactory ( self ) : <EOL> if self . context is not None : <EOL> return self . context <EOL> try : <EOL> from twisted . internet import ssl <EOL> except ImportError : <EOL> return None <EOL> else : <EOL> context = ssl . ClientContextFactory ( ) <EOL> context . method = ssl . SSL . TLSv1_METHOD <EOL> return context <EOL> def __cbLoginCaps ( self , capabilities , username , password ) : <EOL> tryTLS = '<STR_LIT>' in capabilities <EOL> tlsableTransport = interfaces . ITLSTransport ( self . transport , None ) is not None <EOL> nontlsTransport = interfaces . ISSLTransport ( self . transport , None ) is None <EOL> if not self . startedTLS and tryTLS and tlsableTransport and nontlsTransport : <EOL> d = self . startTLS ( ) <EOL> d . addCallbacks ( <EOL> self . __cbLoginTLS , <EOL> self . __ebLoginTLS , <EOL> callbackArgs = ( username , password ) , <EOL> ) <EOL> return d <EOL> else : <EOL> if nontlsTransport : <EOL> log . msg ( "<STR_LIT>" ) <EOL> args = '<STR_LIT:U+0020>' . join ( ( _quote ( username ) , _quote ( password ) ) ) <EOL> return self . sendCommand ( Command ( '<STR_LIT>' , args ) ) <EOL> def _startedTLS ( self , result , context ) : <EOL> self . transport . startTLS ( context ) <EOL> self . _capCache = None <EOL> self . startedTLS = True <EOL> return result <EOL> def __cbLoginTLS ( self , result , username , password ) : <EOL> args = '<STR_LIT:U+0020>' . join ( ( _quote ( username ) , _quote ( password ) ) ) <EOL> return self . sendCommand ( Command ( '<STR_LIT>' , args ) ) <EOL> def __ebLoginTLS ( self , failure ) : <EOL> log . err ( failure ) <EOL> return failure <EOL> def namespace ( self ) : <EOL> """<STR_LIT>""" <EOL> cmd = '<STR_LIT>' <EOL> resp = ( '<STR_LIT>' , ) <EOL> d = self . sendCommand ( Command ( cmd , wantResponse = resp ) ) <EOL> d . addCallback ( self . __cbNamespace ) <EOL> return d <EOL> def __cbNamespace ( self , ( lines , last ) ) : <EOL> for parts in lines : <EOL> if len ( parts ) == <NUM_LIT:4> and parts [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> return [ e or [ ] for e in parts [ <NUM_LIT:1> : ] ] <EOL> log . err ( "<STR_LIT>" ) <EOL> return [ [ ] , [ ] , [ ] ] <EOL> def select ( self , mailbox ) : <EOL> """<STR_LIT>""" <EOL> cmd = '<STR_LIT>' <EOL> args = _prepareMailboxName ( mailbox ) <EOL> resp = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> d = self . sendCommand ( Command ( cmd , args , wantResponse = resp ) ) <EOL> d . addCallback ( self . __cbSelect , <NUM_LIT:1> ) <EOL> return d <EOL> def examine ( self , mailbox ) : <EOL> """<STR_LIT>""" <EOL> cmd = '<STR_LIT>' <EOL> args = _prepareMailboxName ( mailbox ) <EOL> resp = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> d = self . sendCommand ( Command ( cmd , args , wantResponse = resp ) ) <EOL> d . addCallback ( self . __cbSelect , <NUM_LIT:0> ) <EOL> return d <EOL> def _intOrRaise ( self , value , phrase ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return int ( value ) <EOL> except ValueError : <EOL> raise IllegalServerResponse ( phrase ) <EOL> def __cbSelect ( self , ( lines , tagline ) , rw ) : <EOL> """<STR_LIT>""" <EOL> datum = { '<STR_LIT>' : rw } <EOL> lines . append ( parseNestedParens ( tagline ) ) <EOL> for split in lines : <EOL> if len ( split ) > <NUM_LIT:0> and split [ <NUM_LIT:0> ] . upper ( ) == '<STR_LIT:OK>' : <EOL> content = split [ <NUM_LIT:1> ] <EOL> key = content [ <NUM_LIT:0> ] . upper ( ) <EOL> if key == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = False <EOL> elif key == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = True <EOL> elif key == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = self . _intOrRaise ( <EOL> content [ <NUM_LIT:1> ] , split ) <EOL> elif key == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = self . _intOrRaise ( content [ <NUM_LIT:1> ] , split ) <EOL> elif key == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = self . _intOrRaise ( content [ <NUM_LIT:1> ] , split ) <EOL> elif key == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = tuple ( content [ <NUM_LIT:1> ] ) <EOL> else : <EOL> log . err ( '<STR_LIT>' % ( split , ) ) <EOL> elif len ( split ) == <NUM_LIT:2> : <EOL> if split [ <NUM_LIT:0> ] . upper ( ) == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = tuple ( split [ <NUM_LIT:1> ] ) <EOL> elif isinstance ( split [ <NUM_LIT:1> ] , str ) : <EOL> if split [ <NUM_LIT:1> ] . upper ( ) == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = self . _intOrRaise ( split [ <NUM_LIT:0> ] , split ) <EOL> elif split [ <NUM_LIT:1> ] . upper ( ) == '<STR_LIT>' : <EOL> datum [ '<STR_LIT>' ] = self . _intOrRaise ( split [ <NUM_LIT:0> ] , split ) <EOL> else : <EOL> log . err ( '<STR_LIT>' % ( split , ) ) <EOL> else : <EOL> log . err ( '<STR_LIT>' % ( split , ) ) <EOL> else : <EOL> log . err ( '<STR_LIT>' % ( split , ) ) <EOL> return datum <EOL> def create ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . sendCommand ( Command ( '<STR_LIT>' , _prepareMailboxName ( name ) ) ) <EOL> def delete ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . sendCommand ( Command ( '<STR_LIT>' , _prepareMailboxName ( name ) ) ) <EOL> def rename ( self , oldname , newname ) : <EOL> """<STR_LIT>""" <EOL> oldname = _prepareMailboxName ( oldname ) <EOL> newname = _prepareMailboxName ( newname ) <EOL> return self . sendCommand ( Command ( '<STR_LIT>' , '<STR_LIT:U+0020>' . join ( ( oldname , newname ) ) ) ) <EOL> def subscribe ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . sendCommand ( Command ( '<STR_LIT>' , _prepareMailboxName ( name ) ) ) <EOL> def unsubscribe ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . sendCommand ( Command ( '<STR_LIT>' , _prepareMailboxName ( name ) ) ) <EOL> def list ( self , reference , wildcard ) : <EOL> """<STR_LIT>""" <EOL> cmd = '<STR_LIT>' <EOL> args = '<STR_LIT>' % ( reference , wildcard . encode ( '<STR_LIT>' ) ) <EOL> resp = ( '<STR_LIT>' , ) <EOL> d = self . sendCommand ( Command ( cmd , args , wantResponse = resp ) ) <EOL> d . addCallback ( self . __cbList , '<STR_LIT>' ) <EOL> return d <EOL> def lsub ( self , reference , wildcard ) : <EOL> """<STR_LIT>""" <EOL> cmd = '<STR_LIT>' <EOL> args = '<STR_LIT>' % ( reference , wildcard . encode ( '<STR_LIT>' ) ) <EOL> resp = ( '<STR_LIT>' , ) <EOL> d = self . sendCommand ( Command ( cmd , args , wantResponse = resp ) ) <EOL> d . addCallback ( self . __cbList , '<STR_LIT>' ) <EOL> return d <EOL> def __cbList ( self , ( lines , last ) , command ) : <EOL> results = [ ] <EOL> for parts in lines : <EOL> if len ( parts ) == <NUM_LIT:4> and parts [ <NUM_LIT:0> ] == command : <EOL> parts [ <NUM_LIT:1> ] = tuple ( parts [ <NUM_LIT:1> ] ) <EOL> results . append ( tuple ( parts [ <NUM_LIT:1> : ] ) ) <EOL> return results <EOL> def status ( self , mailbox , * names ) : <EOL> """<STR_LIT>""" <EOL> cmd = '<STR_LIT>' <EOL> args = "<STR_LIT>" % ( _prepareMailboxName ( mailbox ) , '<STR_LIT:U+0020>' . join ( names ) ) <EOL> resp = ( '<STR_LIT>' , ) <EOL> d = self . sendCommand ( Command ( cmd , args , wantResponse = resp ) ) <EOL> d . addCallback ( self . __cbStatus ) <EOL> return d <EOL> def __cbStatus ( self , ( lines , last ) ) : <EOL> status = { } <EOL> for parts in lines : <EOL> if parts [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> items = parts [ <NUM_LIT:2> ] <EOL> items = [ items [ i : i + <NUM_LIT:2> ] for i in range ( <NUM_LIT:0> , len ( items ) , <NUM_LIT:2> ) ] <EOL> status . update ( dict ( items ) ) <EOL> for k in status . keys ( ) : <EOL> t = self . STATUS_TRANSFORMATIONS . get ( k ) <EOL> if t : <EOL> try : <EOL> status [ k ] = t ( status [ k ] ) <EOL> except Exception , e : <EOL> raise IllegalServerResponse ( '<STR_LIT>' % ( k , status [ k ] , str ( e ) ) ) <EOL> return status <EOL> def append ( self , mailbox , message , flags = ( ) , date = None ) : <EOL> """<STR_LIT>""" <EOL> message . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> L = message . tell ( ) <EOL> message . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> fmt = '<STR_LIT>' <EOL> if date : <EOL> date = '<STR_LIT>' % date <EOL> else : <EOL> date = '<STR_LIT>' <EOL> cmd = fmt % ( <EOL> _prepareMailboxName ( mailbox ) , '<STR_LIT:U+0020>' . join ( flags ) , <EOL> date , L <EOL> ) <EOL> d = self . sendCommand ( Command ( '<STR_LIT>' , cmd , ( ) , self . __cbContinueAppend , message ) ) <EOL> return d <EOL> def __cbContinueAppend ( self , lines , message ) : <EOL> s = basic . FileSender ( ) <EOL> return s . beginFileTransfer ( message , self . transport , None <EOL> ) . addCallback ( self . __cbFinishAppend ) <EOL> def __cbFinishAppend ( self , foo ) : <EOL> self . sendLine ( '<STR_LIT>' ) <EOL> def check ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sendCommand ( Command ( '<STR_LIT>' ) ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sendCommand ( Command ( '<STR_LIT>' ) ) <EOL> def expunge ( self ) : <EOL> """<STR_LIT>""" <EOL> cmd = '<STR_LIT>' <EOL> resp = ( '<STR_LIT>' , ) <EOL> d = self . sendCommand ( Command ( cmd , wantResponse = resp ) ) <EOL> d . addCallback ( self . __cbExpunge ) <EOL> return d <EOL> def __cbExpunge ( self , ( lines , last ) ) : <EOL> ids = [ ] <EOL> for parts in lines : <EOL> if len ( parts ) == <NUM_LIT:2> and parts [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> ids . append ( self . _intOrRaise ( parts [ <NUM_LIT:0> ] , parts ) ) <EOL> return ids <EOL> def search ( self , * queries , ** kwarg ) : <EOL> """<STR_LIT>""" <EOL> if kwarg . get ( '<STR_LIT>' ) : <EOL> cmd = '<STR_LIT>' <EOL> else : <EOL> cmd = '<STR_LIT>' <EOL> args = '<STR_LIT:U+0020>' . join ( queries ) <EOL> d = self . sendCommand ( Command ( cmd , args , wantResponse = ( cmd , ) ) ) <EOL> d . addCallback ( self . __cbSearch ) <EOL> return d <EOL> def __cbSearch ( self , ( lines , end ) ) : <EOL> ids = [ ] <EOL> for parts in lines : <EOL> if len ( parts ) > <NUM_LIT:0> and parts [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> ids . extend ( [ self . _intOrRaise ( p , parts ) for p in parts [ <NUM_LIT:1> : ] ] ) <EOL> return ids <EOL> def fetchUID ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( messages , useUID = uid , uid = <NUM_LIT:1> ) <EOL> def fetchFlags ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( str ( messages ) , useUID = uid , flags = <NUM_LIT:1> ) <EOL> def fetchInternalDate ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( str ( messages ) , useUID = uid , internaldate = <NUM_LIT:1> ) <EOL> def fetchEnvelope ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( str ( messages ) , useUID = uid , envelope = <NUM_LIT:1> ) <EOL> def fetchBodyStructure ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( messages , useUID = uid , bodystructure = <NUM_LIT:1> ) <EOL> def fetchSimplifiedBody ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( messages , useUID = uid , body = <NUM_LIT:1> ) <EOL> def fetchMessage ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( messages , useUID = uid , rfc822 = <NUM_LIT:1> ) <EOL> def fetchHeaders ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( messages , useUID = uid , rfc822header = <NUM_LIT:1> ) <EOL> def fetchBody ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( messages , useUID = uid , rfc822text = <NUM_LIT:1> ) <EOL> def fetchSize ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( messages , useUID = uid , rfc822size = <NUM_LIT:1> ) <EOL> def fetchFull ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( <EOL> messages , useUID = uid , flags = <NUM_LIT:1> , internaldate = <NUM_LIT:1> , <EOL> rfc822size = <NUM_LIT:1> , envelope = <NUM_LIT:1> , body = <NUM_LIT:1> ) <EOL> def fetchAll ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( <EOL> messages , useUID = uid , flags = <NUM_LIT:1> , internaldate = <NUM_LIT:1> , <EOL> rfc822size = <NUM_LIT:1> , envelope = <NUM_LIT:1> ) <EOL> def fetchFast ( self , messages , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _fetch ( <EOL> messages , useUID = uid , flags = <NUM_LIT:1> , internaldate = <NUM_LIT:1> , rfc822size = <NUM_LIT:1> ) <EOL> def _parseFetchPairs ( self , fetchResponseList ) : <EOL> """<STR_LIT>""" <EOL> values = { } <EOL> responseParts = iter ( fetchResponseList ) <EOL> while True : <EOL> try : <EOL> key = responseParts . next ( ) <EOL> except StopIteration : <EOL> break <EOL> try : <EOL> value = responseParts . next ( ) <EOL> except StopIteration : <EOL> raise IllegalServerResponse ( <EOL> "<STR_LIT>" , fetchResponseList ) <EOL> if key in ( "<STR_LIT>" , "<STR_LIT>" ) and isinstance ( value , list ) and len ( value ) < <NUM_LIT:3> : <EOL> if len ( value ) < <NUM_LIT:2> : <EOL> key = ( key , tuple ( value ) ) <EOL> else : <EOL> key = ( key , ( value [ <NUM_LIT:0> ] , tuple ( value [ <NUM_LIT:1> ] ) ) ) <EOL> try : <EOL> value = responseParts . next ( ) <EOL> except StopIteration : <EOL> raise IllegalServerResponse ( <EOL> "<STR_LIT>" , fetchResponseList ) <EOL> if value . startswith ( '<STR_LIT:<>' ) and value . endswith ( '<STR_LIT:>>' ) : <EOL> try : <EOL> int ( value [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> key = key + ( value , ) <EOL> try : <EOL> value = responseParts . next ( ) <EOL> except StopIteration : <EOL> raise IllegalServerResponse ( <EOL> "<STR_LIT>" , fetchResponseList ) <EOL> values [ key ] = value <EOL> return values <EOL> def _cbFetch ( self , ( lines , last ) , requestedParts , structured ) : <EOL> info = { } <EOL> for parts in lines : <EOL> if len ( parts ) == <NUM_LIT:3> and parts [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> id = self . _intOrRaise ( parts [ <NUM_LIT:0> ] , parts ) <EOL> if id not in info : <EOL> info [ id ] = [ parts [ <NUM_LIT:2> ] ] <EOL> else : <EOL> info [ id ] [ <NUM_LIT:0> ] . extend ( parts [ <NUM_LIT:2> ] ) <EOL> results = { } <EOL> for ( messageId , values ) in info . iteritems ( ) : <EOL> mapping = self . _parseFetchPairs ( values [ <NUM_LIT:0> ] ) <EOL> results . setdefault ( messageId , { } ) . update ( mapping ) <EOL> flagChanges = { } <EOL> for messageId in results . keys ( ) : <EOL> values = results [ messageId ] <EOL> for part in values . keys ( ) : <EOL> if part not in requestedParts and part == '<STR_LIT>' : <EOL> flagChanges [ messageId ] = values [ '<STR_LIT>' ] <EOL> for i in range ( len ( info [ messageId ] [ <NUM_LIT:0> ] ) ) : <EOL> if info [ messageId ] [ <NUM_LIT:0> ] [ i ] == '<STR_LIT>' : <EOL> del info [ messageId ] [ <NUM_LIT:0> ] [ i : i + <NUM_LIT:2> ] <EOL> break <EOL> del values [ '<STR_LIT>' ] <EOL> if not values : <EOL> del results [ messageId ] <EOL> if flagChanges : <EOL> self . flagsChanged ( flagChanges ) <EOL> if structured : <EOL> return results <EOL> else : <EOL> return info <EOL> def fetchSpecific ( self , messages , uid = <NUM_LIT:0> , headerType = None , <EOL> headerNumber = None , headerArgs = None , peek = None , <EOL> offset = None , length = None ) : <EOL> """<STR_LIT>""" <EOL> fmt = '<STR_LIT>' <EOL> if headerNumber is None : <EOL> number = '<STR_LIT>' <EOL> elif isinstance ( headerNumber , int ) : <EOL> number = str ( headerNumber ) <EOL> else : <EOL> number = '<STR_LIT:.>' . join ( map ( str , headerNumber ) ) <EOL> if headerType is None : <EOL> header = '<STR_LIT>' <EOL> elif number : <EOL> header = '<STR_LIT:.>' + headerType <EOL> else : <EOL> header = headerType <EOL> if header and headerType not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if headerArgs is not None : <EOL> payload = '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( headerArgs ) <EOL> else : <EOL> payload = '<STR_LIT>' <EOL> else : <EOL> payload = '<STR_LIT>' <EOL> if offset is None : <EOL> extra = '<STR_LIT>' <EOL> else : <EOL> extra = '<STR_LIT>' % ( offset , length ) <EOL> fetch = uid and '<STR_LIT>' or '<STR_LIT>' <EOL> cmd = fmt % ( messages , peek and '<STR_LIT>' or '<STR_LIT>' , number , header , payload , extra ) <EOL> d = self . sendCommand ( Command ( fetch , cmd , wantResponse = ( '<STR_LIT>' , ) ) ) <EOL> d . addCallback ( self . _cbFetch , ( ) , False ) <EOL> return d <EOL> def _fetch ( self , messages , useUID = <NUM_LIT:0> , ** terms ) : <EOL> fetch = useUID and '<STR_LIT>' or '<STR_LIT>' <EOL> if '<STR_LIT>' in terms : <EOL> del terms [ '<STR_LIT>' ] <EOL> terms [ '<STR_LIT>' ] = True <EOL> if '<STR_LIT>' in terms : <EOL> del terms [ '<STR_LIT>' ] <EOL> terms [ '<STR_LIT>' ] = True <EOL> if '<STR_LIT>' in terms : <EOL> del terms [ '<STR_LIT>' ] <EOL> terms [ '<STR_LIT>' ] = True <EOL> cmd = '<STR_LIT>' % ( messages , '<STR_LIT:U+0020>' . join ( [ s . upper ( ) for s in terms . keys ( ) ] ) ) <EOL> d = self . sendCommand ( Command ( fetch , cmd , wantResponse = ( '<STR_LIT>' , ) ) ) <EOL> d . addCallback ( self . _cbFetch , map ( str . upper , terms . keys ( ) ) , True ) <EOL> return d <EOL> def setFlags ( self , messages , flags , silent = <NUM_LIT:1> , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _store ( str ( messages ) , '<STR_LIT>' , silent , flags , uid ) <EOL> def addFlags ( self , messages , flags , silent = <NUM_LIT:1> , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _store ( str ( messages ) , '<STR_LIT>' , silent , flags , uid ) <EOL> def removeFlags ( self , messages , flags , silent = <NUM_LIT:1> , uid = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> return self . _store ( str ( messages ) , '<STR_LIT>' , silent , flags , uid ) <EOL> def _store ( self , messages , cmd , silent , flags , uid ) : <EOL> if silent : <EOL> cmd = cmd + '<STR_LIT>' <EOL> store = uid and '<STR_LIT>' or '<STR_LIT>' <EOL> args = '<STR_LIT:U+0020>' . join ( ( messages , cmd , '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( flags ) ) ) <EOL> d = self . sendCommand ( Command ( store , args , wantResponse = ( '<STR_LIT>' , ) ) ) <EOL> expected = ( ) <EOL> if not silent : <EOL> expected = ( '<STR_LIT>' , ) <EOL> d . addCallback ( self . _cbFetch , expected , True ) <EOL> return d <EOL> def copy ( self , messages , mailbox , uid ) : <EOL> """<STR_LIT>""" <EOL> if uid : <EOL> cmd = '<STR_LIT>' <EOL> else : <EOL> cmd = '<STR_LIT>' <EOL> args = '<STR_LIT>' % ( messages , _prepareMailboxName ( mailbox ) ) <EOL> return self . sendCommand ( Command ( cmd , args ) ) <EOL> def modeChanged ( self , writeable ) : <EOL> """<STR_LIT>""" <EOL> def flagsChanged ( self , newFlags ) : <EOL> """<STR_LIT>""" <EOL> def newMessages ( self , exists , recent ) : <EOL> """<STR_LIT>""" <EOL> class IllegalIdentifierError ( IMAP4Exception ) : pass <EOL> def parseIdList ( s , lastMessageId = None ) : <EOL> """<STR_LIT>""" <EOL> res = MessageSet ( ) <EOL> parts = s . split ( '<STR_LIT:U+002C>' ) <EOL> for p in parts : <EOL> if '<STR_LIT::>' in p : <EOL> low , high = p . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> try : <EOL> if low == '<STR_LIT:*>' : <EOL> low = None <EOL> else : <EOL> low = long ( low ) <EOL> if high == '<STR_LIT:*>' : <EOL> high = None <EOL> else : <EOL> high = long ( high ) <EOL> if low is high is None : <EOL> raise IllegalIdentifierError ( p ) <EOL> if ( ( low is not None and low <= <NUM_LIT:0> ) or <EOL> ( high is not None and high <= <NUM_LIT:0> ) ) : <EOL> raise IllegalIdentifierError ( p ) <EOL> high = high or lastMessageId <EOL> low = low or lastMessageId <EOL> if low > high : <EOL> low , high = high , low <EOL> res . extend ( ( low , high ) ) <EOL> except ValueError : <EOL> raise IllegalIdentifierError ( p ) <EOL> else : <EOL> try : <EOL> if p == '<STR_LIT:*>' : <EOL> p = None <EOL> else : <EOL> p = long ( p ) <EOL> if p is not None and p <= <NUM_LIT:0> : <EOL> raise IllegalIdentifierError ( p ) <EOL> except ValueError : <EOL> raise IllegalIdentifierError ( p ) <EOL> else : <EOL> res . extend ( p or lastMessageId ) <EOL> return res <EOL> class IllegalQueryError ( IMAP4Exception ) : pass <EOL> _SIMPLE_BOOL = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<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> ) <EOL> _NO_QUOTES = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ) <EOL> def Query ( sorted = <NUM_LIT:0> , ** kwarg ) : <EOL> """<STR_LIT>""" <EOL> cmd = [ ] <EOL> keys = kwarg . keys ( ) <EOL> if sorted : <EOL> keys . sort ( ) <EOL> for k in keys : <EOL> v = kwarg [ k ] <EOL> k = k . upper ( ) <EOL> if k in _SIMPLE_BOOL and v : <EOL> cmd . append ( k ) <EOL> elif k == '<STR_LIT>' : <EOL> cmd . extend ( [ k , v [ <NUM_LIT:0> ] , '<STR_LIT>' % ( v [ <NUM_LIT:1> ] , ) ] ) <EOL> elif k not in _NO_QUOTES : <EOL> cmd . extend ( [ k , '<STR_LIT>' % ( v , ) ] ) <EOL> else : <EOL> cmd . extend ( [ k , '<STR_LIT:%s>' % ( v , ) ] ) <EOL> if len ( cmd ) > <NUM_LIT:1> : <EOL> return '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( cmd ) <EOL> else : <EOL> return '<STR_LIT:U+0020>' . join ( cmd ) <EOL> def Or ( * args ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> raise IllegalQueryError , args <EOL> elif len ( args ) == <NUM_LIT:2> : <EOL> return '<STR_LIT>' % args <EOL> else : <EOL> return '<STR_LIT>' % ( args [ <NUM_LIT:0> ] , Or ( * args [ <NUM_LIT:1> : ] ) ) <EOL> def Not ( query ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( query , ) <EOL> class MismatchedNesting ( IMAP4Exception ) : <EOL> pass <EOL> class MismatchedQuoting ( IMAP4Exception ) : <EOL> pass <EOL> def wildcardToRegexp ( wildcard , delim = None ) : <EOL> wildcard = wildcard . replace ( '<STR_LIT:*>' , '<STR_LIT>' ) <EOL> if delim is None : <EOL> wildcard = wildcard . replace ( '<STR_LIT:%>' , '<STR_LIT>' ) <EOL> else : <EOL> wildcard = wildcard . replace ( '<STR_LIT:%>' , '<STR_LIT>' % re . escape ( delim ) ) <EOL> return re . compile ( wildcard , re . I ) <EOL> def splitQuoted ( s ) : <EOL> """<STR_LIT>""" <EOL> s = s . strip ( ) <EOL> result = [ ] <EOL> word = [ ] <EOL> inQuote = inWord = False <EOL> for i , c in enumerate ( s ) : <EOL> if c == '<STR_LIT:">' : <EOL> if i and s [ i - <NUM_LIT:1> ] == '<STR_LIT:\\>' : <EOL> word . pop ( ) <EOL> word . append ( '<STR_LIT:">' ) <EOL> elif not inQuote : <EOL> inQuote = True <EOL> else : <EOL> inQuote = False <EOL> result . append ( '<STR_LIT>' . join ( word ) ) <EOL> word = [ ] <EOL> elif not inWord and not inQuote and c not in ( '<STR_LIT:">' + string . whitespace ) : <EOL> inWord = True <EOL> word . append ( c ) <EOL> elif inWord and not inQuote and c in string . whitespace : <EOL> w = '<STR_LIT>' . join ( word ) <EOL> if w == '<STR_LIT>' : <EOL> result . append ( None ) <EOL> else : <EOL> result . append ( w ) <EOL> word = [ ] <EOL> inWord = False <EOL> elif inWord or inQuote : <EOL> word . append ( c ) <EOL> if inQuote : <EOL> raise MismatchedQuoting ( s ) <EOL> if inWord : <EOL> w = '<STR_LIT>' . join ( word ) <EOL> if w == '<STR_LIT>' : <EOL> result . append ( None ) <EOL> else : <EOL> result . append ( w ) <EOL> return result <EOL> def splitOn ( sequence , predicate , transformers ) : <EOL> result = [ ] <EOL> mode = predicate ( sequence [ <NUM_LIT:0> ] ) <EOL> tmp = [ sequence [ <NUM_LIT:0> ] ] <EOL> for e in sequence [ <NUM_LIT:1> : ] : <EOL> p = predicate ( e ) <EOL> if p != mode : <EOL> result . extend ( transformers [ mode ] ( tmp ) ) <EOL> tmp = [ e ] <EOL> mode = p <EOL> else : <EOL> tmp . append ( e ) <EOL> result . extend ( transformers [ mode ] ( tmp ) ) <EOL> return result <EOL> def collapseStrings ( results ) : <EOL> """<STR_LIT>""" <EOL> copy = [ ] <EOL> begun = None <EOL> listsList = [ isinstance ( s , types . ListType ) for s in results ] <EOL> pred = lambda e : isinstance ( e , types . TupleType ) <EOL> tran = { <EOL> <NUM_LIT:0> : lambda e : splitQuoted ( '<STR_LIT>' . join ( e ) ) , <EOL> <NUM_LIT:1> : lambda e : [ '<STR_LIT>' . join ( [ i [ <NUM_LIT:0> ] for i in e ] ) ] <EOL> } <EOL> for ( i , c , isList ) in zip ( range ( len ( results ) ) , results , listsList ) : <EOL> if isList : <EOL> if begun is not None : <EOL> copy . extend ( splitOn ( results [ begun : i ] , pred , tran ) ) <EOL> begun = None <EOL> copy . append ( collapseStrings ( c ) ) <EOL> elif begun is None : <EOL> begun = i <EOL> if begun is not None : <EOL> copy . extend ( splitOn ( results [ begun : ] , pred , tran ) ) <EOL> return copy <EOL> def parseNestedParens ( s , handleLiteral = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> s = s . strip ( ) <EOL> inQuote = <NUM_LIT:0> <EOL> contentStack = [ [ ] ] <EOL> try : <EOL> i = <NUM_LIT:0> <EOL> L = len ( s ) <EOL> while i < L : <EOL> c = s [ i ] <EOL> if inQuote : <EOL> if c == '<STR_LIT:\\>' : <EOL> contentStack [ - <NUM_LIT:1> ] . append ( s [ i : i + <NUM_LIT:2> ] ) <EOL> i += <NUM_LIT:2> <EOL> continue <EOL> elif c == '<STR_LIT:">' : <EOL> inQuote = not inQuote <EOL> contentStack [ - <NUM_LIT:1> ] . append ( c ) <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> if c == '<STR_LIT:">' : <EOL> contentStack [ - <NUM_LIT:1> ] . append ( c ) <EOL> inQuote = not inQuote <EOL> i += <NUM_LIT:1> <EOL> elif handleLiteral and c == '<STR_LIT:{>' : <EOL> end = s . find ( '<STR_LIT:}>' , i ) <EOL> if end == - <NUM_LIT:1> : <EOL> raise ValueError , "<STR_LIT>" <EOL> literalSize = int ( s [ i + <NUM_LIT:1> : end ] ) <EOL> contentStack [ - <NUM_LIT:1> ] . append ( ( s [ end + <NUM_LIT:3> : end + <NUM_LIT:3> + literalSize ] , ) ) <EOL> i = end + <NUM_LIT:3> + literalSize <EOL> elif c == '<STR_LIT:(>' or c == '<STR_LIT:[>' : <EOL> contentStack . append ( [ ] ) <EOL> i += <NUM_LIT:1> <EOL> elif c == '<STR_LIT:)>' or c == '<STR_LIT:]>' : <EOL> contentStack [ - <NUM_LIT:2> ] . append ( contentStack . pop ( ) ) <EOL> i += <NUM_LIT:1> <EOL> else : <EOL> contentStack [ - <NUM_LIT:1> ] . append ( c ) <EOL> i += <NUM_LIT:1> <EOL> except IndexError : <EOL> raise MismatchedNesting ( s ) <EOL> if len ( contentStack ) != <NUM_LIT:1> : <EOL> raise MismatchedNesting ( s ) <EOL> return collapseStrings ( contentStack [ <NUM_LIT:0> ] ) <EOL> def _quote ( s ) : <EOL> return '<STR_LIT>' % ( s . replace ( '<STR_LIT:\\>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , '<STR_LIT>' ) , ) <EOL> def _literal ( s ) : <EOL> return '<STR_LIT>' % ( len ( s ) , s ) <EOL> class DontQuoteMe : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return str ( self . value ) <EOL> _ATOM_SPECIALS = '<STR_LIT>' <EOL> def _needsQuote ( s ) : <EOL> if s == '<STR_LIT>' : <EOL> return <NUM_LIT:1> <EOL> for c in s : <EOL> if c < '<STR_LIT>' or c > '<STR_LIT>' : <EOL> return <NUM_LIT:1> <EOL> if c in _ATOM_SPECIALS : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def _prepareMailboxName ( name ) : <EOL> name = name . encode ( '<STR_LIT>' ) <EOL> if _needsQuote ( name ) : <EOL> return _quote ( name ) <EOL> return name <EOL> def _needsLiteral ( s ) : <EOL> return '<STR_LIT:\n>' in s or '<STR_LIT:\r>' in s or len ( s ) > <NUM_LIT:1000> <EOL> def collapseNestedLists ( items ) : <EOL> """<STR_LIT>""" <EOL> pieces = [ ] <EOL> for i in items : <EOL> if i is None : <EOL> pieces . extend ( [ '<STR_LIT:U+0020>' , '<STR_LIT>' ] ) <EOL> elif isinstance ( i , ( DontQuoteMe , int , long ) ) : <EOL> pieces . extend ( [ '<STR_LIT:U+0020>' , str ( i ) ] ) <EOL> elif isinstance ( i , types . StringTypes ) : <EOL> if _needsLiteral ( i ) : <EOL> pieces . extend ( [ '<STR_LIT:U+0020>' , '<STR_LIT:{>' , str ( len ( i ) ) , '<STR_LIT:}>' , IMAP4Server . delimiter , i ] ) <EOL> else : <EOL> pieces . extend ( [ '<STR_LIT:U+0020>' , _quote ( i ) ] ) <EOL> elif hasattr ( i , '<STR_LIT>' ) : <EOL> d = i . read ( ) <EOL> pieces . extend ( [ '<STR_LIT:U+0020>' , '<STR_LIT:{>' , str ( len ( d ) ) , '<STR_LIT:}>' , IMAP4Server . delimiter , d ] ) <EOL> else : <EOL> pieces . extend ( [ '<STR_LIT:U+0020>' , '<STR_LIT>' % ( collapseNestedLists ( i ) , ) ] ) <EOL> return '<STR_LIT>' . join ( pieces [ <NUM_LIT:1> : ] ) <EOL> class IClientAuthentication ( Interface ) : <EOL> def getName ( ) : <EOL> """<STR_LIT>""" <EOL> def challengeResponse ( secret , challenge ) : <EOL> """<STR_LIT>""" <EOL> class CramMD5ClientAuthenticator : <EOL> implements ( IClientAuthentication ) <EOL> def __init__ ( self , user ) : <EOL> self . user = user <EOL> def getName ( self ) : <EOL> return "<STR_LIT>" <EOL> def challengeResponse ( self , secret , chal ) : <EOL> response = hmac . HMAC ( secret , chal ) . hexdigest ( ) <EOL> return '<STR_LIT>' % ( self . user , response ) <EOL> class LOGINAuthenticator : <EOL> implements ( IClientAuthentication ) <EOL> def __init__ ( self , user ) : <EOL> self . user = user <EOL> self . challengeResponse = self . challengeUsername <EOL> def getName ( self ) : <EOL> return "<STR_LIT>" <EOL> def challengeUsername ( self , secret , chal ) : <EOL> self . challengeResponse = self . challengeSecret <EOL> return self . user <EOL> def challengeSecret ( self , secret , chal ) : <EOL> return secret <EOL> class PLAINAuthenticator : <EOL> implements ( IClientAuthentication ) <EOL> def __init__ ( self , user ) : <EOL> self . user = user <EOL> def getName ( self ) : <EOL> return "<STR_LIT>" <EOL> def challengeResponse ( self , secret , chal ) : <EOL> return '<STR_LIT>' % ( self . user , secret ) <EOL> class MailboxException ( IMAP4Exception ) : pass <EOL> class MailboxCollision ( MailboxException ) : <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . args <EOL> class NoSuchMailbox ( MailboxException ) : <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . args <EOL> class ReadOnlyMailbox ( MailboxException ) : <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class IAccount ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def addMailbox ( name , mbox = None ) : <EOL> """<STR_LIT>""" <EOL> def create ( pathspec ) : <EOL> """<STR_LIT>""" <EOL> def select ( name , rw = True ) : <EOL> """<STR_LIT>""" <EOL> def delete ( name ) : <EOL> """<STR_LIT>""" <EOL> def rename ( oldname , newname ) : <EOL> """<STR_LIT>""" <EOL> def isSubscribed ( name ) : <EOL> """<STR_LIT>""" <EOL> def subscribe ( name ) : <EOL> """<STR_LIT>""" <EOL> def unsubscribe ( name ) : <EOL> """<STR_LIT>""" <EOL> def listMailboxes ( ref , wildcard ) : <EOL> """<STR_LIT>""" <EOL> class INamespacePresenter ( Interface ) : <EOL> def getPersonalNamespaces ( ) : <EOL> """<STR_LIT>""" <EOL> def getSharedNamespaces ( ) : <EOL> """<STR_LIT>""" <EOL> def getUserNamespaces ( ) : <EOL> """<STR_LIT>""" <EOL> class MemoryAccount ( object ) : <EOL> implements ( IAccount , INamespacePresenter ) <EOL> mailboxes = None <EOL> subscriptions = None <EOL> top_id = <NUM_LIT:0> <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> self . mailboxes = { } <EOL> self . subscriptions = [ ] <EOL> def allocateID ( self ) : <EOL> id = self . top_id <EOL> self . top_id += <NUM_LIT:1> <EOL> return id <EOL> def addMailbox ( self , name , mbox = None ) : <EOL> name = name . upper ( ) <EOL> if name in self . mailboxes : <EOL> raise MailboxCollision , name <EOL> if mbox is None : <EOL> mbox = self . _emptyMailbox ( name , self . allocateID ( ) ) <EOL> self . mailboxes [ name ] = mbox <EOL> return <NUM_LIT:1> <EOL> def create ( self , pathspec ) : <EOL> paths = filter ( None , pathspec . split ( '<STR_LIT:/>' ) ) <EOL> for accum in range ( <NUM_LIT:1> , len ( paths ) ) : <EOL> try : <EOL> self . addMailbox ( '<STR_LIT:/>' . join ( paths [ : accum ] ) ) <EOL> except MailboxCollision : <EOL> pass <EOL> try : <EOL> self . addMailbox ( '<STR_LIT:/>' . join ( paths ) ) <EOL> except MailboxCollision : <EOL> if not pathspec . endswith ( '<STR_LIT:/>' ) : <EOL> return False <EOL> return True <EOL> def _emptyMailbox ( self , name , id ) : <EOL> raise NotImplementedError <EOL> def select ( self , name , readwrite = <NUM_LIT:1> ) : <EOL> return self . mailboxes . get ( name . upper ( ) ) <EOL> def delete ( self , name ) : <EOL> name = name . upper ( ) <EOL> mbox = self . mailboxes . get ( name ) <EOL> if not mbox : <EOL> raise MailboxException ( "<STR_LIT>" ) <EOL> if r'<STR_LIT>' in mbox . getFlags ( ) : <EOL> for others in self . mailboxes . keys ( ) : <EOL> if others != name and others . startswith ( name ) : <EOL> raise MailboxException , "<STR_LIT>" <EOL> mbox . destroy ( ) <EOL> if self . _inferiorNames ( name ) > <NUM_LIT:1> : <EOL> del self . mailboxes [ name ] <EOL> def rename ( self , oldname , newname ) : <EOL> oldname = oldname . upper ( ) <EOL> newname = newname . upper ( ) <EOL> if oldname not in self . mailboxes : <EOL> raise NoSuchMailbox , oldname <EOL> inferiors = self . _inferiorNames ( oldname ) <EOL> inferiors = [ ( o , o . replace ( oldname , newname , <NUM_LIT:1> ) ) for o in inferiors ] <EOL> for ( old , new ) in inferiors : <EOL> if new in self . mailboxes : <EOL> raise MailboxCollision , new <EOL> for ( old , new ) in inferiors : <EOL> self . mailboxes [ new ] = self . mailboxes [ old ] <EOL> del self . mailboxes [ old ] <EOL> def _inferiorNames ( self , name ) : <EOL> inferiors = [ ] <EOL> for infname in self . mailboxes . keys ( ) : <EOL> if infname . startswith ( name ) : <EOL> inferiors . append ( infname ) <EOL> return inferiors <EOL> def isSubscribed ( self , name ) : <EOL> return name . upper ( ) in self . subscriptions <EOL> def subscribe ( self , name ) : <EOL> name = name . upper ( ) <EOL> if name not in self . subscriptions : <EOL> self . subscriptions . append ( name ) <EOL> def unsubscribe ( self , name ) : <EOL> name = name . upper ( ) <EOL> if name not in self . subscriptions : <EOL> raise MailboxException , "<STR_LIT>" + name <EOL> self . subscriptions . remove ( name ) <EOL> def listMailboxes ( self , ref , wildcard ) : <EOL> ref = self . _inferiorNames ( ref . upper ( ) ) <EOL> wildcard = wildcardToRegexp ( wildcard , '<STR_LIT:/>' ) <EOL> return [ ( i , self . mailboxes [ i ] ) for i in ref if wildcard . match ( i ) ] <EOL> def getPersonalNamespaces ( self ) : <EOL> return [ [ "<STR_LIT>" , "<STR_LIT:/>" ] ] <EOL> def getSharedNamespaces ( self ) : <EOL> return None <EOL> def getOtherNamespaces ( self ) : <EOL> return None <EOL> _statusRequestDict = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def statusRequestHelper ( mbox , names ) : <EOL> r = { } <EOL> for n in names : <EOL> r [ n ] = getattr ( mbox , _statusRequestDict [ n . upper ( ) ] ) ( ) <EOL> return r <EOL> def parseAddr ( addr ) : <EOL> if addr is None : <EOL> return [ ( None , None , None ) , ] <EOL> addrs = email . Utils . getaddresses ( [ addr ] ) <EOL> return [ [ fn or None , None ] + addr . split ( '<STR_LIT:@>' ) for fn , addr in addrs ] <EOL> def getEnvelope ( msg ) : <EOL> headers = msg . getHeaders ( True ) <EOL> date = headers . get ( '<STR_LIT:date>' ) <EOL> subject = headers . get ( '<STR_LIT>' ) <EOL> from_ = headers . get ( '<STR_LIT>' ) <EOL> sender = headers . get ( '<STR_LIT>' , from_ ) <EOL> reply_to = headers . get ( '<STR_LIT>' , from_ ) <EOL> to = headers . get ( '<STR_LIT:to>' ) <EOL> cc = headers . get ( '<STR_LIT>' ) <EOL> bcc = headers . get ( '<STR_LIT>' ) <EOL> in_reply_to = headers . get ( '<STR_LIT>' ) <EOL> mid = headers . get ( '<STR_LIT>' ) <EOL> return ( date , subject , parseAddr ( from_ ) , parseAddr ( sender ) , <EOL> reply_to and parseAddr ( reply_to ) , to and parseAddr ( to ) , <EOL> cc and parseAddr ( cc ) , bcc and parseAddr ( bcc ) , in_reply_to , mid ) <EOL> def getLineCount ( msg ) : <EOL> lines = <NUM_LIT:0> <EOL> for _ in msg . getBodyFile ( ) : <EOL> lines += <NUM_LIT:1> <EOL> return lines <EOL> def unquote ( s ) : <EOL> if s [ <NUM_LIT:0> ] == s [ - <NUM_LIT:1> ] == '<STR_LIT:">' : <EOL> return s [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return s <EOL> def _getContentType ( msg ) : <EOL> """<STR_LIT>""" <EOL> attrs = None <EOL> mm = msg . getHeaders ( False , '<STR_LIT>' ) . get ( '<STR_LIT>' , None ) <EOL> if mm : <EOL> mm = '<STR_LIT>' . join ( mm . splitlines ( ) ) <EOL> mimetype = mm . split ( '<STR_LIT:;>' ) <EOL> if mimetype : <EOL> type = mimetype [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> if len ( type ) == <NUM_LIT:1> : <EOL> major = type [ <NUM_LIT:0> ] <EOL> minor = None <EOL> elif len ( type ) == <NUM_LIT:2> : <EOL> major , minor = type <EOL> else : <EOL> major = minor = None <EOL> attrs = dict ( x . strip ( ) . lower ( ) . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) for x in mimetype [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> major = minor = None <EOL> else : <EOL> major = minor = None <EOL> return major , minor , attrs <EOL> def _getMessageStructure ( message ) : <EOL> """<STR_LIT>""" <EOL> main , subtype , attrs = _getContentType ( message ) <EOL> if main is not None : <EOL> main = main . lower ( ) <EOL> if subtype is not None : <EOL> subtype = subtype . lower ( ) <EOL> if main == '<STR_LIT>' : <EOL> return _MultipartMessageStructure ( message , subtype , attrs ) <EOL> elif ( main , subtype ) == ( '<STR_LIT:message>' , '<STR_LIT>' ) : <EOL> return _RFC822MessageStructure ( message , main , subtype , attrs ) <EOL> elif main == '<STR_LIT:text>' : <EOL> return _TextMessageStructure ( message , main , subtype , attrs ) <EOL> else : <EOL> return _SinglepartMessageStructure ( message , main , subtype , attrs ) <EOL> class _MessageStructure ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , attrs ) : <EOL> """<STR_LIT>""" <EOL> self . message = message <EOL> self . attrs = attrs <EOL> def _disposition ( self , disp ) : <EOL> """<STR_LIT>""" <EOL> if disp : <EOL> disp = disp . split ( '<STR_LIT>' ) <EOL> if len ( disp ) == <NUM_LIT:1> : <EOL> disp = ( disp [ <NUM_LIT:0> ] . lower ( ) , None ) <EOL> elif len ( disp ) > <NUM_LIT:1> : <EOL> params = [ x for param in disp [ <NUM_LIT:1> : ] for x in param . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) ] <EOL> disp = [ disp [ <NUM_LIT:0> ] . lower ( ) , params ] <EOL> return disp <EOL> else : <EOL> return None <EOL> def _unquotedAttrs ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . attrs : <EOL> unquoted = [ ( k , unquote ( v ) ) for ( k , v ) in self . attrs . iteritems ( ) ] <EOL> return [ y for x in sorted ( unquoted ) for y in x ] <EOL> return None <EOL> class _SinglepartMessageStructure ( _MessageStructure ) : <EOL> """<STR_LIT>""" <EOL> _HEADERS = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def __init__ ( self , message , main , subtype , attrs ) : <EOL> """<STR_LIT>""" <EOL> _MessageStructure . __init__ ( self , message , attrs ) <EOL> self . main = main <EOL> self . subtype = subtype <EOL> self . attrs = attrs <EOL> def _basicFields ( self ) : <EOL> """<STR_LIT>""" <EOL> headers = self . message . getHeaders ( False , * self . _HEADERS ) <EOL> size = self . message . getSize ( ) <EOL> major , minor = self . main , self . subtype <EOL> unquotedAttrs = self . _unquotedAttrs ( ) <EOL> return [ <EOL> major , minor , unquotedAttrs , <EOL> headers . get ( '<STR_LIT>' ) , <EOL> headers . get ( '<STR_LIT>' ) , <EOL> headers . get ( '<STR_LIT>' ) , <EOL> size , <EOL> ] <EOL> def encode ( self , extended ) : <EOL> """<STR_LIT>""" <EOL> result = self . _basicFields ( ) <EOL> if extended : <EOL> result . extend ( self . _extended ( ) ) <EOL> return result <EOL> def _extended ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> headers = self . message . getHeaders ( <EOL> False , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> result . append ( headers . get ( '<STR_LIT>' ) ) <EOL> result . append ( self . _disposition ( headers . get ( '<STR_LIT>' ) ) ) <EOL> result . append ( headers . get ( '<STR_LIT>' ) ) <EOL> result . append ( headers . get ( '<STR_LIT>' ) ) <EOL> return result <EOL> class _TextMessageStructure ( _SinglepartMessageStructure ) : <EOL> """<STR_LIT>""" <EOL> def encode ( self , extended ) : <EOL> """<STR_LIT>""" <EOL> result = _SinglepartMessageStructure . _basicFields ( self ) <EOL> result . append ( getLineCount ( self . message ) ) <EOL> if extended : <EOL> result . extend ( self . _extended ( ) ) <EOL> return result <EOL> class _RFC822MessageStructure ( _SinglepartMessageStructure ) : <EOL> """<STR_LIT>""" <EOL> def encode ( self , extended ) : <EOL> """<STR_LIT>""" <EOL> result = _SinglepartMessageStructure . encode ( self , extended ) <EOL> contained = self . message . getSubPart ( <NUM_LIT:0> ) <EOL> result . append ( getEnvelope ( contained ) ) <EOL> result . append ( getBodyStructure ( contained , False ) ) <EOL> result . append ( getLineCount ( contained ) ) <EOL> return result <EOL> class _MultipartMessageStructure ( _MessageStructure ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , subtype , attrs ) : <EOL> """<STR_LIT>""" <EOL> _MessageStructure . __init__ ( self , message , attrs ) <EOL> self . subtype = subtype <EOL> def _getParts ( self ) : <EOL> """<STR_LIT>""" <EOL> i = <NUM_LIT:0> <EOL> while True : <EOL> try : <EOL> part = self . message . getSubPart ( i ) <EOL> except IndexError : <EOL> break <EOL> else : <EOL> yield part <EOL> i += <NUM_LIT:1> <EOL> def encode ( self , extended ) : <EOL> """<STR_LIT>""" <EOL> result = [ _getMessageStructure ( p ) . encode ( extended ) for p in self . _getParts ( ) ] <EOL> result . append ( self . subtype ) <EOL> if extended : <EOL> result . extend ( self . _extended ( ) ) <EOL> return result <EOL> def _extended ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> headers = self . message . getHeaders ( <EOL> False , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> result . append ( self . _unquotedAttrs ( ) ) <EOL> result . append ( self . _disposition ( headers . get ( '<STR_LIT>' ) ) ) <EOL> result . append ( headers . get ( '<STR_LIT>' , None ) ) <EOL> result . append ( headers . get ( '<STR_LIT>' , None ) ) <EOL> return result <EOL> def getBodyStructure ( msg , extended = False ) : <EOL> """<STR_LIT>""" <EOL> return _getMessageStructure ( msg ) . encode ( extended ) <EOL> class IMessagePart ( Interface ) : <EOL> def getHeaders ( negate , * names ) : <EOL> """<STR_LIT>""" <EOL> def getBodyFile ( ) : <EOL> """<STR_LIT>""" <EOL> def getSize ( ) : <EOL> """<STR_LIT>""" <EOL> def isMultipart ( ) : <EOL> """<STR_LIT>""" <EOL> def getSubPart ( part ) : <EOL> """<STR_LIT>""" <EOL> class IMessage ( IMessagePart ) : <EOL> def getUID ( ) : <EOL> """<STR_LIT>""" <EOL> def getFlags ( ) : <EOL> """<STR_LIT>""" <EOL> def getInternalDate ( ) : <EOL> """<STR_LIT>""" <EOL> class IMessageFile ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def open ( ) : <EOL> """<STR_LIT>""" <EOL> class ISearchableMailbox ( Interface ) : <EOL> def search ( query , uid ) : <EOL> """<STR_LIT>""" <EOL> class IMessageCopier ( Interface ) : <EOL> def copy ( messageObject ) : <EOL> """<STR_LIT>""" <EOL> class IMailboxInfo ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def getFlags ( ) : <EOL> """<STR_LIT>""" <EOL> def getHierarchicalDelimiter ( ) : <EOL> """<STR_LIT>""" <EOL> class IMailbox ( IMailboxInfo ) : <EOL> def getUIDValidity ( ) : <EOL> """<STR_LIT>""" <EOL> def getUIDNext ( ) : <EOL> """<STR_LIT>""" <EOL> def getUID ( message ) : <EOL> """<STR_LIT>""" <EOL> def getMessageCount ( ) : <EOL> """<STR_LIT>""" <EOL> def getRecentCount ( ) : <EOL> """<STR_LIT>""" <EOL> def getUnseenCount ( ) : <EOL> """<STR_LIT>""" <EOL> def isWriteable ( ) : <EOL> """<STR_LIT>""" <EOL> def destroy ( ) : <EOL> """<STR_LIT>""" <EOL> def requestStatus ( names ) : <EOL> """<STR_LIT>""" <EOL> def addListener ( listener ) : <EOL> """<STR_LIT>""" <EOL> def removeListener ( listener ) : <EOL> """<STR_LIT>""" <EOL> def addMessage ( message , flags = ( ) , date = None ) : <EOL> """<STR_LIT>""" <EOL> def expunge ( ) : <EOL> """<STR_LIT>""" <EOL> def fetch ( messages , uid ) : <EOL> """<STR_LIT>""" <EOL> def store ( messages , flags , mode , uid ) : <EOL> """<STR_LIT>""" <EOL> class ICloseableMailbox ( Interface ) : <EOL> """<STR_LIT>""" <EOL> def close ( ) : <EOL> """<STR_LIT>""" <EOL> def _formatHeaders ( headers ) : <EOL> hdrs = [ '<STR_LIT>' . join ( ( k . title ( ) , '<STR_LIT:\r\n>' . join ( v . splitlines ( ) ) ) ) for ( k , v ) <EOL> in headers . iteritems ( ) ] <EOL> hdrs = '<STR_LIT:\r\n>' . join ( hdrs ) + '<STR_LIT:\r\n>' <EOL> return hdrs <EOL> def subparts ( m ) : <EOL> i = <NUM_LIT:0> <EOL> try : <EOL> while True : <EOL> yield m . getSubPart ( i ) <EOL> i += <NUM_LIT:1> <EOL> except IndexError : <EOL> pass <EOL> def iterateInReactor ( i ) : <EOL> """<STR_LIT>""" <EOL> from twisted . internet import reactor <EOL> d = defer . Deferred ( ) <EOL> def go ( last ) : <EOL> try : <EOL> r = i . next ( ) <EOL> except StopIteration : <EOL> d . callback ( last ) <EOL> except : <EOL> d . errback ( ) <EOL> else : <EOL> if isinstance ( r , defer . Deferred ) : <EOL> r . addCallback ( go ) <EOL> else : <EOL> reactor . callLater ( <NUM_LIT:0> , go , r ) <EOL> go ( None ) <EOL> return d <EOL> class MessageProducer : <EOL> CHUNK_SIZE = <NUM_LIT:2> ** <NUM_LIT:2> ** <NUM_LIT:2> ** <NUM_LIT:2> <EOL> def __init__ ( self , msg , buffer = None , scheduler = None ) : <EOL> """<STR_LIT>""" <EOL> self . msg = msg <EOL> if buffer is None : <EOL> buffer = tempfile . TemporaryFile ( ) <EOL> self . buffer = buffer <EOL> if scheduler is None : <EOL> scheduler = iterateInReactor <EOL> self . scheduler = scheduler <EOL> self . write = self . buffer . write <EOL> def beginProducing ( self , consumer ) : <EOL> self . consumer = consumer <EOL> return self . scheduler ( self . _produce ( ) ) <EOL> def _produce ( self ) : <EOL> headers = self . msg . getHeaders ( True ) <EOL> boundary = None <EOL> if self . msg . isMultipart ( ) : <EOL> content = headers . get ( '<STR_LIT>' ) <EOL> parts = [ x . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) for x in content . split ( '<STR_LIT:;>' ) [ <NUM_LIT:1> : ] ] <EOL> parts = dict ( [ ( k . lower ( ) . strip ( ) , v ) for ( k , v ) in parts ] ) <EOL> boundary = parts . get ( '<STR_LIT>' ) <EOL> if boundary is None : <EOL> boundary = '<STR_LIT>' % ( time . time ( ) , random . random ( ) ) <EOL> headers [ '<STR_LIT>' ] += '<STR_LIT>' % ( boundary , ) <EOL> else : <EOL> if boundary . startswith ( '<STR_LIT:">' ) and boundary . endswith ( '<STR_LIT:">' ) : <EOL> boundary = boundary [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> self . write ( _formatHeaders ( headers ) ) <EOL> self . write ( '<STR_LIT:\r\n>' ) <EOL> if self . msg . isMultipart ( ) : <EOL> for p in subparts ( self . msg ) : <EOL> self . write ( '<STR_LIT>' % ( boundary , ) ) <EOL> yield MessageProducer ( p , self . buffer , self . scheduler <EOL> ) . beginProducing ( None <EOL> ) <EOL> self . write ( '<STR_LIT>' % ( boundary , ) ) <EOL> else : <EOL> f = self . msg . getBodyFile ( ) <EOL> while True : <EOL> b = f . read ( self . CHUNK_SIZE ) <EOL> if b : <EOL> self . buffer . write ( b ) <EOL> yield None <EOL> else : <EOL> break <EOL> if self . consumer : <EOL> self . buffer . seek ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> yield FileProducer ( self . buffer <EOL> ) . beginProducing ( self . consumer <EOL> ) . addCallback ( lambda _ : self <EOL> ) <EOL> class _FetchParser : <EOL> class Envelope : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class Flags : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class InternalDate : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class RFC822Header : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class RFC822Text : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class RFC822Size : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class RFC822 : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class UID : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class Body : <EOL> type = '<STR_LIT:body>' <EOL> peek = False <EOL> header = None <EOL> mime = None <EOL> text = None <EOL> part = ( ) <EOL> empty = False <EOL> partialBegin = None <EOL> partialLength = None <EOL> def __str__ ( self ) : <EOL> base = '<STR_LIT>' <EOL> part = '<STR_LIT>' <EOL> separator = '<STR_LIT>' <EOL> if self . part : <EOL> part = '<STR_LIT:.>' . join ( [ str ( x + <NUM_LIT:1> ) for x in self . part ] ) <EOL> separator = '<STR_LIT:.>' <EOL> if self . header : <EOL> base += '<STR_LIT>' % ( part , separator , self . header , ) <EOL> elif self . text : <EOL> base += '<STR_LIT>' % ( part , separator ) <EOL> elif self . mime : <EOL> base += '<STR_LIT>' % ( part , separator ) <EOL> elif self . empty : <EOL> base += '<STR_LIT>' % ( part , ) <EOL> if self . partialBegin is not None : <EOL> base += '<STR_LIT>' % ( self . partialBegin , self . partialLength ) <EOL> return base <EOL> class BodyStructure : <EOL> type = '<STR_LIT>' <EOL> __str__ = lambda self : '<STR_LIT>' <EOL> class Header : <EOL> negate = False <EOL> fields = None <EOL> part = None <EOL> def __str__ ( self ) : <EOL> base = '<STR_LIT>' <EOL> if self . fields : <EOL> base += '<STR_LIT>' <EOL> if self . negate : <EOL> base += '<STR_LIT>' <EOL> fields = [ ] <EOL> for f in self . fields : <EOL> f = f . title ( ) <EOL> if _needsQuote ( f ) : <EOL> f = _quote ( f ) <EOL> fields . append ( f ) <EOL> base += '<STR_LIT>' % '<STR_LIT:U+0020>' . join ( fields ) <EOL> if self . part : <EOL> base = '<STR_LIT:.>' . join ( [ str ( x + <NUM_LIT:1> ) for x in self . part ] ) + '<STR_LIT:.>' + base <EOL> return base <EOL> class Text : <EOL> pass <EOL> class MIME : <EOL> pass <EOL> parts = None <EOL> _simple_fetch_att = [ <EOL> ( '<STR_LIT>' , Envelope ) , <EOL> ( '<STR_LIT>' , Flags ) , <EOL> ( '<STR_LIT>' , InternalDate ) , <EOL> ( '<STR_LIT>' , RFC822Header ) , <EOL> ( '<STR_LIT>' , RFC822Text ) , <EOL> ( '<STR_LIT>' , RFC822Size ) , <EOL> ( '<STR_LIT>' , RFC822 ) , <EOL> ( '<STR_LIT>' , UID ) , <EOL> ( '<STR_LIT>' , BodyStructure ) , <EOL> ] <EOL> def __init__ ( self ) : <EOL> self . state = [ '<STR_LIT>' ] <EOL> self . result = [ ] <EOL> self . remaining = '<STR_LIT>' <EOL> def parseString ( self , s ) : <EOL> s = self . remaining + s <EOL> try : <EOL> while s or self . state : <EOL> state = self . state . pop ( ) <EOL> try : <EOL> used = getattr ( self , '<STR_LIT>' + state ) ( s ) <EOL> except : <EOL> self . state . append ( state ) <EOL> raise <EOL> else : <EOL> s = s [ used : ] <EOL> finally : <EOL> self . remaining = s <EOL> def state_initial ( self , s ) : <EOL> if s == '<STR_LIT>' : <EOL> return <NUM_LIT:0> <EOL> l = s . lower ( ) <EOL> if l . startswith ( '<STR_LIT:all>' ) : <EOL> self . result . extend ( ( <EOL> self . Flags ( ) , self . InternalDate ( ) , <EOL> self . RFC822Size ( ) , self . Envelope ( ) <EOL> ) ) <EOL> return <NUM_LIT:3> <EOL> if l . startswith ( '<STR_LIT>' ) : <EOL> self . result . extend ( ( <EOL> self . Flags ( ) , self . InternalDate ( ) , <EOL> self . RFC822Size ( ) , self . Envelope ( ) , <EOL> self . Body ( ) <EOL> ) ) <EOL> return <NUM_LIT:4> <EOL> if l . startswith ( '<STR_LIT>' ) : <EOL> self . result . extend ( ( <EOL> self . Flags ( ) , self . InternalDate ( ) , self . RFC822Size ( ) , <EOL> ) ) <EOL> return <NUM_LIT:4> <EOL> if l . startswith ( '<STR_LIT:(>' ) : <EOL> self . state . extend ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return <NUM_LIT:1> <EOL> self . state . append ( '<STR_LIT>' ) <EOL> return <NUM_LIT:0> <EOL> def state_close_paren ( self , s ) : <EOL> if s . startswith ( '<STR_LIT:)>' ) : <EOL> return <NUM_LIT:1> <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def state_whitespace ( self , s ) : <EOL> if not s or not s [ <NUM_LIT:0> ] . isspace ( ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> i = <NUM_LIT:0> <EOL> for i in range ( len ( s ) ) : <EOL> if not s [ i ] . isspace ( ) : <EOL> break <EOL> return i <EOL> def state_maybe_fetch_att ( self , s ) : <EOL> if not s . startswith ( '<STR_LIT:)>' ) : <EOL> self . state . extend ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return <NUM_LIT:0> <EOL> def state_fetch_att ( self , s ) : <EOL> l = s . lower ( ) <EOL> for ( name , cls ) in self . _simple_fetch_att : <EOL> if l . startswith ( name ) : <EOL> self . result . append ( cls ( ) ) <EOL> return len ( name ) <EOL> b = self . Body ( ) <EOL> if l . startswith ( '<STR_LIT>' ) : <EOL> b . peek = True <EOL> used = <NUM_LIT:9> <EOL> elif l . startswith ( '<STR_LIT:body>' ) : <EOL> used = <NUM_LIT:4> <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % ( l , ) ) <EOL> self . pending_body = b <EOL> self . state . extend ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return used <EOL> def state_got_body ( self , s ) : <EOL> self . result . append ( self . pending_body ) <EOL> del self . pending_body <EOL> return <NUM_LIT:0> <EOL> def state_maybe_section ( self , s ) : <EOL> if not s . startswith ( "<STR_LIT:[>" ) : <EOL> return <NUM_LIT:0> <EOL> self . state . extend ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return <NUM_LIT:1> <EOL> _partExpr = re . compile ( r'<STR_LIT>' ) <EOL> def state_part_number ( self , s ) : <EOL> m = self . _partExpr . match ( s ) <EOL> if m is not None : <EOL> self . parts = [ int ( p ) - <NUM_LIT:1> for p in m . groups ( ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:.>' ) ] <EOL> return m . end ( ) <EOL> else : <EOL> self . parts = [ ] <EOL> return <NUM_LIT:0> <EOL> def state_section ( self , s ) : <EOL> l = s . lower ( ) <EOL> used = <NUM_LIT:0> <EOL> if l . startswith ( '<STR_LIT:]>' ) : <EOL> self . pending_body . empty = True <EOL> used += <NUM_LIT:1> <EOL> elif l . startswith ( '<STR_LIT>' ) : <EOL> h = self . pending_body . header = self . Header ( ) <EOL> h . negate = True <EOL> h . fields = ( ) <EOL> used += <NUM_LIT:7> <EOL> elif l . startswith ( '<STR_LIT>' ) : <EOL> self . pending_body . text = self . Text ( ) <EOL> used += <NUM_LIT:5> <EOL> elif l . startswith ( '<STR_LIT>' ) : <EOL> self . pending_body . mime = self . MIME ( ) <EOL> used += <NUM_LIT:5> <EOL> else : <EOL> h = self . Header ( ) <EOL> if l . startswith ( '<STR_LIT>' ) : <EOL> h . negate = True <EOL> used += <NUM_LIT> <EOL> elif l . startswith ( '<STR_LIT>' ) : <EOL> used += <NUM_LIT> <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % ( l , ) ) <EOL> self . pending_body . header = h <EOL> self . state . extend ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . pending_body . part = tuple ( self . parts ) <EOL> self . parts = None <EOL> return used <EOL> def state_finish_section ( self , s ) : <EOL> if not s . startswith ( '<STR_LIT:]>' ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return <NUM_LIT:1> <EOL> def state_header_list ( self , s ) : <EOL> if not s . startswith ( '<STR_LIT:(>' ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> end = s . find ( '<STR_LIT:)>' ) <EOL> if end == - <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> headers = s [ <NUM_LIT:1> : end ] . split ( ) <EOL> self . pending_body . header . fields = map ( str . upper , headers ) <EOL> return end + <NUM_LIT:1> <EOL> def state_maybe_partial ( self , s ) : <EOL> if not s . startswith ( '<STR_LIT:<>' ) : <EOL> return <NUM_LIT:0> <EOL> end = s . find ( '<STR_LIT:>>' ) <EOL> if end == - <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> partial = s [ <NUM_LIT:1> : end ] <EOL> parts = partial . split ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if len ( parts ) != <NUM_LIT:2> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> begin , length = map ( int , parts ) <EOL> self . pending_body . partialBegin = begin <EOL> self . pending_body . partialLength = length <EOL> return end + <NUM_LIT:1> <EOL> class FileProducer : <EOL> CHUNK_SIZE = <NUM_LIT:2> ** <NUM_LIT:2> ** <NUM_LIT:2> ** <NUM_LIT:2> <EOL> firstWrite = True <EOL> def __init__ ( self , f ) : <EOL> self . f = f <EOL> def beginProducing ( self , consumer ) : <EOL> self . consumer = consumer <EOL> self . produce = consumer . write <EOL> d = self . _onDone = defer . Deferred ( ) <EOL> self . consumer . registerProducer ( self , False ) <EOL> return d <EOL> def resumeProducing ( self ) : <EOL> b = '<STR_LIT>' <EOL> if self . firstWrite : <EOL> b = '<STR_LIT>' % self . _size ( ) <EOL> self . firstWrite = False <EOL> if not self . f : <EOL> return <EOL> b = b + self . f . read ( self . CHUNK_SIZE ) <EOL> if not b : <EOL> self . consumer . unregisterProducer ( ) <EOL> self . _onDone . callback ( self ) <EOL> self . _onDone = self . f = self . consumer = None <EOL> else : <EOL> self . produce ( b ) <EOL> def pauseProducing ( self ) : <EOL> pass <EOL> def stopProducing ( self ) : <EOL> pass <EOL> def _size ( self ) : <EOL> b = self . f . tell ( ) <EOL> self . f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> e = self . f . tell ( ) <EOL> self . f . seek ( b , <NUM_LIT:0> ) <EOL> return e - b <EOL> def parseTime ( s ) : <EOL> months = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<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>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> expr = { <EOL> '<STR_LIT>' : r"<STR_LIT>" , <EOL> '<STR_LIT>' : r"<STR_LIT>" , <EOL> '<STR_LIT>' : r"<STR_LIT>" <EOL> } <EOL> m = re . match ( '<STR_LIT>' % expr , s ) <EOL> if not m : <EOL> raise ValueError , "<STR_LIT>" % ( s , ) <EOL> d = m . groupdict ( ) <EOL> try : <EOL> d [ '<STR_LIT>' ] = <NUM_LIT:1> + ( months . index ( d [ '<STR_LIT>' ] . lower ( ) ) % <NUM_LIT:12> ) <EOL> d [ '<STR_LIT>' ] = int ( d [ '<STR_LIT>' ] ) <EOL> d [ '<STR_LIT>' ] = int ( d [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> raise ValueError , "<STR_LIT>" % ( s , ) <EOL> else : <EOL> return time . struct_time ( <EOL> ( d [ '<STR_LIT>' ] , d [ '<STR_LIT>' ] , d [ '<STR_LIT>' ] , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> ) <EOL> import codecs <EOL> def modified_base64 ( s ) : <EOL> s_utf7 = s . encode ( '<STR_LIT>' ) <EOL> return s_utf7 [ <NUM_LIT:1> : - <NUM_LIT:1> ] . replace ( '<STR_LIT:/>' , '<STR_LIT:U+002C>' ) <EOL> def modified_unbase64 ( s ) : <EOL> s_utf7 = '<STR_LIT:+>' + s . replace ( '<STR_LIT:U+002C>' , '<STR_LIT:/>' ) + '<STR_LIT:->' <EOL> return s_utf7 . decode ( '<STR_LIT>' ) <EOL> def encoder ( s , errors = None ) : <EOL> """<STR_LIT>""" <EOL> r = [ ] <EOL> _in = [ ] <EOL> for c in s : <EOL> if ord ( c ) in ( range ( <NUM_LIT> , <NUM_LIT> ) + range ( <NUM_LIT> , <NUM_LIT> ) ) : <EOL> if _in : <EOL> r . extend ( [ '<STR_LIT:&>' , modified_base64 ( '<STR_LIT>' . join ( _in ) ) , '<STR_LIT:->' ] ) <EOL> del _in [ : ] <EOL> r . append ( str ( c ) ) <EOL> elif c == '<STR_LIT:&>' : <EOL> if _in : <EOL> r . extend ( [ '<STR_LIT:&>' , modified_base64 ( '<STR_LIT>' . join ( _in ) ) , '<STR_LIT:->' ] ) <EOL> del _in [ : ] <EOL> r . append ( '<STR_LIT>' ) <EOL> else : <EOL> _in . append ( c ) <EOL> if _in : <EOL> r . extend ( [ '<STR_LIT:&>' , modified_base64 ( '<STR_LIT>' . join ( _in ) ) , '<STR_LIT:->' ] ) <EOL> return ( '<STR_LIT>' . join ( r ) , len ( s ) ) <EOL> def decoder ( s , errors = None ) : <EOL> """<STR_LIT>""" <EOL> r = [ ] <EOL> decode = [ ] <EOL> for c in s : <EOL> if c == '<STR_LIT:&>' and not decode : <EOL> decode . append ( '<STR_LIT:&>' ) <EOL> elif c == '<STR_LIT:->' and decode : <EOL> if len ( decode ) == <NUM_LIT:1> : <EOL> r . append ( '<STR_LIT:&>' ) <EOL> else : <EOL> r . append ( modified_unbase64 ( '<STR_LIT>' . join ( decode [ <NUM_LIT:1> : ] ) ) ) <EOL> decode = [ ] <EOL> elif decode : <EOL> decode . append ( c ) <EOL> else : <EOL> r . append ( c ) <EOL> if decode : <EOL> r . append ( modified_unbase64 ( '<STR_LIT>' . join ( decode [ <NUM_LIT:1> : ] ) ) ) <EOL> return ( '<STR_LIT>' . join ( r ) , len ( s ) ) <EOL> class StreamReader ( codecs . StreamReader ) : <EOL> def decode ( self , s , errors = '<STR_LIT:strict>' ) : <EOL> return decoder ( s ) <EOL> class StreamWriter ( codecs . StreamWriter ) : <EOL> def encode ( self , s , errors = '<STR_LIT:strict>' ) : <EOL> return encoder ( s ) <EOL> _codecInfo = ( encoder , decoder , StreamReader , StreamWriter ) <EOL> try : <EOL> _codecInfoClass = codecs . CodecInfo <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> _codecInfo = _codecInfoClass ( * _codecInfo ) <EOL> def imap4_utf_7 ( name ) : <EOL> if name == '<STR_LIT>' : <EOL> return _codecInfo <EOL> codecs . register ( imap4_utf_7 ) <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<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>' , <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>' , '<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>' , <EOL> '<STR_LIT>' , <EOL> ] </s>
