Commit d78b7df6 authored by louiz’'s avatar louiz’

Implement the execution of ad-hoc commands (#1832)

parent bbc55fa4
......@@ -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
......
......@@ -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()
......@@ -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:
......
......@@ -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):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment