--- Copyright (C) 2009-2010 Florian Zeitz
+-- Copyright (C) 2009-2011 Florian Zeitz
--
-- This file is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
local hosts = prosody.hosts;
local t_concat = table.concat;
-require "util.iterators";
+local iterators = require "util.iterators";
+local keys, values = iterators.keys, iterators.values;
local usermanager_user_exists = require "core.usermanager".user_exists;
local usermanager_create_user = require "core.usermanager".create_user;
local usermanager_get_password = require "core.usermanager".get_password;
local array = require "util.array";
local modulemanager = require "modulemanager";
+module:depends("adhoc");
local adhoc_new = module:require "adhoc".new;
function add_user_command_handler(self, data, state)
end
end
+function config_reload_handler(self, data, state)
+ local ok, err = prosody.reload_config();
+ if ok then
+ return { status = "completed", info = "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
+ else
+ return { status = "completed", error = { message = "Failed to reload config: " .. tostring(err) } };
+ end
+end
+
+
function delete_user_command_handler(self, data, state)
local delete_user_layout = dataforms_new{
title = "Deleting a User";
end
end
--- TODO: Allow reloading multiple modules (depends on list-multi)
function reload_modules_handler(self, data, state)
local layout = dataforms_new {
- title = "Reload module";
- instructions = "Select the module to be reloaded";
+ title = "Reload modules";
+ instructions = "Select the modules to be reloaded";
{ name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
- { name = "module", type = "list-single", required = true, label = "Module to be reloaded:"};
+ { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
};
if state then
if data.action == "cancel" then
return { status = "canceled" };
end
local fields = layout:data(data.form);
- if (not fields.module) or (fields.module == "") then
+ if #fields.modules == 0 then
return { status = "completed", error = {
message = "Please specify a module. (This means your client misbehaved, as this field is required)"
} };
end
- local ok, err = modulemanager.reload(data.to, fields.module);
- if ok then
- return { status = "completed", info = 'Module "'..fields.module..'" successfully reloaded on host "'..data.to..'".' };
- else
- return { status = "completed", error = { message = 'Failed to reload module "'..fields.module..'" on host "'..data.to..
- '". Error was: "'..tostring(err)..'"' } };
+ local ok_list, err_list = {}, {};
+ for _, module in ipairs(fields.modules) do
+ local ok, err = modulemanager.reload(data.to, module);
+ if ok then
+ ok_list[#ok_list + 1] = module;
+ else
+ err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
+ end
end
+ local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
+ (#err_list > 0 and ("Failed to reload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
+ return { status = "completed", info = info };
else
local modules = array.collect(keys(hosts[data.to].modules)):sort();
- return { status = "executing", form = { layout = layout; values = { module = modules } } }, "executing";
+ return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
end
end
else
return { status = "executing", form = shut_down_service_layout }, "executing";
end
-
- return true;
end
--- TODO: Allow unloading multiple modules (depends on list-multi)
function unload_modules_handler(self, data, state)
local layout = dataforms_new {
- title = "Unload module";
- instructions = "Select the module to be unloaded";
+ title = "Unload modules";
+ instructions = "Select the modules to be unloaded";
{ name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
- { name = "module", type = "list-single", required = true, label = "Module to be unloaded:"};
+ { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
};
if state then
if data.action == "cancel" then
return { status = "canceled" };
end
local fields = layout:data(data.form);
- if (not fields.module) or (fields.module == "") then
+ if #fields.modules == 0 then
return { status = "completed", error = {
message = "Please specify a module. (This means your client misbehaved, as this field is required)"
} };
end
- 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 = { message = 'Failed to unload module "'..fields.module..'" on host "'..data.to..
- '". Error was: "'..tostring(err)..'"' } };
+ local ok_list, err_list = {}, {};
+ for _, module in ipairs(fields.modules) do
+ local ok, err = modulemanager.unload(data.to, module);
+ if ok then
+ ok_list[#ok_list + 1] = module;
+ else
+ err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
+ end
end
+ local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
+ (#err_list > 0 and ("Failed to unload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
+ return { status = "completed", info = info };
else
local modules = array.collect(keys(hosts[data.to].modules)):sort();
- return { status = "executing", form = { layout = layout; values = { module = modules } } }, "executing";
+ return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
end
end
local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
+local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users", get_online_users_command_handler, "admin");
local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
-local reload_modules_desc = adhoc_new("Reload module", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
-local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "admin");
-local unload_modules_desc = adhoc_new("Unload module", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
+local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
+local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
+local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
module:add_item("adhoc", add_user_desc);
module:add_item("adhoc", change_user_password_desc);
+module:add_item("adhoc", config_reload_desc);
module:add_item("adhoc", delete_user_desc);
module:add_item("adhoc", end_user_session_desc);
module:add_item("adhoc", get_user_password_desc);