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.
6 -- luacheck: ignore 212/self 212/data 212/state 412/err 422/err
10 local prosody
= _G
.prosody
;
11 local hosts
= prosody
.hosts
;
12 local t_concat
= table.concat
;
13 local t_sort
= table.sort;
15 local module_host
= module
:get_host();
17 local keys
= require
"util.iterators".keys
;
18 local usermanager_user_exists
= require
"core.usermanager".user_exists
;
19 local usermanager_create_user
= require
"core.usermanager".create_user
;
20 local usermanager_delete_user
= require
"core.usermanager".delete_user
;
21 local usermanager_get_password
= require
"core.usermanager".get_password
;
22 local usermanager_set_password
= require
"core.usermanager".set_password
;
23 local hostmanager_activate
= require
"core.hostmanager".activate
;
24 local hostmanager_deactivate
= require
"core.hostmanager".deactivate
;
25 local rm_load_roster
= require
"core.rostermanager".load_roster
;
26 local st
, jid
= require
"util.stanza", require
"util.jid";
27 local timer_add_task
= require
"util.timer".add_task
;
28 local dataforms_new
= require
"util.dataforms".new
;
29 local array
= require
"util.array";
30 local modulemanager
= require
"core.modulemanager";
31 local core_post_stanza
= prosody
.core_post_stanza
;
32 local adhoc_simple
= require
"util.adhoc".new_simple_form
;
33 local adhoc_initial
= require
"util.adhoc".new_initial_data_form
;
34 local set
= require
"util.set";
36 module
:depends("adhoc");
37 local adhoc_new
= module
:require
"adhoc".new
;
39 local function generate_error_message(errors
)
41 for name
, err
in pairs(errors
) do
42 errmsg
[#errmsg
+ 1] = name
.. ": " .. err
;
44 return { status
= "completed", error = { message
= t_concat(errmsg
, "\n") } };
48 local add_user_layout
= dataforms_new
{
49 title
= "Adding a User";
50 instructions
= "Fill out this form to add a user.";
52 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
53 { name
= "accountjid", type = "jid-single", required
= true, label
= "The Jabber ID for the account to be added" };
54 { name
= "password", type = "text-private", label
= "The password for this account" };
55 { name
= "password-verify", type = "text-private", label
= "Retype password" };
58 local add_user_command_handler
= adhoc_simple(add_user_layout
, function(fields
, err
)
60 return generate_error_message(err
);
62 local username
, host
, resource
= jid
.split(fields
.accountjid
);
63 if module_host
~= host
then
64 return { status
= "completed", error = { message
= "Trying to add a user on " .. host
.. " but command was sent to " .. module_host
}};
66 if (fields
["password"] == fields
["password-verify"]) and username
and host
then
67 if usermanager_user_exists(username
, host
) then
68 return { status
= "completed", error = { message
= "Account already exists" } };
70 if usermanager_create_user(username
, fields
.password
, host
) then
71 module
:log("info", "Created new account %s@%s", username
, host
);
72 return { status
= "completed", info
= "Account successfully created" };
74 return { status
= "completed", error = { message
= "Failed to write data to disk" } };
78 module
:log("debug", "Invalid data, password mismatch or empty username while creating account for %s", fields
.accountjid
or "<nil>");
79 return { status
= "completed", error = { message
= "Invalid data.\nPassword mismatch, or empty username" } };
83 -- Changing a user's password
84 local change_user_password_layout
= dataforms_new
{
85 title
= "Changing a User Password";
86 instructions
= "Fill out this form to change a user's password.";
88 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
89 { name
= "accountjid", type = "jid-single", required
= true, label
= "The Jabber ID for this account" };
90 { name
= "password", type = "text-private", required
= true, label
= "The password for this account" };
93 local change_user_password_command_handler
= adhoc_simple(change_user_password_layout
, function(fields
, err
)
95 return generate_error_message(err
);
97 local username
, host
, resource
= jid
.split(fields
.accountjid
);
98 if module_host
~= host
then
100 status
= "completed",
102 message
= "Trying to change the password of a user on " .. host
.. " but command was sent to " .. module_host
106 if usermanager_user_exists(username
, host
) and usermanager_set_password(username
, fields
.password
, host
, nil) then
107 return { status
= "completed", info
= "Password successfully changed" };
109 return { status
= "completed", error = { message
= "User does not exist" } };
113 -- Reloading the config
114 local function config_reload_handler(self
, data
, state
)
115 local ok
, err
= prosody
.reload_config();
117 return { status
= "completed", info
= "Configuration reloaded (modules may need to be reloaded for this to have an effect)" };
119 return { status
= "completed", error = { message
= "Failed to reload config: " .. tostring(err
) } };
123 -- Deleting a user's account
124 local delete_user_layout
= dataforms_new
{
125 title
= "Deleting a User";
126 instructions
= "Fill out this form to delete a user.";
128 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
129 { name
= "accountjids", type = "jid-multi", required
= true, label
= "The Jabber ID(s) to delete" };
132 local delete_user_command_handler
= adhoc_simple(delete_user_layout
, function(fields
, err
)
134 return generate_error_message(err
);
137 local succeeded
= {};
138 for _
, aJID
in ipairs(fields
.accountjids
) do
139 local username
, host
, resource
= jid
.split(aJID
);
140 if (host
== module_host
) and usermanager_user_exists(username
, host
) and usermanager_delete_user(username
, host
) then
141 module
:log("debug", "User %s has been deleted", aJID
);
142 succeeded
[#succeeded
+1] = aJID
;
144 module
:log("debug", "Tried to delete non-existant user %s", aJID
);
145 failed
[#failed
+1] = aJID
;
148 return {status
= "completed", info
= (#succeeded
~= 0 and
149 "The following accounts were successfully deleted:\n"..t_concat(succeeded
, "\n").."\n" or "")..
151 "The following accounts could not be deleted:\n"..t_concat(failed
, "\n") or "") };
154 -- Ending a user's session
155 local function disconnect_user(match_jid
)
156 local node
, hostname
, givenResource
= jid
.split(match_jid
);
157 local host
= hosts
[hostname
];
158 local sessions
= host
.sessions
[node
] and host
.sessions
[node
].sessions
;
159 for resource
, session
in pairs(sessions
or {}) do
160 if not givenResource
or (resource
== givenResource
) then
161 module
:log("debug", "Disconnecting %s@%s/%s", node
, hostname
, resource
);
168 local end_user_session_layout
= dataforms_new
{
169 title
= "Ending a User Session";
170 instructions
= "Fill out this form to end a user's session.";
172 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
173 { name
= "accountjids", type = "jid-multi", label
= "The Jabber ID(s) for which to end sessions", required
= true };
176 local end_user_session_handler
= adhoc_simple(end_user_session_layout
, function(fields
, err
)
178 return generate_error_message(err
);
181 local succeeded
= {};
182 for _
, aJID
in ipairs(fields
.accountjids
) do
183 local username
, host
, resource
= jid
.split(aJID
);
184 if (host
== module_host
) and usermanager_user_exists(username
, host
) and disconnect_user(aJID
) then
185 succeeded
[#succeeded
+1] = aJID
;
187 failed
[#failed
+1] = aJID
;
190 return {status
= "completed", info
= (#succeeded
~= 0 and
191 "The following accounts were successfully disconnected:\n"..t_concat(succeeded
, "\n").."\n" or "")..
193 "The following accounts could not be disconnected:\n"..t_concat(failed
, "\n") or "") };
196 -- Getting a user's password
197 local get_user_password_layout
= dataforms_new
{
198 title
= "Getting User's Password";
199 instructions
= "Fill out this form to get a user's password.";
201 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
202 { name
= "accountjid", type = "jid-single", required
= true, label
= "The Jabber ID for which to retrieve the password" };
205 local get_user_password_result_layout
= dataforms_new
{
206 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
207 { name
= "accountjid", type = "jid-single", label
= "JID" };
208 { name
= "password", type = "text-single", label
= "Password" };
211 local get_user_password_handler
= adhoc_simple(get_user_password_layout
, function(fields
, err
)
213 return generate_error_message(err
);
215 local user
, host
, resource
= jid
.split(fields
.accountjid
);
218 if host
~= module_host
then
219 return { status
= "completed", error = { message
= "Tried to get password for a user on " .. host
.. " but command was sent to " .. module_host
} };
220 elseif usermanager_user_exists(user
, host
) then
221 accountjid
= fields
.accountjid
;
222 password
= usermanager_get_password(user
, host
);
224 return { status
= "completed", error = { message
= "User does not exist" } };
226 return { status
= "completed", result
= { layout
= get_user_password_result_layout
, values
= {accountjid
= accountjid
, password
= password
} } };
229 -- Getting a user's roster
230 local get_user_roster_layout
= dataforms_new
{
231 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
232 { name
= "accountjid", type = "jid-single", required
= true, label
= "The Jabber ID for which to retrieve the roster" };
235 local get_user_roster_result_layout
= dataforms_new
{
236 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
237 { name
= "accountjid", type = "jid-single", label
= "This is the roster for" };
238 { name
= "roster", type = "text-multi", label
= "Roster XML" };
241 local get_user_roster_handler
= adhoc_simple(get_user_roster_layout
, function(fields
, err
)
243 return generate_error_message(err
);
246 local user
, host
, resource
= jid
.split(fields
.accountjid
);
247 if host
~= module_host
then
248 return { status
= "completed", error = { message
= "Tried to get roster for a user on " .. host
.. " but command was sent to " .. module_host
} };
249 elseif not usermanager_user_exists(user
, host
) then
250 return { status
= "completed", error = { message
= "User does not exist" } };
252 local roster
= rm_load_roster(user
, host
);
254 local query
= st
.stanza("query", { xmlns
= "jabber:iq:roster" });
255 for contact_jid
in pairs(roster
) do
259 subscription
= roster
[contact_jid
].subscription
,
260 ask
= roster
[contact_jid
].ask
,
261 name
= roster
[contact_jid
].name
,
263 for group
in pairs(roster
[contact_jid
].groups
) do
264 query
:tag("group"):text(group
):up();
270 local query_text
= tostring(query
):gsub("><", ">\n<");
272 local result
= get_user_roster_result_layout
:form({ accountjid
= user
.."@"..host
, roster
= query_text
}, "result");
273 result
:add_child(query
);
274 return { status
= "completed", other
= result
};
277 -- Getting user statistics
278 local get_user_stats_layout
= dataforms_new
{
279 title
= "Get User Statistics";
280 instructions
= "Fill out this form to gather user statistics.";
282 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
283 { name
= "accountjid", type = "jid-single", required
= true, label
= "The Jabber ID for statistics" };
286 local get_user_stats_result_layout
= dataforms_new
{
287 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
288 { name
= "ipaddresses", type = "text-multi", label
= "IP Addresses" };
289 { name
= "rostersize", type = "text-single", label
= "Roster size" };
290 { name
= "onlineresources", type = "text-multi", label
= "Online Resources" };
293 local get_user_stats_handler
= adhoc_simple(get_user_stats_layout
, function(fields
, err
)
295 return generate_error_message(err
);
298 local user
, host
= jid
.split(fields
.accountjid
);
299 if host
~= module_host
then
300 return { status
= "completed", error = { message
= "Tried to get stats for a user on " .. host
.. " but command was sent to " .. module_host
} };
301 elseif not usermanager_user_exists(user
, host
) then
302 return { status
= "completed", error = { message
= "User does not exist" } };
304 local roster
= rm_load_roster(user
, host
);
305 local rostersize
= 0;
307 local resources
= "";
308 for contact_jid
in pairs(roster
) do
310 rostersize
= rostersize
+ 1;
313 for resource
, session
in pairs((hosts
[host
].sessions
[user
] and hosts
[host
].sessions
[user
].sessions
) or {}) do
314 resources
= resources
.. "\n" .. resource
;
315 IPs
= IPs
.. "\n" .. session
.ip
;
317 return { status
= "completed", result
= {layout
= get_user_stats_result_layout
, values
= {ipaddresses
= IPs
, rostersize
= tostring(rostersize
),
318 onlineresources
= resources
}} };
321 -- Getting a list of online users
322 local get_online_users_layout
= dataforms_new
{
323 title
= "Getting List of Online Users";
324 instructions
= "How many users should be returned at most?";
326 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
327 { name
= "max_items", type = "list-single", label
= "Maximum number of users",
328 options
= { "25", "50", "75", "100", "150", "200", "all" } };
329 { name
= "details", type = "boolean", label
= "Show details" };
332 local get_online_users_result_layout
= dataforms_new
{
333 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
334 { name
= "onlineuserjids", type = "text-multi", label
= "The list of all online users" };
337 local get_online_users_command_handler
= adhoc_simple(get_online_users_layout
, function(fields
, err
)
339 return generate_error_message(err
);
342 local max_items
= nil
343 if fields
.max_items
~= "all" then
344 max_items
= tonumber(fields
.max_items
);
348 for username
, user
in pairs(hosts
[module_host
].sessions
or {}) do
349 if (max_items
~= nil) and (count
>= max_items
) then
352 users
[#users
+1] = username
.."@"..module_host
;
354 if fields
.details
then
355 for resource
, session
in pairs(user
.sessions
or {}) do
356 local status
, priority
, ip
= "unavailable", tostring(session
.priority
or "-"), session
.ip
or "<unknown>";
357 if session
.presence
then
358 status
= session
.presence
:child_with_name("show");
360 status
= status
:get_text() or "[invalid!]";
362 status
= "available";
365 users
[#users
+1] = " - "..resource
..": "..status
.."("..priority
.."), IP: ["..ip
.."]";
369 return { status
= "completed", result
= {layout
= get_online_users_result_layout
, values
= {onlineuserjids
=t_concat(users
, "\n")}} };
372 -- Getting a list of S2S connections (this host)
373 local list_s2s_this_result
= dataforms_new
{
374 title
= "List of S2S connections on this host";
376 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/s2s#list" };
377 { name
= "sessions", type = "text-multi", label
= "Connections:" };
378 { name
= "num_in", type = "text-single", label
= "#incoming connections:" };
379 { name
= "num_out", type = "text-single", label
= "#outgoing connections:" };
382 local function session_flags(session
, line
)
386 line
[#line
+1] = "["..session
.id
.."]"
388 line
[#line
+1] = "["..session
.type..(tostring(session
):match("%x*$")).."]"
392 if session
.cert_identity_status
== "valid" then
393 flags
[#flags
+1] = "authenticated";
395 if session
.secure
then
396 flags
[#flags
+1] = "encrypted";
398 if session
.compressed
then
399 flags
[#flags
+1] = "compressed";
401 if session
.smacks
then
402 flags
[#flags
+1] = "sm";
404 if session
.ip
and session
.ip
:match(":") then
405 flags
[#flags
+1] = "IPv6";
407 line
[#line
+1] = "("..t_concat(flags
, ", ")..")";
409 return t_concat(line
, " ");
412 local function list_s2s_this_handler(self
, data
, state
)
413 local count_in
, count_out
= 0, 0;
416 local s2s_sessions
= module
:shared
"/*/s2s/sessions";
417 for _
, session
in pairs(s2s_sessions
) do
418 local remotehost
, localhost
, direction
;
419 if session
.direction
== "outgoing" then
421 count_out
= count_out
+ 1;
422 remotehost
, localhost
= session
.to_host
or "?", session
.from_host
or "?";
425 count_in
= count_in
+ 1;
426 remotehost
, localhost
= session
.from_host
or "?", session
.to_host
or "?";
428 local sess_lines
= { r
= remotehost
,
429 session_flags(session
, { "", direction
, remotehost
or "?" })};
431 if localhost
== module_host
then
432 s2s_list
[#s2s_list
+1] = sess_lines
;
436 t_sort(s2s_list
, function(a
, b
)
440 for i
, sess_lines
in ipairs(s2s_list
) do
441 s2s_list
[i
] = sess_lines
[1];
444 return { status
= "completed", result
= { layout
= list_s2s_this_result
; values
= {
445 sessions
= t_concat(s2s_list
, "\n"),
446 num_in
= tostring(count_in
),
447 num_out
= tostring(count_out
)
451 -- Getting a list of loaded modules
452 local list_modules_result
= dataforms_new
{
453 title
= "List of loaded modules";
455 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/modules#list" };
456 { name
= "modules", type = "text-multi", label
= "The following modules are loaded:" };
459 local function list_modules_handler(self
, data
, state
)
460 local modules
= array
.collect(keys(hosts
[module_host
].modules
)):sort():concat("\n");
461 return { status
= "completed", result
= { layout
= list_modules_result
; values
= { modules
= modules
} } };
465 local load_module_layout
= dataforms_new
{
466 title
= "Load module";
467 instructions
= "Specify the module to be loaded";
469 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/modules#load" };
470 { name
= "module", type = "text-single", required
= true, label
= "Module to be loaded:"};
473 local load_module_handler
= adhoc_simple(load_module_layout
, function(fields
, err
)
475 return generate_error_message(err
);
477 if modulemanager
.is_loaded(module_host
, fields
.module
) then
478 return { status
= "completed", info
= "Module already loaded" };
480 local ok
, err
= modulemanager
.load(module_host
, fields
.module
);
482 return { status
= "completed", info
= 'Module "'..fields
.module
..'" successfully loaded on host "'..module_host
..'".' };
484 return { status
= "completed", error = { message
= 'Failed to load module "'..fields
.module
..'" on host "'..module_host
..
485 '". Error was: "'..tostring(err
or "<unspecified>")..'"' } };
489 -- Globally loading a module
490 local globally_load_module_layout
= dataforms_new
{
491 title
= "Globally load module";
492 instructions
= "Specify the module to be loaded on all hosts";
494 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/modules#global-load" };
495 { name
= "module", type = "text-single", required
= true, label
= "Module to globally load:"};
498 local globally_load_module_handler
= adhoc_simple(globally_load_module_layout
, function(fields
, err
)
499 local ok_list
, err_list
= {}, {};
502 return generate_error_message(err
);
505 local ok
, err
= modulemanager
.load(module_host
, fields
.module
);
507 ok_list
[#ok_list
+ 1] = module_host
;
509 err_list
[#err_list
+ 1] = module_host
.. " (Error: " .. tostring(err
) .. ")";
512 -- Is this a global module?
513 if modulemanager
.is_loaded("*", fields
.module
) and not modulemanager
.is_loaded(module_host
, fields
.module
) then
514 return { status
= "completed", info
= 'Global module '..fields
.module
..' loaded.' };
517 -- This is either a shared or "normal" module, load it on all other hosts
518 for host_name
, host
in pairs(hosts
) do
519 if host_name
~= module_host
and host
.type == "local" then
520 local ok
, err
= modulemanager
.load(host_name
, fields
.module
);
522 ok_list
[#ok_list
+ 1] = host_name
;
524 err_list
[#err_list
+ 1] = host_name
.. " (Error: " .. tostring(err
) .. ")";
529 local info
= (#ok_list
> 0 and ("The module "..fields
.module
.." was successfully loaded onto the hosts:\n"..t_concat(ok_list
, "\n")) or "")
530 .. ((#ok_list
> 0 and #err_list
> 0) and "\n" or "") ..
531 (#err_list
> 0 and ("Failed to load the module "..fields
.module
.." onto the hosts:\n"..t_concat(err_list
, "\n")) or "");
532 return { status
= "completed", info
= info
};
536 local reload_modules_layout
= dataforms_new
{
537 title
= "Reload modules";
538 instructions
= "Select the modules to be reloaded";
540 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/modules#reload" };
541 { name
= "modules", type = "list-multi", required
= true, label
= "Modules to be reloaded:"};
544 local reload_modules_handler
= adhoc_initial(reload_modules_layout
, function()
545 return { modules
= array
.collect(keys(hosts
[module_host
].modules
)):sort() };
546 end, function(fields
, err
)
548 return generate_error_message(err
);
550 local ok_list
, err_list
= {}, {};
551 for _
, module
in ipairs(fields
.modules
) do
552 local ok
, err
= modulemanager
.reload(module_host
, module
);
554 ok_list
[#ok_list
+ 1] = module
;
556 err_list
[#err_list
+ 1] = module
.. "(Error: " .. tostring(err
) .. ")";
559 local info
= (#ok_list
> 0 and ("The following modules were successfully reloaded on host "..module_host
..":\n"..t_concat(ok_list
, "\n")) or "")
560 .. ((#ok_list
> 0 and #err_list
> 0) and "\n" or "") ..
561 (#err_list
> 0 and ("Failed to reload the following modules on host "..module_host
..":\n"..t_concat(err_list
, "\n")) or "");
562 return { status
= "completed", info
= info
};
565 -- Globally reloading a module
566 local globally_reload_module_layout
= dataforms_new
{
567 title
= "Globally reload module";
568 instructions
= "Specify the module to reload on all hosts";
570 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/modules#global-reload" };
571 { name
= "module", type = "list-single", required
= true, label
= "Module to globally reload:"};
574 local globally_reload_module_handler
= adhoc_initial(globally_reload_module_layout
, function()
575 local loaded_modules
= array(keys(modulemanager
.get_modules("*")));
576 for _
, host
in pairs(hosts
) do
577 loaded_modules
:append(array(keys(host
.modules
)));
579 loaded_modules
= array(set
.new(loaded_modules
):items()):sort();
580 return { module
= loaded_modules
};
581 end, function(fields
, err
)
582 local is_global
= false;
585 return generate_error_message(err
);
588 if modulemanager
.is_loaded("*", fields
.module
) then
589 local ok
, err
= modulemanager
.reload("*", fields
.module
);
591 return { status
= "completed", info
= 'Global module '..fields
.module
..' failed to reload: '..err
};
596 local ok_list
, err_list
= {}, {};
597 for host_name
in pairs(hosts
) do
598 if modulemanager
.is_loaded(host_name
, fields
.module
) then
599 local ok
, err
= modulemanager
.reload(host_name
, fields
.module
);
601 ok_list
[#ok_list
+ 1] = host_name
;
603 err_list
[#err_list
+ 1] = host_name
.. " (Error: " .. tostring(err
) .. ")";
608 if #ok_list
== 0 and #err_list
== 0 then
610 return { status
= "completed", info
= 'Successfully reloaded global module '..fields
.module
};
612 return { status
= "completed", info
= 'Module '..fields
.module
..' not loaded on any host.' };
616 local info
= (#ok_list
> 0 and ("The module "..fields
.module
.." was successfully reloaded on the hosts:\n"..t_concat(ok_list
, "\n")) or "")
617 .. ((#ok_list
> 0 and #err_list
> 0) and "\n" or "") ..
618 (#err_list
> 0 and ("Failed to reload the module "..fields
.module
.." on the hosts:\n"..t_concat(err_list
, "\n")) or "");
619 return { status
= "completed", info
= info
};
622 local function send_to_online(message
, server
)
625 sessions
= { [server
] = hosts
[server
] };
631 for domain
, session
in pairs(sessions
) do
632 for user
in pairs(session
.sessions
or {}) do
634 message
.attr
.from
= domain
;
635 message
.attr
.to
= user
.."@"..domain
;
636 core_post_stanza(session
, message
);
643 -- Shutting down the service
644 local shut_down_service_layout
= dataforms_new
{
645 title
= "Shutting Down the Service";
646 instructions
= "Fill out this form to shut down the service.";
648 { name
= "FORM_TYPE", type = "hidden", value
= "http://jabber.org/protocol/admin" };
649 { name
= "delay", type = "list-single", label
= "Time delay before shutting down",
652 {label
= "5 seconds", value
= "5"},
653 {label
= "30 seconds", value
= "30"},
654 {label
= "60 seconds", value
= "60"},
655 {label
= "90 seconds", value
= "90"},
656 {label
= "2 minutes", value
= "120"},
657 {label
= "3 minutes", value
= "180"},
658 {label
= "4 minutes", value
= "240"},
659 {label
= "5 minutes", value
= "300"},
662 { name
= "announcement", type = "text-multi", label
= "Announcement" };
665 local shut_down_service_handler
= adhoc_simple(shut_down_service_layout
, function(fields
, err
)
667 return generate_error_message(err
);
670 if fields
.announcement
and #fields
.announcement
> 0 then
671 local message
= st
.message({type = "headline"}, fields
.announcement
):up()
672 :tag("subject"):text("Server is shutting down");
673 send_to_online(message
);
676 timer_add_task(tonumber(fields
.delay
or "5"), function() prosody
.shutdown("Shutdown by adhoc command") end);
678 return { status
= "completed", info
= "Server is about to shut down" };
682 local unload_modules_layout
= dataforms_new
{
683 title
= "Unload modules";
684 instructions
= "Select the modules to be unloaded";
686 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/modules#unload" };
687 { name
= "modules", type = "list-multi", required
= true, label
= "Modules to be unloaded:"};
690 local unload_modules_handler
= adhoc_initial(unload_modules_layout
, function()
691 return { modules
= array
.collect(keys(hosts
[module_host
].modules
)):sort() };
692 end, function(fields
, err
)
694 return generate_error_message(err
);
696 local ok_list
, err_list
= {}, {};
697 for _
, module
in ipairs(fields
.modules
) do
698 local ok
, err
= modulemanager
.unload(module_host
, module
);
700 ok_list
[#ok_list
+ 1] = module
;
702 err_list
[#err_list
+ 1] = module
.. "(Error: " .. tostring(err
) .. ")";
705 local info
= (#ok_list
> 0 and ("The following modules were successfully unloaded on host "..module_host
..":\n"..t_concat(ok_list
, "\n")) or "")
706 .. ((#ok_list
> 0 and #err_list
> 0) and "\n" or "") ..
707 (#err_list
> 0 and ("Failed to unload the following modules on host "..module_host
..":\n"..t_concat(err_list
, "\n")) or "");
708 return { status
= "completed", info
= info
};
711 -- Globally unloading a module
712 local globally_unload_module_layout
= dataforms_new
{
713 title
= "Globally unload module";
714 instructions
= "Specify a module to unload on all hosts";
716 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/modules#global-unload" };
717 { name
= "module", type = "list-single", required
= true, label
= "Module to globally unload:"};
720 local globally_unload_module_handler
= adhoc_initial(globally_unload_module_layout
, function()
721 local loaded_modules
= array(keys(modulemanager
.get_modules("*")));
722 for _
, host
in pairs(hosts
) do
723 loaded_modules
:append(array(keys(host
.modules
)));
725 loaded_modules
= array(set
.new(loaded_modules
):items()):sort();
726 return { module
= loaded_modules
};
727 end, function(fields
, err
)
728 local is_global
= false;
730 return generate_error_message(err
);
733 if modulemanager
.is_loaded("*", fields
.module
) then
734 local ok
, err
= modulemanager
.unload("*", fields
.module
);
736 return { status
= "completed", info
= 'Global module '..fields
.module
..' failed to unload: '..err
};
741 local ok_list
, err_list
= {}, {};
742 for host_name
in pairs(hosts
) do
743 if modulemanager
.is_loaded(host_name
, fields
.module
) then
744 local ok
, err
= modulemanager
.unload(host_name
, fields
.module
);
746 ok_list
[#ok_list
+ 1] = host_name
;
748 err_list
[#err_list
+ 1] = host_name
.. " (Error: " .. tostring(err
) .. ")";
753 if #ok_list
== 0 and #err_list
== 0 then
755 return { status
= "completed", info
= 'Successfully unloaded global module '..fields
.module
};
757 return { status
= "completed", info
= 'Module '..fields
.module
..' not loaded on any host.' };
761 local info
= (#ok_list
> 0 and ("The module "..fields
.module
.." was successfully unloaded on the hosts:\n"..t_concat(ok_list
, "\n")) or "")
762 .. ((#ok_list
> 0 and #err_list
> 0) and "\n" or "") ..
763 (#err_list
> 0 and ("Failed to unload the module "..fields
.module
.." on the hosts:\n"..t_concat(err_list
, "\n")) or "");
764 return { status
= "completed", info
= info
};
768 local activate_host_layout
= dataforms_new
{
769 title
= "Activate host";
772 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/hosts#activate" };
773 { name
= "host", type = "text-single", required
= true, label
= "Host:"};
776 local activate_host_handler
= adhoc_simple(activate_host_layout
, function(fields
, err
)
778 return generate_error_message(err
);
780 local ok
, err
= hostmanager_activate(fields
.host
);
783 return { status
= "completed", info
= fields
.host
.. " activated" };
785 return { status
= "canceled", error = err
}
789 -- Deactivating a host
790 local deactivate_host_layout
= dataforms_new
{
791 title
= "Deactivate host";
794 { name
= "FORM_TYPE", type = "hidden", value
= "http://prosody.im/protocol/hosts#activate" };
795 { name
= "host", type = "text-single", required
= true, label
= "Host:"};
798 local deactivate_host_handler
= adhoc_simple(deactivate_host_layout
, function(fields
, err
)
800 return generate_error_message(err
);
802 local ok
, err
= hostmanager_deactivate(fields
.host
);
805 return { status
= "completed", info
= fields
.host
.. " deactivated" };
807 return { status
= "canceled", error = err
}
811 -- luacheck: max_line_length 180
813 local add_user_desc
= adhoc_new("Add User", "http://jabber.org/protocol/admin#add-user", add_user_command_handler
, "admin");
814 local change_user_password_desc
= adhoc_new("Change User Password", "http://jabber.org/protocol/admin#change-user-password", change_user_password_command_handler
, "admin");
815 local config_reload_desc
= adhoc_new("Reload configuration", "http://prosody.im/protocol/config#reload", config_reload_handler
, "global_admin");
816 local delete_user_desc
= adhoc_new("Delete User", "http://jabber.org/protocol/admin#delete-user", delete_user_command_handler
, "admin");
817 local end_user_session_desc
= adhoc_new("End User Session", "http://jabber.org/protocol/admin#end-user-session", end_user_session_handler
, "admin");
818 local get_user_password_desc
= adhoc_new("Get User Password", "http://jabber.org/protocol/admin#get-user-password", get_user_password_handler
, "admin");
819 local get_user_roster_desc
= adhoc_new("Get User Roster","http://jabber.org/protocol/admin#get-user-roster", get_user_roster_handler
, "admin");
820 local get_user_stats_desc
= adhoc_new("Get User Statistics","http://jabber.org/protocol/admin#user-stats", get_user_stats_handler
, "admin");
821 local get_online_users_desc
= adhoc_new("Get List of Online Users", "http://jabber.org/protocol/admin#get-online-users-list", get_online_users_command_handler
, "admin");
822 local list_s2s_this_desc
= adhoc_new("List S2S connections", "http://prosody.im/protocol/s2s#list", list_s2s_this_handler
, "admin");
823 local list_modules_desc
= adhoc_new("List loaded modules", "http://prosody.im/protocol/modules#list", list_modules_handler
, "admin");
824 local load_module_desc
= adhoc_new("Load module", "http://prosody.im/protocol/modules#load", load_module_handler
, "admin");
825 local globally_load_module_desc
= adhoc_new("Globally load module", "http://prosody.im/protocol/modules#global-load", globally_load_module_handler
, "global_admin");
826 local reload_modules_desc
= adhoc_new("Reload modules", "http://prosody.im/protocol/modules#reload", reload_modules_handler
, "admin");
827 local globally_reload_module_desc
= adhoc_new("Globally reload module", "http://prosody.im/protocol/modules#global-reload", globally_reload_module_handler
, "global_admin");
828 local shut_down_service_desc
= adhoc_new("Shut Down Service", "http://jabber.org/protocol/admin#shutdown", shut_down_service_handler
, "global_admin");
829 local unload_modules_desc
= adhoc_new("Unload modules", "http://prosody.im/protocol/modules#unload", unload_modules_handler
, "admin");
830 local globally_unload_module_desc
= adhoc_new("Globally unload module", "http://prosody.im/protocol/modules#global-unload", globally_unload_module_handler
, "global_admin");
831 local activate_host_desc
= adhoc_new("Activate host", "http://prosody.im/protocol/hosts#activate", activate_host_handler
, "global_admin");
832 local deactivate_host_desc
= adhoc_new("Deactivate host", "http://prosody.im/protocol/hosts#deactivate", deactivate_host_handler
, "global_admin");
834 module
:provides("adhoc", add_user_desc
);
835 module
:provides("adhoc", change_user_password_desc
);
836 module
:provides("adhoc", config_reload_desc
);
837 module
:provides("adhoc", delete_user_desc
);
838 module
:provides("adhoc", end_user_session_desc
);
839 module
:provides("adhoc", get_user_password_desc
);
840 module
:provides("adhoc", get_user_roster_desc
);
841 module
:provides("adhoc", get_user_stats_desc
);
842 module
:provides("adhoc", get_online_users_desc
);
843 module
:provides("adhoc", list_s2s_this_desc
);
844 module
:provides("adhoc", list_modules_desc
);
845 module
:provides("adhoc", load_module_desc
);
846 module
:provides("adhoc", globally_load_module_desc
);
847 module
:provides("adhoc", reload_modules_desc
);
848 module
:provides("adhoc", globally_reload_module_desc
);
849 module
:provides("adhoc", shut_down_service_desc
);
850 module
:provides("adhoc", unload_modules_desc
);
851 module
:provides("adhoc", globally_unload_module_desc
);
852 module
:provides("adhoc", activate_host_desc
);
853 module
:provides("adhoc", deactivate_host_desc
);