annotate libervia/backend/plugins/plugin_exp_pubsub_hook.py @ 4272:89a0999884ac default tip @

cli (list/set): add "--comments" argument.
author Goffi <goffi@goffi.org>
date Thu, 20 Jun 2024 14:46:55 +0200
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Pubsub Hooks
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
20 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
21 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
24 from libervia.backend.memory import persistent
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import jid
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.internet import defer
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
27
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 log = getLogger(__name__)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
30 NS_PUBSUB_HOOK = "PUBSUB_HOOK"
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 PLUGIN_INFO = {
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_NAME: "PubSub Hook",
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_IMPORT_NAME: NS_PUBSUB_HOOK,
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_TYPE: "EXP",
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_PROTOCOLS: [],
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_DEPENDENCIES: ["XEP-0060"],
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_MAIN: "PubsubHook",
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_HANDLER: "no",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 C.PI_DESCRIPTION: _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 """Experimental plugin to launch on action on Pubsub notifications"""
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42 ),
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 }
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 #  python module
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
46 HOOK_TYPE_PYTHON = "python"
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 # python file path
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
48 HOOK_TYPE_PYTHON_FILE = "python_file"
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 # python code directly
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
50 HOOK_TYPE_PYTHON_CODE = "python_code"
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 HOOK_TYPES = (HOOK_TYPE_PYTHON, HOOK_TYPE_PYTHON_FILE, HOOK_TYPE_PYTHON_CODE)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 class PubsubHook(object):
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
56 log.info(_("PubSub Hook initialization"))
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.host = host
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
58 self.node_hooks = (
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
59 {}
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
60 ) # keep track of the number of hooks per node (for all profiles)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
61 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
62 "ps_hook_add", ".plugin", in_sign="ssssbs", out_sign="", method=self._addHook
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
64 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
65 "ps_hook_remove",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 in_sign="sssss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 out_sign="i",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 method=self._removeHook,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
71 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
72 "ps_hook_list",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
73 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 in_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 out_sign="aa{ss}",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
76 method=self._list_hooks,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
77 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @defer.inlineCallbacks
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
80 def profile_connected(self, client):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
81 hooks = client._hooks = persistent.PersistentBinaryDict(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 NS_PUBSUB_HOOK, client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
83 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 client._hooks_temporary = {}
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 yield hooks.load()
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 for node in hooks:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
87 self._install_node_manager(client, node)
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
89 def profile_disconnected(self, client):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 for node in client._hooks:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
91 self._remove_node_manager(client, node)
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
93 def _install_node_manager(self, client, node):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 if node in self.node_hooks:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
95 log.debug(_("node manager already set for {node}").format(node=node))
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 self.node_hooks[node] += 1
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 else:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 # first hook on this node
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
99 self.host.plugins["XEP-0060"].add_managed_node(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
100 node, items_cb=self._items_received
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
101 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 self.node_hooks[node] = 0
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
103 log.info(_("node manager installed on {node}").format(node=node))
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
105 def _remove_node_manager(self, client, node):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 try:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self.node_hooks[node] -= 1
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
109 log.error(_("trying to remove a {node} without hook").format(node=node))
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 else:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 if self.node_hooks[node] == 0:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 del self.node_hooks[node]
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
113 self.host.plugins["XEP-0060"].remove_managed_node(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
114 node, self._items_received
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
115 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
116 log.debug(_("hook removed"))
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
118 log.debug(_("node still needed for an other hook"))
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
120 def install_hook(self, client, service, node, hook_type, hook_arg, persistent):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 if hook_type not in HOOK_TYPES:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
122 raise exceptions.DataError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
123 _("{hook_type} is not handled").format(hook_type=hook_type)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
124 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 if hook_type != HOOK_TYPE_PYTHON_FILE:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
126 raise NotImplementedError(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
127 _("{hook_type} hook type not implemented yet").format(hook_type=hook_type)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
128 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
129 self._install_node_manager(client, node)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 hook_data = {"service": service, "type": hook_type, "arg": hook_arg}
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 if persistent:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 hooks_list = client._hooks.setdefault(node, [])
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 hooks_list.append(hook_data)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 client._hooks.force(node)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 hooks_list = client._hooks_temporary.setdefault(node, [])
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 hooks_list.append(hook_data)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140 log.info(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
141 _("{persistent} hook installed on {node} for {profile}").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
142 persistent=_("persistent") if persistent else _("temporary"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 node=node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 profile=client.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
145 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
148 def _items_received(self, client, itemsEvent):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 node = itemsEvent.nodeIdentifier
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 for hooks in (client._hooks, client._hooks_temporary):
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 if node not in hooks:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 continue
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 hooks_list = hooks[node]
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 for hook_data in hooks_list[:]:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
155 if hook_data["service"] != itemsEvent.sender.userhostJID():
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 continue
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
158 callback = hook_data["callback"]
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 except KeyError:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 # first time we get this hook, we create the callback
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
161 hook_type = hook_data["type"]
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 try:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 if hook_type == HOOK_TYPE_PYTHON_FILE:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 hook_globals = {}
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
165 exec(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
166 compile(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
167 open(hook_data["arg"], "rb").read(),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
168 hook_data["arg"],
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
169 "exec",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
170 ),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
171 hook_globals,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
172 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 callback = hook_globals["hook"]
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 raise NotImplementedError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
176 _("{hook_type} hook type not implemented yet").format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
177 hook_type=hook_type
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
178 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
179 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 except Exception as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
181 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
182 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
183 "Can't load Pubsub hook at node {node}, it will be removed: {reason}"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
184 ).format(node=node, reason=e)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
185 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 hooks_list.remove(hook_data)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 continue
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 for item in itemsEvent.items:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 try:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 callback(self.host, client, item)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 except Exception as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
193 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
194 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
195 "Error while running Pubsub hook for node {node}: {msg}"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 ).format(node=node, msg=e)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
197 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 def _addHook(self, service, node, hook_type, hook_arg, persistent, profile):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
200 client = self.host.get_client(profile)
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 service = jid.JID(service) if service else client.jid.userhostJID()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
202 return self.add_hook(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
203 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
204 service,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
205 str(node),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
206 str(hook_type),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
207 str(hook_arg),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
208 persistent,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
209 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
211 def add_hook(self, client, service, node, hook_type, hook_arg, persistent):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 r"""Add a hook which will be triggered on a pubsub notification
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
213
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 @param service(jid.JID): service of the node
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 @param node(unicode): Pubsub node
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 @param hook_type(unicode): type of the hook, one of:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 - HOOK_TYPE_PYTHON: a python module (must be in path)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 module must have a "hook" method which will be called
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 - HOOK_TYPE_PYTHON_FILE: a python file
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 file must have a "hook" method which will be called
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 - HOOK_TYPE_PYTHON_CODE: direct python code
2444
30278ea1ca7c plugin XEP-0060: added node watching methods to bridge:
Goffi <goffi@goffi.org>
parents: 2307
diff changeset
222 /!\ Python hooks will be executed in SàT context,
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 with host, client and item as arguments, it means that:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 - they can do whatever they wants, so don't run untrusted hooks
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 - they MUST NOT BLOCK, they are run in Twisted async environment and blocking would block whole SàT process
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 - item are domish.Element
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 @param hook_arg(unicode): argument of the hook, depending on the hook_type
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 can be a module path, file path, python code
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 """
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 assert service is not None
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
231 return self.install_hook(client, service, node, hook_type, hook_arg, persistent)
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 def _removeHook(self, service, node, hook_type, hook_arg, profile):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
234 client = self.host.get_client(profile)
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 service = jid.JID(service) if service else client.jid.userhostJID()
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
236 return self.remove_hook(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
237 client, service, node, hook_type or None, hook_arg or None
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
238 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
240 def remove_hook(self, client, service, node, hook_type=None, hook_arg=None):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 """Remove a persistent or temporaty root
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
242
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 @param service(jid.JID): service of the node
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 @param node(unicode): Pubsub node
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
245 @param hook_type(unicode, None): same as for [add_hook]
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 match all if None
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
247 @param hook_arg(unicode, None): same as for [add_hook]
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 match all if None
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 @return(int): number of hooks removed
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 """
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 removed = 0
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 for hooks in (client._hooks, client._hooks_temporary):
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 if node in hooks:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 for hook_data in hooks[node]:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
255 if (
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
256 service != hook_data["service"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
257 or hook_type is not None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
258 and hook_type != hook_data["type"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
259 or hook_arg is not None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
260 and hook_arg != hook_data["arg"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
261 ):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 continue
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
263 hooks[node].remove(hook_data)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 removed += 1
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 if not hooks[node]:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
266 #  no more hooks, we can remove the node
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 del hooks[node]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
268 self._remove_node_manager(client, node)
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 else:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 if hooks == client._hooks:
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 hooks.force(node)
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 return removed
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
273
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
274 def _list_hooks(self, profile):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
275 hooks_list = self.list_hooks(self.host.get_client(profile))
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 for hook in hooks_list:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
277 hook["service"] = hook["service"].full()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
278 hook["persistent"] = C.bool_const(hook["persistent"])
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 return hooks_list
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
280
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
281 def list_hooks(self, client):
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 """return list of registered hooks"""
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 hooks_list = []
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 for hooks in (client._hooks, client._hooks_temporary):
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 persistent = hooks is client._hooks
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
286 for node, hooks_data in hooks.items():
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 for hook_data in hooks_data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
288 hooks_list.append(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
289 {
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
290 "service": hook_data["service"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
291 "node": node,
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
292 "type": hook_data["type"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
293 "arg": hook_data["arg"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
294 "persistent": persistent,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
295 }
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
296 )
2307
8fa7edd0da24 plugin Pubsub Hook: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 return hooks_list