Mercurial > prosody-modules
annotate mod_adhoc/adhoc/adhoc.lib.lua @ 603:efc9d88b70ab
merged.
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Thu, 09 Feb 2012 00:54:39 +0000 |
parents | 67f03b7786af |
children |
rev | line source |
---|---|
169
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
149
diff
changeset
|
1 -- Copyright (C) 2009-2010 Florian Zeitz |
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
149
diff
changeset
|
2 -- |
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
149
diff
changeset
|
3 -- This file is MIT/X11 licensed. Please see the |
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
149
diff
changeset
|
4 -- COPYING file in the source package for more information. |
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
149
diff
changeset
|
5 -- |
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
149
diff
changeset
|
6 |
124
843cadf36306
mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents:
123
diff
changeset
|
7 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
|
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 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
|
10 |
124
843cadf36306
mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents:
123
diff
changeset
|
11 local states = {} |
843cadf36306
mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents:
123
diff
changeset
|
12 |
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
|
13 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
|
14 |
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 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
|
16 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
|
17 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
|
18 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
|
19 |
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 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
|
21 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
|
22 |
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
|
23 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
|
24 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
|
25 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
|
26 |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
27 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
|
28 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
|
29 local dataIn = {}; |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
30 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
|
31 dataIn.from = stanza.attr.from; |
149
b8a89ebf71e3
mod_adhoc: default action is "execute"
Florian Zeitz < florob@babelmonkeys.de>
parents:
124
diff
changeset
|
32 dataIn.action = stanza.tags[1].attr.action or "execute"; |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
33 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
|
34 |
124
843cadf36306
mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents:
123
diff
changeset
|
35 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
|
36 states[sessionid] = state; |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
37 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
|
38 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
|
39 states[sessionid] = nil; |
843cadf36306
mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents:
123
diff
changeset
|
40 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
|
41 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
|
42 states[sessionid] = nil; |
843cadf36306
mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents:
123
diff
changeset
|
43 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
|
44 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
|
45 states[sessionid] = nil; |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
46 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message); |
169
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
149
diff
changeset
|
47 origin.send(stanza); |
b3a68e71b8a1
mod_adhoc, mod_adhoc_cmd_admin: Handle errors according to XEP
Florian Zeitz < florob@babelmonkeys.de>
parents:
149
diff
changeset
|
48 return true; |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
49 else |
124
843cadf36306
mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents:
123
diff
changeset
|
50 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
|
51 end |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
52 |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
53 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
|
54 if name == "info" then |
122
c3a874eec712
mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
55 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
|
56 elseif name == "warn" then |
c3a874eec712
mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
57 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
|
58 elseif name == "error" then |
122
c3a874eec712
mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
59 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
|
60 elseif name =="actions" then |
c3a874eec712
mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
61 local actions = st.stanza("actions"); |
c3a874eec712
mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
62 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
|
63 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
|
64 actions:tag(action):up(); |
c3a874eec712
mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
65 else |
c3a874eec712
mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
66 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
|
67 '" 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
|
68 end |
c3a874eec712
mod_adhoc: Add more features described in XEP-0050
Florian Zeitz <florob@babelmonkeys.de>
parents:
121
diff
changeset
|
69 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
|
70 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
|
71 elseif name == "form" then |
250
67f03b7786af
mod_adhoc: Fix dataform creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
72 cmdtag:add_child((content.layout or content):form(content.values)); |
123
c04443ea114c
mod_adhoc, mod_adhoc_cmd_admin: use util.datforms for type="result" forms
Florian Zeitz <florob@babelmonkeys.de>
parents:
122
diff
changeset
|
73 elseif name == "result" then |
250
67f03b7786af
mod_adhoc: Fix dataform creation
Florian Zeitz <florob@babelmonkeys.de>
parents:
207
diff
changeset
|
74 cmdtag:add_child((content.layout or content):form(content.values, "result")); |
121
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
75 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
|
76 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
|
77 end |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
78 end |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
79 stanza:add_child(cmdtag); |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
80 origin.send(stanza); |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
81 |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
82 return true; |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
83 end |
a9898f13c89e
mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents:
43
diff
changeset
|
84 |
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
|
85 return _M; |