# HG changeset patch # User Goffi # Date 1429211179 -7200 # Node ID 61f92273fb6964906677e75da9bfcfb8a7d8b9af # Parent 2f87fa282dfddd2fb1142003c7991b97e1d05d24 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft diff -r 2f87fa282dfd -r 61f92273fb69 sat_pubsub/delegation.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sat_pubsub/delegation.py Thu Apr 16 21:06:19 2015 +0200 @@ -0,0 +1,73 @@ +#!/usr/bin/python +#-*- coding: utf-8 -*- +# +""" +Copyright (c) 2015 Jérôme Poisson + + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero 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 Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +--- + +This module implements XEP-0355 (Namespace delegation) to use SàT Pubsub as PEP service +""" + +from wokkel.subprotocols import XMPPHandler +from wokkel import pubsub +from twisted.python import log + +DELEGATION_NS = 'urn:xmpp:delegation:1' +FORWARDED_NS = 'urn:xmpp:forward:0' +DELEGATION_ADV_XPATH = '/message/delegation[@xmlns="{}"]'.format(DELEGATION_NS) + +class InvalidStanza(Exception): + pass + +class DelegationsHandler(XMPPHandler): + + def __init__(self): + super(DelegationsHandler, self).__init__() + + def connectionInitialized(self): + self.xmlstream.addObserver(DELEGATION_ADV_XPATH, self.onAdvertise) + + def onAdvertise(self, message): + """Managage the advertising delegations""" + delegation_elt = message.elements(DELEGATION_NS, 'delegation').next() + delegated = {} + for delegated_elt in delegation_elt.elements(DELEGATION_NS): + try: + if delegated_elt.name != 'delegated': + raise InvalidStanza(u'unexpected element {}'.format(delegated_elt.name)) + try: + namespace = delegated_elt['namespace'] + except KeyError: + raise InvalidStanza(u'was expecting a "namespace" attribute in delegated element') + delegated[namespace] = [] + for attribute_elt in delegated_elt.elements(DELEGATION_NS, 'attribute'): + try: + delegated[namespace].append(attribute_elt["name"]) + except KeyError: + raise InvalidStanza(u'was expecting a "name" attribute in attribute element') + except InvalidStanza as e: + log.msg("Invalid stanza received ({})".format(e)) + + log.msg(u'delegations updated:\n{}'.format( + u'\n'.join([u" - namespace {}{}".format(ns, + u"" if not attributes else u" with filtering on {} attribute(s)".format( + u", ".join(attributes))) for ns, attributes in delegated.items()]))) + + if not pubsub.NS_PUBSUB in delegated: + log.msg(u"Didn't got pubsub delegation from server, can't act as a PEP service") + diff -r 2f87fa282dfd -r 61f92273fb69 sat_pubsub/tap.py --- a/sat_pubsub/tap.py Mon Apr 13 17:29:18 2015 +0200 +++ b/sat_pubsub/tap.py Thu Apr 16 21:06:19 2015 +0200 @@ -65,6 +65,7 @@ from sat_pubsub import __version__, const, mam from sat_pubsub.backend import BackendService from sat_pubsub.privilege import PrivilegesHandler +from sat_pubsub.delegation import DelegationsHandler class Options(usage.Options): @@ -142,6 +143,10 @@ ph.setHandlerParent(cs) bs.privilege = ph + dh = DelegationsHandler() + dh.setHandlerParent(cs) + bs.delegation = dh + resource = IPubSubResource(bs) resource.hideNodes = config["hide-nodes"] resource.serviceJID = config["jid"]