Annotate

plugins/adhoc/mod_adhoc.lua @ 6391:4d334d00c635

plugins/muc/muc.lib: Add instant room support
author daurnimator <quae@daurnimator.com>
date Fri, 05 Sep 2014 11:19:16 -0400
parent 5762:785da1854eb9
child 6841:be87ab2d611c
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
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local st = require "util.stanza";
5760
e599d9a367cf mod_adhoc: Sort commands by node. This guarantees the order remains the same across restarts etc.
Florian Zeitz <florob@babelmonkeys.de>
parents: 4926
diff changeset
9 local keys = require "util.iterators".keys;
e599d9a367cf mod_adhoc: Sort commands by node. This guarantees the order remains the same across restarts etc.
Florian Zeitz <florob@babelmonkeys.de>
parents: 4926
diff changeset
10 local array_collect = require "util.array".collect;
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local is_admin = require "core.usermanager".is_admin;
5762
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
12 local jid_split = require "util.jid".split;
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 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
14 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
15 local commands = {};
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 module:add_feature(xmlns_cmd);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
19 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
20 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
21 if commands[node] then
5762
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
22 local from = stanza.attr.from;
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
23 local privileged = is_admin(from, stanza.attr.to);
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
24 local global_admin = is_admin(from);
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
25 local username, hostname = jid_split(from);
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
26 local command = commands[node];
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
27 if (command.permission == "admin" and privileged)
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
28 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
29 or (command.permission == "local_user" and hostname == module.host)
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
30 or (command.permission == "user") then
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
31 reply:tag("identity", { name = command.name,
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
32 category = "automation", type = "command-node" }):up();
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
33 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
34 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
35 event.exists = true;
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
36 else
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
37 return origin.send(st.error_reply(stanza, "auth", "forbidden", "This item is not available to you"));
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)
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
47 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
48 if node ~= xmlns_cmd then
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);
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
55 local username, hostname = jid_split(from);
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
56 local nodes = array_collect(keys(commands)):sort();
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
57 for _, node in ipairs(nodes) do
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
58 local command = commands[node];
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
59 if (command.permission == "admin" and admin)
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
60 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
61 or (command.permission == "local_user" and hostname == module.host)
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
62 or (command.permission == "user") then
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
63 reply:tag("item", { name = command.name,
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
64 node = node, jid = module:get_host() });
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
65 reply:up();
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 end
5761
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
68 event.exists = true;
91f8cd53584c mod_adhoc: Use mod_disco for disco handling
Florian Zeitz <florob@babelmonkeys.de>
parents: 5760
diff changeset
69 end);
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
3456
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
71 module:hook("iq/host/"..xmlns_cmd..":command", function (event)
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local origin, stanza = event.origin, event.stanza;
3456
1201a743fe63 mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents: 3286
diff changeset
73 if stanza.attr.type == "set" then
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local node = stanza.tags[1].attr.node
5762
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
75 local command = commands[node];
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
76 if command then
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
77 local from = stanza.attr.from;
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
78 local admin = is_admin(from, stanza.attr.to);
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
79 local global_admin = is_admin(from);
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
80 local username, hostname = jid_split(from);
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
81 if (command.permission == "admin" and not admin)
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
82 or (command.permission == "global_admin" and not global_admin)
785da1854eb9 mod_adhoc: Add local_user permission
Florian Zeitz <florob@babelmonkeys.de>
parents: 5761
diff changeset
83 or (command.permission == "local_user" and hostname ~= module.host) then
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 :add_child(commands[node]:cmdtag("canceled")
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 return true
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 -- User has permission now execute the command
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 return adhoc_handle_cmd(commands[node], origin, stanza);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 end
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end, 500);
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
4450
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
95 local function adhoc_added(event)
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
96 local item = event.item;
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
97 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
98 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
99
4450
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
100 local function adhoc_removed(event)
3220
b3772f9bc359 mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 commands[event.item.node] = nil;
4450
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
102 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
103
4450
15547fba1f09 mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents: 4291
diff changeset
104 module:handle_items("adhoc", adhoc_added, adhoc_removed);
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
105 module:handle_items("adhoc-provider", adhoc_added, adhoc_removed);