Move adhoc plugin into own directory with separate files

This commit is contained in:
JC Brand 2023-02-09 23:41:43 +01:00
parent 0fcdb2a594
commit 5db3e8ca51
5 changed files with 51 additions and 47 deletions

View File

@ -2,13 +2,13 @@
* -------------------- * --------------------
* Any of the following components may be removed if they're not needed. * Any of the following components may be removed if they're not needed.
*/ */
import "./plugins/adhoc.js"; // XEP-0050 Ad Hoc Commands
import "./plugins/bookmarks/index.js"; // XEP-0199 XMPP Ping import "./plugins/bookmarks/index.js"; // XEP-0199 XMPP Ping
import "./plugins/bosh.js"; // XEP-0206 BOSH import "./plugins/bosh.js"; // XEP-0206 BOSH
import "./plugins/caps/index.js"; // XEP-0115 Entity Capabilities import "./plugins/caps/index.js"; // XEP-0115 Entity Capabilities
import "./plugins/chat/index.js"; // RFC-6121 Instant messaging import "./plugins/chat/index.js"; // RFC-6121 Instant messaging
import "./plugins/chatboxes/index.js"; import "./plugins/chatboxes/index.js";
import "./plugins/disco/index.js"; // XEP-0030 Service discovery import "./plugins/disco/index.js"; // XEP-0030 Service discovery
import "./plugins/adhoc/index.js"; // XEP-0050 Ad Hoc Commands
import "./plugins/headlines/index.js"; // Support for headline messages import "./plugins/headlines/index.js"; // Support for headline messages
import "./plugins/mam/index.js"; // XEP-0313 Message Archive Management import "./plugins/mam/index.js"; // XEP-0313 Message Archive Management
import "./plugins/muc/index.js"; // XEP-0045 Multi-user chat import "./plugins/muc/index.js"; // XEP-0045 Multi-user chat

View File

@ -1,4 +1,4 @@
import isElement from 'lodash-es/isElement'; import { isElement } from './utils/core.js';
const LEVELS = { const LEVELS = {
'debug': 0, 'debug': 0,

View File

@ -1,34 +1,11 @@
import log from "@converse/headless/log"; import log from '@converse/headless/log';
import sizzle from 'sizzle'; import { _converse, api, converse } from "@converse/headless/core";
import { __ } from 'i18n'; import { getCommandFields, parseForCommands } from './utils.js';
import { converse } from "../core.js";
import { getAttributes } from '@converse/headless/shared/parsers';
const { Strophe, u, stx, $iq } = converse.env; const { Strophe, $iq, u, stx } = converse.env;
let api;
Strophe.addNamespace('ADHOC', 'http://jabber.org/protocol/commands');
function parseForCommands (stanza) { export default {
const items = sizzle(`query[xmlns="${Strophe.NS.DISCO_ITEMS}"][node="${Strophe.NS.ADHOC}"] item`, stanza);
return items.map(getAttributes)
}
function getCommandFields (iq, jid) {
const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
const data = {
sessionid: cmd_el.getAttribute('sessionid'),
instructions: sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent,
fields: sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
.map(f => u.xForm2TemplateResult(f, cmd_el, { domain: jid })),
actions: Array.from(cmd_el.querySelector('actions')?.children).map((a) => a.nodeName.toLowerCase()) ?? []
}
return data;
}
const adhoc_api = {
/** /**
* The XEP-0050 Ad-Hoc Commands API * The XEP-0050 Ad-Hoc Commands API
* *
@ -80,6 +57,7 @@ const adhoc_api = {
log.error(`Error while trying to execute command for ${jid}`); log.error(`Error while trying to execute command for ${jid}`);
log.error(e); log.error(e);
} }
const { __ } = _converse;
return { return {
instructions: __('An error occurred while trying to fetch the command form'), instructions: __('An error occurred while trying to fetch the command form'),
fields: [] fields: []
@ -99,15 +77,17 @@ const adhoc_api = {
const iq = const iq =
stx`<iq type="set" to="${jid}" xmlns="jabber:client"> stx`<iq type="set" to="${jid}" xmlns="jabber:client">
<command sessionid="${sessionid}" node="${node}" action="${action}" xmlns="${Strophe.NS.ADHOC}"> <command sessionid="${sessionid}" node="${node}" action="${action}" xmlns="${Strophe.NS.ADHOC}">
${ !['cancel', 'prev'].includes(action) ? stx`
<x xmlns="${Strophe.NS.XFORM}" type="submit"> <x xmlns="${Strophe.NS.XFORM}" type="submit">
${ inputs.reduce((out, { name, value }) => out + `<field var="${name}"><value>${value}</value></field>`, '') } ${ inputs.reduce((out, { name, value }) => out + `<field var="${name}"><value>${value}</value></field>`, '') }
</x> </x>` : '' }
</command> </command>
</iq>`; </iq>`;
const result = await api.sendIQ(iq, null, false); const result = await api.sendIQ(iq, null, false);
if (result === null) { if (result === null) {
log.warn(`A timeout occurred while trying to run an ad-hoc command`); log.warn(`A timeout occurred while trying to run an ad-hoc command`);
const { __ } = _converse;
return { return {
status: 'error', status: 'error',
note: __('A timeout occurred'), note: __('A timeout occurred'),
@ -127,17 +107,3 @@ const adhoc_api = {
} }
} }
} }
converse.plugins.add('converse-adhoc', {
dependencies: ["converse-disco"],
initialize () {
const _converse = this._converse;
api = _converse.api;
Object.assign(api, adhoc_api);
}
});
export default adhoc_api;

View File

@ -0,0 +1,16 @@
import adhoc_api from './api.js';
import { converse } from "@converse/headless/core";
const { Strophe } = converse.env;
Strophe.addNamespace('ADHOC', 'http://jabber.org/protocol/commands');
converse.plugins.add('converse-adhoc', {
dependencies: ["converse-disco"],
initialize () {
Object.assign(this._converse.api, adhoc_api);
}
});

View File

@ -0,0 +1,22 @@
import sizzle from 'sizzle';
import { converse } from "@converse/headless/core";
import { getAttributes } from '@converse/headless/shared/parsers';
const { Strophe, u } = converse.env;
export function parseForCommands (stanza) {
const items = sizzle(`query[xmlns="${Strophe.NS.DISCO_ITEMS}"][node="${Strophe.NS.ADHOC}"] item`, stanza);
return items.map(getAttributes)
}
export function getCommandFields (iq, jid) {
const cmd_el = sizzle(`command[xmlns="${Strophe.NS.ADHOC}"]`, iq).pop();
const data = {
sessionid: cmd_el.getAttribute('sessionid'),
instructions: sizzle('x[type="form"][xmlns="jabber:x:data"] instructions', cmd_el).pop()?.textContent,
fields: sizzle('x[type="form"][xmlns="jabber:x:data"] field', cmd_el)
.map(f => u.xForm2TemplateResult(f, cmd_el, { domain: jid })),
actions: Array.from(cmd_el.querySelector('actions')?.children).map((a) => a.nodeName.toLowerCase()) ?? []
}
return data;
}