Annotate

mod_adhoc/adhoc/adhoc.lib.lua @ 122:c3a874eec712

mod_adhoc: Add more features described in XEP-0050
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 22 Jan 2010 14:45:36 +0100
parent 121:a9898f13c89e
child 123:c04443ea114c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
1 local st = require "util.stanza";
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
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
5 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
6
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 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
8 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
9 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
10 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
11
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 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
13 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
14
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
15 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
16 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
17 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
18
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
19 function _M.handle_cmd(command, origin, stanza)
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
20 local sessionid = stanza.tags[1].attr.sessionid or nil;
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
21 local dataIn = {};
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
22 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
23 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
24 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
25 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
26
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
27 local data, sessid = command:handler(dataIn, sessionid);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
28 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
29 if data.status == "completed" then
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
30 cmdtag = command:cmdtag("completed", sessid);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
31 elseif data.status == "canceled" then
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
32 cmdtag = command:cmdtag("canceled", sessid);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
33 elseif data.status == "error" then
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
34 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
35 cmdtag = command:cmdtag("canceled", sessid);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
36 else
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
37 cmdtag = command:cmdtag("executing", sessid);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
38 end
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
39
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
40 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
41 if name == "info" then
122
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
42 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
43 elseif name == "warn" then
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
44 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
45 elseif name == "error" then
122
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
46 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
47 elseif name =="actions" then
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
48 local actions = st.stanza("actions");
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
49 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
50 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
51 actions:tag(action):up();
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
52 else
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
53 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
54 '" 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
55 end
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
56 end
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
57 cmdtag:add_child(actions):up();
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
58 elseif name == "form" then
122
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
59 cmdtag:add_child(data.form:form()):up();
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
60 elseif name == "other" then
122
c3a874eec712 mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
61 cmdtag:add_child(content):up();
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
62 end
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
63 end
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
64 stanza:add_child(cmdtag);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
65 origin.send(stanza);
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
66
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
67 return true;
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
68 end
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 43
diff changeset
69
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
70 return _M;