Mercurial > prosody-hg
comparison plugins/adhoc/mod_adhoc.lua @ 5761:91f8cd53584c
mod_adhoc: Use mod_disco for disco handling
| author | Florian Zeitz <florob@babelmonkeys.de> |
|---|---|
| date | Wed, 24 Jul 2013 22:58:44 +0200 |
| parents | e599d9a367cf |
| children | 785da1854eb9 |
comparison
equal
deleted
inserted
replaced
| 5760:e599d9a367cf | 5761:91f8cd53584c |
|---|---|
| 9 local keys = require "util.iterators".keys; | 9 local keys = require "util.iterators".keys; |
| 10 local array_collect = require "util.array".collect; | 10 local array_collect = require "util.array".collect; |
| 11 local is_admin = require "core.usermanager".is_admin; | 11 local is_admin = require "core.usermanager".is_admin; |
| 12 local adhoc_handle_cmd = module:require "adhoc".handle_cmd; | 12 local adhoc_handle_cmd = module:require "adhoc".handle_cmd; |
| 13 local xmlns_cmd = "http://jabber.org/protocol/commands"; | 13 local xmlns_cmd = "http://jabber.org/protocol/commands"; |
| 14 local xmlns_disco = "http://jabber.org/protocol/disco"; | |
| 15 local commands = {}; | 14 local commands = {}; |
| 16 | 15 |
| 17 module:add_feature(xmlns_cmd); | 16 module:add_feature(xmlns_cmd); |
| 18 | 17 |
| 19 module:hook("iq/host/"..xmlns_disco.."#info:query", function (event) | 18 module:hook("host-disco-info-node", function (event) |
| 20 local origin, stanza = event.origin, event.stanza; | 19 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node; |
| 21 local node = stanza.tags[1].attr.node; | 20 if commands[node] then |
| 22 if stanza.attr.type == "get" and node then | 21 local privileged = is_admin(stanza.attr.from, stanza.attr.to); |
| 23 if commands[node] then | 22 local global_admin = is_admin(stanza.attr.from); |
| 24 local privileged = is_admin(stanza.attr.from, stanza.attr.to); | 23 local command = commands[node]; |
| 25 if (commands[node].permission == "admin" and privileged) | 24 if (command.permission == "admin" and privileged) |
| 26 or (commands[node].permission == "user") then | 25 or (command.permission == "global_admin" and global_admin) |
| 27 reply = st.reply(stanza); | 26 or (command.permission == "user") then |
| 28 reply:tag("query", { xmlns = xmlns_disco.."#info", | 27 reply:tag("identity", { name = command.name, |
| 29 node = node }); | 28 category = "automation", type = "command-node" }):up(); |
| 30 reply:tag("identity", { name = commands[node].name, | 29 reply:tag("feature", { var = xmlns_cmd }):up(); |
| 31 category = "automation", type = "command-node" }):up(); | 30 reply:tag("feature", { var = "jabber:x:data" }):up(); |
| 32 reply:tag("feature", { var = xmlns_cmd }):up(); | 31 event.exists = true; |
| 33 reply:tag("feature", { var = "jabber:x:data" }):up(); | 32 else |
| 34 else | 33 return origin.send(st.error_reply(stanza, "auth", "forbidden", "This item is not available to you")); |
| 35 reply = st.error_reply(stanza, "auth", "forbidden", "This item is not available to you"); | |
| 36 end | |
| 37 origin.send(reply); | |
| 38 return true; | |
| 39 elseif node == xmlns_cmd then | |
| 40 reply = st.reply(stanza); | |
| 41 reply:tag("query", { xmlns = xmlns_disco.."#info", | |
| 42 node = node }); | |
| 43 reply:tag("identity", { name = "Ad-Hoc Commands", | |
| 44 category = "automation", type = "command-list" }):up(); | |
| 45 origin.send(reply); | |
| 46 return true; | |
| 47 | |
| 48 end | 34 end |
| 35 elseif node == xmlns_cmd then | |
| 36 reply:tag("identity", { name = "Ad-Hoc Commands", | |
| 37 category = "automation", type = "command-list" }):up(); | |
| 38 event.exists = true; | |
| 49 end | 39 end |
| 50 end); | 40 end); |
| 51 | 41 |
| 52 module:hook("iq/host/"..xmlns_disco.."#items:query", function (event) | 42 module:hook("host-disco-items-node", function (event) |
| 53 local origin, stanza = event.origin, event.stanza; | 43 local stanza, origin, reply, node = event.stanza, event.origin, event.reply, event.node; |
| 54 if stanza.attr.type == "get" and stanza.tags[1].attr.node | 44 if node ~= xmlns_cmd then |
| 55 and stanza.tags[1].attr.node == xmlns_cmd then | 45 return; |
| 56 local admin = is_admin(stanza.attr.from, stanza.attr.to); | 46 end |
| 57 local global_admin = is_admin(stanza.attr.from); | 47 |
| 58 reply = st.reply(stanza); | 48 local admin = is_admin(stanza.attr.from, stanza.attr.to); |
| 59 reply:tag("query", { xmlns = xmlns_disco.."#items", | 49 local global_admin = is_admin(stanza.attr.from); |
| 60 node = xmlns_cmd }); | 50 local nodes = array_collect(keys(commands)):sort(); |
| 61 local nodes = array_collect(keys(commands)):sort(); | 51 for _, node in ipairs(nodes) do |
| 62 for _, node in ipairs(nodes) do | 52 local command = commands[node]; |
| 63 local command = commands[node]; | 53 if (command.permission == "admin" and admin) |
| 64 if (command.permission == "admin" and admin) | 54 or (command.permission == "global_admin" and global_admin) |
| 65 or (command.permission == "global_admin" and global_admin) | 55 or (command.permission == "user") then |
| 66 or (command.permission == "user") then | 56 reply:tag("item", { name = command.name, |
| 67 reply:tag("item", { name = command.name, | 57 node = node, jid = module:get_host() }); |
| 68 node = node, jid = module:get_host() }); | 58 reply:up(); |
| 69 reply:up(); | |
| 70 end | |
| 71 end | 59 end |
| 72 origin.send(reply); | |
| 73 return true; | |
| 74 end | 60 end |
| 75 end, 500); | 61 event.exists = true; |
| 62 end); | |
| 76 | 63 |
| 77 module:hook("iq/host/"..xmlns_cmd..":command", function (event) | 64 module:hook("iq/host/"..xmlns_cmd..":command", function (event) |
| 78 local origin, stanza = event.origin, event.stanza; | 65 local origin, stanza = event.origin, event.stanza; |
| 79 if stanza.attr.type == "set" then | 66 if stanza.attr.type == "set" then |
| 80 local node = stanza.tags[1].attr.node | 67 local node = stanza.tags[1].attr.node |
