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 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())
......@@ -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):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment