From 5e446d50a8f77b932449cd96a58f6cfa1af647a8 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Wed, 30 May 2018 19:21:56 +0300 Subject: [PATCH] Get rid of direct calls to 'session' Mnesia table Fixes #2439 --- src/ejabberd_sm.erl | 22 ++++++++++++++++------ src/mod_configure.erl | 32 +++++--------------------------- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index e35f31079..67fa8ca00 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -62,6 +62,7 @@ connected_users_number/0, user_resources/2, kick_user/2, + kick_user/3, get_session_pid/3, get_session_sid/3, get_session_sids/2, @@ -978,14 +979,23 @@ user_resources(User, Server) -> Resources = get_user_resources(User, Server), lists:sort(Resources). +-spec kick_user(binary(), binary()) -> non_neg_integer(). kick_user(User, Server) -> Resources = get_user_resources(User, Server), - lists:foreach( - fun(Resource) -> - PID = get_session_pid(User, Server, Resource), - ejabberd_c2s:route(PID, kick) - end, Resources), - length(Resources). + lists:foldl( + fun(Resource, Acc) -> + case kick_user(User, Server, Resource) of + false -> Acc; + true -> Acc + 1 + end + end, 0, Resources). + +-spec kick_user(binary(), binary(), binary()) -> boolean(). +kick_user(User, Server, Resource) -> + case get_session_pid(User, Server, Resource) of + none -> false; + Pid -> ejabberd_c2s:route(Pid, kick) + end. make_sid() -> {p1_time_compat:unique_timestamp(), self()}. diff --git a/src/mod_configure.erl b/src/mod_configure.erl index 471e2bcdc..74b3cc793 100644 --- a/src/mod_configure.erl +++ b/src/mod_configure.erl @@ -1552,39 +1552,17 @@ set_form(From, Host, ?NS_ADMINL(<<"delete-user">>), || {User, Server} <- ASL2], {result, undefined}; set_form(From, Host, ?NS_ADMINL(<<"end-user-session">>), - Lang, XData) -> + _Lang, XData) -> AccountString = get_value(<<"accountjid">>, XData), JID = jid:decode(AccountString), - LUser = JID#jid.luser, LServer = JID#jid.lserver, true = LServer == Host orelse get_permission_level(From) == global, - Xmlelement = xmpp:serr_policy_violation(<<"has been kicked">>, Lang), case JID#jid.lresource of - <<>> -> - SIs = mnesia:dirty_select(session, - [{#session{usr = {LUser, LServer, '_'}, - sid = '$1', - info = '$2', - _ = '_'}, - [], [{{'$1', '$2'}}]}]), - Pids = [P || {{_, P}, Info} <- SIs, - not proplists:get_bool(offline, Info)], - lists:foreach(fun(Pid) -> - Pid ! {kick, kicked_by_admin, Xmlelement} - end, Pids); - R -> - [{{_, Pid}, Info}] = mnesia:dirty_select( - session, - [{#session{usr = {LUser, LServer, R}, - sid = '$1', - info = '$2', - _ = '_'}, - [], [{{'$1', '$2'}}]}]), - case proplists:get_bool(offline, Info) of - true -> ok; - false -> Pid ! {kick, kicked_by_admin, Xmlelement} - end + <<>> -> + ejabberd_sm:kick_user(JID#jid.luser, JID#jid.lserver); + R -> + ejabberd_sm:kick_user(JID#jid.luser, JID#jid.lserver, R) end, {result, undefined}; set_form(From, Host, -- 2.40.0