Software /
code /
prosody
Annotate
plugins/adhoc/adhoc.lib.lua @ 4655:9159546cb2f3
mod_component: Handle component connecting to non-existent host
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 22 Apr 2012 16:35:26 +0100 |
parent | 4632:52b6901cabb0 |
child | 4858:33458e1d84c8 |
rev | line source |
---|---|
3230
a5c3a82d677e
mod_adhoc/adhoc.lib: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
3229
diff
changeset
|
1 -- Copyright (C) 2009-2010 Florian Zeitz |
a5c3a82d677e
mod_adhoc/adhoc.lib: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
3229
diff
changeset
|
2 -- |
a5c3a82d677e
mod_adhoc/adhoc.lib: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
3229
diff
changeset
|
3 -- This file is MIT/X11 licensed. Please see the |
a5c3a82d677e
mod_adhoc/adhoc.lib: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
3229
diff
changeset
|
4 -- COPYING file in the source package for more information. |
a5c3a82d677e
mod_adhoc/adhoc.lib: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
3229
diff
changeset
|
5 -- |
a5c3a82d677e
mod_adhoc/adhoc.lib: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents:
3229
diff
changeset
|
6 |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local st, uuid = require "util.stanza", require "util.uuid"; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 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
|
10 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 local states = {} |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 local _M = {}; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 function _cmdtag(desc, status, sessionid, action) |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 local cmd = st.stanza("command", { xmlns = xmlns_cmd, node = desc.node, status = status }); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 if sessionid then cmd.attr.sessionid = sessionid; end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 if action then cmd.attr.action = action; end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 return cmd; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 function _M.new(name, node, handler, permission) |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 return { name = name, node = node, handler = handler, cmdtag = _cmdtag, permission = (permission or "user") }; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 function _M.handle_cmd(command, origin, stanza) |
4632
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
28 local cmdtag, actions; |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 local sessionid = stanza.tags[1].attr.sessionid or uuid.generate(); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local dataIn = {}; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 dataIn.to = stanza.attr.to; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 dataIn.from = stanza.attr.from; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 dataIn.action = stanza.tags[1].attr.action or "execute"; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 dataIn.form = stanza.tags[1]:child_with_ns("jabber:x:data"); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 local data, state = command:handler(dataIn, states[sessionid]); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 states[sessionid] = state; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 local stanza = st.reply(stanza); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 if data.status == "completed" then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 states[sessionid] = nil; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 cmdtag = command:cmdtag("completed", sessionid); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 elseif data.status == "canceled" then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 states[sessionid] = nil; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 cmdtag = command:cmdtag("canceled", sessionid); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 elseif data.status == "error" then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 states[sessionid] = nil; |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message); |
3229
0abb73c43bc8
mod_adhoc/adhoc.lib: Handle errors according to XEP
Matthew Wild <mwild1@gmail.com>
parents:
3220
diff
changeset
|
48 origin.send(stanza); |
0abb73c43bc8
mod_adhoc/adhoc.lib: Handle errors according to XEP
Matthew Wild <mwild1@gmail.com>
parents:
3220
diff
changeset
|
49 return true; |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3484
diff
changeset
|
50 else |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 cmdtag = command:cmdtag("executing", sessionid); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 for name, content in pairs(data) do |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 if name == "info" then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 cmdtag:tag("note", {type="info"}):text(content):up(); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 elseif name == "warn" then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 cmdtag:tag("note", {type="warn"}):text(content):up(); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 elseif name == "error" then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 cmdtag:tag("note", {type="error"}):text(content.message):up(); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 elseif name =="actions" then |
4632
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
62 actions = st.stanza("actions"); |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 for _, action in ipairs(content) do |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 if (action == "prev") or (action == "next") or (action == "complete") then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 actions:tag(action):up(); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 else |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 module:log("error", 'Command "'..command.name.. |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 '" at node "'..command.node..'" provided an invalid action "'..action..'"'); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
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 elseif name == "form" then |
3484
66910810a9f7
mod_adhoc: Fix passing data to util.dataforms
Florian Zeitz <florob@babelmonkeys.de>
parents:
3346
diff
changeset
|
72 cmdtag:add_child((content.layout or content):form(content.values)); |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 elseif name == "result" then |
3484
66910810a9f7
mod_adhoc: Fix passing data to util.dataforms
Florian Zeitz <florob@babelmonkeys.de>
parents:
3346
diff
changeset
|
74 cmdtag:add_child((content.layout or content):form(content.values, "result")); |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 elseif name == "other" then |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 cmdtag:add_child(content); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 end |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 end |
4632
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
79 |
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
80 if not actions then |
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
81 actions = st.stanza("actions"); |
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
82 actions:tag("complete"):up(); |
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
83 end |
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
84 cmdtag:add_child(actions); |
52b6901cabb0
mod_adhoc: Always allow at least the "complete" action
Florian Zeitz <florob@babelmonkeys.de>
parents:
3540
diff
changeset
|
85 |
3220
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 stanza:add_child(cmdtag); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 origin.send(stanza); |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 |
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 |
b3772f9bc359
mod_adhoc: Imported from prosody-modules, thanks Florob!
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 return _M; |