annotate mod_adhoc/adhoc/adhoc.lib.lua @ 134:744deabdee81

mod_ipcheck: Fixed: 'service-unavailable' was sent instead of 'forbidden' on unauthorized access.
author Waqas Hussain <waqas20@gmail.com>
date Sat, 06 Mar 2010 06:19:38 +0500
parents 843cadf36306
children b8a89ebf71e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
1 local st, uuid = require "util.stanza", require "util.uuid";
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
2
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
3 local xmlns_cmd = "http://jabber.org/protocol/commands";
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
4
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
5 local states = {}
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
6
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
7 local _M = {};
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
8
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
9 function _cmdtag(desc, status, sessionid, action)
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
10 local cmd = st.stanza("command", { xmlns = xmlns_cmd, node = desc.node, status = status });
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
11 if sessionid then cmd.attr.sessionid = sessionid; end
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
12 if action then cmd.attr.action = action; end
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
13
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14 return cmd;
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
15 end
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16
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
17 function _M.new(name, node, handler, permission)
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
18 return { name = name, node = node, handler = handler, cmdtag = _cmdtag, permission = (permission or "user") };
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
19 end
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
20
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
21 function _M.handle_cmd(command, origin, stanza)
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
22 local sessionid = stanza.tags[1].attr.sessionid or uuid.generate();
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
23 local dataIn = {};
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
24 dataIn.to = stanza.attr.to;
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
25 dataIn.from = stanza.attr.from;
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
26 dataIn.action = stanza.tags[1].attr.action or nil;
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
27 dataIn.form = stanza.tags[1]:child_with_ns("jabber:x:data");
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
28
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
29 local data, state = command:handler(dataIn, states[sessionid]);
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
30 states[sessionid] = state;
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
31 local stanza = st.reply(stanza);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
32 if data.status == "completed" then
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
33 states[sessionid] = nil;
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
34 cmdtag = command:cmdtag("completed", sessionid);
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
35 elseif data.status == "canceled" then
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
36 states[sessionid] = nil;
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
37 cmdtag = command:cmdtag("canceled", sessionid);
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
38 elseif data.status == "error" then
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
39 states[sessionid] = nil;
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
40 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message);
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
41 cmdtag = command:cmdtag("canceled", sessionid);
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
42 else
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 123
diff changeset
43 cmdtag = command:cmdtag("executing", sessionid);
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
44 end
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
45
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
46 for name, content in pairs(data) do
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
47 if name == "info" then
122
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
48 cmdtag:tag("note", {type="info"}):text(content):up();
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
49 elseif name == "warn" then
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
50 cmdtag:tag("note", {type="warn"}):text(content):up();
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
51 elseif name == "error" then
122
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
52 cmdtag:tag("note", {type="error"}):text(content.message):up();
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
53 elseif name =="actions" then
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
54 local actions = st.stanza("actions");
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
55 for _, action in ipairs(content) do
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
56 if (action == "prev") or (action == "next") or (action == "complete") then
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
57 actions:tag(action):up();
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
58 else
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
59 module:log("error", 'Command "'..command.name..
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
60 '" at node "'..command.node..'" provided an invalid action "'..action..'"');
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
61 end
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
62 end
123
c04443ea114c mod_adhoc, mod_adhoc_cmd_admin: use util.datforms for type="result" forms
Florian Zeitz <florob@babelmonkeys.de>
parents: 122
diff changeset
63 cmdtag:add_child(actions);
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
64 elseif name == "form" then
123
c04443ea114c mod_adhoc, mod_adhoc_cmd_admin: use util.datforms for type="result" forms
Florian Zeitz <florob@babelmonkeys.de>
parents: 122
diff changeset
65 cmdtag:add_child(content:form());
c04443ea114c mod_adhoc, mod_adhoc_cmd_admin: use util.datforms for type="result" forms
Florian Zeitz <florob@babelmonkeys.de>
parents: 122
diff changeset
66 elseif name == "result" then
c04443ea114c mod_adhoc, mod_adhoc_cmd_admin: use util.datforms for type="result" forms
Florian Zeitz <florob@babelmonkeys.de>
parents: 122
diff changeset
67 cmdtag:add_child(content.layout:form(content.data, "result"));
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
68 elseif name == "other" then
123
c04443ea114c mod_adhoc, mod_adhoc_cmd_admin: use util.datforms for type="result" forms
Florian Zeitz <florob@babelmonkeys.de>
parents: 122
diff changeset
69 cmdtag:add_child(content);
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
70 end
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
71 end
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
72 stanza:add_child(cmdtag);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
73 origin.send(stanza);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
74
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
75 return true;
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
76 end
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
77
36
58d326d86a9a mod_adhoc: add adhoc.lib.lua to ease implementing new commands (as a consequence mod_adhoc is a directory now)
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
78 return _M;