Split ad-hoc commands code into smaller files
This commit is contained in:
parent
de4fd466dc
commit
c69eb6e1bf
@ -1,125 +1,12 @@
|
||||
import 'shared/autocomplete/index.js';
|
||||
import log from "@converse/headless/log";
|
||||
import tpl_adhoc from './templates/ad-hoc.js';
|
||||
import { CustomElement } from 'components/element.js';
|
||||
import { __ } from 'i18n';
|
||||
import { api, converse } from "@converse/headless/core";
|
||||
import { html } from "lit-html";
|
||||
import { fetchCommandForm } from './utils.js';
|
||||
|
||||
const { Strophe, $iq, sizzle } = converse.env;
|
||||
const u = converse.env.utils;
|
||||
|
||||
|
||||
const tpl_command_form = (o, command) => {
|
||||
const i18n_hide = __('Hide');
|
||||
const i18n_run = __('Execute');
|
||||
return html`
|
||||
<form @submit=${o.runCommand}>
|
||||
${ command.alert ? html`<div class="alert alert-${command.alert_type}" role="alert">${command.alert}</div>` : '' }
|
||||
<fieldset class="form-group">
|
||||
<input type="hidden" name="command_node" value="${command.node}"/>
|
||||
<input type="hidden" name="command_jid" value="${command.jid}"/>
|
||||
|
||||
<p class="form-help">${command.instructions}</p>
|
||||
${ command.fields }
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<input type="submit" class="btn btn-primary" value="${i18n_run}">
|
||||
<input type="button" class="btn btn-secondary button-cancel" value="${i18n_hide}" @click=${o.hideCommandForm}>
|
||||
</fieldset>
|
||||
</form>
|
||||
`;
|
||||
}
|
||||
|
||||
|
||||
const tpl_command = (o, command) => html`
|
||||
<li class="room-item list-group-item">
|
||||
<div class="available-chatroom d-flex flex-row">
|
||||
<a class="open-room available-room w-100"
|
||||
@click=${o.toggleCommandForm}
|
||||
data-command-node="${command.node}"
|
||||
data-command-jid="${command.jid}"
|
||||
data-command-name="${command.name}"
|
||||
title="${command.name}"
|
||||
href="#">${command.name || command.jid}</a>
|
||||
</div>
|
||||
${ command.node === o.showform ? tpl_command_form(o, command) : '' }
|
||||
</li>
|
||||
`;
|
||||
|
||||
|
||||
async function getAutoCompleteList () {
|
||||
const models = [...(await api.rooms.get()), ...(await api.contacts.get())];
|
||||
const jids = [...new Set(models.map(o => Strophe.getDomainFromJid(o.get('jid'))))];
|
||||
return jids;
|
||||
}
|
||||
|
||||
const tpl_adhoc = (o) => {
|
||||
const i18n_choose_service = __('On which entity do you want to run commands?');
|
||||
const i18n_choose_service_instructions = __(
|
||||
'Certain XMPP services and entities allow privileged users to execute ad-hoc commands on them.');
|
||||
const i18n_commands_found = __('Commands found');
|
||||
const i18n_fetch_commands = __('List available commands');
|
||||
const i18n_jid_placeholder = __('XMPP Address');
|
||||
const i18n_no_commands_found = __('No commands found');
|
||||
return html`
|
||||
${ o.alert ? html`<div class="alert alert-${o.alert_type}" role="alert">${o.alert}</div>` : '' }
|
||||
<form class="converse-form" @submit=${o.fetchCommands}>
|
||||
<fieldset class="form-group">
|
||||
<label>
|
||||
${i18n_choose_service}
|
||||
<p class="form-help">${i18n_choose_service_instructions}</p>
|
||||
<converse-autocomplete
|
||||
.getAutoCompleteList="${getAutoCompleteList}"
|
||||
placeholder="${i18n_jid_placeholder}"
|
||||
name="jid"/>
|
||||
</label>
|
||||
</fieldset>
|
||||
<fieldset class="form-group">
|
||||
<input type="submit" class="btn btn-primary" value="${i18n_fetch_commands}">
|
||||
</fieldset>
|
||||
${ o.view === 'list-commands' ? html`
|
||||
<fieldset class="form-group">
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item active">${ o.commands.length ? i18n_commands_found : i18n_no_commands_found }:</li>
|
||||
${ o.commands.map(cmd => tpl_command(o, cmd)) }
|
||||
</ul>
|
||||
</fieldset>`
|
||||
: '' }
|
||||
|
||||
</form>
|
||||
`;
|
||||
}
|
||||
|
||||
|
||||
async function fetchCommandForm (command) {
|
||||
const node = command.node;
|
||||
const jid = command.jid;
|
||||
const stanza = $iq({
|
||||
'type': 'set',
|
||||
'to': jid
|
||||
}).c('command', {
|
||||
'xmlns': Strophe.NS.ADHOC,
|
||||
'node': node,
|
||||
'action': 'execute'
|
||||
});
|
||||
try {
|
||||
const iq = await api.sendIQ(stanza);
|
||||
const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
|
||||
command.sessionid = cmd_el.getAttribute('sessionid');
|
||||
command.instructions = sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent;
|
||||
command.fields = sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
|
||||
.map(f => u.xForm2TemplateResult(f, cmd_el));
|
||||
|
||||
} catch (e) {
|
||||
if (e === null) {
|
||||
log.error(`Error: timeout while trying to execute command for ${jid}`);
|
||||
} else {
|
||||
log.error(`Error while trying to execute command for ${jid}`);
|
||||
log.error(e);
|
||||
}
|
||||
command.fields = [];
|
||||
}
|
||||
}
|
||||
const { Strophe, $iq, sizzle, u } = converse.env;
|
||||
|
||||
|
||||
export default class AdHocCommands extends CustomElement {
|
||||
|
23
src/plugins/muc-views/templates/ad-hoc-command-form.js
Normal file
23
src/plugins/muc-views/templates/ad-hoc-command-form.js
Normal file
@ -0,0 +1,23 @@
|
||||
import { __ } from 'i18n';
|
||||
import { html } from "lit-html";
|
||||
|
||||
export default (o, command) => {
|
||||
const i18n_hide = __('Hide');
|
||||
const i18n_run = __('Execute');
|
||||
return html`
|
||||
<form @submit=${o.runCommand}>
|
||||
${ command.alert ? html`<div class="alert alert-${command.alert_type}" role="alert">${command.alert}</div>` : '' }
|
||||
<fieldset class="form-group">
|
||||
<input type="hidden" name="command_node" value="${command.node}"/>
|
||||
<input type="hidden" name="command_jid" value="${command.jid}"/>
|
||||
|
||||
<p class="form-help">${command.instructions}</p>
|
||||
${ command.fields }
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<input type="submit" class="btn btn-primary" value="${i18n_run}">
|
||||
<input type="button" class="btn btn-secondary button-cancel" value="${i18n_hide}" @click=${o.hideCommandForm}>
|
||||
</fieldset>
|
||||
</form>
|
||||
`;
|
||||
}
|
17
src/plugins/muc-views/templates/ad-hoc-command.js
Normal file
17
src/plugins/muc-views/templates/ad-hoc-command.js
Normal file
@ -0,0 +1,17 @@
|
||||
import { html } from "lit-html";
|
||||
import tpl_command_form from './ad-hoc-command-form.js';
|
||||
|
||||
export default (o, command) => html`
|
||||
<li class="room-item list-group-item">
|
||||
<div class="available-chatroom d-flex flex-row">
|
||||
<a class="open-room available-room w-100"
|
||||
@click=${o.toggleCommandForm}
|
||||
data-command-node="${command.node}"
|
||||
data-command-jid="${command.jid}"
|
||||
data-command-name="${command.name}"
|
||||
title="${command.name}"
|
||||
href="#">${command.name || command.jid}</a>
|
||||
</div>
|
||||
${ command.node === o.showform ? tpl_command_form(o, command) : '' }
|
||||
</li>
|
||||
`;
|
42
src/plugins/muc-views/templates/ad-hoc.js
Normal file
42
src/plugins/muc-views/templates/ad-hoc.js
Normal file
@ -0,0 +1,42 @@
|
||||
import tpl_command from './ad-hoc-command.js';
|
||||
import { __ } from 'i18n';
|
||||
import { getAutoCompleteList } from '../utils.js';
|
||||
import { html } from "lit-html";
|
||||
|
||||
|
||||
export default (o) => {
|
||||
const i18n_choose_service = __('On which entity do you want to run commands?');
|
||||
const i18n_choose_service_instructions = __(
|
||||
'Certain XMPP services and entities allow privileged users to execute ad-hoc commands on them.');
|
||||
const i18n_commands_found = __('Commands found');
|
||||
const i18n_fetch_commands = __('List available commands');
|
||||
const i18n_jid_placeholder = __('XMPP Address');
|
||||
const i18n_no_commands_found = __('No commands found');
|
||||
return html`
|
||||
${ o.alert ? html`<div class="alert alert-${o.alert_type}" role="alert">${o.alert}</div>` : '' }
|
||||
<form class="converse-form" @submit=${o.fetchCommands}>
|
||||
<fieldset class="form-group">
|
||||
<label>
|
||||
${i18n_choose_service}
|
||||
<p class="form-help">${i18n_choose_service_instructions}</p>
|
||||
<converse-autocomplete
|
||||
.getAutoCompleteList="${getAutoCompleteList}"
|
||||
placeholder="${i18n_jid_placeholder}"
|
||||
name="jid"/>
|
||||
</label>
|
||||
</fieldset>
|
||||
<fieldset class="form-group">
|
||||
<input type="submit" class="btn btn-primary" value="${i18n_fetch_commands}">
|
||||
</fieldset>
|
||||
${ o.view === 'list-commands' ? html`
|
||||
<fieldset class="form-group">
|
||||
<ul class="list-group">
|
||||
<li class="list-group-item active">${ o.commands.length ? i18n_commands_found : i18n_no_commands_found }:</li>
|
||||
${ o.commands.map(cmd => tpl_command(o, cmd)) }
|
||||
</ul>
|
||||
</fieldset>`
|
||||
: '' }
|
||||
|
||||
</form>
|
||||
`;
|
||||
}
|
@ -1,4 +1,7 @@
|
||||
import { _converse, api } from "@converse/headless/core";
|
||||
import { _converse, api, converse } from "@converse/headless/core";
|
||||
import log from "@converse/headless/log";
|
||||
|
||||
const { Strophe, $iq, sizzle, u } = converse.env;
|
||||
|
||||
|
||||
export function getAutoCompleteListItem (text, input) {
|
||||
@ -36,3 +39,39 @@ export function getAutoCompleteListItem (text, input) {
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
export async function getAutoCompleteList () {
|
||||
const models = [...(await api.rooms.get()), ...(await api.contacts.get())];
|
||||
const jids = [...new Set(models.map(o => Strophe.getDomainFromJid(o.get('jid'))))];
|
||||
return jids;
|
||||
}
|
||||
|
||||
export async function fetchCommandForm (command) {
|
||||
const node = command.node;
|
||||
const jid = command.jid;
|
||||
const stanza = $iq({
|
||||
'type': 'set',
|
||||
'to': jid
|
||||
}).c('command', {
|
||||
'xmlns': Strophe.NS.ADHOC,
|
||||
'node': node,
|
||||
'action': 'execute'
|
||||
});
|
||||
try {
|
||||
const iq = await api.sendIQ(stanza);
|
||||
const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
|
||||
command.sessionid = cmd_el.getAttribute('sessionid');
|
||||
command.instructions = sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent;
|
||||
command.fields = sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
|
||||
.map(f => u.xForm2TemplateResult(f, cmd_el));
|
||||
|
||||
} catch (e) {
|
||||
if (e === null) {
|
||||
log.error(`Error: timeout while trying to execute command for ${jid}`);
|
||||
} else {
|
||||
log.error(`Error while trying to execute command for ${jid}`);
|
||||
log.error(e);
|
||||
}
|
||||
command.fields = [];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user