Annotate

plugins/adhoc.lua @ 498:50d0bd035bb7

util.sasl.oauthbearer: Don't send authzid It's not needed and not recommended in XMPP unless we want to act as someone other than who we authenticate as. We find out the JID during resource binding.
author Kim Alvefur <zash@zash.se>
date Fri, 23 Jun 2023 12:09:49 +0200
parent 490:6b2f31da9610
child 499:57417c37d018
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 145
diff changeset
1 local verse = require "verse";
490
6b2f31da9610 Update for new Prosody module namespace
Kim Alvefur <zash@zash.se>
parents: 380
diff changeset
2 local adhoc = require "verse.lib.adhoc";
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local xmlns_commands = "http://jabber.org/protocol/commands";
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
5 local xmlns_data = "jabber:x:data";
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
7 local command_mt = {};
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
8 command_mt.__index = command_mt;
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
9
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
10 -- Table of commands we provide
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local commands = {};
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 function verse.plugins.adhoc(stream)
308
2bcc97bc5f43 plugins.adhoc: Add explicit dependency on the disco plugin.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
14 stream:add_plugin("disco");
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 stream:add_disco_feature(xmlns_commands);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
17 function stream:query_commands(jid, callback)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
18 stream:disco_items(jid, xmlns_commands, function (items)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
19 stream:debug("adhoc list returned")
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
20 local command_list = {};
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
21 for _, item in ipairs(items) do
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
22 command_list[item.node] = item.name;
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
23 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
24 stream:debug("adhoc calling callback")
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
25 return callback(command_list);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
26 end);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
27 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
28
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
29 function stream:execute_command(jid, command, callback)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
30 local cmd = setmetatable({
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
31 stream = stream, jid = jid,
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
32 command = command, callback = callback
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
33 }, command_mt);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
34 return cmd:execute();
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
35 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
36
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
37 -- ACL checker for commands we provide
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 local function has_affiliation(jid, aff)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 if not(aff) or aff == "user" then return true; end
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
40 if type(aff) == "function" then
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
41 return aff(jid);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
42 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
43 -- TODO: Support 'roster', etc.
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
45
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 function stream:add_adhoc_command(name, node, handler, permission)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 commands[node] = adhoc.new(name, node, handler, permission);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 stream:add_disco_item({ jid = stream.jid, node = node, name = name }, xmlns_commands);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 return commands[node];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
51
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 local function handle_command(stanza)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local command_tag = stanza.tags[1];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 local node = command_tag.attr.node;
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
55
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 local handler = commands[node];
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if not handler then return; end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
58
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 if not has_affiliation(stanza.attr.from, handler.permission) then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 stream:send(verse.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 :add_child(handler:cmdtag("canceled")
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 return true
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
65
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 -- User has permission now execute the command
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 return adhoc.handle_cmd(handler, { send = function (d) return stream:send(d) end }, stanza);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
69
116
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 stream:hook("iq/"..xmlns_commands, function (stanza)
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 local type = stanza.attr.type;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local name = stanza.tags[1].name;
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if type == "set" and name == "command" then
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 return handle_command(stanza);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 end
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 end);
151c8cc776df verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 end
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
78
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
79 function command_mt:_process_response(result)
341
a95890d86fe4 plugins.adhoc, plugins.jingle: Fix checking of type attribute (thanks Mark)
Matthew Wild <mwild1@gmail.com>
parents: 340
diff changeset
80 if result.attr.type == "error" then
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
81 self.status = "canceled";
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
82 self.callback(self, {});
340
0ce227f6034c plugins.adhoc: Add missing return in error case
Matthew Wild <mwild1@gmail.com>
parents: 309
diff changeset
83 return;
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
84 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
85 local command = result:get_child("command", xmlns_commands);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
86 self.status = command.attr.status;
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
87 self.sessionid = command.attr.sessionid;
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
88 self.form = command:get_child("x", xmlns_data);
309
6297d658769f plugins.adhoc: Collect a <note/>
Kim Alvefur <zash@zash.se>
parents: 308
diff changeset
89 self.note = command:get_child("note"); --FIXME handle multiple <note/>s
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
90 self.callback(self);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
91 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
92
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
93 -- Initial execution of a command
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
94 function command_mt:execute()
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
95 local iq = verse.iq({ to = self.jid, type = "set" })
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
96 :tag("command", { xmlns = xmlns_commands, node = self.command });
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
97 self.stream:send_iq(iq, function (result)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
98 self:_process_response(result);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
99 end);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
100 end
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
101
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
102 function command_mt:next(form)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
103 local iq = verse.iq({ to = self.jid, type = "set" })
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
104 :tag("command", {
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
105 xmlns = xmlns_commands,
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
106 node = self.command,
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
107 sessionid = self.sessionid
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
108 });
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
109
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
110 if form then iq:add_child(form); end
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 341
diff changeset
111
122
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
112 self.stream:send_iq(iq, function (result)
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
113 self:_process_response(result);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
114 end);
e2600454fd54 plugins.adhoc: Support for querying for and executing commands
Matthew Wild <mwild1@gmail.com>
parents: 116
diff changeset
115 end