Mercurial > libervia-backend
annotate sat/plugins/plugin_adhoc_dbus.py @ 2646:712cb4ff3e13
core: new EncryptionHandler class which manage encrypted session as a core feature:
Plugin handling encryption can now register using host.registerEncryptionPlugin, and an encryption session can now be started using messageEncryptionStart bridge method.
This will make encryption handling more easy, as we now know if a session is clear or e2e encrypted, and which plugin handle it.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 29 Jul 2018 19:22:56 +0200 |
parents | 56f94936df1e |
children | 8dd9db785ac8 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1933
diff
changeset
|
1 #!/usr/bin/env python2 |
822 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SAT plugin for adding D-Bus to Ad-Hoc Commands | |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
822 | 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 | |
1540
9a4e95c62380
plugin ad-hoc D-Bus: exception fixe + minor fixes
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
20 from sat.core.i18n import _ |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
822
diff
changeset
|
21 from sat.core.constants import Const as C |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
22 from sat.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:
916
diff
changeset
|
24 log = getLogger(__name__) |
1540
9a4e95c62380
plugin ad-hoc D-Bus: exception fixe + minor fixes
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
25 from sat.core import exceptions |
9a4e95c62380
plugin ad-hoc D-Bus: exception fixe + minor fixes
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
26 from twisted.internet import defer |
822 | 27 from wokkel import data_form |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
28 |
1542
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
29 try: |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
30 from lxml import etree |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
31 except ImportError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
32 raise exceptions.MissingModule( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
33 u"Missing module lxml, please download/install it from http://lxml.de/" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
34 ) |
1542
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
35 import os.path |
822 | 36 import uuid |
37 import dbus | |
38 from dbus.mainloop.glib import DBusGMainLoop | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
39 |
822 | 40 DBusGMainLoop(set_as_default=True) |
41 | |
42 FD_NAME = "org.freedesktop.DBus" | |
43 FD_PATH = "/org/freedekstop/DBus" | |
44 INTROSPECT_IFACE = "org.freedesktop.DBus.Introspectable" | |
45 | |
46 INTROSPECT_METHOD = "Introspect" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
47 IGNORED_IFACES_START = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
48 "org.freedesktop", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
49 "org.qtproject", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 "org.kde.KMainWindow", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 ) # commands in interface starting with these values will be ignored |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 FLAG_LOOP = "LOOP" |
822 | 53 |
54 PLUGIN_INFO = { | |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
55 C.PI_NAME: "Ad-Hoc Commands - D-Bus", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
56 C.PI_IMPORT_NAME: "AD_HOC_DBUS", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
57 C.PI_TYPE: "Misc", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
58 C.PI_PROTOCOLS: [], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
59 C.PI_DEPENDENCIES: ["XEP-0050"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
60 C.PI_MAIN: "AdHocDBus", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
61 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 C.PI_DESCRIPTION: _("""Add D-Bus management to Ad-Hoc commands"""), |
822 | 63 } |
64 | |
65 | |
66 class AdHocDBus(object): | |
67 def __init__(self, host): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
68 log.info(_("plugin Ad-Hoc D-Bus initialization")) |
822 | 69 self.host = host |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
70 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
71 "adHocDBusAddAuto", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 in_sign="sasasasasasass", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 out_sign="(sa(sss))", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 method=self._adHocDBusAddAuto, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 async=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 ) |
822 | 78 self.session_bus = dbus.SessionBus() |
79 self.fd_object = self.session_bus.get_object(FD_NAME, FD_PATH, introspect=False) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 self.XEP_0050 = host.plugins["XEP-0050"] |
822 | 81 |
82 def _DBusAsyncCall(self, proxy, method, *args, **kwargs): | |
83 """ Call a DBus method asynchronously and return a deferred | |
84 @param proxy: DBus object proxy, as returner by get_object | |
85 @param method: name of the method to call | |
86 @param args: will be transmitted to the method | |
87 @param kwargs: will be transmetted to the method, except for the following poped values: | |
88 - interface: name of the interface to use | |
89 @return: a deferred | |
90 | |
91 """ | |
92 d = defer.Deferred() | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
93 interface = kwargs.pop("interface", None) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
94 kwargs["reply_handler"] = lambda ret=None: d.callback(ret) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
95 kwargs["error_handler"] = d.errback |
822 | 96 proxy.get_dbus_method(method, dbus_interface=interface)(*args, **kwargs) |
97 return d | |
98 | |
99 def _DBusListNames(self): | |
100 return self._DBusAsyncCall(self.fd_object, "ListNames") | |
101 | |
102 def _DBusIntrospect(self, proxy): | |
103 return self._DBusAsyncCall(proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE) | |
104 | |
105 def _acceptMethod(self, method): | |
106 """ Return True if we accept the method for a command | |
107 @param method: etree.Element | |
108 @return: True if the method is acceptable | |
109 | |
110 """ | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
111 if method.xpath( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
112 "arg[@direction='in']" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
113 ): # we don't accept method with argument for the moment |
822 | 114 return False |
115 return True | |
116 | |
117 @defer.inlineCallbacks | |
118 def _introspect(self, methods, bus_name, proxy): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
119 log.debug("introspecting path [%s]" % proxy.object_path) |
822 | 120 introspect_xml = yield self._DBusIntrospect(proxy) |
121 el = etree.fromstring(introspect_xml) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
122 for node in el.iterchildren("node", "interface"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
123 if node.tag == "node": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
124 new_path = os.path.join(proxy.object_path, node.get("name")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
125 new_proxy = self.session_bus.get_object( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
126 bus_name, new_path, introspect=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
127 ) |
822 | 128 yield self._introspect(methods, bus_name, new_proxy) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
129 elif node.tag == "interface": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
130 name = node.get("name") |
822 | 131 if any(name.startswith(ignored) for ignored in IGNORED_IFACES_START): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
132 log.debug("interface [%s] is ignored" % name) |
822 | 133 continue |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
134 log.debug("introspecting interface [%s]" % name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
135 for method in node.iterchildren("method"): |
822 | 136 if self._acceptMethod(method): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
137 method_name = method.get("name") |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
138 log.debug("method accepted: [%s]" % method_name) |
822 | 139 methods.add((proxy.object_path, name, method_name)) |
140 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
141 def _adHocDBusAddAuto( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
142 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
143 prog_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
144 allowed_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 allowed_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 allowed_magics, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 forbidden_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 forbidden_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 flags, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
150 profile_key, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
151 ): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
152 return self.adHocDBusAddAuto( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
153 prog_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
154 allowed_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
155 allowed_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
156 allowed_magics, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
157 forbidden_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
158 forbidden_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
159 flags, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
160 profile_key, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
161 ) |
822 | 162 |
163 @defer.inlineCallbacks | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
164 def adHocDBusAddAuto( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
165 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
166 prog_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
167 allowed_jids=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
168 allowed_groups=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 allowed_magics=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 forbidden_jids=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
171 forbidden_groups=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
172 flags=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
173 profile_key=C.PROF_KEY_NONE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
174 ): |
822 | 175 bus_names = yield self._DBusListNames() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
176 bus_names = [bus_name for bus_name in bus_names if "." + prog_name in bus_name] |
822 | 177 if not bus_names: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
178 log.info("Can't find any bus for [%s]" % prog_name) |
1933
16e65f15f31f
plugin adhoc_dbus, jp (cmd/adhoc): fixes adHocDBusAddAuto when no bus is found
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
179 defer.returnValue(("", [])) |
822 | 180 bus_names.sort() |
181 for bus_name in bus_names: | |
182 if bus_name.endswith(prog_name): | |
183 break | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
184 log.info("bus name found: [%s]" % bus_name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
185 proxy = self.session_bus.get_object(bus_name, "/", introspect=False) |
822 | 186 methods = set() |
187 | |
188 yield self._introspect(methods, bus_name, proxy) | |
189 | |
190 if methods: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
191 self._addCommand( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
192 prog_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
193 bus_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
194 methods, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
195 allowed_jids=allowed_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
196 allowed_groups=allowed_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
197 allowed_magics=allowed_magics, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
198 forbidden_jids=forbidden_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
199 forbidden_groups=forbidden_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
200 flags=flags, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
201 profile_key=profile_key, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
202 ) |
822 | 203 |
204 defer.returnValue((bus_name, methods)) | |
205 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
206 def _addCommand( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
207 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
208 adhoc_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
209 bus_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
210 methods, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
211 allowed_jids=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
212 allowed_groups=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
213 allowed_magics=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
214 forbidden_jids=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
215 forbidden_groups=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
216 flags=None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
217 profile_key=C.PROF_KEY_NONE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
218 ): |
822 | 219 if flags is None: |
220 flags = set() | |
221 | |
222 def DBusCallback(command_elt, session_data, action, node, profile): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
223 actions = session_data.setdefault("actions", []) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
224 names_map = session_data.setdefault("names_map", {}) |
822 | 225 actions.append(action) |
226 | |
227 if len(actions) == 1: | |
228 # it's our first request, we ask the desired new status | |
229 status = self.XEP_0050.STATUS.EXECUTING | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
230 form = data_form.Form("form", title=_("Command selection")) |
822 | 231 options = [] |
232 for path, iface, command in methods: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
233 label = command.rsplit(".", 1)[-1] |
822 | 234 name = str(uuid.uuid4()) |
235 names_map[name] = (path, iface, command) | |
236 options.append(data_form.Option(name, label)) | |
237 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
238 field = data_form.Field( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
239 "list-single", "command", options=options, required=True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
240 ) |
822 | 241 form.addField(field) |
242 | |
243 payload = form.toElement() | |
244 note = None | |
245 | |
246 elif len(actions) == 2: | |
247 # we should have the answer here | |
248 try: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
249 x_elt = command_elt.elements(data_form.NS_X_DATA, "x").next() |
822 | 250 answer_form = data_form.Form.fromElement(x_elt) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
251 command = answer_form["command"] |
1540
9a4e95c62380
plugin ad-hoc D-Bus: exception fixe + minor fixes
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
252 except (KeyError, StopIteration): |
822 | 253 raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.BAD_PAYLOAD) |
254 | |
255 if command not in names_map: | |
256 raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.BAD_PAYLOAD) | |
257 | |
258 path, iface, command = names_map[command] | |
259 proxy = self.session_bus.get_object(bus_name, path) | |
260 | |
1540
9a4e95c62380
plugin ad-hoc D-Bus: exception fixe + minor fixes
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
261 self._DBusAsyncCall(proxy, command, interface=iface) |
822 | 262 |
263 # job done, we can end the session, except if we have FLAG_LOOP | |
264 if FLAG_LOOP in flags: | |
265 # We have a loop, so we clear everything and we execute again the command as we had a first call (command_elt is not used, so None is OK) | |
266 del actions[:] | |
267 names_map.clear() | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
268 return DBusCallback( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
269 None, session_data, self.XEP_0050.ACTION.EXECUTE, node, profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
270 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
271 form = data_form.Form("form", title=_(u"Updated")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
272 form.addField(data_form.Field("fixed", u"Command sent")) |
822 | 273 status = self.XEP_0050.STATUS.COMPLETED |
274 payload = None | |
275 note = (self.XEP_0050.NOTE.INFO, _(u"Command sent")) | |
276 else: | |
277 raise self.XEP_0050.AdHocError(self.XEP_0050.ERROR.INTERNAL) | |
278 | |
279 return (payload, status, None, note) | |
280 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
281 self.XEP_0050.addAdHocCommand( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
282 DBusCallback, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
283 adhoc_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
284 allowed_jids=allowed_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
285 allowed_groups=allowed_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
286 allowed_magics=allowed_magics, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
287 forbidden_jids=forbidden_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
288 forbidden_groups=forbidden_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
289 profile_key=profile_key, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
290 ) |