mod_admin_adhoc: Mark 'accountjids' field as required in 'end user sessions' command...
[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
13 local module_host = module:get_host();
14
15 local keys = require "util.iterators".keys;
16 local usermanager_user_exists = require "core.usermanager".user_exists;
17 local usermanager_create_user = require "core.usermanager".create_user;
18 local usermanager_delete_user = require "core.usermanager".delete_user;
19 local usermanager_get_password = require "core.usermanager".get_password;
20 local usermanager_set_password = require "core.usermanager".set_password;
21 local hostmanager_activate = require "core.hostmanager".activate;
22 local hostmanager_deactivate = require "core.hostmanager".deactivate;
23 local rm_load_roster = require "core.rostermanager".load_roster;
24 local st, jid = require "util.stanza", require "util.jid";
25 local timer_add_task = require "util.timer".add_task;
26 local dataforms_new = require "util.dataforms".new;
27 local array = require "util.array";
28 local modulemanager = require "modulemanager";
29 local core_post_stanza = prosody.core_post_stanza;
30 local adhoc_simple = require "util.adhoc".new_simple_form;
31 local adhoc_initial = require "util.adhoc".new_initial_data_form;
32
33 module:depends("adhoc");
34 local adhoc_new = module:require "adhoc".new;
35
36 local function generate_error_message(errors)
37         local errmsg = {};
38         for name, err in pairs(errors) do
39                 errmsg[#errmsg + 1] = name .. ": " .. err;
40         end
41         return { status = "completed", error = { message = t_concat(errmsg, "\n") } };
42 end
43
44 -- Adding a new user
45 local add_user_layout = dataforms_new{
46         title = "Adding a User";
47         instructions = "Fill out this form to add a user.";
48
49         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
50         { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
51         { name = "password", type = "text-private", label = "The password for this account" };
52         { name = "password-verify", type = "text-private", label = "Retype password" };
53 };
54
55 local add_user_command_handler = adhoc_simple(add_user_layout, function(fields, err)
56         if err then
57                 return generate_error_message(err);
58         end
59         local username, host, resource = jid.split(fields.accountjid);
60         if module_host ~= host then
61                 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. module_host}};
62         end
63         if (fields["password"] == fields["password-verify"]) and username and host then
64                 if usermanager_user_exists(username, host) then
65                         return { status = "completed", error = { message = "Account already exists" } };
66                 else
67                         if usermanager_create_user(username, fields.password, host) then
68                                 module:log("info", "Created new account %s@%s", username, host);
69                                 return { status = "completed", info = "Account successfully created" };
70                         else
71                                 return { status = "completed", error = { message = "Failed to write data to disk" } };
72                         end
73                 end
74         else
75                 module:log("debug", "Invalid data, password mismatch or empty username while creating account for %s", fields.accountjid or "<nil>");
76                 return { status = "completed", error = { message = "Invalid data.\nPassword mismatch, or empty username" } };
77         end
78 end);
79
80 -- Changing a user's password
81 local change_user_password_layout = dataforms_new{
82         title = "Changing a User Password";
83         instructions = "Fill out this form to change a user's password.";
84
85         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
86         { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for this account" };
87         { name = "password", type = "text-private", required = true, label = "The password for this account" };
88 };
89
90 local change_user_password_command_handler = adhoc_simple(change_user_password_layout, function(fields, err)
91         if err then
92                 return generate_error_message(err);
93         end
94         local username, host, resource = jid.split(fields.accountjid);
95         if module_host ~= host then
96                 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. module_host}};
97         end
98         if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
99                 return { status = "completed", info = "Password successfully changed" };
100         else
101                 return { status = "completed", error = { message = "User does not exist" } };
102         end
103 end);
104
105 -- Reloading the config
106 local function config_reload_handler(self, data, state)
107         local ok, err = prosody.reload_config();
108         if ok then
109                 return { status = "completed", info = "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
110         else
111                 return { status = "completed", error = { message = "Failed to reload config: " .. tostring(err) } };
112         end
113 end
114
115 -- Deleting a user's account
116 local delete_user_layout = dataforms_new{
117         title = "Deleting a User";
118         instructions = "Fill out this form to delete a user.";
119
120         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
121         { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
122 };
123
124 local delete_user_command_handler = adhoc_simple(delete_user_layout, function(fields, err)
125         if err then
126                 return generate_error_message(err);
127         end
128         local failed = {};
129         local succeeded = {};
130         for _, aJID in ipairs(fields.accountjids) do
131                 local username, host, resource = jid.split(aJID);
132                 if (host == module_host) and  usermanager_user_exists(username, host) and usermanager_delete_user(username, host) then
133                         module:log("debug", "User %s has been deleted", aJID);
134                         succeeded[#succeeded+1] = aJID;
135                 else
136                         module:log("debug", "Tried to delete non-existant user %s", aJID);
137                         failed[#failed+1] = aJID;
138                 end
139         end
140         return {status = "completed", info = (#succeeded ~= 0 and
141                         "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
142                         (#failed ~= 0 and
143                         "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
144 end);
145
146 -- Ending a user's session
147 local function disconnect_user(match_jid)
148         local node, hostname, givenResource = jid.split(match_jid);
149         local host = hosts[hostname];
150         local sessions = host.sessions[node] and host.sessions[node].sessions;
151         for resource, session in pairs(sessions or {}) do
152                 if not givenResource or (resource == givenResource) then
153                         module:log("debug", "Disconnecting %s@%s/%s", node, hostname, resource);
154                         session:close();
155                 end
156         end
157         return true;
158 end
159
160 local end_user_session_layout = dataforms_new{
161         title = "Ending a User Session";
162         instructions = "Fill out this form to end a user's session.";
163
164         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
165         { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions", required = true };
166 };
167
168 local end_user_session_handler = adhoc_simple(end_user_session_layout, function(fields, err)
169         if err then
170                 return generate_error_message(err);
171         end
172         local failed = {};
173         local succeeded = {};
174         for _, aJID in ipairs(fields.accountjids) do
175                 local username, host, resource = jid.split(aJID);
176                 if (host == module_host) and  usermanager_user_exists(username, host) and disconnect_user(aJID) then
177                         succeeded[#succeeded+1] = aJID;
178                 else
179                         failed[#failed+1] = aJID;
180                 end
181         end
182         return {status = "completed", info = (#succeeded ~= 0 and
183                 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
184                 (#failed ~= 0 and
185                 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
186 end);
187
188 -- Getting a user's password
189 local get_user_password_layout = dataforms_new{
190         title = "Getting User's Password";
191         instructions = "Fill out this form to get a user's password.";
192
193         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
194         { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the password" };
195 };
196
197 local get_user_password_result_layout = dataforms_new{
198         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
199         { name = "accountjid", type = "jid-single", label = "JID" };
200         { name = "password", type = "text-single", label = "Password" };
201 };
202
203 local get_user_password_handler = adhoc_simple(get_user_password_layout, function(fields, err)
204         if err then
205                 return generate_error_message(err);
206         end
207         local user, host, resource = jid.split(fields.accountjid);
208         local accountjid = "";
209         local password = "";
210         if host ~= module_host then
211                 return { status = "completed", error = { message = "Tried to get password for a user on " .. host .. " but command was sent to " .. module_host } };
212         elseif usermanager_user_exists(user, host) then
213                 accountjid = fields.accountjid;
214                 password = usermanager_get_password(user, host);
215         else
216                 return { status = "completed", error = { message = "User does not exist" } };
217         end
218         return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
219 end);
220
221 -- Getting a user's roster
222 local get_user_roster_layout = dataforms_new{
223         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
224         { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the roster" };
225 };
226
227 local get_user_roster_result_layout = dataforms_new{
228         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
229         { name = "accountjid", type = "jid-single", label = "This is the roster for" };
230         { name = "roster", type = "text-multi", label = "Roster XML" };
231 };
232
233 local get_user_roster_handler = adhoc_simple(get_user_roster_layout, function(fields, err)
234         if err then
235                 return generate_error_message(err);
236         end
237
238         local user, host, resource = jid.split(fields.accountjid);
239         if host ~= module_host then
240                 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. module_host } };
241         elseif not usermanager_user_exists(user, host) then
242                 return { status = "completed", error = { message = "User does not exist" } };
243         end
244         local roster = rm_load_roster(user, host);
245
246         local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
247         for jid in pairs(roster) do
248                 if jid ~= "pending" and jid then
249                         query:tag("item", {
250                                 jid = jid,
251                                 subscription = roster[jid].subscription,
252                                 ask = roster[jid].ask,
253                                 name = roster[jid].name,
254                         });
255                         for group in pairs(roster[jid].groups) do
256                                 query:tag("group"):text(group):up();
257                         end
258                         query:up();
259                 end
260         end
261
262         local query_text = tostring(query):gsub("><", ">\n<");
263
264         local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result");
265         result:add_child(query);
266         return { status = "completed", other = result };
267 end);
268
269 -- Getting user statistics
270 local get_user_stats_layout = dataforms_new{
271         title = "Get User Statistics";
272         instructions = "Fill out this form to gather user statistics.";
273
274         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
275         { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for statistics" };
276 };
277
278 local get_user_stats_result_layout = dataforms_new{
279         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
280         { name = "ipaddresses", type = "text-multi", label = "IP Addresses" };
281         { name = "rostersize", type = "text-single", label = "Roster size" };
282         { name = "onlineresources", type = "text-multi", label = "Online Resources" };
283 };
284
285 local get_user_stats_handler = adhoc_simple(get_user_stats_layout, function(fields, err)
286         if err then
287                 return generate_error_message(err);
288         end
289
290         local user, host, resource = jid.split(fields.accountjid);
291         if host ~= module_host then
292                 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. module_host } };
293         elseif not usermanager_user_exists(user, host) then
294                 return { status = "completed", error = { message = "User does not exist" } };
295         end
296         local roster = rm_load_roster(user, host);
297         local rostersize = 0;
298         local IPs = "";
299         local resources = "";
300         for jid in pairs(roster) do
301                 if jid ~= "pending" and jid then
302                         rostersize = rostersize + 1;
303                 end
304         end
305         for resource, session in pairs((hosts[host].sessions[user] and hosts[host].sessions[user].sessions) or {}) do
306                 resources = resources .. "\n" .. resource;
307                 IPs = IPs .. "\n" .. session.ip;
308         end
309         return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
310                 onlineresources = resources}} };
311 end);
312
313 -- Getting a list of online users
314 local get_online_users_layout = dataforms_new{
315         title = "Getting List of Online Users";
316         instructions = "How many users should be returned at most?";
317
318         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
319         { name = "max_items", type = "list-single", label = "Maximum number of users",
320                 value = { "25", "50", "75", "100", "150", "200", "all" } };
321         { name = "details", type = "boolean", label = "Show details" };
322 };
323
324 local get_online_users_result_layout = dataforms_new{
325         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
326         { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
327 };
328
329 local get_online_users_command_handler = adhoc_simple(get_online_users_layout, function(fields, err)
330         if err then
331                 return generate_error_message(err);
332         end
333
334         local max_items = nil
335         if fields.max_items ~= "all" then
336                 max_items = tonumber(fields.max_items);
337         end
338         local count = 0;
339         local users = {};
340         for username, user in pairs(hosts[module_host].sessions or {}) do
341                 if (max_items ~= nil) and (count >= max_items) then
342                         break;
343                 end
344                 users[#users+1] = username.."@"..module_host;
345                 count = count + 1;
346                 if fields.details then
347                         for resource, session in pairs(user.sessions or {}) do
348                                 local status, priority = "unavailable", tostring(session.priority or "-");
349                                 if session.presence then
350                                         status = session.presence:child_with_name("show");
351                                         if status then
352                                                 status = status:get_text() or "[invalid!]";
353                                         else
354                                                 status = "available";
355                                         end
356                                 end
357                                 users[#users+1] = " - "..resource..": "..status.."("..priority..")";
358                         end
359                 end
360         end
361         return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
362 end);
363
364 -- Getting a list of loaded modules
365 local list_modules_result = dataforms_new {
366         title = "List of loaded modules";
367
368         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
369         { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
370 };
371
372 local function list_modules_handler(self, data, state)
373         local modules = array.collect(keys(hosts[module_host].modules)):sort():concat("\n");
374         return { status = "completed", result = { layout = list_modules_result; values = { modules = modules } } };
375 end
376
377 -- Loading a module
378 local load_module_layout = dataforms_new {
379         title = "Load module";
380         instructions = "Specify the module to be loaded";
381
382         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
383         { name = "module", type = "text-single", required = true, label = "Module to be loaded:"};
384 };
385
386 local load_module_handler = adhoc_simple(load_module_layout, function(fields, err)
387         if err then
388                 return generate_error_message(err);
389         end
390         if modulemanager.is_loaded(module_host, fields.module) then
391                 return { status = "completed", info = "Module already loaded" };
392         end
393         local ok, err = modulemanager.load(module_host, fields.module);
394         if ok then
395                 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..module_host..'".' };
396         else
397                 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..module_host..
398                 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
399         end
400 end);
401
402 -- Globally loading a module
403 local globally_load_module_layout = dataforms_new {
404         title = "Globally load module";
405         instructions = "Specify the module to be loaded on all hosts";
406
407         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-load" };
408         { name = "module", type = "text-single", required = true, label = "Module to globally load:"};
409 };
410
411 local globally_load_module_handler = adhoc_simple(globally_load_module_layout, function(fields, err)
412         local ok_list, err_list = {}, {};
413
414         if err then
415                 return generate_error_message(err);
416         end
417
418         local ok, err = modulemanager.load(module_host, fields.module);
419         if ok then
420                 ok_list[#ok_list + 1] = module_host;
421         else
422                 err_list[#err_list + 1] = module_host .. " (Error: " .. tostring(err) .. ")";
423         end
424
425         -- Is this a global module?
426         if modulemanager.is_loaded("*", fields.module) and not modulemanager.is_loaded(module_host, fields.module) then
427                 return { status = "completed", info = 'Global module '..fields.module..' loaded.' };
428         end
429
430         -- This is either a shared or "normal" module, load it on all other hosts
431         for host_name, host in pairs(hosts) do
432                 if host_name ~= module_host and host.type == "local" then
433                         local ok, err = modulemanager.load(host_name, fields.module);
434                         if ok then
435                                 ok_list[#ok_list + 1] = host_name;
436                         else
437                                 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
438                         end
439                 end
440         end
441
442         local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully loaded onto the hosts:\n"..t_concat(ok_list, "\n")) or "")
443                 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
444                 (#err_list > 0 and ("Failed to load the module "..fields.module.." onto the hosts:\n"..t_concat(err_list, "\n")) or "");
445         return { status = "completed", info = info };
446 end);
447
448 -- Reloading modules
449 local reload_modules_layout = dataforms_new {
450         title = "Reload modules";
451         instructions = "Select the modules to be reloaded";
452
453         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
454         { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
455 };
456
457 local reload_modules_handler = adhoc_initial(reload_modules_layout, function()
458         return { modules = array.collect(keys(hosts[module_host].modules)):sort() };
459 end, function(fields, err)
460         if err then
461                 return generate_error_message(err);
462         end
463         local ok_list, err_list = {}, {};
464         for _, module in ipairs(fields.modules) do
465                 local ok, err = modulemanager.reload(module_host, module);
466                 if ok then
467                         ok_list[#ok_list + 1] = module;
468                 else
469                         err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
470                 end
471         end
472         local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..module_host..":\n"..t_concat(ok_list, "\n")) or "")
473                 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
474                 (#err_list > 0 and ("Failed to reload the following modules on host "..module_host..":\n"..t_concat(err_list, "\n")) or "");
475         return { status = "completed", info = info };
476 end);
477
478 -- Globally reloading a module
479 local globally_reload_module_layout = dataforms_new {
480         title = "Globally reload module";
481         instructions = "Specify the module to reload on all hosts";
482
483         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-reload" };
484         { name = "module", type = "list-single", required = true, label = "Module to globally reload:"};
485 };
486
487 local globally_reload_module_handler = adhoc_initial(globally_reload_module_layout, function()
488         local loaded_modules = array(keys(modulemanager.get_modules("*")));
489         for _, host in pairs(hosts) do
490                 loaded_modules:append(array(keys(host.modules)));
491         end
492         loaded_modules = array(keys(set.new(loaded_modules):items())):sort();
493         return { module = loaded_modules };
494 end, function(fields, err)
495         local is_global = false;
496
497         if err then
498                 return generate_error_message(err);
499         end
500
501         if modulemanager.is_loaded("*", fields.module) then
502                 local ok, err = modulemanager.reload("*", fields.module);
503                 if not ok then
504                         return { status = "completed", info = 'Global module '..fields.module..' failed to reload: '..err };
505                 end
506                 is_global = true;
507         end
508
509         local ok_list, err_list = {}, {};
510         for host_name, host in pairs(hosts) do
511                 if modulemanager.is_loaded(host_name, fields.module)  then
512                         local ok, err = modulemanager.reload(host_name, fields.module);
513                         if ok then
514                                 ok_list[#ok_list + 1] = host_name;
515                         else
516                                 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
517                         end
518                 end
519         end
520
521         if #ok_list == 0 and #err_list == 0 then
522                 if is_global then
523                         return { status = "completed", info = 'Successfully reloaded global module '..fields.module };
524                 else
525                         return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
526                 end
527         end
528
529         local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully reloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
530                 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
531                 (#err_list > 0 and ("Failed to reload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
532         return { status = "completed", info = info };
533 end);
534
535 local function send_to_online(message, server)
536         if server then
537                 sessions = { [server] = hosts[server] };
538         else
539                 sessions = hosts;
540         end
541
542         local c = 0;
543         for domain, session in pairs(sessions) do
544                 for user in pairs(session.sessions or {}) do
545                         c = c + 1;
546                         message.attr.from = domain;
547                         message.attr.to = user.."@"..domain;
548                         core_post_stanza(session, message);
549                 end
550         end
551
552         return c;
553 end
554
555 -- Shutting down the service
556 local shut_down_service_layout = dataforms_new{
557         title = "Shutting Down the Service";
558         instructions = "Fill out this form to shut down the service.";
559
560         { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
561         { name = "delay", type = "list-single", label = "Time delay before shutting down",
562                 value = { {label = "30 seconds", value = "30"},
563                           {label = "60 seconds", value = "60"},
564                           {label = "90 seconds", value = "90"},
565                           {label = "2 minutes", value = "120"},
566                           {label = "3 minutes", value = "180"},
567                           {label = "4 minutes", value = "240"},
568                           {label = "5 minutes", value = "300"},
569                 };
570         };
571         { name = "announcement", type = "text-multi", label = "Announcement" };
572 };
573
574 local shut_down_service_handler = adhoc_simple(shut_down_service_layout, function(fields, err)
575         if err then
576                 return generate_error_message(err);
577         end
578
579         if fields.announcement and #fields.announcement > 0 then
580                 local message = st.message({type = "headline"}, fields.announcement):up()
581                         :tag("subject"):text("Server is shutting down");
582                 send_to_online(message);
583         end
584
585         timer_add_task(tonumber(fields.delay or "5"), function(time) prosody.shutdown("Shutdown by adhoc command") end);
586
587         return { status = "completed", info = "Server is about to shut down" };
588 end);
589
590 -- Unloading modules
591 local unload_modules_layout = dataforms_new {
592         title = "Unload modules";
593         instructions = "Select the modules to be unloaded";
594
595         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
596         { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
597 };
598
599 local unload_modules_handler = adhoc_initial(unload_modules_layout, function()
600         return { modules = array.collect(keys(hosts[module_host].modules)):sort() };
601 end, function(fields, err)
602         if err then
603                 return generate_error_message(err);
604         end
605         local ok_list, err_list = {}, {};
606         for _, module in ipairs(fields.modules) do
607                 local ok, err = modulemanager.unload(module_host, module);
608                 if ok then
609                         ok_list[#ok_list + 1] = module;
610                 else
611                         err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
612                 end
613         end
614         local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..module_host..":\n"..t_concat(ok_list, "\n")) or "")
615                 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
616                 (#err_list > 0 and ("Failed to unload the following modules on host "..module_host..":\n"..t_concat(err_list, "\n")) or "");
617         return { status = "completed", info = info };
618 end);
619
620 -- Globally unloading a module
621 local globally_unload_module_layout = dataforms_new {
622         title = "Globally unload module";
623         instructions = "Specify a module to unload on all hosts";
624
625         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#global-unload" };
626         { name = "module", type = "list-single", required = true, label = "Module to globally unload:"};
627 };
628
629 local globally_unload_module_handler = adhoc_initial(globally_unload_module_layout, function()
630         local loaded_modules = array(keys(modulemanager.get_modules("*")));
631         for _, host in pairs(hosts) do
632                 loaded_modules:append(array(keys(host.modules)));
633         end
634         loaded_modules = array(keys(set.new(loaded_modules):items())):sort();
635         return { module = loaded_modules };
636 end, function(fields, err)
637         local is_global = false;
638         if err then
639                 return generate_error_message(err);
640         end
641
642         if modulemanager.is_loaded("*", fields.module) then
643                 local ok, err = modulemanager.unload("*", fields.module);
644                 if not ok then
645                         return { status = "completed", info = 'Global module '..fields.module..' failed to unload: '..err };
646                 end
647                 is_global = true;
648         end
649
650         local ok_list, err_list = {}, {};
651         for host_name, host in pairs(hosts) do
652                 if modulemanager.is_loaded(host_name, fields.module)  then
653                         local ok, err = modulemanager.unload(host_name, fields.module);
654                         if ok then
655                                 ok_list[#ok_list + 1] = host_name;
656                         else
657                                 err_list[#err_list + 1] = host_name .. " (Error: " .. tostring(err) .. ")";
658                         end
659                 end
660         end
661
662         if #ok_list == 0 and #err_list == 0 then
663                 if is_global then
664                         return { status = "completed", info = 'Successfully unloaded global module '..fields.module };
665                 else
666                         return { status = "completed", info = 'Module '..fields.module..' not loaded on any host.' };
667                 end
668         end
669
670         local info = (#ok_list > 0 and ("The module "..fields.module.." was successfully unloaded on the hosts:\n"..t_concat(ok_list, "\n")) or "")
671                 .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
672                 (#err_list > 0 and ("Failed to unload the module "..fields.module.." on the hosts:\n"..t_concat(err_list, "\n")) or "");
673         return { status = "completed", info = info };
674 end);
675
676 -- Activating a host
677 local activate_host_layout = dataforms_new {
678         title = "Activate host";
679         instructions = "";
680
681         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
682         { name = "host", type = "text-single", required = true, label = "Host:"};
683 };
684
685 local activate_host_handler = adhoc_simple(activate_host_layout, function(fields, err)
686         if err then
687                 return generate_error_message(err);
688         end
689         local ok, err = hostmanager_activate(fields.host);
690
691         if ok then
692                 return { status = "completed", info = fields.host .. " activated" };
693         else
694                 return { status = "canceled", error = err }
695         end
696 end);
697
698 -- Deactivating a host
699 local deactivate_host_layout = dataforms_new {
700         title = "Deactivate host";
701         instructions = "";
702
703         { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
704         { name = "host", type = "text-single", required = true, label = "Host:"};
705 };
706
707 local deactivate_host_handler = adhoc_simple(deactivate_host_layout, function(fields, err)
708         if err then
709                 return generate_error_message(err);
710         end
711         local ok, err = hostmanager_deactivate(fields.host);
712
713         if ok then
714                 return { status = "completed", info = fields.host .. " deactivated" };
715         else
716                 return { status = "canceled", error = err }
717         end
718 end);
719
720
721 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
722 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
723 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
724 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
725 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
726 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
727 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
728 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
729 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");
730 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
731 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
732 local globally_load_module_desc = adhoc_new("Globally load module", "http://prosody.im/protocol/modules#global-load", globally_load_module_handler, "global_admin");
733 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
734 local globally_reload_module_desc = adhoc_new("Globally reload module", "http://prosody.im/protocol/modules#global-reload", globally_reload_module_handler, "global_admin");
735 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
736 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
737 local globally_unload_module_desc = adhoc_new("Globally unload module", "http://prosody.im/protocol/modules#global-unload", globally_unload_module_handler, "global_admin");
738 local activate_host_desc = adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler, "global_admin");
739 local deactivate_host_desc = adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler, "global_admin");
740
741 module:provides("adhoc", add_user_desc);
742 module:provides("adhoc", change_user_password_desc);
743 module:provides("adhoc", config_reload_desc);
744 module:provides("adhoc", delete_user_desc);
745 module:provides("adhoc", end_user_session_desc);
746 module:provides("adhoc", get_user_password_desc);
747 module:provides("adhoc", get_user_roster_desc);
748 module:provides("adhoc", get_user_stats_desc);
749 module:provides("adhoc", get_online_users_desc);
750 module:provides("adhoc", list_modules_desc);
751 module:provides("adhoc", load_module_desc);
752 module:provides("adhoc", globally_load_module_desc);
753 module:provides("adhoc", reload_modules_desc);
754 module:provides("adhoc", globally_reload_module_desc);
755 module:provides("adhoc", shut_down_service_desc);
756 module:provides("adhoc", unload_modules_desc);
757 module:provides("adhoc", globally_unload_module_desc);
758 module:provides("adhoc", activate_host_desc);
759 module:provides("adhoc", deactivate_host_desc);