annotate sat_pubsub/delegation.py @ 288:073161f6f143

namespace delegation: disco nesting management
author Goffi <goffi@goffi.org>
date Fri, 17 Apr 2015 21:09:37 +0200
parents 61f92273fb69
children f08f8536cab8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
3 #
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
286
2f87fa282dfd updated old docstring (privilege is actually a new (2015) module not derivated from idavoll)
Goffi <goffi@goffi.org>
parents: 285
diff changeset
5 Copyright (c) 2015 Jérôme Poisson
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
6
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
7
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
12
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
17
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
20
286
2f87fa282dfd updated old docstring (privilege is actually a new (2015) module not derivated from idavoll)
Goffi <goffi@goffi.org>
parents: 285
diff changeset
21 ---
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
22
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
23 This module implements XEP-0355 (Namespace delegation) to use SàT Pubsub as PEP service
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
24 """
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
25
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
26 from wokkel.subprotocols import XMPPHandler
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
27 from wokkel import pubsub
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
28 from wokkel import disco, iwokkel
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
29 from twisted.python import log
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
30 from twisted.words.protocols.jabber import error
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
31 from zope.interface import implements
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
32
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
33 DELEGATION_NS = 'urn:xmpp:delegation:1'
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
34 FORWARDED_NS = 'urn:xmpp:forward:0'
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
35 DELEGATION_ADV_XPATH = '/message/delegation[@xmlns="{}"]'.format(DELEGATION_NS)
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
36
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
37 DELEGATION_MAIN_SEP = "::"
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
38 DELEGATION_BARE_SEP = ":bare:"
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
39
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
40 class InvalidStanza(Exception):
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
41 pass
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
42
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
43 class DelegationsHandler(XMPPHandler):
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
44 implements(iwokkel.IDisco)
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
45
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
46 def __init__(self):
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
47 super(DelegationsHandler, self).__init__()
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
48
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
49 def connectionInitialized(self):
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
50 self.xmlstream.addObserver(DELEGATION_ADV_XPATH, self.onAdvertise)
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
51
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
52 def onAdvertise(self, message):
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
53 """Managage the <message/> advertising delegations"""
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
54 delegation_elt = message.elements(DELEGATION_NS, 'delegation').next()
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
55 delegated = {}
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
56 for delegated_elt in delegation_elt.elements(DELEGATION_NS):
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
57 try:
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
58 if delegated_elt.name != 'delegated':
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
59 raise InvalidStanza(u'unexpected element {}'.format(delegated_elt.name))
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
60 try:
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
61 namespace = delegated_elt['namespace']
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
62 except KeyError:
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
63 raise InvalidStanza(u'was expecting a "namespace" attribute in delegated element')
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
64 delegated[namespace] = []
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
65 for attribute_elt in delegated_elt.elements(DELEGATION_NS, 'attribute'):
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
66 try:
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
67 delegated[namespace].append(attribute_elt["name"])
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
68 except KeyError:
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
69 raise InvalidStanza(u'was expecting a "name" attribute in attribute element')
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
70 except InvalidStanza as e:
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
71 log.msg("Invalid stanza received ({})".format(e))
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
72
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
73 log.msg(u'delegations updated:\n{}'.format(
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
74 u'\n'.join([u" - namespace {}{}".format(ns,
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
75 u"" if not attributes else u" with filtering on {} attribute(s)".format(
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
76 u", ".join(attributes))) for ns, attributes in delegated.items()])))
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
77
287
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
78 if not pubsub.NS_PUBSUB in delegated:
61f92273fb69 implementation of XEP-0355 (Namespace Delegation) to use SàT Pubsub as PEP service, first draft
Goffi <goffi@goffi.org>
parents: 286
diff changeset
79 log.msg(u"Didn't got pubsub delegation from server, can't act as a PEP service")
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
80
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
81 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
82 """Manage disco nesting
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
83
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
84 This method looks for DiscoHandler in sibling handlers and use it to
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
85 collect main disco infos. It then filters by delegated namespace and return it.
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
86 An identity is added for PEP if pubsub namespace is requested.
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
87
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
88 The same features/identities are returned for main and bare nodes
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
89 """
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
90 if not nodeIdentifier.startswith(DELEGATION_NS):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
91 return []
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
92
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
93 try:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
94 _, namespace = nodeIdentifier.split(DELEGATION_MAIN_SEP, 1)
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
95 except ValueError:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
96 try:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
97 _, namespace = nodeIdentifier.split(DELEGATION_BARE_SEP, 1)
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
98 except ValueError:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
99 log.msg("Unexpected disco node: {}".format(nodeIdentifier))
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
100 raise error.StanzaError('not-acceptable')
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
101
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
102 if not namespace:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
103 log.msg("No namespace found in node {}".format(nodeIdentifier))
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
104 return []
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
105
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
106 def gotInfos(infos):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
107 ns_features = []
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
108 for info in infos:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
109 if isinstance(info, disco.DiscoFeature) and info.startswith(namespace):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
110 ns_features.append(info)
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
111
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
112 if namespace == pubsub.NS_PUBSUB:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
113 ns_features.append(disco.DiscoIdentity('pubsub', 'pep'))
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
114
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
115 return ns_features
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
116
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
117 for handler in self.parent.handlers:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
118 if isinstance(handler, disco.DiscoHandler):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
119 break
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
120
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
121 if not isinstance(handler, disco.DiscoHandler):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
122 log.err("Can't find DiscoHandler")
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
123 return []
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
124
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
125 d = handler.info(requestor, target, '')
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
126 d.addCallback(gotInfos)
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
127 return d
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
128
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
129 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
130 return []