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