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 "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;
34 module:depends("adhoc");
35 local adhoc_new = module:require "adhoc".new;
37 local function generate_error_message(errors)
39 for name, err in pairs(errors) do
40 errmsg[#errmsg + 1] = name .. ": " .. err;
42 return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
46 local add_user_layout = dataforms_new{
47 title = "Adding a User";
48 instructions = "Fill out this form to add a user.";
50 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
51 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
52 { name = "password", type = "text-private", label = "The password for this account" };
53 { name = "password-verify", type = "text-private", label = "Retype password" };
56 local add_user_command_handler = adhoc_simple(add_user_layout, function(fields, err)
58 return generate_error_message(err);
60 local username, host, resource = jid.split(fields.accountjid);
61 if module_host ~= host then
62 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. module_host}};
64 if (fields["password"] == fields["password-verify"]) and username and host then
65 if usermanager_user_exists(username, host) then
66 return { status = "completed", error = { message = "Account already exists" } };
68 if usermanager_create_user(username, fields.password, host) then
69 module:log("info", "Created new account %s@%s", username, host);
70 return { status = "completed", info = "Account successfully created" };
72 return { status = "completed", error = { message = "Failed to write data to disk" } };
76 module:log("debug", "Invalid data, password mismatch or empty username while creating account for %s", fields.accountjid or "<nil>");
77 return { status = "completed", error = { message = "Invalid data.\nPassword mismatch, or empty username" } };
81 -- Changing a user's password
82 local change_user_password_layout = dataforms_new{
83 title = "Changing a User Password";
84 instructions = "Fill out this form to change a user's password.";
86 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
87 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for this account" };
88 { name = "password", type = "text-private", required = true, label = "The password for this account" };
91 local change_user_password_command_handler = adhoc_simple(change_user_password_layout, function(fields, err)
93 return generate_error_message(err);
95 local username, host, resource = jid.split(fields.accountjid);
96 if module_host ~= host then
97 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. module_host}};
99 if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
100 return { status = "completed", info = "Password successfully changed" };
102 return { status = "completed", error = { message = "User does not exist" } };
106 -- Reloading the config
107 local function config_reload_handler(self, data, state)
108 local ok, err = prosody.reload_config();
110 return { status = "completed", info = "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
112 return { status = "completed", error = { message = "Failed to reload config: " .. tostring(err) } };
116 -- Deleting a user's account
117 local delete_user_layout = dataforms_new{
118 title = "Deleting a User";
119 instructions = "Fill out this form to delete a user.";
121 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
122 { name = "accountjids", type = "jid-multi", required = true, label = "The Jabber ID(s) to delete" };
125 local delete_user_command_handler = adhoc_simple(delete_user_layout, function(fields, err)
127 return generate_error_message(err);
130 local succeeded = {};
131 for _, aJID in ipairs(fields.accountjids) do
132 local username, host, resource = jid.split(aJID);
133 if (host == module_host) and usermanager_user_exists(username, host) and usermanager_delete_user(username, host) then
134 module:log("debug", "User %s has been deleted", aJID);
135 succeeded[#succeeded+1] = aJID;
137 module:log("debug", "Tried to delete non-existant user %s", aJID);
138 failed[#failed+1] = aJID;
141 return {status = "completed", info = (#succeeded ~= 0 and
142 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
144 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
147 -- Ending a user's session
148 local function disconnect_user(match_jid)
149 local node, hostname, givenResource = jid.split(match_jid);
150 local host = hosts[hostname];
151 local sessions = host.sessions[node] and host.sessions[node].sessions;
152 for resource, session in pairs(sessions or {}) do
153 if not givenResource or (resource == givenResource) then
154 module:log("debug", "Disconnecting %s@%s/%s", node, hostname, resource);
161 local end_user_session_layout = dataforms_new{
162 title = "Ending a User Session";
163 instructions = "Fill out this form to end a user's session.";
165 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
166 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions", required = true };
169 local end_user_session_handler = adhoc_simple(end_user_session_layout, function(fields, err)
171 return generate_error_message(err);
174 local succeeded = {};
175 for _, aJID in ipairs(fields.accountjids) do
176 local username, host, resource = jid.split(aJID);
177 if (host == module_host) and usermanager_user_exists(username, host) and disconnect_user(aJID) then
178 succeeded[#succeeded+1] = aJID;
180 failed[#failed+1] = aJID;
183 return {status = "completed", info = (#succeeded ~= 0 and
184 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
186 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
189 -- Getting a user's password
190 local get_user_password_layout = dataforms_new{
191 title = "Getting User's Password";
192 instructions = "Fill out this form to get a user's password.";
194 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
195 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the password" };
198 local get_user_password_result_layout = dataforms_new{
199 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
200 { name = "accountjid", type = "jid-single", label = "JID" };
201 { name = "password", type = "text-single", label = "Password" };
204 local get_user_password_handler = adhoc_simple(get_user_password_layout, function(fields, err)
206 return generate_error_message(err);
208 local user, host, resource = jid.split(fields.accountjid);
209 local accountjid = "";
211 if host ~= module_host then
212 return { status = "completed", error = { message = "Tried to get password for a user on " .. host .. " but command was sent to " .. module_host } };
213 elseif usermanager_user_exists(user, host) then
214 accountjid = fields.accountjid;
215 password = usermanager_get_password(user, host);
217 return { status = "completed", error = { message = "User does not exist" } };
219 return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
222 -- Getting a user's roster
223 local get_user_roster_layout = dataforms_new{
224 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
225 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the roster" };
228 local get_user_roster_result_layout = dataforms_new{
229 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
230 { name = "accountjid", type = "jid-single", label = "This is the roster for" };
231 { name = "roster", type = "text-multi", label = "Roster XML" };
234 local get_user_roster_handler = adhoc_simple(get_user_roster_layout, function(fields, err)
236 return generate_error_message(err);
239 local user, host, resource = jid.split(fields.accountjid);
240 if host ~= module_host then
241 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. module_host } };
242 elseif not usermanager_user_exists(user, host) then
243 return { status = "completed", error = { message = "User does not exist" } };
245 local roster = rm_load_roster(user, host);
247 local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
248 for jid in pairs(roster) do
249 if jid ~= "pending" and jid then
252 subscription = roster[jid].subscription,
253 ask = roster[jid].ask,
254 name = roster[jid].name,
256 for group in pairs(roster[jid].groups) do
257 query:tag("group"):text(group):up();
263 local query_text = tostring(query):gsub("><", ">\n<");
265 local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result");
266 result:add_child(query);
267 return { status = "completed", other = result };
270 -- Getting user statistics
271 local get_user_stats_layout = dataforms_new{
272 title = "Get User Statistics";
273 instructions = "Fill out this form to gather user statistics.";
275 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
276 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for statistics" };
279 local get_user_stats_result_layout = dataforms_new{
280 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
281 { name = "ipaddresses", type = "text-multi", label = "IP Addresses" };
282 { name = "rostersize", type = "text-single", label = "Roster size" };
283 { name = "onlineresources", type = "text-multi", label = "Online Resources" };
286 local get_user_stats_handler = adhoc_simple(get_user_stats_layout, function(fields, err)
288 return generate_error_message(err);
291 local user, host, resource = jid.split(fields.accountjid);
292 if host ~= module_host then
293 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. module_host } };
294 elseif not usermanager_user_exists(user, host) then
295 return { status = "completed", error = { message = "User does not exist" } };
297 local roster = rm_load_roster(user, host);
298 local rostersize = 0;
300 local resources = "";
301 for jid in pairs(roster) do
302 if jid ~= "pending" and jid then
303 rostersize = rostersize + 1;
306 for resource, session in pairs((hosts[host].sessions[user] and hosts[host].sessions[user].sessions) or {}) do
307 resources = resources .. "\n" .. resource;
308 IPs = IPs .. "\n" .. session.ip;
310 return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
311 onlineresources = resources}} };
314 -- Getting a list of online users
315 local get_online_users_layout = dataforms_new{
316 title = "Getting List of Online Users";
317 instructions = "How many users should be returned at most?";
319 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
320 { name = "max_items", type = "list-single", label = "Maximum number of users",
321 value = { "25", "50", "75", "100", "150", "200", "all" } };
322 { name = "details", type = "boolean", label = "Show details" };
325 local get_online_users_result_layout = dataforms_new{
326 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
327 { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
330 local get_online_users_command_handler = adhoc_simple(get_online_users_layout, function(fields, err)
332 return generate_error_message(err);
335 local max_items = nil
336 if fields.max_items ~= "all" then
337 max_items = tonumber(fields.max_items);
341 for username, user in pairs(hosts[module_host].sessions or {}) do
342 if (max_items ~= nil) and (count >= max_items) then
345 users[#users+1] = username.."@"..module_host;
347 if fields.details then
348 for resource, session in pairs(user.sessions or {}) do
349 local status, priority, ip = "unavailable", tostring(session.priority or "-"), session.ip or "<unknown>";
350 if session.presence then
351 status = session.presence:child_with_name("show");
353 status = status:get_text() or "[invalid!]";
355 status = "available";
358 users[#users+1] = " - "..resource..": "..status.."("..priority.."), IP: ["..ip.."]";
362 return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
365 -- Getting a list of S2S connections (this host)
366 local list_s2s_this_result = dataforms_new {
367 title = "List of S2S connections on this host";
369 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/s2s#list" };
370 { name = "sessions", type = "text-multi", label = "Connections:" };
371 { name = "num_in", type = "text-single", label = "#incomming connections:" };
372 { name = "num_out", type = "text-single", label = "#outgoing connections:" };
375 local function session_flags(session, line)
379 line[#line+1] = "["..session.id.."]"
381 line[#line+1] = "["..session.type..(tostring(session):match("%x*$")).."]"
385 if session.cert_identity_status == "valid" then
386 flags[#flags+1] = "authenticated";
388 if session.secure then
389 flags[#flags+1] = "encrypted";
391 if session.compressed then
392 flags[#flags+1] = "compressed)";
394 if session.smacks then
395 flags[#flags+1] = "sm";
397 if session.ip and session.ip:match(":") then
398 flags[#flags+1] = "IPv6";
400 line[#line+1] = "("..t_concat(flags, ", ")..")";
402 return t_concat(line, " ");
405 local function list_s2s_this_handler(self, data, state)
406 local count_in, count_out = 0, 0;
409 local s2s_sessions = module:shared"/*/s2s/sessions";
410 for _, session in pairs(s2s_sessions) do
411 local remotehost, localhost, direction;
412 if session.direction == "outgoing" then
414 count_out = count_out + 1;
415 remotehost, localhost = session.to_host or "?", session.from_host or "?";
418 count_in = count_in + 1;
419 remotehost, localhost = session.from_host or "?", session.to_host or "?";
421 local sess_lines = { r = remotehost,
422 session_flags(session, { "", direction, remotehost or "?" })};
424 if remotehost:match(module_host) or localhost:match(module_host) then
425 s2s_list[#s2s_list+1] = sess_lines;
429 t_sort(s2s_list, function(a, b)
433 for i, sess_lines in ipairs(s2s_list) do
434 s2s_list[i] = sess_lines[1];
437 return { status = "completed", result = { layout = list_s2s_this_result; values = {
438 sessions = t_concat(s2s_list, "\n"),
439 num_in = tostring(count_in),
440 num_out = tostring(count_out)
444 -- Getting a list of loaded modules
445 local list_modules_result = dataforms_new {
446 title = "List of loaded modules";
448 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
449 { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
452 local function list_modules_handler(self, data, state)
453 local modules = array.collect(keys(hosts[module_host].modules)):sort():concat("\n");
454 return { status = "completed", result = { layout = list_modules_result; values = { modules = modules } } };
458 local load_module_layout = dataforms_new {
459 title = "Load module";
460 instructions = "Specify the module to be loaded";
462 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
463 { name = "module", type = "text-single", required = true, label = "Module to be loaded:"};
466 local load_module_handler = adhoc_simple(load_module_layout, function(fields, err)
468 return generate_error_message(err);
470 if modulemanager.is_loaded(module_host, fields.module) then
471 return { status = "completed", info = "Module already loaded" };
473 local ok, err = modulemanager.load(module_host, fields.module);
475 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..module_host..'".' };
477 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..module_host..
478 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
482 -- Globally loading a module
483 local globally_load_module_layout = dataforms_new {
484 title = "Globally load module";
485 instructions = "Specify the module to be loaded on all hosts";
487 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-load" };
488 { name = "module", type = "text-single", required = true, label = "Module to globally load:"};
491 local globally_load_module_handler = adhoc_simple(globally_load_module_layout, function(fields, err)
492 local ok_list, err_list = {}, {};
495 return generate_error_message(err);
498 local ok, err = modulemanager.load(module_host, fields.module);
500 ok_list[#ok_list + 1] = module_host;
502 err_list[#err_list + 1] = module_host .. " (Error: " .. tostring(err) .. ")";
505 -- Is this a global module?
506 if modulemanager.is_loaded("*", fields.module) and not modulemanager.is_loaded(module_host, fields.module) then
507 return { status = "completed", info = 'Global module '..fields.module..' loaded.' };
510 -- This is either a shared or "normal" module, load it on all other hosts
511 for host_name, host in pairs(hosts) do
512 if host_name ~= module_host and host.type == "local" then
513 local ok, err = modulemanager.load(host_name, fields.module);
515 ok_list[#ok_list + 1] = host_name;
517 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
522 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully loaded onto the hosts:\n"..t_concat(ok_list, "\n")) or "")
523 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
524 (#err_list > 0 and ("Failed to load the module "..fields.module.." onto the hosts:\n"..t_concat(err_list, "\n")) or "");
525 return { status = "completed", info = info };
529 local reload_modules_layout = dataforms_new {
530 title = "Reload modules";
531 instructions = "Select the modules to be reloaded";
533 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
534 { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
537 local reload_modules_handler = adhoc_initial(reload_modules_layout, function()
538 return { modules = array.collect(keys(hosts[module_host].modules)):sort() };
539 end, function(fields, err)
541 return generate_error_message(err);
543 local ok_list, err_list = {}, {};
544 for _, module in ipairs(fields.modules) do
545 local ok, err = modulemanager.reload(module_host, module);
547 ok_list[#ok_list + 1] = module;
549 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
552 local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..module_host..":\n"..t_concat(ok_list, "\n")) or "")
553 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
554 (#err_list > 0 and ("Failed to reload the following modules on host "..module_host..":\n"..t_concat(err_list, "\n")) or "");
555 return { status = "completed", info = info };
558 -- Globally reloading a module
559 local globally_reload_module_layout = dataforms_new {
560 title = "Globally reload module";
561 instructions = "Specify the module to reload on all hosts";
563 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-reload" };
564 { name = "module", type = "list-single", required = true, label = "Module to globally reload:"};
567 local globally_reload_module_handler = adhoc_initial(globally_reload_module_layout, function()
568 local loaded_modules = array(keys(modulemanager.get_modules("*")));
569 for _, host in pairs(hosts) do
570 loaded_modules:append(array(keys(host.modules)));
572 loaded_modules = array(set.new(loaded_modules):items()):sort();
573 return { module = loaded_modules };
574 end, function(fields, err)
575 local is_global = false;
578 return generate_error_message(err);
581 if modulemanager.is_loaded("*", fields.module) then
582 local ok, err = modulemanager.reload("*", fields.module);
584 return { status = "completed", info = 'Global module '..fields.module..' failed to reload: '..err };
589 local ok_list, err_list = {}, {};
590 for host_name, host in pairs(hosts) do
591 if modulemanager.is_loaded(host_name, fields.module) then
592 local ok, err = modulemanager.reload(host_name, fields.module);
594 ok_list[#ok_list + 1] = host_name;
596 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
601 if #ok_list == 0 and #err_list == 0 then
603 return { status = "completed", info = 'Successfully reloaded global module '..fields.module };
605 return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
609 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully reloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
610 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
611 (#err_list > 0 and ("Failed to reload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
612 return { status = "completed", info = info };
615 local function send_to_online(message, server)
617 sessions = { [server] = hosts[server] };
623 for domain, session in pairs(sessions) do
624 for user in pairs(session.sessions or {}) do
626 message.attr.from = domain;
627 message.attr.to = user.."@"..domain;
628 core_post_stanza(session, message);
635 -- Shutting down the service
636 local shut_down_service_layout = dataforms_new{
637 title = "Shutting Down the Service";
638 instructions = "Fill out this form to shut down the service.";
640 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
641 { name = "delay", type = "list-single", label = "Time delay before shutting down",
642 value = { {label = "30 seconds", value = "30"},
643 {label = "60 seconds", value = "60"},
644 {label = "90 seconds", value = "90"},
645 {label = "2 minutes", value = "120"},
646 {label = "3 minutes", value = "180"},
647 {label = "4 minutes", value = "240"},
648 {label = "5 minutes", value = "300"},
651 { name = "announcement", type = "text-multi", label = "Announcement" };
654 local shut_down_service_handler = adhoc_simple(shut_down_service_layout, function(fields, err)
656 return generate_error_message(err);
659 if fields.announcement and #fields.announcement > 0 then
660 local message = st.message({type = "headline"}, fields.announcement):up()
661 :tag("subject"):text("Server is shutting down");
662 send_to_online(message);
665 timer_add_task(tonumber(fields.delay or "5"), function(time) prosody.shutdown("Shutdown by adhoc command") end);
667 return { status = "completed", info = "Server is about to shut down" };
671 local unload_modules_layout = dataforms_new {
672 title = "Unload modules";
673 instructions = "Select the modules to be unloaded";
675 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
676 { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
679 local unload_modules_handler = adhoc_initial(unload_modules_layout, function()
680 return { modules = array.collect(keys(hosts[module_host].modules)):sort() };
681 end, function(fields, err)
683 return generate_error_message(err);
685 local ok_list, err_list = {}, {};
686 for _, module in ipairs(fields.modules) do
687 local ok, err = modulemanager.unload(module_host, module);
689 ok_list[#ok_list + 1] = module;
691 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
694 local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..module_host..":\n"..t_concat(ok_list, "\n")) or "")
695 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
696 (#err_list > 0 and ("Failed to unload the following modules on host "..module_host..":\n"..t_concat(err_list, "\n")) or "");
697 return { status = "completed", info = info };
700 -- Globally unloading a module
701 local globally_unload_module_layout = dataforms_new {
702 title = "Globally unload module";
703 instructions = "Specify a module to unload on all hosts";
705 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-unload" };
706 { name = "module", type = "list-single", required = true, label = "Module to globally unload:"};
709 local globally_unload_module_handler = adhoc_initial(globally_unload_module_layout, function()
710 local loaded_modules = array(keys(modulemanager.get_modules("*")));
711 for _, host in pairs(hosts) do
712 loaded_modules:append(array(keys(host.modules)));
714 loaded_modules = array(set.new(loaded_modules):items()):sort();
715 return { module = loaded_modules };
716 end, function(fields, err)
717 local is_global = false;
719 return generate_error_message(err);
722 if modulemanager.is_loaded("*", fields.module) then
723 local ok, err = modulemanager.unload("*", fields.module);
725 return { status = "completed", info = 'Global module '..fields.module..' failed to unload: '..err };
730 local ok_list, err_list = {}, {};
731 for host_name, host in pairs(hosts) do
732 if modulemanager.is_loaded(host_name, fields.module) then
733 local ok, err = modulemanager.unload(host_name, fields.module);
735 ok_list[#ok_list + 1] = host_name;
737 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
742 if #ok_list == 0 and #err_list == 0 then
744 return { status = "completed", info = 'Successfully unloaded global module '..fields.module };
746 return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
750 local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully unloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
751 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
752 (#err_list > 0 and ("Failed to unload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
753 return { status = "completed", info = info };
757 local activate_host_layout = dataforms_new {
758 title = "Activate host";
761 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
762 { name = "host", type = "text-single", required = true, label = "Host:"};
765 local activate_host_handler = adhoc_simple(activate_host_layout, function(fields, err)
767 return generate_error_message(err);
769 local ok, err = hostmanager_activate(fields.host);
772 return { status = "completed", info = fields.host .. " activated" };
774 return { status = "canceled", error = err }
778 -- Deactivating a host
779 local deactivate_host_layout = dataforms_new {
780 title = "Deactivate host";
783 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
784 { name = "host", type = "text-single", required = true, label = "Host:"};
787 local deactivate_host_handler = adhoc_simple(deactivate_host_layout, function(fields, err)
789 return generate_error_message(err);
791 local ok, err = hostmanager_deactivate(fields.host);
794 return { status = "completed", info = fields.host .. " deactivated" };
796 return { status = "canceled", error = err }
801 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
802 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
803 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
804 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
805 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
806 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
807 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
808 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
809 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");
810 local list_s2s_this_desc = adhoc_new("List S2S connections", "http://prosody.im/protocol/s2s#list", list_s2s_this_handler, "admin");
811 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
812 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
813 local globally_load_module_desc = adhoc_new("Globally load module", "http://prosody.im/protocol/modules#global-load", globally_load_module_handler, "global_admin");
814 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
815 local globally_reload_module_desc = adhoc_new("Globally reload module", "http://prosody.im/protocol/modules#global-reload", globally_reload_module_handler, "global_admin");
816 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
817 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
818 local globally_unload_module_desc = adhoc_new("Globally unload module", "http://prosody.im/protocol/modules#global-unload", globally_unload_module_handler, "global_admin");
819 local activate_host_desc = adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler, "global_admin");
820 local deactivate_host_desc = adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler, "global_admin");
822 module:provides("adhoc", add_user_desc);
823 module:provides("adhoc", change_user_password_desc);
824 module:provides("adhoc", config_reload_desc);
825 module:provides("adhoc", delete_user_desc);
826 module:provides("adhoc", end_user_session_desc);
827 module:provides("adhoc", get_user_password_desc);
828 module:provides("adhoc", get_user_roster_desc);
829 module:provides("adhoc", get_user_stats_desc);
830 module:provides("adhoc", get_online_users_desc);
831 module:provides("adhoc", list_s2s_this_desc);
832 module:provides("adhoc", list_modules_desc);
833 module:provides("adhoc", load_module_desc);
834 module:provides("adhoc", globally_load_module_desc);
835 module:provides("adhoc", reload_modules_desc);
836 module:provides("adhoc", globally_reload_module_desc);
837 module:provides("adhoc", shut_down_service_desc);
838 module:provides("adhoc", unload_modules_desc);
839 module:provides("adhoc", globally_unload_module_desc);
840 module:provides("adhoc", activate_host_desc);
841 module:provides("adhoc", deactivate_host_desc);