<s> """<STR_LIT>""" <EOL> import simplexml , time , sys <EOL> from protocol import * <EOL> from client import PlugIn <EOL> DefaultTimeout = <NUM_LIT> <EOL> ID = <NUM_LIT:0> <EOL> class Dispatcher ( PlugIn ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> PlugIn . __init__ ( self ) <EOL> DBG_LINE = '<STR_LIT>' <EOL> self . handlers = { } <EOL> self . _expected = { } <EOL> self . _defaultHandler = None <EOL> self . _pendingExceptions = [ ] <EOL> self . _eventHandler = None <EOL> self . _cycleHandlers = [ ] <EOL> self . _exported_methods = [ self . Process , self . RegisterHandler , self . RegisterDefaultHandler , self . RegisterEventHandler , self . UnregisterCycleHandler , self . RegisterCycleHandler , self . RegisterHandlerOnce , self . UnregisterHandler , self . RegisterProtocol , self . WaitForResponse , self . SendAndWaitForResponse , self . send , self . disconnect , self . SendAndCallForResponse , ] <EOL> def dumpHandlers ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . handlers <EOL> def restoreHandlers ( self , handlers ) : <EOL> """<STR_LIT>""" <EOL> self . handlers = handlers <EOL> def _init ( self ) : <EOL> """<STR_LIT>""" <EOL> self . RegisterNamespace ( '<STR_LIT>' ) <EOL> self . RegisterNamespace ( NS_STREAMS ) <EOL> self . RegisterNamespace ( self . _owner . defaultNamespace ) <EOL> self . RegisterProtocol ( '<STR_LIT>' , Iq ) <EOL> self . RegisterProtocol ( '<STR_LIT>' , Presence ) <EOL> self . RegisterProtocol ( '<STR_LIT:message>' , Message ) <EOL> self . RegisterDefaultHandler ( self . returnStanzaHandler ) <EOL> self . RegisterHandler ( '<STR_LIT:error>' , self . streamErrorHandler , xmlns = NS_STREAMS ) <EOL> def plugin ( self , owner ) : <EOL> """<STR_LIT>""" <EOL> self . _init ( ) <EOL> for method in self . _old_owners_methods : <EOL> if method . __name__ == '<STR_LIT>' : self . _owner_send = method ; break <EOL> self . _owner . lastErrNode = None <EOL> self . _owner . lastErr = None <EOL> self . _owner . lastErrCode = None <EOL> self . StreamInit ( ) <EOL> def plugout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . Stream . dispatch = None <EOL> self . Stream . DEBUG = None <EOL> self . Stream . features = None <EOL> self . Stream . destroy ( ) <EOL> def StreamInit ( self ) : <EOL> """<STR_LIT>""" <EOL> self . Stream = simplexml . NodeBuilder ( ) <EOL> self . Stream . _dispatch_depth = <NUM_LIT:2> <EOL> self . Stream . dispatch = self . dispatch <EOL> self . Stream . stream_header_received = self . _check_stream_start <EOL> self . _owner . debug_flags . append ( simplexml . DBG_NODEBUILDER ) <EOL> self . Stream . DEBUG = self . _owner . DEBUG <EOL> self . Stream . features = None <EOL> self . _metastream = Node ( '<STR_LIT>' ) <EOL> self . _metastream . setNamespace ( self . _owner . Namespace ) <EOL> self . _metastream . setAttr ( '<STR_LIT:version>' , '<STR_LIT:1.0>' ) <EOL> self . _metastream . setAttr ( '<STR_LIT>' , NS_STREAMS ) <EOL> self . _metastream . setAttr ( '<STR_LIT:to>' , self . _owner . Server ) <EOL> self . _owner . send ( "<STR_LIT>" % str ( self . _metastream ) [ : - <NUM_LIT:2> ] ) <EOL> def _check_stream_start ( self , ns , tag , attrs ) : <EOL> if ns < > NS_STREAMS or tag < > '<STR_LIT>' : <EOL> raise ValueError ( '<STR_LIT>' % ( tag , ns ) ) <EOL> def Process ( self , timeout = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> for handler in self . _cycleHandlers : handler ( self ) <EOL> if len ( self . _pendingExceptions ) > <NUM_LIT:0> : <EOL> _pendingException = self . _pendingExceptions . pop ( ) <EOL> raise _pendingException [ <NUM_LIT:0> ] , _pendingException [ <NUM_LIT:1> ] , _pendingException [ <NUM_LIT:2> ] <EOL> if self . _owner . Connection . pending_data ( timeout ) : <EOL> try : data = self . _owner . Connection . receive ( ) <EOL> except IOError : return <EOL> self . Stream . Parse ( data ) <EOL> if len ( self . _pendingExceptions ) > <NUM_LIT:0> : <EOL> _pendingException = self . _pendingExceptions . pop ( ) <EOL> raise _pendingException [ <NUM_LIT:0> ] , _pendingException [ <NUM_LIT:1> ] , _pendingException [ <NUM_LIT:2> ] <EOL> if data : return len ( data ) <EOL> return '<STR_LIT:0>' <EOL> def RegisterNamespace ( self , xmlns , order = '<STR_LIT:info>' ) : <EOL> """<STR_LIT>""" <EOL> self . DEBUG ( '<STR_LIT>' % xmlns , order ) <EOL> self . handlers [ xmlns ] = { } <EOL> self . RegisterProtocol ( '<STR_LIT>' , Protocol , xmlns = xmlns ) <EOL> self . RegisterProtocol ( '<STR_LIT:default>' , Protocol , xmlns = xmlns ) <EOL> def RegisterProtocol ( self , tag_name , Proto , xmlns = None , order = '<STR_LIT:info>' ) : <EOL> """<STR_LIT>""" <EOL> if not xmlns : xmlns = self . _owner . defaultNamespace <EOL> self . DEBUG ( '<STR_LIT>' % ( tag_name , Proto , xmlns ) , order ) <EOL> self . handlers [ xmlns ] [ tag_name ] = { type : Proto , '<STR_LIT:default>' : [ ] } <EOL> def RegisterNamespaceHandler ( self , xmlns , handler , typ = '<STR_LIT>' , ns = '<STR_LIT>' , makefirst = <NUM_LIT:0> , system = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> self . RegisterHandler ( '<STR_LIT:default>' , handler , typ , ns , xmlns , makefirst , system ) <EOL> def RegisterHandler ( self , name , handler , typ = '<STR_LIT>' , ns = '<STR_LIT>' , xmlns = None , makefirst = <NUM_LIT:0> , system = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not xmlns : xmlns = self . _owner . defaultNamespace <EOL> self . DEBUG ( '<STR_LIT>' % ( handler , name , typ , ns , xmlns ) , '<STR_LIT:info>' ) <EOL> if not typ and not ns : typ = '<STR_LIT:default>' <EOL> if not self . handlers . has_key ( xmlns ) : self . RegisterNamespace ( xmlns , '<STR_LIT>' ) <EOL> if not self . handlers [ xmlns ] . has_key ( name ) : self . RegisterProtocol ( name , Protocol , xmlns , '<STR_LIT>' ) <EOL> if not self . handlers [ xmlns ] [ name ] . has_key ( typ + ns ) : self . handlers [ xmlns ] [ name ] [ typ + ns ] = [ ] <EOL> if makefirst : self . handlers [ xmlns ] [ name ] [ typ + ns ] . insert ( <NUM_LIT:0> , { '<STR_LIT>' : handler , '<STR_LIT>' : system } ) <EOL> else : self . handlers [ xmlns ] [ name ] [ typ + ns ] . append ( { '<STR_LIT>' : handler , '<STR_LIT>' : system } ) <EOL> def RegisterHandlerOnce ( self , name , handler , typ = '<STR_LIT>' , ns = '<STR_LIT>' , xmlns = None , makefirst = <NUM_LIT:0> , system = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not xmlns : xmlns = self . _owner . defaultNamespace <EOL> self . RegisterHandler ( name , handler , typ , ns , xmlns , makefirst , system ) <EOL> def UnregisterHandler ( self , name , handler , typ = '<STR_LIT>' , ns = '<STR_LIT>' , xmlns = None ) : <EOL> """<STR_LIT>""" <EOL> if not xmlns : xmlns = self . _owner . defaultNamespace <EOL> if not self . handlers . has_key ( xmlns ) : return <EOL> if not typ and not ns : typ = '<STR_LIT:default>' <EOL> for pack in self . handlers [ xmlns ] [ name ] [ typ + ns ] : <EOL> if handler == pack [ '<STR_LIT>' ] : break <EOL> else : pack = None <EOL> try : self . handlers [ xmlns ] [ name ] [ typ + ns ] . remove ( pack ) <EOL> except ValueError : pass <EOL> def RegisterDefaultHandler ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> self . _defaultHandler = handler <EOL> def RegisterEventHandler ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> self . _eventHandler = handler <EOL> def returnStanzaHandler ( self , conn , stanza ) : <EOL> """<STR_LIT>""" <EOL> if stanza . getType ( ) in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> conn . send ( Error ( stanza , ERR_FEATURE_NOT_IMPLEMENTED ) ) <EOL> def streamErrorHandler ( self , conn , error ) : <EOL> name , text = '<STR_LIT:error>' , error . getData ( ) <EOL> for tag in error . getChildren ( ) : <EOL> if tag . getNamespace ( ) == NS_XMPP_STREAMS : <EOL> if tag . getName ( ) == '<STR_LIT:text>' : text = tag . getData ( ) <EOL> else : name = tag . getName ( ) <EOL> if name in stream_exceptions . keys ( ) : exc = stream_exceptions [ name ] <EOL> else : exc = StreamError <EOL> raise exc ( ( name , text ) ) <EOL> def RegisterCycleHandler ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> if handler not in self . _cycleHandlers : self . _cycleHandlers . append ( handler ) <EOL> def UnregisterCycleHandler ( self , handler ) : <EOL> """<STR_LIT>""" <EOL> if handler in self . _cycleHandlers : self . _cycleHandlers . remove ( handler ) <EOL> def Event ( self , realm , event , data ) : <EOL> """<STR_LIT>""" <EOL> if self . _eventHandler : self . _eventHandler ( realm , event , data ) <EOL> def dispatch ( self , stanza , session = None , direct = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not session : session = self <EOL> session . Stream . _mini_dom = None <EOL> name = stanza . getName ( ) <EOL> if not direct and self . _owner . _route : <EOL> if name == '<STR_LIT>' : <EOL> if stanza . getAttr ( '<STR_LIT:error>' ) == None : <EOL> if len ( stanza . getChildren ( ) ) == <NUM_LIT:1> : <EOL> stanza = stanza . getChildren ( ) [ <NUM_LIT:0> ] <EOL> name = stanza . getName ( ) <EOL> else : <EOL> for each in stanza . getChildren ( ) : <EOL> self . dispatch ( each , session , direct = <NUM_LIT:1> ) <EOL> return <EOL> elif name == '<STR_LIT>' : <EOL> return <EOL> elif name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> pass <EOL> else : <EOL> raise UnsupportedStanzaType ( name ) <EOL> if name == '<STR_LIT>' : session . Stream . features = stanza <EOL> xmlns = stanza . getNamespace ( ) <EOL> if not self . handlers . has_key ( xmlns ) : <EOL> self . DEBUG ( "<STR_LIT>" + xmlns , '<STR_LIT>' ) <EOL> xmlns = '<STR_LIT>' <EOL> if not self . handlers [ xmlns ] . has_key ( name ) : <EOL> self . DEBUG ( "<STR_LIT>" + name , '<STR_LIT>' ) <EOL> name = '<STR_LIT>' <EOL> else : <EOL> self . DEBUG ( "<STR_LIT>" % ( xmlns , name ) , '<STR_LIT>' ) <EOL> if stanza . __class__ . __name__ == '<STR_LIT>' : stanza = self . handlers [ xmlns ] [ name ] [ type ] ( node = stanza ) <EOL> typ = stanza . getType ( ) <EOL> if not typ : typ = '<STR_LIT>' <EOL> stanza . props = stanza . getProperties ( ) <EOL> ID = stanza . getID ( ) <EOL> session . DEBUG ( "<STR_LIT>" % ( name , typ , stanza . props , ID ) , '<STR_LIT>' ) <EOL> list = [ '<STR_LIT:default>' ] <EOL> if self . handlers [ xmlns ] [ name ] . has_key ( typ ) : list . append ( typ ) <EOL> for prop in stanza . props : <EOL> if self . handlers [ xmlns ] [ name ] . has_key ( prop ) : list . append ( prop ) <EOL> if typ and self . handlers [ xmlns ] [ name ] . has_key ( typ + prop ) : list . append ( typ + prop ) <EOL> chain = self . handlers [ xmlns ] [ '<STR_LIT:default>' ] [ '<STR_LIT:default>' ] <EOL> for key in list : <EOL> if key : chain = chain + self . handlers [ xmlns ] [ name ] [ key ] <EOL> output = '<STR_LIT>' <EOL> if session . _expected . has_key ( ID ) : <EOL> user = <NUM_LIT:0> <EOL> if type ( session . _expected [ ID ] ) == type ( ( ) ) : <EOL> cb , args = session . _expected [ ID ] <EOL> session . DEBUG ( "<STR_LIT>" % ( cb , args ) , '<STR_LIT>' ) <EOL> try : cb ( session , stanza , ** args ) <EOL> except Exception , typ : <EOL> if typ . __class__ . __name__ < > '<STR_LIT>' : raise <EOL> else : <EOL> session . DEBUG ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> session . _expected [ ID ] = stanza <EOL> else : user = <NUM_LIT:1> <EOL> for handler in chain : <EOL> if user or handler [ '<STR_LIT>' ] : <EOL> try : <EOL> handler [ '<STR_LIT>' ] ( session , stanza ) <EOL> except Exception , typ : <EOL> if typ . __class__ . __name__ < > '<STR_LIT>' : <EOL> self . _pendingExceptions . insert ( <NUM_LIT:0> , sys . exc_info ( ) ) <EOL> return <EOL> user = <NUM_LIT:0> <EOL> if user and self . _defaultHandler : self . _defaultHandler ( session , stanza ) <EOL> def WaitForResponse ( self , ID , timeout = DefaultTimeout ) : <EOL> """<STR_LIT>""" <EOL> self . _expected [ ID ] = None <EOL> has_timed_out = <NUM_LIT:0> <EOL> abort_time = time . time ( ) + timeout <EOL> self . DEBUG ( "<STR_LIT>" % ( ID , timeout ) , '<STR_LIT>' ) <EOL> while not self . _expected [ ID ] : <EOL> if not self . Process ( <NUM_LIT> ) : <EOL> self . _owner . lastErr = "<STR_LIT>" <EOL> return None <EOL> if time . time ( ) > abort_time : <EOL> self . _owner . lastErr = "<STR_LIT>" <EOL> return None <EOL> response = self . _expected [ ID ] <EOL> del self . _expected [ ID ] <EOL> if response . getErrorCode ( ) : <EOL> self . _owner . lastErrNode = response <EOL> self . _owner . lastErr = response . getError ( ) <EOL> self . _owner . lastErrCode = response . getErrorCode ( ) <EOL> return response <EOL> def SendAndWaitForResponse ( self , stanza , timeout = DefaultTimeout ) : <EOL> """<STR_LIT>""" <EOL> return self . WaitForResponse ( self . send ( stanza ) , timeout ) <EOL> def SendAndCallForResponse ( self , stanza , func , args = { } ) : <EOL> """<STR_LIT>""" <EOL> self . _expected [ self . send ( stanza ) ] = ( func , args ) <EOL> def send ( self , stanza ) : <EOL> """<STR_LIT>""" <EOL> if type ( stanza ) in [ type ( '<STR_LIT>' ) , type ( u'<STR_LIT>' ) ] : return self . _owner_send ( stanza ) <EOL> if not isinstance ( stanza , Protocol ) : _ID = None <EOL> elif not stanza . getID ( ) : <EOL> global ID <EOL> ID += <NUM_LIT:1> <EOL> _ID = ` ID ` <EOL> stanza . setID ( _ID ) <EOL> else : _ID = stanza . getID ( ) <EOL> if self . _owner . _registered_name and not stanza . getAttr ( '<STR_LIT>' ) : stanza . setAttr ( '<STR_LIT>' , self . _owner . _registered_name ) <EOL> if self . _owner . _route and stanza . getName ( ) != '<STR_LIT>' : <EOL> to = self . _owner . Server <EOL> if stanza . getTo ( ) and stanza . getTo ( ) . getDomain ( ) : <EOL> to = stanza . getTo ( ) . getDomain ( ) <EOL> frm = stanza . getFrom ( ) <EOL> if frm . getDomain ( ) : <EOL> frm = frm . getDomain ( ) <EOL> route = Protocol ( '<STR_LIT>' , to = to , frm = frm , payload = [ stanza ] ) <EOL> stanza = route <EOL> stanza . setNamespace ( self . _owner . Namespace ) <EOL> stanza . setParent ( self . _metastream ) <EOL> self . _owner_send ( stanza ) <EOL> return _ID <EOL> def disconnect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _owner_send ( '<STR_LIT>' ) <EOL> while self . Process ( <NUM_LIT:1> ) : pass </s>
