Mercurial > libervia-pubsub
annotate sat_pubsub/delegation.py @ 289:f08f8536cab8
mod delegation: extensions management (XEP-0128)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 18 Apr 2015 00:15:01 +0200 |
parents | 073161f6f143 |
children | 61fb4817b77f |
rev | line source |
---|---|
242 | 1 #!/usr/bin/python |
2 #-*- coding: utf-8 -*- | |
3 # | |
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 | 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 | |
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 | 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 | 24 """ |
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 |
289
f08f8536cab8
mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents:
288
diff
changeset
|
28 from wokkel import data_form |
288
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
29 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
|
30 from twisted.python import log |
288
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
31 from twisted.words.protocols.jabber import error |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
32 from zope.interface import implements |
242 | 33 |
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
|
34 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
|
35 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
|
36 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
|
37 |
288
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
38 DELEGATION_MAIN_SEP = "::" |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
39 DELEGATION_BARE_SEP = ":bare:" |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
40 |
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
|
41 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
|
42 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
|
43 |
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
|
44 class DelegationsHandler(XMPPHandler): |
288
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
45 implements(iwokkel.IDisco) |
242 | 46 |
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
|
47 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
|
48 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
|
49 |
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
|
50 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
|
51 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
|
52 |
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
|
53 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
|
54 """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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 |
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
|
74 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
|
75 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
|
76 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
|
77 u", ".join(attributes))) for ns, attributes in delegated.items()]))) |
242 | 78 |
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
|
79 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
|
80 log.msg(u"Didn't got pubsub delegation from server, can't act as a PEP service") |
242 | 81 |
288
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
82 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
83 """Manage disco nesting |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
84 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
85 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
|
86 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
|
87 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
|
88 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
89 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
|
90 """ |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
91 if not nodeIdentifier.startswith(DELEGATION_NS): |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
92 return [] |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
93 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
94 try: |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
95 _, namespace = nodeIdentifier.split(DELEGATION_MAIN_SEP, 1) |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
96 except ValueError: |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
97 try: |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
98 _, namespace = nodeIdentifier.split(DELEGATION_BARE_SEP, 1) |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
99 except ValueError: |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
100 log.msg("Unexpected disco node: {}".format(nodeIdentifier)) |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
101 raise error.StanzaError('not-acceptable') |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
102 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
103 if not namespace: |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
104 log.msg("No namespace found in node {}".format(nodeIdentifier)) |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
105 return [] |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
106 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
107 def gotInfos(infos): |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
108 ns_features = [] |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
109 for info in infos: |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
110 if isinstance(info, disco.DiscoFeature) and info.startswith(namespace): |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
111 ns_features.append(info) |
289
f08f8536cab8
mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents:
288
diff
changeset
|
112 elif (isinstance(info, data_form.Form) and info.formNamespace |
f08f8536cab8
mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents:
288
diff
changeset
|
113 and info.formNamespace.startwith(namespace)): |
f08f8536cab8
mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents:
288
diff
changeset
|
114 # extensions management (XEP-0128) |
f08f8536cab8
mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents:
288
diff
changeset
|
115 ns_features.append(info) |
288
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 if namespace == pubsub.NS_PUBSUB: |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
118 ns_features.append(disco.DiscoIdentity('pubsub', 'pep')) |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
119 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
120 return ns_features |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
121 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
122 for handler in self.parent.handlers: |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
123 if isinstance(handler, disco.DiscoHandler): |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
124 break |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
125 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
126 if not isinstance(handler, disco.DiscoHandler): |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
127 log.err("Can't find DiscoHandler") |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
128 return [] |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
129 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
130 d = handler.info(requestor, target, '') |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
131 d.addCallback(gotInfos) |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
132 return d |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
133 |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
134 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
073161f6f143
namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents:
287
diff
changeset
|
135 return [] |