Mercurial > prosody-modules
comparison mod_adhoc/adhoc/adhoc.lib.lua @ 124:843cadf36306
mod_adhoc*: Move state handling to mod_adhoc itself
| author | Florian Zeitz <florob@babelmonkeys.de> |
|---|---|
| date | Sat, 23 Jan 2010 04:43:28 +0100 |
| parents | c04443ea114c |
| children | b8a89ebf71e3 |
comparison
equal
deleted
inserted
replaced
| 123:c04443ea114c | 124:843cadf36306 |
|---|---|
| 1 local st = require "util.stanza"; | 1 local st, uuid = require "util.stanza", require "util.uuid"; |
| 2 | 2 |
| 3 local xmlns_cmd = "http://jabber.org/protocol/commands"; | 3 local xmlns_cmd = "http://jabber.org/protocol/commands"; |
| 4 | |
| 5 local states = {} | |
| 4 | 6 |
| 5 local _M = {}; | 7 local _M = {}; |
| 6 | 8 |
| 7 function _cmdtag(desc, status, sessionid, action) | 9 function _cmdtag(desc, status, sessionid, action) |
| 8 local cmd = st.stanza("command", { xmlns = xmlns_cmd, node = desc.node, status = status }); | 10 local cmd = st.stanza("command", { xmlns = xmlns_cmd, node = desc.node, status = status }); |
| 15 function _M.new(name, node, handler, permission) | 17 function _M.new(name, node, handler, permission) |
| 16 return { name = name, node = node, handler = handler, cmdtag = _cmdtag, permission = (permission or "user") }; | 18 return { name = name, node = node, handler = handler, cmdtag = _cmdtag, permission = (permission or "user") }; |
| 17 end | 19 end |
| 18 | 20 |
| 19 function _M.handle_cmd(command, origin, stanza) | 21 function _M.handle_cmd(command, origin, stanza) |
| 20 local sessionid = stanza.tags[1].attr.sessionid or nil; | 22 local sessionid = stanza.tags[1].attr.sessionid or uuid.generate(); |
| 21 local dataIn = {}; | 23 local dataIn = {}; |
| 22 dataIn.to = stanza.attr.to; | 24 dataIn.to = stanza.attr.to; |
| 23 dataIn.from = stanza.attr.from; | 25 dataIn.from = stanza.attr.from; |
| 24 dataIn.action = stanza.tags[1].attr.action or nil; | 26 dataIn.action = stanza.tags[1].attr.action or nil; |
| 25 dataIn.form = stanza.tags[1]:child_with_ns("jabber:x:data"); | 27 dataIn.form = stanza.tags[1]:child_with_ns("jabber:x:data"); |
| 26 | 28 |
| 27 local data, sessid = command:handler(dataIn, sessionid); | 29 local data, state = command:handler(dataIn, states[sessionid]); |
| 30 states[sessionid] = state; | |
| 28 local stanza = st.reply(stanza); | 31 local stanza = st.reply(stanza); |
| 29 if data.status == "completed" then | 32 if data.status == "completed" then |
| 30 cmdtag = command:cmdtag("completed", sessid); | 33 states[sessionid] = nil; |
| 34 cmdtag = command:cmdtag("completed", sessionid); | |
| 31 elseif data.status == "canceled" then | 35 elseif data.status == "canceled" then |
| 32 cmdtag = command:cmdtag("canceled", sessid); | 36 states[sessionid] = nil; |
| 37 cmdtag = command:cmdtag("canceled", sessionid); | |
| 33 elseif data.status == "error" then | 38 elseif data.status == "error" then |
| 39 states[sessionid] = nil; | |
| 34 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message); | 40 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message); |
| 35 cmdtag = command:cmdtag("canceled", sessid); | 41 cmdtag = command:cmdtag("canceled", sessionid); |
| 36 else | 42 else |
| 37 cmdtag = command:cmdtag("executing", sessid); | 43 cmdtag = command:cmdtag("executing", sessionid); |
| 38 end | 44 end |
| 39 | 45 |
| 40 for name, content in pairs(data) do | 46 for name, content in pairs(data) do |
| 41 if name == "info" then | 47 if name == "info" then |
| 42 cmdtag:tag("note", {type="info"}):text(content):up(); | 48 cmdtag:tag("note", {type="info"}):text(content):up(); |
