1 -- Copyright (C) 2009-2011 Florian Zeitz
3 -- This file is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
9 local prosody = _G.prosody;
10 local hosts = prosody.hosts;
11 local t_concat = table.concat;
13 local iterators = require "util.iterators";
14 local keys, values = iterators.keys, iterators.values;
15 local usermanager_user_exists = require "core.usermanager".user_exists;
16 local usermanager_create_user = require "core.usermanager".create_user;
17 local usermanager_delete_user = require "core.usermanager".delete_user;
18 local usermanager_get_password = require "core.usermanager".get_password;
19 local usermanager_set_password = require "core.usermanager".set_password;
20 local hostmanager_activate = require "core.hostmanager".activate;
21 local hostmanager_deactivate = require "core.hostmanager".deactivate;
22 local is_admin = require "core.usermanager".is_admin;
23 local rm_load_roster = require "core.rostermanager".load_roster;
24 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
25 local timer_add_task = require "util.timer".add_task;
26 local dataforms_new = require "util.dataforms".new;
27 local array = require "util.array";
28 local modulemanager = require "modulemanager";
29 local core_post_stanza = prosody.core_post_stanza;
31 module:depends("adhoc");
32 local adhoc_new = module:require "adhoc".new;
34 local function generate_error_message(errors)
36 for name, err in pairs(errors) do
37 errmsg[#errmsg + 1] = name .. ": " .. err;
39 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
42 function add_user_command_handler(self, data, state)
43 local add_user_layout = dataforms_new{
44 title = "Adding a User";
45 instructions = "Fill out this form to add a user.";
47 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
48 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
49 { name = "password", type = "text-private", label = "The password for this account" };
50 { name = "password-verify", type = "text-private", label = "Retype password" };
54 if data.action == "cancel" then
55 return { status = "canceled" };
57 local fields, err = add_user_layout:data(data.form);
59 return generate_error_message(err);
61 local username, host, resource = jid.split(fields.accountjid);
62 if data.to ~= host then
63 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. data.to}};
65 if (fields["password"] == fields["password-verify"]) and username and host then
66 if usermanager_user_exists(username, host) then
67 return { status = "completed", error = { message = "Account already exists" } };
69 if usermanager_create_user(username, fields.password, host) then
70 module:log("info", "Created new account %s@%s", username, host);
71 return { status = "completed", info = "Account successfully created" };
73 return { status = "completed", error = { message = "Failed to write data to disk" } };
77 module:log("debug", "Invalid data, password mismatch or empty username while creating account for %s", fields.accountjid or "<nil>");
78 return { status = "completed", error = { message = "Invalid data.\nPassword mismatch, or empty username" } };
81 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = add_user_layout }, "executing";
85 function change_user_password_command_handler(self, data, state)
86 local change_user_password_layout = dataforms_new{
87 title = "Changing a User Password";
88 instructions = "Fill out this form to change a user's password.";
90 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
91 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for this account" };
92 { name = "password", type = "text-private", required = true, label = "The password for this account" };
96 if data.action == "cancel" then
97 return { status = "canceled" };
99 local fields, err = change_user_password_layout:data(data.form);
101 return generate_error_message(err);
103 local username, host, resource = jid.split(fields.accountjid);
104 if data.to ~= host then
105 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. data.to}};
107 if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
108 return { status = "completed", info = "Password successfully changed" };
110 return { status = "completed", error = { message = "User does not exist" } };
113 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = change_user_password_layout }, "executing";
117 function config_reload_handler(self, data, state)
118 local ok, err = prosody.reload_config();
120 return { status = "completed", info = "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
122 return { status = "completed", error = { message = "Failed to reload config: " .. tostring(err) } };
127 function delete_user_command_handler(self, data, state)
128 local delete_user_layout = dataforms_new{
129 title = "Deleting a User";
130 instructions = "Fill out this form to delete a user.";
132 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
133 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
137 if data.action == "cancel" then
138 return { status = "canceled" };
140 local fields, err = delete_user_layout:data(data.form);
142 return generate_error_message(err);
145 local succeeded = {};
146 for _, aJID in ipairs(fields.accountjids) do
147 local username, host, resource = jid.split(aJID);
148 if (host == data.to) and usermanager_user_exists(username, host) and usermanager_delete_user(username, host) then
149 module:log("debug", "User %s has been deleted", aJID);
150 succeeded[#succeeded+1] = aJID;
152 module:log("debug", "Tried to delete non-existant user %s", aJID);
153 failed[#failed+1] = aJID;
156 return {status = "completed", info = (#succeeded ~= 0 and
157 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
159 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
161 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = delete_user_layout }, "executing";
165 function disconnect_user(match_jid)
166 local node, hostname, givenResource = jid.split(match_jid);
167 local host = hosts[hostname];
168 local sessions = host.sessions[node] and host.sessions[node].sessions;
169 for resource, session in pairs(sessions or {}) do
170 if not givenResource or (resource == givenResource) then
171 module:log("debug", "Disconnecting %s@%s/%s", node, hostname, resource);
178 function end_user_session_handler(self, data, state)
179 local end_user_session_layout = dataforms_new{
180 title = "Ending a User Session";
181 instructions = "Fill out this form to end a user's session.";
183 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
184 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
188 if data.action == "cancel" then
189 return { status = "canceled" };
192 local fields, err = end_user_session_layout:data(data.form);
194 return generate_error_message(err);
197 local succeeded = {};
198 for _, aJID in ipairs(fields.accountjids) do
199 local username, host, resource = jid.split(aJID);
200 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) then
201 succeeded[#succeeded+1] = aJID;
203 failed[#failed+1] = aJID;
206 return {status = "completed", info = (#succeeded ~= 0 and
207 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
209 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
211 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = end_user_session_layout }, "executing";
215 function get_user_password_handler(self, data, state)
216 local get_user_password_layout = dataforms_new{
217 title = "Getting User's Password";
218 instructions = "Fill out this form to get a user's password.";
220 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
221 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the password" };
224 local get_user_password_result_layout = dataforms_new{
225 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
226 { name = "accountjid", type = "jid-single", label = "JID" };
227 { name = "password", type = "text-single", label = "Password" };
231 if data.action == "cancel" then
232 return { status = "canceled" };
234 local fields, err = get_user_password_layout:data(data.form);
236 return generate_error_message(err);
238 local user, host, resource = jid.split(fields.accountjid);
239 local accountjid = "";
241 if host ~= data.to then
242 return { status = "completed", error = { message = "Tried to get password for a user on " .. host .. " but command was sent to " .. data.to } };
243 elseif usermanager_user_exists(user, host) then
244 accountjid = fields.accountjid;
245 password = usermanager_get_password(user, host);
247 return { status = "completed", error = { message = "User does not exist" } };
249 return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
251 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_user_password_layout }, "executing";
255 function get_user_roster_handler(self, data, state)
256 local get_user_roster_layout = dataforms_new{
257 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
258 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the roster" };
261 local get_user_roster_result_layout = dataforms_new{
262 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
263 { name = "accountjid", type = "jid-single", label = "This is the roster for" };
264 { name = "roster", type = "text-multi", label = "Roster XML" };
268 if data.action == "cancel" then
269 return { status = "canceled" };
272 local fields, err = get_user_roster_layout:data(data.form);
275 return generate_error_message(err);
278 local user, host, resource = jid.split(fields.accountjid);
279 if host ~= data.to then
280 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. data.to } };
281 elseif not usermanager_user_exists(user, host) then
282 return { status = "completed", error = { message = "User does not exist" } };
284 local roster = rm_load_roster(user, host);
286 local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
287 for jid in pairs(roster) do
288 if jid ~= "pending" and jid then
291 subscription = roster[jid].subscription,
292 ask = roster[jid].ask,
293 name = roster[jid].name,
295 for group in pairs(roster[jid].groups) do
296 query:tag("group"):text(group):up();
302 local query_text = tostring(query):gsub("><", ">\n<");
304 local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result");
305 result:add_child(query);
306 return { status = "completed", other = result };
308 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_user_roster_layout }, "executing";
312 function get_user_stats_handler(self, data, state)
313 local get_user_stats_layout = dataforms_new{
314 title = "Get User Statistics";
315 instructions = "Fill out this form to gather user statistics.";
317 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
318 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for statistics" };
321 local get_user_stats_result_layout = dataforms_new{
322 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
323 { name = "ipaddresses", type = "text-multi", label = "IP Addresses" };
324 { name = "rostersize", type = "text-single", label = "Roster size" };
325 { name = "onlineresources", type = "text-multi", label = "Online Resources" };
329 if data.action == "cancel" then
330 return { status = "canceled" };
333 local fields, err = get_user_stats_layout:data(data.form);
336 return generate_error_message(err);
339 local user, host, resource = jid.split(fields.accountjid);
340 if host ~= data.to then
341 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. data.to } };
342 elseif not usermanager_user_exists(user, host) then
343 return { status = "completed", error = { message = "User does not exist" } };
345 local roster = rm_load_roster(user, host);
346 local rostersize = 0;
348 local resources = "";
349 for jid in pairs(roster) do
350 if jid ~= "pending" and jid then
351 rostersize = rostersize + 1;
354 for resource, session in pairs((hosts[host].sessions[user] and hosts[host].sessions[user].sessions) or {}) do
355 resources = resources .. "\n" .. resource;
356 IPs = IPs .. "\n" .. session.ip;
358 return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
359 onlineresources = resources}} };
361 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_user_stats_layout }, "executing";
365 function get_online_users_command_handler(self, data, state)
366 local get_online_users_layout = dataforms_new{
367 title = "Getting List of Online Users";
368 instructions = "How many users should be returned at most?";
370 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
371 { name = "max_items", type = "list-single", label = "Maximum number of users",
372 value = { "25", "50", "75", "100", "150", "200", "all" } };
373 { name = "details", type = "boolean", label = "Show details" };
376 local get_online_users_result_layout = dataforms_new{
377 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
378 { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
382 if data.action == "cancel" then
383 return { status = "canceled" };
386 local fields, err = get_online_users_layout:data(data.form);
389 return generate_error_message(err);
392 local max_items = nil
393 if fields.max_items ~= "all" then
394 max_items = tonumber(fields.max_items);
398 for username, user in pairs(hosts[data.to].sessions or {}) do
399 if (max_items ~= nil) and (count >= max_items) then
402 users[#users+1] = username.."@"..data.to;
404 if fields.details then
405 for resource, session in pairs(user.sessions or {}) do
406 local status, priority = "unavailable", tostring(session.priority or "-");
407 if session.presence then
408 status = session.presence:child_with_name("show");
410 status = status:get_text() or "[invalid!]";
412 status = "available";
415 users[#users+1] = " - "..resource..": "..status.."("..priority..")";
419 return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
421 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_online_users_layout }, "executing";
425 function list_modules_handler(self, data, state)
426 local result = dataforms_new {
427 title = "List of loaded modules";
429 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
430 { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
433 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n");
435 return { status = "completed", result = { layout = result; values = { modules = modules } } };
438 function load_module_handler(self, data, state)
439 local layout = dataforms_new {
440 title = "Load module";
441 instructions = "Specify the module to be loaded";
443 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
444 { name = "module", type = "text-single", required = true, label = "Module to be loaded:"};
447 if data.action == "cancel" then
448 return { status = "canceled" };
450 local fields, err = layout:data(data.form);
452 return generate_error_message(err);
454 if modulemanager.is_loaded(data.to, fields.module) then
455 return { status = "completed", info = "Module already loaded" };
457 local ok, err = modulemanager.load(data.to, fields.module);
459 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..data.to..'".' };
461 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..data.to..
462 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
465 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = layout }, "executing";
469 local function globally_load_module_handler(self, data, state)
470 local layout = dataforms_new {
471 title = "Globally load module";
472 instructions = "Specify the module to be loaded on all hosts";
474 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-load" };
475 { name = "module", type = "text-single", required = true, label = "Module to globally load:"};
478 local ok_list, err_list = {}, {};
480 if data.action == "cancel" then
481 return { status = "canceled" };
484 local fields, err = layout:data(data.form);
486 return generate_error_message(err);
489 local ok, err = modulemanager.load(data.to, fields.module);
491 ok_list[#ok_list + 1] = data.to;
493 err_list[#err_list + 1] = data.to .. " (Error: " .. tostring(err) .. ")";
496 -- Is this a global module?
497 if modulemanager.is_loaded("*", fields.module) and not modulemanager.is_loaded(data.to, fields.module) then
498 return { status = "completed", info = 'Global module '..fields.module..' loaded.' };
501 -- This is either a shared or "normal" module, load it on all other hosts
502 for host_name, host in pairs(hosts) do
503 if host_name ~= data.to and host.type == "local" then
504 local ok, err = modulemanager.load(host_name, fields.module);
506 ok_list[#ok_list + 1] = host_name;
508 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
513 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully loaded onto the hosts:\n"..t_concat(ok_list, "\n")) or "")
514 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
515 (#err_list > 0 and ("Failed to load the module "..fields.module.." onto the hosts:\n"..t_concat(err_list, "\n")) or "");
516 return { status = "completed", info = info };
518 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = layout }, "executing";
522 function reload_modules_handler(self, data, state)
523 local layout = dataforms_new {
524 title = "Reload modules";
525 instructions = "Select the modules to be reloaded";
527 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
528 { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
531 if data.action == "cancel" then
532 return { status = "canceled" };
534 local fields, err = layout:data(data.form);
536 return generate_error_message(err);
538 local ok_list, err_list = {}, {};
539 for _, module in ipairs(fields.modules) do
540 local ok, err = modulemanager.reload(data.to, module);
542 ok_list[#ok_list + 1] = module;
544 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
547 local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")
548 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
549 (#err_list > 0 and ("Failed to reload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
550 return { status = "completed", info = info };
552 local modules = array.collect(keys(hosts[data.to].modules)):sort();
553 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout; values = { modules = modules } } }, "executing";
557 local function globally_reload_module_handler(self, data, state)
558 local layout = dataforms_new {
559 title = "Globally reload module";
560 instructions = "Specify the module to reload on all hosts";
562 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-reload" };
563 { name = "module", type = "list-single", required = true, label = "Module to globally reload:"};
566 if data.action == "cancel" then
567 return { status = "canceled" };
570 local is_global = false;
571 local fields, err = layout:data(data.form);
573 return generate_error_message(err);
576 if modulemanager.is_loaded("*", fields.module) then
577 local ok, err = modulemanager.reload("*", fields.module);
579 return { status = "completed", info = 'Global module '..fields.module..' failed to reload: '..err };
584 local ok_list, err_list = {}, {};
585 for host_name, host in pairs(hosts) do
586 if modulemanager.is_loaded(host_name, fields.module) then
587 local ok, err = modulemanager.reload(host_name, fields.module);
589 ok_list[#ok_list + 1] = host_name;
591 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
596 if #ok_list == 0 and #err_list == 0 then
598 return { status = "completed", info = 'Successfully reloaded global module '..fields.module };
600 return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
604 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully reloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
605 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
606 (#err_list > 0 and ("Failed to reload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
607 return { status = "completed", info = info };
609 local loaded_modules = array(keys(modulemanager.get_modules("*")));
610 for _, host in pairs(hosts) do
611 loaded_modules:append(array(keys(host.modules)));
613 loaded_modules = array(keys(set.new(loaded_modules):items())):sort();
614 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout, values = { module = loaded_modules } } }, "executing";
618 function send_to_online(message, server)
620 sessions = { [server] = hosts[server] };
626 for domain, session in pairs(sessions) do
627 for user in pairs(session.sessions or {}) do
629 message.attr.from = domain;
630 message.attr.to = user.."@"..domain;
631 core_post_stanza(session, message);
638 function shut_down_service_handler(self, data, state)
639 local shut_down_service_layout = dataforms_new{
640 title = "Shutting Down the Service";
641 instructions = "Fill out this form to shut down the service.";
643 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
644 { name = "delay", type = "list-single", label = "Time delay before shutting down",
645 value = { {label = "30 seconds", value = "30"},
646 {label = "60 seconds", value = "60"},
647 {label = "90 seconds", value = "90"},
648 {label = "2 minutes", value = "120"},
649 {label = "3 minutes", value = "180"},
650 {label = "4 minutes", value = "240"},
651 {label = "5 minutes", value = "300"},
654 { name = "announcement", type = "text-multi", label = "Announcement" };
658 if data.action == "cancel" then
659 return { status = "canceled" };
662 local fields, err = shut_down_service_layout:data(data.form);
665 return generate_error_message(err);
668 if fields.announcement and #fields.announcement > 0 then
669 local message = st.message({type = "headline"}, fields.announcement):up()
670 :tag("subject"):text("Server is shutting down");
671 send_to_online(message);
674 timer_add_task(tonumber(fields.delay or "5"), function(time) prosody.shutdown("Shutdown by adhoc command") end);
676 return { status = "completed", info = "Server is about to shut down" };
678 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = shut_down_service_layout }, "executing";
682 function unload_modules_handler(self, data, state)
683 local layout = dataforms_new {
684 title = "Unload modules";
685 instructions = "Select the modules to be unloaded";
687 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
688 { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
691 if data.action == "cancel" then
692 return { status = "canceled" };
694 local fields, err = layout:data(data.form);
696 return generate_error_message(err);
698 local ok_list, err_list = {}, {};
699 for _, module in ipairs(fields.modules) do
700 local ok, err = modulemanager.unload(data.to, module);
702 ok_list[#ok_list + 1] = module;
704 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
707 local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")
708 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
709 (#err_list > 0 and ("Failed to unload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
710 return { status = "completed", info = info };
712 local modules = array.collect(keys(hosts[data.to].modules)):sort();
713 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout; values = { modules = modules } } }, "executing";
717 local function globally_unload_module_handler(self, data, state)
718 local layout = dataforms_new {
719 title = "Globally unload module";
720 instructions = "Specify a module to unload on all hosts";
722 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-unload" };
723 { name = "module", type = "list-single", required = true, label = "Module to globally unload:"};
726 if data.action == "cancel" then
727 return { status = "canceled" };
730 local is_global = false;
731 local fields, err = layout:data(data.form);
733 return generate_error_message(err);
736 if modulemanager.is_loaded("*", fields.module) then
737 local ok, err = modulemanager.unload("*", fields.module);
739 return { status = "completed", info = 'Global module '..fields.module..' failed to unload: '..err };
744 local ok_list, err_list = {}, {};
745 for host_name, host in pairs(hosts) do
746 if modulemanager.is_loaded(host_name, fields.module) then
747 local ok, err = modulemanager.unload(host_name, fields.module);
749 ok_list[#ok_list + 1] = host_name;
751 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
756 if #ok_list == 0 and #err_list == 0 then
758 return { status = "completed", info = 'Successfully unloaded global module '..fields.module };
760 return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
764 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully unloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
765 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
766 (#err_list > 0 and ("Failed to unload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
767 return { status = "completed", info = info };
769 local loaded_modules = array(keys(modulemanager.get_modules("*")));
770 for _, host in pairs(hosts) do
771 loaded_modules:append(array(keys(host.modules)));
773 loaded_modules = array(keys(set.new(loaded_modules):items())):sort();
774 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout, values = { module = loaded_modules } } }, "executing";
779 function activate_host_handler(self, data, state)
780 local layout = dataforms_new {
781 title = "Activate host";
784 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
785 { name = "host", type = "text-single", required = true, label = "Host:"};
788 if data.action == "cancel" then
789 return { status = "canceled" };
791 local fields, err = layout:data(data.form);
793 return generate_error_message(err);
795 local ok, err = hostmanager_activate(fields.host);
798 return { status = "completed", info = fields.host .. " activated" };
800 return { status = "canceled", error = err }
803 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout } }, "executing";
807 function deactivate_host_handler(self, data, state)
808 local layout = dataforms_new {
809 title = "Deactivate host";
812 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
813 { name = "host", type = "text-single", required = true, label = "Host:"};
816 if data.action == "cancel" then
817 return { status = "canceled" };
819 local fields, err = layout:data(data.form);
821 return generate_error_message(err);
823 local ok, err = hostmanager_deactivate(fields.host);
826 return { status = "completed", info = fields.host .. " deactivated" };
828 return { status = "canceled", error = err }
831 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout } }, "executing";
836 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
837 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
838 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
839 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
840 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
841 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
842 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
843 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
844 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");
845 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
846 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
847 local globally_load_module_desc = adhoc_new("Globally load module", "http://prosody.im/protocol/modules#global-load", globally_load_module_handler, "global_admin");
848 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
849 local globally_reload_module_desc = adhoc_new("Globally reload module", "http://prosody.im/protocol/modules#global-reload", globally_reload_module_handler, "global_admin");
850 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
851 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
852 local globally_unload_module_desc = adhoc_new("Globally unload module", "http://prosody.im/protocol/modules#global-unload", globally_unload_module_handler, "global_admin");
853 local activate_host_desc = adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler, "global_admin");
854 local deactivate_host_desc = adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler, "global_admin");
856 module:provides("adhoc", add_user_desc);
857 module:provides("adhoc", change_user_password_desc);
858 module:provides("adhoc", config_reload_desc);
859 module:provides("adhoc", delete_user_desc);
860 module:provides("adhoc", end_user_session_desc);
861 module:provides("adhoc", get_user_password_desc);
862 module:provides("adhoc", get_user_roster_desc);
863 module:provides("adhoc", get_user_stats_desc);
864 module:provides("adhoc", get_online_users_desc);
865 module:provides("adhoc", list_modules_desc);
866 module:provides("adhoc", load_module_desc);
867 module:provides("adhoc", globally_load_module_desc);
868 module:provides("adhoc", reload_modules_desc);
869 module:provides("adhoc", globally_reload_module_desc);
870 module:provides("adhoc", shut_down_service_desc);
871 module:provides("adhoc", unload_modules_desc);
872 module:provides("adhoc", globally_unload_module_desc);
873 module:provides("adhoc", activate_host_desc);
874 module:provides("adhoc", deactivate_host_desc);