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;
12 local t_sort = table.sort;
14 local module_host = module:get_host();
16 local keys = require "util.iterators".keys;
17 local usermanager_user_exists = require "core.usermanager".user_exists;
18 local usermanager_create_user = require "core.usermanager".create_user;
19 local usermanager_delete_user = require "core.usermanager".delete_user;
20 local usermanager_get_password = require "core.usermanager".get_password;
21 local usermanager_set_password = require "core.usermanager".set_password;
22 local hostmanager_activate = require "core.hostmanager".activate;
23 local hostmanager_deactivate = require "core.hostmanager".deactivate;
24 local rm_load_roster = require "core.rostermanager".load_roster;
25 local st, jid = require "util.stanza", require "util.jid";
26 local timer_add_task = require "util.timer".add_task;
27 local dataforms_new = require "util.dataforms".new;
28 local array = require "util.array";
29 local modulemanager = require "core.modulemanager";
30 local core_post_stanza = prosody.core_post_stanza;
31 local adhoc_simple = require "util.adhoc".new_simple_form;
32 local adhoc_initial = require "util.adhoc".new_initial_data_form;
33 local set = require"util.set";
35 module:depends("adhoc");
36 local adhoc_new = module:require "adhoc".new;
38 local function generate_error_message(errors)
40 for name, err in pairs(errors) do
41 errmsg[#errmsg + 1] = name .. ": " .. err;
43 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
47 local add_user_layout = dataforms_new{
48 title = "Adding a User";
49 instructions = "Fill out this form to add a user.";
51 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
52 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
53 { name = "password", type = "text-private", label = "The password for this account" };
54 { name = "password-verify", type = "text-private", label = "Retype password" };
57 local add_user_command_handler = adhoc_simple(add_user_layout, function(fields, err)
59 return generate_error_message(err);
61 local username, host, resource = jid.split(fields.accountjid);
62 if module_host ~= host then
63 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. module_host}};
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" } };
82 -- Changing a user's password
83 local change_user_password_layout = dataforms_new{
84 title = "Changing a User Password";
85 instructions = "Fill out this form to change a user's password.";
87 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
88 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for this account" };
89 { name = "password", type = "text-private", required = true, label = "The password for this account" };
92 local change_user_password_command_handler = adhoc_simple(change_user_password_layout, function(fields, err)
94 return generate_error_message(err);
96 local username, host, resource = jid.split(fields.accountjid);
97 if module_host ~= host then
98 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. module_host}};
100 if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
101 return { status = "completed", info = "Password successfully changed" };
103 return { status = "completed", error = { message = "User does not exist" } };
107 -- Reloading the config
108 local function config_reload_handler(self, data, state)
109 local ok, err = prosody.reload_config();
111 return { status = "completed", info = "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
113 return { status = "completed", error = { message = "Failed to reload config: " .. tostring(err) } };
117 -- Deleting a user's account
118 local delete_user_layout = dataforms_new{
119 title = "Deleting a User";
120 instructions = "Fill out this form to delete a user.";
122 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
123 { name = "accountjids", type = "jid-multi", required = true, label = "The Jabber ID(s) to delete" };
126 local delete_user_command_handler = adhoc_simple(delete_user_layout, function(fields, err)
128 return generate_error_message(err);
131 local succeeded = {};
132 for _, aJID in ipairs(fields.accountjids) do
133 local username, host, resource = jid.split(aJID);
134 if (host == module_host) and usermanager_user_exists(username, host) and usermanager_delete_user(username, host) then
135 module:log("debug", "User %s has been deleted", aJID);
136 succeeded[#succeeded+1] = aJID;
138 module:log("debug", "Tried to delete non-existant user %s", aJID);
139 failed[#failed+1] = aJID;
142 return {status = "completed", info = (#succeeded ~= 0 and
143 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
145 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
148 -- Ending a user's session
149 local function disconnect_user(match_jid)
150 local node, hostname, givenResource = jid.split(match_jid);
151 local host = hosts[hostname];
152 local sessions = host.sessions[node] and host.sessions[node].sessions;
153 for resource, session in pairs(sessions or {}) do
154 if not givenResource or (resource == givenResource) then
155 module:log("debug", "Disconnecting %s@%s/%s", node, hostname, resource);
162 local end_user_session_layout = dataforms_new{
163 title = "Ending a User Session";
164 instructions = "Fill out this form to end a user's session.";
166 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
167 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions", required = true };
170 local end_user_session_handler = adhoc_simple(end_user_session_layout, function(fields, err)
172 return generate_error_message(err);
175 local succeeded = {};
176 for _, aJID in ipairs(fields.accountjids) do
177 local username, host, resource = jid.split(aJID);
178 if (host == module_host) and usermanager_user_exists(username, host) and disconnect_user(aJID) then
179 succeeded[#succeeded+1] = aJID;
181 failed[#failed+1] = aJID;
184 return {status = "completed", info = (#succeeded ~= 0 and
185 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
187 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
190 -- Getting a user's password
191 local get_user_password_layout = dataforms_new{
192 title = "Getting User's Password";
193 instructions = "Fill out this form to get a user's password.";
195 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
196 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the password" };
199 local get_user_password_result_layout = dataforms_new{
200 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
201 { name = "accountjid", type = "jid-single", label = "JID" };
202 { name = "password", type = "text-single", label = "Password" };
205 local get_user_password_handler = adhoc_simple(get_user_password_layout, function(fields, err)
207 return generate_error_message(err);
209 local user, host, resource = jid.split(fields.accountjid);
210 local accountjid = "";
212 if host ~= module_host then
213 return { status = "completed", error = { message = "Tried to get password for a user on " .. host .. " but command was sent to " .. module_host } };
214 elseif usermanager_user_exists(user, host) then
215 accountjid = fields.accountjid;
216 password = usermanager_get_password(user, host);
218 return { status = "completed", error = { message = "User does not exist" } };
220 return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
223 -- Getting a user's roster
224 local get_user_roster_layout = dataforms_new{
225 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
226 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the roster" };
229 local get_user_roster_result_layout = dataforms_new{
230 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
231 { name = "accountjid", type = "jid-single", label = "This is the roster for" };
232 { name = "roster", type = "text-multi", label = "Roster XML" };
235 local get_user_roster_handler = adhoc_simple(get_user_roster_layout, function(fields, err)
237 return generate_error_message(err);
240 local user, host, resource = jid.split(fields.accountjid);
241 if host ~= module_host then
242 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. module_host } };
243 elseif not usermanager_user_exists(user, host) then
244 return { status = "completed", error = { message = "User does not exist" } };
246 local roster = rm_load_roster(user, host);
248 local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
249 for jid in pairs(roster) do
253 subscription = roster[jid].subscription,
254 ask = roster[jid].ask,
255 name = roster[jid].name,
257 for group in pairs(roster[jid].groups) do
258 query:tag("group"):text(group):up();
264 local query_text = tostring(query):gsub("><", ">\n<");
266 local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result");
267 result:add_child(query);
268 return { status = "completed", other = result };
271 -- Getting user statistics
272 local get_user_stats_layout = dataforms_new{
273 title = "Get User Statistics";
274 instructions = "Fill out this form to gather user statistics.";
276 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
277 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for statistics" };
280 local get_user_stats_result_layout = dataforms_new{
281 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
282 { name = "ipaddresses", type = "text-multi", label = "IP Addresses" };
283 { name = "rostersize", type = "text-single", label = "Roster size" };
284 { name = "onlineresources", type = "text-multi", label = "Online Resources" };
287 local get_user_stats_handler = adhoc_simple(get_user_stats_layout, function(fields, err)
289 return generate_error_message(err);
292 local user, host, resource = jid.split(fields.accountjid);
293 if host ~= module_host then
294 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. module_host } };
295 elseif not usermanager_user_exists(user, host) then
296 return { status = "completed", error = { message = "User does not exist" } };
298 local roster = rm_load_roster(user, host);
299 local rostersize = 0;
301 local resources = "";
302 for jid in pairs(roster) do
304 rostersize = rostersize + 1;
307 for resource, session in pairs((hosts[host].sessions[user] and hosts[host].sessions[user].sessions) or {}) do
308 resources = resources .. "\n" .. resource;
309 IPs = IPs .. "\n" .. session.ip;
311 return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
312 onlineresources = resources}} };
315 -- Getting a list of online users
316 local get_online_users_layout = dataforms_new{
317 title = "Getting List of Online Users";
318 instructions = "How many users should be returned at most?";
320 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
321 { name = "max_items", type = "list-single", label = "Maximum number of users",
322 value = { "25", "50", "75", "100", "150", "200", "all" } };
323 { name = "details", type = "boolean", label = "Show details" };
326 local get_online_users_result_layout = dataforms_new{
327 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
328 { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
331 local get_online_users_command_handler = adhoc_simple(get_online_users_layout, function(fields, err)
333 return generate_error_message(err);
336 local max_items = nil
337 if fields.max_items ~= "all" then
338 max_items = tonumber(fields.max_items);
342 for username, user in pairs(hosts[module_host].sessions or {}) do
343 if (max_items ~= nil) and (count >= max_items) then
346 users[#users+1] = username.."@"..module_host;
348 if fields.details then
349 for resource, session in pairs(user.sessions or {}) do
350 local status, priority, ip = "unavailable", tostring(session.priority or "-"), session.ip or "<unknown>";
351 if session.presence then
352 status = session.presence:child_with_name("show");
354 status = status:get_text() or "[invalid!]";
356 status = "available";
359 users[#users+1] = " - "..resource..": "..status.."("..priority.."), IP: ["..ip.."]";
363 return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
366 -- Getting a list of S2S connections (this host)
367 local list_s2s_this_result = dataforms_new {
368 title = "List of S2S connections on this host";
370 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/s2s#list" };
371 { name = "sessions", type = "text-multi", label = "Connections:" };
372 { name = "num_in", type = "text-single", label = "#incomming connections:" };
373 { name = "num_out", type = "text-single", label = "#outgoing connections:" };
376 local function session_flags(session, line)
380 line[#line+1] = "["..session.id.."]"
382 line[#line+1] = "["..session.type..(tostring(session):match("%x*$")).."]"
386 if session.cert_identity_status == "valid" then
387 flags[#flags+1] = "authenticated";
389 if session.secure then
390 flags[#flags+1] = "encrypted";
392 if session.compressed then
393 flags[#flags+1] = "compressed)";
395 if session.smacks then
396 flags[#flags+1] = "sm";
398 if session.ip and session.ip:match(":") then
399 flags[#flags+1] = "IPv6";
401 line[#line+1] = "("..t_concat(flags, ", ")..")";
403 return t_concat(line, " ");
406 local function list_s2s_this_handler(self, data, state)
407 local count_in, count_out = 0, 0;
410 local s2s_sessions = module:shared"/*/s2s/sessions";
411 for _, session in pairs(s2s_sessions) do
412 local remotehost, localhost, direction;
413 if session.direction == "outgoing" then
415 count_out = count_out + 1;
416 remotehost, localhost = session.to_host or "?", session.from_host or "?";
419 count_in = count_in + 1;
420 remotehost, localhost = session.from_host or "?", session.to_host or "?";
422 local sess_lines = { r = remotehost,
423 session_flags(session, { "", direction, remotehost or "?" })};
425 if remotehost:match(module_host) or localhost:match(module_host) then
426 s2s_list[#s2s_list+1] = sess_lines;
430 t_sort(s2s_list, function(a, b)
434 for i, sess_lines in ipairs(s2s_list) do
435 s2s_list[i] = sess_lines[1];
438 return { status = "completed", result = { layout = list_s2s_this_result; values = {
439 sessions = t_concat(s2s_list, "\n"),
440 num_in = tostring(count_in),
441 num_out = tostring(count_out)
445 -- Getting a list of loaded modules
446 local list_modules_result = dataforms_new {
447 title = "List of loaded modules";
449 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
450 { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
453 local function list_modules_handler(self, data, state)
454 local modules = array.collect(keys(hosts[module_host].modules)):sort():concat("\n");
455 return { status = "completed", result = { layout = list_modules_result; values = { modules = modules } } };
459 local load_module_layout = dataforms_new {
460 title = "Load module";
461 instructions = "Specify the module to be loaded";
463 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
464 { name = "module", type = "text-single", required = true, label = "Module to be loaded:"};
467 local load_module_handler = adhoc_simple(load_module_layout, function(fields, err)
469 return generate_error_message(err);
471 if modulemanager.is_loaded(module_host, fields.module) then
472 return { status = "completed", info = "Module already loaded" };
474 local ok, err = modulemanager.load(module_host, fields.module);
476 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..module_host..'".' };
478 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..module_host..
479 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
483 -- Globally loading a module
484 local globally_load_module_layout = dataforms_new {
485 title = "Globally load module";
486 instructions = "Specify the module to be loaded on all hosts";
488 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-load" };
489 { name = "module", type = "text-single", required = true, label = "Module to globally load:"};
492 local globally_load_module_handler = adhoc_simple(globally_load_module_layout, function(fields, err)
493 local ok_list, err_list = {}, {};
496 return generate_error_message(err);
499 local ok, err = modulemanager.load(module_host, fields.module);
501 ok_list[#ok_list + 1] = module_host;
503 err_list[#err_list + 1] = module_host .. " (Error: " .. tostring(err) .. ")";
506 -- Is this a global module?
507 if modulemanager.is_loaded("*", fields.module) and not modulemanager.is_loaded(module_host, fields.module) then
508 return { status = "completed", info = 'Global module '..fields.module..' loaded.' };
511 -- This is either a shared or "normal" module, load it on all other hosts
512 for host_name, host in pairs(hosts) do
513 if host_name ~= module_host and host.type == "local" then
514 local ok, err = modulemanager.load(host_name, fields.module);
516 ok_list[#ok_list + 1] = host_name;
518 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
523 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully loaded onto the hosts:\n"..t_concat(ok_list, "\n")) or "")
524 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
525 (#err_list > 0 and ("Failed to load the module "..fields.module.." onto the hosts:\n"..t_concat(err_list, "\n")) or "");
526 return { status = "completed", info = info };
530 local reload_modules_layout = dataforms_new {
531 title = "Reload modules";
532 instructions = "Select the modules to be reloaded";
534 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
535 { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
538 local reload_modules_handler = adhoc_initial(reload_modules_layout, function()
539 return { modules = array.collect(keys(hosts[module_host].modules)):sort() };
540 end, function(fields, err)
542 return generate_error_message(err);
544 local ok_list, err_list = {}, {};
545 for _, module in ipairs(fields.modules) do
546 local ok, err = modulemanager.reload(module_host, module);
548 ok_list[#ok_list + 1] = module;
550 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
553 local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..module_host..":\n"..t_concat(ok_list, "\n")) or "")
554 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
555 (#err_list > 0 and ("Failed to reload the following modules on host "..module_host..":\n"..t_concat(err_list, "\n")) or "");
556 return { status = "completed", info = info };
559 -- Globally reloading a module
560 local globally_reload_module_layout = dataforms_new {
561 title = "Globally reload module";
562 instructions = "Specify the module to reload on all hosts";
564 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-reload" };
565 { name = "module", type = "list-single", required = true, label = "Module to globally reload:"};
568 local globally_reload_module_handler = adhoc_initial(globally_reload_module_layout, function()
569 local loaded_modules = array(keys(modulemanager.get_modules("*")));
570 for _, host in pairs(hosts) do
571 loaded_modules:append(array(keys(host.modules)));
573 loaded_modules = array(set.new(loaded_modules):items()):sort();
574 return { module = loaded_modules };
575 end, function(fields, err)
576 local is_global = false;
579 return generate_error_message(err);
582 if modulemanager.is_loaded("*", fields.module) then
583 local ok, err = modulemanager.reload("*", fields.module);
585 return { status = "completed", info = 'Global module '..fields.module..' failed to reload: '..err };
590 local ok_list, err_list = {}, {};
591 for host_name, host in pairs(hosts) do
592 if modulemanager.is_loaded(host_name, fields.module) then
593 local ok, err = modulemanager.reload(host_name, fields.module);
595 ok_list[#ok_list + 1] = host_name;
597 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
602 if #ok_list == 0 and #err_list == 0 then
604 return { status = "completed", info = 'Successfully reloaded global module '..fields.module };
606 return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
610 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully reloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
611 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
612 (#err_list > 0 and ("Failed to reload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
613 return { status = "completed", info = info };
616 local function send_to_online(message, server)
619 sessions = { [server] = hosts[server] };
625 for domain, session in pairs(sessions) do
626 for user in pairs(session.sessions or {}) do
628 message.attr.from = domain;
629 message.attr.to = user.."@"..domain;
630 core_post_stanza(session, message);
637 -- Shutting down the service
638 local shut_down_service_layout = dataforms_new{
639 title = "Shutting Down the Service";
640 instructions = "Fill out this form to shut down the service.";
642 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
643 { name = "delay", type = "list-single", label = "Time delay before shutting down",
644 value = { {label = "30 seconds", value = "30"},
645 {label = "60 seconds", value = "60"},
646 {label = "90 seconds", value = "90"},
647 {label = "2 minutes", value = "120"},
648 {label = "3 minutes", value = "180"},
649 {label = "4 minutes", value = "240"},
650 {label = "5 minutes", value = "300"},
653 { name = "announcement", type = "text-multi", label = "Announcement" };
656 local shut_down_service_handler = adhoc_simple(shut_down_service_layout, function(fields, err)
658 return generate_error_message(err);
661 if fields.announcement and #fields.announcement > 0 then
662 local message = st.message({type = "headline"}, fields.announcement):up()
663 :tag("subject"):text("Server is shutting down");
664 send_to_online(message);
667 timer_add_task(tonumber(fields.delay or "5"), function(time) prosody.shutdown("Shutdown by adhoc command") end);
669 return { status = "completed", info = "Server is about to shut down" };
673 local unload_modules_layout = dataforms_new {
674 title = "Unload modules";
675 instructions = "Select the modules to be unloaded";
677 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
678 { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
681 local unload_modules_handler = adhoc_initial(unload_modules_layout, function()
682 return { modules = array.collect(keys(hosts[module_host].modules)):sort() };
683 end, function(fields, err)
685 return generate_error_message(err);
687 local ok_list, err_list = {}, {};
688 for _, module in ipairs(fields.modules) do
689 local ok, err = modulemanager.unload(module_host, module);
691 ok_list[#ok_list + 1] = module;
693 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
696 local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..module_host..":\n"..t_concat(ok_list, "\n")) or "")
697 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
698 (#err_list > 0 and ("Failed to unload the following modules on host "..module_host..":\n"..t_concat(err_list, "\n")) or "");
699 return { status = "completed", info = info };
702 -- Globally unloading a module
703 local globally_unload_module_layout = dataforms_new {
704 title = "Globally unload module";
705 instructions = "Specify a module to unload on all hosts";
707 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-unload" };
708 { name = "module", type = "list-single", required = true, label = "Module to globally unload:"};
711 local globally_unload_module_handler = adhoc_initial(globally_unload_module_layout, function()
712 local loaded_modules = array(keys(modulemanager.get_modules("*")));
713 for _, host in pairs(hosts) do
714 loaded_modules:append(array(keys(host.modules)));
716 loaded_modules = array(set.new(loaded_modules):items()):sort();
717 return { module = loaded_modules };
718 end, function(fields, err)
719 local is_global = false;
721 return generate_error_message(err);
724 if modulemanager.is_loaded("*", fields.module) then
725 local ok, err = modulemanager.unload("*", fields.module);
727 return { status = "completed", info = 'Global module '..fields.module..' failed to unload: '..err };
732 local ok_list, err_list = {}, {};
733 for host_name, host in pairs(hosts) do
734 if modulemanager.is_loaded(host_name, fields.module) then
735 local ok, err = modulemanager.unload(host_name, fields.module);
737 ok_list[#ok_list + 1] = host_name;
739 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
744 if #ok_list == 0 and #err_list == 0 then
746 return { status = "completed", info = 'Successfully unloaded global module '..fields.module };
748 return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
752 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully unloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
753 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
754 (#err_list > 0 and ("Failed to unload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
755 return { status = "completed", info = info };
759 local activate_host_layout = dataforms_new {
760 title = "Activate host";
763 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
764 { name = "host", type = "text-single", required = true, label = "Host:"};
767 local activate_host_handler = adhoc_simple(activate_host_layout, function(fields, err)
769 return generate_error_message(err);
771 local ok, err = hostmanager_activate(fields.host);
774 return { status = "completed", info = fields.host .. " activated" };
776 return { status = "canceled", error = err }
780 -- Deactivating a host
781 local deactivate_host_layout = dataforms_new {
782 title = "Deactivate host";
785 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
786 { name = "host", type = "text-single", required = true, label = "Host:"};
789 local deactivate_host_handler = adhoc_simple(deactivate_host_layout, function(fields, err)
791 return generate_error_message(err);
793 local ok, err = hostmanager_deactivate(fields.host);
796 return { status = "completed", info = fields.host .. " deactivated" };
798 return { status = "canceled", error = err }
803 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
804 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
805 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
806 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
807 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
808 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
809 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
810 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
811 local get_online_users_desc = adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users-list", get_online_users_command_handler, "admin");
812 local list_s2s_this_desc = adhoc_new("List S2S connections", "http://prosody.im/protocol/s2s#list", list_s2s_this_handler, "admin");
813 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
814 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
815 local globally_load_module_desc = adhoc_new("Globally load module", "http://prosody.im/protocol/modules#global-load", globally_load_module_handler, "global_admin");
816 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
817 local globally_reload_module_desc = adhoc_new("Globally reload module", "http://prosody.im/protocol/modules#global-reload", globally_reload_module_handler, "global_admin");
818 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
819 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
820 local globally_unload_module_desc = adhoc_new("Globally unload module", "http://prosody.im/protocol/modules#global-unload", globally_unload_module_handler, "global_admin");
821 local activate_host_desc = adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler, "global_admin");
822 local deactivate_host_desc = adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler, "global_admin");
824 module:provides("adhoc", add_user_desc);
825 module:provides("adhoc", change_user_password_desc);
826 module:provides("adhoc", config_reload_desc);
827 module:provides("adhoc", delete_user_desc);
828 module:provides("adhoc", end_user_session_desc);
829 module:provides("adhoc", get_user_password_desc);
830 module:provides("adhoc", get_user_roster_desc);
831 module:provides("adhoc", get_user_stats_desc);
832 module:provides("adhoc", get_online_users_desc);
833 module:provides("adhoc", list_s2s_this_desc);
834 module:provides("adhoc", list_modules_desc);
835 module:provides("adhoc", load_module_desc);
836 module:provides("adhoc", globally_load_module_desc);
837 module:provides("adhoc", reload_modules_desc);
838 module:provides("adhoc", globally_reload_module_desc);
839 module:provides("adhoc", shut_down_service_desc);
840 module:provides("adhoc", unload_modules_desc);
841 module:provides("adhoc", globally_unload_module_desc);
842 module:provides("adhoc", activate_host_desc);
843 module:provides("adhoc", deactivate_host_desc);