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 require "util.iterators";
14 local usermanager_user_exists = require "core.usermanager".user_exists;
15 local usermanager_create_user = require "core.usermanager".create_user;
16 local usermanager_get_password = require "core.usermanager".get_password;
17 local usermanager_set_password = require "core.usermanager".set_password;
18 local is_admin = require "core.usermanager".is_admin;
19 local rm_load_roster = require "core.rostermanager".load_roster;
20 local st, jid, uuid = require "util.stanza", require "util.jid", require "util.uuid";
21 local timer_add_task = require "util.timer".add_task;
22 local dataforms_new = require "util.dataforms".new;
23 local array = require "util.array";
24 local modulemanager = require "modulemanager";
26 local adhoc_new = module:require "adhoc".new;
28 function add_user_command_handler(self, data, state)
29 local add_user_layout = dataforms_new{
30 title = "Adding a User";
31 instructions = "Fill out this form to add a user.";
33 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
34 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for the account to be added" };
35 { name = "password", type = "text-private", label = "The password for this account" };
36 { name = "password-verify", type = "text-private", label = "Retype password" };
40 if data.action == "cancel" then
41 return { status = "canceled" };
43 local fields = add_user_layout:data(data.form);
44 if not fields.accountjid then
45 return { status = "completed", error = { message = "You need to specify a JID." } };
47 local username, host, resource = jid.split(fields.accountjid);
48 if data.to ~= host then
49 return { status = "completed", error = { message = "Trying to add a user on " .. host .. " but command was sent to " .. data.to}};
51 if (fields["password"] == fields["password-verify"]) and username and host then
52 if usermanager_user_exists(username, host) then
53 return { status = "completed", error = { message = "Account already exists" } };
55 if usermanager_create_user(username, fields.password, host) then
56 module:log("info", "Created new account " .. username.."@"..host);
57 return { status = "completed", info = "Account successfully created" };
59 return { status = "completed", error = { message = "Failed to write data to disk" } };
63 module:log("debug", (fields.accountjid or "<nil>") .. " " .. (fields.password or "<nil>") .. " "
64 .. (fields["password-verify"] or "<nil>"));
65 return { status = "completed", error = { message = "Invalid data.\nPassword mismatch, or empty username" } };
68 return { status = "executing", form = add_user_layout }, "executing";
72 function change_user_password_command_handler(self, data, state)
73 local change_user_password_layout = dataforms_new{
74 title = "Changing a User Password";
75 instructions = "Fill out this form to change a user's password.";
77 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
78 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for this account" };
79 { name = "password", type = "text-private", required = true, label = "The password for this account" };
83 if data.action == "cancel" then
84 return { status = "canceled" };
86 local fields = change_user_password_layout:data(data.form);
87 if not fields.accountjid or fields.accountjid == "" or not fields.password then
88 return { status = "completed", error = { message = "Please specify username and password" } };
90 local username, host, resource = jid.split(fields.accountjid);
91 if data.to ~= host then
92 return { status = "completed", error = { message = "Trying to change the password of a user on " .. host .. " but command was sent to " .. data.to}};
94 if usermanager_user_exists(username, host) and usermanager_set_password(username, fields.password, host) then
95 return { status = "completed", info = "Password successfully changed" };
97 return { status = "completed", error = { message = "User does not exist" } };
100 return { status = "executing", form = change_user_password_layout }, "executing";
104 function config_reload_handler(self, data, state)
105 local ok, err = prosody.reload_config();
107 return { status = "completed", info = "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
109 return { status = "completed", error = { message = "Failed to reload config: " .. tostring(err) } };
114 function delete_user_command_handler(self, data, state)
115 local delete_user_layout = dataforms_new{
116 title = "Deleting a User";
117 instructions = "Fill out this form to delete a user.";
119 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
120 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
124 if data.action == "cancel" then
125 return { status = "canceled" };
127 local fields = delete_user_layout:data(data.form);
129 local succeeded = {};
130 for _, aJID in ipairs(fields.accountjids) do
131 local username, host, resource = jid.split(aJID);
132 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then
133 module:log("debug", "User " .. aJID .. " has been deleted");
134 succeeded[#succeeded+1] = aJID;
136 module:log("debug", "Tried to delete non-existant user "..aJID);
137 failed[#failed+1] = aJID;
140 return {status = "completed", info = (#succeeded ~= 0 and
141 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
143 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
145 return { status = "executing", form = delete_user_layout }, "executing";
149 function disconnect_user(match_jid)
150 local node, hostname, givenResource = jid.split(match_jid);
151 local host = hosts[hostname];
152 local sessions = host.sessions[node] and host.sessions[node].sessions;
153 for resource, session in pairs(sessions or {}) do
154 if not givenResource or (resource == givenResource) then
155 module:log("debug", "Disconnecting "..node.."@"..hostname.."/"..resource);
162 function end_user_session_handler(self, data, state)
163 local end_user_session_layout = dataforms_new{
164 title = "Ending a User Session";
165 instructions = "Fill out this form to end a user's session.";
167 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
168 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
172 if data.action == "cancel" then
173 return { status = "canceled" };
176 local fields = end_user_session_layout:data(data.form);
178 local succeeded = {};
179 for _, aJID in ipairs(fields.accountjids) do
180 local username, host, resource = jid.split(aJID);
181 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) then
182 succeeded[#succeeded+1] = aJID;
184 failed[#failed+1] = aJID;
187 return {status = "completed", info = (#succeeded ~= 0 and
188 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
190 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
192 return { status = "executing", form = end_user_session_layout }, "executing";
196 local end_user_session_layout = dataforms_new{
197 title = "Ending a User Session";
198 instructions = "Fill out this form to end a user's session.";
200 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
201 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
205 function get_user_password_handler(self, data, state)
206 local get_user_password_layout = dataforms_new{
207 title = "Getting User's Password";
208 instructions = "Fill out this form to get a user's password.";
210 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
211 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the password" };
214 local get_user_password_result_layout = dataforms_new{
215 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
216 { name = "accountjid", type = "jid-single", label = "JID" };
217 { name = "password", type = "text-single", label = "Password" };
221 if data.action == "cancel" then
222 return { status = "canceled" };
224 local fields = get_user_password_layout:data(data.form);
225 if not fields.accountjid then
226 return { status = "completed", error = { message = "Please specify a JID." } };
228 local user, host, resource = jid.split(fields.accountjid);
229 local accountjid = "";
231 if host ~= data.to then
232 return { status = "completed", error = { message = "Tried to get password for a user on " .. host .. " but command was sent to " .. data.to } };
233 elseif usermanager_user_exists(user, host) then
234 accountjid = fields.accountjid;
235 password = usermanager_get_password(user, host);
237 return { status = "completed", error = { message = "User does not exist" } };
239 return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
241 return { status = "executing", form = get_user_password_layout }, "executing";
245 function get_user_roster_handler(self, data, state)
246 local get_user_roster_layout = dataforms_new{
247 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
248 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the roster" };
251 local get_user_roster_result_layout = dataforms_new{
252 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
253 { name = "accountjid", type = "jid-single", label = "This is the roster for" };
254 { name = "roster", type = "text-multi", label = "Roster XML" };
258 if data.action == "cancel" then
259 return { status = "canceled" };
262 local fields = get_user_roster_layout:data(data.form);
264 if not fields.accountjid then
265 return { status = "completed", error = { message = "Please specify a JID" } };
268 local user, host, resource = jid.split(fields.accountjid);
269 if host ~= data.to then
270 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. data.to } };
271 elseif not usermanager_user_exists(user, host) then
272 return { status = "completed", error = { message = "User does not exist" } };
274 local roster = rm_load_roster(user, host);
276 local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
277 for jid in pairs(roster) do
278 if jid ~= "pending" and jid then
281 subscription = roster[jid].subscription,
282 ask = roster[jid].ask,
283 name = roster[jid].name,
285 for group in pairs(roster[jid].groups) do
286 query:tag("group"):text(group):up();
292 local query_text = query:__tostring(); -- TODO: Use upcoming pretty_print() function
293 query_text = query_text:gsub("><", ">\n<");
295 local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result");
296 result:add_child(query);
297 return { status = "completed", other = result };
299 return { status = "executing", form = get_user_roster_layout }, "executing";
303 function get_user_stats_handler(self, data, state)
304 local get_user_stats_layout = dataforms_new{
305 title = "Get User Statistics";
306 instructions = "Fill out this form to gather user statistics.";
308 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
309 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for statistics" };
312 local get_user_stats_result_layout = dataforms_new{
313 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
314 { name = "ipaddresses", type = "text-multi", label = "IP Addresses" };
315 { name = "rostersize", type = "text-single", label = "Roster size" };
316 { name = "onlineresources", type = "text-multi", label = "Online Resources" };
320 if data.action == "cancel" then
321 return { status = "canceled" };
324 local fields = get_user_stats_layout:data(data.form);
326 if not fields.accountjid then
327 return { status = "completed", error = { message = "Please specify a JID." } };
330 local user, host, resource = jid.split(fields.accountjid);
331 if host ~= data.to then
332 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. data.to } };
333 elseif not usermanager_user_exists(user, host) then
334 return { status = "completed", error = { message = "User does not exist" } };
336 local roster = rm_load_roster(user, host);
337 local rostersize = 0;
339 local resources = "";
340 for jid in pairs(roster) do
341 if jid ~= "pending" and jid then
342 rostersize = rostersize + 1;
345 for resource, session in pairs((hosts[host].sessions[user] and hosts[host].sessions[user].sessions) or {}) do
346 resources = resources .. "\n" .. resource;
347 IPs = IPs .. "\n" .. session.ip;
349 return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
350 onlineresources = resources}} };
352 return { status = "executing", form = get_user_stats_layout }, "executing";
356 function get_online_users_command_handler(self, data, state)
357 local get_online_users_layout = dataforms_new{
358 title = "Getting List of Online Users";
359 instructions = "How many users should be returned at most?";
361 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
362 { name = "max_items", type = "list-single", label = "Maximum number of users",
363 value = { "25", "50", "75", "100", "150", "200", "all" } };
364 { name = "details", type = "boolean", label = "Show details" };
367 local get_online_users_result_layout = dataforms_new{
368 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
369 { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
373 if data.action == "cancel" then
374 return { status = "canceled" };
377 local fields = get_online_users_layout:data(data.form);
379 local max_items = nil
380 if fields.max_items ~= "all" then
381 max_items = tonumber(fields.max_items);
385 for username, user in pairs(hosts[data.to].sessions or {}) do
386 if (max_items ~= nil) and (count >= max_items) then
389 users[#users+1] = username.."@"..data.to;
391 if fields.details then
392 for resource, session in pairs(user.sessions or {}) do
393 local status, priority = "unavailable", tostring(session.priority or "-");
394 if session.presence then
395 status = session.presence:child_with_name("show");
397 status = status:get_text() or "[invalid!]";
399 status = "available";
402 users[#users+1] = " - "..resource..": "..status.."("..priority..")";
406 return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
408 return { status = "executing", form = get_online_users_layout }, "executing";
412 function list_modules_handler(self, data, state)
413 local result = dataforms_new {
414 title = "List of loaded modules";
416 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
417 { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
420 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n");
422 return { status = "completed", result = { layout = result; values = { modules = modules } } };
425 function load_module_handler(self, data, state)
426 local layout = dataforms_new {
427 title = "Load module";
428 instructions = "Specify the module to be loaded";
430 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
431 { name = "module", type = "text-single", required = true, label = "Module to be loaded:"};
434 if data.action == "cancel" then
435 return { status = "canceled" };
437 local fields = layout:data(data.form);
438 if (not fields.module) or (fields.module == "") then
439 return { status = "completed", error = {
440 message = "Please specify a module."
443 if modulemanager.is_loaded(data.to, fields.module) then
444 return { status = "completed", info = "Module already loaded" };
446 local ok, err = modulemanager.load(data.to, fields.module);
448 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..data.to..'".' };
450 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..data.to..
451 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
454 local modules = array.collect(keys(hosts[data.to].modules)):sort();
455 return { status = "executing", form = layout }, "executing";
459 function reload_modules_handler(self, data, state)
460 local layout = dataforms_new {
461 title = "Reload modules";
462 instructions = "Select the modules to be reloaded";
464 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
465 { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
468 if data.action == "cancel" then
469 return { status = "canceled" };
471 local fields = layout:data(data.form);
472 if #fields.modules == 0 then
473 return { status = "completed", error = {
474 message = "Please specify a module. (This means your client misbehaved, as this field is required)"
477 local ok_list, err_list = {}, {};
478 for _, module in ipairs(fields.modules) do
479 local ok, err = modulemanager.reload(data.to, module);
481 ok_list[#ok_list + 1] = module;
483 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
486 local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
487 (#err_list > 0 and ("Failed to reload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
488 return { status = "completed", info = info };
490 local modules = array.collect(keys(hosts[data.to].modules)):sort();
491 return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
495 function send_to_online(message, server)
497 sessions = { [server] = hosts[server] };
503 for domain, session in pairs(sessions) do
504 for user in pairs(session.sessions or {}) do
506 message.attr.from = domain;
507 message.attr.to = user.."@"..domain;
508 core_post_stanza(session, message);
515 function shut_down_service_handler(self, data, state)
516 local shut_down_service_layout = dataforms_new{
517 title = "Shutting Down the Service";
518 instructions = "Fill out this form to shut down the service.";
520 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
521 { name = "delay", type = "list-single", label = "Time delay before shutting down",
522 value = { {label = "30 seconds", value = "30"},
523 {label = "60 seconds", value = "60"},
524 {label = "90 seconds", value = "90"},
525 {label = "2 minutes", value = "120"},
526 {label = "3 minutes", value = "180"},
527 {label = "4 minutes", value = "240"},
528 {label = "5 minutes", value = "300"},
531 { name = "announcement", type = "text-multi", label = "Announcement" };
535 if data.action == "cancel" then
536 return { status = "canceled" };
539 local fields = shut_down_service_layout:data(data.form);
541 if fields.announcement and #fields.announcement > 0 then
542 local message = st.message({type = "headline"}, fields.announcement):up()
543 :tag("subject"):text("Server is shutting down");
544 send_to_online(message);
547 timer_add_task(tonumber(fields.delay or "5"), prosody.shutdown);
549 return { status = "completed", info = "Server is about to shut down" };
551 return { status = "executing", form = shut_down_service_layout }, "executing";
555 function unload_modules_handler(self, data, state)
556 local layout = dataforms_new {
557 title = "Unload modules";
558 instructions = "Select the modules to be unloaded";
560 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
561 { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
564 if data.action == "cancel" then
565 return { status = "canceled" };
567 local fields = layout:data(data.form);
568 if #fields.modules == 0 then
569 return { status = "completed", error = {
570 message = "Please specify a module. (This means your client misbehaved, as this field is required)"
573 local ok_list, err_list = {}, {};
574 for _, module in ipairs(fields.modules) do
575 local ok, err = modulemanager.unload(data.to, module);
577 ok_list[#ok_list + 1] = module;
579 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
582 local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
583 (#err_list > 0 and ("Failed to unload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
584 return { status = "completed", info = info };
586 local modules = array.collect(keys(hosts[data.to].modules)):sort();
587 return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
591 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
592 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
593 local config_reload_desc = adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler, "global_admin");
594 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
595 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
596 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
597 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
598 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
599 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");
600 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
601 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
602 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
603 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "global_admin");
604 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
606 module:add_item("adhoc", add_user_desc);
607 module:add_item("adhoc", change_user_password_desc);
608 module:add_item("adhoc", config_reload_desc);
609 module:add_item("adhoc", delete_user_desc);
610 module:add_item("adhoc", end_user_session_desc);
611 module:add_item("adhoc", get_user_password_desc);
612 module:add_item("adhoc", get_user_roster_desc);
613 module:add_item("adhoc", get_user_stats_desc);
614 module:add_item("adhoc", get_online_users_desc);
615 module:add_item("adhoc", list_modules_desc);
616 module:add_item("adhoc", load_module_desc);
617 module:add_item("adhoc", reload_modules_desc);
618 module:add_item("adhoc", shut_down_service_desc);
619 module:add_item("adhoc", unload_modules_desc);