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 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 admin = is_admin(stanza.attr.from, stanza.attr.to);
55 local global_admin = is_admin(stanza.attr.from);
56 reply = st.reply(stanza);
57 reply:tag("query", { xmlns = xmlns_disco.."#items",
59 for node, command in pairs(commands) do
60 if (command.permission == "admin" and admin)
61 or (command.permission == "global_admin" and global_admin)
62 or (command.permission == "user") then
63 reply:tag("item", { name = command.name,
64 node = node, jid = module:get_host() });
73 module:hook("iq/host/"..xmlns_cmd..":command", function (event)
74 local origin, stanza = event.origin, event.stanza;
75 if stanza.attr.type == "set" then
76 local node = stanza.tags[1].attr.node
77 if commands[node] then
78 local admin = is_admin(stanza.attr.from, stanza.attr.to);
79 local global_admin = is_admin(stanza.attr.from);
80 if (commands[node].permission == "admin" and not admin)
81 or (commands[node].permission == "global_admin" and not global_admin) then
82 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
83 :add_child(commands[node]:cmdtag("canceled")
84 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
87 -- User has permission now execute the command
88 return adhoc_handle_cmd(commands[node], origin, stanza);
93 local function adhoc_added(event)
94 local item = event.item;
95 commands[item.node] = item;
98 local function adhoc_removed(event)
99 commands[event.item.node] = nil;
102 module:handle_items("adhoc", adhoc_added, adhoc_removed);
103 module:handle_items("adhoc-provider", adhoc_added, adhoc_removed);