mod_csi_simple: Consider messages encrypted payload as important (fixes part of ...
[prosody.git] / plugins / adhoc / mod_adhoc.lua
blobbf1775b4cbd5cc7792e279a592c634e4bd9b6c38
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 --
8 local it = require "util.iterators";
9 local st = require "util.stanza";
10 local is_admin = require "core.usermanager".is_admin;
11 local jid_split = require "util.jid".split;
12 local adhoc_handle_cmd = module:require "adhoc".handle_cmd;
13 local xmlns_cmd = "http://jabber.org/protocol/commands";
14 local commands = {};
16 module:add_feature(xmlns_cmd);
18 module:hook("host-disco-info-node", function (event)
19 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node;
20 if commands[node] then
21 local from = stanza.attr.from;
22 local privileged = is_admin(from, stanza.attr.to);
23 local global_admin = is_admin(from);
24 local username, hostname = jid_split(from);
25 local command = commands[node];
26 if (command.permission == "admin" and privileged)
27 or (command.permission == "global_admin" and global_admin)
28 or (command.permission == "local_user" and hostname == module.host)
29 or (command.permission == "user") then
30 reply:tag("identity", { name = command.name,
31 category = "automation", type = "command-node" }):up();
32 reply:tag("feature", { var = xmlns_cmd }):up();
33 reply:tag("feature", { var = "jabber:x:data" }):up();
34 event.exists = true;
35 else
36 origin.send(st.error_reply(stanza, "auth", "forbidden", "This item is not available to you"));
37 return true;
38 end
39 elseif node == xmlns_cmd then
40 reply:tag("identity", { name = "Ad-Hoc Commands",
41 category = "automation", type = "command-list" }):up();
42 event.exists = true;
43 end
44 end);
46 module:hook("host-disco-items-node", function (event)
47 local stanza, reply, disco_node = event.stanza, event.reply, event.node;
48 if disco_node ~= xmlns_cmd then
49 return;
50 end
52 local from = stanza.attr.from;
53 local admin = is_admin(from, stanza.attr.to);
54 local global_admin = is_admin(from);
55 local username, hostname = jid_split(from);
56 for node, command in it.sorted_pairs(commands) do
57 if (command.permission == "admin" and admin)
58 or (command.permission == "global_admin" and global_admin)
59 or (command.permission == "local_user" and hostname == module.host)
60 or (command.permission == "user") then
61 reply:tag("item", { name = command.name,
62 node = node, jid = module:get_host() });
63 reply:up();
64 end
65 end
66 event.exists = true;
67 end);
69 module:hook("iq-set/host/"..xmlns_cmd..":command", function (event)
70 local origin, stanza = event.origin, event.stanza;
71 local node = stanza.tags[1].attr.node
72 local command = commands[node];
73 if command then
74 local from = stanza.attr.from;
75 local admin = is_admin(from, stanza.attr.to);
76 local global_admin = is_admin(from);
77 local username, hostname = jid_split(from);
78 if (command.permission == "admin" and not admin)
79 or (command.permission == "global_admin" and not global_admin)
80 or (command.permission == "local_user" and hostname ~= module.host) then
81 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
82 :add_child(commands[node]:cmdtag("canceled")
83 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
84 return true
85 end
86 -- User has permission now execute the command
87 adhoc_handle_cmd(commands[node], origin, stanza);
88 return true;
89 end
90 end, 500);
92 local function adhoc_added(event)
93 local item = event.item;
94 commands[item.node] = item;
95 end
97 local function adhoc_removed(event)
98 commands[event.item.node] = nil;
99 end
101 module:handle_items("adhoc", adhoc_added, adhoc_removed); -- COMPAT pre module:provides() introduced in 0.9
102 module:handle_items("adhoc-provider", adhoc_added, adhoc_removed);