1 -- Copyright (C) 2009-2011 Florian Zeitz
3 -- This file is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
9 local prosody = _G.prosody;
10 local hosts = prosody.hosts;
11 local t_concat = table.concat;
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_get_password = require "core.usermanager".get_password;
18 local usermanager_set_password = require "core.usermanager".set_password;
19 local is_admin = require "core.usermanager".is_admin;
20 local rm_load_roster = require "core.rostermanager".load_roster;
21 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
22 local timer_add_task = require "util.timer".add_task;
23 local dataforms_new = require "util.dataforms".new;
24 local array = require "util.array";
25 local modulemanager = require "modulemanager";
27 module:depends"adhoc";
28 local adhoc_new = module:require "adhoc".new;
30 function add_user_command_handler(self, data, state)
31 local add_user_layout = dataforms_new{
32 title = "Adding a User";
33 instructions = "Fill out this form to add a user.";
35 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
36 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
37 { name = "password", type = "text-private", label = "The password for this account" };
38 { name = "password-verify", type = "text-private", label = "Retype password" };
42 if data.action == "cancel" then
43 return { status = "canceled" };
45 local fields = add_user_layout:data(data.form);
46 if not fields.accountjid then
47 return { status = "completed", error = { message = "You need to specify a JID." } };
49 local username, host, resource = jid.split(fields.accountjid);
50 if data.to ~= host then
51 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. data.to}};
53 if (fields["password"] == fields["password-verify"]) and username and host then
54 if usermanager_user_exists(username, host) then
55 return { status = "completed", error = { message = "Account already exists" } };
57 if usermanager_create_user(username, fields.password, host) then
58 module:log("info", "Created new account " .. username.."@"..host);
59 return { status = "completed", info = "Account successfully created" };
61 return { status = "completed", error = { message = "Failed to write data to disk" } };
65 module:log("debug", (fields.accountjid or "<nil>") .. " " .. (fields.password or "<nil>") .. " "
66 .. (fields["password-verify"] or "<nil>"));
67 return { status = "completed", error = { message = "Invalid data.\nPassword mismatch, or empty username" } };
70 return { status = "executing", form = add_user_layout }, "executing";
74 function change_user_password_command_handler(self, data, state)
75 local change_user_password_layout = dataforms_new{
76 title = "Changing a User Password";
77 instructions = "Fill out this form to change a user's password.";
79 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
80 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for this account" };
81 { name = "password", type = "text-private", required = true, label = "The password for this account" };
85 if data.action == "cancel" then
86 return { status = "canceled" };
88 local fields = change_user_password_layout:data(data.form);
89 if not fields.accountjid or fields.accountjid == "" or not fields.password then
90 return { status = "completed", error = { message = "Please specify username and password" } };
92 local username, host, resource = jid.split(fields.accountjid);
93 if data.to ~= host then
94 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. data.to}};
96 if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
97 return { status = "completed", info = "Password successfully changed" };
99 return { status = "completed", error = { message = "User does not exist" } };
102 return { status = "executing", form = change_user_password_layout }, "executing";
106 function config_reload_handler(self, data, state)
107 local ok, err = prosody.reload_config();
109 return { status = "completed", info = "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
111 return { status = "completed", error = { message = "Failed to reload config: " .. tostring(err) } };
116 function delete_user_command_handler(self, data, state)
117 local delete_user_layout = dataforms_new{
118 title = "Deleting a User";
119 instructions = "Fill out this form to delete a user.";
121 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
122 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
126 if data.action == "cancel" then
127 return { status = "canceled" };
129 local fields = delete_user_layout:data(data.form);
131 local succeeded = {};
132 for _, aJID in ipairs(fields.accountjids) do
133 local username, host, resource = jid.split(aJID);
134 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then
135 module:log("debug", "User " .. aJID .. " has been deleted");
136 succeeded[#succeeded+1] = aJID;
138 module:log("debug", "Tried to delete non-existant user "..aJID);
139 failed[#failed+1] = aJID;
142 return {status = "completed", info = (#succeeded ~= 0 and
143 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
145 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
147 return { status = "executing", form = delete_user_layout }, "executing";
151 function disconnect_user(match_jid)
152 local node, hostname, givenResource = jid.split(match_jid);
153 local host = hosts[hostname];
154 local sessions = host.sessions[node] and host.sessions[node].sessions;
155 for resource, session in pairs(sessions or {}) do
156 if not givenResource or (resource == givenResource) then
157 module:log("debug", "Disconnecting "..node.."@"..hostname.."/"..resource);
164 function end_user_session_handler(self, data, state)
165 local end_user_session_layout = dataforms_new{
166 title = "Ending a User Session";
167 instructions = "Fill out this form to end a user's session.";
169 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
170 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
174 if data.action == "cancel" then
175 return { status = "canceled" };
178 local fields = end_user_session_layout:data(data.form);
180 local succeeded = {};
181 for _, aJID in ipairs(fields.accountjids) do
182 local username, host, resource = jid.split(aJID);
183 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) then
184 succeeded[#succeeded+1] = aJID;
186 failed[#failed+1] = aJID;
189 return {status = "completed", info = (#succeeded ~= 0 and
190 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
192 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
194 return { status = "executing", form = end_user_session_layout }, "executing";
198 local end_user_session_layout = dataforms_new{
199 title = "Ending a User Session";
200 instructions = "Fill out this form to end a user's session.";
202 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
203 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
207 function get_user_password_handler(self, data, state)
208 local get_user_password_layout = dataforms_new{
209 title = "Getting User's Password";
210 instructions = "Fill out this form to get a user's password.";
212 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
213 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the password" };
216 local get_user_password_result_layout = dataforms_new{
217 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
218 { name = "accountjid", type = "jid-single", label = "JID" };
219 { name = "password", type = "text-single", label = "Password" };
223 if data.action == "cancel" then
224 return { status = "canceled" };
226 local fields = get_user_password_layout:data(data.form);
227 if not fields.accountjid then
228 return { status = "completed", error = { message = "Please specify a JID." } };
230 local user, host, resource = jid.split(fields.accountjid);
231 local accountjid = "";
233 if host ~= data.to then
234 return { status = "completed", error = { message = "Tried to get password for a user on " .. host .. " but command was sent to " .. data.to } };
235 elseif usermanager_user_exists(user, host) then
236 accountjid = fields.accountjid;
237 password = usermanager_get_password(user, host);
239 return { status = "completed", error = { message = "User does not exist" } };
241 return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
243 return { status = "executing", form = get_user_password_layout }, "executing";
247 function get_user_roster_handler(self, data, state)
248 local get_user_roster_layout = dataforms_new{
249 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
250 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the roster" };
253 local get_user_roster_result_layout = dataforms_new{
254 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
255 { name = "accountjid", type = "jid-single", label = "This is the roster for" };
256 { name = "roster", type = "text-multi", label = "Roster XML" };
260 if data.action == "cancel" then
261 return { status = "canceled" };
264 local fields = get_user_roster_layout:data(data.form);
266 if not fields.accountjid then
267 return { status = "completed", error = { message = "Please specify a JID" } };
270 local user, host, resource = jid.split(fields.accountjid);
271 if host ~= data.to then
272 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. data.to } };
273 elseif not usermanager_user_exists(user, host) then
274 return { status = "completed", error = { message = "User does not exist" } };
276 local roster = rm_load_roster(user, host);
278 local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
279 for jid in pairs(roster) do
280 if jid ~= "pending" and jid then
283 subscription = roster[jid].subscription,
284 ask = roster[jid].ask,
285 name = roster[jid].name,
287 for group in pairs(roster[jid].groups) do
288 query:tag("group"):text(group):up();
294 local query_text = query:__tostring(); -- TODO: Use upcoming pretty_print() function
295 query_text = query_text:gsub("><", ">\n<");
297 local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result");
298 result:add_child(query);
299 return { status = "completed", other = result };
301 return { status = "executing", form = get_user_roster_layout }, "executing";
305 function get_user_stats_handler(self, data, state)
306 local get_user_stats_layout = dataforms_new{
307 title = "Get User Statistics";
308 instructions = "Fill out this form to gather user statistics.";
310 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
311 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for statistics" };
314 local get_user_stats_result_layout = dataforms_new{
315 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
316 { name = "ipaddresses", type = "text-multi", label = "IP Addresses" };
317 { name = "rostersize", type = "text-single", label = "Roster size" };
318 { name = "onlineresources", type = "text-multi", label = "Online Resources" };
322 if data.action == "cancel" then
323 return { status = "canceled" };
326 local fields = get_user_stats_layout:data(data.form);
328 if not fields.accountjid then
329 return { status = "completed", error = { message = "Please specify a JID." } };
332 local user, host, resource = jid.split(fields.accountjid);
333 if host ~= data.to then
334 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. data.to } };
335 elseif not usermanager_user_exists(user, host) then
336 return { status = "completed", error = { message = "User does not exist" } };
338 local roster = rm_load_roster(user, host);
339 local rostersize = 0;
341 local resources = "";
342 for jid in pairs(roster) do
343 if jid ~= "pending" and jid then
344 rostersize = rostersize + 1;
347 for resource, session in pairs((hosts[host].sessions[user] and hosts[host].sessions[user].sessions) or {}) do
348 resources = resources .. "\n" .. resource;
349 IPs = IPs .. "\n" .. session.ip;
351 return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
352 onlineresources = resources}} };
354 return { status = "executing", form = get_user_stats_layout }, "executing";
358 function get_online_users_command_handler(self, data, state)
359 local get_online_users_layout = dataforms_new{
360 title = "Getting List of Online Users";
361 instructions = "How many users should be returned at most?";
363 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
364 { name = "max_items", type = "list-single", label = "Maximum number of users",
365 value = { "25", "50", "75", "100", "150", "200", "all" } };
366 { name = "details", type = "boolean", label = "Show details" };
369 local get_online_users_result_layout = dataforms_new{
370 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
371 { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
375 if data.action == "cancel" then
376 return { status = "canceled" };
379 local fields = get_online_users_layout:data(data.form);
381 local max_items = nil
382 if fields.max_items ~= "all" then
383 max_items = tonumber(fields.max_items);
387 for username, user in pairs(hosts[data.to].sessions or {}) do
388 if (max_items ~= nil) and (count >= max_items) then
391 users[#users+1] = username.."@"..data.to;
393 if fields.details then
394 for resource, session in pairs(user.sessions or {}) do
395 local status, priority = "unavailable", tostring(session.priority or "-");
396 if session.presence then
397 status = session.presence:child_with_name("show");
399 status = status:get_text() or "[invalid!]";
401 status = "available";
404 users[#users+1] = " - "..resource..": "..status.."("..priority..")";
408 return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
410 return { status = "executing", form = get_online_users_layout }, "executing";
414 function list_modules_handler(self, data, state)
415 local result = dataforms_new {
416 title = "List of loaded modules";
418 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
419 { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
422 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n");
424 return { status = "completed", result = { layout = result; values = { modules = modules } } };
427 function load_module_handler(self, data, state)
428 local layout = dataforms_new {
429 title = "Load module";
430 instructions = "Specify the module to be loaded";
432 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
433 { name = "module", type = "text-single", required = true, label = "Module to be loaded:"};
436 if data.action == "cancel" then
437 return { status = "canceled" };
439 local fields = layout:data(data.form);
440 if (not fields.module) or (fields.module == "") then
441 return { status = "completed", error = {
442 message = "Please specify a module."
445 if modulemanager.is_loaded(data.to, fields.module) then
446 return { status = "completed", info = "Module already loaded" };
448 local ok, err = modulemanager.load(data.to, fields.module);
450 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..data.to..'".' };
452 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..data.to..
453 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
456 local modules = array.collect(keys(hosts[data.to].modules)):sort();
457 return { status = "executing", form = layout }, "executing";
461 function reload_modules_handler(self, data, state)
462 local layout = dataforms_new {
463 title = "Reload modules";
464 instructions = "Select the modules to be reloaded";
466 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
467 { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
470 if data.action == "cancel" then
471 return { status = "canceled" };
473 local fields = layout:data(data.form);
474 if #fields.modules == 0 then
475 return { status = "completed", error = {
476 message = "Please specify a module. (This means your client misbehaved, as this field is required)"
479 local ok_list, err_list = {}, {};
480 for _, module in ipairs(fields.modules) do
481 local ok, err = modulemanager.reload(data.to, module);
483 ok_list[#ok_list + 1] = module;
485 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
488 local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
489 (#err_list > 0 and ("Failed to reload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
490 return { status = "completed", info = info };
492 local modules = array.collect(keys(hosts[data.to].modules)):sort();
493 return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
497 function send_to_online(message, server)
499 sessions = { [server] = hosts[server] };
505 for domain, session in pairs(sessions) do
506 for user in pairs(session.sessions or {}) do
508 message.attr.from = domain;
509 message.attr.to = user.."@"..domain;
510 core_post_stanza(session, message);
517 function shut_down_service_handler(self, data, state)
518 local shut_down_service_layout = dataforms_new{
519 title = "Shutting Down the Service";
520 instructions = "Fill out this form to shut down the service.";
522 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
523 { name = "delay", type = "list-single", label = "Time delay before shutting down",
524 value = { {label = "30 seconds", value = "30"},
525 {label = "60 seconds", value = "60"},
526 {label = "90 seconds", value = "90"},
527 {label = "2 minutes", value = "120"},
528 {label = "3 minutes", value = "180"},
529 {label = "4 minutes", value = "240"},
530 {label = "5 minutes", value = "300"},
533 { name = "announcement", type = "text-multi", label = "Announcement" };
537 if data.action == "cancel" then
538 return { status = "canceled" };
541 local fields = shut_down_service_layout:data(data.form);
543 if fields.announcement and #fields.announcement > 0 then
544 local message = st.message({type = "headline"}, fields.announcement):up()
545 :tag("subject"):text("Server is shutting down");
546 send_to_online(message);
549 timer_add_task(tonumber(fields.delay or "5"), prosody.shutdown);
551 return { status = "completed", info = "Server is about to shut down" };
553 return { status = "executing", form = shut_down_service_layout }, "executing";
557 function unload_modules_handler(self, data, state)
558 local layout = dataforms_new {
559 title = "Unload modules";
560 instructions = "Select the modules to be unloaded";
562 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
563 { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
566 if data.action == "cancel" then
567 return { status = "canceled" };
569 local fields = layout:data(data.form);
570 if #fields.modules == 0 then
571 return { status = "completed", error = {
572 message = "Please specify a module. (This means your client misbehaved, as this field is required)"
575 local ok_list, err_list = {}, {};
576 for _, module in ipairs(fields.modules) do
577 local ok, err = modulemanager.unload(data.to, module);
579 ok_list[#ok_list + 1] = module;
581 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
584 local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
585 (#err_list > 0 and ("Failed to unload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
586 return { status = "completed", info = info };
588 local modules = array.collect(keys(hosts[data.to].modules)):sort();
589 return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
593 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
594 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
595 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
596 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
597 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
598 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
599 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
600 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
601 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");
602 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
603 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
604 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
605 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
606 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
608 module:add_item("adhoc", add_user_desc);
609 module:add_item("adhoc", change_user_password_desc);
610 module:add_item("adhoc", config_reload_desc);
611 module:add_item("adhoc", delete_user_desc);
612 module:add_item("adhoc", end_user_session_desc);
613 module:add_item("adhoc", get_user_password_desc);
614 module:add_item("adhoc", get_user_roster_desc);
615 module:add_item("adhoc", get_user_stats_desc);
616 module:add_item("adhoc", get_online_users_desc);
617 module:add_item("adhoc", list_modules_desc);
618 module:add_item("adhoc", load_module_desc);
619 module:add_item("adhoc", reload_modules_desc);
620 module:add_item("adhoc", shut_down_service_desc);
621 module:add_item("adhoc", unload_modules_desc);