Merge 0.9->0.10
[prosody.git] / plugins / adhoc / mod_adhoc.lua
1 -- Copyright (C) 2009 Thilo Cestonaro
2 -- Copyright (C) 2009-2011 Florian Zeitz
3 --
4 -- This file is MIT/X11 licensed. Please see the
5 -- COPYING file in the source package for more information.
6 --
7
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";
15 local commands = {};
16
17 module:add_feature(xmlns_cmd);
18
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();
35                         event.exists = true;
36                 else
37                         origin.send(st.error_reply(stanza, "auth", "forbidden", "This item is not available to you"));
38                         return true;
39                 end
40         elseif node == xmlns_cmd then
41                 reply:tag("identity", { name = "Ad-Hoc Commands",
42                     category = "automation", type = "command-list" }):up();
43                     event.exists = true;
44         end
45 end);
46
47 module:hook("host-disco-items-node", function (event)
48         local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
49         if node ~= xmlns_cmd then
50                 return;
51         end
52
53         local from = stanza.attr.from;
54         local admin = is_admin(from, stanza.attr.to);
55         local global_admin = is_admin(from);
56         local username, hostname = jid_split(from);
57         local nodes = array_collect(keys(commands)):sort();
58         for _, node in ipairs(nodes) do
59                 local command = commands[node];
60                 if (command.permission == "admin" and admin)
61                     or (command.permission == "global_admin" and global_admin)
62                     or (command.permission == "local_user" and hostname == module.host)
63                     or (command.permission == "user") then
64                         reply:tag("item", { name = command.name,
65                             node = node, jid = module:get_host() });
66                         reply:up();
67                 end
68         end
69         event.exists = true;
70 end);
71
72 module:hook("iq/host/"..xmlns_cmd..":command", function (event)
73         local origin, stanza = event.origin, event.stanza;
74         if stanza.attr.type == "set" then
75                 local node = stanza.tags[1].attr.node
76                 local command = commands[node];
77                 if command then
78                         local from = stanza.attr.from;
79                         local admin = is_admin(from, stanza.attr.to);
80                         local global_admin = is_admin(from);
81                         local username, hostname = jid_split(from);
82                         if (command.permission == "admin" and not admin)
83                             or (command.permission == "global_admin" and not global_admin)
84                             or (command.permission == "local_user" and hostname ~= module.host) then
85                                 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
86                                     :add_child(commands[node]:cmdtag("canceled")
87                                         :tag("note", {type="error"}):text("You don't have permission to execute this command")));
88                                 return true
89                         end
90                         -- User has permission now execute the command
91                         adhoc_handle_cmd(commands[node], origin, stanza);
92                         return true;
93                 end
94         end
95 end, 500);
96
97 local function adhoc_added(event)
98         local item = event.item;
99         commands[item.node] = item;
100 end
101
102 local function adhoc_removed(event)
103         commands[event.item.node] = nil;
104 end
105
106 module:handle_items("adhoc", adhoc_added, adhoc_removed);
107 module:handle_items("adhoc-provider", adhoc_added, adhoc_removed);