mod_presence: Re-probe for contacts presence after outgoing 'subscribed' (fixes ...
[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 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";
13 local commands = {};
14
15 module:add_feature(xmlns_cmd);
16
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",
27                                     node = node });
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();
32                         else
33                                 reply = st.error_reply(stanza, "auth", "forbidden", "This item is not available to you");
34                         end
35                         origin.send(reply);
36                         return true;
37                 elseif node == xmlns_cmd then
38                         reply = st.reply(stanza);
39                         reply:tag("query", { xmlns = xmlns_disco.."#info",
40                             node = node });
41                         reply:tag("identity", { name = "Ad-Hoc Commands",
42                             category = "automation", type = "command-list" }):up();
43                         origin.send(reply);
44                         return true;
45
46                 end
47         end
48 end);
49
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",
58                     node = xmlns_cmd });
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() });
65                                 reply:up();
66                         end
67                 end
68                 origin.send(reply);
69                 return true;
70         end
71 end, 500);
72
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")));
85                                 return true
86                         end
87                         -- User has permission now execute the command
88                         return adhoc_handle_cmd(commands[node], origin, stanza);
89                 end
90         end
91 end, 500);
92
93 local function adhoc_added(event)
94         local item = event.item;
95         commands[item.node] = item;
96 end
97
98 local function adhoc_removed(event)
99         commands[event.item.node] = nil;
100 end
101
102 module:handle_items("adhoc", adhoc_added, adhoc_removed);
103 module:handle_items("adhoc-provider", adhoc_added, adhoc_removed);