Annotate

plugins/adhoc/mod_adhoc.lua @ 11907:0dc2c3530d64

MUC: Add 'muc-pre-set-affiliation' event, allowing to block change or modify data
author Matthew Wild <mwild1@gmail.com>
date Tue, 16 Nov 2021 11:41:08 +0000
parent 11209:f6661fac7e9a
child 12387:05c250fa335a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Copyright (C) 2009 Thilo Cestonaro
4291
122f142da281 mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents: 3511
diff changeset
2 -- Copyright (C) 2009-2011 Florian Zeitz
3456
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
3 --
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 -- This file is MIT/X11 licensed. Please see the
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 --
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
9331
2f634cc02eac mod_adhoc: Use util.iterators.sorted_pairs() to sort commands
Matthew Wild <mwild1@gmail.com>
parents: 9222
diff changeset
8 local it = require "util.iterators";
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local st = require "util.stanza";
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local is_admin = require "core.usermanager".is_admin;
10542
f1886a48a6d4 mod_adhoc: Remove unused variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9571
diff changeset
11 local jid_host = require "util.jid".host;
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local adhoc_handle_cmd = module:require "adhoc".handle_cmd;
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local xmlns_cmd = "http://jabber.org/protocol/commands";
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local commands = {};
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 module:add_feature(xmlns_cmd);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
18 module:hook("host-disco-info-node", function (event)
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
19 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
20 if commands[node] then
5762
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
21 local from = stanza.attr.from;
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
22 local privileged = is_admin(from, stanza.attr.to);
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
23 local global_admin = is_admin(from);
10542
f1886a48a6d4 mod_adhoc: Remove unused variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9571
diff changeset
24 local hostname = jid_host(from);
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
25 local command = commands[node];
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
26 if (command.permission == "admin" and privileged)
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
27 or (command.permission == "global_admin" and global_admin)
5762
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
28 or (command.permission == "local_user" and hostname == module.host)
10565
421b2f8369fd mod_adhoc: Improve permission setting (fix #1482) BC
Kim Alvefur <zash@zash.se>
parents: 10542
diff changeset
29 or (command.permission == "any") then
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
30 reply:tag("identity", { name = command.name,
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
31 category = "automation", type = "command-node" }):up();
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
32 reply:tag("feature", { var = xmlns_cmd }):up();
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
33 reply:tag("feature", { var = "jabber:x:data" }):up();
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
34 event.exists = true;
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
35 else
6841
be87ab2d611c plugins: Explicitly return to halt event propagation (session.send sometimes does not return true)
Kim Alvefur <zash@zash.se>
parents: 5762
diff changeset
36 origin.send(st.error_reply(stanza, "auth", "forbidden", "This item is not available to you"));
be87ab2d611c plugins: Explicitly return to halt event propagation (session.send sometimes does not return true)
Kim Alvefur <zash@zash.se>
parents: 5762
diff changeset
37 return true;
3457
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
38 end
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
39 elseif node == xmlns_cmd then
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
40 reply:tag("identity", { name = "Ad-Hoc Commands",
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
41 category = "automation", type = "command-list" }):up();
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
42 event.exists = true;
3457
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
43 end
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
44 end);
24d2c9be0149 mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents: 3456
diff changeset
45
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
46 module:hook("host-disco-items-node", function (event)
8563
50f2ad088589 mod_adhoc: Remove unused local [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8460
diff changeset
47 local stanza, reply, disco_node = event.stanza, event.reply, event.node;
8460
77e59f8057bf mod_adhoc: Rename variable to avoid name clash [luacheck]
Kim Alvefur <zash@zash.se>
parents: 6841
diff changeset
48 if disco_node ~= xmlns_cmd then
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
49 return;
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
50 end
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
51
5762
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
52 local from = stanza.attr.from;
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
53 local admin = is_admin(from, stanza.attr.to);
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
54 local global_admin = is_admin(from);
10542
f1886a48a6d4 mod_adhoc: Remove unused variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9571
diff changeset
55 local hostname = jid_host(from);
9331
2f634cc02eac mod_adhoc: Use util.iterators.sorted_pairs() to sort commands
Matthew Wild <mwild1@gmail.com>
parents: 9222
diff changeset
56 for node, command in it.sorted_pairs(commands) do
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
57 if (command.permission == "admin" and admin)
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
58 or (command.permission == "global_admin" and global_admin)
5762
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
59 or (command.permission == "local_user" and hostname == module.host)
10565
421b2f8369fd mod_adhoc: Improve permission setting (fix #1482) BC
Kim Alvefur <zash@zash.se>
parents: 10542
diff changeset
60 or (command.permission == "any") then
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
61 reply:tag("item", { name = command.name,
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
62 node = node, jid = module:get_host() });
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
63 reply:up();
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
66 event.exists = true;
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
67 end);
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
9222
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
69 module:hook("iq-set/host/"..xmlns_cmd..":command", function (event)
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 local origin, stanza = event.origin, event.stanza;
9222
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
71 local node = stanza.tags[1].attr.node
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
72 local command = commands[node];
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
73 if command then
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
74 local from = stanza.attr.from;
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
75 local admin = is_admin(from, stanza.attr.to);
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
76 local global_admin = is_admin(from);
10542
f1886a48a6d4 mod_adhoc: Remove unused variable [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9571
diff changeset
77 local hostname = jid_host(from);
9222
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
78 if (command.permission == "admin" and not admin)
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
79 or (command.permission == "global_admin" and not global_admin)
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
80 or (command.permission == "local_user" and hostname ~= module.host) then
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
81 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
82 :add_child(commands[node]:cmdtag("canceled")
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
83 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
84 return true
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 end
9222
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
86 -- User has permission now execute the command
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
87 adhoc_handle_cmd(commands[node], origin, stanza);
fe8abac62682 mod_adhoc: Simplify iq handling by hooking on iq-set/ instead of iq/.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 8563
diff changeset
88 return true;
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 end, 500);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91
4450
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
92 local function adhoc_added(event)
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
93 local item = event.item;
11209
f6661fac7e9a mod_adhoc: Log commands provided at debug level
Kim Alvefur <zash@zash.se>
parents: 10565
diff changeset
94 -- Dang this was noicy
f6661fac7e9a mod_adhoc: Log commands provided at debug level
Kim Alvefur <zash@zash.se>
parents: 10565
diff changeset
95 module:log("debug", "Command added by mod_%s: %q, %q", item._provided_by or "<unknown module>", item.name, item.node);
3231
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
96 commands[item.node] = item;
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
97 end
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
98
4450
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
99 local function adhoc_removed(event)
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 commands[event.item.node] = nil;
4450
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
101 end
3231
ad3fbed1dda5 mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents: 3220
diff changeset
102
9571
5c475f6e89a4 mod_adhoc: Add compat marker for older handling of adhoc items
Kim Alvefur <zash@zash.se>
parents: 9331
diff changeset
103 module:handle_items("adhoc", adhoc_added, adhoc_removed); -- COMPAT pre module:provides() introduced in 0.9
4926
58714123f600 mod_adhoc, mod_admin_adhoc, mod_announce: Use module:provides() to manage Ad-Hoc commands
Florian Zeitz <florob@babelmonkeys.de>
parents: 4450
diff changeset
104 module:handle_items("adhoc-provider", adhoc_added, adhoc_removed);