comparison sat_pubsub/delegation.py @ 287:61f92273fb69

implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
author Goffi <goffi@goffi.org>
date Thu, 16 Apr 2015 21:06:19 +0200
parents sat_pubsub/privilege.py@2f87fa282dfd
children 073161f6f143
comparison
equal deleted inserted replaced
286:2f87fa282dfd 287:61f92273fb69
1 #!/usr/bin/python
2 #-*- coding: utf-8 -*-
3 #
4 """
5 Copyright (c) 2015 Jérôme Poisson
6
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ---
22
23 This module implements XEP-0355 (Namespace delegation) to use SàT Pubsub as PEP service
24 """
25
26 from wokkel.subprotocols import XMPPHandler
27 from wokkel import pubsub
28 from twisted.python import log
29
30 DELEGATION_NS = 'urn:xmpp:delegation:1'
31 FORWARDED_NS = 'urn:xmpp:forward:0'
32 DELEGATION_ADV_XPATH = '/message/delegation[@xmlns="{}"]'.format(DELEGATION_NS)
33
34 class InvalidStanza(Exception):
35 pass
36
37 class DelegationsHandler(XMPPHandler):
38
39 def __init__(self):
40 super(DelegationsHandler, self).__init__()
41
42 def connectionInitialized(self):
43 self.xmlstream.addObserver(DELEGATION_ADV_XPATH, self.onAdvertise)
44
45 def onAdvertise(self, message):
46 """Managage the <message/> advertising delegations"""
47 delegation_elt = message.elements(DELEGATION_NS, 'delegation').next()
48 delegated = {}
49 for delegated_elt in delegation_elt.elements(DELEGATION_NS):
50 try:
51 if delegated_elt.name != 'delegated':
52 raise InvalidStanza(u'unexpected element {}'.format(delegated_elt.name))
53 try:
54 namespace = delegated_elt['namespace']
55 except KeyError:
56 raise InvalidStanza(u'was expecting a "namespace" attribute in delegated element')
57 delegated[namespace] = []
58 for attribute_elt in delegated_elt.elements(DELEGATION_NS, 'attribute'):
59 try:
60 delegated[namespace].append(attribute_elt["name"])
61 except KeyError:
62 raise InvalidStanza(u'was expecting a "name" attribute in attribute element')
63 except InvalidStanza as e:
64 log.msg("Invalid stanza received ({})".format(e))
65
66 log.msg(u'delegations updated:\n{}'.format(
67 u'\n'.join([u" - namespace {}{}".format(ns,
68 u"" if not attributes else u" with filtering on {} attribute(s)".format(
69 u", ".join(attributes))) for ns, attributes in delegated.items()])))
70
71 if not pubsub.NS_PUBSUB in delegated:
72 log.msg(u"Didn't got pubsub delegation from server, can't act as a PEP service")
73