-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
62 lines (58 loc) · 1.76 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @file commands.js
* Implements XEP-0050 (Ad-Hoc Commands).
* @author Christoph Burschka
* @see http://xmpp.org/extensions/xep-0050.html
*/
define(['strophe.js'], ({Strophe, $iq}) => {
Strophe.addNamespace('COMMANDS', 'http://jabber.org/protocol/commands');
Strophe.addNamespace('DATA', 'jabber:x:data');
Strophe.addConnectionPlugin('commands', {
init(connection) {
this._c = connection;
},
/**
* List the available command nodes on a component.
*
* @param {String} to
* @param {int} timeout (optional)
*
* @return {Promise}
*/
list(to, timeout) {
return this._c.disco.queryItems(to, {timeout, node: Strophe.NS.COMMANDS});
},
/**
* Execute a command node.
*
* @param {String} to
* @param {String} node
* @param {int} timeout (optional)
*
* @return {Promise}
*/
execute(to, node, timeout) {
const id = this._c.getUniqueId('commands');
const iq = $iq({id, to, type: 'set'});
iq.c('query', {node, xmlns: Strophe.NS.COMMANDS, action: 'execute'});
return new Promise((resolve, reject) => this._c.sendIQ(iq, resolve, reject, timeout));
},
/**
*
* @param {String} to
* @param {String} node
* @param {Object} data (optional)
* @param {int} timeout (optional)
*
* @return {Promise}
*/
submit(to, node, data, timeout) {
const id = this._c.getUniqueId('commands');
const iq = $iq({id, to, type: 'set'});
iq.c('query', {node, xmlns: Strophe.NS.COMMANDS});
iq.c('x', {xmlns: Strophe.NS.DATA, type: 'submit'});
Array.concat(data).forEach(node => iq.cnode(node).up());
return new Promise((resolve, reject) => this._c.sendIQ(iq, resolve, reject, timeout));
},
});
});