Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_exp_command_export.py @ 4332:71c939e34ca6
XEP-0373 (OX): Adjust to gpgme updates: generate with explicit algorithm and subkeys
author | Syndace <me@syndace.dev> |
---|---|
date | Sat, 13 Jul 2024 18:28:28 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
604 | 3 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
4 # SAT plugin to export commands (experimental) |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
604 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
10 # (at your option) any later version. |
604 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
15 # GNU Affero General Public License for more details. |
604 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
604
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
604 | 19 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
20 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
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:
4051
diff
changeset
|
22 from libervia.backend.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
23 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
24 log = getLogger(__name__) |
604 | 25 from twisted.words.protocols.jabber import jid |
26 from twisted.internet import reactor, protocol | |
27 | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
28 from libervia.backend.tools import trigger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4051
diff
changeset
|
29 from libervia.backend.tools.utils import clean_ustr |
604 | 30 |
31 PLUGIN_INFO = { | |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
32 C.PI_NAME: "Command export plugin", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
33 C.PI_IMPORT_NAME: "EXP-COMMANS-EXPORT", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
34 C.PI_TYPE: "EXP", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
35 C.PI_PROTOCOLS: [], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
36 C.PI_DEPENDENCIES: [], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
37 C.PI_MAIN: "CommandExport", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
38 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 C.PI_DESCRIPTION: _("""Implementation of command export"""), |
604 | 40 } |
41 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
42 |
604 | 43 class ExportCommandProtocol(protocol.ProcessProtocol): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
44 """Try to register an account with prosody""" |
604 | 45 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
46 def __init__(self, parent, client, target, options): |
604 | 47 self.parent = parent |
48 self.target = target | |
49 self.options = options | |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
50 self.client = client |
604 | 51 |
52 def _clean(self, data): | |
53 if not data: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 log.error("data should not be empty !") |
3028 | 55 return "" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 decoded = data.decode("utf-8", "ignore")[: -1 if data[-1] == "\n" else None] |
604 | 57 return clean_ustr(decoded) |
58 | |
59 def connectionMade(self): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
60 log.info("connectionMade :)") |
604 | 61 |
62 def outReceived(self, data): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 self.client.sendMessage(self.target, {"": self._clean(data)}, no_trigger=True) |
604 | 64 |
65 def errReceived(self, data): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
66 self.client.sendMessage(self.target, {"": self._clean(data)}, no_trigger=True) |
604 | 67 |
68 def processEnded(self, reason): | |
3028 | 69 log.info("process finished: %d" % (reason.value.exitCode,)) |
604 | 70 self.parent.removeProcess(self.target, self) |
71 | |
72 def write(self, message): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 self.transport.write(message.encode("utf-8")) |
604 | 74 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
75 def bool_option(self, key): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
76 """Get boolean value from options |
604 | 77 @param key: name of the option |
78 @return: True if key exists and set to "true" (case insensitive), | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
79 False in all other cases""" |
604 | 80 value = self.options.get(key, "") |
81 return value.lower() == "true" | |
82 | |
83 | |
84 class CommandExport(object): | |
85 """Command export plugin: export a command to an entity""" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
86 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
87 # XXX: This plugin can be potentially dangerous if we don't trust entities linked |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
88 # this is specially true if we have other triggers. |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
89 # FIXME: spawned should be a client attribute, not a class one |
604 | 90 |
91 def __init__(self, host): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
811
diff
changeset
|
92 log.info(_("Plugin command export initialization")) |
604 | 93 self.host = host |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 self.spawned = {} # key = entity |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
95 host.trigger.add( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
96 "message_received", self.message_received_trigger, priority=10000 |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
97 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
98 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
99 "command_export", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 in_sign="sasasa{ss}s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 out_sign="", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
103 method=self._export_command, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
104 ) |
604 | 105 |
106 def removeProcess(self, entity, process): | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
107 """Called when the process is finished |
604 | 108 @param entity: jid.JID attached to the process |
109 @param process: process to remove""" | |
110 try: | |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
111 processes_set = self.spawned[(entity, process.client.profile)] |
604 | 112 processes_set.discard(process) |
113 if not processes_set: | |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
114 del self.spawned[(entity, process.client.profile)] |
604 | 115 except ValueError: |
116 pass | |
117 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
118 def message_received_trigger(self, client, message_elt, post_treat): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
119 """Check if source is linked and repeat message, else do nothing""" |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
120 from_jid = jid.JID(message_elt["from"]) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
121 spawned_key = (from_jid.userhostJID(), client.profile) |
604 | 122 |
123 if spawned_key in self.spawned: | |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
124 try: |
3028 | 125 body = next(message_elt.elements(C.NS_CLIENT, "body")) |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
126 except StopIteration: |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
127 # do not block message without body (chat state notification...) |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
128 return True |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
129 |
3028 | 130 mess_data = str(body) + "\n" |
604 | 131 processes_set = self.spawned[spawned_key] |
132 _continue = False | |
133 exclusive = False | |
134 for process in processes_set: | |
135 process.write(mess_data) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
136 _continue &= process.bool_option("continue") |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
137 exclusive |= process.bool_option("exclusive") |
604 | 138 if exclusive: |
1374
0befb14ecf62
renamed tools.misc to tools.trigger
Goffi <goffi@goffi.org>
parents:
993
diff
changeset
|
139 raise trigger.SkipOtherTriggers |
604 | 140 return _continue |
141 | |
142 return True | |
143 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
144 def _export_command(self, command, args, targets, options, profile_key): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
145 """Export a commands to authorised targets |
604 | 146 @param command: full path of the command to execute |
147 @param args: list of arguments, with command name as first one | |
148 @param targets: list of allowed entities | |
149 @param options: export options, a dict which can have the following keys ("true" to set booleans): | |
150 - exclusive: if set, skip all other triggers | |
151 - loop: if set, restart the command once terminated #TODO | |
152 - pty: if set, launch in a pseudo terminal | |
4051
c23cad65ae99
core: renamed `messageReceived` trigger to `message_received`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
153 - continue: continue normal message_received handling |
604 | 154 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
155 client = self.host.get_client(profile_key) |
604 | 156 for target in targets: |
157 try: | |
158 _jid = jid.JID(target) | |
159 if not _jid.user or not _jid.host: | |
160 raise jid.InvalidFormat | |
161 _jid = _jid.userhostJID() | |
1742
244a605623d6
complete the Exception's list when catching JID error:
souliane <souliane@mailoo.org>
parents:
1654
diff
changeset
|
162 except (RuntimeError, jid.InvalidFormat, AttributeError): |
3028 | 163 log.info("invalid target ignored: %s" % (target,)) |
604 | 164 continue |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
165 process_prot = ExportCommandProtocol(self, client, _jid, options) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
166 self.spawned.setdefault((_jid, client.profile), set()).add(process_prot) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
167 reactor.spawnProcess( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
168 process_prot, command, args, usePTY=process_prot.bool_option("pty") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 ) |