Software /
code /
prosody-modules
Annotate
mod_adhoc/adhoc/mod_adhoc.lua @ 121:a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
mod_adhoc_cmd_*: Update to work with aforementioned change
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Fri, 22 Jan 2010 04:25:58 +0100 |
parent | 109:9b63fd1196c0 |
child | 125:8111c8a9e1ad |
rev | line source |
---|---|
6 | 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"; | |
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; |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
109
diff
changeset
|
9 local adhoc_handle_cmd = module:require "adhoc".handle_cmd; |
6 | 10 local commands = {}; |
11 | |
12 module:add_feature("http://jabber.org/protocol/commands"); | |
13 | |
14 module:hook("iq/host/http://jabber.org/protocol/disco#items:query", function (event) | |
15 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
|
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? |
6 | 17 if stanza.attr.type == "get" and stanza.tags[1].attr.node and stanza.tags[1].attr.node == "http://jabber.org/protocol/commands" then |
18 reply = st.reply(stanza); | |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
109
diff
changeset
|
19 reply:tag("query", {xmlns="http://jabber.org/protocol/disco#items", node="http://jabber.org/protocol/commands"}); |
6 | 20 for i = 1, #commands do |
21 -- 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
|
22 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
|
23 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
|
24 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
|
25 end |
6 | 26 end |
27 origin.send(reply); | |
28 return true; | |
29 end | |
30 end, 500); | |
31 | |
32 module:hook("iq/host", function (event) | |
33 local origin, stanza = event.origin, event.stanza; | |
34 if stanza.attr.type == "set" and stanza.tags[1] and stanza.tags[1].name == "command" then | |
35 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
|
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? |
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
37 for i = 1, #commands do |
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
38 if commands[i].node == node then |
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
39 -- 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
|
40 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
|
41 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
|
42 :add_child(commands[i]:cmdtag("canceled") |
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
43 :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
|
44 return true |
6 | 45 end |
93
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
46 -- User has permission now execute the command |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
109
diff
changeset
|
47 return adhoc_handle_cmd(commands[i], origin, stanza); |
6 | 48 end |
93
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
49 end |
6 | 50 end |
51 end, 500); | |
52 | |
53 module:hook("item-added/adhoc", function (event) | |
109
9b63fd1196c0
mod_adhoc_cmd*: Don't explicitly remove items. Handled by Prosody itself already
Florian Zeitz <florob@babelmonkeys.de>
parents:
93
diff
changeset
|
54 commands[ #commands + 1] = event.item; |
6 | 55 end, 500); |
9 | 56 |
57 local _G = _G; | |
58 local t_remove = _G.table.remove; | |
59 module:hook("item-removed/adhoc", function (event) | |
60 for i = 1, #commands do | |
61 if commands[i].node == event.item.node then | |
62 t_remove(commands, i); | |
28
b9d063dd16d5
mod_adhoc, mod_adhoc_cmd_ping: Code cleanup
Florian Zeitz <florob@babelmonkeys.de>
parents:
9
diff
changeset
|
63 break; |
9 | 64 end |
65 end | |
66 end, 500); |