1 -- Copyright (C) 2009-2010 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 delete_user_command_handler(self, data, state)
105 local delete_user_layout = dataforms_new{
106 title = "Deleting a User";
107 instructions = "Fill out this form to delete a user.";
109 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
110 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) to delete" };
114 if data.action == "cancel" then
115 return { status = "canceled" };
117 local fields = delete_user_layout:data(data.form);
119 local succeeded = {};
120 for _, aJID in ipairs(fields.accountjids) do
121 local username, host, resource = jid.split(aJID);
122 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) and usermanager_create_user(username, nil, host) then
123 module:log("debug", "User " .. aJID .. " has been deleted");
124 succeeded[#succeeded+1] = aJID;
126 module:log("debug", "Tried to delete non-existant user "..aJID);
127 failed[#failed+1] = aJID;
130 return {status = "completed", info = (#succeeded ~= 0 and
131 "The following accounts were successfully deleted:\n"..t_concat(succeeded, "\n").."\n" or "")..
133 "The following accounts could not be deleted:\n"..t_concat(failed, "\n") or "") };
135 return { status = "executing", form = delete_user_layout }, "executing";
139 function disconnect_user(match_jid)
140 local node, hostname, givenResource = jid.split(match_jid);
141 local host = hosts[hostname];
142 local sessions = host.sessions[node] and host.sessions[node].sessions;
143 for resource, session in pairs(sessions or {}) do
144 if not givenResource or (resource == givenResource) then
145 module:log("debug", "Disconnecting "..node.."@"..hostname.."/"..resource);
152 function end_user_session_handler(self, data, state)
153 local end_user_session_layout = dataforms_new{
154 title = "Ending a User Session";
155 instructions = "Fill out this form to end a user's session.";
157 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
158 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
162 if data.action == "cancel" then
163 return { status = "canceled" };
166 local fields = end_user_session_layout:data(data.form);
168 local succeeded = {};
169 for _, aJID in ipairs(fields.accountjids) do
170 local username, host, resource = jid.split(aJID);
171 if (host == data.to) and usermanager_user_exists(username, host) and disconnect_user(aJID) then
172 succeeded[#succeeded+1] = aJID;
174 failed[#failed+1] = aJID;
177 return {status = "completed", info = (#succeeded ~= 0 and
178 "The following accounts were successfully disconnected:\n"..t_concat(succeeded, "\n").."\n" or "")..
180 "The following accounts could not be disconnected:\n"..t_concat(failed, "\n") or "") };
182 return { status = "executing", form = end_user_session_layout }, "executing";
186 local end_user_session_layout = dataforms_new{
187 title = "Ending a User Session";
188 instructions = "Fill out this form to end a user's session.";
190 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
191 { name = "accountjids", type = "jid-multi", label = "The Jabber ID(s) for which to end sessions" };
195 function get_user_password_handler(self, data, state)
196 local get_user_password_layout = dataforms_new{
197 title = "Getting User's Password";
198 instructions = "Fill out this form to get a user's password.";
200 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
201 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the password" };
204 local get_user_password_result_layout = dataforms_new{
205 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
206 { name = "accountjid", type = "jid-single", label = "JID" };
207 { name = "password", type = "text-single", label = "Password" };
211 if data.action == "cancel" then
212 return { status = "canceled" };
214 local fields = get_user_password_layout:data(data.form);
215 if not fields.accountjid then
216 return { status = "completed", error = { message = "Please specify a JID." } };
218 local user, host, resource = jid.split(fields.accountjid);
219 local accountjid = "";
221 if host ~= data.to then
222 return { status = "completed", error = { message = "Tried to get password for a user on " .. host .. " but command was sent to " .. data.to } };
223 elseif usermanager_user_exists(user, host) then
224 accountjid = fields.accountjid;
225 password = usermanager_get_password(user, host);
227 return { status = "completed", error = { message = "User does not exist" } };
229 return { status = "completed", result = { layout = get_user_password_result_layout, values = {accountjid = accountjid, password = password} } };
231 return { status = "executing", form = get_user_password_layout }, "executing";
235 function get_user_roster_handler(self, data, state)
236 local get_user_roster_layout = dataforms_new{
237 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
238 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for which to retrieve the roster" };
241 local get_user_roster_result_layout = dataforms_new{
242 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
243 { name = "accountjid", type = "jid-single", label = "This is the roster for" };
244 { name = "roster", type = "text-multi", label = "Roster XML" };
248 if data.action == "cancel" then
249 return { status = "canceled" };
252 local fields = get_user_roster_layout:data(data.form);
254 if not fields.accountjid then
255 return { status = "completed", error = { message = "Please specify a JID" } };
258 local user, host, resource = jid.split(fields.accountjid);
259 if host ~= data.to then
260 return { status = "completed", error = { message = "Tried to get roster for a user on " .. host .. " but command was sent to " .. data.to } };
261 elseif not usermanager_user_exists(user, host) then
262 return { status = "completed", error = { message = "User does not exist" } };
264 local roster = rm_load_roster(user, host);
266 local query = st.stanza("query", { xmlns = "jabber:iq:roster" });
267 for jid in pairs(roster) do
268 if jid ~= "pending" and jid then
271 subscription = roster[jid].subscription,
272 ask = roster[jid].ask,
273 name = roster[jid].name,
275 for group in pairs(roster[jid].groups) do
276 query:tag("group"):text(group):up();
282 local query_text = query:__tostring(); -- TODO: Use upcoming pretty_print() function
283 query_text = query_text:gsub("><", ">\n<");
285 local result = get_user_roster_result_layout:form({ accountjid = user.."@"..host, roster = query_text }, "result");
286 result:add_child(query);
287 return { status = "completed", other = result };
289 return { status = "executing", form = get_user_roster_layout }, "executing";
293 function get_user_stats_handler(self, data, state)
294 local get_user_stats_layout = dataforms_new{
295 title = "Get User Statistics";
296 instructions = "Fill out this form to gather user statistics.";
298 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
299 { name = "accountjid", type = "jid-single", required = true, label = "The Jabber ID for statistics" };
302 local get_user_stats_result_layout = dataforms_new{
303 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
304 { name = "ipaddresses", type = "text-multi", label = "IP Addresses" };
305 { name = "rostersize", type = "text-single", label = "Roster size" };
306 { name = "onlineresources", type = "text-multi", label = "Online Resources" };
310 if data.action == "cancel" then
311 return { status = "canceled" };
314 local fields = get_user_stats_layout:data(data.form);
316 if not fields.accountjid then
317 return { status = "completed", error = { message = "Please specify a JID." } };
320 local user, host, resource = jid.split(fields.accountjid);
321 if host ~= data.to then
322 return { status = "completed", error = { message = "Tried to get stats for a user on " .. host .. " but command was sent to " .. data.to } };
323 elseif not usermanager_user_exists(user, host) then
324 return { status = "completed", error = { message = "User does not exist" } };
326 local roster = rm_load_roster(user, host);
327 local rostersize = 0;
329 local resources = "";
330 for jid in pairs(roster) do
331 if jid ~= "pending" and jid then
332 rostersize = rostersize + 1;
335 for resource, session in pairs((hosts[host].sessions[user] and hosts[host].sessions[user].sessions) or {}) do
336 resources = resources .. "\n" .. resource;
337 IPs = IPs .. "\n" .. session.ip;
339 return { status = "completed", result = {layout = get_user_stats_result_layout, values = {ipaddresses = IPs, rostersize = tostring(rostersize),
340 onlineresources = resources}} };
342 return { status = "executing", form = get_user_stats_layout }, "executing";
346 function get_online_users_command_handler(self, data, state)
347 local get_online_users_layout = dataforms_new{
348 title = "Getting List of Online Users";
349 instructions = "How many users should be returned at most?";
351 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
352 { name = "max_items", type = "list-single", label = "Maximum number of users",
353 value = { "25", "50", "75", "100", "150", "200", "all" } };
354 { name = "details", type = "boolean", label = "Show details" };
357 local get_online_users_result_layout = dataforms_new{
358 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
359 { name = "onlineuserjids", type = "text-multi", label = "The list of all online users" };
363 if data.action == "cancel" then
364 return { status = "canceled" };
367 local fields = get_online_users_layout:data(data.form);
369 local max_items = nil
370 if fields.max_items ~= "all" then
371 max_items = tonumber(fields.max_items);
375 for username, user in pairs(hosts[data.to].sessions or {}) do
376 if (max_items ~= nil) and (count >= max_items) then
379 users[#users+1] = username.."@"..data.to;
381 if fields.details then
382 for resource, session in pairs(user.sessions or {}) do
383 local status, priority = "unavailable", tostring(session.priority or "-");
384 if session.presence then
385 status = session.presence:child_with_name("show");
387 status = status:get_text() or "[invalid!]";
389 status = "available";
392 users[#users+1] = " - "..resource..": "..status.."("..priority..")";
396 return { status = "completed", result = {layout = get_online_users_result_layout, values = {onlineuserjids=t_concat(users, "\n")}} };
398 return { status = "executing", form = get_online_users_layout }, "executing";
402 function list_modules_handler(self, data, state)
403 local result = dataforms_new {
404 title = "List of loaded modules";
406 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#list" };
407 { name = "modules", type = "text-multi", label = "The following modules are loaded:" };
410 local modules = array.collect(keys(hosts[data.to].modules)):sort():concat("\n");
412 return { status = "completed", result = { layout = result; values = { modules = modules } } };
415 function load_module_handler(self, data, state)
416 local layout = dataforms_new {
417 title = "Load module";
418 instructions = "Specify the module to be loaded";
420 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#load" };
421 { name = "module", type = "text-single", required = true, label = "Module to be loaded:"};
424 if data.action == "cancel" then
425 return { status = "canceled" };
427 local fields = layout:data(data.form);
428 if (not fields.module) or (fields.module == "") then
429 return { status = "completed", error = {
430 message = "Please specify a module."
433 if modulemanager.is_loaded(data.to, fields.module) then
434 return { status = "completed", info = "Module already loaded" };
436 local ok, err = modulemanager.load(data.to, fields.module);
438 return { status = "completed", info = 'Module "'..fields.module..'" successfully loaded on host "'..data.to..'".' };
440 return { status = "completed", error = { message = 'Failed to load module "'..fields.module..'" on host "'..data.to..
441 '". Error was: "'..tostring(err or "<unspecified>")..'"' } };
444 local modules = array.collect(keys(hosts[data.to].modules)):sort();
445 return { status = "executing", form = layout }, "executing";
449 function reload_modules_handler(self, data, state)
450 local layout = dataforms_new {
451 title = "Reload modules";
452 instructions = "Select the modules to be reloaded";
454 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#reload" };
455 { name = "modules", type = "list-multi", required = true, label = "Modules to be reloaded:"};
458 if data.action == "cancel" then
459 return { status = "canceled" };
461 local fields = layout:data(data.form);
462 if #fields.modules == 0 then
463 return { status = "completed", error = {
464 message = "Please specify a module. (This means your client misbehaved, as this field is required)"
467 local ok_list, err_list = {}, {};
468 for _, module in ipairs(fields.modules) do
469 local ok, err = modulemanager.reload(data.to, module);
471 ok_list[#ok_list + 1] = module;
473 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
476 local info = (#ok_list > 0 and ("The following modules were successfully reloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
477 (#err_list > 0 and ("Failed to reload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
478 return { status = "completed", info = info };
480 local modules = array.collect(keys(hosts[data.to].modules)):sort();
481 return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
485 function send_to_online(message, server)
487 sessions = { [server] = hosts[server] };
493 for domain, session in pairs(sessions) do
494 for user in pairs(session.sessions or {}) do
496 message.attr.from = domain;
497 message.attr.to = user.."@"..domain;
498 core_post_stanza(session, message);
505 function shut_down_service_handler(self, data, state)
506 local shut_down_service_layout = dataforms_new{
507 title = "Shutting Down the Service";
508 instructions = "Fill out this form to shut down the service.";
510 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/admin" };
511 { name = "delay", type = "list-single", label = "Time delay before shutting down",
512 value = { {label = "30 seconds", value = "30"},
513 {label = "60 seconds", value = "60"},
514 {label = "90 seconds", value = "90"},
515 {label = "2 minutes", value = "120"},
516 {label = "3 minutes", value = "180"},
517 {label = "4 minutes", value = "240"},
518 {label = "5 minutes", value = "300"},
521 { name = "announcement", type = "text-multi", label = "Announcement" };
525 if data.action == "cancel" then
526 return { status = "canceled" };
529 local fields = shut_down_service_layout:data(data.form);
531 if fields.announcement and #fields.announcement > 0 then
532 local message = st.message({type = "headline"}, fields.announcement):up()
533 :tag("subject"):text("Server is shutting down");
534 send_to_online(message);
537 timer_add_task(tonumber(fields.delay or "5"), prosody.shutdown);
539 return { status = "completed", info = "Server is about to shut down" };
541 return { status = "executing", form = shut_down_service_layout }, "executing";
547 function unload_modules_handler(self, data, state)
548 local layout = dataforms_new {
549 title = "Unload modules";
550 instructions = "Select the modules to be unloaded";
552 { name = "FORM_TYPE", type = "hidden", value = "http://prosody.im/protocol/modules#unload" };
553 { name = "modules", type = "list-multi", required = true, label = "Modules to be unloaded:"};
556 if data.action == "cancel" then
557 return { status = "canceled" };
559 local fields = layout:data(data.form);
560 if #fields.modules == 0 then
561 return { status = "completed", error = {
562 message = "Please specify a module. (This means your client misbehaved, as this field is required)"
565 local ok_list, err_list = {}, {};
566 for _, module in ipairs(fields.modules) do
567 local ok, err = modulemanager.unload(data.to, module);
569 ok_list[#ok_list + 1] = module;
571 err_list[#err_list + 1] = module .. "(Error: " .. tostring(err) .. ")";
574 local info = (#ok_list > 0 and ("The following modules were successfully unloaded on host "..data.to..":\n"..t_concat(ok_list, "\n")) or "")..
575 (#err_list > 0 and ("Failed to unload the following modules on host "..data.to..":\n"..t_concat(err_list, "\n")) or "");
576 return { status = "completed", info = info };
578 local modules = array.collect(keys(hosts[data.to].modules)):sort();
579 return { status = "executing", form = { layout = layout; values = { modules = modules } } }, "executing";
583 local add_user_desc = adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler, "admin");
584 local change_user_password_desc = adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler, "admin");
585 local delete_user_desc = adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler, "admin");
586 local end_user_session_desc = adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler, "admin");
587 local get_user_password_desc = adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler, "admin");
588 local get_user_roster_desc = adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler, "admin");
589 local get_user_stats_desc = adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler, "admin");
590 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");
591 local list_modules_desc = adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler, "admin");
592 local load_module_desc = adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler, "admin");
593 local reload_modules_desc = adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler, "admin");
594 local shut_down_service_desc = adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler, "admin");
595 local unload_modules_desc = adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler, "admin");
597 module:add_item("adhoc", add_user_desc);
598 module:add_item("adhoc", change_user_password_desc);
599 module:add_item("adhoc", delete_user_desc);
600 module:add_item("adhoc", end_user_session_desc);
601 module:add_item("adhoc", get_user_password_desc);
602 module:add_item("adhoc", get_user_roster_desc);
603 module:add_item("adhoc", get_user_stats_desc);
604 module:add_item("adhoc", get_online_users_desc);
605 module:add_item("adhoc", list_modules_desc);
606 module:add_item("adhoc", load_module_desc);
607 module:add_item("adhoc", reload_modules_desc);
608 module:add_item("adhoc", shut_down_service_desc);
609 module:add_item("adhoc", unload_modules_desc);