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.
*/
import "./plugins/adhoc.js"; // XEP-0050 Ad Hoc Commands
import "./plugins/bookmarks/index.js"; // XEP-0199 XMPP Ping
import "./plugins/bosh.js"; // XEP-0206 BOSH
import "./plugins/caps/index.js"; // XEP-0115 Entity Capabilities
import "./plugins/chat/index.js"; // RFC-6121 Instant messaging
import "./plugins/chatboxes/index.js";
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/mam/index.js"; // XEP-0313 Message Archive Management
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 = {
'debug': 0,

View File

@ -1,34 +1,11 @@
import log from "@converse/headless/log";
import sizzle from 'sizzle';
import { __ } from 'i18n';
import { converse } from "../core.js";
import { getAttributes } from '@converse/headless/shared/parsers';
import log from '@converse/headless/log';
import { _converse, api, converse } from "@converse/headless/core";
import { getCommandFields, parseForCommands } from './utils.js';
const { Strophe, u, stx, $iq } = converse.env;
let api;
Strophe.addNamespace('ADHOC', 'http://jabber.org/protocol/commands');
const { Strophe, $iq, u, stx } = converse.env;
function parseForCommands (stanza) {
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 = {
export default {
/**
* 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(e);
}
const { __ } = _converse;
return {
instructions: __('An error occurred while trying to fetch the command form'),
fields: []
@ -99,15 +77,17 @@ const adhoc_api = {
const iq =
stx`<iq type="set" to="${jid}" xmlns="jabber:client">
<command sessionid="${sessionid}" node="${node}" action="${action}" xmlns="${Strophe.NS.ADHOC}">
<x xmlns="${Strophe.NS.XFORM}" type="submit">
${ inputs.reduce((out, { name, value }) => out + `<field var="${name}"><value>${value}</value></field>`, '') }
</x>
${ !['cancel', 'prev'].includes(action) ? stx`
<x xmlns="${Strophe.NS.XFORM}" type="submit">
${ inputs.reduce((out, { name, value }) => out + `<field var="${name}"><value>${value}</value></field>`, '') }
</x>` : '' }
</command>
</iq>`;
const result = await api.sendIQ(iq, null, false);
if (result === null) {
log.warn(`A timeout occurred while trying to run an ad-hoc command`);
const { __ } = _converse;
return {
status: 'error',
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;
}