Commit 6e946d5f authored by Philipp Hörist's avatar Philipp Hörist

Add PubSub (XEP-0060) module

parent 396c27a9
......@@ -56,6 +56,7 @@ from nbxmpp.modules.vcard_avatar import VCardAvatar
from nbxmpp.modules.captcha import Captcha
from nbxmpp.modules.entity_caps import EntityCaps
from nbxmpp.modules.blocking import Blocking
from nbxmpp.modules.pubsub import PubSub
from nbxmpp.modules.misc import unwrap_carbon
from nbxmpp.modules.misc import unwrap_mam
from nbxmpp.util import get_properties_struct
......@@ -179,6 +180,7 @@ class XMPPDispatcher(PlugIn):
self._modules['VCardAvatar'] = VCardAvatar(self._owner)
self._modules['EntityCaps'] = EntityCaps(self._owner)
self._modules['Blocking'] = Blocking(self._owner)
self._modules['PubSub'] = PubSub(self._owner)
for instance in self._modules.values():
for handler in instance.handlers:
# Copyright (C) 2018 Philipp Hörist <philipp AT>
# This file is part of nbxmpp.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <>.
import logging
from nbxmpp.protocol import NS_PUBSUB
from nbxmpp.protocol import NS_PUBSUB_EVENT
from nbxmpp.protocol import Iq
from nbxmpp.protocol import isResultNode
from nbxmpp.structs import StanzaHandler
from nbxmpp.structs import PubSubEventData
from nbxmpp.structs import CommonResult
from nbxmpp.util import call_on_response
from nbxmpp.util import callback
log = logging.getLogger('nbxmpp.m.pubsub')
class PubSub:
def __init__(self, client):
self._client = client
self.handlers = [
def _process_pubsub_base(self, _con, stanza, properties):
event = stanza.getTag('event', namespace=NS_PUBSUB_EVENT)
items = event.getTag('items')
if len(items.getChildren()) != 1:
log.warning('PubSub event with more than one item')
node = items.getAttr('node')
item = items.getTag('item')
id_ = item.getAttr('id')
properties.pubsub_event = PubSubEventData(node, id_, item, None)
def publish(self, jid, node, item, id_=None, options=None):
query = Iq('set', to=jid)
pubsub = query.addChild('pubsub', namespace=NS_PUBSUB)
publish = pubsub.addChild('publish', {'node': node})
attrs = {}
if id_ is not None:
attrs = {'id': id_}
publish.addChild('item', attrs, [item])
if options:
publish = pubsub.addChild('publish-options')
return query
def _default_response(self, stanza):
if not isResultNode(stanza):'Error: %s', stanza.getError())
return CommonResult(jid=stanza.getFrom(),
return CommonResult(jid=stanza.getFrom())
......@@ -58,6 +58,8 @@ HTTPAuthData.__new__.__defaults__ = (None, None, None, None)
StanzaIDData = namedtuple('StanzaIDData', 'id by')
StanzaIDData.__new__.__defaults__ = (None, None)
PubSubEventData = namedtuple('PubSubEventData', 'node id item data')
class MAMData(namedtuple('MAMData', 'id query_id archive namespace timestamp')):
......@@ -103,6 +105,11 @@ class MessageProperties:
self.voice_request = None
self.self_message = False
self.mam = None
self.pubsub_event = None
def is_pubsub_event(self):
return self.pubsub_event is not None
def is_mam_message(self):
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment