libs/adhoc.lib.lua

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, new_id = require "prosody.util.stanza", require "prosody.util.id".short;
8
 
9
local xmlns_cmd = "http://jabber.org/protocol/commands";
10
 
11
local states = {}
12
 
13
local _M = {};
14
 
15
local 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 new_id();
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
	local cmdtag;
39
	if data.status == "completed" then
40
		states[sessionid] = nil;
41
		cmdtag = command:cmdtag("completed", sessionid);
42
	elseif data.status == "canceled" then
43
		states[sessionid] = nil;
44
		cmdtag = command:cmdtag("canceled", sessionid);
45
	elseif data.status == "error" then
46
		states[sessionid] = nil;
47
		stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message);
48
		origin.send(stanza);
49
		return true;
50
	else
51
		cmdtag = command:cmdtag("executing", sessionid);
52
	end
53
 
54
	for name, content in pairs(data) do
55
		if name == "info" then
56
			cmdtag:tag("note", {type="info"}):text(content):up();
57
		elseif name == "warn" then
58
			cmdtag:tag("note", {type="warn"}):text(content):up();
59
		elseif name == "error" then
60
			cmdtag:tag("note", {type="error"}):text(content.message):up();
61
		elseif name =="actions" then
62
			local actions = st.stanza("actions");
63
			for _, action in ipairs(content) do
64
				if (action == "prev") or (action == "next") or (action == "complete") then
65
					actions:tag(action):up();
66
				else
67
					module:log("error", 'Command "'..command.name..
68
						'" at node "'..command.node..'" provided an invalid action "'..action..'"');
69
				end
70
			end
71
			cmdtag:add_child(actions);
72
		elseif name == "form" then
73
			cmdtag:add_child((content.layout or content):form(content.values));
74
		elseif name == "result" then
75
			cmdtag:add_child((content.layout or content):form(content.values, "result"));
76
		elseif name == "other" then
77
			cmdtag:add_child(content);
78
		end
79
	end
80
	stanza:add_child(cmdtag);
81
	origin.send(stanza);
82
 
83
	return true;
84
end
85
 
86
return _M;