prosody: Enable storage manager.
[prosody.git] / plugins / adhoc / mod_adhoc.lua
1 -- Copyright (C) 2009 Thilo Cestonaro
2 -- 
3 -- This file is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6
7 local st = require "util.stanza";
8 local is_admin = require "core.usermanager".is_admin;
9 local adhoc_handle_cmd = module:require "adhoc".handle_cmd;
10 local xmlns_cmd = "http://jabber.org/protocol/commands";
11 local xmlns_disco = "http://jabber.org/protocol/disco";
12 local commands = {};
13
14 module:add_feature(xmlns_cmd);
15
16 module:hook("iq/host/"..xmlns_disco.."#items:query", function (event)
17         local origin, stanza = event.origin, event.stanza;
18         local privileged = is_admin(stanza.attr.from, stanza.attr.to);
19         if stanza.attr.type == "get" and stanza.tags[1].attr.node
20             and stanza.tags[1].attr.node == xmlns_cmd then
21                 reply = st.reply(stanza);
22                 reply:tag("query", { xmlns = xmlns_disco.."#items",
23                     node = xmlns_cmd });
24                 for node, command in pairs(commands) do
25                         if (command.permission == "admin" and privileged)
26                             or (command.permission == "user") then
27                                 reply:tag("item", { name = command.name,
28                                     node = node, jid = module:get_host() });
29                                 reply:up();
30                         end
31                 end
32                 origin.send(reply);
33                 return true;
34         end
35 end, 500);
36
37 module:hook("iq/host", function (event)
38         local origin, stanza = event.origin, event.stanza;
39         if stanza.attr.type == "set" and stanza.tags[1]
40             and stanza.tags[1].name == "command" then 
41                 local node = stanza.tags[1].attr.node
42                 -- TODO: Is this correct, or should is_admin be changed?
43                 local privileged = is_admin(event.stanza.attr.from)
44                     or is_admin(stanza.attr.from, stanza.attr.to);
45                 if commands[node] then
46                         if commands[node].permission == "admin"
47                             and not privileged then
48                                 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
49                                     :add_child(commands[node]:cmdtag("canceled")
50                                         :tag("note", {type="error"}):text("You don't have permission to execute this command")));
51                                 return true
52                         end
53                         -- User has permission now execute the command
54                         return adhoc_handle_cmd(commands[node], origin, stanza);
55                 end
56         end
57 end, 500);
58
59 local function handle_item_added(item)
60         commands[item.node] = item;
61 end
62
63 module:hook("item-added/adhoc", function (event)
64         return handle_item_added(event.item);
65 end, 500);
66
67 module:hook("item-removed/adhoc", function (event)
68         commands[event.item.node] = nil;
69 end, 500);
70
71 -- Pick up any items that are already added
72 for _, item in ipairs(module:get_host_items("adhoc")) do
73         handle_item_added(item);
74 end