Mercurial > prosody-modules
comparison mod_adhoc/adhoc/mod_adhoc.lua @ 125:8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Mon, 25 Jan 2010 01:42:21 +0100 |
parents | a9898f13c89e |
children | b3a68e71b8a1 |
comparison
equal
deleted
inserted
replaced
124:843cadf36306 | 125:8111c8a9e1ad |
---|---|
5 -- | 5 -- |
6 | 6 |
7 local st = require "util.stanza"; | 7 local st = require "util.stanza"; |
8 local is_admin = require "core.usermanager".is_admin; | 8 local is_admin = require "core.usermanager".is_admin; |
9 local adhoc_handle_cmd = module:require "adhoc".handle_cmd; | 9 local adhoc_handle_cmd = module:require "adhoc".handle_cmd; |
10 local xmlns_cmd = "http://jabber.org/protocol/commands"; | |
11 local xmlns_disco = "http://jabber.org/protocol/disco"; | |
10 local commands = {}; | 12 local commands = {}; |
11 | 13 |
12 module:add_feature("http://jabber.org/protocol/commands"); | 14 module:add_feature(xmlns_cmd); |
13 | 15 |
14 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function (event) | 16 module:hook("iq/host/"..xmlns_disco.."#items:query", function (event) |
15 local origin, stanza = event.origin, event.stanza; | 17 local origin, stanza = event.origin, event.stanza; |
16 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? | 18 -- TODO: Is this correct, or should is_admin be changed? |
17 if stanza.attr.type == "get" and stanza.tags[1].attr.node and stanza.tags[1].attr.node == "http://jabber.org/protocol/commands" then | 19 local privileged = is_admin(stanza.attr.from) |
20 or is_admin(stanza.attr.from, stanza.attr.to); | |
21 if stanza.attr.type == "get" and stanza.tags[1].attr.node | |
22 and stanza.tags[1].attr.node == xmlns_cmd then | |
18 reply = st.reply(stanza); | 23 reply = st.reply(stanza); |
19 reply:tag("query", {xmlns="http://jabber.org/protocol/disco#items", node="http://jabber.org/protocol/commands"}); | 24 reply:tag("query", { xmlns = xmlns_disco.."#items", |
20 for i = 1, #commands do | 25 node = xmlns_cmd }); |
21 -- module:log("info", "adding command %s", commands[i].name); | 26 for node, command in pairs(commands) do |
22 if (commands[i].permission == "admin" and privileged) or (commands[i].permission == "user") then | 27 if (command.permission == "admin" and privileged) |
23 reply:tag("item", {name=commands[i].name, node=commands[i].node, jid=module:get_host()}); | 28 or (command.permission == "user") then |
29 reply:tag("item", { name = command.name, | |
30 node = node, jid = module:get_host() }); | |
24 reply:up(); | 31 reply:up(); |
25 end | 32 end |
26 end | 33 end |
27 origin.send(reply); | 34 origin.send(reply); |
28 return true; | 35 return true; |
29 end | 36 end |
30 end, 500); | 37 end, 500); |
31 | 38 |
32 module:hook("iq/host", function (event) | 39 module:hook("iq/host", function (event) |
33 local origin, stanza = event.origin, event.stanza; | 40 local origin, stanza = event.origin, event.stanza; |
34 if stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "command" then | 41 if stanza.attr.type == "set" and stanza.tags[1] |
35 local node = stanza.tags[1].attr.node | 42 and stanza.tags[1].name == "command" then |
36 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? | 43 local node = stanza.tags[1].attr.node |
37 for i = 1, #commands do | 44 -- TODO: Is this correct, or should is_admin be changed? |
38 if commands[i].node == node then | 45 local privileged = is_admin(event.stanza.attr.from) |
39 -- check whether user has permission to execute this command first | 46 or is_admin(stanza.attr.from, stanza.attr.to); |
40 if commands[i].permission == "admin" and not privileged then | 47 if commands[node] then |
48 if commands[node].permission == "admin" | |
49 and not privileged then | |
41 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up() | 50 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up() |
42 :add_child(commands[i]:cmdtag("canceled") | 51 :add_child(commands[node]:cmdtag("canceled") |
43 :tag("note", {type="error"}):text("You don't have permission to execute this command"))); | 52 :tag("note", {type="error"}):text("You don't have permission to execute this command"))); |
44 return true | 53 return true |
45 end | 54 end |
46 -- User has permission now execute the command | 55 -- User has permission now execute the command |
47 return adhoc_handle_cmd(commands[i], origin, stanza); | 56 return adhoc_handle_cmd(commands[node], origin, stanza); |
48 end | 57 end |
49 end | 58 end |
50 end | |
51 end, 500); | 59 end, 500); |
52 | 60 |
53 module:hook("item-added/adhoc", function (event) | 61 module:hook("item-added/adhoc", function (event) |
54 commands[ #commands + 1] = event.item; | 62 commands[event.item.node] = event.item; |
55 end, 500); | 63 end, 500); |
56 | 64 |
57 local _G = _G; | |
58 local t_remove = _G.table.remove; | |
59 module:hook("item-removed/adhoc", function (event) | 65 module:hook("item-removed/adhoc", function (event) |
60 for i = 1, #commands do | 66 commands[event.item.node] = nil; |
61 if commands[i].node == event.item.node then | |
62 t_remove(commands, i); | |
63 break; | |
64 end | |
65 end | |
66 end, 500); | 67 end, 500); |