<s> """<STR_LIT>""" <EOL> from zope . interface import implements <EOL> from twisted . internet import defer , protocol <EOL> from twisted . python import reflect <EOL> from twisted . python . deprecate import deprecated <EOL> from twisted . python . versions import Version <EOL> from twisted . words . protocols . jabber import error , jid , xmlstream <EOL> from twisted . words . protocols . jabber . xmlstream import toResponse <EOL> from twisted . words . xish import domish , utility <EOL> from twisted . words . xish . xmlstream import BootstrapMixin <EOL> from wokkel . iwokkel import IDisco <EOL> from wokkel . subprotocols import XMPPHandler <EOL> IQ_GET = '<STR_LIT>' <EOL> IQ_SET = '<STR_LIT>' <EOL> NS_VERSION = '<STR_LIT>' <EOL> VERSION = IQ_GET + '<STR_LIT>' + NS_VERSION + '<STR_LIT>' <EOL> def parseXml ( string ) : <EOL> """<STR_LIT>""" <EOL> roots = [ ] <EOL> results = [ ] <EOL> elementStream = domish . elementStream ( ) <EOL> elementStream . DocumentStartEvent = roots . append <EOL> elementStream . ElementEvent = lambda elem : roots [ <NUM_LIT:0> ] . addChild ( elem ) <EOL> elementStream . DocumentEndEvent = lambda : results . append ( roots [ <NUM_LIT:0> ] ) <EOL> elementStream . parse ( string ) <EOL> return results and results [ <NUM_LIT:0> ] or None <EOL> def stripNamespace ( rootElement ) : <EOL> namespace = rootElement . uri <EOL> def strip ( element ) : <EOL> if element . uri == namespace : <EOL> element . uri = None <EOL> if element . defaultUri == namespace : <EOL> element . defaultUri = None <EOL> for child in element . elements ( ) : <EOL> strip ( child ) <EOL> if namespace is not None : <EOL> strip ( rootElement ) <EOL> return rootElement <EOL> class FallbackHandler ( XMPPHandler ) : <EOL> """<STR_LIT>""" <EOL> def connectionInitialized ( self ) : <EOL> self . xmlstream . addObserver ( IQ_SET , self . iqFallback , - <NUM_LIT:1> ) <EOL> self . xmlstream . addObserver ( IQ_GET , self . iqFallback , - <NUM_LIT:1> ) <EOL> def iqFallback ( self , iq ) : <EOL> if iq . handled == True : <EOL> return <EOL> reply = error . StanzaError ( '<STR_LIT>' ) <EOL> self . xmlstream . send ( reply . toResponse ( iq ) ) <EOL> class VersionHandler ( XMPPHandler ) : <EOL> """<STR_LIT>""" <EOL> implements ( IDisco ) <EOL> def __init__ ( self , name , version ) : <EOL> self . name = name <EOL> self . version = version <EOL> def connectionInitialized ( self ) : <EOL> self . xmlstream . addObserver ( VERSION , self . onVersion ) <EOL> def onVersion ( self , iq ) : <EOL> response = toResponse ( iq , "<STR_LIT:result>" ) <EOL> query = response . addElement ( ( NS_VERSION , "<STR_LIT>" ) ) <EOL> query . addElement ( "<STR_LIT:name>" , content = self . name ) <EOL> query . addElement ( "<STR_LIT:version>" , content = self . version ) <EOL> self . send ( response ) <EOL> iq . handled = True <EOL> def getDiscoInfo ( self , requestor , target , node ) : <EOL> info = set ( ) <EOL> if not node : <EOL> from wokkel import disco <EOL> info . add ( disco . DiscoFeature ( NS_VERSION ) ) <EOL> return defer . succeed ( info ) <EOL> def getDiscoItems ( self , requestor , target , node ) : <EOL> return defer . succeed ( [ ] ) <EOL> class XmlPipe ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . source = utility . EventDispatcher ( ) <EOL> self . sink = utility . EventDispatcher ( ) <EOL> self . source . send = lambda obj : self . sink . dispatch ( obj ) <EOL> self . sink . send = lambda obj : self . source . dispatch ( obj ) <EOL> class Stanza ( object ) : <EOL> """<STR_LIT>""" <EOL> recipient = None <EOL> sender = None <EOL> stanzaKind = None <EOL> stanzaID = None <EOL> stanzaType = None <EOL> def __init__ ( self , recipient = None , sender = None ) : <EOL> self . recipient = recipient <EOL> self . sender = sender <EOL> @ classmethod <EOL> def fromElement ( Class , element ) : <EOL> """<STR_LIT>""" <EOL> stanza = Class ( ) <EOL> stanza . parseElement ( element ) <EOL> return stanza <EOL> def parseElement ( self , element ) : <EOL> """<STR_LIT>""" <EOL> if element . hasAttribute ( '<STR_LIT>' ) : <EOL> self . sender = jid . internJID ( element [ '<STR_LIT>' ] ) <EOL> if element . hasAttribute ( '<STR_LIT:to>' ) : <EOL> self . recipient = jid . internJID ( element [ '<STR_LIT:to>' ] ) <EOL> self . stanzaType = element . getAttribute ( '<STR_LIT:type>' ) <EOL> self . stanzaID = element . getAttribute ( '<STR_LIT:id>' ) <EOL> stripNamespace ( element ) <EOL> self . element = element <EOL> handlers = { } <EOL> reflect . accumulateClassDict ( self . __class__ , '<STR_LIT>' , handlers ) <EOL> for child in element . elements ( ) : <EOL> try : <EOL> handler = handlers [ child . uri , child . name ] <EOL> except KeyError : <EOL> try : <EOL> handler = handlers [ None ] <EOL> except KeyError : <EOL> continue <EOL> getattr ( self , handler ) ( child ) <EOL> def toElement ( self ) : <EOL> element = domish . Element ( ( None , self . stanzaKind ) ) <EOL> if self . sender is not None : <EOL> element [ '<STR_LIT>' ] = self . sender . full ( ) <EOL> if self . recipient is not None : <EOL> element [ '<STR_LIT:to>' ] = self . recipient . full ( ) <EOL> if self . stanzaType : <EOL> element [ '<STR_LIT:type>' ] = self . stanzaType <EOL> if self . stanzaID : <EOL> element [ '<STR_LIT:id>' ] = self . stanzaID <EOL> return element <EOL> class ErrorStanza ( Stanza ) : <EOL> def parseElement ( self , element ) : <EOL> Stanza . parseElement ( self , element ) <EOL> self . exception = error . exceptionFromStanza ( element ) <EOL> class Request ( Stanza ) : <EOL> """<STR_LIT>""" <EOL> stanzaKind = '<STR_LIT>' <EOL> stanzaType = '<STR_LIT>' <EOL> timeout = None <EOL> childParsers = { None : '<STR_LIT>' } <EOL> def __init__ ( self , recipient = None , sender = None , stanzaType = None ) : <EOL> Stanza . __init__ ( self , recipient = recipient , sender = sender ) <EOL> if stanzaType is not None : <EOL> self . stanzaType = stanzaType <EOL> def parseRequest ( self , element ) : <EOL> """<STR_LIT>""" <EOL> def toElement ( self ) : <EOL> element = Stanza . toElement ( self ) <EOL> if not self . stanzaID : <EOL> element . addUniqueId ( ) <EOL> self . stanzaID = element [ '<STR_LIT:id>' ] <EOL> return element <EOL> class DeferredXmlStreamFactory ( BootstrapMixin , protocol . ClientFactory ) : <EOL> protocol = xmlstream . XmlStream <EOL> def __init__ ( self , authenticator ) : <EOL> BootstrapMixin . __init__ ( self ) <EOL> self . authenticator = authenticator <EOL> deferred = defer . Deferred ( ) <EOL> self . deferred = deferred <EOL> self . addBootstrap ( xmlstream . STREAM_AUTHD_EVENT , self . deferred . callback ) <EOL> self . addBootstrap ( xmlstream . INIT_FAILED_EVENT , deferred . errback ) <EOL> def buildProtocol ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> xs = self . protocol ( self . authenticator ) <EOL> xs . factory = self <EOL> self . installBootstraps ( xs ) <EOL> return xs <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> self . deferred . errback ( reason ) <EOL> @ deprecated ( Version ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:8> , <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> def prepareIDNName ( name ) : <EOL> """<STR_LIT>""" <EOL> return name . encode ( '<STR_LIT>' ) </s>
