annotate sat_pubsub/delegation.py @ 493:33222c54d2af

remove uses of twisted.internet.defer.returnValue This function has been deprecated in Twisted 24.7.0.
author Povilas Kanapickas <povilas@radix.lt>
date Wed, 11 Dec 2024 01:19:46 +0200
parents b544109ab4c4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
1 #!/usr/bin/env python3
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #
459
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
3 # Copyright (c) 2015-2021 Jérôme Poisson
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
4
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
5
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
6 # This program is free software: you can redistribute it and/or modify
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
9 # (at your option) any later version.
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
10
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
11 # This program is distributed in the hope that it will be useful,
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
14 # GNU Affero General Public License for more details.
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
15
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
18
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
19 # ---
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
20
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 310
diff changeset
21 # 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
22
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
23 from typing import Callable, Any
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
24
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
25 from twisted.python import log
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
26 from twisted.internet import reactor, defer
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
27 from twisted.words.protocols.jabber import error, jid
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
28 from twisted.words.protocols.jabber import xmlstream
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
29 from twisted.words.xish import domish
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
30 from wokkel import pubsub
289
f08f8536cab8 mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents: 288
diff changeset
31 from wokkel import data_form
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
32 from wokkel import disco, iwokkel
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
33 from wokkel import mam
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
34 from wokkel.iwokkel import IPubSubService
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
35 from wokkel.subprotocols import XMPPHandler
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
36 from zope.interface import implementer
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
37
459
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
38 DELEGATION_NS = 'urn:xmpp:delegation:2'
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
39 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
40 DELEGATION_ADV_XPATH = '/message/delegation[@xmlns="{}"]'.format(DELEGATION_NS)
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
41 DELEGATION_FWD_XPATH = '/iq[@type="set"]/delegation[@xmlns="{}"]/forwarded[@xmlns="{}"]'.format(DELEGATION_NS, FORWARDED_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
42
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
43 DELEGATION_MAIN_SEP = "::"
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
44 DELEGATION_BARE_SEP = ":bare:"
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
45
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
46 SEND_HOOK_TIMEOUT = 300
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
47 TO_HACK = ((IPubSubService, pubsub, "PubSubRequest"),
459
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
48 (mam.IMAMService, mam, "MAMRequest"),
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
49 (None, disco, "_DiscoRequest"))
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
50
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
51
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 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
53 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
54
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
55
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
56 @implementer(iwokkel.IDisco)
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
57 class DelegationsHandler(XMPPHandler):
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
58 _service_hacked = False
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
59
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 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
61 super(DelegationsHandler, self).__init__()
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
62 self.backend = None
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
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
64 def _service_hack(self):
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
65 """Patch the request classes of services to track delegated stanzas"""
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
66 # XXX: we need to monkey patch to track origin of the stanza in PubSubRequest.
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
67 # As PubSubRequest from sat.tmp.wokkel.pubsub use _request_class while
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
68 # original wokkel.pubsub use directly pubsub.PubSubRequest, we need to
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
69 # check which version is used before monkeypatching
459
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
70 for service, module, default_base_cls in TO_HACK:
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
71 module_patched = False
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
72 for handler in self.parent.handlers:
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
73 if not service or service.providedBy(handler):
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
74 if hasattr(handler, '_request_class'):
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
75 request_base_class = handler._request_class
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
76 else:
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
77 request_base_class = getattr(module, default_base_cls)
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
78
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
79 class RequestWithDelegation(request_base_class):
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
80 """A XxxRequest which put an indicator if the stanza comme from delegation"""
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
81
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
82 @classmethod
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
83 def fromElement(cls, element):
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
84 """Check if element comme from delegation, and set a delegated flags
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
85
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
86 delegated flag is either False, or it's a jid of the delegating server
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
87 the delegated flag must be set on element before use
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
88 """
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
89 try:
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
90 # __getattr__ is overriden in domish.Element, so we use __getattribute__
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
91 delegated = element.__getattribute__('delegated')
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
92 except AttributeError:
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
93 delegated = False
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
94 instance = cls.__base__.fromElement(element)
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
95 instance.delegated = delegated
459
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
96 try:
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
97 instance.recipient.delegated = delegated
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
98 except (AttributeError, TypeError):
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
99 pass
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
100 return instance
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
101
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
102 if hasattr(handler, '_request_class'):
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
103 handler._request_class = RequestWithDelegation
459
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
104 elif not module_patched:
322
54d90c73b8b5 mam: various improvments:
Goffi <goffi@goffi.org>
parents: 312
diff changeset
105 setattr(module, default_base_cls, RequestWithDelegation)
459
cebcb7f56889 backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco:
Goffi <goffi@goffi.org>
parents: 414
diff changeset
106 module_patched = True
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
107 DelegationsHandler._service_hacked = True
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
108
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
109 def connectionInitialized(self):
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
110 self.backend = self.parent.parent.getServiceNamed('backend')
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
111 if not self._service_hacked:
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
112 self._service_hack()
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
113 self.xmlstream.addObserver(DELEGATION_ADV_XPATH, self.onAdvertise)
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
114 self.xmlstream.addObserver(DELEGATION_FWD_XPATH, self._obsWrapper, 0, self.onForward)
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
115 self._current_iqs = {} # dict of iq being handled by delegation
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
116 self.xs_send = self.xmlstream.send
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
117 self.xmlstream.send = self._sendHack
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
118
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
119 def delegatedResult(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
120 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
121 iq_req_elt: domish.Element,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
122 iq_resp_elt: domish.Element,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
123 wrapping_iq_elt: domish.Element
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
124 ) -> None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
125 """Method called when a result to a delegated stanza has been received
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
126
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
127 The result is wrapped and sent back to server
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
128 """
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
129 iq_result_elt = xmlstream.toResponse(wrapping_iq_elt, 'result')
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
130 fwd_elt = iq_result_elt.addElement(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
131 'delegation', DELEGATION_NS
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
132 ).addElement('forwarded', FORWARDED_NS)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
133 fwd_elt.addChild(iq_resp_elt)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
134 iq_resp_elt.uri = iq_resp_elt.defaultUri = 'jabber:client'
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
135 self.xs_send(iq_result_elt)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
136
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
137 def _sendHack(self, elt):
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
138 """This method is called instead of xmlstream to control sending
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
139
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
140 @param obj(domsish.Element, unicode, str): obj sent to real xmlstream
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
141 """
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
142 if isinstance(elt, domish.Element) and elt.name=='iq':
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
143 try:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
144 iq_id = elt["id"]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
145 iq_req_elt, callback, cb_args, timeout = self._current_iqs[iq_id]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
146 if elt['to'] != iq_req_elt["from"]:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
147 log.err(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
148 "IQ id conflict: the managed entity doesn't match (got "
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
149 f"{elt['to']!r} and was expecting {iq_req_elt['from']!r})"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
150 )
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
151 raise KeyError
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
152 except KeyError:
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
153 # the iq is not a delegated one
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
154 self.xs_send(elt)
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
155 else:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
156 if not timeout.called:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
157 timeout.cancel()
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
158 del self._current_iqs[iq_id]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
159 callback(iq_req_elt, elt, *cb_args)
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
160 else:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
161 self.xs_send(elt)
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
162
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
163 def _obsWrapper(self, observer, stanza):
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
164 """Wrapper to observer which catch StanzaError
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
165
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
166 @param observer(callable): method to wrap
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
167 """
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
168 try:
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
169 observer(stanza)
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
170 except error.StanzaError as e:
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
171 error_elt = e.toResponse(stanza)
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
172 self.xs_send(error_elt)
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
173 stanza.handled = True
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
174
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
175 def onAdvertise(self, message):
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
176 """Manage the <message/> advertising delegations"""
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
177 if self.backend.config["server_jid"] is None:
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
178 # if server_jid is not specified in config, we use the advertising message
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
179 # to get it (and replace the one found from this component jid)
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
180 self.backend.server_jid = self.backend.config["server_jid"] = jid.JID(
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
181 message["from"]
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
182 )
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
183 else:
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
184 if jid.JID(message["from"]) != self.backend.server_jid:
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
185 log.err(
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
186 f"Delagation advertising message received from {message['from']}, "
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
187 f"while expected server jid is {self.backend.server_jid}, this may "
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
188 "be a security threat, please check your configuration and network."
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
189 )
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
190 raise error.StanzaError("not-allowed")
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
191 delegation_elt = next(message.elements(DELEGATION_NS, 'delegation'))
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
192 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
193 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
194 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
195 if delegated_elt.name != 'delegated':
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
196 raise InvalidStanza('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
197 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
198 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
199 except KeyError:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
200 raise InvalidStanza('was expecting a "namespace" attribute in delegated element')
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
201 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
202 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
203 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
204 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
205 except KeyError:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
206 raise InvalidStanza('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
207 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
208 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
209
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
210 log.msg('delegations updated:\n{}'.format(
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
211 '\n'.join(["- namespace {}{}".format(ns,
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
212 "" if not attributes else " with filtering on {} attribute(s)".format(
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
213 ", ".join(attributes))) for ns, attributes in list(delegated.items())])))
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
214
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
215 if not pubsub.NS_PUBSUB in delegated:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
216 log.msg(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
217 "Didn't got pubsub delegation from server, can't act as a PEP service"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
218 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
219
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
220 def registerSendHook(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
221 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
222 iq_elt: domish.Element,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
223 callback: Callable[[domish.Element, domish.Element, ...], None],
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
224 *args
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
225 ) -> None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
226 """Register a methode to call when an IQ element response is received
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
227
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
228 If no result is received before SEND_HOOK_TIMEOUT seconds, the hook is deleted
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
229 @param iq_elt: source IQ element sent. Its "id" attribute will be used to track
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
230 response
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
231 @param callback: method to call when answer is seen
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
232 Will be called with:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
233 - original IQ request
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
234 - received IQ result (or error)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
235 - optional extra arguments
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
236 self.xs_send should be used to send final result
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
237 @param *args: argument to use with callback
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
238 """
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
239 iq_id = iq_elt["id"]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
240 timeout = reactor.callLater(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
241 SEND_HOOK_TIMEOUT, self._current_iqs.pop, (iq_id, None)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
242 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
243 self._current_iqs[iq_id] = (iq_elt, callback, args, timeout)
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
244
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
245 def onForward(self, iq):
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
246 """Manage forwarded iq
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
247
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
248 @param iq(domish.Element): full delegation stanza
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
249 """
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
250 if jid.JID(iq['from']) != self.backend.server_jid:
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
251 log.err("SECURITY WARNING: forwarded stanza doesn't come from our server: "
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 459
diff changeset
252 f"{iq.toXml()}")
310
e6a9a3c93314 delegation: fixed bad security check which was rejecting all delegations from external servers:
Goffi <goffi@goffi.org>
parents: 292
diff changeset
253 raise error.StanzaError('not-allowed')
e6a9a3c93314 delegation: fixed bad security check which was rejecting all delegations from external servers:
Goffi <goffi@goffi.org>
parents: 292
diff changeset
254
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
255 try:
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
256 delegation_elt = next(iq.elements(DELEGATION_NS, 'delegation'))
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
257 forwarded_elt = next(delegation_elt.elements(FORWARDED_NS, 'forwarded'))
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
258 fwd_iq = next(forwarded_elt.elements('jabber:client', 'iq'))
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
259 except StopIteration:
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
260 raise error.StanzaError('not-acceptable')
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
261
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 460
diff changeset
262 self.registerSendHook(fwd_iq, self.delegatedResult, iq)
292
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
263 fwd_iq.delegated = True
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
264
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
265 # we need a recipient in pubsub request for PEP
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
266 # so we set "to" attribute if it doesn't exist
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
267 if not fwd_iq.hasAttribute('to'):
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
268 fwd_iq["to"] = jid.JID(fwd_iq["from"]).userhost()
6918a0dad359 delegation: delegated stanza are tracked
Goffi <goffi@goffi.org>
parents: 291
diff changeset
269
291
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
270 # we now inject the element in the stream
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
271 self.xmlstream.dispatch(fwd_iq)
61fb4817b77f delegation: iq forwarded management:
Goffi <goffi@goffi.org>
parents: 289
diff changeset
272
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
273 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
274 """Manage disco nesting
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
275
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
276 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
277 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
278 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
279
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
280 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
281 """
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
282 if not nodeIdentifier.startswith(DELEGATION_NS):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
283 return []
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
284 try:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
285 _, namespace = nodeIdentifier.split(DELEGATION_MAIN_SEP, 1)
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
286 except ValueError:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
287 try:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
288 _, namespace = nodeIdentifier.split(DELEGATION_BARE_SEP, 1)
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
289 except ValueError:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
290 log.msg("Unexpected disco node: {}".format(nodeIdentifier))
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
291 raise error.StanzaError('not-acceptable')
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
292
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
293 if not namespace:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
294 log.msg("No namespace found in node {}".format(nodeIdentifier))
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
295 return []
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
296
334
0a93d71f9503 delegation: use same disco for all pubsub namespaces
Goffi <goffi@goffi.org>
parents: 322
diff changeset
297 if namespace.startswith(pubsub.NS_PUBSUB):
0a93d71f9503 delegation: use same disco for all pubsub namespaces
Goffi <goffi@goffi.org>
parents: 322
diff changeset
298 # pubsub use several namespaces starting with NS_PUBSUB (e.g. http://jabber.org/protocol/pubsub#owner)
0a93d71f9503 delegation: use same disco for all pubsub namespaces
Goffi <goffi@goffi.org>
parents: 322
diff changeset
299 # we return the same disco for all of them
0a93d71f9503 delegation: use same disco for all pubsub namespaces
Goffi <goffi@goffi.org>
parents: 322
diff changeset
300 namespace = pubsub.NS_PUBSUB
0a93d71f9503 delegation: use same disco for all pubsub namespaces
Goffi <goffi@goffi.org>
parents: 322
diff changeset
301
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
302 def gotInfos(infos):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
303 ns_features = []
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
304 for info in infos:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
305 if isinstance(info, disco.DiscoFeature) and info.startswith(namespace):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
306 ns_features.append(info)
289
f08f8536cab8 mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents: 288
diff changeset
307 elif (isinstance(info, data_form.Form) and info.formNamespace
f08f8536cab8 mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents: 288
diff changeset
308 and info.formNamespace.startwith(namespace)):
f08f8536cab8 mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents: 288
diff changeset
309 # extensions management (XEP-0128)
f08f8536cab8 mod delegation: extensions management (XEP-0128)
Goffi <goffi@goffi.org>
parents: 288
diff changeset
310 ns_features.append(info)
288
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
311
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
312 if namespace == pubsub.NS_PUBSUB:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
313 ns_features.append(disco.DiscoIdentity('pubsub', 'pep'))
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
314
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
315 return ns_features
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
316
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
317 for handler in self.parent.handlers:
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
318 if isinstance(handler, disco.DiscoHandler):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
319 break
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
320
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
321 if not isinstance(handler, disco.DiscoHandler):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
322 log.err("Can't find DiscoHandler")
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
323 return []
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
324
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
325 d = handler.info(requestor, target, '')
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
326 d.addCallback(gotInfos)
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
327 return d
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
328
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
329 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
073161f6f143 namespace delegation: disco nesting management
Goffi <goffi@goffi.org>
parents: 287
diff changeset
330 return []
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
331
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
332
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
333 # we monkeypatch DiscoHandler to add delegation informations
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
334 def _onDiscoItems(self, iq):
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
335 request = disco._DiscoRequest.fromElement(iq)
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
336 # it's really ugly to attach pep data to recipient
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
337 # but we don't have many options
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
338 request.recipient.pep = iq.delegated
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
339
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
340 def toResponse(items):
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
341 response = disco.DiscoItems()
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
342 response.nodeIdentifier = request.nodeIdentifier
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
343
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
344 for item in items:
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
345 response.append(item)
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
346
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
347 return response.toElement()
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
348
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
349 d = self.items(request.sender, request.recipient,
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
350 request.nodeIdentifier)
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
351 d.addCallback(toResponse)
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
352 return d
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
353
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
354
3bbab2173ebc implemented disco items:
Goffi <goffi@goffi.org>
parents: 334
diff changeset
355 disco.DiscoHandler._onDiscoItems = _onDiscoItems