Skip to content

Commit

Permalink
New kickall_kindly command: kick users, prevent login, but keep serve…
Browse files Browse the repository at this point in the history
…r running (4309)
  • Loading branch information
badlop committed Nov 13, 2024
1 parent 98f1869 commit 2022fb1
Showing 1 changed file with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions src/ejabberd_admin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
reopen_log/0, rotate_log/0,
set_loglevel/1,
stop_kindly/2, send_service_message_all_mucs/2,
kickall_kindly/2,
registered_vhosts/0,
reload_config/0,
dump_config/1,
Expand Down Expand Up @@ -167,8 +168,9 @@ get_commands_spec() ->
module = ?MODULE, function = rotate_log,
args = [], result = {res, rescode}},
#ejabberd_commands{name = stop_kindly, tags = [server],
desc = "Inform users and rooms, wait, and stop the server",
longdesc = "Provide the delay in seconds, and the "
desc = "Stop kindly the server (informing users)",
longdesc = "Inform users and rooms, wait, and stop the server.\n"
"Provide the delay in seconds, and the "
"announcement quoted, for example: \n"
"`ejabberdctl stop_kindly 60 "
"\\\"The server will stop in one minute.\\\"`",
Expand All @@ -177,6 +179,21 @@ get_commands_spec() ->
args_example = [60, <<"Server will stop now.">>],
args = [{delay, integer}, {announcement, string}],
result = {res, rescode}},
#ejabberd_commands{name = kickall_kindly, tags = [server],
desc = "Kick kindly all users and prevent login (informing them)",
longdesc = "Inform users and rooms, wait, kick them, "
"don't allow them to login/join again,"
"but maintain the server running.\n"
"Provide the delay in seconds, and the "
"announcement quoted, for example: \n"
"`ejabberdctl kickall_kindly 60 "
"\\\"The server will stop in one minute.\\\"`",
note = "added in 24.xx",
module = ?MODULE, function = kickall_kindly,
args_desc = ["Seconds to wait", "Announcement to send, with quotes"],
args_example = [60, <<"Server will stop now.">>],
args = [{delay, integer}, {announcement, string}],
result = {res, rescode}},
#ejabberd_commands{name = get_loglevel, tags = [logs],
desc = "Get the current loglevel",
module = ejabberd_logger, function = get,
Expand Down Expand Up @@ -672,39 +689,49 @@ set_loglevel(LogLevel) ->
%%% Stop Kindly
%%%

kickall_kindly(DelaySeconds, AnnouncementTextString) ->
perform_kindly(DelaySeconds, AnnouncementTextString, kickall).

stop_kindly(DelaySeconds, AnnouncementTextString) ->
Subject = (str:format("Server stop in ~p seconds!", [DelaySeconds])),
WaitingDesc = (str:format("Waiting ~p seconds", [DelaySeconds])),
perform_kindly(DelaySeconds, AnnouncementTextString, stop).

perform_kindly(DelaySeconds, AnnouncementTextString, Action) ->
Subject = str:format("Server stop in ~p seconds!", [DelaySeconds]),
WaitingDesc = str:format("Waiting ~p seconds", [DelaySeconds]),
AnnouncementText = list_to_binary(AnnouncementTextString),
Steps = [
{"Stopping ejabberd port listeners",
ejabberd_listener, stop_listeners, []},
{"Sending announcement to connected users",
mod_announce, send_announcement_to_all,
[ejabberd_config:get_myname(), Subject, AnnouncementText]},
{"Sending service message to MUC rooms",
ejabberd_admin, send_service_message_all_mucs,
[Subject, AnnouncementText]},
{WaitingDesc, timer, sleep, [DelaySeconds * 1000]},
{"Stopping ejabberd", application, stop, [ejabberd]},
{"Stopping Mnesia", mnesia, stop, []},
{"Stopping Erlang node", init, stop, []}
],
PreSteps =
[{"Stopping ejabberd port listeners", ejabberd_listener, stop_listeners, []},
{"Sending announcement to connected users",
mod_announce,
send_announcement_to_all,
[ejabberd_config:get_myname(), Subject, AnnouncementText]},
{"Sending service message to MUC rooms",
ejabberd_admin,
send_service_message_all_mucs,
[Subject, AnnouncementText]},
{WaitingDesc, timer, sleep, [DelaySeconds * 1000]},
{"Stopping ejabberd", application, stop, [ejabberd]}],
SpecificSteps =
case Action of
kickall ->
[{"Starting ejabberd", application, start, [ejabberd]},
{"Stopping ejabberd port listeners", ejabberd_listener, stop_listeners, []}];
stop ->
[{"Stopping Mnesia", mnesia, stop, []}, {"Stopping Erlang node", init, stop, []}]
end,
Steps = PreSteps ++ SpecificSteps,
NumberLast = length(Steps),
TimestampStart = calendar:datetime_to_gregorian_seconds({date(), time()}),
lists:foldl(
fun({Desc, Mod, Func, Args}, NumberThis) ->
SecondsDiff =
calendar:datetime_to_gregorian_seconds({date(), time()})
- TimestampStart,
io:format("[~p/~p ~ps] ~ts... ",
[NumberThis, NumberLast, SecondsDiff, Desc]),
Result = (catch apply(Mod, Func, Args)),
io:format("~p~n", [Result]),
NumberThis+1
end,
1,
Steps),
lists:foldl(fun({Desc, Mod, Func, Args}, NumberThis) ->
SecondsDiff =
calendar:datetime_to_gregorian_seconds({date(), time()}) - TimestampStart,
io:format("[~p/~p ~ps] ~ts... ", [NumberThis, NumberLast, SecondsDiff, Desc]),
Result = (catch apply(Mod, Func, Args)),
io:format("~p~n", [Result]),
NumberThis + 1
end,
1,
Steps),
ok.

send_service_message_all_mucs(Subject, AnnouncementText) ->
Expand Down

0 comments on commit 2022fb1

Please sign in to comment.