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