diff --git a/src/plugins/muc-views/adhoc-commands.js b/src/plugins/muc-views/adhoc-commands.js index 7e41084bb..d20bbc7db 100644 --- a/src/plugins/muc-views/adhoc-commands.js +++ b/src/plugins/muc-views/adhoc-commands.js @@ -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` -
- ${ command.alert ? html`` : '' } -
- - - -

${command.instructions}

- ${ command.fields } -
-
- - -
-
- `; -} - - -const tpl_command = (o, command) => html` -
  • -
    - ${command.name || command.jid} -
    - ${ command.node === o.showform ? tpl_command_form(o, command) : '' } -
  • -`; - - -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`` : '' } -
    -
    - -
    -
    - -
    - ${ o.view === 'list-commands' ? html` -
    - -
    ` - : '' } - -
    - `; -} - - -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 { diff --git a/src/plugins/muc-views/templates/ad-hoc-command-form.js b/src/plugins/muc-views/templates/ad-hoc-command-form.js new file mode 100644 index 000000000..12ec2a793 --- /dev/null +++ b/src/plugins/muc-views/templates/ad-hoc-command-form.js @@ -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` +
    + ${ command.alert ? html`` : '' } +
    + + + +

    ${command.instructions}

    + ${ command.fields } +
    +
    + + +
    +
    + `; +} diff --git a/src/plugins/muc-views/templates/ad-hoc-command.js b/src/plugins/muc-views/templates/ad-hoc-command.js new file mode 100644 index 000000000..bdb6784ee --- /dev/null +++ b/src/plugins/muc-views/templates/ad-hoc-command.js @@ -0,0 +1,17 @@ +import { html } from "lit-html"; +import tpl_command_form from './ad-hoc-command-form.js'; + +export default (o, command) => html` +
  • +
    + ${command.name || command.jid} +
    + ${ command.node === o.showform ? tpl_command_form(o, command) : '' } +
  • +`; diff --git a/src/plugins/muc-views/templates/ad-hoc.js b/src/plugins/muc-views/templates/ad-hoc.js new file mode 100644 index 000000000..4974f00eb --- /dev/null +++ b/src/plugins/muc-views/templates/ad-hoc.js @@ -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`` : '' } +
    +
    + +
    +
    + +
    + ${ o.view === 'list-commands' ? html` +
    + +
    ` + : '' } + +
    + `; +} diff --git a/src/plugins/muc-views/utils.js b/src/plugins/muc-views/utils.js index 67fa5999b..63d167a09 100644 --- a/src/plugins/muc-views/utils.js +++ b/src/plugins/muc-views/utils.js @@ -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 = []; + } +}