net.http.server: Properly handle persistent connections
[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 = query:__tostring(); -- TODO: Use upcoming pretty_print() function
303                 query_text = query_text:gsub("><", ">\n<");
304
305                 local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result");
306                 result:add_child(query);
307                 return { status = "completed", other = result };
308         else
309                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_user_roster_layout }, "executing";
310         end
311 end
312
313 function get_user_stats_handler(self, data, state)
314         local get_user_stats_layout = dataforms_new{
315                 title = "Get User Statistics";
316                 instructions = "Fill out this form to gather user statistics.";
317
318                 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
319                 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for statistics" };
320         };
321
322         local get_user_stats_result_layout = dataforms_new{
323                 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
324                 { name = "ipaddresses", type = "text-multi", label = "IP Addresses" };
325                 { name = "rostersize", type = "text-single", label = "Roster size" };
326                 { name = "onlineresources", type = "text-multi", label = "Online Resources" };
327         };
328
329         if state then
330                 if data.action == "cancel" then
331                         return { status = "canceled" };
332                 end
333
334                 local fields, err = get_user_stats_layout:data(data.form);
335
336                 if err then
337                         return generate_error_message(err);
338                 end
339
340                 local user, host, resource = jid.split(fields.accountjid);
341                 if host ~= data.to then
342                         return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. data.to } };
343                 elseif not usermanager_user_exists(user, host) then
344                         return { status = "completed", error = { message = "User does not exist" } };
345                 end
346                 local roster = rm_load_roster(user, host);
347                 local rostersize = 0;
348                 local IPs = "";
349                 local resources = "";
350                 for jid in pairs(roster) do
351                         if jid ~= "pending" and jid then
352                                 rostersize = rostersize + 1;
353                         end
354                 end
355                 for resource, session in pairs((hosts[host].sessions[user] and hosts[host].sessions[user].sessions) or {}) do
356                         resources = resources .. "\n" .. resource;
357                         IPs = IPs .. "\n" .. session.ip;
358                 end
359                 return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
360                         onlineresources = resources}} };
361         else
362                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_user_stats_layout }, "executing";
363         end
364 end
365
366 function get_online_users_command_handler(self, data, state)
367         local get_online_users_layout = dataforms_new{
368                 title = "Getting List of Online Users";
369                 instructions = "How many users should be returned at most?";
370
371                 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
372                 { name = "max_items", type = "list-single", label = "Maximum number of users",
373                         value = { "25", "50", "75", "100", "150", "200", "all" } };
374                 { name = "details", type = "boolean", label = "Show details" };
375         };
376
377         local get_online_users_result_layout = dataforms_new{
378                 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
379                 { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
380         };
381
382         if state then
383                 if data.action == "cancel" then
384                         return { status = "canceled" };
385                 end
386
387                 local fields, err = get_online_users_layout:data(data.form);
388
389                 if err then
390                         return generate_error_message(err);
391                 end
392
393                 local max_items = nil
394                 if fields.max_items ~= "all" then
395                         max_items = tonumber(fields.max_items);
396                 end
397                 local count = 0;
398                 local users = {};
399                 for username, user in pairs(hosts[data.to].sessions or {}) do
400                         if (max_items ~= nil) and (count >= max_items) then
401                                 break;
402                         end
403                         users[#users+1] = username.."@"..data.to;
404                         count = count + 1;
405                         if fields.details then
406                                 for resource, session in pairs(user.sessions or {}) do
407                                         local status, priority = "unavailable", tostring(session.priority or "-");
408                                         if session.presence then
409                                                 status = session.presence:child_with_name("show");
410                                                 if status then
411                                                         status = status:get_text() or "[invalid!]";
412                                                 else
413                                                         status = "available";
414                                                 end
415                                         end
416                                         users[#users+1] = " - "..resource..": "..status.."("..priority..")";
417                                 end
418                         end
419                 end
420                 return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
421         else
422                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = get_online_users_layout }, "executing";
423         end
424 end
425
426 function list_modules_handler(self, data, state)
427         local result = dataforms_new {
428                 title = "List of loaded modules";
429
430                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
431                 { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
432         };
433
434         local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n");
435
436         return { status = "completed", result = { layout = result; values = { modules = modules } } };
437 end
438
439 function load_module_handler(self, data, state)
440         local layout = dataforms_new {
441                 title = "Load module";
442                 instructions = "Specify the module to be loaded";
443
444                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
445                 { name = "module", type = "text-single", required = true, label = "Module to be loaded:"};
446         };
447         if state then
448                 if data.action == "cancel" then
449                         return { status = "canceled" };
450                 end
451                 local fields, err = layout:data(data.form);
452                 if err then
453                         return generate_error_message(err);
454                 end
455                 if modulemanager.is_loaded(data.to, fields.module) then
456                         return { status = "completed", info = "Module already loaded" };
457                 end
458                 local ok, err = modulemanager.load(data.to, fields.module);
459                 if ok then
460                         return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..data.to..'".' };
461                 else
462                         return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..data.to..
463                         '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
464                 end
465         else
466                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = layout }, "executing";
467         end
468 end
469
470 function reload_modules_handler(self, data, state)
471         local layout = dataforms_new {
472                 title = "Reload modules";
473                 instructions = "Select the modules to be reloaded";
474
475                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
476                 { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
477         };
478         if state then
479                 if data.action == "cancel" then
480                         return { status = "canceled" };
481                 end
482                 local fields, err = layout:data(data.form);
483                 if err then
484                         return generate_error_message(err);
485                 end
486                 local ok_list, err_list = {}, {};
487                 for _, module in ipairs(fields.modules) do
488                         local ok, err = modulemanager.reload(data.to, module);
489                         if ok then
490                                 ok_list[#ok_list + 1] = module;
491                         else
492                                 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
493                         end
494                 end
495                 local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
496                         (#err_list > 0 and ("Failed to reload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
497                 return { status = "completed", info = info };
498         else
499                 local modules = array.collect(keys(hosts[data.to].modules)):sort();
500                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout; values = { modules = modules } } }, "executing";
501         end
502 end
503
504 function send_to_online(message, server)
505         if server then
506                 sessions = { [server] = hosts[server] };
507         else
508                 sessions = hosts;
509         end
510
511         local c = 0;
512         for domain, session in pairs(sessions) do
513                 for user in pairs(session.sessions or {}) do
514                         c = c + 1;
515                         message.attr.from = domain;
516                         message.attr.to = user.."@"..domain;
517                         core_post_stanza(session, message);
518                 end
519         end
520
521         return c;
522 end
523
524 function shut_down_service_handler(self, data, state)
525         local shut_down_service_layout = dataforms_new{
526                 title = "Shutting Down the Service";
527                 instructions = "Fill out this form to shut down the service.";
528
529                 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
530                 { name = "delay", type = "list-single", label = "Time delay before shutting down",
531                         value = { {label = "30 seconds", value = "30"},
532                                   {label = "60 seconds", value = "60"},
533                                   {label = "90 seconds", value = "90"},
534                                   {label = "2 minutes", value = "120"},
535                                   {label = "3 minutes", value = "180"},
536                                   {label = "4 minutes", value = "240"},
537                                   {label = "5 minutes", value = "300"},
538                         };
539                 };
540                 { name = "announcement", type = "text-multi", label = "Announcement" };
541         };
542
543         if state then
544                 if data.action == "cancel" then
545                         return { status = "canceled" };
546                 end
547
548                 local fields, err = shut_down_service_layout:data(data.form);
549
550                 if err then
551                         return generate_error_message(err);
552                 end
553
554                 if fields.announcement and #fields.announcement > 0 then
555                         local message = st.message({type = "headline"}, fields.announcement):up()
556                                 :tag("subject"):text("Server is shutting down");
557                         send_to_online(message);
558                 end
559
560                 timer_add_task(tonumber(fields.delay or "5"), prosody.shutdown);
561
562                 return { status = "completed", info = "Server is about to shut down" };
563         else
564                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = shut_down_service_layout }, "executing";
565         end
566 end
567
568 function unload_modules_handler(self, data, state)
569         local layout = dataforms_new {
570                 title = "Unload modules";
571                 instructions = "Select the modules to be unloaded";
572
573                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
574                 { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
575         };
576         if state then
577                 if data.action == "cancel" then
578                         return { status = "canceled" };
579                 end
580                 local fields, err = layout:data(data.form);
581                 if err then
582                         return generate_error_message(err);
583                 end
584                 local ok_list, err_list = {}, {};
585                 for _, module in ipairs(fields.modules) do
586                         local ok, err = modulemanager.unload(data.to, module);
587                         if ok then
588                                 ok_list[#ok_list + 1] = module;
589                         else
590                                 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
591                         end
592                 end
593                 local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
594                         (#err_list > 0 and ("Failed to unload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
595                 return { status = "completed", info = info };
596         else
597                 local modules = array.collect(keys(hosts[data.to].modules)):sort();
598                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout; values = { modules = modules } } }, "executing";
599         end
600 end
601
602 function activate_host_handler(self, data, state)
603         local layout = dataforms_new {
604                 title = "Activate host";
605                 instructions = "";
606
607                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
608                 { name = "host", type = "text-single", required = true, label = "Host:"};
609         };
610         if state then
611                 if data.action == "cancel" then
612                         return { status = "canceled" };
613                 end
614                 local fields, err = layout:data(data.form);
615                 if err then
616                         return generate_error_message(err);
617                 end
618                 local ok, err = hostmanager_activate(fields.host);
619
620                 if ok then
621                         return { status = "completed", info = fields.host .. " activated" };
622                 else
623                         return { status = "canceled", error = err }
624                 end
625         else
626                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout } }, "executing";
627         end
628 end
629
630 function deactivate_host_handler(self, data, state)
631         local layout = dataforms_new {
632                 title = "Deactivate host";
633                 instructions = "";
634
635                 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/hosts#activate" };
636                 { name = "host", type = "text-single", required = true, label = "Host:"};
637         };
638         if state then
639                 if data.action == "cancel" then
640                         return { status = "canceled" };
641                 end
642                 local fields, err = layout:data(data.form);
643                 if err then
644                         return generate_error_message(err);
645                 end
646                 local ok, err = hostmanager_deactivate(fields.host);
647
648                 if ok then
649                         return { status = "completed", info = fields.host .. " deactivated" };
650                 else
651                         return { status = "canceled", error = err }
652                 end
653         else
654                 return { status = "executing", actions = {"next", "complete", default = "complete"}, form = { layout = layout } }, "executing";
655         end
656 end
657
658
659 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
660 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
661 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
662 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
663 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
664 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
665 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
666 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
667 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");
668 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
669 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
670 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
671 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
672 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
673 local activate_host_desc = adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler, "global_admin");
674 local deactivate_host_desc = adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler, "global_admin");
675
676 module:provides("adhoc", add_user_desc);
677 module:provides("adhoc", change_user_password_desc);
678 module:provides("adhoc", config_reload_desc);
679 module:provides("adhoc", delete_user_desc);
680 module:provides("adhoc", end_user_session_desc);
681 module:provides("adhoc", get_user_password_desc);
682 module:provides("adhoc", get_user_roster_desc);
683 module:provides("adhoc", get_user_stats_desc);
684 module:provides("adhoc", get_online_users_desc);
685 module:provides("adhoc", list_modules_desc);
686 module:provides("adhoc", load_module_desc);
687 module:provides("adhoc", reload_modules_desc);
688 module:provides("adhoc", shut_down_service_desc);
689 module:provides("adhoc", unload_modules_desc);
690 module:provides("adhoc", activate_host_desc);
691 module:provides("adhoc", deactivate_host_desc);