Comparison

plugins/adhoc/mod_adhoc.lua @ 3456:1201a743fe63

mod_adhoc: Code restructuring
author Florian Zeitz <florob@babelmonkeys.de>
date Sun, 22 Aug 2010 20:48:47 +0200
parent 3286:e5234625fc42
child 3457:24d2c9be0149
comparison
equal deleted inserted replaced
3455:67cdc0366d46 3456:1201a743fe63
1 -- Copyright (C) 2009 Thilo Cestonaro 1 -- Copyright (C) 2009 Thilo Cestonaro
2 -- 2 -- Copyright (C) 2009-2010 Florian Zeitz
3 --
3 -- This file is MIT/X11 licensed. Please see the 4 -- This file is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information. 5 -- COPYING file in the source package for more information.
5 -- 6 --
6 7
7 local st = require "util.stanza"; 8 local st = require "util.stanza";
13 14
14 module:add_feature(xmlns_cmd); 15 module:add_feature(xmlns_cmd);
15 16
16 module:hook("iq/host/"..xmlns_disco.."#items:query", function (event) 17 module:hook("iq/host/"..xmlns_disco.."#items:query", function (event)
17 local origin, stanza = event.origin, event.stanza; 18 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 19 if stanza.attr.type == "get" and stanza.tags[1].attr.node
20 and stanza.tags[1].attr.node == xmlns_cmd then 20 and stanza.tags[1].attr.node == xmlns_cmd then
21 local privileged = is_admin(stanza.attr.from, stanza.attr.to);
21 reply = st.reply(stanza); 22 reply = st.reply(stanza);
22 reply:tag("query", { xmlns = xmlns_disco.."#items", 23 reply:tag("query", { xmlns = xmlns_disco.."#items",
23 node = xmlns_cmd }); 24 node = xmlns_cmd });
24 for node, command in pairs(commands) do 25 for node, command in pairs(commands) do
25 if (command.permission == "admin" and privileged) 26 if (command.permission == "admin" and privileged)
32 origin.send(reply); 33 origin.send(reply);
33 return true; 34 return true;
34 end 35 end
35 end, 500); 36 end, 500);
36 37
37 module:hook("iq/host", function (event) 38 module:hook("iq/host/"..xmlns_cmd..":command", function (event)
38 local origin, stanza = event.origin, event.stanza; 39 local origin, stanza = event.origin, event.stanza;
39 if stanza.attr.type == "set" and stanza.tags[1] 40 if stanza.attr.type == "set" then
40 and stanza.tags[1].name == "command" then
41 local node = stanza.tags[1].attr.node 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 42 if commands[node] then
43 local privileged = is_admin(stanza.attr.from, stanza.attr.to);
46 if commands[node].permission == "admin" 44 if commands[node].permission == "admin"
47 and not privileged then 45 and not privileged then
48 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up() 46 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") 47 :add_child(commands[node]:cmdtag("canceled")
50 :tag("note", {type="error"}):text("You don't have permission to execute this command"))); 48 :tag("note", {type="error"}):text("You don't have permission to execute this command")));