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