Mercurial > prosody-hg
comparison plugins/adhoc/mod_adhoc.lua @ 3220:b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 09 Jun 2010 21:27:43 +0100 |
| parents | |
| children | ad3fbed1dda5 |
comparison
equal
deleted
inserted
replaced
| 3219:fd06023cbdcc | 3220:b3772f9bc359 |
|---|---|
| 1 -- Copyright (C) 2009 Thilo Cestonaro | |
| 2 -- | |
| 3 -- This file is MIT/X11 licensed. Please see the | |
| 4 -- COPYING file in the source package for more information. | |
| 5 -- | |
| 6 | |
| 7 local st = require "util.stanza"; | |
| 8 local is_admin = require "core.usermanager".is_admin; | |
| 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"; | |
| 12 local commands = {}; | |
| 13 | |
| 14 module:add_feature(xmlns_cmd); | |
| 15 | |
| 16 module:hook("iq/host/"..xmlns_disco.."#items:query", function (event) | |
| 17 local origin, stanza = event.origin, event.stanza; | |
| 18 -- TODO: Is this correct, or should is_admin be changed? | |
| 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 | |
| 23 reply = st.reply(stanza); | |
| 24 reply:tag("query", { xmlns = xmlns_disco.."#items", | |
| 25 node = xmlns_cmd }); | |
| 26 for node, command in pairs(commands) do | |
| 27 if (command.permission == "admin" and privileged) | |
| 28 or (command.permission == "user") then | |
| 29 reply:tag("item", { name = command.name, | |
| 30 node = node, jid = module:get_host() }); | |
| 31 reply:up(); | |
| 32 end | |
| 33 end | |
| 34 origin.send(reply); | |
| 35 return true; | |
| 36 end | |
| 37 end, 500); | |
| 38 | |
| 39 module:hook("iq/host", function (event) | |
| 40 local origin, stanza = event.origin, event.stanza; | |
| 41 if stanza.attr.type == "set" and stanza.tags[1] | |
| 42 and stanza.tags[1].name == "command" then | |
| 43 local node = stanza.tags[1].attr.node | |
| 44 -- TODO: Is this correct, or should is_admin be changed? | |
| 45 local privileged = is_admin(event.stanza.attr.from) | |
| 46 or is_admin(stanza.attr.from, stanza.attr.to); | |
| 47 if commands[node] then | |
| 48 if commands[node].permission == "admin" | |
| 49 and not privileged then | |
| 50 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up() | |
| 51 :add_child(commands[node]:cmdtag("canceled") | |
| 52 :tag("note", {type="error"}):text("You don't have permission to execute this command"))); | |
| 53 return true | |
| 54 end | |
| 55 -- User has permission now execute the command | |
| 56 return adhoc_handle_cmd(commands[node], origin, stanza); | |
| 57 end | |
| 58 end | |
| 59 end, 500); | |
| 60 | |
| 61 module:hook("item-added/adhoc", function (event) | |
| 62 commands[event.item.node] = event.item; | |
| 63 end, 500); | |
| 64 | |
| 65 module:hook("item-removed/adhoc", function (event) | |
| 66 commands[event.item.node] = nil; | |
| 67 end, 500); |
