changeset 206:d3498f115fcd

mod_adhoc_cmd_modules: Add "Unload module" command
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 09 Jul 2010 22:38:05 +0200
parents a6361a1fda5e
children 1ae653712e37
files mod_adhoc_cmd_modules/mod_adhoc_cmd_modules.lua
diffstat 1 files changed, 34 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_adhoc_cmd_modules/mod_adhoc_cmd_modules.lua	Fri Jul 09 22:29:26 2010 +0200
+++ b/mod_adhoc_cmd_modules/mod_adhoc_cmd_modules.lua	Fri Jul 09 22:38:05 2010 +0200
@@ -28,7 +28,7 @@
 	return { status = "completed", result = { layout = result; data = { modules = modules } } };
 end
 
--- TODO: Allow reloading multiple modules (depends on list-multi
+-- TODO: Allow reloading multiple modules (depends on list-multi)
 function reload_modules_handler(self, data, state)
 	local modules = {};
 	local layout = dataforms_new {
@@ -59,8 +59,41 @@
 	end
 end
 
+-- TODO: Allow unloading multiple modules (depends on list-multi)
+function unload_modules_handler(self, data, state)
+	local modules = {};
+	local layout = dataforms_new {
+		title = "Unload module";
+		instructions = "Select the module to be unloaded";
+
+		{ name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
+		{ name = "module", type = "list-single", value = modules, label = "Module to be unloaded:"};
+	};
+	if state then
+		if data.action == "cancel" then
+			return { status = "canceled" };
+		end
+		fields = layout:data(data.form);
+		local ok, err = modulemanager.unload(data.to, fields.module);
+		if ok then
+			return { status = "completed", info = 'Module "'..fields.module..'" successfully unloaded on host "'..data.to..'".' };
+		else
+			return { status = "completed", error = 'Failed to unload module "'..fields.module..'" on host "'..data.to..
+			'". Error was: "'..tostring(err)..'"' };
+		end
+	else
+		local modules2 = array.collect(keys(hosts[data.to].modules)):sort();
+		for i, val in ipairs(modules2) do
+			modules[i] = val;
+		end
+		return { status = "executing", form = layout }, "executing";
+	end
+end
+
 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
 local reload_modules_desc = adhoc_new("Reload module", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
+local unload_modules_desc = adhoc_new("Unload module", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
 
 module:add_item("adhoc", list_modules_desc);
 module:add_item("adhoc", reload_modules_desc);
+module:add_item("adhoc", unload_modules_desc);