From d78b7df68b452bd81af153e285224fc568962af4 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Sun, 4 May 2014 20:44:10 +0200 Subject: Implement the execution of ad-hoc commands (#1832) --- src/core/core.py | 5 ++++ src/core/handlers.py | 53 ++++++++++++++++++++++++++++++++++++++++- src/tabs/adhoc_commands_list.py | 12 +++++----- src/tabs/data_forms.py | 4 ++-- 4 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/core/core.py b/src/core/core.py index d53782b5..88d8ea80 100644 --- a/src/core/core.py +++ b/src/core/core.py @@ -1891,6 +1891,11 @@ class Core(object): outgoing_stanza = handlers.outgoing_stanza incoming_stanza = handlers.incoming_stanza validate_ssl = handlers.validate_ssl + on_next_adhoc_step = handlers.on_next_adhoc_step + on_adhoc_error = handlers.on_adhoc_error + cancel_adhoc_command = handlers.cancel_adhoc_command + validate_adhoc_step = handlers.validate_adhoc_step + terminate_adhoc_command = handlers.terminate_adhoc_command command_help = commands.command_help command_runkey = commands.command_runkey command_status = commands.command_status diff --git a/src/core/handlers.py b/src/core/handlers.py index 45a4ff2a..525613aa 100644 --- a/src/core/handlers.py +++ b/src/core/handlers.py @@ -1098,7 +1098,6 @@ def validate_ssl(self, pem): if not config.silent_set('certificate', found_cert): self.information(_('Unable to write in the config file'), 'Error') - def _composing_tab_state(tab, state): """ Set a tab state to or from the "composing" state @@ -1125,4 +1124,56 @@ def _composing_tab_state(tab, state): elif tab.state == 'composing' and state != 'composing': tab.restore_state() +### Ad-hoc commands +def on_next_adhoc_step(self, iq, adhoc_session): + status = iq['command']['status'] + xform = iq.xml.find('{http://jabber.org/protocol/commands}command/{jabber:x:data}x') + if xform is not None: + form = self.xmpp.plugin['xep_0004'].buildForm(xform) + else: + form = None + + if status == 'error': + return self.information("An error occured while executing the command") + + if status == 'executing': + if not form: + self.information("Adhoc command step does not contain a data-form. Aborting the execution.", "Error") + return self.xmpp.plugin['xep_0050'].cancel_command(adhoc_session) + on_validate = self.validate_adhoc_step + on_cancel = self.cancel_adhoc_command + if status == 'completed': + on_validate = lambda form, session: self.close_tab() + on_cancel = lambda form, session: self.close_tab() + + # If a form is available, use it, and add the Notes from the + # response to it, if any + if form: + for note in iq['command']['notes']: + form.add_field(type='fixed', label=note[1]) + self.open_new_form(form, on_cancel, on_validate, + session=adhoc_session) + else: # otherwise, just display an information + # message + notes = '\n'.join([note[1] for note in iq['command']['notes']]) + self.information("Adhoc command %s: %s" % (status, notes), "Info") + +def on_adhoc_error(self, iq, adhoc_session): + self.xmpp.plugin['xep_0050'].terminate_command(session) + error_message = self.get_error_message(iq) + self.information("An error occured while executing the command: %s" % (error_message), + 'Error') + +def cancel_adhoc_command(self, form, session): + self.xmpp.plugin['xep_0050'].cancel_command(session) + self.close_tab() + +def validate_adhoc_step(self, form, session): + session['payload'] = form + self.xmpp.plugin['xep_0050'].continue_command(session) + self.close_tab() + +def terminate_adhoc_command(self, form, session): + self.xmpp.plugin['xep_0050'].terminate_command(session) + self.close_tab() diff --git a/src/tabs/adhoc_commands_list.py b/src/tabs/adhoc_commands_list.py index d0504677..76cd941a 100644 --- a/src/tabs/adhoc_commands_list.py +++ b/src/tabs/adhoc_commands_list.py @@ -25,12 +25,14 @@ class AdhocCommandsListTab(ListTab): self.key_func['^M'] = self.execute_selected_command def execute_selected_command(self): - row = self.listview.get_selected_row() - log.debug("Executing command %s", row) + node, name, jid = self.listview.get_selected_row() + session = {'next': self.core.on_next_adhoc_step, + 'error': self.core.on_adhoc_error} + self.core.xmpp.plugin['xep_0050'].start_command(jid, node, session) def get_columns_sizes(self): - return {'Node': int(self.width * 2 / 8), - 'Description': int(self.width * 6 / 8)} + return {'Node': int(self.width * 3 / 8), + 'Description': int(self.width * 5 / 8)} def on_list_received(self, iq): """ @@ -39,14 +41,12 @@ class AdhocCommandsListTab(ListTab): if iq['type'] == 'error': self.set_error(iq['error']['type'], iq['error']['code'], iq['error']['text']) return - log.debug("iq: %s", iq) def get_items(): substanza = iq['disco_items'] for item in substanza['substanzas']: if isinstance(item, DiscoItem): yield item items = [(item['node'], item['name'] or '', item['jid']) for item in get_items()] - log.debug(items) self.listview.set_lines(items) self.info_header.message = _('Ad-hoc commands of JID %s') % self.name if self.core.current_tab() is self: diff --git a/src/tabs/data_forms.py b/src/tabs/data_forms.py index ecd28e33..44c29e6c 100644 --- a/src/tabs/data_forms.py +++ b/src/tabs/data_forms.py @@ -44,13 +44,13 @@ class DataFormsTab(Tab): self.update_commands() def on_cancel(self): - self._on_cancel(self._form) + self._on_cancel(self._form, **self._kwargs) return True def on_send(self): self._form.reply() self.form_win.reply() - self._on_send(self._form) + self._on_send(self._form, **self._kwargs) return True def on_input(self, key, raw=False): -- cgit v1.2.3