changeset 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 8111c8a9e1ad
files mod_adhoc/adhoc/adhoc.lib.lua mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua mod_adhoc_cmd_ping/mod_adhoc_cmd_ping.lua mod_adhoc_cmd_uptime/mod_adhoc_cmd_uptime.lua
diffstat 4 files changed, 53 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/mod_adhoc/adhoc/adhoc.lib.lua	Fri Jan 22 19:05:28 2010 +0100
+++ b/mod_adhoc/adhoc/adhoc.lib.lua	Sat Jan 23 04:43:28 2010 +0100
@@ -1,7 +1,9 @@
-local st = require "util.stanza";
+local st, uuid = require "util.stanza", require "util.uuid";
 
 local xmlns_cmd = "http://jabber.org/protocol/commands";
 
+local states = {}
+
 local _M = {};
 
 function _cmdtag(desc, status, sessionid, action)
@@ -17,24 +19,28 @@
 end
 
 function _M.handle_cmd(command, origin, stanza)
-	local sessionid = stanza.tags[1].attr.sessionid or nil;
+	local sessionid = stanza.tags[1].attr.sessionid or uuid.generate();
 	local dataIn = {};
 	dataIn.to = stanza.attr.to;
 	dataIn.from = stanza.attr.from;
 	dataIn.action = stanza.tags[1].attr.action or nil;
 	dataIn.form = stanza.tags[1]:child_with_ns("jabber:x:data");
 
-	local data, sessid = command:handler(dataIn, sessionid);
+	local data, state = command:handler(dataIn, states[sessionid]);
+	states[sessionid] = state;
 	local stanza = st.reply(stanza);
 	if data.status == "completed" then
-		cmdtag = command:cmdtag("completed", sessid);
+		states[sessionid] = nil;
+		cmdtag = command:cmdtag("completed", sessionid);
 	elseif data.status == "canceled" then
-		cmdtag = command:cmdtag("canceled", sessid);
+		states[sessionid] = nil;
+		cmdtag = command:cmdtag("canceled", sessionid);
 	elseif data.status == "error" then
+		states[sessionid] = nil;
 		stanza = st.error_reply(stanza, data.error.type, data.error.condition, data.error.message);
-		cmdtag = command:cmdtag("canceled", sessid);
+		cmdtag = command:cmdtag("canceled", sessionid);
 	else 
-		cmdtag = command:cmdtag("executing", sessid);
+		cmdtag = command:cmdtag("executing", sessionid);
 	end
 
 	for name, content in pairs(data) do
--- a/mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua	Fri Jan 22 19:05:28 2010 +0100
+++ b/mod_adhoc_cmd_admin/mod_adhoc_cmd_admin.lua	Sat Jan 23 04:43:28 2010 +0100
@@ -21,8 +21,6 @@
 module:log("debug", module:get_name());
 local adhoc_new = module:require "adhoc".new;
 
-local sessions = {};
-
 local add_user_layout = dataforms_new{
 	title = "Adding a User";
 	instructions = "Fill out this form to add a user.";
@@ -87,70 +85,56 @@
 	{ name = "announcement", type = "text-multi", required = true, label = "Announcement" };
 };
 
-function add_user_command_handler(self, data, sessid)
-	if sessid and sessions[sessid] then
+function add_user_command_handler(self, data, state)
+	if state then
 		if data.action == "cancel" then
-			sessions[sessid] = nil;
-			return { status = "canceled" }, sessid;
+			return { status = "canceled" };
 		end
 		local fields = add_user_layout:data(data.form);
 		local username, host, resource = jid.split(fields.accountjid);
 		if (fields["password"] == fields["password-verify"]) and username and host and host == data.to then
 			if usermanager_user_exists(username, host) then
-				sessions[sessid] = nil;
-				return { status = "error", error = { type = "cancel", condition = "conflict", message = "Account already exists" } }, sessid;
+				return { status = "error", error = { type = "cancel", condition = "conflict", message = "Account already exists" } };
 			else
 				if usermanager_create_user(username, fields.password, host) then
-					sessions[sessid] = nil;
 					module:log("info", "Created new account " .. username.."@"..host);
-					return { status = "completed", info = "Account successfully created" }, sessid;
+					return { status = "completed", info = "Account successfully created" };
 				else
-					sessions[sessid] = nil;
 					return { status = "error", error = { type = "wait", condition = "internal-server-error",
-						 message = "Failed to write data to disk" } }, sessid;
+						 message = "Failed to write data to disk" } };
 				end
 			end
 		else
 			module:log("debug", fields.accountjid .. " " .. fields.password .. " " .. fields["password-verify"]);
-			sessions[sessid] = nil;
 			return { status = "error", error = { type = "cancel", condition = "conflict",
-				 message = "Invalid data.\nPassword mismatch, or empty username" } }, sessid;
+				 message = "Invalid data.\nPassword mismatch, or empty username" } };
 		end
 	else
-		local sessionid=uuid.generate();
-		sessions[sessionid] = "executing";
-		return { status = "executing", form = add_user_layout }, sessionid;
+		return { status = "executing", form = add_user_layout }, "executing";
 	end
 end
 
-function change_user_password_command_handler(self, data, sessid)
-	if sessid and sessions[sessid] then
+function change_user_password_command_handler(self, data, state)
+	if state then
 		if data.action == "cancel" then
-			sessions[sessid] = nil;
-			return { status = "canceled" }, sessid;
+			return { status = "canceled" };
 		end
 		local fields = change_user_password_layout:data(data.form);
 		local username, host, resource = jid.split(fields.accountjid);
 		if usermanager_user_exists(username, host) and usermanager_create_user(username, fields.password, host) then
-			return { status = "completed", info = "Password successfully changed" }, sessid;
+			return { status = "completed", info = "Password successfully changed" };
 		else
-			sessions[sessid] = nil;
-			return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } }, sessid;
+			return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } };
 		end
-		sessions[sessid] = nil;
-		return { status = "canceled" }, sessid;
 	else
-		local sessionid=uuid.generate();
-		sessions[sessionid] = "executing";
-		return { status = "executing", form = change_user_password_layout }, sessionid;
+		return { status = "executing", form = change_user_password_layout }, "executing";
 	end
 end
 
-function delete_user_command_handler(self, data, sessid)
-	if sessid and sessions[sessid] then
+function delete_user_command_handler(self, data, state)
+	if state then
 		if data.action == "cancel" then
-			sessions[sessid] = nil;
-			return { status = "canceled" }, sessid;
+			return { status = "canceled" };
 		end
 		local fields = delete_user_layout:data(data.form);
 		local failed = {};
@@ -165,23 +149,19 @@
 				failed[#failed+1] = aJID;
 			end
 		end
-		sessions[sessid] = nil;
 		return {status = "completed", info = (#succeeded ~= 0 and
 				"The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
 				(#failed ~= 0 and
-				"The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") }, sessid;
+				"The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
 	else
-		local sessionid=uuid.generate();
-		sessions[sessionid] = "executing";
-		return { status = "executing", form = delete_user_layout }, sessionid;
+		return { status = "executing", form = delete_user_layout }, "executing";
 	end
 end
 
-function get_user_password_handler(self, data, sessid)
-	if sessid and sessions[sessid] then
+function get_user_password_handler(self, data, state)
+	if state then
 		if data.action == "cancel" then
-			sessions[sessid] = nil;
-			return { status = "canceled" }, sessid;
+			return { status = "canceled" };
 		end
 		local fields = get_user_password_layout:data(data.form);
 		local user, host, resource = jid.split(fields.accountjid);
@@ -191,23 +171,18 @@
 			accountjid = fields.accountjid;
 			password = usermanager_get_password(user, host);
 		else
-			sessions[sessid] = nil;
-			return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } }, sessid;
+			return { status = "error", error = { type = "cancel", condition = "item-not-found", message = "User does not exist" } };
 		end
-		sessions[sessid] = nil;
-		return { status = "completed", result = { layout = get_user_password_result_layout, data = {accountjid = accountjid, password = password} } }, sessid;
+		return { status = "completed", result = { layout = get_user_password_result_layout, data = {accountjid = accountjid, password = password} } };
 	else
-		local sessionid=uuid.generate();
-		sessions[sessionid] = "executing";
-		return { status = "executing", form = get_user_password_layout }, sessionid;
+		return { status = "executing", form = get_user_password_layout }, "executing";
 	end
 end
 
-function get_online_users_command_handler(self, data, sessid)
-	if sessid and sessions[sessid] then
+function get_online_users_command_handler(self, data, state)
+	if state then
 		if data.action == "cancel" then
-			sessions[sessid] = nil;
-			return { status = "canceled" }, sessid;
+			return { status = "canceled" };
 		end
 
 		local fields = add_user_layout:data(data.form);
@@ -225,20 +200,16 @@
 			users = ((users and users.."\n") or "")..(username.."@"..data.to);
 			count = count + 1;
 		end
-		sessions[sessid] = nil;
-		return { status = "completed", result = {layout = get_online_users_result_layout, data = {onlineuserjids=users}} }, sessid;
+		return { status = "completed", result = {layout = get_online_users_result_layout, data = {onlineuserjids=users}} };
 	else
-		local sessionid=uuid.generate();
-		sessions[sessionid] = "executing";
-		return { status = "executing", form = get_online_users_layout }, sessionid;
+		return { status = "executing", form = get_online_users_layout }, "executing";
 	end
 end
 
-function announce_handler(self, data, sessid)
-	if sessid and sessions[sessid] then
+function announce_handler(self, data, state)
+	if state then
 		if data.action == "cancel" then
-			sessions[sessid] = nil;
-			return { status = "canceled" }, sessid;
+			return { status = "canceled" };
 		end
 
 		local fields = announce_layout:data(data.form);
@@ -256,12 +227,9 @@
 		end
 		
 		module:log("info", "Announcement sent to %d online users", c);
-		sessions[sessid] = nil;
-		return { status = "completed", info = "Announcement sent." }, sessid;
+		return { status = "completed", info = "Announcement sent." };
 	else
-		local sessionid=uuid.generate();
-		sessions[sessionid] = "executing";
-		return { status = "executing", form = announce_layout }, sessionid;
+		return { status = "executing", form = announce_layout }, "executing";
 	end
 
 	return true;
--- a/mod_adhoc_cmd_ping/mod_adhoc_cmd_ping.lua	Fri Jan 22 19:05:28 2010 +0100
+++ b/mod_adhoc_cmd_ping/mod_adhoc_cmd_ping.lua	Sat Jan 23 04:43:28 2010 +0100
@@ -7,9 +7,9 @@
 local st = require "util.stanza";
 local adhoc_new = module:require "adhoc".new;
 
-function ping_command_handler (self, data, sessid)
+function ping_command_handler (self, data, state)
 	local now = os.date("%Y-%m-%dT%X");
-	return { info = "Pong\n"..now, status = "completed" }, now;
+	return { info = "Pong\n"..now, status = "completed" };
 end
 
 local descriptor = adhoc_new("Ping", "ping", ping_command_handler);
--- a/mod_adhoc_cmd_uptime/mod_adhoc_cmd_uptime.lua	Fri Jan 22 19:05:28 2010 +0100
+++ b/mod_adhoc_cmd_uptime/mod_adhoc_cmd_uptime.lua	Sat Jan 23 04:43:28 2010 +0100
@@ -6,7 +6,7 @@
 
 local _G = _G;
 local prosody = _G.prosody;
-local st, uuid = require "util.stanza", require "util.uuid";
+local st = require "util.stanza";
 local adhoc_new = module:require "adhoc".new;
 
 function uptime()
@@ -23,8 +23,8 @@
 		minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
 end
 
-function uptime_command_handler (self, data, sessid)
-	return { info = uptime(), status = "completed" }, uuid.generate();
+function uptime_command_handler (self, data, state)
+	return { info = uptime(), status = "completed" };
 end
 
 local descriptor = adhoc_new("Get uptime", "uptime", uptime_command_handler);