bf00b73db3e2bb6060e775ccdba58166955428b7
[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 function send_to_online(message, server)
558         if server then
559                 sessions = { [server] = hosts[server] };
560         else
561                 sessions = hosts;
562         end
563
564         local c = 0;
565         for domain, session in pairs(sessions) do
566                 for user in pairs(session.sessions or {}) do
567                         c = c + 1;
568                         message.attr.from = domain;
569                         message.attr.to = user.."@"..domain;
570                         core_post_stanza(session, message);
571                 end
572         end
573
574         return c;
575 end
576
577 function shut_down_service_handler(self, data, state)
578         local shut_down_service_layout = dataforms_new{
579                 title = "Shutting Down the Service";
580                 instructions = "Fill out this form to shut down the service.";
581
582                 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
583                 { name = "delay", type = "list-single", label = "Time delay before shutting down",
584                         value = { {label = "30 seconds", value = "30"},
585                                   {label = "60 seconds", value = "60"},
586                                   {label = "90 seconds", value = "90"},
587                                   {label = "2 minutes", value = "120"},
588                                   {label = "3 minutes", value = "180"},
589                                   {label = "4 minutes", value = "240"},
590                                   {label = "5 minutes", value = "300"},
591                         };
592                 };
593                 { name = "announcement", type = "text-multi", label = "Announcement" };
594         };
595
596         if state then
597                 if data.action == "cancel" then
598                         return { status = "canceled" };
599                 end
600
601                 local fields, err = shut_down_service_layout:data(data.form);
602
603                 if err then
604                         return generate_error_message(err);
605                 end
606
607                 if fields.announcement and #fields.announcement > 0 then
608                         local message = st.message({type = "headline"}, fields.announcement):up()
609                                 :tag("subject"):text("Server is shutting down");
610                         send_to_online(message);
611                 end
612
613                 timer_add_task(tonumber(fields.delay or "5"), prosody.shutdown);
614
615                 return { status = "completed", info = "Server is about to shut down" };
616         else
617                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = shut_down_service_layout }, "executing";
618         end
619 end
620
621 function unload_modules_handler(self, data, state)
622         local layout = dataforms_new {
623                 title = "Unload modules";
624                 instructions = "Select the modules to be unloaded";
625
626                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
627                 { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
628         };
629         if state then
630                 if data.action == "cancel" then
631                         return { status = "canceled" };
632                 end
633                 local fields, err = layout:data(data.form);
634                 if err then
635                         return generate_error_message(err);
636                 end
637                 local ok_list, err_list = {}, {};
638                 for _, module in ipairs(fields.modules) do
639                         local ok, err = modulemanager.unload(data.to, module);
640                         if ok then
641                                 ok_list[#ok_list + 1] = module;
642                         else
643                                 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
644                         end
645                 end
646                 local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")
647                         .. ((#ok_list > 0 and #err_list > 0) and "\n" or "") ..
648                         (#err_list > 0 and ("Failed to unload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
649                 return { status = "completed", info = info };
650         else
651                 local modules = array.collect(keys(hosts[data.to].modules)):sort();
652                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout; values = { modules = modules } } }, "executing";
653         end
654 end
655
656 function activate_host_handler(self, data, state)
657         local layout = dataforms_new {
658                 title = "Activate host";
659                 instructions = "";
660
661                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
662                 { name = "host", type = "text-single", required = true, label = "Host:"};
663         };
664         if state then
665                 if data.action == "cancel" then
666                         return { status = "canceled" };
667                 end
668                 local fields, err = layout:data(data.form);
669                 if err then
670                         return generate_error_message(err);
671                 end
672                 local ok, err = hostmanager_activate(fields.host);
673
674                 if ok then
675                         return { status = "completed", info = fields.host .. " activated" };
676                 else
677                         return { status = "canceled", error = err }
678                 end
679         else
680                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout } }, "executing";
681         end
682 end
683
684 function deactivate_host_handler(self, data, state)
685         local layout = dataforms_new {
686                 title = "Deactivate host";
687                 instructions = "";
688
689                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
690                 { name = "host", type = "text-single", required = true, label = "Host:"};
691         };
692         if state then
693                 if data.action == "cancel" then
694                         return { status = "canceled" };
695                 end
696                 local fields, err = layout:data(data.form);
697                 if err then
698                         return generate_error_message(err);
699                 end
700                 local ok, err = hostmanager_deactivate(fields.host);
701
702                 if ok then
703                         return { status = "completed", info = fields.host .. " deactivated" };
704                 else
705                         return { status = "canceled", error = err }
706                 end
707         else
708                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout } }, "executing";
709         end
710 end
711
712
713 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
714 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
715 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
716 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
717 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
718 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
719 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
720 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
721 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");
722 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
723 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
724 local globally_load_module_desc = adhoc_new("Globally load module", "http://prosody.im/protocol/modules#global-load", globally_load_module_handler, "global_admin");
725 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
726 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
727 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
728 local activate_host_desc = adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler, "global_admin");
729 local deactivate_host_desc = adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler, "global_admin");
730
731 module:provides("adhoc", add_user_desc);
732 module:provides("adhoc", change_user_password_desc);
733 module:provides("adhoc", config_reload_desc);
734 module:provides("adhoc", delete_user_desc);
735 module:provides("adhoc", end_user_session_desc);
736 module:provides("adhoc", get_user_password_desc);
737 module:provides("adhoc", get_user_roster_desc);
738 module:provides("adhoc", get_user_stats_desc);
739 module:provides("adhoc", get_online_users_desc);
740 module:provides("adhoc", list_modules_desc);
741 module:provides("adhoc", load_module_desc);
742 module:provides("adhoc", globally_load_module_desc);
743 module:provides("adhoc", reload_modules_desc);
744 module:provides("adhoc", shut_down_service_desc);
745 module:provides("adhoc", unload_modules_desc);
746 module:provides("adhoc", activate_host_desc);
747 module:provides("adhoc", deactivate_host_desc);