Software / code / verse
Comparison
libs/adhoc.lib.lua @ 116:151c8cc776df
verse.plugins.adhoc: XEP-0050 Ad-hoc commands plugin
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 26 Aug 2010 17:52:16 +0100 |
| child | 198:f6702e7ce5f9 |
comparison
equal
deleted
inserted
replaced
| 115:9f8cacfca7c7 | 116:151c8cc776df |
|---|---|
| 1 -- Copyright (C) 2009-2010 Florian Zeitz | |
| 2 -- | |
| 3 -- This file is MIT/X11 licensed. Please see the | |
| 4 -- COPYING file in the source package for more information. | |
| 5 -- | |
| 6 | |
| 7 local st, uuid = require "util.stanza", require "util.uuid"; | |
| 8 | |
| 9 local xmlns_cmd = "http://jabber.org/protocol/commands"; | |
| 10 | |
| 11 local states = {} | |
| 12 | |
| 13 local _M = {}; | |
| 14 | |
| 15 function _cmdtag(desc, status, sessionid, action) | |
| 16 local cmd = st.stanza("command", { xmlns = xmlns_cmd, node = desc.node, status = status }); | |
| 17 if sessionid then cmd.attr.sessionid = sessionid; end | |
| 18 if action then cmd.attr.action = action; end | |
| 19 | |
| 20 return cmd; | |
| 21 end | |
| 22 | |
| 23 function _M.new(name, node, handler, permission) | |
| 24 return { name = name, node = node, handler = handler, cmdtag = _cmdtag, permission = (permission or "user") }; | |
| 25 end | |
| 26 | |
| 27 function _M.handle_cmd(command, origin, stanza) | |
| 28 local sessionid = stanza.tags[1].attr.sessionid or uuid.generate(); | |
| 29 local dataIn = {}; | |
| 30 dataIn.to = stanza.attr.to; | |
| 31 dataIn.from = stanza.attr.from; | |
| 32 dataIn.action = stanza.tags[1].attr.action or "execute"; | |
| 33 dataIn.form = stanza.tags[1]:child_with_ns("jabber:x:data"); | |
| 34 | |
| 35 local data, state = command:handler(dataIn, states[sessionid]); | |
| 36 states[sessionid] = state; | |
| 37 local stanza = st.reply(stanza); | |
| 38 if data.status == "completed" then | |
| 39 states[sessionid] = nil; | |
| 40 cmdtag = command:cmdtag("completed", sessionid); | |
| 41 elseif data.status == "canceled" then | |
| 42 states[sessionid] = nil; | |
| 43 cmdtag = command:cmdtag("canceled", sessionid); | |
| 44 elseif data.status == "error" then | |
| 45 states[sessionid] = nil; | |
| 46 stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message); | |
| 47 origin.send(stanza); | |
| 48 return true; | |
| 49 else | |
| 50 cmdtag = command:cmdtag("executing", sessionid); | |
| 51 end | |
| 52 | |
| 53 for name, content in pairs(data) do | |
| 54 if name == "info" then | |
| 55 cmdtag:tag("note", {type="info"}):text(content):up(); | |
| 56 elseif name == "warn" then | |
| 57 cmdtag:tag("note", {type="warn"}):text(content):up(); | |
| 58 elseif name == "error" then | |
| 59 cmdtag:tag("note", {type="error"}):text(content.message):up(); | |
| 60 elseif name =="actions" then | |
| 61 local actions = st.stanza("actions"); | |
| 62 for _, action in ipairs(content) do | |
| 63 if (action == "prev") or (action == "next") or (action == "complete") then | |
| 64 actions:tag(action):up(); | |
| 65 else | |
| 66 module:log("error", 'Command "'..command.name.. | |
| 67 '" at node "'..command.node..'" provided an invalid action "'..action..'"'); | |
| 68 end | |
| 69 end | |
| 70 cmdtag:add_child(actions); | |
| 71 elseif name == "form" then | |
| 72 cmdtag:add_child((content.layout or content):form(content.data)); | |
| 73 elseif name == "result" then | |
| 74 cmdtag:add_child((content.layout or content):form(content.data, "result")); | |
| 75 elseif name == "other" then | |
| 76 cmdtag:add_child(content); | |
| 77 end | |
| 78 end | |
| 79 stanza:add_child(cmdtag); | |
| 80 origin.send(stanza); | |
| 81 | |
| 82 return true; | |
| 83 end | |
| 84 | |
| 85 return _M; |