Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_exp_pubsub_admin.py @ 4071:4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 11:49:51 +0200 |
parents | sat/plugins/plugin_exp_pubsub_admin.py@524856bd7b19 |
children | 0d7bb4df2343 |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # SAT plugin to send pubsub requests with administrator privilege | |
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from libervia.backend.core.i18n import _ | |
21 from libervia.backend.core import exceptions | |
22 from libervia.backend.core.constants import Const as C | |
23 from libervia.backend.core.log import getLogger | |
24 from libervia.backend.tools.common import data_format | |
25 from twisted.words.protocols.jabber import jid | |
26 from wokkel import pubsub | |
27 from wokkel import generic | |
28 | |
29 log = getLogger(__name__) | |
30 | |
31 PLUGIN_INFO = { | |
32 C.PI_NAME: "Pubsub Administrator", | |
33 C.PI_IMPORT_NAME: "PUBSUB_ADMIN", | |
34 C.PI_TYPE: C.PLUG_TYPE_EXP, | |
35 C.PI_PROTOCOLS: [], | |
36 C.PI_DEPENDENCIES: [], | |
37 C.PI_RECOMMENDATIONS: [], | |
38 C.PI_MAIN: "PubsubAdmin", | |
39 C.PI_HANDLER: "no", | |
40 C.PI_DESCRIPTION: _("""\Implementation of Pubsub Administrator | |
41 This allows a pubsub administrator to overwrite completly items, including publisher. | |
42 Specially useful when importing a node."""), | |
43 } | |
44 | |
45 NS_PUBSUB_ADMIN = "https://salut-a-toi.org/spec/pubsub_admin:0" | |
46 | |
47 | |
48 class PubsubAdmin(object): | |
49 | |
50 def __init__(self, host): | |
51 self.host = host | |
52 host.bridge.add_method( | |
53 "ps_admin_items_send", | |
54 ".plugin", | |
55 in_sign="ssasss", | |
56 out_sign="as", | |
57 method=self._publish, | |
58 async_=True, | |
59 ) | |
60 | |
61 def _publish(self, service, nodeIdentifier, items, extra=None, | |
62 profile_key=C.PROF_KEY_NONE): | |
63 client = self.host.get_client(profile_key) | |
64 service = None if not service else jid.JID(service) | |
65 extra = data_format.deserialise(extra) | |
66 items = [generic.parseXml(i.encode('utf-8')) for i in items] | |
67 return self.publish( | |
68 client, service, nodeIdentifier, items, extra | |
69 ) | |
70 | |
71 def _send_cb(self, iq_result): | |
72 publish_elt = iq_result.admin.pubsub.publish | |
73 ids = [] | |
74 for item_elt in publish_elt.elements(pubsub.NS_PUBSUB, 'item'): | |
75 ids.append(item_elt['id']) | |
76 return ids | |
77 | |
78 def publish(self, client, service, nodeIdentifier, items, extra=None): | |
79 for item in items: | |
80 if item.name != 'item' or item.uri != pubsub.NS_PUBSUB: | |
81 raise exceptions.DataError( | |
82 'Invalid element, a pubsub item is expected: {xml}'.format( | |
83 xml=item.toXml())) | |
84 iq_elt = client.IQ() | |
85 iq_elt['to'] = service.full() if service else client.jid.userhost() | |
86 admin_elt = iq_elt.addElement((NS_PUBSUB_ADMIN, 'admin')) | |
87 pubsub_elt = admin_elt.addElement((pubsub.NS_PUBSUB, 'pubsub')) | |
88 publish_elt = pubsub_elt.addElement('publish') | |
89 publish_elt['node'] = nodeIdentifier | |
90 for item in items: | |
91 publish_elt.addChild(item) | |
92 d = iq_elt.send() | |
93 d.addCallback(self._send_cb) | |
94 return d |