1 -- Copyright (C) 2009 Thilo Cestonaro
2 -- Copyright (C) 2009-2010 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 is_admin = require "core.usermanager".is_admin;
10 local adhoc_handle_cmd = module:require "adhoc".handle_cmd;
11 local xmlns_cmd = "http://jabber.org/protocol/commands";
12 local xmlns_disco = "http://jabber.org/protocol/disco";
15 module:add_feature(xmlns_cmd);
17 module:hook("iq/host/"..xmlns_disco.."#info:query", function (event)
18 local origin, stanza = event.origin, event.stanza;
19 local node = stanza.tags[1].attr.node;
20 if stanza.attr.type == "get" and node then
21 if commands[node] then
22 local privileged = is_admin(stanza.attr.from, stanza.attr.to);
23 if (commands[node].permission == "admin" and privileged)
24 or (commands[node].permission == "user") then
25 reply = st.reply(stanza);
26 reply:tag("query", { xmlns = xmlns_disco.."#info",
28 reply:tag("identity", { name = commands[node].name,
29 category = "automation", type = "command-node" }):up();
30 reply:tag("feature", { var = xmlns_cmd }):up();
31 reply:tag("feature", { var = "jabber:x:data" }):up();
33 reply = st.error_reply(stanza, "auth", "forbidden", "This item is not available to you");
37 elseif node == xmlns_cmd then
38 reply = st.reply(stanza);
39 reply:tag("query", { xmlns = xmlns_disco.."#info",
41 reply:tag("identity", { name = "Ad-Hoc Commands",
42 category = "automation", type = "command-list" }):up();
50 module:hook("iq/host/"..xmlns_disco.."#items:query", function (event)
51 local origin, stanza = event.origin, event.stanza;
52 if stanza.attr.type == "get" and stanza.tags[1].attr.node
53 and stanza.tags[1].attr.node == xmlns_cmd then
54 local privileged = is_admin(stanza.attr.from, stanza.attr.to);
55 reply = st.reply(stanza);
56 reply:tag("query", { xmlns = xmlns_disco.."#items",
58 for node, command in pairs(commands) do
59 if (command.permission == "admin" and privileged)
60 or (command.permission == "user") then
61 reply:tag("item", { name = command.name,
62 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 if commands[node] then
76 local privileged = is_admin(stanza.attr.from, stanza.attr.to);
77 if commands[node].permission == "admin"
78 and not privileged then
79 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
80 :add_child(commands[node]:cmdtag("canceled")
81 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
84 -- User has permission now execute the command
85 return adhoc_handle_cmd(commands[node], origin, stanza);
90 local function handle_item_added(item)
91 commands[item.node] = item;
94 module:hook("item-added/adhoc", function (event)
95 return handle_item_added(event.item);
98 module:hook("item-removed/adhoc", function (event)
99 commands[event.item.node] = nil;
102 -- Pick up any items that are already added
103 for _, item in ipairs(module:get_host_items("adhoc")) do
104 handle_item_added(item);