1 -- Copyright (C) 2009 Thilo Cestonaro
2 -- Copyright (C) 2009-2011 Florian Zeitz
4 -- This file is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
8 local st = require "util.stanza";
9 local keys = require "util.iterators".keys;
10 local array_collect = require "util.array".collect;
11 local is_admin = require "core.usermanager".is_admin;
12 local jid_split = require "util.jid".split;
13 local adhoc_handle_cmd = module:require "adhoc".handle_cmd;
14 local xmlns_cmd = "http://jabber.org/protocol/commands";
17 module:add_feature(xmlns_cmd);
19 module:hook("host-disco-info-node", function (event)
20 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
21 if commands[node] then
22 local from = stanza.attr.from;
23 local privileged = is_admin(from, stanza.attr.to);
24 local global_admin = is_admin(from);
25 local username, hostname = jid_split(from);
26 local command = commands[node];
27 if (command.permission == "admin" and privileged)
28 or (command.permission == "global_admin" and global_admin)
29 or (command.permission == "local_user" and hostname == module.host)
30 or (command.permission == "user") then
31 reply:tag("identity", { name = command.name,
32 category = "automation", type = "command-node" }):up();
33 reply:tag("feature", { var = xmlns_cmd }):up();
34 reply:tag("feature", { var = "jabber:x:data" }):up();
37 return origin.send(st.error_reply(stanza, "auth", "forbidden", "This item is not available to you"));
39 elseif node == xmlns_cmd then
40 reply:tag("identity", { name = "Ad-Hoc Commands",
41 category = "automation", type = "command-list" }):up();
46 module:hook("host-disco-items-node", function (event)
47 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
48 if node ~= xmlns_cmd then
52 local from = stanza.attr.from;
53 local admin = is_admin(from, stanza.attr.to);
54 local global_admin = is_admin(from);
55 local username, hostname = jid_split(from);
56 local nodes = array_collect(keys(commands)):sort();
57 for _, node in ipairs(nodes) do
58 local command = commands[node];
59 if (command.permission == "admin" and admin)
60 or (command.permission == "global_admin" and global_admin)
61 or (command.permission == "local_user" and hostname == module.host)
62 or (command.permission == "user") then
63 reply:tag("item", { name = command.name,
64 node = node, jid = module:get_host() });
71 module:hook("iq/host/"..xmlns_cmd..":command", function (event)
72 local origin, stanza = event.origin, event.stanza;
73 if stanza.attr.type == "set" then
74 local node = stanza.tags[1].attr.node
75 local command = commands[node];
77 local from = stanza.attr.from;
78 local admin = is_admin(from, stanza.attr.to);
79 local global_admin = is_admin(from);
80 local username, hostname = jid_split(from);
81 if (command.permission == "admin" and not admin)
82 or (command.permission == "global_admin" and not global_admin)
83 or (command.permission == "local_user" and hostname ~= module.host) then
84 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
85 :add_child(commands[node]:cmdtag("canceled")
86 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
89 -- User has permission now execute the command
90 return adhoc_handle_cmd(commands[node], origin, stanza);
95 local function adhoc_added(event)
96 local item = event.item;
97 commands[item.node] = item;
100 local function adhoc_removed(event)
101 commands[event.item.node] = nil;
104 module:handle_items("adhoc", adhoc_added, adhoc_removed);
105 module:handle_items("adhoc-provider", adhoc_added, adhoc_removed);