Mercurial > prosody-modules
annotate mod_adhoc/adhoc/mod_adhoc.lua @ 428:a46c2326eed7
mod_auth_wordpress: Update Copyright
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 11 Sep 2011 23:30:06 +0200 |
parents | 9b4a114b2fe6 |
children |
rev | line source |
---|---|
6 | 1 -- Copyright (C) 2009 Thilo Cestonaro |
169
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
125
diff
changeset
|
2 -- Copyright (C) 2009-2010 Florian Zeitz |
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
125
diff
changeset
|
3 -- |
6 | 4 -- This file is MIT/X11 licensed. Please see the |
5 -- COPYING file in the source package for more information. | |
6 -- | |
7 | |
8 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
|
9 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
|
10 local adhoc_handle_cmd = module:require "adhoc".handle_cmd; |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
11 local xmlns_cmd = "http://jabber.org/protocol/commands"; |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
12 local xmlns_disco = "http://jabber.org/protocol/disco"; |
6 | 13 local commands = {}; |
14 | |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
15 module:add_feature(xmlns_cmd); |
6 | 16 |
246
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
17 module:hook("iq/host/"..xmlns_disco.."#info:query", function (event) |
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
18 local origin, stanza = event.origin, event.stanza; |
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
19 local node = stanza.tags[1].attr.node; |
254
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
20 if stanza.attr.type == "get" and node then |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
21 if commands[node] then |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
22 -- Required for Prosody <= 0.7 |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
23 local privileged = is_admin(stanza.attr.from) |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
24 or is_admin(stanza.attr.from, stanza.attr.to); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
25 if (commands[node].permission == "admin" and privileged) |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
26 or (commands[node].permission == "user") then |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
27 reply = st.reply(stanza); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
28 reply:tag("query", { xmlns = xmlns_disco.."#info", |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
29 node = node }); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
30 reply:tag("identity", { name = commands[node].name, |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
31 category = "automation", type = "command-node" }):up(); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
32 reply:tag("feature", { var = xmlns_cmd }):up(); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
33 reply:tag("feature", { var = "jabber:x:data" }):up(); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
34 else |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
35 reply = st.error_reply(stanza, "auth", "forbidden", "This item is not available to you"); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
36 end |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
37 origin.send(reply); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
38 return true; |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
39 elseif node == xmlns_cmd then |
246
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
40 reply = st.reply(stanza); |
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
41 reply:tag("query", { xmlns = xmlns_disco.."#info", |
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
42 node = node }); |
254
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
43 reply:tag("identity", { name = "Ad-Hoc Commands", |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
44 category = "automation", type = "command-list" }):up(); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
45 origin.send(reply); |
9b4a114b2fe6
mod_adhoc: Answer disco#info for node=xmlns_cmd
Florian Zeitz <florob@babelmonkeys.de>
parents:
246
diff
changeset
|
46 return true; |
246
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
47 end |
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
48 end |
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
49 end); |
5a389f9f2ec7
mod_adhoc: Answer disco#info (This is a MUST in XEP-0050)
Florian Zeitz <florob@babelmonkeys.de>
parents:
245
diff
changeset
|
50 |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
51 module:hook("iq/host/"..xmlns_disco.."#items:query", function (event) |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
52 local origin, stanza = event.origin, event.stanza; |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
53 if stanza.attr.type == "get" and stanza.tags[1].attr.node |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
54 and stanza.tags[1].attr.node == xmlns_cmd then |
244
e0802b2716c3
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
55 -- Required for Prosody <= 0.7 |
e0802b2716c3
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
56 local privileged = is_admin(stanza.attr.from) |
e0802b2716c3
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
57 or is_admin(stanza.attr.from, stanza.attr.to); |
6 | 58 reply = st.reply(stanza); |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
59 reply:tag("query", { xmlns = xmlns_disco.."#items", |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
60 node = xmlns_cmd }); |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
61 for node, command in pairs(commands) do |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
62 if (command.permission == "admin" and privileged) |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
63 or (command.permission == "user") then |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
64 reply:tag("item", { name = command.name, |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
65 node = node, jid = module:get_host() }); |
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
|
66 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
|
67 end |
6 | 68 end |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
69 origin.send(reply); |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
70 return true; |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
71 end |
6 | 72 end, 500); |
73 | |
245
84d03c44bdb9
mod_adhoc: Fix typo introduced during refactoring
Florian Zeitz <florob@babelmonkeys.de>
parents:
244
diff
changeset
|
74 module:hook("iq/host/"..xmlns_cmd..":command", function (event) |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
75 local origin, stanza = event.origin, event.stanza; |
244
e0802b2716c3
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
76 if stanza.attr.type == "set" then |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
77 local node = stanza.tags[1].attr.node |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
78 if commands[node] then |
244
e0802b2716c3
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
79 -- Required for Prosody <= 0.7 |
e0802b2716c3
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
80 local privileged = is_admin(event.stanza.attr.from) |
e0802b2716c3
mod_adhoc: Code restructuring
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
81 or is_admin(stanza.attr.from, stanza.attr.to); |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
82 if commands[node].permission == "admin" |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
83 and not privileged then |
93
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
84 origin.send(st.error_reply(stanza, "auth", "forbidden", "You don't have permission to execute this command"):up() |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
85 :add_child(commands[node]:cmdtag("canceled") |
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
86 :tag("note", {type="error"}):text("You don't have permission to execute this command"))); |
93
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
87 return true |
6 | 88 end |
93
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
89 -- User has permission now execute the command |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
90 return adhoc_handle_cmd(commands[node], origin, stanza); |
6 | 91 end |
93
611d16867410
mod_adhoc: Check for global and host admins
Florian Zeitz <florob@babelmonkeys.de>
parents:
49
diff
changeset
|
92 end |
6 | 93 end, 500); |
94 | |
170
0d438a7ac4fc
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:
169
diff
changeset
|
95 local function handle_item_added(item) |
0d438a7ac4fc
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:
169
diff
changeset
|
96 commands[item.node] = item; |
0d438a7ac4fc
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:
169
diff
changeset
|
97 end |
0d438a7ac4fc
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:
169
diff
changeset
|
98 |
6 | 99 module:hook("item-added/adhoc", function (event) |
170
0d438a7ac4fc
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:
169
diff
changeset
|
100 return handle_item_added(event.item); |
6 | 101 end, 500); |
9 | 102 |
103 module:hook("item-removed/adhoc", function (event) | |
125
8111c8a9e1ad
mod_adhoc: Use hashtable instead of array, coding style
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
104 commands[event.item.node] = nil; |
9 | 105 end, 500); |
170
0d438a7ac4fc
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:
169
diff
changeset
|
106 |
0d438a7ac4fc
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:
169
diff
changeset
|
107 -- Pick up any items that are already added |
0d438a7ac4fc
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:
169
diff
changeset
|
108 for _, item in ipairs(module:get_host_items("adhoc")) do |
0d438a7ac4fc
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:
169
diff
changeset
|
109 handle_item_added(item); |
0d438a7ac4fc
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:
169
diff
changeset
|
110 end |