Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits: 6e946d5f by Philipp Hörist at 2019-01-28T21:16:46Z Add PubSub (XEP-0060) module
- - - - - 18edd864 by Philipp Hörist at 2019-01-28T21:18:17Z Handle pubsub events for XEP-0172
- - - - -
4 changed files:
- nbxmpp/dispatcher_nb.py - nbxmpp/modules/nickname.py - + nbxmpp/modules/pubsub.py - nbxmpp/structs.py
Changes:
===================================== nbxmpp/dispatcher_nb.py ===================================== @@ -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:
===================================== nbxmpp/modules/nickname.py ===================================== @@ -18,6 +18,8 @@ import logging
from nbxmpp.protocol import NS_NICK +from nbxmpp.protocol import NS_PUBSUB_EVENT +from nbxmpp.protocol import Node from nbxmpp.structs import StanzaHandler from nbxmpp.const import PresenceType
@@ -28,6 +30,10 @@ class Nickname: def __init__(self, client): self._client = client self.handlers = [ + StanzaHandler(name='message', + callback=self._process_pubsub_nickname, + ns=NS_PUBSUB_EVENT, + priority=16), StanzaHandler(name='message', callback=self._process_nickname, ns=NS_NICK, @@ -51,9 +57,29 @@ class Nickname: return properties.nickname = self._parse_nickname(stanza)
+ def _process_pubsub_nickname(self, _con, _stanza, properties): + if not properties.is_pubsub_event: + return + + if properties.pubsub_event.node != NS_NICK: + return + + nick = self._parse_nickname(properties.pubsub_event.item) + log.info('Received nickname: %s - %s', properties.jid, nick) + + properties.pubsub_event = properties.pubsub_event._replace(data=nick) + @staticmethod def _parse_nickname(stanza): nickname = stanza.getTag('nick', namespace=NS_NICK) if nickname is None: return return nickname.getData() or None + + def set_nickname(self, nickname): + item = Node('nick', {'xmlns': NS_NICK}) + if nickname is not None: + item.addData(nickname) + jid = self._client.get_bound_jid().getBare() + self._client.get_module('PubSub').publish( + jid, NS_NICK, item, id_='current')
===================================== nbxmpp/modules/pubsub.py ===================================== @@ -0,0 +1,75 @@ +# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com> +# +# 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 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# 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 http://www.gnu.org/licenses/. + +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 = [ + StanzaHandler(name='message', + callback=self._process_pubsub_base, + ns=NS_PUBSUB_EVENT, + priority=15), + ] + + 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') + log.warning(stanza) + node = items.getAttr('node') + item = items.getTag('item') + id_ = item.getAttr('id') + properties.pubsub_event = PubSubEventData(node, id_, item, None) + + @call_on_response('_default_response') + 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') + publish.addChild(node=options) + return query + + @callback + def _default_response(self, stanza): + if not isResultNode(stanza): + log.info('Error: %s', stanza.getError()) + return CommonResult(jid=stanza.getFrom(), + error=stanza.getError()) + return CommonResult(jid=stanza.getFrom())
===================================== nbxmpp/structs.py ===================================== @@ -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 + + @property + def is_pubsub_event(self): + return self.pubsub_event is not None
@property def is_mam_message(self):
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/compare/396c27a9462edc4fd4288b1ed7...