# -*- test-case-name: tx_xmpp.test.test.test_pubsub -*- # # Copyright (c) Ralph Meijer, 2007-2021 # Copyright (c) Adrien Cossa, 2016 # Copyright (c) Jérôme Poisson, 2017-2026 # See LICENSE for details. """ XMPP publish-subscribe protocol. This protocol is specified in U{XEP-0060}. """ from typing import TYPE_CHECKING, Literal, Self from twisted.internet import defer from twisted.python import log from twisted.words.protocols.jabber import error, jid from twisted.words.xish import domish from zope.interface import implementer from . import data_form, disco, generic, shim from .compat import IQ from .itx_xmpp import IPubSubClient, IPubSubResource, IPubSubService from .subprotocols import IQHandlerMixin, XMPPHandler if TYPE_CHECKING: from . import rsm # Iq get and set XPath queries IQ_GET = '/iq[@type="get"]' IQ_SET = '/iq[@type="set"]' # Publish-subscribe namespaces NS_PUBSUB = "http://jabber.org/protocol/pubsub" NS_PUBSUB_EVENT = NS_PUBSUB + "#event" NS_PUBSUB_ERRORS = NS_PUBSUB + "#errors" NS_PUBSUB_OWNER = NS_PUBSUB + "#owner" NS_PUBSUB_NODE_CONFIG = NS_PUBSUB + "#node_config" NS_PUBSUB_META_DATA = NS_PUBSUB + "#meta-data" NS_PUBSUB_SUBSCRIBE_OPTIONS = NS_PUBSUB + "#subscribe_options" NS_PUBSUB_PUBLISH_OPTIONS = NS_PUBSUB + "#publish-options" NS_ORDER_BY = "urn:xmpp:order-by:0" # XPath to match pubsub requests PUBSUB_REQUEST = ( '/iq[@type="get" or @type="set"]/' + 'pubsub[@xmlns="' + NS_PUBSUB + '" or ' + '@xmlns="' + NS_PUBSUB_OWNER + '"]' ) BOOL_TRUE = ("1", "true") BOOL_FALSE = ("0", "false") class SubscriptionPending(Exception): """ Raised when the requested subscription is pending acceptance. """ class SubscriptionUnconfigured(Exception): """ Raised when the requested subscription needs to be configured before becoming active. """ class PubSubError(error.StanzaError): """ Exception with publish-subscribe specific condition. """ def __init__(self, condition, pubsubCondition, feature=None, text=None): appCondition = domish.Element((NS_PUBSUB_ERRORS, pubsubCondition)) if feature: appCondition["feature"] = feature error.StanzaError.__init__(self, condition, text=text, appCondition=appCondition) class BadRequest(error.StanzaError): """ Bad request stanza error. """ def __init__(self, pubsubCondition=None, text=None): if pubsubCondition: appCondition = domish.Element((NS_PUBSUB_ERRORS, pubsubCondition)) else: appCondition = None error.StanzaError.__init__( self, "bad-request", text=text, appCondition=appCondition ) class Unsupported(PubSubError): def __init__(self, feature, text=None): self.feature = feature PubSubError.__init__( self, "feature-not-implemented", "unsupported", feature, text ) def __str__(self): message = PubSubError.__str__(self) message += ", feature %r" % self.feature return message class Subscription: """ A subscription to a node. @ivar nodeIdentifier: The identifier of the node subscribed to. The root node is denoted by L{None}. @ivar subscriber: The subscribing entity. @ivar state: The subscription state. One of C{'subscribed'}, C{'pending'}, C{'unconfigured'}, C{'none'}. @ivar options: Optional list of subscription options. @ivar subscriptionIdentifier: Optional subscription identifier. """ def __init__( self, nodeIdentifier: str | None, subscriber: jid.JID | None, state: Literal["subscribed", "pending", "unconfigured", "none"] | None, options: dict | None = None, subscriptionIdentifier: str | None = None, ) -> None: self.nodeIdentifier = nodeIdentifier self.subscriber = subscriber self.state = state self.options = options or {} self.subscriptionIdentifier = subscriptionIdentifier @staticmethod def fromElement(element: domish.Element) -> "Subscription": return Subscription( element.getAttribute("node"), jid.JID(element.getAttribute("jid")), element.getAttribute("subscription"), subscriptionIdentifier=element.getAttribute("subid"), ) def toElement(self, defaultUri: str | None = None) -> domish.Element: """ Return the DOM representation of this subscription. @rtype: L{domish.Element} """ element = domish.Element((defaultUri, "subscription")) if self.nodeIdentifier: element["node"] = self.nodeIdentifier element["jid"] = str(self.subscriber) element["subscription"] = self.state if self.subscriptionIdentifier: element["subid"] = self.subscriptionIdentifier return element class Item(domish.Element): """ Publish subscribe item. This behaves like an object providing L{domish.IElement}. Item payload can be added using C{addChild} or C{addRawXml}, or using the C{payload} keyword argument to C{__init__}. """ def __init__(self, id=None, payload=None): """ @param id: optional item identifier @type id: L{str} @param payload: optional item payload. Either as a domish element, or as serialized XML. @type payload: object providing L{domish.IElement} or L{str}. """ domish.Element.__init__(self, (None, "item")) if id is not None: self["id"] = id if payload is not None: if isinstance(payload, str): self.addRawXml(payload) else: self.addChild(payload) class PubSubRequest(generic.Stanza): """ A publish-subscribe request. The set of instance variables used depends on the type of request. If a variable is not applicable or not passed in the request, its value is L{None}. @ivar verb: The type of publish-subscribe request. See C{_requestVerbMap}. @ivar affiliations: Affiliations to be modified, as a dictionary of entity (L{JID} to affiliation (L{str}). @ivar items: The items to be published, as L{domish.Element}s. @ivar itemIdentifiers: Identifiers of the items to be retrieved or retracted. @ivar maxItems: Maximum number of items to retrieve. @ivar nodeIdentifier: Identifier of the node the request is about. @ivar nodeType: The type of node that should be created, or for which the configuration is retrieved. C{'leaf'} or C{'collection'}. @ivar options: Configurations options for nodes, subscriptions and publish requests. @ivar subscriber: The subscribing entity. @ivar subscriptionIdentifier: Identifier for a specific subscription. @ivar subscriptions: Subscriptions to be modified, as a set of L{Subscription}. @ivar notify: Whether to notify subscribers of retracted items. @ivar orderBy: List of order-by attributes for items retrieval. """ verb: str | None = None affiliations: dict[jid.JID, str] | None = None items: list[domish.Element] | None = None itemIdentifiers: list[str] | None = None maxItems: int | None = None nodeIdentifier: str | None = None nodeType: str | None = None options: data_form.Form | None = None subscriber: jid.JID | None = None subscriptionIdentifier: str | None = None subscriptions: set[Subscription] | None = None notify: bool | None = None orderBy: list[str] | None = None # Map request iq type and subelement name to request verb _requestVerbMap = { ("set", NS_PUBSUB, "publish"): "publish", ("set", NS_PUBSUB, "subscribe"): "subscribe", ("set", NS_PUBSUB, "unsubscribe"): "unsubscribe", ("get", NS_PUBSUB, "options"): "optionsGet", ("set", NS_PUBSUB, "options"): "optionsSet", ("get", NS_PUBSUB, "subscriptions"): "subscriptions", ("get", NS_PUBSUB, "affiliations"): "affiliations", ("set", NS_PUBSUB, "create"): "create", ("get", NS_PUBSUB_OWNER, "default"): "default", ("get", NS_PUBSUB_OWNER, "configure"): "configureGet", ("set", NS_PUBSUB_OWNER, "configure"): "configureSet", ("get", NS_PUBSUB, "items"): "items", ("set", NS_PUBSUB, "retract"): "retract", ("set", NS_PUBSUB_OWNER, "purge"): "purge", ("set", NS_PUBSUB_OWNER, "delete"): "delete", ("get", NS_PUBSUB_OWNER, "affiliations"): "affiliationsGet", ("set", NS_PUBSUB_OWNER, "affiliations"): "affiliationsSet", ("get", NS_PUBSUB_OWNER, "subscriptions"): "subscriptionsGet", ("set", NS_PUBSUB_OWNER, "subscriptions"): "subscriptionsSet", } # Map request verb to request iq type and subelement name _verbRequestMap = dict(((v, k) for k, v in _requestVerbMap.items())) # Map request verb to parameter handler names _parameters = { "publish": ["node", "items", "publishOptionsOrNone"], "subscribe": ["nodeOrEmpty", "jid", "optionsWithSubscribe"], "unsubscribe": ["nodeOrEmpty", "jid", "subidOrNone"], "optionsGet": ["nodeOrEmpty", "jid", "subidOrNone"], "optionsSet": ["nodeOrEmpty", "jid", "options", "subidOrNone"], "subscriptions": ["nodeOrNone"], "affiliations": ["nodeOrNone"], "create": ["nodeOrNone", "configureOrNone"], "default": ["default"], "configureGet": ["nodeOrEmpty"], "configureSet": ["nodeOrEmpty", "configureOrNone"], "items": ["node", "maxItems", "itemIdentifiers", "subidOrNone", "orderBy"], "retract": ["node", "notify", "itemIdentifiers"], "purge": ["node"], "delete": ["node"], "affiliationsGet": ["nodeOrNone"], "affiliationsSet": ["node", "affiliations"], "subscriptionsGet": ["nodeOrNone"], "subscriptionsSet": ["node", "subscriptions"], } def __init__(self, verb=None): self.verb = verb def _parse_node(self, verbElement): """ Parse the required node identifier out of the verbElement. """ try: self.nodeIdentifier = verbElement["node"] except KeyError: raise BadRequest("nodeid-required") def _render_node(self, verbElement): """ Render the required node identifier on the verbElement. """ if not self.nodeIdentifier: raise Exception("Node identifier is required") verbElement["node"] = self.nodeIdentifier def _parse_nodeOrEmpty(self, verbElement): """ Parse the node identifier out of the verbElement. May be empty. """ self.nodeIdentifier = verbElement.getAttribute("node", "") def _render_nodeOrEmpty(self, verbElement): """ Render the node identifier on the verbElement. May be empty. """ if self.nodeIdentifier: verbElement["node"] = self.nodeIdentifier def _parse_nodeOrNone(self, verbElement): """ Parse the optional node identifier out of the verbElement. """ self.nodeIdentifier = verbElement.getAttribute("node") def _render_nodeOrNone(self, verbElement): """ Render the optional node identifier on the verbElement. """ if self.nodeIdentifier: verbElement["node"] = self.nodeIdentifier def _parse_items(self, verbElement): """ Parse items out of the verbElement for publish requests. """ self.items = [] for element in verbElement.elements(): if element.uri == NS_PUBSUB and element.name == "item": self.items.append(element) def _render_items(self, verbElement): """ Render items into the verbElement for publish requests. """ if self.items: for item in self.items: item.uri = NS_PUBSUB verbElement.addChild(item) def _parse_publishOptionsOrNone(self, verbElement): """ Parse optional publish-options form in publish request. """ for element in verbElement.parent.elements(): form = data_form.findForm(element, NS_PUBSUB_PUBLISH_OPTIONS) if form is not None: if form.formType != "submit": raise BadRequest(text="Unexpected form type '%s'" % form.formType) else: form = data_form.Form("submit", formNamespace=NS_PUBSUB_PUBLISH_OPTIONS) self.options = form def _render_publishOptionsOrNone(self, verbElement): if self.options is not None: if self.options.formType != "submit": log.err( "Invalid type for publish-options form ({formType}): {xml}".format( formType=self.options.formType, xml=verbElement.toXml() ) ) return publishOptions = verbElement.parent.addElement("publish-options") publishOptions.addChild(self.options.toElement()) def _parse_jid(self, verbElement): """ Parse subscriber out of the verbElement for un-/subscribe requests. """ try: self.subscriber = jid.internJID(verbElement["jid"]) except KeyError: raise BadRequest("jid-required") def _render_jid(self, verbElement): """ Render subscriber into the verbElement for un-/subscribe requests. """ verbElement["jid"] = self.subscriber.full() def _parse_default(self, verbElement): """ Parse node type out of a request for the default node configuration. """ form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG) if form is not None and form.formType == "submit": values = form.getValues() self.nodeType = values.get("pubsub#node_type", "leaf") else: self.nodeType = "leaf" def _parse_configure(self, verbElement): """ Parse options out of a request for setting the node configuration. """ form = data_form.findForm(verbElement, NS_PUBSUB_NODE_CONFIG) if form is not None: if form.formType in ("submit", "cancel"): self.options = form else: raise BadRequest(text="Unexpected form type '%s'" % form.formType) else: raise BadRequest(text="Missing configuration form") def _parse_configureOrNone(self, verbElement): """ Parse optional node configuration form in create request. """ for element in verbElement.parent.elements(): if ( element.uri in (NS_PUBSUB, NS_PUBSUB_OWNER) and element.name == "configure" ): form = data_form.findForm(element, NS_PUBSUB_NODE_CONFIG) if form is not None: if form.formType in ("submit", "cancel"): self.options = form else: raise BadRequest(text="Unexpected form type '%s'" % form.formType) else: if self.verb == "create": # Empty in create request means use default config self.options = data_form.Form( "submit", formNamespace=NS_PUBSUB_NODE_CONFIG ) else: # For configureSet, a form is required raise BadRequest(text="data form is required") def _render_configureOrNone(self, verbElement): """ Render optional node configuration form in create request. """ if self.options is not None: if verbElement.name == "configure": configure = verbElement else: configure = verbElement.parent.addElement("configure") configure.addChild(self.options.toElement()) def _parse_itemIdentifiers(self, verbElement): """ Parse item identifiers out of items and retract requests. """ self.itemIdentifiers = [] for element in verbElement.elements(): if element.uri == NS_PUBSUB and element.name == "item": try: self.itemIdentifiers.append(element["id"]) except KeyError: raise BadRequest() def _render_itemIdentifiers(self, verbElement): """ Render item identifiers into items and retract requests. """ if self.itemIdentifiers: for itemIdentifier in self.itemIdentifiers: item = verbElement.addElement("item") item["id"] = itemIdentifier def _parse_maxItems(self, verbElement): """ Parse maximum items out of an items request. """ value = verbElement.getAttribute("max_items") if value: try: self.maxItems = int(value) except ValueError: raise BadRequest( text="Field max_items requires a positive " + "integer value" ) def _render_maxItems(self, verbElement): """ Render maximum items into an items request. """ if self.maxItems: verbElement["max_items"] = str(self.maxItems) def _parse_subidOrNone(self, verbElement): """ Parse subscription identifier out of a request. """ self.subscriptionIdentifier = verbElement.getAttribute("subid") def _render_subidOrNone(self, verbElement): """ Render subscription identifier into a request. """ if self.subscriptionIdentifier: verbElement["subid"] = self.subscriptionIdentifier def _parse_options(self, verbElement): """ Parse options form out of a subscription options request. """ form = data_form.findForm(verbElement, NS_PUBSUB_SUBSCRIBE_OPTIONS) if form is not None: if form.formType in ("submit", "cancel"): self.options = form else: raise BadRequest(text="Unexpected form type '%s'" % form.formType) else: raise BadRequest(text="Missing options form") def _render_options(self, verbElement): verbElement.addChild(self.options.toElement()) def _parse_optionsWithSubscribe(self, verbElement): for element in verbElement.parent.elements(): if element.name == "options" and element.uri == NS_PUBSUB: form = data_form.findForm(element, NS_PUBSUB_SUBSCRIBE_OPTIONS) if form is not None: if form.formType != "submit": raise BadRequest(text="Unexpected form type '%s'" % form.formType) else: form = data_form.Form( "submit", formNamespace=NS_PUBSUB_SUBSCRIBE_OPTIONS ) self.options = form def _render_optionsWithSubscribe(self, verbElement): if self.options is not None: optionsElement = verbElement.parent.addElement("options") self._render_options(optionsElement) def _parse_affiliations(self, verbElement): self.affiliations = {} for element in verbElement.elements(): if element.uri == NS_PUBSUB_OWNER and element.name == "affiliation": try: entity = jid.internJID(element["jid"]).userhostJID() except KeyError: raise BadRequest(text="Missing jid attribute") if entity in self.affiliations: raise BadRequest(text="Multiple affiliations for an entity") try: affiliation = element["affiliation"] except KeyError: raise BadRequest(text="Missing affiliation attribute") self.affiliations[entity] = affiliation def _render_affiliations(self, verbElement): for entity, affiliation in self.affiliations.items(): affiliationElement = verbElement.addElement((NS_PUBSUB_OWNER, "affiliation")) affiliationElement["jid"] = entity.full() affiliationElement["affiliation"] = affiliation def _parse_subscriptions(self, verbElement): self.subscriptions = set() seen_entities = set() for element in verbElement.elements(): if element.uri == NS_PUBSUB_OWNER and element.name == "subscription": try: subscriber = jid.internJID(element["jid"]).userhostJID() except KeyError: raise BadRequest(text="Missing jid attribute") if subscriber in seen_entities: raise BadRequest(text="Multiple subscriptions for an subscriber") seen_entities.add(subscriber) try: state = element["subscription"] except KeyError: # §8.8.2.1 says that value MUST NOT be changed # if subscription is missing continue self.subscriptions.add( Subscription(self.nodeIdentifier, subscriber, state) ) def _render_subscriptions(self, verbElement): for subscription in self.subscriptions: subscriptionElement = verbElement.addElement( (NS_PUBSUB_OWNER, "subscription") ) subscriptionElement["jid"] = subscription.subscriber.full() subscriptionElement["subscription"] = subscription.state def _parse_notify(self, verbElement): value = verbElement.getAttribute("notify") if value: if value in BOOL_TRUE: self.notify = True elif value in BOOL_FALSE: self.notify = False else: raise BadRequest(text="Field notify must be a boolean value") def _render_notify(self, verbElement): if self.notify is not None: verbElement["notify"] = "true" if self.notify else "false" def _parse_orderBy(self, verbElement): pubsub_elt = verbElement.parent self.orderBy = [] for element in pubsub_elt.elements(NS_ORDER_BY, "order"): self.orderBy.append(element["by"]) def _render_orderBy(self, verbElement): if self.orderBy is None: return pubsub_elt = verbElement.parent for by_attr in self.orderBy: order_elt = pubsub_elt.addElement((NS_ORDER_BY, "order")) order_elt["by"] = by_attr def parseElement(self, element): """ Parse the publish-subscribe verb and parameters out of a request. """ generic.Stanza.parseElement(self, element) verbs = [] verbElements = [] for child in element.pubsub.elements(): key = (self.stanzaType, child.uri, child.name) try: verb = self._requestVerbMap[key] except KeyError: continue verbs.append(verb) verbElements.append(child) if not verbs: raise NotImplementedError() if len(verbs) > 1: if "optionsSet" in verbs and "subscribe" in verbs: self.verb = "subscribe" verbElement = verbElements[verbs.index("subscribe")] else: raise NotImplementedError() else: self.verb = verbs[0] verbElement = verbElements[0] for parameter in self._parameters[self.verb]: getattr(self, "_parse_%s" % parameter)(verbElement) def send(self, xs): """ Send this request to its recipient. This renders all of the relevant parameters for this specific requests into an L{IQ}, and invoke its C{send} method. This returns a deferred that fires upon reception of a response. See L{IQ} for details. @param xs: The XML stream to send the request on. @type xs: L{twisted.words.protocols.jabber.xmlstream.XmlStream} @rtype: L{defer.Deferred}. """ try: self.stanzaType, childURI, childName = self._verbRequestMap[self.verb] except KeyError: raise NotImplementedError() iq = IQ(xs, self.stanzaType) iq.addElement((childURI, "pubsub")) verbElement = iq.pubsub.addElement(childName) if self.sender: iq["from"] = self.sender.full() if self.recipient: iq["to"] = self.recipient.full() for parameter in self._parameters[self.verb]: getattr(self, "_render_%s" % parameter)(verbElement) return iq.send() class PubSubEvent: """ A publish subscribe event. @param sender: The entity from which the notification was received. @type sender: L{jid.JID} @param recipient: The entity to which the notification was sent. @type recipient: L{tx_xmpp.pubsub.ItemsEvent} @param nodeIdentifier: Identifier of the node the event pertains to. @type nodeIdentifier: L{str} @param headers: SHIM headers, see L{tx_xmpp.shim.extractHeaders}. @type headers: L{dict} """ def __init__(self, sender, recipient, nodeIdentifier, headers): self.sender = sender self.recipient = recipient self.nodeIdentifier = nodeIdentifier self.headers = headers class ItemsEvent(PubSubEvent): """ A publish-subscribe event that signifies new, updated and retracted items. @param items: List of received items as domish elements. @type items: L{list} of L{domish.Element} """ def __init__(self, sender, recipient, nodeIdentifier, items, headers): PubSubEvent.__init__(self, sender, recipient, nodeIdentifier, headers) self.items = items class DeleteEvent(PubSubEvent): """ A publish-subscribe event that signifies the deletion of a node. """ redirectURI = None class PurgeEvent(PubSubEvent): """ A publish-subscribe event that signifies the purging of a node. """ @implementer(IPubSubClient) class PubSubClient(XMPPHandler): """ Publish subscribe client protocol. """ _request_class = PubSubRequest def connectionInitialized(self): self.xmlstream.addObserver( '/message/event[@xmlns="%s"]' % NS_PUBSUB_EVENT, self._onEvent ) def _onEvent(self, message): if message.getAttribute("type") == "error": return try: sender = jid.JID(message["from"]) recipient = jid.JID(message["to"]) except KeyError: return actionElement = None for element in message.event.elements(): if element.uri == NS_PUBSUB_EVENT: actionElement = element if not actionElement: return eventHandler = getattr(self, "_onEvent_%s" % actionElement.name, None) if eventHandler: headers = shim.extractHeaders(message) eventHandler(sender, recipient, actionElement, headers) message.handled = True def _onEvent_items(self, sender, recipient, action, headers): nodeIdentifier = action["node"] items = [ element for element in action.elements() if element.name in ("item", "retract") ] event = ItemsEvent(sender, recipient, nodeIdentifier, items, headers) self.itemsReceived(event) def _onEvent_delete(self, sender, recipient, action, headers): nodeIdentifier = action["node"] event = DeleteEvent(sender, recipient, nodeIdentifier, headers) if action.redirect: event.redirectURI = action.redirect.getAttribute("uri") self.deleteReceived(event) def _onEvent_purge(self, sender, recipient, action, headers): nodeIdentifier = action["node"] event = PurgeEvent(sender, recipient, nodeIdentifier, headers) self.purgeReceived(event) def itemsReceived(self, event): pass def deleteReceived(self, event): pass def purgeReceived(self, event): pass def createNode(self, service, nodeIdentifier=None, options=None, sender=None): """ Create a publish subscribe node. @param service: The publish subscribe service to create the node at. @type service: L{JID} @param nodeIdentifier: Optional suggestion for the id of the node. @type nodeIdentifier: L{str} @param options: Optional node configuration options. @type options: L{dict} """ request = self._request_class("create") request.recipient = service request.nodeIdentifier = nodeIdentifier request.sender = sender if options: form = data_form.Form(formType="submit", formNamespace=NS_PUBSUB_NODE_CONFIG) form.makeFields(options) request.options = form def cb(iq): try: new_node = iq.pubsub.create["node"] except AttributeError: # the suggested node identifier was accepted new_node = nodeIdentifier return new_node d = request.send(self.xmlstream) d.addCallback(cb) return d def deleteNode(self, service, nodeIdentifier, sender=None): """ Delete a publish subscribe node. @param service: The publish subscribe service to delete the node from. @type service: L{JID} @param nodeIdentifier: The identifier of the node. @type nodeIdentifier: L{str} """ request = self._request_class("delete") request.recipient = service request.nodeIdentifier = nodeIdentifier request.sender = sender return request.send(self.xmlstream) def subscribe(self, service, nodeIdentifier, subscriber, options=None, sender=None): """ Subscribe to a publish subscribe node. @param service: The publish subscribe service that keeps the node. @type service: L{JID} @param nodeIdentifier: The identifier of the node. @type nodeIdentifier: L{str} @param subscriber: The entity to subscribe to the node. This entity will get notifications of new published items. @type subscriber: L{JID} @param options: Subscription options. @type options: L{dict} @return: Deferred that fires with L{Subscription} or errbacks with L{SubscriptionPending} or L{SubscriptionUnconfigured}. @rtype: L{defer.Deferred} """ request = self._request_class("subscribe") request.recipient = service request.nodeIdentifier = nodeIdentifier request.subscriber = subscriber request.sender = sender if options: form = data_form.Form( formType="submit", formNamespace=NS_PUBSUB_SUBSCRIBE_OPTIONS ) form.makeFields(options) request.options = form def cb(iq): subscription = Subscription.fromElement(iq.pubsub.subscription) if subscription.state == "pending": raise SubscriptionPending() elif subscription.state == "unconfigured": raise SubscriptionUnconfigured() else: # we assume subscription == 'subscribed' # any other value would be invalid, but that should have # yielded a stanza error. return subscription d = request.send(self.xmlstream) d.addCallback(cb) return d def unsubscribe( self, service, nodeIdentifier, subscriber, subscriptionIdentifier=None, sender=None, ): """ Unsubscribe from a publish subscribe node. @param service: The publish subscribe service that keeps the node. @type service: L{JID} @param nodeIdentifier: The identifier of the node. @type nodeIdentifier: L{str} @param subscriber: The entity to unsubscribe from the node. @type subscriber: L{JID} @param subscriptionIdentifier: Optional subscription identifier. @type subscriptionIdentifier: L{str} """ request = self._request_class("unsubscribe") request.recipient = service request.nodeIdentifier = nodeIdentifier request.subscriber = subscriber request.subscriptionIdentifier = subscriptionIdentifier request.sender = sender return request.send(self.xmlstream) def publish(self, service, nodeIdentifier, items=None, sender=None, options=None): """ Publish to a publish subscribe node. @param service: The publish subscribe service that keeps the node. @type service: L{JID} @param nodeIdentifier: The identifier of the node. @type nodeIdentifier: L{str} @param items: Optional list of L{Item}s to publish. @type items: L{list} @param options: Optional publish-options form (see XEP-0060 §7.1.5) @type options: C{dict} """ request = self._request_class("publish") request.recipient = service request.nodeIdentifier = nodeIdentifier request.items = items if options: form = data_form.Form( formType="submit", formNamespace=NS_PUBSUB_PUBLISH_OPTIONS ) form.makeFields(options) request.options = form request.sender = sender return request.send(self.xmlstream) def items( self, service, nodeIdentifier, maxItems=None, subscriptionIdentifier=None, sender=None, itemIdentifiers=None, orderBy=None, ): """ Retrieve previously published items from a publish subscribe node. @param service: The publish subscribe service that keeps the node. @type service: L{JID} @param nodeIdentifier: The identifier of the node. @type nodeIdentifier: L{str} @param maxItems: Optional limit on the number of retrieved items. @type maxItems: L{int} @param subscriptionIdentifier: Optional subscription identifier. In case the node has been subscribed to multiple times, this narrows the results to the specific subscription. @type subscriptionIdentifier: L{str} @param itemIdentifiers: Identifiers of the items to be retrieved. @type itemIdentifiers: L{set} of L{str} @param orderBy: Keys to order by @type orderBy: L{list} of L{unicode} """ request = self._request_class("items") request.recipient = service request.nodeIdentifier = nodeIdentifier if maxItems: request.maxItems = str(int(maxItems)) request.subscriptionIdentifier = subscriptionIdentifier request.sender = sender request.itemIdentifiers = itemIdentifiers request.orderBy = orderBy def cb(iq): items = [] for element in iq.pubsub.items.elements(): if element.uri == NS_PUBSUB and element.name == "item": items.append(element) return items d = request.send(self.xmlstream) d.addCallback(cb) return d def retractItems( self, service, nodeIdentifier, itemIdentifiers, notify=None, sender=None ): """ Retract items from a publish subscribe node. @param service: The publish subscribe service to delete the node from. @type service: L{JID} @param nodeIdentifier: The identifier of the node. @type nodeIdentifier: C{unicode} @param itemIdentifiers: Identifiers of the items to be retracted. @type itemIdentifiers: C{set} @param notify: True if notification is required @type notify: C{unicode} """ request = self._request_class("retract") request.recipient = service request.nodeIdentifier = nodeIdentifier request.itemIdentifiers = itemIdentifiers request.notify = notify request.sender = sender return request.send(self.xmlstream) def getOptions( self, service, nodeIdentifier, subscriber, subscriptionIdentifier=None, sender=None, ): """ Get subscription options. @param service: The publish subscribe service that keeps the node. @type service: L{JID} @param nodeIdentifier: The identifier of the node. @type nodeIdentifier: L{str} @param subscriber: The entity subscribed to the node. @type subscriber: L{JID} @param subscriptionIdentifier: Optional subscription identifier. @type subscriptionIdentifier: L{str} @rtype: L{data_form.Form} """ request = self._request_class("optionsGet") request.recipient = service request.nodeIdentifier = nodeIdentifier request.subscriber = subscriber request.subscriptionIdentifier = subscriptionIdentifier request.sender = sender def cb(iq): form = data_form.findForm(iq.pubsub.options, NS_PUBSUB_SUBSCRIBE_OPTIONS) form.typeCheck() return form d = request.send(self.xmlstream) d.addCallback(cb) return d def setOptions( self, service, nodeIdentifier, subscriber, options, subscriptionIdentifier=None, sender=None, ): """ Set subscription options. @param service: The publish subscribe service that keeps the node. @type service: L{JID} @param nodeIdentifier: The identifier of the node. @type nodeIdentifier: L{str} @param subscriber: The entity subscribed to the node. @type subscriber: L{JID} @param options: Subscription options. @type options: L{dict}. @param subscriptionIdentifier: Optional subscription identifier. @type subscriptionIdentifier: L{str} """ request = self._request_class("optionsSet") request.recipient = service request.nodeIdentifier = nodeIdentifier request.subscriber = subscriber request.subscriptionIdentifier = subscriptionIdentifier request.sender = sender form = data_form.Form( formType="submit", formNamespace=NS_PUBSUB_SUBSCRIBE_OPTIONS ) form.makeFields(options) request.options = form d = request.send(self.xmlstream) return d @implementer(IPubSubService, disco.IDisco) class PubSubService(XMPPHandler, IQHandlerMixin): """ Protocol implementation for a XMPP Publish Subscribe Service. The word Service here is used as taken from the Publish Subscribe specification. It is the party responsible for keeping nodes and their subscriptions, and sending out notifications. Methods from the L{IPubSubService} interface that are called as a result of an XMPP request may raise exceptions. Alternatively the deferred returned by these methods may have their errback called. These are handled as follows: - If the exception is an instance of L{error.StanzaError}, an error response iq is returned. - Any other exception is reported using L{log.msg}. An error response with the condition C{internal-server-error} is returned. The default implementation of said methods raises an L{Unsupported} exception and are meant to be overridden. @ivar discoIdentity: Service discovery identity as a dictionary with keys C{'category'}, C{'type'} and C{'name'}. @ivar pubSubFeatures: List of supported publish-subscribe features for service discovery, as L{str}. @type pubSubFeatures: L{list} or L{None} """ iqHandlers = { "/*": "_onPubSubRequest", } _legacyHandlers = { "publish": ("publish", ["sender", "recipient", "nodeIdentifier", "items"]), "subscribe": ( "subscribe", ["sender", "recipient", "nodeIdentifier", "subscriber"], ), "unsubscribe": ( "unsubscribe", ["sender", "recipient", "nodeIdentifier", "subscriber"], ), "subscriptions": ("subscriptions", ["sender", "recipient"]), "affiliations": ("affiliations", ["sender", "recipient"]), "create": ("create", ["sender", "recipient", "nodeIdentifier"]), "getConfigurationOptions": ("getConfigurationOptions", []), "default": ("getDefaultConfiguration", ["sender", "recipient", "nodeType"]), "configureGet": ("getConfiguration", ["sender", "recipient", "nodeIdentifier"]), "configureSet": ( "setConfiguration", ["sender", "recipient", "nodeIdentifier", "options"], ), "items": ( "items", ["sender", "recipient", "nodeIdentifier", "maxItems", "itemIdentifiers"], ), "retract": ( "retract", ["sender", "recipient", "nodeIdentifier", "itemIdentifiers"], ), "purge": ("purge", ["sender", "recipient", "nodeIdentifier"]), "delete": ("delete", ["sender", "recipient", "nodeIdentifier"]), } _request_class = PubSubRequest hideNodes = False def __init__(self, resource=None): self.resource = resource self.discoIdentity = { "category": "pubsub", "type": "service", "name": "Generic Publish-Subscribe Service", } self.pubSubFeatures = [] def connectionMade(self): self.xmlstream.addObserver(PUBSUB_REQUEST, self.handleRequest) def getDiscoInfo(self, requestor, target, nodeIdentifier=""): def toInfo(nodeInfo): if not nodeInfo: return nodeType, metaData = nodeInfo["type"], nodeInfo["meta-data"] info.append(disco.DiscoIdentity("pubsub", nodeType)) if metaData: form = data_form.Form( formType="result", formNamespace=NS_PUBSUB_META_DATA ) form.addField( data_form.Field( var="pubsub#node_type", value=nodeType, label="The type of node (collection or leaf)", ) ) for metaDatum in metaData: form.addField(data_form.Field.fromDict(metaDatum)) info.append(form) return info = [] request = self._request_class("discoInfo") if self.resource is not None: resource = self.resource.locateResource(request) identity = resource.discoIdentity features = resource.features getInfo = resource.getInfo else: category = self.discoIdentity["category"] idType = self.discoIdentity["type"] name = self.discoIdentity["name"] identity = disco.DiscoIdentity(category, idType, name) features = self.pubSubFeatures getInfo = self.getNodeInfo if not nodeIdentifier: info.append(identity) info.append(disco.DiscoFeature(disco.NS_DISCO_ITEMS)) info.extend( [ disco.DiscoFeature("%s#%s" % (NS_PUBSUB, feature)) for feature in features ] ) d = defer.maybeDeferred(getInfo, requestor, target, nodeIdentifier or "") d.addCallback(toInfo) d.addErrback(log.err) d.addCallback(lambda _: info) return d def _parseNodes(self, nodes, target): """parse return values of resource.getNodes basestring values are used as node tuple are unpacked as node, name """ items = [] for node in nodes: if not node: continue elif isinstance(node, str): items.append(disco.DiscoItem(target, node)) else: _node, name = node items.append(disco.DiscoItem(target, _node, name)) return items def getDiscoItems(self, requestor, target, nodeIdentifier=""): if self.hideNodes: d = defer.succeed([]) elif self.resource is not None: request = self._request_class("discoInfo") resource = self.resource.locateResource(request) d = resource.getNodes(requestor, target, nodeIdentifier) elif nodeIdentifier: d = defer.maybeDeferred(self.getNodes, requestor, target) else: d = defer.succeed([]) d.addCallback(self._parseNodes, target) return d def _onPubSubRequest(self, iq): request = self._request_class.fromElement(iq) if self.resource is not None: resource = self.resource.locateResource(request) else: resource = self # Preprocess the request, knowing the handling resource try: preProcessor = getattr(self, "_preProcess_%s" % request.verb) except AttributeError: pass else: request = preProcessor(resource, request) if request is None: return defer.succeed(None) # Process the request itself, if resource is not self: try: handler = getattr(resource, request.verb) except AttributeError: text = "Request verb: %s" % request.verb return defer.fail(Unsupported("", text)) d = handler(request) else: try: handlerName, argNames = self._legacyHandlers[request.verb] except KeyError: text = "Request verb: %s" % request.verb return defer.fail(Unsupported("", text)) handler = getattr(self, handlerName) args = [getattr(request, arg) for arg in argNames] d = handler(*args) # If needed, translate the result into a response try: cb = getattr(self, "_toResponse_%s" % request.verb) except AttributeError: pass else: d.addCallback(cb, resource, request) return d def _toResponse_subscribe( self, result: Subscription, resource: "PubSubResource", request: PubSubRequest ) -> domish.Element: response = domish.Element((NS_PUBSUB, "pubsub")) response.addChild(result.toElement(NS_PUBSUB)) return response def _toResponse_unsubscribe( self, result: Subscription | None, resource: "PubSubResource", request: PubSubRequest, ) -> domish.Element: response = domish.Element((NS_PUBSUB, "pubsub")) if result is None: result = Subscription(request.nodeIdentifier, request.subscriber, "none") response.addChild(result.toElement(NS_PUBSUB)) return response def _toResponse_subscriptions(self, result, resource, request): response = domish.Element((NS_PUBSUB, "pubsub")) subscriptions = response.addElement("subscriptions") for subscription in result: subscriptions.addChild(subscription.toElement(NS_PUBSUB)) return response def _toResponse_affiliations(self, result, resource, request): response = domish.Element((NS_PUBSUB, "pubsub")) affiliations = response.addElement("affiliations") for nodeIdentifier, affiliation in result: item = affiliations.addElement("affiliation") item["node"] = nodeIdentifier item["affiliation"] = affiliation return response def _toResponse_create(self, result, resource, request): if not request.nodeIdentifier or request.nodeIdentifier != result: response = domish.Element((NS_PUBSUB, "pubsub")) create = response.addElement("create") create["node"] = result return response else: return None def _formFromConfiguration(self, resource, values): fieldDefs = resource.getConfigurationOptions() form = data_form.Form(formType="form", formNamespace=NS_PUBSUB_NODE_CONFIG) form.makeFields(values, fieldDefs) return form def _checkConfiguration(self, resource, form): fieldDefs = resource.getConfigurationOptions() form.typeCheck(fieldDefs, filterUnknown=True) def _preProcess_create(self, resource, request): if request.options: self._checkConfiguration(resource, request.options) return request def _preProcess_default(self, resource, request): if request.nodeType not in ("leaf", "collection"): raise error.StanzaError("not-acceptable") else: return request def _toResponse_default(self, options, resource, request): response = domish.Element((NS_PUBSUB_OWNER, "pubsub")) default = response.addElement("default") form = self._formFromConfiguration(resource, options) default.addChild(form.toElement()) return response def _toResponse_configureGet(self, options, resource, request): response = domish.Element((NS_PUBSUB_OWNER, "pubsub")) configure = response.addElement("configure") form = self._formFromConfiguration(resource, options) configure.addChild(form.toElement()) if request.nodeIdentifier: configure["node"] = request.nodeIdentifier return response def _preProcess_configureSet(self, resource, request): if request.options.formType == "cancel": return None else: self._checkConfiguration(resource, request.options) return request def _toResponse_items(self, result, resource, request): response = domish.Element((NS_PUBSUB, "pubsub")) items = response.addElement("items") items["node"] = request.nodeIdentifier for item in result: item.uri = NS_PUBSUB items.addChild(item) return response def _toResponse_subscriptionsGet(self, result, resource, request): response = domish.Element((NS_PUBSUB_OWNER, "pubsub")) subscriptions = response.addElement("subscriptions") subscriptions["node"] = request.nodeIdentifier for subscription in result: subscription_element = subscription.toElement(NS_PUBSUB) del subscription_element["node"] subscriptions.addChild(subscription_element) return response def _createNotification( self, eventType, service, nodeIdentifier, subscriber, subscriptions=None ): headers = [] if subscriptions: for subscription in subscriptions: if nodeIdentifier != subscription.nodeIdentifier: headers.append(("Collection", subscription.nodeIdentifier)) message = domish.Element((None, "message")) message["from"] = service.full() message["to"] = subscriber.full() event = message.addElement((NS_PUBSUB_EVENT, "event")) element = event.addElement(eventType) element["node"] = nodeIdentifier if headers: message.addChild(shim.Headers(headers)) return message def _toResponse_affiliationsGet(self, result, resource, request): response = domish.Element((NS_PUBSUB_OWNER, "pubsub")) affiliations = response.addElement("affiliations") if request.nodeIdentifier: affiliations["node"] = request.nodeIdentifier for entity, affiliation in result.items(): item = affiliations.addElement("affiliation") item["jid"] = entity.full() item["affiliation"] = affiliation return response # public methods def notifyPublish(self, service, nodeIdentifier, notifications): for subscriber, subscriptions, items in notifications: message = self._createNotification( "items", service, nodeIdentifier, subscriber, subscriptions ) for item in items: item.uri = NS_PUBSUB_EVENT message.event.items.addChild(item) self.send(message) def notifyRetract(self, service, nodeIdentifier, notifications): for subscriber, subscriptions, items in notifications: message = self._createNotification( "items", service, nodeIdentifier, subscriber, subscriptions ) for item in items: retract = domish.Element((NS_PUBSUB_EVENT, "retract")) retract["id"] = item["id"] message.event.items.addChild(retract) self.send(message) def notifyPurge(self, service, nodeIdentifier, subscribers): for subscriber in subscribers: message = self._createNotification( "purge", service, nodeIdentifier, subscriber ) self.send(message) def notifyDelete(self, service, nodeIdentifier, subscribers, redirectURI=None): for subscriber in subscribers: message = self._createNotification( "delete", service, nodeIdentifier, subscriber ) if redirectURI: redirect = message.event.delete.addElement("redirect") redirect["uri"] = redirectURI self.send(message) def getNodeInfo(self, requestor, service, nodeIdentifier): return None def getNodes(self, requestor, service): return [] def publish(self, requestor, service, nodeIdentifier, items, options=None): raise Unsupported("publish") def subscribe(self, requestor, service, nodeIdentifier, subscriber): raise Unsupported("subscribe") def unsubscribe(self, requestor, service, nodeIdentifier, subscriber): raise Unsupported("subscribe") def subscriptions(self, requestor, service): raise Unsupported("retrieve-subscriptions") def affiliations(self, requestor, service): raise Unsupported("retrieve-affiliations") def create(self, requestor, service, nodeIdentifier): raise Unsupported("create-nodes") def getConfigurationOptions(self): return {} def getDefaultConfiguration(self, requestor, service, nodeType): raise Unsupported("retrieve-default") def getConfiguration(self, requestor, service, nodeIdentifier): raise Unsupported("config-node") def setConfiguration(self, requestor, service, nodeIdentifier, options): raise Unsupported("config-node") def items(self, requestor, service, nodeIdentifier, maxItems, itemIdentifiers): raise Unsupported("retrieve-items") def retract(self, requestor, service, nodeIdentifier, itemIdentifiers): raise Unsupported("retract-items") def purge(self, requestor, service, nodeIdentifier): raise Unsupported("purge-nodes") def delete(self, requestor, service, nodeIdentifier): raise Unsupported("delete-nodes") @implementer(IPubSubResource) class PubSubResource: """ Base resource for publish-subscribe nodes. This class provides a base implementation for pubsub resources that handle specific nodes or collections of nodes. Subclasses should override the methods they wish to support. The resource is used by L{PubSubService} to delegate pubsub requests to appropriate handlers based on the node identifier in the request. """ features: list[str] = [] discoIdentity = disco.DiscoIdentity("pubsub", "service", "Publish-Subscribe Service") def locateResource(self, request: PubSubRequest) -> Self: """ Locate the resource that should handle the given request. This method is called by L{PubSubService} to determine which resource should handle a pubsub request. By default, it returns itself, but subclasses can override this to delegate to different resources based on the request (e.g., based on the node identifier). @param request: The publish-subscribe request to handle. @return: The resource that should handle the request. """ return self def getInfo( self, requestor: jid.JID, service: jid.JID, nodeIdentifier: str, ) -> defer.Deferred[None | dict[str, object]]: """ Get node type and meta-data for service discovery. This method is called when a disco#info request is made for a node. The returned dictionary should contain information about the node type and its meta-data. @param requestor: The entity that made the request. @param service: The pubsub service entity. @param nodeIdentifier: The identifier of the node to get info for. @return: A deferred that fires with a dictionary containing node info. The dictionary should have keys 'type' (str: 'leaf' or 'collection') and 'meta-data' (list of meta-data dicts). Return None or empty dict if no info is available. """ return defer.succeed(None) def getNodes( self, requestor: jid.JID, service: jid.JID, nodeIdentifier: str, ) -> defer.Deferred[list[str | tuple[str, str]]]: """ Get child nodes contained by this node. This method is called when a disco#items request is made for a node. It should return a list of child node identifiers. @param requestor: The entity that made the request. @param service: The pubsub service entity. @param nodeIdentifier: The identifier of the parent node. @return: A deferred that fires with a list of child node identifiers. Each element can be either a string (node identifier only) or a tuple of (node_identifier, node_name). """ return defer.succeed([]) def getConfigurationOptions(self) -> dict[str, dict[str, object]]: """ Retrieve all known node configuration options. This method returns the schema for node configuration options that can be set when creating or configuring a node. The returned dictionary maps option names to their definitions. Each option definition is a dictionary with the following keys: - 'type' (str): The field type (e.g., 'boolean', 'text-single', 'list-single', 'jid-single', etc.) - 'label' (str): A human-readable label for the option - 'options' (dict, optional): For list types, a mapping of valid values to their labels - 'default' (any, optional): The default value for this option - 'required' (bool, optional): Whether this option is required Example:: { "pubsub#persist_items": { "type": "boolean", "label": "Persist items to storage" }, "pubsub#max_items": { "type": "text-single", "label": "Maximum number of items to store" }, "pubsub#send_last_published_item": { "type": "list-single", "label": "When to send the last published item", "options": { "never": "Never", "on_sub": "On subscription" } } } @return: A dictionary of configuration option definitions. """ return {} def publish(self, request: PubSubRequest) -> defer.Deferred[None | domish.Element]: """ Handle a publish request. This method is called when a client publishes items to a node. The implementation should store the items and notify subscribers. @param request: The publish request containing node identifier, items to publish, and optional publish-options. @return: A deferred that fires with: - C{None} if the publish operation should return an empty IQ result - A C{domish.Element} containing a C{} element with the published item(s). The element should have the form:: @raises Unsupported: If publish is not supported (default behavior). """ return defer.fail(Unsupported("publish")) def subscribe(self, request: PubSubRequest) -> defer.Deferred[Subscription]: """ Handle a subscribe request. This method is called when a client wants to subscribe to a node. The implementation should create or update the subscription and return the resulting subscription state. @param request: The subscribe request containing node identifier, subscriber JID, and optional subscription options. @return: A deferred that fires with a L{Subscription} object representing the resulting subscription state. @raises Unsupported: If subscribe is not supported (default behavior). """ return defer.fail(Unsupported("subscribe")) def unsubscribe(self, request: PubSubRequest) -> defer.Deferred[None]: """ Handle an unsubscribe request. This method is called when a client wants to unsubscribe from a node. The implementation should remove the subscription. @param request: The unsubscribe request containing node identifier and subscriber JID. @return: A deferred that fires when the unsubscribe operation completes. @raises Unsupported: If unsubscribe is not supported (default behavior). """ return defer.fail(Unsupported("subscribe")) def subscriptions(self, request: PubSubRequest) -> defer.Deferred[list[Subscription]]: """ Handle a subscriptions retrieval request. This method is called when a client wants to retrieve their subscriptions. The implementation should return all subscriptions for the requesting entity. @param request: The subscriptions request containing optional node identifier filter. @return: A deferred that fires with a list of L{Subscription} objects. @raises Unsupported: If subscriptions retrieval is not supported (default behavior). """ return defer.fail(Unsupported("retrieve-subscriptions")) def affiliations( self, request: PubSubRequest ) -> defer.Deferred[list[tuple[str, str]]]: """ Handle an affiliations retrieval request. This method is called when a client wants to retrieve affiliations for entities. The implementation should return affiliations as (node_identifier, affiliation_state) tuples. Valid affiliation states are: 'owner', 'admin', 'publisher', 'subscribed', 'none', 'outcast'. @param request: The affiliations request containing optional node identifier filter. @return: A deferred that fires with a list of (node_identifier, affiliation_state) tuples. @raises Unsupported: If affiliations retrieval is not supported (default behavior). """ return defer.fail(Unsupported("retrieve-affiliations")) def create(self, request: PubSubRequest) -> defer.Deferred[str]: """ Handle a node creation request. This method is called when a client wants to create a new node. The implementation should create the node with the specified configuration and return the node identifier. @param request: The create request containing optional suggested node identifier and configuration options. @return: A deferred that fires with the identifier of the created node. This may differ from the suggested identifier if the service assigns its own identifier. @raises Unsupported: If node creation is not supported (default behavior). """ return defer.fail(Unsupported("create-nodes")) def default(self, request: PubSubRequest) -> defer.Deferred[dict[str, object]]: """ Handle a default node configuration request. This method is called when a client requests the default configuration for a new node of a specific type. @param request: The default request containing the node type ('leaf' or 'collection'). @return: A deferred that fires with a dictionary representing the default configuration. Keys are field names, values are the default values (str, int, or bool). @raises Unsupported: If default configuration retrieval is not supported (default behavior). """ return defer.fail(Unsupported("retrieve-default")) def configureGet(self, request: PubSubRequest) -> defer.Deferred[dict[str, object]]: """ Handle a node configuration retrieval request. This method is called when a client requests the current configuration of an existing node. @param request: The configure request containing the node identifier. @return: A deferred that fires with a dictionary representing the node configuration. Keys are field names, values are the current values (str, int, or bool). @raises Unsupported: If configuration retrieval is not supported (default behavior). """ return defer.fail(Unsupported("config-node")) def configureSet(self, request: PubSubRequest) -> defer.Deferred[None]: """ Handle a node configuration update request. This method is called when a client wants to update the configuration of an existing node. @param request: The configure request containing the node identifier and new configuration options in request.options. @return: A deferred that fires when the configuration update completes. @raises Unsupported: If configuration update is not supported (default behavior). """ return defer.fail(Unsupported("config-node")) def items( self, request: PubSubRequest ) -> defer.Deferred[ list[domish.Element] | tuple[list[domish.Element], "rsm.RSMResponse"] ]: """ Handle an items retrieval request. This method is called when a client wants to retrieve published items from a node. @param request: The items request containing the node identifier, optional max_items limit, optional item identifiers to retrieve, and optional subscription identifier. @return: A deferred that fires with: - A list of item elements (L{domish.Element}) for basic implementations. - A tuple of (list of item elements, L{rsm.RSMResponse}) when using RSM (Result Set Management) support via L{rsm.PubSubService}. @raises Unsupported: If items retrieval is not supported (default behavior). """ return defer.fail(Unsupported("retrieve-items")) def retract(self, request: PubSubRequest) -> defer.Deferred[None]: """ Handle an items retraction request. This method is called when a client wants to retract previously published items from a node. @param request: The retract request containing the node identifier, item identifiers to retract, and optional notify flag. @return: A deferred that fires when the retraction operation completes. @raises Unsupported: If items retraction is not supported (default behavior). """ return defer.fail(Unsupported("retract-items")) def purge(self, request: PubSubRequest) -> defer.Deferred[None]: """ Handle a node purge request. This method is called when a client wants to purge all items from a node. @param request: The purge request containing the node identifier. @return: A deferred that fires when the purge operation completes. @raises Unsupported: If node purging is not supported (default behavior). """ return defer.fail(Unsupported("purge-nodes")) def delete(self, request: PubSubRequest) -> defer.Deferred[None]: """ Handle a node deletion request. This method is called when a client wants to delete a node. The implementation should remove the node and all its data. @param request: The delete request containing the node identifier. @return: A deferred that fires when the deletion operation completes. @raises Unsupported: If node deletion is not supported (default behavior). """ return defer.fail(Unsupported("delete-nodes")) def affiliationsGet( self, request: PubSubRequest ) -> defer.Deferred[dict[jid.JID, str]]: """ Handle an affiliations retrieval request (owner interface). This method is called when an owner/admin wants to retrieve affiliations for a specific node. @param request: The affiliations request containing the node identifier. @return: A deferred that fires with a dictionary mapping entity JIDs to their affiliation states. Valid states are: 'owner', 'admin', 'publisher', 'subscribed', 'none', 'outcast'. @raises Unsupported: If affiliations retrieval is not supported (default behavior). """ return defer.fail(Unsupported("modify-affiliations")) def affiliationsSet(self, request: PubSubRequest) -> defer.Deferred[None]: """ Handle an affiliations update request (owner interface). This method is called when an owner/admin wants to update affiliations for entities in a node. @param request: The affiliations request containing the node identifier and the affiliations to set in request.affiliations (dict mapping JID to affiliation). @return: A deferred that fires when the affiliations update completes. @raises Unsupported: If affiliations update is not supported (default behavior). """ return defer.fail(Unsupported("modify-affiliations")) def subscriptionsGet( self, request: PubSubRequest ) -> defer.Deferred[list[Subscription]]: """ Handle a subscriptions retrieval request (owner interface). This method is called when an owner/admin wants to retrieve all subscriptions for a specific node. @param request: The subscriptions request containing the node identifier. @return: A deferred that fires with a list of L{Subscription} objects representing all subscriptions for the node. @raises Unsupported: If subscriptions management is not supported (default behavior). """ return defer.fail(Unsupported("manage-subscriptions")) def subscriptionsSet(self, request: PubSubRequest) -> defer.Deferred[None]: """ Handle a subscriptions update request (owner interface). This method is called when an owner/admin wants to update subscriptions for entities in a node. @param request: The subscriptions request containing the node identifier and the subscriptions to set in request.subscriptions (set of Subscription objects). @return: A deferred that fires when the subscriptions update completes. @raises Unsupported: If subscriptions management is not supported (default behavior). """ return defer.fail(Unsupported("manage-subscriptions"))