<s> """<STR_LIT>""" <EOL> from zope . interface import implements <EOL> from twisted . internet import defer <EOL> from twisted . python import log <EOL> from twisted . words . protocols . jabber import jid , error <EOL> from twisted . words . xish import domish <EOL> from wokkel import disco , data_form , generic , shim <EOL> from wokkel . compat import IQ <EOL> from wokkel . subprotocols import IQHandlerMixin , XMPPHandler <EOL> from wokkel . iwokkel import IPubSubClient , IPubSubService , IPubSubResource <EOL> IQ_GET = '<STR_LIT>' <EOL> IQ_SET = '<STR_LIT>' <EOL> NS_PUBSUB = '<STR_LIT>' <EOL> NS_PUBSUB_EVENT = NS_PUBSUB + '<STR_LIT>' <EOL> NS_PUBSUB_ERRORS = NS_PUBSUB + '<STR_LIT>' <EOL> NS_PUBSUB_OWNER = NS_PUBSUB + "<STR_LIT>" <EOL> NS_PUBSUB_NODE_CONFIG = NS_PUBSUB + "<STR_LIT>" <EOL> NS_PUBSUB_META_DATA = NS_PUBSUB + "<STR_LIT>" <EOL> NS_PUBSUB_SUBSCRIBE_OPTIONS = NS_PUBSUB + "<STR_LIT>" <EOL> PUBSUB_REQUEST = '<STR_LIT>' + '<STR_LIT>' + NS_PUBSUB + '<STR_LIT>' + '<STR_LIT>' + NS_PUBSUB_OWNER + '<STR_LIT>' <EOL> class SubscriptionPending ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class SubscriptionUnconfigured ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class PubSubError ( error . StanzaError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , condition , pubsubCondition , feature = None , text = None ) : <EOL> appCondition = domish . Element ( ( NS_PUBSUB_ERRORS , pubsubCondition ) ) <EOL> if feature : <EOL> appCondition [ '<STR_LIT>' ] = feature <EOL> error . StanzaError . __init__ ( self , condition , <EOL> text = text , <EOL> appCondition = appCondition ) <EOL> class BadRequest ( error . StanzaError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pubsubCondition = None , text = None ) : <EOL> if pubsubCondition : <EOL> appCondition = domish . Element ( ( NS_PUBSUB_ERRORS , pubsubCondition ) ) <EOL> else : <EOL> appCondition = None <EOL> error . StanzaError . __init__ ( self , '<STR_LIT>' , <EOL> text = text , <EOL> appCondition = appCondition ) <EOL> class Unsupported ( PubSubError ) : <EOL> def __init__ ( self , feature , text = None ) : <EOL> self . feature = feature <EOL> PubSubError . __init__ ( self , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> feature , <EOL> text ) <EOL> def __str__ ( self ) : <EOL> message = PubSubError . __str__ ( self ) <EOL> message += '<STR_LIT>' % self . feature <EOL> return message <EOL> class Subscription ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , nodeIdentifier , subscriber , state , options = None , <EOL> subscriptionIdentifier = None ) : <EOL> self . nodeIdentifier = nodeIdentifier <EOL> self . subscriber = subscriber <EOL> self . state = state <EOL> self . options = options or { } <EOL> self . subscriptionIdentifier = subscriptionIdentifier <EOL> @ staticmethod <EOL> def fromElement ( element ) : <EOL> return Subscription ( <EOL> element . getAttribute ( '<STR_LIT>' ) , <EOL> jid . JID ( element . getAttribute ( '<STR_LIT>' ) ) , <EOL> element . getAttribute ( '<STR_LIT>' ) , <EOL> subscriptionIdentifier = element . getAttribute ( '<STR_LIT>' ) ) <EOL> def toElement ( self , defaultUri = None ) : <EOL> """<STR_LIT>""" <EOL> element = domish . Element ( ( defaultUri , '<STR_LIT>' ) ) <EOL> if self . nodeIdentifier : <EOL> element [ '<STR_LIT>' ] = self . nodeIdentifier <EOL> element [ '<STR_LIT>' ] = unicode ( self . subscriber ) <EOL> element [ '<STR_LIT>' ] = self . state <EOL> if self . subscriptionIdentifier : <EOL> element [ '<STR_LIT>' ] = self . subscriptionIdentifier <EOL> return element <EOL> class Item ( domish . Element ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , id = None , payload = None ) : <EOL> """<STR_LIT>""" <EOL> domish . Element . __init__ ( self , ( None , '<STR_LIT>' ) ) <EOL> if id is not None : <EOL> self [ '<STR_LIT:id>' ] = id <EOL> if payload is not None : <EOL> if isinstance ( payload , basestring ) : <EOL> self . addRawXml ( payload ) <EOL> else : <EOL> self . addChild ( payload ) <EOL> class PubSubRequest ( generic . Stanza ) : <EOL> """<STR_LIT>""" <EOL> verb = None <EOL> affiliations = None <EOL> items = None <EOL> itemIdentifiers = None <EOL> maxItems = None <EOL> nodeIdentifier = None <EOL> nodeType = None <EOL> options = None <EOL> subscriber = None <EOL> subscriptionIdentifier = None <EOL> subscriptions = None <EOL> affiliations = None <EOL> _requestVerbMap = { <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT:default>' ) : '<STR_LIT:default>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> ( '<STR_LIT>' , NS_PUBSUB_OWNER , '<STR_LIT>' ) : '<STR_LIT>' , <EOL> } <EOL> _verbRequestMap = dict ( ( ( v , k ) for k , v in _requestVerbMap . iteritems ( ) ) ) <EOL> _parameters = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT:default>' : [ '<STR_LIT:default>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> def __init__ ( self , verb = None ) : <EOL> self . verb = verb <EOL> def _parse_node ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . nodeIdentifier = verbElement [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> def _render_node ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> if not self . nodeIdentifier : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> verbElement [ '<STR_LIT>' ] = self . nodeIdentifier <EOL> def _parse_nodeOrEmpty ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> self . nodeIdentifier = verbElement . getAttribute ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> def _render_nodeOrEmpty ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> if self . nodeIdentifier : <EOL> verbElement [ '<STR_LIT>' ] = self . nodeIdentifier <EOL> def _parse_nodeOrNone ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> self . nodeIdentifier = verbElement . getAttribute ( "<STR_LIT>" ) <EOL> def _render_nodeOrNone ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> if self . nodeIdentifier : <EOL> verbElement [ '<STR_LIT>' ] = self . nodeIdentifier <EOL> def _parse_items ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> self . items = [ ] <EOL> for element in verbElement . elements ( ) : <EOL> if element . uri == NS_PUBSUB and element . name == '<STR_LIT>' : <EOL> self . items . append ( element ) <EOL> def _render_items ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> if self . items : <EOL> for item in self . items : <EOL> item . uri = NS_PUBSUB <EOL> verbElement . addChild ( item ) <EOL> def _parse_jid ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . subscriber = jid . internJID ( verbElement [ "<STR_LIT>" ] ) <EOL> except KeyError : <EOL> raise BadRequest ( '<STR_LIT>' ) <EOL> def _render_jid ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> verbElement [ '<STR_LIT>' ] = self . subscriber . full ( ) <EOL> def _parse_default ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> form = data_form . findForm ( verbElement , NS_PUBSUB_NODE_CONFIG ) <EOL> if form is not None and form . formType == '<STR_LIT>' : <EOL> values = form . getValues ( ) <EOL> self . nodeType = values . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> self . nodeType = '<STR_LIT>' <EOL> def _parse_configure ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> form = data_form . findForm ( verbElement , NS_PUBSUB_NODE_CONFIG ) <EOL> if form is not None : <EOL> if form . formType in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . options = form <EOL> else : <EOL> raise BadRequest ( text = u"<STR_LIT>" % form . formType ) <EOL> else : <EOL> raise BadRequest ( text = "<STR_LIT>" ) <EOL> def _parse_configureOrNone ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> for element in verbElement . parent . elements ( ) : <EOL> if element . uri == NS_PUBSUB and element . name == '<STR_LIT>' : <EOL> form = data_form . findForm ( element , NS_PUBSUB_NODE_CONFIG ) <EOL> if form is not None : <EOL> if form . formType != '<STR_LIT>' : <EOL> raise BadRequest ( text = u"<STR_LIT>" % <EOL> form . formType ) <EOL> else : <EOL> form = data_form . Form ( '<STR_LIT>' , <EOL> formNamespace = NS_PUBSUB_NODE_CONFIG ) <EOL> self . options = form <EOL> def _render_configureOrNone ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> if self . options is not None : <EOL> configure = verbElement . parent . addElement ( '<STR_LIT>' ) <EOL> configure . addChild ( self . options . toElement ( ) ) <EOL> def _parse_itemIdentifiers ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> self . itemIdentifiers = [ ] <EOL> for element in verbElement . elements ( ) : <EOL> if element . uri == NS_PUBSUB and element . name == '<STR_LIT>' : <EOL> try : <EOL> self . itemIdentifiers . append ( element [ "<STR_LIT:id>" ] ) <EOL> except KeyError : <EOL> raise BadRequest ( ) <EOL> def _render_itemIdentifiers ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> if self . itemIdentifiers : <EOL> for itemIdentifier in self . itemIdentifiers : <EOL> item = verbElement . addElement ( '<STR_LIT>' ) <EOL> item [ '<STR_LIT:id>' ] = itemIdentifier <EOL> def _parse_maxItems ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> value = verbElement . getAttribute ( '<STR_LIT>' ) <EOL> if value : <EOL> try : <EOL> self . maxItems = int ( value ) <EOL> except ValueError : <EOL> raise BadRequest ( text = "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> def _render_maxItems ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> if self . maxItems : <EOL> verbElement [ '<STR_LIT>' ] = unicode ( self . maxItems ) <EOL> def _parse_subidOrNone ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> self . subscriptionIdentifier = verbElement . getAttribute ( "<STR_LIT>" ) <EOL> def _render_subidOrNone ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> if self . subscriptionIdentifier : <EOL> verbElement [ '<STR_LIT>' ] = self . subscriptionIdentifier <EOL> def _parse_options ( self , verbElement ) : <EOL> """<STR_LIT>""" <EOL> form = data_form . findForm ( verbElement , NS_PUBSUB_SUBSCRIBE_OPTIONS ) <EOL> if form is not None : <EOL> if form . formType in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . options = form <EOL> else : <EOL> raise BadRequest ( text = u"<STR_LIT>" % form . formType ) <EOL> else : <EOL> raise BadRequest ( text = "<STR_LIT>" ) <EOL> def _render_options ( self , verbElement ) : <EOL> verbElement . addChild ( self . options . toElement ( ) ) <EOL> def _parse_optionsWithSubscribe ( self , verbElement ) : <EOL> for element in verbElement . parent . elements ( ) : <EOL> if element . name == '<STR_LIT>' and element . uri == NS_PUBSUB : <EOL> form = data_form . findForm ( element , <EOL> NS_PUBSUB_SUBSCRIBE_OPTIONS ) <EOL> if form is not None : <EOL> if form . formType != '<STR_LIT>' : <EOL> raise BadRequest ( text = u"<STR_LIT>" % <EOL> form . formType ) <EOL> else : <EOL> form = data_form . Form ( '<STR_LIT>' , <EOL> formNamespace = NS_PUBSUB_SUBSCRIBE_OPTIONS ) <EOL> self . options = form <EOL> def _render_optionsWithSubscribe ( self , verbElement ) : <EOL> if self . options is not None : <EOL> optionsElement = verbElement . parent . addElement ( '<STR_LIT>' ) <EOL> self . _render_options ( optionsElement ) <EOL> def _parse_affiliations ( self , verbElement ) : <EOL> self . affiliations = { } <EOL> for element in verbElement . elements ( ) : <EOL> if ( element . uri == NS_PUBSUB_OWNER and <EOL> element . name == '<STR_LIT>' ) : <EOL> try : <EOL> entity = jid . internJID ( element [ '<STR_LIT>' ] ) . userhostJID ( ) <EOL> except KeyError : <EOL> raise BadRequest ( text = '<STR_LIT>' ) <EOL> if entity in self . affiliations : <EOL> raise BadRequest ( text = '<STR_LIT>' ) <EOL> try : <EOL> affiliation = element [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise BadRequest ( text = '<STR_LIT>' ) <EOL> self . affiliations [ entity ] = affiliation <EOL> def parseElement ( self , element ) : <EOL> """<STR_LIT>""" <EOL> generic . Stanza . parseElement ( self , element ) <EOL> verbs = [ ] <EOL> verbElements = [ ] <EOL> for child in element . pubsub . elements ( ) : <EOL> key = ( self . stanzaType , child . uri , child . name ) <EOL> try : <EOL> verb = self . _requestVerbMap [ key ] <EOL> except KeyError : <EOL> continue <EOL> verbs . append ( verb ) <EOL> verbElements . append ( child ) <EOL> if not verbs : <EOL> raise NotImplementedError ( ) <EOL> if len ( verbs ) > <NUM_LIT:1> : <EOL> if '<STR_LIT>' in verbs and '<STR_LIT>' in verbs : <EOL> self . verb = '<STR_LIT>' <EOL> verbElement = verbElements [ verbs . index ( '<STR_LIT>' ) ] <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> else : <EOL> self . verb = verbs [ <NUM_LIT:0> ] <EOL> verbElement = verbElements [ <NUM_LIT:0> ] <EOL> for parameter in self . _parameters [ self . verb ] : <EOL> getattr ( self , '<STR_LIT>' % parameter ) ( verbElement ) <EOL> def send ( self , xs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ( self . stanzaType , <EOL> childURI , <EOL> childName ) = self . _verbRequestMap [ self . verb ] <EOL> except KeyError : <EOL> raise NotImplementedError ( ) <EOL> iq = IQ ( xs , self . stanzaType ) <EOL> iq . addElement ( ( childURI , '<STR_LIT>' ) ) <EOL> verbElement = iq . pubsub . addElement ( childName ) <EOL> if self . sender : <EOL> iq [ '<STR_LIT>' ] = self . sender . full ( ) <EOL> if self . recipient : <EOL> iq [ '<STR_LIT:to>' ] = self . recipient . full ( ) <EOL> for parameter in self . _parameters [ self . verb ] : <EOL> getattr ( self , '<STR_LIT>' % parameter ) ( verbElement ) <EOL> return iq . send ( ) <EOL> class PubSubEvent ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sender , recipient , nodeIdentifier , headers ) : <EOL> self . sender = sender <EOL> self . recipient = recipient <EOL> self . nodeIdentifier = nodeIdentifier <EOL> self . headers = headers <EOL> class ItemsEvent ( PubSubEvent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sender , recipient , nodeIdentifier , items , headers ) : <EOL> PubSubEvent . __init__ ( self , sender , recipient , nodeIdentifier , headers ) <EOL> self . items = items <EOL> class DeleteEvent ( PubSubEvent ) : <EOL> """<STR_LIT>""" <EOL> redirectURI = None <EOL> class PurgeEvent ( PubSubEvent ) : <EOL> """<STR_LIT>""" <EOL> class PubSubClient ( XMPPHandler ) : <EOL> """<STR_LIT>""" <EOL> implements ( IPubSubClient ) <EOL> def connectionInitialized ( self ) : <EOL> self . xmlstream . addObserver ( '<STR_LIT>' % <EOL> NS_PUBSUB_EVENT , self . _onEvent ) <EOL> def _onEvent ( self , message ) : <EOL> if message . getAttribute ( '<STR_LIT:type>' ) == '<STR_LIT:error>' : <EOL> return <EOL> try : <EOL> sender = jid . JID ( message [ "<STR_LIT>" ] ) <EOL> recipient = jid . JID ( message [ "<STR_LIT:to>" ] ) <EOL> except KeyError : <EOL> return <EOL> actionElement = None <EOL> for element in message . event . elements ( ) : <EOL> if element . uri == NS_PUBSUB_EVENT : <EOL> actionElement = element <EOL> if not actionElement : <EOL> return <EOL> eventHandler = getattr ( self , "<STR_LIT>" % actionElement . name , None ) <EOL> if eventHandler : <EOL> headers = shim . extractHeaders ( message ) <EOL> eventHandler ( sender , recipient , actionElement , headers ) <EOL> message . handled = True <EOL> def _onEvent_items ( self , sender , recipient , action , headers ) : <EOL> nodeIdentifier = action [ "<STR_LIT>" ] <EOL> items = [ element for element in action . elements ( ) <EOL> if element . name in ( '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> event = ItemsEvent ( sender , recipient , nodeIdentifier , items , headers ) <EOL> self . itemsReceived ( event ) <EOL> def _onEvent_delete ( self , sender , recipient , action , headers ) : <EOL> nodeIdentifier = action [ "<STR_LIT>" ] <EOL> event = DeleteEvent ( sender , recipient , nodeIdentifier , headers ) <EOL> if action . redirect : <EOL> event . redirectURI = action . redirect . getAttribute ( '<STR_LIT>' ) <EOL> self . deleteReceived ( event ) <EOL> def _onEvent_purge ( self , sender , recipient , action , headers ) : <EOL> nodeIdentifier = action [ "<STR_LIT>" ] <EOL> event = PurgeEvent ( sender , recipient , nodeIdentifier , headers ) <EOL> self . purgeReceived ( event ) <EOL> def itemsReceived ( self , event ) : <EOL> pass <EOL> def deleteReceived ( self , event ) : <EOL> pass <EOL> def purgeReceived ( self , event ) : <EOL> pass <EOL> def createNode ( self , service , nodeIdentifier = None , options = None , <EOL> sender = None ) : <EOL> """<STR_LIT>""" <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> request . recipient = service <EOL> request . nodeIdentifier = nodeIdentifier <EOL> request . sender = sender <EOL> if options : <EOL> form = data_form . Form ( formType = '<STR_LIT>' , <EOL> formNamespace = NS_PUBSUB_NODE_CONFIG ) <EOL> form . makeFields ( options ) <EOL> request . options = form <EOL> def cb ( iq ) : <EOL> try : <EOL> new_node = iq . pubsub . create [ "<STR_LIT>" ] <EOL> except AttributeError : <EOL> new_node = nodeIdentifier <EOL> return new_node <EOL> d = request . send ( self . xmlstream ) <EOL> d . addCallback ( cb ) <EOL> return d <EOL> def deleteNode ( self , service , nodeIdentifier , sender = None ) : <EOL> """<STR_LIT>""" <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> request . recipient = service <EOL> request . nodeIdentifier = nodeIdentifier <EOL> request . sender = sender <EOL> return request . send ( self . xmlstream ) <EOL> def subscribe ( self , service , nodeIdentifier , subscriber , <EOL> options = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> request . recipient = service <EOL> request . nodeIdentifier = nodeIdentifier <EOL> request . subscriber = subscriber <EOL> request . sender = sender <EOL> if options : <EOL> form = data_form . Form ( formType = '<STR_LIT>' , <EOL> formNamespace = NS_PUBSUB_SUBSCRIBE_OPTIONS ) <EOL> form . makeFields ( options ) <EOL> request . options = form <EOL> def cb ( iq ) : <EOL> subscription = Subscription . fromElement ( iq . pubsub . subscription ) <EOL> if subscription . state == '<STR_LIT>' : <EOL> raise SubscriptionPending ( ) <EOL> elif subscription . state == '<STR_LIT>' : <EOL> raise SubscriptionUnconfigured ( ) <EOL> else : <EOL> return subscription <EOL> d = request . send ( self . xmlstream ) <EOL> d . addCallback ( cb ) <EOL> return d <EOL> def unsubscribe ( self , service , nodeIdentifier , subscriber , <EOL> subscriptionIdentifier = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> request . recipient = service <EOL> request . nodeIdentifier = nodeIdentifier <EOL> request . subscriber = subscriber <EOL> request . subscriptionIdentifier = subscriptionIdentifier <EOL> request . sender = sender <EOL> return request . send ( self . xmlstream ) <EOL> def publish ( self , service , nodeIdentifier , items = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> request . recipient = service <EOL> request . nodeIdentifier = nodeIdentifier <EOL> request . items = items <EOL> request . sender = sender <EOL> return request . send ( self . xmlstream ) <EOL> def items ( self , service , nodeIdentifier , maxItems = None , <EOL> subscriptionIdentifier = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> request . recipient = service <EOL> request . nodeIdentifier = nodeIdentifier <EOL> if maxItems : <EOL> request . maxItems = str ( int ( maxItems ) ) <EOL> request . subscriptionIdentifier = subscriptionIdentifier <EOL> request . sender = sender <EOL> def cb ( iq ) : <EOL> items = [ ] <EOL> for element in iq . pubsub . items . elements ( ) : <EOL> if element . uri == NS_PUBSUB and element . name == '<STR_LIT>' : <EOL> items . append ( element ) <EOL> return items <EOL> d = request . send ( self . xmlstream ) <EOL> d . addCallback ( cb ) <EOL> return d <EOL> def getOptions ( self , service , nodeIdentifier , subscriber , <EOL> subscriptionIdentifier = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> request . recipient = service <EOL> request . nodeIdentifier = nodeIdentifier <EOL> request . subscriber = subscriber <EOL> request . subscriptionIdentifier = subscriptionIdentifier <EOL> request . sender = sender <EOL> def cb ( iq ) : <EOL> form = data_form . findForm ( iq . pubsub . options , <EOL> NS_PUBSUB_SUBSCRIBE_OPTIONS ) <EOL> form . typeCheck ( ) <EOL> return form <EOL> d = request . send ( self . xmlstream ) <EOL> d . addCallback ( cb ) <EOL> return d <EOL> def setOptions ( self , service , nodeIdentifier , subscriber , <EOL> options , subscriptionIdentifier = None , sender = None ) : <EOL> """<STR_LIT>""" <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> request . recipient = service <EOL> request . nodeIdentifier = nodeIdentifier <EOL> request . subscriber = subscriber <EOL> request . subscriptionIdentifier = subscriptionIdentifier <EOL> request . sender = sender <EOL> form = data_form . Form ( formType = '<STR_LIT>' , <EOL> formNamespace = NS_PUBSUB_SUBSCRIBE_OPTIONS ) <EOL> form . makeFields ( options ) <EOL> request . options = form <EOL> d = request . send ( self . xmlstream ) <EOL> return d <EOL> class PubSubService ( XMPPHandler , IQHandlerMixin ) : <EOL> """<STR_LIT>""" <EOL> implements ( IPubSubService , disco . IDisco ) <EOL> iqHandlers = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _legacyHandlers = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ ] ) , <EOL> '<STR_LIT:default>' : ( '<STR_LIT>' , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> } <EOL> hideNodes = False <EOL> def __init__ ( self , resource = None ) : <EOL> self . resource = resource <EOL> self . discoIdentity = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' } <EOL> self . pubSubFeatures = [ ] <EOL> def connectionMade ( self ) : <EOL> self . xmlstream . addObserver ( PUBSUB_REQUEST , self . handleRequest ) <EOL> def getDiscoInfo ( self , requestor , target , nodeIdentifier = '<STR_LIT>' ) : <EOL> def toInfo ( nodeInfo ) : <EOL> if not nodeInfo : <EOL> return <EOL> ( nodeType , metaData ) = nodeInfo [ '<STR_LIT:type>' ] , nodeInfo [ '<STR_LIT>' ] <EOL> info . append ( disco . DiscoIdentity ( '<STR_LIT>' , nodeType ) ) <EOL> if metaData : <EOL> form = data_form . Form ( formType = "<STR_LIT:result>" , <EOL> formNamespace = NS_PUBSUB_META_DATA ) <EOL> form . addField ( <EOL> data_form . Field ( <EOL> var = '<STR_LIT>' , <EOL> value = nodeType , <EOL> label = '<STR_LIT>' <EOL> ) <EOL> ) <EOL> for metaDatum in metaData : <EOL> form . addField ( data_form . Field . fromDict ( metaDatum ) ) <EOL> info . append ( form ) <EOL> return <EOL> info = [ ] <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> if self . resource is not None : <EOL> resource = self . resource . locateResource ( request ) <EOL> identity = resource . discoIdentity <EOL> features = resource . features <EOL> getInfo = resource . getInfo <EOL> else : <EOL> category = self . discoIdentity [ '<STR_LIT>' ] <EOL> idType = self . discoIdentity [ '<STR_LIT:type>' ] <EOL> name = self . discoIdentity [ '<STR_LIT:name>' ] <EOL> identity = disco . DiscoIdentity ( category , idType , name ) <EOL> features = self . pubSubFeatures <EOL> getInfo = self . getNodeInfo <EOL> if not nodeIdentifier : <EOL> info . append ( identity ) <EOL> info . append ( disco . DiscoFeature ( disco . NS_DISCO_ITEMS ) ) <EOL> info . extend ( [ disco . DiscoFeature ( "<STR_LIT>" % ( NS_PUBSUB , feature ) ) <EOL> for feature in features ] ) <EOL> d = defer . maybeDeferred ( getInfo , requestor , target , nodeIdentifier or '<STR_LIT>' ) <EOL> d . addCallback ( toInfo ) <EOL> d . addErrback ( log . err ) <EOL> d . addCallback ( lambda _ : info ) <EOL> return d <EOL> def getDiscoItems ( self , requestor , target , nodeIdentifier = '<STR_LIT>' ) : <EOL> if self . hideNodes : <EOL> d = defer . succeed ( [ ] ) <EOL> elif self . resource is not None : <EOL> request = PubSubRequest ( '<STR_LIT>' ) <EOL> resource = self . resource . locateResource ( request ) <EOL> d = resource . getNodes ( requestor , target , nodeIdentifier ) <EOL> elif nodeIdentifier : <EOL> d = self . getNodes ( requestor , target ) <EOL> else : <EOL> d = defer . succeed ( [ ] ) <EOL> d . addCallback ( lambda nodes : [ disco . DiscoItem ( target , node ) <EOL> for node in nodes ] ) <EOL> return d <EOL> def _onPubSubRequest ( self , iq ) : <EOL> request = PubSubRequest . fromElement ( iq ) <EOL> if self . resource is not None : <EOL> resource = self . resource . locateResource ( request ) <EOL> else : <EOL> resource = self <EOL> try : <EOL> preProcessor = getattr ( self , '<STR_LIT>' % request . verb ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> request = preProcessor ( resource , request ) <EOL> if request is None : <EOL> return defer . succeed ( None ) <EOL> if resource is not self : <EOL> try : <EOL> handler = getattr ( resource , request . verb ) <EOL> except AttributeError : <EOL> text = "<STR_LIT>" % request . verb <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' , text ) ) <EOL> d = handler ( request ) <EOL> else : <EOL> try : <EOL> handlerName , argNames = self . _legacyHandlers [ request . verb ] <EOL> except KeyError : <EOL> text = "<STR_LIT>" % request . verb <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' , text ) ) <EOL> handler = getattr ( self , handlerName ) <EOL> args = [ getattr ( request , arg ) for arg in argNames ] <EOL> d = handler ( * args ) <EOL> try : <EOL> cb = getattr ( self , '<STR_LIT>' % request . verb ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> d . addCallback ( cb , resource , request ) <EOL> return d <EOL> def _toResponse_subscribe ( self , result , resource , request ) : <EOL> response = domish . Element ( ( NS_PUBSUB , "<STR_LIT>" ) ) <EOL> response . addChild ( result . toElement ( NS_PUBSUB ) ) <EOL> return response <EOL> def _toResponse_subscriptions ( self , result , resource , request ) : <EOL> response = domish . Element ( ( NS_PUBSUB , '<STR_LIT>' ) ) <EOL> subscriptions = response . addElement ( '<STR_LIT>' ) <EOL> for subscription in result : <EOL> subscriptions . addChild ( subscription . toElement ( NS_PUBSUB ) ) <EOL> return response <EOL> def _toResponse_affiliations ( self , result , resource , request ) : <EOL> response = domish . Element ( ( NS_PUBSUB , '<STR_LIT>' ) ) <EOL> affiliations = response . addElement ( '<STR_LIT>' ) <EOL> for nodeIdentifier , affiliation in result : <EOL> item = affiliations . addElement ( '<STR_LIT>' ) <EOL> item [ '<STR_LIT>' ] = nodeIdentifier <EOL> item [ '<STR_LIT>' ] = affiliation <EOL> return response <EOL> def _toResponse_create ( self , result , resource , request ) : <EOL> if not request . nodeIdentifier or request . nodeIdentifier != result : <EOL> response = domish . Element ( ( NS_PUBSUB , '<STR_LIT>' ) ) <EOL> create = response . addElement ( '<STR_LIT>' ) <EOL> create [ '<STR_LIT>' ] = result <EOL> return response <EOL> else : <EOL> return None <EOL> def _formFromConfiguration ( self , resource , values ) : <EOL> fieldDefs = resource . getConfigurationOptions ( ) <EOL> form = data_form . Form ( formType = "<STR_LIT>" , <EOL> formNamespace = NS_PUBSUB_NODE_CONFIG ) <EOL> form . makeFields ( values , fieldDefs ) <EOL> return form <EOL> def _checkConfiguration ( self , resource , form ) : <EOL> fieldDefs = resource . getConfigurationOptions ( ) <EOL> form . typeCheck ( fieldDefs , filterUnknown = True ) <EOL> def _preProcess_create ( self , resource , request ) : <EOL> if request . options : <EOL> self . _checkConfiguration ( resource , request . options ) <EOL> return request <EOL> def _preProcess_default ( self , resource , request ) : <EOL> if request . nodeType not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise error . StanzaError ( '<STR_LIT>' ) <EOL> else : <EOL> return request <EOL> def _toResponse_default ( self , options , resource , request ) : <EOL> response = domish . Element ( ( NS_PUBSUB_OWNER , "<STR_LIT>" ) ) <EOL> default = response . addElement ( "<STR_LIT:default>" ) <EOL> form = self . _formFromConfiguration ( resource , options ) <EOL> default . addChild ( form . toElement ( ) ) <EOL> return response <EOL> def _toResponse_configureGet ( self , options , resource , request ) : <EOL> response = domish . Element ( ( NS_PUBSUB_OWNER , "<STR_LIT>" ) ) <EOL> configure = response . addElement ( "<STR_LIT>" ) <EOL> form = self . _formFromConfiguration ( resource , options ) <EOL> configure . addChild ( form . toElement ( ) ) <EOL> if request . nodeIdentifier : <EOL> configure [ "<STR_LIT>" ] = request . nodeIdentifier <EOL> return response <EOL> def _preProcess_configureSet ( self , resource , request ) : <EOL> if request . options . formType == '<STR_LIT>' : <EOL> return None <EOL> else : <EOL> self . _checkConfiguration ( resource , request . options ) <EOL> return request <EOL> def _toResponse_items ( self , result , resource , request ) : <EOL> response = domish . Element ( ( NS_PUBSUB , '<STR_LIT>' ) ) <EOL> items = response . addElement ( '<STR_LIT>' ) <EOL> items [ "<STR_LIT>" ] = request . nodeIdentifier <EOL> for item in result : <EOL> item . uri = NS_PUBSUB <EOL> items . addChild ( item ) <EOL> return response <EOL> def _createNotification ( self , eventType , service , nodeIdentifier , <EOL> subscriber , subscriptions = None ) : <EOL> headers = [ ] <EOL> if subscriptions : <EOL> for subscription in subscriptions : <EOL> if nodeIdentifier != subscription . nodeIdentifier : <EOL> headers . append ( ( '<STR_LIT>' , subscription . nodeIdentifier ) ) <EOL> message = domish . Element ( ( None , "<STR_LIT:message>" ) ) <EOL> message [ "<STR_LIT>" ] = service . full ( ) <EOL> message [ "<STR_LIT:to>" ] = subscriber . full ( ) <EOL> event = message . addElement ( ( NS_PUBSUB_EVENT , "<STR_LIT>" ) ) <EOL> element = event . addElement ( eventType ) <EOL> element [ "<STR_LIT>" ] = nodeIdentifier <EOL> if headers : <EOL> message . addChild ( shim . Headers ( headers ) ) <EOL> return message <EOL> def _toResponse_affiliationsGet ( self , result , resource , request ) : <EOL> response = domish . Element ( ( NS_PUBSUB_OWNER , '<STR_LIT>' ) ) <EOL> affiliations = response . addElement ( '<STR_LIT>' ) <EOL> if request . nodeIdentifier : <EOL> affiliations [ '<STR_LIT>' ] = request . nodeIdentifier <EOL> for entity , affiliation in result . iteritems ( ) : <EOL> item = affiliations . addElement ( '<STR_LIT>' ) <EOL> item [ '<STR_LIT>' ] = entity . full ( ) <EOL> item [ '<STR_LIT>' ] = affiliation <EOL> return response <EOL> def notifyPublish ( self , service , nodeIdentifier , notifications ) : <EOL> for subscriber , subscriptions , items in notifications : <EOL> message = self . _createNotification ( '<STR_LIT>' , service , <EOL> nodeIdentifier , subscriber , <EOL> subscriptions ) <EOL> for item in items : <EOL> item . uri = NS_PUBSUB_EVENT <EOL> message . event . items . addChild ( item ) <EOL> self . send ( message ) <EOL> def notifyDelete ( self , service , nodeIdentifier , subscribers , <EOL> redirectURI = None ) : <EOL> for subscriber in subscribers : <EOL> message = self . _createNotification ( '<STR_LIT>' , service , <EOL> nodeIdentifier , <EOL> subscriber ) <EOL> if redirectURI : <EOL> redirect = message . event . delete . addElement ( '<STR_LIT>' ) <EOL> redirect [ '<STR_LIT>' ] = redirectURI <EOL> self . send ( message ) <EOL> def getNodeInfo ( self , requestor , service , nodeIdentifier ) : <EOL> return None <EOL> def getNodes ( self , requestor , service ) : <EOL> return [ ] <EOL> def publish ( self , requestor , service , nodeIdentifier , items ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def subscribe ( self , requestor , service , nodeIdentifier , subscriber ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def unsubscribe ( self , requestor , service , nodeIdentifier , subscriber ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def subscriptions ( self , requestor , service ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def affiliations ( self , requestor , service ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def create ( self , requestor , service , nodeIdentifier ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def getConfigurationOptions ( self ) : <EOL> return { } <EOL> def getDefaultConfiguration ( self , requestor , service , nodeType ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def getConfiguration ( self , requestor , service , nodeIdentifier ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def setConfiguration ( self , requestor , service , nodeIdentifier , options ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def items ( self , requestor , service , nodeIdentifier , maxItems , <EOL> itemIdentifiers ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def retract ( self , requestor , service , nodeIdentifier , itemIdentifiers ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def purge ( self , requestor , service , nodeIdentifier ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> def delete ( self , requestor , service , nodeIdentifier ) : <EOL> raise Unsupported ( '<STR_LIT>' ) <EOL> class PubSubResource ( object ) : <EOL> implements ( IPubSubResource ) <EOL> features = [ ] <EOL> discoIdentity = disco . DiscoIdentity ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def locateResource ( self , request ) : <EOL> return self <EOL> def getInfo ( self , requestor , service , nodeIdentifier ) : <EOL> return defer . succeed ( None ) <EOL> def getNodes ( self , requestor , service , nodeIdentifier ) : <EOL> return defer . succeed ( [ ] ) <EOL> def getConfigurationOptions ( self ) : <EOL> return { } <EOL> def publish ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def subscribe ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def unsubscribe ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def subscriptions ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def affiliations ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def create ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def default ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def configureGet ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def configureSet ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def items ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def retract ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def purge ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def delete ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def affiliationsGet ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def affiliationsSet ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def subscriptionsGet ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) <EOL> def subscriptionsSet ( self , request ) : <EOL> return defer . fail ( Unsupported ( '<STR_LIT>' ) ) </s>
