author | Florian Zeitz <florob@babelmonkeys.de> |
Wed, 24 Jul 2013 22:08:07 +0200 | |
changeset 5760 | e599d9a367cf |
parent 4926 | 58714123f600 |
child 5761 | 91f8cd53584c |
permissions | -rw-r--r-- |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
-- Copyright (C) 2009 Thilo Cestonaro |
4291
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
2 |
-- Copyright (C) 2009-2011 Florian Zeitz |
3456
1201a743fe63
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
3286
diff
changeset
|
3 |
-- |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
-- This file is MIT/X11 licensed. Please see the |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 |
-- COPYING file in the source package for more information. |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
-- |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
|
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
local st = require "util.stanza"; |
5760
e599d9a367cf
mod_adhoc: Sort commands by node. This guarantees the order remains the same across restarts etc.
Florian Zeitz <florob@babelmonkeys.de>
parents:
4926
diff
changeset
|
9 |
local keys = require "util.iterators".keys; |
e599d9a367cf
mod_adhoc: Sort commands by node. This guarantees the order remains the same across restarts etc.
Florian Zeitz <florob@babelmonkeys.de>
parents:
4926
diff
changeset
|
10 |
local array_collect = require "util.array".collect; |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
local is_admin = require "core.usermanager".is_admin; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
local adhoc_handle_cmd = module:require "adhoc".handle_cmd; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
local xmlns_cmd = "http://jabber.org/protocol/commands"; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
local xmlns_disco = "http://jabber.org/protocol/disco"; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 |
local commands = {}; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 |
|
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 |
module:add_feature(xmlns_cmd); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
|
3457
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
19 |
module:hook("iq/host/"..xmlns_disco.."#info:query", function (event) |
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
20 |
local origin, stanza = event.origin, event.stanza; |
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
21 |
local node = stanza.tags[1].attr.node; |
3511
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
22 |
if stanza.attr.type == "get" and node then |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
23 |
if commands[node] then |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
24 |
local privileged = is_admin(stanza.attr.from, stanza.attr.to); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
25 |
if (commands[node].permission == "admin" and privileged) |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
26 |
or (commands[node].permission == "user") then |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
27 |
reply = st.reply(stanza); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
28 |
reply:tag("query", { xmlns = xmlns_disco.."#info", |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
29 |
node = node }); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
30 |
reply:tag("identity", { name = commands[node].name, |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
31 |
category = "automation", type = "command-node" }):up(); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
32 |
reply:tag("feature", { var = xmlns_cmd }):up(); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
33 |
reply:tag("feature", { var = "jabber:x:data" }):up(); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
34 |
else |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
35 |
reply = st.error_reply(stanza, "auth", "forbidden", "This item is not available to you"); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
36 |
end |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
37 |
origin.send(reply); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
38 |
return true; |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
39 |
elseif node == xmlns_cmd then |
3457
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
40 |
reply = st.reply(stanza); |
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
41 |
reply:tag("query", { xmlns = xmlns_disco.."#info", |
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
42 |
node = node }); |
3511
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
43 |
reply:tag("identity", { name = "Ad-Hoc Commands", |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
44 |
category = "automation", type = "command-list" }):up(); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
45 |
origin.send(reply); |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
46 |
return true; |
4495403470cb
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
3485
diff
changeset
|
47 |
|
3457
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
48 |
end |
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
49 |
end |
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
50 |
end); |
24d2c9be0149
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
3456
diff
changeset
|
51 |
|
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 |
module:hook("iq/host/"..xmlns_disco.."#items:query", function (event) |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
local origin, stanza = event.origin, event.stanza; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
if stanza.attr.type == "get" and stanza.tags[1].attr.node |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 |
and stanza.tags[1].attr.node == xmlns_cmd then |
4291
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
56 |
local admin = is_admin(stanza.attr.from, stanza.attr.to); |
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
57 |
local global_admin = is_admin(stanza.attr.from); |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 |
reply = st.reply(stanza); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 |
reply:tag("query", { xmlns = xmlns_disco.."#items", |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 |
node = xmlns_cmd }); |
5760
e599d9a367cf
mod_adhoc: Sort commands by node. This guarantees the order remains the same across restarts etc.
Florian Zeitz <florob@babelmonkeys.de>
parents:
4926
diff
changeset
|
61 |
local nodes = array_collect(keys(commands)):sort(); |
e599d9a367cf
mod_adhoc: Sort commands by node. This guarantees the order remains the same across restarts etc.
Florian Zeitz <florob@babelmonkeys.de>
parents:
4926
diff
changeset
|
62 |
for _, node in ipairs(nodes) do |
e599d9a367cf
mod_adhoc: Sort commands by node. This guarantees the order remains the same across restarts etc.
Florian Zeitz <florob@babelmonkeys.de>
parents:
4926
diff
changeset
|
63 |
local command = commands[node]; |
4291
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
64 |
if (command.permission == "admin" and admin) |
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
65 |
or (command.permission == "global_admin" and global_admin) |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 |
or (command.permission == "user") then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 |
reply:tag("item", { name = command.name, |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 |
node = node, jid = module:get_host() }); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 |
reply:up(); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 |
end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 |
end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
origin.send(reply); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 |
return true; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 |
end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 |
end, 500); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 |
|
3456
1201a743fe63
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
3286
diff
changeset
|
77 |
module:hook("iq/host/"..xmlns_cmd..":command", function (event) |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 |
local origin, stanza = event.origin, event.stanza; |
3456
1201a743fe63
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
3286
diff
changeset
|
79 |
if stanza.attr.type == "set" then |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 |
local node = stanza.tags[1].attr.node |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 |
if commands[node] then |
4291
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
82 |
local admin = is_admin(stanza.attr.from, stanza.attr.to); |
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
83 |
local global_admin = is_admin(stanza.attr.from); |
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
84 |
if (commands[node].permission == "admin" and not admin) |
122f142da281
mod_adhoc: Add support for commands only executable by global administrators
Florian Zeitz <florob@babelmonkeys.de>
parents:
3511
diff
changeset
|
85 |
or (commands[node].permission == "global_admin" and not global_admin) then |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 |
origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up() |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 |
:add_child(commands[node]:cmdtag("canceled") |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
:tag("note", {type="error"}):text("You don't have permission to execute this command"))); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 |
return true |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 |
end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 |
-- User has permission now execute the command |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 |
return adhoc_handle_cmd(commands[node], origin, stanza); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 |
end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 |
end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 |
end, 500); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 |
|
4450
15547fba1f09
mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents:
4291
diff
changeset
|
97 |
local function adhoc_added(event) |
15547fba1f09
mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents:
4291
diff
changeset
|
98 |
local item = event.item; |
3231
ad3fbed1dda5
mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents:
3220
diff
changeset
|
99 |
commands[item.node] = item; |
ad3fbed1dda5
mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents:
3220
diff
changeset
|
100 |
end |
ad3fbed1dda5
mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents:
3220
diff
changeset
|
101 |
|
4450
15547fba1f09
mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents:
4291
diff
changeset
|
102 |
local function adhoc_removed(event) |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 |
commands[event.item.node] = nil; |
4450
15547fba1f09
mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents:
4291
diff
changeset
|
104 |
end |
3231
ad3fbed1dda5
mod_adhoc: Scan through list of items on load, in case items have been added before we were loaded
Matthew Wild <mwild1@gmail.com>
parents:
3220
diff
changeset
|
105 |
|
4450
15547fba1f09
mod_adhoc: Use module:handle_items()
Matthew Wild <mwild1@gmail.com>
parents:
4291
diff
changeset
|
106 |
module:handle_items("adhoc", adhoc_added, adhoc_removed); |
4926
58714123f600
mod_adhoc, mod_admin_adhoc, mod_announce: Use module:provides() to manage Ad-Hoc commands
Florian Zeitz <florob@babelmonkeys.de>
parents:
4450
diff
changeset
|
107 |
module:handle_items("adhoc-provider", adhoc_added, adhoc_removed); |