annotate mod_adhoc/adhoc/mod_adhoc.lua @ 95:e704834c5613

mod_proxy65: hopefully remove a memory leak (thx waqas!)
author Thilo Cestonaro <thilo@cestona.ro>
date Tue, 17 Nov 2009 21:20:41 +0100
parents 611d16867410
children 9b63fd1196c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
1 -- Copyright (C) 2009 Thilo Cestonaro
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
2 --
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
3 -- This file is MIT/X11 licensed. Please see the
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
5 --
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
6
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
7 local st = require "util.stanza";
43
adc9eff8adb2 mod_adhoc, mod_adhoc_cmd_admin: Show only commands they may execute to the user
Florian Zeitz <florob@babelmonkeys.de>
parents: 36
diff changeset
8 local is_admin = require "core.usermanager".is_admin;
6
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
9 local commands = {};
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
10
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
11 module:add_feature("http://jabber.org/protocol/commands");
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
12
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
13 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function (event)
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
14 local origin, stanza = event.origin, event.stanza;
93
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
15 local privileged = is_admin(event.stanza.attr.from) or is_admin(stanza.attr.from, stanza.attr.to); -- TODO: Is this correct, or should is_admin be changed?
6
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
16 if stanza.attr.type == "get" and stanza.tags[1].attr.node and stanza.tags[1].attr.node == "http://jabber.org/protocol/commands" then
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
17 reply = st.reply(stanza);
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
18 reply:tag("query", {xmlns="http://jabber.org/protocol/disco#items", node="http://jabber.org/protocol/commands"})
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
19 for i = 1, #commands do
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
20 -- module:log("info", "adding command %s", commands[i].name);
43
adc9eff8adb2 mod_adhoc, mod_adhoc_cmd_admin: Show only commands they may execute to the user
Florian Zeitz <florob@babelmonkeys.de>
parents: 36
diff changeset
21 if (commands[i].permission == "admin" and privileged) or (commands[i].permission == "user") then
adc9eff8adb2 mod_adhoc, mod_adhoc_cmd_admin: Show only commands they may execute to the user
Florian Zeitz <florob@babelmonkeys.de>
parents: 36
diff changeset
22 reply:tag("item", {name=commands[i].name, node=commands[i].node, jid=module:get_host()});
adc9eff8adb2 mod_adhoc, mod_adhoc_cmd_admin: Show only commands they may execute to the user
Florian Zeitz <florob@babelmonkeys.de>
parents: 36
diff changeset
23 reply:up();
adc9eff8adb2 mod_adhoc, mod_adhoc_cmd_admin: Show only commands they may execute to the user
Florian Zeitz <florob@babelmonkeys.de>
parents: 36
diff changeset
24 end
6
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
25 end
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
26 origin.send(reply);
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
27 return true;
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
28 end
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
29 end, 500);
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
30
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
31 module:hook("iq/host", function (event)
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
32 local origin, stanza = event.origin, event.stanza;
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
33 if stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "command" then
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
34 local node = stanza.tags[1].attr.node
93
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
35 local privileged = is_admin(event.stanza.attr.from) or is_admin(stanza.attr.from, stanza.attr.to); -- TODO: Is this correct, or should is_admin be changed?
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
36 for i = 1, #commands do
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
37 if commands[i].node == node then
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
38 -- check whether user has permission to execute this command first
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
39 if commands[i].permission == "admin" and not privileged then
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
40 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up()
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
41 :add_child(commands[i]:cmdtag("canceled")
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
42 :tag("note", {type="error"}):text("You don't have permission to execute this command")));
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
43 return true
6
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
44 end
93
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
45 -- User has permission now execute the command
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
46 return commands[i].handler(commands[i], origin, stanza);
6
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
47 end
93
611d16867410 mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents: 49
diff changeset
48 end
6
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
49 end
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
50 end, 500);
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
51
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
52 module:hook("item-added/adhoc", function (event)
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
53 commands[ # commands + 1] = event.item;
d497d5df360d adds mod_adhoc
Thilo Cestonaro <thilo@cestona.ro>
parents:
diff changeset
54 end, 500);
9
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
55
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
56 local _G = _G;
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
57 local t_remove = _G.table.remove;
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
58 module:hook("item-removed/adhoc", function (event)
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
59 for i = 1, #commands do
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
60 if commands[i].node == event.item.node then
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
61 t_remove(commands, i);
28
b9d063dd16d5 mod_adhoc, mod_adhoc_cmd_ping: Code cleanup
Florian Zeitz <florob@babelmonkeys.de>
parents: 9
diff changeset
62 break;
9
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
63 end
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
64 end
2be8bcce5b18 thx to Florob:
ephraim@errorm.fritz.box
parents: 6
diff changeset
65 end, 500);