Merge 0.10->trunk
[prosody.git] / plugins / mod_admin_adhoc.lua
1 -- Copyright (C) 2009-2011 Florian Zeitz
2 --
3 -- This file is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
5 --
6
7 local _G = _G;
8
9 local prosody = _G.prosody;
10 local hosts = prosody.hosts;
11 local t_concat = table.concat;
12 local t_sort = table.sort;
13
14 local module_host = module:get_host();
15
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";
34
35 module:depends("adhoc");
36 local adhoc_new = module:require "adhoc".new;
37
38 local function generate_error_message(errors)
39         local errmsg = {};
40         for name, err in pairs(errors) do
41                 errmsg[#errmsg + 1] = name .. ": " .. err;
42         end
43         return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
44 end
45
46 -- Adding a new user
47 local add_user_layout = dataforms_new{
48         title = "Adding a User";
49         instructions = "Fill out this form to add a user.";
50
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" };
55 };
56
57 local add_user_command_handler = adhoc_simple(add_user_layout, function(fields, err)
58         if err then
59                 return generate_error_message(err);
60         end
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}};
64         end
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" } };
68                 else
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" };
72                         else
73                                 return { status = "completed", error = { message = "Failed to write data to disk" } };
74                         end
75                 end
76         else
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" } };
79         end
80 end);
81
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.";
86
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" };
90 };
91
92 local change_user_password_command_handler = adhoc_simple(change_user_password_layout, function(fields, err)
93         if err then
94                 return generate_error_message(err);
95         end
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}};
99         end
100         if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
101                 return { status = "completed", info = "Password successfully changed" };
102         else
103                 return { status = "completed", error = { message = "User does not exist" } };
104         end
105 end);
106
107 -- Reloading the config
108 local function config_reload_handler(self, data, state)
109         local ok, err = prosody.reload_config();
110         if ok then
111                 return { status = "completed", info = "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
112         else
113                 return { status = "completed", error = { message = "Failed to reload config: " .. tostring(err) } };
114         end
115 end
116
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.";
121
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" };
124 };
125
126 local delete_user_command_handler = adhoc_simple(delete_user_layout, function(fields, err)
127         if err then
128                 return generate_error_message(err);
129         end
130         local failed = {};
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;
137                 else
138                         module:log("debug", "Tried to delete non-existant user %s", aJID);
139                         failed[#failed+1] = aJID;
140                 end
141         end
142         return {status = "completed", info = (#succeeded ~= 0 and
143                         "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
144                         (#failed ~= 0 and
145                         "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
146 end);
147
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);
156                         session:close();
157                 end
158         end
159         return true;
160 end
161
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.";
165
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 };
168 };
169
170 local end_user_session_handler = adhoc_simple(end_user_session_layout, function(fields, err)
171         if err then
172                 return generate_error_message(err);
173         end
174         local failed = {};
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;
180                 else
181                         failed[#failed+1] = aJID;
182                 end
183         end
184         return {status = "completed", info = (#succeeded ~= 0 and
185                 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
186                 (#failed ~= 0 and
187                 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
188 end);
189
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.";
194
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" };
197 };
198
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" };
203 };
204
205 local get_user_password_handler = adhoc_simple(get_user_password_layout, function(fields, err)
206         if err then
207                 return generate_error_message(err);
208         end
209         local user, host, resource = jid.split(fields.accountjid);
210         local accountjid = "";
211         local password = "";
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);
217         else
218                 return { status = "completed", error = { message = "User does not exist" } };
219         end
220         return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
221 end);
222
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" };
227 };
228
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" };
233 };
234
235 local get_user_roster_handler = adhoc_simple(get_user_roster_layout, function(fields, err)
236         if err then
237                 return generate_error_message(err);
238         end
239
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" } };
245         end
246         local roster = rm_load_roster(user, host);
247
248         local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
249         for jid in pairs(roster) do
250                 if jid then
251                         query:tag("item", {
252                                 jid = jid,
253                                 subscription = roster[jid].subscription,
254                                 ask = roster[jid].ask,
255                                 name = roster[jid].name,
256                         });
257                         for group in pairs(roster[jid].groups) do
258                                 query:tag("group"):text(group):up();
259                         end
260                         query:up();
261                 end
262         end
263
264         local query_text = tostring(query):gsub("><", ">\n<");
265
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 };
269 end);
270
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.";
275
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" };
278 };
279
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" };
285 };
286
287 local get_user_stats_handler = adhoc_simple(get_user_stats_layout, function(fields, err)
288         if err then
289                 return generate_error_message(err);
290         end
291
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" } };
297         end
298         local roster = rm_load_roster(user, host);
299         local rostersize = 0;
300         local IPs = "";
301         local resources = "";
302         for jid in pairs(roster) do
303                 if jid then
304                         rostersize = rostersize + 1;
305                 end
306         end
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;
310         end
311         return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
312                 onlineresources = resources}} };
313 end);
314
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?";
319
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" };
324 };
325
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" };
329 };
330
331 local get_online_users_command_handler = adhoc_simple(get_online_users_layout, function(fields, err)
332         if err then
333                 return generate_error_message(err);
334         end
335
336         local max_items = nil
337         if fields.max_items ~= "all" then
338                 max_items = tonumber(fields.max_items);
339         end
340         local count = 0;
341         local users = {};
342         for username, user in pairs(hosts[module_host].sessions or {}) do
343                 if (max_items ~= nil) and (count >= max_items) then
344                         break;
345                 end
346                 users[#users+1] = username.."@"..module_host;
347                 count = count + 1;
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");
353                                         if status then
354                                                 status = status:get_text() or "[invalid!]";
355                                         else
356                                                 status = "available";
357                                         end
358                                 end
359                                 users[#users+1] = " - "..resource..": "..status.."("..priority.."), IP: ["..ip.."]";
360                         end
361                 end
362         end
363         return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
364 end);
365
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";
369
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:" };
374 };
375
376 local function session_flags(session, line)
377         line = line or {};
378
379         if session.id then
380                 line[#line+1] = "["..session.id.."]"
381         else
382                 line[#line+1] = "["..session.type..(tostring(session):match("%x*$")).."]"
383         end
384
385         local flags = {};
386         if session.cert_identity_status == "valid" then
387                 flags[#flags+1] = "authenticated";
388         end
389         if session.secure then
390                 flags[#flags+1] = "encrypted";
391         end
392         if session.compressed then
393                 flags[#flags+1] = "compressed";
394         end
395         if session.smacks then
396                 flags[#flags+1] = "sm";
397         end
398         if session.ip and session.ip:match(":") then
399                 flags[#flags+1] = "IPv6";
400         end
401         line[#line+1] = "("..t_concat(flags, ", ")..")";
402
403         return t_concat(line, " ");
404 end
405
406 local function list_s2s_this_handler(self, data, state)
407         local count_in, count_out = 0, 0;
408         local s2s_list = {};
409
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
414                         direction = "->";
415                         count_out = count_out + 1;
416                         remotehost, localhost = session.to_host or "?", session.from_host or "?";
417                 else
418                         direction = "<-";
419                         count_in = count_in + 1;
420                         remotehost, localhost = session.from_host or "?", session.to_host or "?";
421                 end
422                 local sess_lines = { r = remotehost,
423                         session_flags(session, { "", direction, remotehost or "?" })};
424
425                 if localhost == module_host then
426                         s2s_list[#s2s_list+1] = sess_lines;
427                 end
428         end
429
430         t_sort(s2s_list, function(a, b)
431                 return a.r < b.r;
432         end);
433
434         for i, sess_lines in ipairs(s2s_list) do
435                 s2s_list[i] = sess_lines[1];
436         end
437
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)
442         } } };
443 end
444
445 -- Getting a list of loaded modules
446 local list_modules_result = dataforms_new {
447         title = "List of loaded modules";
448
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:" };
451 };
452
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 } } };
456 end
457
458 -- Loading a module
459 local load_module_layout = dataforms_new {
460         title = "Load module";
461         instructions = "Specify the module to be loaded";
462
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:"};
465 };
466
467 local load_module_handler = adhoc_simple(load_module_layout, function(fields, err)
468         if err then
469                 return generate_error_message(err);
470         end
471         if modulemanager.is_loaded(module_host, fields.module) then
472                 return { status = "completed", info = "Module already loaded" };
473         end
474         local ok, err = modulemanager.load(module_host, fields.module);
475         if ok then
476                 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..module_host..'".' };
477         else
478                 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..module_host..
479                 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
480         end
481 end);
482
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";
487
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:"};
490 };
491
492 local globally_load_module_handler = adhoc_simple(globally_load_module_layout, function(fields, err)
493         local ok_list, err_list = {}, {};
494
495         if err then
496                 return generate_error_message(err);
497         end
498
499         local ok, err = modulemanager.load(module_host, fields.module);
500         if ok then
501                 ok_list[#ok_list + 1] = module_host;
502         else
503                 err_list[#err_list + 1] = module_host .. " (Error: " .. tostring(err) .. ")";
504         end
505
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.' };
509         end
510
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);
515                         if ok then
516                                 ok_list[#ok_list + 1] = host_name;
517                         else
518                                 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
519                         end
520                 end
521         end
522
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 };
527 end);
528
529 -- Reloading modules
530 local reload_modules_layout = dataforms_new {
531         title = "Reload modules";
532         instructions = "Select the modules to be reloaded";
533
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:"};
536 };
537
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)
541         if err then
542                 return generate_error_message(err);
543         end
544         local ok_list, err_list = {}, {};
545         for _, module in ipairs(fields.modules) do
546                 local ok, err = modulemanager.reload(module_host, module);
547                 if ok then
548                         ok_list[#ok_list + 1] = module;
549                 else
550                         err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
551                 end
552         end
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 };
557 end);
558
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";
563
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:"};
566 };
567
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)));
572         end
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;
577
578         if err then
579                 return generate_error_message(err);
580         end
581
582         if modulemanager.is_loaded("*", fields.module) then
583                 local ok, err = modulemanager.reload("*", fields.module);
584                 if not ok then
585                         return { status = "completed", info = 'Global module '..fields.module..' failed to reload: '..err };
586                 end
587                 is_global = true;
588         end
589
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);
594                         if ok then
595                                 ok_list[#ok_list + 1] = host_name;
596                         else
597                                 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
598                         end
599                 end
600         end
601
602         if #ok_list == 0 and #err_list == 0 then
603                 if is_global then
604                         return { status = "completed", info = 'Successfully reloaded global module '..fields.module };
605                 else
606                         return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
607                 end
608         end
609
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 };
614 end);
615
616 local function send_to_online(message, server)
617         local sessions;
618         if server then
619                 sessions = { [server] = hosts[server] };
620         else
621                 sessions = hosts;
622         end
623
624         local c = 0;
625         for domain, session in pairs(sessions) do
626                 for user in pairs(session.sessions or {}) do
627                         c = c + 1;
628                         message.attr.from = domain;
629                         message.attr.to = user.."@"..domain;
630                         core_post_stanza(session, message);
631                 end
632         end
633
634         return c;
635 end
636
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.";
641
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"},
651                 };
652         };
653         { name = "announcement", type = "text-multi", label = "Announcement" };
654 };
655
656 local shut_down_service_handler = adhoc_simple(shut_down_service_layout, function(fields, err)
657         if err then
658                 return generate_error_message(err);
659         end
660
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);
665         end
666
667         timer_add_task(tonumber(fields.delay or "5"), function(time) prosody.shutdown("Shutdown by adhoc command") end);
668
669         return { status = "completed", info = "Server is about to shut down" };
670 end);
671
672 -- Unloading modules
673 local unload_modules_layout = dataforms_new {
674         title = "Unload modules";
675         instructions = "Select the modules to be unloaded";
676
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:"};
679 };
680
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)
684         if err then
685                 return generate_error_message(err);
686         end
687         local ok_list, err_list = {}, {};
688         for _, module in ipairs(fields.modules) do
689                 local ok, err = modulemanager.unload(module_host, module);
690                 if ok then
691                         ok_list[#ok_list + 1] = module;
692                 else
693                         err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
694                 end
695         end
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 };
700 end);
701
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";
706
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:"};
709 };
710
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)));
715         end
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;
720         if err then
721                 return generate_error_message(err);
722         end
723
724         if modulemanager.is_loaded("*", fields.module) then
725                 local ok, err = modulemanager.unload("*", fields.module);
726                 if not ok then
727                         return { status = "completed", info = 'Global module '..fields.module..' failed to unload: '..err };
728                 end
729                 is_global = true;
730         end
731
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);
736                         if ok then
737                                 ok_list[#ok_list + 1] = host_name;
738                         else
739                                 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
740                         end
741                 end
742         end
743
744         if #ok_list == 0 and #err_list == 0 then
745                 if is_global then
746                         return { status = "completed", info = 'Successfully unloaded global module '..fields.module };
747                 else
748                         return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
749                 end
750         end
751
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 };
756 end);
757
758 -- Activating a host
759 local activate_host_layout = dataforms_new {
760         title = "Activate host";
761         instructions = "";
762
763         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
764         { name = "host", type = "text-single", required = true, label = "Host:"};
765 };
766
767 local activate_host_handler = adhoc_simple(activate_host_layout, function(fields, err)
768         if err then
769                 return generate_error_message(err);
770         end
771         local ok, err = hostmanager_activate(fields.host);
772
773         if ok then
774                 return { status = "completed", info = fields.host .. " activated" };
775         else
776                 return { status = "canceled", error = err }
777         end
778 end);
779
780 -- Deactivating a host
781 local deactivate_host_layout = dataforms_new {
782         title = "Deactivate host";
783         instructions = "";
784
785         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
786         { name = "host", type = "text-single", required = true, label = "Host:"};
787 };
788
789 local deactivate_host_handler = adhoc_simple(deactivate_host_layout, function(fields, err)
790         if err then
791                 return generate_error_message(err);
792         end
793         local ok, err = hostmanager_deactivate(fields.host);
794
795         if ok then
796                 return { status = "completed", info = fields.host .. " deactivated" };
797         else
798                 return { status = "canceled", error = err }
799         end
800 end);
801
802
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");
823
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);