2002-11-24 21:36:57 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_sm.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-12-14 21:51:01 +01:00
|
|
|
%%% Purpose : Session manager
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 24 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2021-01-27 16:55:25 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2021 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2002-11-24 21:36:57 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_sm).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2017-02-14 13:39:57 +01:00
|
|
|
-ifndef(GEN_SERVER).
|
|
|
|
-define(GEN_SERVER, gen_server).
|
|
|
|
-endif.
|
|
|
|
-behaviour(?GEN_SERVER).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
%% API
|
2017-02-24 10:05:47 +01:00
|
|
|
-export([start_link/0,
|
2017-02-24 10:39:44 +01:00
|
|
|
stop/0,
|
2017-02-16 09:00:26 +01:00
|
|
|
route/1,
|
2016-12-11 13:03:37 +01:00
|
|
|
route/2,
|
2014-03-19 00:51:33 +01:00
|
|
|
open_session/5,
|
|
|
|
open_session/6,
|
|
|
|
close_session/4,
|
2018-01-26 13:02:06 +01:00
|
|
|
check_in_subscription/2,
|
2017-02-23 16:55:35 +01:00
|
|
|
bounce_offline_message/1,
|
2018-12-05 11:14:29 +01:00
|
|
|
bounce_sm_packet/1,
|
2005-04-17 20:08:34 +02:00
|
|
|
disconnect_removed_user/2,
|
|
|
|
get_user_resources/2,
|
2014-04-07 22:10:08 +02:00
|
|
|
get_user_present_resources/2,
|
2018-12-10 09:53:27 +01:00
|
|
|
set_presence/6,
|
|
|
|
unset_presence/5,
|
2006-01-24 00:13:06 +01:00
|
|
|
close_session_unset_presence/5,
|
2003-01-01 20:54:44 +01:00
|
|
|
dirty_get_sessions_list/0,
|
2003-01-18 20:42:48 +01:00
|
|
|
dirty_get_my_sessions_list/0,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_session_list/1,
|
2009-07-17 21:05:55 +02:00
|
|
|
get_vh_session_number/1,
|
2016-02-19 14:15:11 +01:00
|
|
|
get_vh_by_backend/1,
|
2010-04-27 12:33:56 +02:00
|
|
|
force_update_presence/1,
|
2008-10-12 14:04:10 +02:00
|
|
|
connected_users/0,
|
|
|
|
connected_users_number/0,
|
|
|
|
user_resources/2,
|
2014-05-12 12:44:40 +02:00
|
|
|
kick_user/2,
|
2018-05-30 18:21:56 +02:00
|
|
|
kick_user/3,
|
2007-12-01 06:16:30 +01:00
|
|
|
get_session_pid/3,
|
2017-07-20 20:22:50 +02:00
|
|
|
get_session_sid/3,
|
|
|
|
get_session_sids/2,
|
2019-06-26 10:56:25 +02:00
|
|
|
get_session_sids/3,
|
2017-01-13 10:03:39 +01:00
|
|
|
get_user_info/2,
|
2008-02-15 17:35:32 +01:00
|
|
|
get_user_info/3,
|
2018-12-01 11:33:44 +01:00
|
|
|
set_user_info/5,
|
|
|
|
del_user_info/4,
|
2011-08-17 19:42:11 +02:00
|
|
|
get_user_ip/3,
|
2014-05-01 18:58:14 +02:00
|
|
|
get_max_user_sessions/2,
|
2014-05-08 19:28:12 +02:00
|
|
|
get_all_pids/0,
|
2016-01-26 10:00:11 +01:00
|
|
|
is_existing_resource/3,
|
2016-04-08 10:46:21 +02:00
|
|
|
get_commands_spec/0,
|
2016-12-11 13:03:37 +01:00
|
|
|
c2s_handle_info/2,
|
2017-02-23 08:12:19 +01:00
|
|
|
host_up/1,
|
|
|
|
host_down/1,
|
2017-04-14 12:57:52 +02:00
|
|
|
make_sid/0,
|
|
|
|
clean_cache/1,
|
2018-11-30 14:18:49 +01:00
|
|
|
config_reloaded/0
|
2003-01-29 18:12:23 +01:00
|
|
|
]).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2008-10-12 14:04:10 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
2015-03-09 12:57:33 +01:00
|
|
|
-include("ejabberd_sm.hrl").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2015-03-09 12:57:33 +01:00
|
|
|
|
|
|
|
-callback init() -> ok | {error, any()}.
|
2017-04-14 12:57:52 +02:00
|
|
|
-callback set_session(#session{}) -> ok | {error, any()}.
|
|
|
|
-callback delete_session(#session{}) -> ok | {error, any()}.
|
2015-03-09 12:57:33 +01:00
|
|
|
-callback get_sessions() -> [#session{}].
|
|
|
|
-callback get_sessions(binary()) -> [#session{}].
|
2017-04-14 12:57:52 +02:00
|
|
|
-callback get_sessions(binary(), binary()) -> {ok, [#session{}]} | {error, any()}.
|
|
|
|
-callback use_cache(binary()) -> boolean().
|
|
|
|
-callback cache_nodes(binary()) -> [node()].
|
|
|
|
|
|
|
|
-optional_callbacks([use_cache/1, cache_nodes/1]).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {}).
|
|
|
|
|
2006-05-21 18:34:32 +02:00
|
|
|
%% default value for the maximum number of user connections
|
2006-05-22 12:17:22 +02:00
|
|
|
-define(MAX_USER_SESSIONS, infinity).
|
2006-05-21 18:34:32 +02:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2016-08-09 09:56:32 +02:00
|
|
|
-export_type([sid/0, info/0]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2017-02-14 13:39:57 +01:00
|
|
|
?GEN_SERVER:start_link({local, ?MODULE}, ?MODULE, [], []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2019-07-26 10:40:19 +02:00
|
|
|
-spec stop() -> ok.
|
2017-02-24 10:39:44 +01:00
|
|
|
stop() ->
|
2019-07-26 10:40:19 +02:00
|
|
|
_ = supervisor:terminate_child(ejabberd_sup, ?MODULE),
|
|
|
|
_ = supervisor:delete_child(ejabberd_sup, ?MODULE),
|
|
|
|
_ = supervisor:terminate_child(ejabberd_sup, ejabberd_c2s_sup),
|
|
|
|
_ = supervisor:delete_child(ejabberd_sup, ejabberd_c2s_sup),
|
|
|
|
ok.
|
2017-02-24 10:39:44 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec route(jid(), term()) -> ok.
|
|
|
|
%% @doc route arbitrary term to c2s process(es)
|
|
|
|
route(To, Term) ->
|
2019-07-07 21:12:14 +02:00
|
|
|
try do_route(To, Term), ok
|
|
|
|
catch ?EX_RULE(E, R, St) ->
|
|
|
|
StackTrace = ?EX_STACK(St),
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to route term to ~ts:~n"
|
2019-07-07 21:12:14 +02:00
|
|
|
"** Term = ~p~n"
|
2019-09-23 14:17:20 +02:00
|
|
|
"** ~ts",
|
2019-07-07 21:12:14 +02:00
|
|
|
[jid:encode(To), Term,
|
|
|
|
misc:format_exception(2, E, R, StackTrace)])
|
2016-12-11 13:03:37 +01:00
|
|
|
end.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec route(stanza()) -> ok.
|
|
|
|
route(Packet) ->
|
2017-11-09 00:21:40 +01:00
|
|
|
#jid{lserver = LServer} = xmpp:get_to(Packet),
|
|
|
|
case ejabberd_hooks:run_fold(sm_receive_packet, LServer, Packet, []) of
|
|
|
|
drop ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Hook dropped stanza:~n~ts", [xmpp:pp(Packet)]);
|
2017-11-09 00:21:40 +01:00
|
|
|
Packet1 ->
|
2019-07-07 21:12:14 +02:00
|
|
|
do_route(Packet1),
|
|
|
|
ok
|
2006-01-29 05:38:31 +01:00
|
|
|
end.
|
|
|
|
|
2014-03-19 00:51:33 +01:00
|
|
|
-spec open_session(sid(), binary(), binary(), binary(), prio(), info()) -> ok.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2014-03-19 00:51:33 +01:00
|
|
|
open_session(SID, User, Server, Resource, Priority, Info) ->
|
|
|
|
set_session(SID, User, Server, Resource, Priority, Info),
|
2006-05-21 18:34:32 +02:00
|
|
|
check_for_sessions_to_replace(User, Server, Resource),
|
2015-11-24 16:44:13 +01:00
|
|
|
JID = jid:make(User, Server, Resource),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:run(sm_register_connection_hook,
|
|
|
|
JID#jid.lserver, [SID, JID, Info]).
|
|
|
|
|
2014-03-19 00:51:33 +01:00
|
|
|
-spec open_session(sid(), binary(), binary(), binary(), info()) -> ok.
|
|
|
|
|
|
|
|
open_session(SID, User, Server, Resource, Info) ->
|
|
|
|
open_session(SID, User, Server, Resource, undefined, Info).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec close_session(sid(), binary(), binary(), binary()) -> ok.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2006-04-07 02:39:24 +02:00
|
|
|
close_session(SID, User, Server, Resource) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2017-04-14 19:41:25 +02:00
|
|
|
Sessions = get_sessions(Mod, LUser, LServer, LResource),
|
|
|
|
Info = case lists:keyfind(SID, #session.sid, Sessions) of
|
|
|
|
#session{info = I} = Session ->
|
2017-04-14 12:57:52 +02:00
|
|
|
delete_session(Mod, Session),
|
|
|
|
I;
|
2017-04-14 19:41:25 +02:00
|
|
|
_ ->
|
2017-04-14 12:57:52 +02:00
|
|
|
[]
|
2015-03-09 12:57:33 +01:00
|
|
|
end,
|
2015-11-24 16:44:13 +01:00
|
|
|
JID = jid:make(User, Server, Resource),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:run(sm_remove_connection_hook,
|
|
|
|
JID#jid.lserver, [SID, JID, Info]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
-spec check_in_subscription(boolean(), presence()) -> boolean() | {stop, false}.
|
|
|
|
check_in_subscription(Acc, #presence{to = To}) ->
|
|
|
|
#jid{user = User, server = Server} = To,
|
2017-05-11 14:49:06 +02:00
|
|
|
case ejabberd_auth:user_exists(User, Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> Acc;
|
|
|
|
false -> {stop, false}
|
2007-05-03 07:15:12 +02:00
|
|
|
end.
|
|
|
|
|
2017-02-23 16:55:35 +01:00
|
|
|
-spec bounce_offline_message({bounce, message()} | any()) -> any().
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2018-12-05 11:14:29 +01:00
|
|
|
bounce_offline_message({bounce, #message{type = T}} = Acc)
|
|
|
|
when T == chat; T == groupchat; T == normal ->
|
|
|
|
bounce_sm_packet(Acc);
|
|
|
|
bounce_offline_message(Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
-spec bounce_sm_packet({bounce | term(), stanza()}) -> any().
|
|
|
|
bounce_sm_packet({bounce, Packet} = Acc) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("User session not found"),
|
2016-11-25 09:41:24 +01:00
|
|
|
Err = xmpp:err_service_unavailable(Txt, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2017-01-21 11:47:47 +01:00
|
|
|
{stop, Acc};
|
2018-12-05 11:14:29 +01:00
|
|
|
bounce_sm_packet({_, Packet} = Acc) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Dropping packet to unavailable resource:~n~ts",
|
2018-12-05 11:14:29 +01:00
|
|
|
[xmpp:pp(Packet)]),
|
2017-01-21 11:47:47 +01:00
|
|
|
Acc.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec disconnect_removed_user(binary(), binary()) -> ok.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
disconnect_removed_user(User, Server) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
route(jid:make(User, Server), {exit, ?T("User removed")}).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
get_user_resources(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
Ss = get_sessions(Mod, LUser, LServer),
|
2015-03-09 12:57:33 +01:00
|
|
|
[element(3, S#session.usr) || S <- clean_session_list(Ss)].
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2014-04-07 22:10:08 +02:00
|
|
|
-spec get_user_present_resources(binary(), binary()) -> [tuple()].
|
|
|
|
|
|
|
|
get_user_present_resources(LUser, LServer) ->
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
Ss = get_sessions(Mod, LUser, LServer),
|
2015-03-09 12:57:33 +01:00
|
|
|
[{S#session.priority, element(3, S#session.usr)}
|
|
|
|
|| S <- clean_session_list(Ss), is_integer(S#session.priority)].
|
2014-04-07 22:10:08 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_user_ip(binary(), binary(), binary()) -> ip().
|
|
|
|
|
2007-05-21 05:21:34 +02:00
|
|
|
get_user_ip(User, Server, Resource) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
case get_sessions(Mod, LUser, LServer, LResource) of
|
2007-05-21 05:21:34 +02:00
|
|
|
[] ->
|
|
|
|
undefined;
|
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
proplists:get_value(ip, Session#session.info)
|
|
|
|
end.
|
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-spec get_user_info(binary(), binary()) -> [{binary(), info()}].
|
|
|
|
get_user_info(User, Server) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
Ss = get_sessions(Mod, LUser, LServer),
|
2018-06-15 11:28:44 +02:00
|
|
|
[{LResource, [{node, node(Pid)}, {ts, Ts}, {pid, Pid},
|
|
|
|
{priority, Priority} | Info]}
|
2017-01-13 10:03:39 +01:00
|
|
|
|| #session{usr = {_, _, LResource},
|
2018-06-15 11:28:44 +02:00
|
|
|
priority = Priority,
|
2017-01-13 10:03:39 +01:00
|
|
|
info = Info,
|
2018-06-15 11:28:44 +02:00
|
|
|
sid = {Ts, Pid}} <- clean_session_list(Ss)].
|
2017-01-13 10:03:39 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_user_info(binary(), binary(), binary()) -> info() | offline.
|
|
|
|
|
2008-02-15 17:35:32 +01:00
|
|
|
get_user_info(User, Server, Resource) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
case get_sessions(Mod, LUser, LServer, LResource) of
|
2008-02-15 17:35:32 +01:00
|
|
|
[] ->
|
|
|
|
offline;
|
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
2018-06-15 11:28:44 +02:00
|
|
|
{Ts, Pid} = Session#session.sid,
|
|
|
|
Node = node(Pid),
|
|
|
|
Priority = Session#session.priority,
|
|
|
|
[{node, Node}, {ts, Ts}, {pid, Pid}, {priority, Priority}
|
|
|
|
|Session#session.info]
|
2008-02-15 17:35:32 +01:00
|
|
|
end.
|
2007-05-21 05:21:34 +02:00
|
|
|
|
2018-12-01 11:33:44 +01:00
|
|
|
-spec set_user_info(binary(), binary(), binary(), atom(), term()) -> ok | {error, any()}.
|
|
|
|
set_user_info(User, Server, Resource, Key, Val) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
|
|
|
Mod = get_sm_backend(LServer),
|
|
|
|
case get_sessions(Mod, LUser, LServer, LResource) of
|
|
|
|
[] -> {error, notfound};
|
|
|
|
Ss ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(#session{sid = {_, Pid},
|
|
|
|
info = Info} = Session, _) when Pid == self() ->
|
|
|
|
Info1 = lists:keystore(Key, 1, Info, {Key, Val}),
|
|
|
|
set_session(Session#session{info = Info1});
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, {error, not_owner}, Ss)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec del_user_info(binary(), binary(), binary(), atom()) -> ok | {error, any()}.
|
|
|
|
del_user_info(User, Server, Resource, Key) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
|
|
|
Mod = get_sm_backend(LServer),
|
|
|
|
case get_sessions(Mod, LUser, LServer, LResource) of
|
|
|
|
[] -> {error, notfound};
|
|
|
|
Ss ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(#session{sid = {_, Pid},
|
|
|
|
info = Info} = Session, _) when Pid == self() ->
|
|
|
|
Info1 = lists:keydelete(Key, 1, Info),
|
|
|
|
set_session(Session#session{info = Info1});
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, {error, not_owner}, Ss)
|
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec set_presence(sid(), binary(), binary(), binary(),
|
2018-12-10 09:53:27 +01:00
|
|
|
prio(), presence()) -> ok | {error, notfound}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2018-12-10 09:53:27 +01:00
|
|
|
set_presence(SID, User, Server, Resource, Priority, Presence) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
|
|
|
Mod = get_sm_backend(LServer),
|
|
|
|
case get_sessions(Mod, LUser, LServer, LResource) of
|
|
|
|
[] -> {error, notfound};
|
|
|
|
Ss ->
|
2018-12-10 15:06:21 +01:00
|
|
|
case lists:keyfind(SID, #session.sid, Ss) of
|
2018-12-10 09:53:27 +01:00
|
|
|
#session{info = Info} ->
|
|
|
|
set_session(SID, User, Server, Resource, Priority, Info),
|
|
|
|
ejabberd_hooks:run(set_presence_hook,
|
|
|
|
LServer,
|
|
|
|
[User, Server, Resource, Presence]);
|
|
|
|
false ->
|
|
|
|
{error, notfound}
|
|
|
|
end
|
|
|
|
end.
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec unset_presence(sid(), binary(), binary(),
|
2018-12-10 09:53:27 +01:00
|
|
|
binary(), binary()) -> ok | {error, notfound}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2018-12-10 09:53:27 +01:00
|
|
|
unset_presence(SID, User, Server, Resource, Status) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
|
|
|
Mod = get_sm_backend(LServer),
|
|
|
|
case get_sessions(Mod, LUser, LServer, LResource) of
|
|
|
|
[] -> {error, notfound};
|
|
|
|
Ss ->
|
2018-12-10 15:06:21 +01:00
|
|
|
case lists:keyfind(SID, #session.sid, Ss) of
|
2018-12-10 09:53:27 +01:00
|
|
|
#session{info = Info} ->
|
|
|
|
set_session(SID, User, Server, Resource, undefined, Info),
|
|
|
|
ejabberd_hooks:run(unset_presence_hook,
|
|
|
|
LServer,
|
|
|
|
[User, Server, Resource, Status]);
|
|
|
|
false ->
|
|
|
|
{error, notfound}
|
|
|
|
end
|
|
|
|
end.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec close_session_unset_presence(sid(), binary(), binary(),
|
|
|
|
binary(), binary()) -> ok.
|
|
|
|
|
|
|
|
close_session_unset_presence(SID, User, Server,
|
|
|
|
Resource, Status) ->
|
2006-04-07 02:39:24 +02:00
|
|
|
close_session(SID, User, Server, Resource),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:run(unset_presence_hook,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:nameprep(Server),
|
2006-01-29 05:38:31 +01:00
|
|
|
[User, Server, Resource, Status]).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec get_session_pid(binary(), binary(), binary()) -> none | pid().
|
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
get_session_pid(User, Server, Resource) ->
|
2017-07-20 20:22:50 +02:00
|
|
|
case get_session_sid(User, Server, Resource) of
|
|
|
|
{_, PID} -> PID;
|
|
|
|
none -> none
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec get_session_sid(binary(), binary(), binary()) -> none | sid().
|
|
|
|
|
|
|
|
get_session_sid(User, Server, Resource) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
case get_sessions(Mod, LUser, LServer, LResource) of
|
2017-12-19 18:59:25 +01:00
|
|
|
[] ->
|
|
|
|
none;
|
|
|
|
Ss ->
|
|
|
|
#session{sid = SID} = lists:max(Ss),
|
|
|
|
SID
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2017-07-20 20:22:50 +02:00
|
|
|
-spec get_session_sids(binary(), binary()) -> [sid()].
|
|
|
|
|
|
|
|
get_session_sids(User, Server) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
Sessions = get_sessions(Mod, LUser, LServer),
|
2017-08-18 16:50:08 +02:00
|
|
|
[SID || #session{sid = SID} <- Sessions].
|
2017-07-20 20:22:50 +02:00
|
|
|
|
2019-06-26 10:56:25 +02:00
|
|
|
-spec get_session_sids(binary(), binary(), binary()) -> [sid()].
|
|
|
|
|
|
|
|
get_session_sids(User, Server, Resource) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
|
|
|
Mod = get_sm_backend(LServer),
|
|
|
|
Sessions = get_sessions(Mod, LUser, LServer, LResource),
|
|
|
|
[SID || #session{sid = SID} <- Sessions].
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec dirty_get_sessions_list() -> [ljid()].
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
dirty_get_sessions_list() ->
|
2016-02-19 14:15:11 +01:00
|
|
|
lists:flatmap(
|
|
|
|
fun(Mod) ->
|
2018-11-30 14:18:49 +01:00
|
|
|
[S#session.usr || S <- get_sessions(Mod)]
|
2016-02-19 14:15:11 +01:00
|
|
|
end, get_sm_backends()).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
-spec dirty_get_my_sessions_list() -> [#session{}].
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
dirty_get_my_sessions_list() ->
|
2016-02-19 14:15:11 +01:00
|
|
|
lists:flatmap(
|
|
|
|
fun(Mod) ->
|
2018-11-30 14:18:49 +01:00
|
|
|
[S || S <- get_sessions(Mod),
|
2016-02-19 14:15:11 +01:00
|
|
|
node(element(2, S#session.sid)) == node()]
|
|
|
|
end, get_sm_backends()).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2014-05-08 19:28:12 +02:00
|
|
|
-spec get_vh_session_list(binary()) -> [ljid()].
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
get_vh_session_list(Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
[S#session.usr || S <- get_sessions(Mod, LServer)].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2014-05-08 19:28:12 +02:00
|
|
|
-spec get_all_pids() -> [pid()].
|
|
|
|
|
|
|
|
get_all_pids() ->
|
2016-02-19 14:15:11 +01:00
|
|
|
lists:flatmap(
|
|
|
|
fun(Mod) ->
|
2018-11-30 14:18:49 +01:00
|
|
|
[element(2, S#session.sid) || S <- get_sessions(Mod)]
|
2016-02-19 14:15:11 +01:00
|
|
|
end, get_sm_backends()).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
-spec get_vh_session_number(binary()) -> non_neg_integer().
|
|
|
|
|
2009-07-17 21:05:55 +02:00
|
|
|
get_vh_session_number(Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
length(get_sessions(Mod, LServer)).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-12-28 07:47:11 +01:00
|
|
|
c2s_handle_info(#{lang := Lang} = State, replaced) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
State1 = State#{replaced => true},
|
2019-06-22 16:08:45 +02:00
|
|
|
Err = xmpp:serr_conflict(?T("Replaced by new connection"), Lang),
|
2016-12-28 07:47:11 +01:00
|
|
|
{stop, ejabberd_c2s:send(State1, Err)};
|
|
|
|
c2s_handle_info(#{lang := Lang} = State, kick) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Err = xmpp:serr_policy_violation(?T("has been kicked"), Lang),
|
2016-12-28 07:47:11 +01:00
|
|
|
{stop, ejabberd_c2s:send(State, Err)};
|
|
|
|
c2s_handle_info(#{lang := Lang} = State, {exit, Reason}) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
Err = xmpp:serr_conflict(Reason, Lang),
|
2016-12-28 07:47:11 +01:00
|
|
|
{stop, ejabberd_c2s:send(State, Err)};
|
|
|
|
c2s_handle_info(State, _) ->
|
|
|
|
State.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
-spec config_reloaded() -> ok.
|
|
|
|
config_reloaded() ->
|
|
|
|
init_cache().
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
init([]) ->
|
2017-02-23 08:12:19 +01:00
|
|
|
process_flag(trap_exit, true),
|
2017-04-14 12:57:52 +02:00
|
|
|
init_cache(),
|
2018-07-15 08:52:03 +02:00
|
|
|
case lists:foldl(
|
|
|
|
fun(Mod, ok) -> Mod:init();
|
|
|
|
(_, Err) -> Err
|
|
|
|
end, ok, get_sm_backends()) of
|
|
|
|
ok ->
|
|
|
|
clean_cache(),
|
|
|
|
gen_iq_handler:start(?MODULE),
|
|
|
|
ejabberd_hooks:add(host_up, ?MODULE, host_up, 50),
|
|
|
|
ejabberd_hooks:add(host_down, ?MODULE, host_down, 60),
|
|
|
|
ejabberd_hooks:add(config_reloaded, ?MODULE, config_reloaded, 50),
|
2019-06-14 11:33:26 +02:00
|
|
|
lists:foreach(fun host_up/1, ejabberd_option:hosts()),
|
2018-07-15 08:52:03 +02:00
|
|
|
ejabberd_commands:register_commands(get_commands_spec()),
|
|
|
|
{ok, #state{}};
|
|
|
|
{error, Why} ->
|
|
|
|
{stop, Why}
|
|
|
|
end.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
|
|
|
{noreply, State}.
|
2017-02-14 08:25:08 +01:00
|
|
|
|
|
|
|
handle_info(Info, State) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2017-02-14 08:25:08 +01:00
|
|
|
{noreply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
lists:foreach(fun host_down/1, ejabberd_option:hosts()),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:delete(host_up, ?MODULE, host_up, 50),
|
|
|
|
ejabberd_hooks:delete(host_down, ?MODULE, host_down, 60),
|
2017-04-14 12:57:52 +02:00
|
|
|
ejabberd_hooks:delete(config_reloaded, ?MODULE, config_reloaded, 50),
|
2016-01-26 10:00:11 +01:00
|
|
|
ejabberd_commands:unregister_commands(get_commands_spec()),
|
2006-01-29 05:38:31 +01:00
|
|
|
ok.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2017-02-23 08:12:19 +01:00
|
|
|
-spec host_up(binary()) -> ok.
|
|
|
|
host_up(Host) ->
|
|
|
|
ejabberd_hooks:add(c2s_handle_info, Host,
|
|
|
|
ejabberd_sm, c2s_handle_info, 50),
|
|
|
|
ejabberd_hooks:add(roster_in_subscription, Host,
|
|
|
|
ejabberd_sm, check_in_subscription, 20),
|
|
|
|
ejabberd_hooks:add(offline_message_hook, Host,
|
|
|
|
ejabberd_sm, bounce_offline_message, 100),
|
2018-12-05 11:14:29 +01:00
|
|
|
ejabberd_hooks:add(bounce_sm_packet, Host,
|
|
|
|
ejabberd_sm, bounce_sm_packet, 100),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:add(remove_user, Host,
|
|
|
|
ejabberd_sm, disconnect_removed_user, 100),
|
|
|
|
ejabberd_c2s:host_up(Host).
|
|
|
|
|
|
|
|
-spec host_down(binary()) -> ok.
|
|
|
|
host_down(Host) ->
|
|
|
|
Mod = get_sm_backend(Host),
|
2017-08-10 12:17:31 +02:00
|
|
|
Err = case ejabberd_cluster:get_nodes() of
|
|
|
|
[Node] when Node == node() -> xmpp:serr_system_shutdown();
|
|
|
|
_ -> xmpp:serr_reset()
|
|
|
|
end,
|
2017-02-23 08:12:19 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(#session{sid = {_, Pid}}) when node(Pid) == node() ->
|
2017-08-10 12:17:31 +02:00
|
|
|
ejabberd_c2s:send(Pid, Err),
|
2020-04-01 14:35:49 +02:00
|
|
|
ejabberd_c2s:stop_async(Pid);
|
2017-02-23 08:12:19 +01:00
|
|
|
(_) ->
|
|
|
|
ok
|
2017-04-14 12:57:52 +02:00
|
|
|
end, get_sessions(Mod, Host)),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:delete(c2s_handle_info, Host,
|
|
|
|
ejabberd_sm, c2s_handle_info, 50),
|
|
|
|
ejabberd_hooks:delete(roster_in_subscription, Host,
|
|
|
|
ejabberd_sm, check_in_subscription, 20),
|
|
|
|
ejabberd_hooks:delete(offline_message_hook, Host,
|
|
|
|
ejabberd_sm, bounce_offline_message, 100),
|
2018-12-05 11:14:29 +01:00
|
|
|
ejabberd_hooks:delete(bounce_sm_packet, Host,
|
|
|
|
ejabberd_sm, bounce_sm_packet, 100),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:delete(remove_user, Host,
|
|
|
|
ejabberd_sm, disconnect_removed_user, 100),
|
|
|
|
ejabberd_c2s:host_down(Host).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
-spec set_session(sid(), binary(), binary(), binary(),
|
2017-04-14 12:57:52 +02:00
|
|
|
prio(), info()) -> ok | {error, any()}.
|
2015-10-07 00:06:58 +02:00
|
|
|
|
2008-02-15 17:35:32 +01:00
|
|
|
set_session(SID, User, Server, Resource, Priority, Info) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
USR = {LUser, LServer, LResource},
|
2018-12-01 11:33:44 +01:00
|
|
|
set_session(#session{sid = SID, usr = USR, us = US,
|
|
|
|
priority = Priority, info = Info}).
|
|
|
|
|
|
|
|
-spec set_session(#session{}) -> ok | {error, any()}.
|
|
|
|
set_session(#session{us = {LUser, LServer}} = Session) ->
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-12-01 11:33:44 +01:00
|
|
|
case Mod:set_session(Session) of
|
2017-04-14 12:57:52 +02:00
|
|
|
ok ->
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?SM_CACHE, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec get_sessions(module()) -> [#session{}].
|
|
|
|
get_sessions(Mod) ->
|
2019-07-30 19:57:09 +02:00
|
|
|
delete_dead(Mod, Mod:get_sessions()).
|
2017-04-14 12:57:52 +02:00
|
|
|
|
|
|
|
-spec get_sessions(module(), binary()) -> [#session{}].
|
|
|
|
get_sessions(Mod, LServer) ->
|
2019-07-30 19:57:09 +02:00
|
|
|
delete_dead(Mod, Mod:get_sessions(LServer)).
|
2017-04-14 12:57:52 +02:00
|
|
|
|
|
|
|
-spec get_sessions(module(), binary(), binary()) -> [#session{}].
|
|
|
|
get_sessions(Mod, LUser, LServer) ->
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
case ets_cache:lookup(
|
|
|
|
?SM_CACHE, {LUser, LServer},
|
|
|
|
fun() ->
|
|
|
|
case Mod:get_sessions(LUser, LServer) of
|
|
|
|
{ok, Ss} when Ss /= [] ->
|
|
|
|
{ok, Ss};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end) of
|
|
|
|
{ok, Sessions} ->
|
2019-07-30 19:57:09 +02:00
|
|
|
delete_dead(Mod, Sessions);
|
2017-04-14 12:57:52 +02:00
|
|
|
error ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
case Mod:get_sessions(LUser, LServer) of
|
2019-07-30 19:57:09 +02:00
|
|
|
{ok, Ss} -> delete_dead(Mod, Ss);
|
2017-04-14 12:57:52 +02:00
|
|
|
_ -> []
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec get_sessions(module(), binary(), binary(), binary()) -> [#session{}].
|
|
|
|
get_sessions(Mod, LUser, LServer, LResource) ->
|
|
|
|
Sessions = get_sessions(Mod, LUser, LServer),
|
|
|
|
[S || S <- Sessions, element(3, S#session.usr) == LResource].
|
|
|
|
|
|
|
|
-spec delete_session(module(), #session{}) -> ok.
|
|
|
|
delete_session(Mod, #session{usr = {LUser, LServer, _}} = Session) ->
|
|
|
|
Mod:delete_session(Session),
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?SM_CACHE, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2019-07-30 19:57:09 +02:00
|
|
|
-spec delete_dead(module(), [#session{}]) -> [#session{}].
|
|
|
|
delete_dead(Mod, Sessions) ->
|
|
|
|
lists:filter(
|
|
|
|
fun(#session{sid = {_, Pid}} = Session) when node(Pid) == node() ->
|
|
|
|
case is_process_alive(Pid) of
|
|
|
|
true -> true;
|
|
|
|
false ->
|
|
|
|
delete_session(Mod, Session),
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
true
|
|
|
|
end, Sessions).
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec do_route(jid(), term()) -> any().
|
|
|
|
do_route(#jid{lresource = <<"">>} = To, Term) ->
|
2016-11-07 08:10:57 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
2016-12-11 13:03:37 +01:00
|
|
|
do_route(jid:replace_resource(To, R), Term)
|
2016-11-07 08:10:57 +01:00
|
|
|
end, get_user_resources(To#jid.user, To#jid.server));
|
2016-12-11 13:03:37 +01:00
|
|
|
do_route(To, Term) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Broadcasting ~p to ~ts", [Term, jid:encode(To)]),
|
2016-11-07 08:10:57 +01:00
|
|
|
{U, S, R} = jid:tolower(To),
|
|
|
|
Mod = get_sm_backend(S),
|
2018-11-30 14:18:49 +01:00
|
|
|
case get_sessions(Mod, U, S, R) of
|
2016-11-07 08:10:57 +01:00
|
|
|
[] ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Dropping broadcast to unavailable resourse: ~p", [Term]);
|
2016-11-07 08:10:57 +01:00
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
Pid = element(2, Session#session.sid),
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Sending to process ~p: ~p", [Pid, Term]),
|
2017-02-18 07:36:27 +01:00
|
|
|
ejabberd_c2s:route(Pid, Term)
|
2016-12-11 13:03:37 +01:00
|
|
|
end.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec do_route(stanza()) -> any().
|
2018-01-26 13:02:06 +01:00
|
|
|
do_route(#presence{to = To, type = T} = Packet)
|
2016-11-07 08:10:57 +01:00
|
|
|
when T == subscribe; T == subscribed; T == unsubscribe; T == unsubscribed ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Processing subscription:~n~ts", [xmpp:pp(Packet)]),
|
2018-01-26 13:02:06 +01:00
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
2017-02-16 09:00:26 +01:00
|
|
|
case is_privacy_allow(Packet) andalso
|
2016-11-07 08:10:57 +01:00
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
roster_in_subscription,
|
2018-01-26 13:02:06 +01:00
|
|
|
LServer, false, [Packet]) of
|
2016-11-07 08:10:57 +01:00
|
|
|
true ->
|
|
|
|
Mod = get_sm_backend(LServer),
|
|
|
|
lists:foreach(
|
|
|
|
fun(#session{sid = SID, usr = {_, _, R},
|
|
|
|
priority = Prio}) when is_integer(Prio) ->
|
|
|
|
Pid = element(2, SID),
|
2017-02-16 09:00:26 +01:00
|
|
|
Packet1 = Packet#presence{to = jid:replace_resource(To, R)},
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Sending to process ~p:~n~ts",
|
2017-02-16 09:00:26 +01:00
|
|
|
[Pid, xmpp:pp(Packet1)]),
|
2017-02-18 07:36:27 +01:00
|
|
|
ejabberd_c2s:route(Pid, {route, Packet1});
|
2016-11-07 08:10:57 +01:00
|
|
|
(_) ->
|
|
|
|
ok
|
2018-11-30 14:18:49 +01:00
|
|
|
end, get_sessions(Mod, LUser, LServer));
|
2016-11-07 08:10:57 +01:00
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
2017-02-16 09:00:26 +01:00
|
|
|
do_route(#presence{to = #jid{lresource = <<"">>} = To} = Packet) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Processing presence to bare JID:~n~ts", [xmpp:pp(Packet)]),
|
2016-11-07 08:10:57 +01:00
|
|
|
{LUser, LServer, _} = jid:tolower(To),
|
|
|
|
lists:foreach(
|
|
|
|
fun({_, R}) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
do_route(Packet#presence{to = jid:replace_resource(To, R)})
|
2016-11-07 08:10:57 +01:00
|
|
|
end, get_user_present_resources(LUser, LServer));
|
2018-12-05 11:14:29 +01:00
|
|
|
do_route(#message{to = #jid{lresource = <<"">>} = To, type = T} = Packet) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Processing message to bare JID:~n~ts", [xmpp:pp(Packet)]),
|
2017-04-05 20:41:10 +02:00
|
|
|
if T == chat; T == headline; T == normal ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_message(Packet);
|
2016-11-07 08:10:57 +01:00
|
|
|
true ->
|
2018-12-05 11:14:29 +01:00
|
|
|
ejabberd_hooks:run_fold(bounce_sm_packet,
|
|
|
|
To#jid.lserver, {bounce, Packet}, [])
|
|
|
|
end;
|
|
|
|
do_route(#iq{to = #jid{lresource = <<"">>} = To, type = T} = Packet) ->
|
|
|
|
if T == set; T == get ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Processing IQ to bare JID:~n~ts", [xmpp:pp(Packet)]),
|
2018-12-05 11:14:29 +01:00
|
|
|
gen_iq_handler:handle(?MODULE, Packet);
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:run_fold(bounce_sm_packet,
|
|
|
|
To#jid.lserver, {pass, Packet}, [])
|
2016-11-07 08:10:57 +01:00
|
|
|
end;
|
2017-02-16 09:00:26 +01:00
|
|
|
do_route(Packet) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Processing packet to full JID:~n~ts", [xmpp:pp(Packet)]),
|
2017-02-16 09:00:26 +01:00
|
|
|
To = xmpp:get_to(Packet),
|
2016-11-07 08:10:57 +01:00
|
|
|
{LUser, LServer, LResource} = jid:tolower(To),
|
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
case get_sessions(Mod, LUser, LServer, LResource) of
|
2016-11-07 08:10:57 +01:00
|
|
|
[] ->
|
|
|
|
case Packet of
|
2017-04-05 21:03:13 +02:00
|
|
|
#message{type = T} when T == chat; T == normal ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_message(Packet);
|
2017-04-05 21:03:13 +02:00
|
|
|
#message{type = T} when T == headline ->
|
2018-12-05 11:14:29 +01:00
|
|
|
ejabberd_hooks:run_fold(bounce_sm_packet,
|
|
|
|
LServer, {pass, Packet}, []);
|
2016-11-07 08:10:57 +01:00
|
|
|
#presence{} ->
|
2018-12-05 11:14:29 +01:00
|
|
|
ejabberd_hooks:run_fold(bounce_sm_packet,
|
|
|
|
LServer, {pass, Packet}, []);
|
2016-11-07 08:10:57 +01:00
|
|
|
_ ->
|
2018-12-05 11:14:29 +01:00
|
|
|
ejabberd_hooks:run_fold(bounce_sm_packet,
|
|
|
|
LServer, {bounce, Packet}, [])
|
2016-11-07 08:10:57 +01:00
|
|
|
end;
|
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
Pid = element(2, Session#session.sid),
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Sending to process ~p:~n~ts", [Pid, xmpp:pp(Packet)]),
|
2017-02-18 07:36:27 +01:00
|
|
|
ejabberd_c2s:route(Pid, {route, Packet})
|
2002-11-30 19:46:16 +01:00
|
|
|
end.
|
|
|
|
|
2009-03-24 17:22:33 +01:00
|
|
|
%% The default list applies to the user as a whole,
|
|
|
|
%% and is processed if there is no active list set
|
|
|
|
%% for the target session/resource to which a stanza is addressed,
|
|
|
|
%% or if there are no current sessions for the user.
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec is_privacy_allow(stanza()) -> boolean().
|
|
|
|
is_privacy_allow(Packet) ->
|
|
|
|
To = xmpp:get_to(Packet),
|
2017-01-09 15:02:17 +01:00
|
|
|
LServer = To#jid.server,
|
|
|
|
allow == ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, LServer, allow,
|
2017-02-16 09:00:26 +01:00
|
|
|
[To, Packet, in]).
|
2009-03-24 17:22:33 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec route_message(message()) -> any().
|
|
|
|
route_message(#message{to = To, type = Type} = Packet) ->
|
2004-01-02 20:36:38 +01:00
|
|
|
LUser = To#jid.luser,
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = To#jid.lserver,
|
2006-04-23 11:31:54 +02:00
|
|
|
PrioRes = get_user_present_resources(LUser, LServer),
|
|
|
|
case catch lists:max(PrioRes) of
|
2016-11-22 19:25:20 +01:00
|
|
|
{MaxPrio, MaxRes}
|
|
|
|
when is_integer(MaxPrio), MaxPrio >= 0 ->
|
|
|
|
lists:foreach(fun ({P, R}) when P == MaxPrio;
|
2015-03-25 01:17:35 +01:00
|
|
|
(P >= 0) and (Type == headline) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LResource = jid:resourceprep(R),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
case get_sessions(Mod, LUser, LServer,
|
|
|
|
LResource) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] ->
|
|
|
|
ok; % Race condition
|
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
Pid = element(2, Session#session.sid),
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Sending to process ~p~n", [Pid]),
|
2016-11-22 19:25:20 +01:00
|
|
|
LMaxRes = jid:resourceprep(MaxRes),
|
|
|
|
Packet1 = maybe_mark_as_copy(Packet,
|
|
|
|
LResource,
|
|
|
|
LMaxRes,
|
|
|
|
P, MaxPrio),
|
2017-02-18 07:36:27 +01:00
|
|
|
ejabberd_c2s:route(Pid, {route, Packet1})
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
%% Ignore other priority:
|
|
|
|
({_Prio, _Res}) -> ok
|
|
|
|
end,
|
|
|
|
PrioRes);
|
|
|
|
_ ->
|
2017-05-11 14:49:06 +02:00
|
|
|
case ejabberd_auth:user_exists(LUser, LServer) andalso
|
2017-02-16 09:00:26 +01:00
|
|
|
is_privacy_allow(Packet) of
|
2016-11-08 13:15:19 +01:00
|
|
|
true ->
|
2017-01-21 11:47:47 +01:00
|
|
|
ejabberd_hooks:run_fold(offline_message_hook,
|
2017-02-23 16:55:35 +01:00
|
|
|
LServer, {bounce, Packet}, []);
|
2016-11-08 13:15:19 +01:00
|
|
|
false ->
|
2016-11-25 09:41:24 +01:00
|
|
|
Err = xmpp:err_service_unavailable(),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2016-11-08 13:15:19 +01:00
|
|
|
end
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2016-11-22 19:25:20 +01:00
|
|
|
-spec maybe_mark_as_copy(message(), binary(), binary(), integer(), integer())
|
|
|
|
-> message().
|
|
|
|
maybe_mark_as_copy(Packet, R, R, P, P) ->
|
|
|
|
Packet;
|
|
|
|
maybe_mark_as_copy(Packet, _, _, P, P) ->
|
2016-11-24 10:47:26 +01:00
|
|
|
xmpp:put_meta(Packet, sm_copy, true);
|
2016-11-22 19:25:20 +01:00
|
|
|
maybe_mark_as_copy(Packet, _, _, _, _) ->
|
|
|
|
Packet.
|
|
|
|
|
2002-12-14 21:07:26 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec clean_session_list([#session{}]) -> [#session{}].
|
2006-01-24 00:13:06 +01:00
|
|
|
clean_session_list(Ss) ->
|
|
|
|
clean_session_list(lists:keysort(#session.usr, Ss), []).
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec clean_session_list([#session{}], [#session{}]) -> [#session{}].
|
2013-03-14 10:33:02 +01:00
|
|
|
clean_session_list([], Res) -> Res;
|
|
|
|
clean_session_list([S], Res) -> [S | Res];
|
2006-01-24 00:13:06 +01:00
|
|
|
clean_session_list([S1, S2 | Rest], Res) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
if S1#session.usr == S2#session.usr ->
|
|
|
|
if S1#session.sid > S2#session.sid ->
|
|
|
|
clean_session_list([S1 | Rest], Res);
|
|
|
|
true -> clean_session_list([S2 | Rest], Res)
|
|
|
|
end;
|
|
|
|
true -> clean_session_list([S2 | Rest], [S1 | Res])
|
2002-12-14 21:07:26 +01:00
|
|
|
end.
|
|
|
|
|
2002-12-30 21:25:08 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2006-05-21 18:34:32 +02:00
|
|
|
%% On new session, check if some existing connections need to be replace
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec check_for_sessions_to_replace(binary(), binary(), binary()) -> ok | replaced.
|
2006-05-21 18:34:32 +02:00
|
|
|
check_for_sessions_to_replace(User, Server, Resource) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2006-05-21 18:34:32 +02:00
|
|
|
check_existing_resources(LUser, LServer, LResource),
|
|
|
|
check_max_sessions(LUser, LServer).
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec check_existing_resources(binary(), binary(), binary()) -> ok.
|
2006-05-21 18:34:32 +02:00
|
|
|
check_existing_resources(LUser, LServer, LResource) ->
|
2016-07-23 01:08:05 +02:00
|
|
|
Mod = get_sm_backend(LServer),
|
2017-04-14 12:57:52 +02:00
|
|
|
Ss = get_sessions(Mod, LUser, LServer, LResource),
|
2018-11-30 14:18:49 +01:00
|
|
|
if Ss == [] -> ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2018-11-30 14:18:49 +01:00
|
|
|
SIDs = [SID || #session{sid = SID} <- Ss],
|
2013-03-14 10:33:02 +01:00
|
|
|
MaxSID = lists:max(SIDs),
|
2016-07-23 01:08:05 +02:00
|
|
|
lists:foreach(fun ({_, Pid} = S) when S /= MaxSID ->
|
2017-02-18 07:36:27 +01:00
|
|
|
ejabberd_c2s:route(Pid, replaced);
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) -> ok
|
|
|
|
end,
|
|
|
|
SIDs)
|
2006-05-21 18:34:32 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec is_existing_resource(binary(), binary(), binary()) -> boolean().
|
|
|
|
|
2011-08-17 19:42:11 +02:00
|
|
|
is_existing_resource(LUser, LServer, LResource) ->
|
|
|
|
[] /= get_resource_sessions(LUser, LServer, LResource).
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_resource_sessions(binary(), binary(), binary()) -> [sid()].
|
2011-08-17 19:42:11 +02:00
|
|
|
get_resource_sessions(User, Server, Resource) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
[S#session.sid || S <- get_sessions(Mod, LUser, LServer, LResource)].
|
2011-08-17 19:42:11 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec check_max_sessions(binary(), binary()) -> ok | replaced.
|
2006-05-21 18:34:32 +02:00
|
|
|
check_max_sessions(LUser, LServer) ->
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2017-04-14 12:57:52 +02:00
|
|
|
Ss = get_sessions(Mod, LUser, LServer),
|
2006-11-23 07:34:05 +01:00
|
|
|
MaxSessions = get_max_user_sessions(LUser, LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
if length(Ss) =< MaxSessions -> ok;
|
2016-10-02 22:01:03 +02:00
|
|
|
true ->
|
2018-11-30 14:18:49 +01:00
|
|
|
#session{sid = {_, Pid}} = lists:min(Ss),
|
2017-02-18 07:36:27 +01:00
|
|
|
ejabberd_c2s:route(Pid, replaced)
|
2006-05-21 18:34:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Get the user_max_session setting
|
|
|
|
%% This option defines the max number of time a given users are allowed to
|
|
|
|
%% log in
|
2006-05-22 12:17:22 +02:00
|
|
|
%% Defaults to infinity
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_max_user_sessions(binary(), binary()) -> infinity | non_neg_integer().
|
2006-11-23 07:34:05 +01:00
|
|
|
get_max_user_sessions(LUser, Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case ejabberd_shaper:match(Host, max_user_sessions,
|
|
|
|
jid:make(LUser, Host)) of
|
|
|
|
Max when is_integer(Max) -> Max;
|
|
|
|
infinity -> infinity;
|
|
|
|
_ -> ?MAX_USER_SESSIONS
|
2006-05-21 18:34:32 +02:00
|
|
|
end.
|
|
|
|
|
2003-01-01 20:54:44 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec force_update_presence({binary(), binary()}) -> ok.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-03-09 12:57:33 +01:00
|
|
|
force_update_presence({LUser, LServer}) ->
|
2016-02-19 14:15:11 +01:00
|
|
|
Mod = get_sm_backend(LServer),
|
2018-11-30 14:18:49 +01:00
|
|
|
Ss = get_sessions(Mod, LUser, LServer),
|
2015-03-09 12:57:33 +01:00
|
|
|
lists:foreach(fun (#session{sid = {_, Pid}}) ->
|
2017-05-17 13:47:35 +02:00
|
|
|
ejabberd_c2s:resend_presence(Pid)
|
2015-03-09 12:57:33 +01:00
|
|
|
end,
|
|
|
|
Ss).
|
|
|
|
|
2016-02-19 14:15:11 +01:00
|
|
|
-spec get_sm_backend(binary()) -> module().
|
2015-03-09 12:57:33 +01:00
|
|
|
|
2016-02-19 14:15:11 +01:00
|
|
|
get_sm_backend(Host) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
DBType = ejabberd_option:sm_db_type(Host),
|
2019-06-15 14:14:45 +02:00
|
|
|
list_to_existing_atom("ejabberd_sm_" ++ atom_to_list(DBType)).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
2016-02-19 14:15:11 +01:00
|
|
|
-spec get_sm_backends() -> [module()].
|
|
|
|
|
|
|
|
get_sm_backends() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
lists:usort([get_sm_backend(Host) || Host <- ejabberd_option:hosts()]).
|
2016-02-19 14:15:11 +01:00
|
|
|
|
|
|
|
-spec get_vh_by_backend(module()) -> [binary()].
|
|
|
|
|
|
|
|
get_vh_by_backend(Mod) ->
|
|
|
|
lists:filter(
|
|
|
|
fun(Host) ->
|
|
|
|
get_sm_backend(Host) == Mod
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ejabberd_option:hosts()).
|
2016-02-19 14:15:11 +01:00
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Cache stuff
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
-spec init_cache() -> ok.
|
|
|
|
init_cache() ->
|
|
|
|
case use_cache() of
|
|
|
|
true ->
|
|
|
|
ets_cache:new(?SM_CACHE, cache_opts());
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?SM_CACHE)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_opts() -> [proplists:property()].
|
|
|
|
cache_opts() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = ejabberd_option:sm_cache_size(),
|
|
|
|
CacheMissed = ejabberd_option:sm_cache_missed(),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = ejabberd_option:sm_cache_life_time(),
|
2017-04-14 12:57:52 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
2018-09-09 08:59:08 +02:00
|
|
|
-spec clean_cache(node()) -> non_neg_integer().
|
2017-04-14 12:57:52 +02:00
|
|
|
clean_cache(Node) ->
|
|
|
|
ets_cache:filter(
|
|
|
|
?SM_CACHE,
|
|
|
|
fun(_, error) ->
|
|
|
|
false;
|
|
|
|
(_, {ok, Ss}) ->
|
|
|
|
not lists:any(
|
|
|
|
fun(#session{sid = {_, Pid}}) ->
|
|
|
|
node(Pid) == Node
|
|
|
|
end, Ss)
|
|
|
|
end).
|
|
|
|
|
|
|
|
-spec clean_cache() -> ok.
|
|
|
|
clean_cache() ->
|
|
|
|
ejabberd_cluster:eval_everywhere(?MODULE, clean_cache, [node()]).
|
|
|
|
|
|
|
|
-spec use_cache(module(), binary()) -> boolean().
|
|
|
|
use_cache(Mod, LServer) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 1) of
|
|
|
|
true -> Mod:use_cache(LServer);
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> ejabberd_option:sm_use_cache(LServer)
|
2017-04-14 12:57:52 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec use_cache() -> boolean().
|
|
|
|
use_cache() ->
|
|
|
|
lists:any(
|
|
|
|
fun(Host) ->
|
|
|
|
Mod = get_sm_backend(Host),
|
|
|
|
use_cache(Mod, Host)
|
2019-06-14 11:33:26 +02:00
|
|
|
end, ejabberd_option:hosts()).
|
2017-04-14 12:57:52 +02:00
|
|
|
|
|
|
|
-spec cache_nodes(module(), binary()) -> [node()].
|
|
|
|
cache_nodes(Mod, LServer) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 1) of
|
|
|
|
true -> Mod:cache_nodes(LServer);
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
|
|
|
end.
|
|
|
|
|
2008-10-12 14:04:10 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%% ejabberd commands
|
|
|
|
|
2016-01-26 10:00:11 +01:00
|
|
|
get_commands_spec() ->
|
2017-07-21 11:18:44 +02:00
|
|
|
[#ejabberd_commands{name = connected_users, tags = [session],
|
2013-03-14 10:33:02 +01:00
|
|
|
desc = "List all established sessions",
|
2015-09-28 14:25:43 +02:00
|
|
|
policy = admin,
|
2013-03-14 10:33:02 +01:00
|
|
|
module = ?MODULE, function = connected_users, args = [],
|
2017-07-21 11:18:44 +02:00
|
|
|
result_desc = "List of users sessions",
|
|
|
|
result_example = [<<"user1@example.com">>, <<"user2@example.com">>],
|
2013-03-14 10:33:02 +01:00
|
|
|
result = {connected_users, {list, {sessions, string}}}},
|
2021-04-13 14:47:49 +02:00
|
|
|
#ejabberd_commands{name = connected_users_number, tags = [session, statistics],
|
2013-03-14 10:33:02 +01:00
|
|
|
desc = "Get the number of established sessions",
|
2015-09-28 14:25:43 +02:00
|
|
|
policy = admin,
|
2013-03-14 10:33:02 +01:00
|
|
|
module = ?MODULE, function = connected_users_number,
|
2017-07-21 11:18:44 +02:00
|
|
|
result_example = 2,
|
2013-03-14 10:33:02 +01:00
|
|
|
args = [], result = {num_sessions, integer}},
|
2017-07-21 11:18:44 +02:00
|
|
|
#ejabberd_commands{name = user_resources, tags = [session],
|
2013-03-14 10:33:02 +01:00
|
|
|
desc = "List user's connected resources",
|
2017-08-03 13:48:57 +02:00
|
|
|
policy = admin,
|
2013-03-14 10:33:02 +01:00
|
|
|
module = ?MODULE, function = user_resources,
|
2017-07-21 11:42:03 +02:00
|
|
|
args = [{user, binary}, {host, binary}],
|
2017-07-21 11:18:44 +02:00
|
|
|
args_desc = ["User name", "Server name"],
|
|
|
|
args_example = [<<"user1">>, <<"example.com">>],
|
|
|
|
result_example = [<<"tka1">>, <<"Gajim">>, <<"mobile-app">>],
|
2014-04-29 00:50:43 +02:00
|
|
|
result = {resources, {list, {resource, string}}}},
|
2017-07-21 11:18:44 +02:00
|
|
|
#ejabberd_commands{name = kick_user, tags = [session],
|
2014-04-29 00:50:43 +02:00
|
|
|
desc = "Disconnect user's active sessions",
|
2014-05-12 12:44:40 +02:00
|
|
|
module = ?MODULE, function = kick_user,
|
2014-04-29 00:50:43 +02:00
|
|
|
args = [{user, binary}, {host, binary}],
|
2017-07-21 11:18:44 +02:00
|
|
|
args_desc = ["User name", "Server name"],
|
|
|
|
args_example = [<<"user1">>, <<"example.com">>],
|
|
|
|
result_desc = "Number of resources that were kicked",
|
|
|
|
result_example = 3,
|
2014-04-29 00:50:43 +02:00
|
|
|
result = {num_resources, integer}}].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec connected_users() -> [binary()].
|
2008-10-12 14:04:10 +02:00
|
|
|
|
|
|
|
connected_users() ->
|
2006-02-15 05:15:54 +01:00
|
|
|
USRs = dirty_get_sessions_list(),
|
|
|
|
SUSRs = lists:sort(USRs),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun ({U, S, R}) -> <<U/binary, $@, S/binary, $/, R/binary>> end,
|
|
|
|
SUSRs).
|
2008-10-12 14:04:10 +02:00
|
|
|
|
|
|
|
connected_users_number() ->
|
|
|
|
length(dirty_get_sessions_list()).
|
|
|
|
|
|
|
|
user_resources(User, Server) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Resources = get_user_resources(User, Server),
|
2008-10-12 14:04:10 +02:00
|
|
|
lists:sort(Resources).
|
2006-02-15 05:15:54 +01:00
|
|
|
|
2018-05-30 18:21:56 +02:00
|
|
|
-spec kick_user(binary(), binary()) -> non_neg_integer().
|
2014-05-12 12:44:40 +02:00
|
|
|
kick_user(User, Server) ->
|
2014-04-29 00:50:43 +02:00
|
|
|
Resources = get_user_resources(User, Server),
|
2018-05-30 18:21:56 +02:00
|
|
|
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.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-04-08 10:46:21 +02:00
|
|
|
make_sid() ->
|
2019-02-27 11:00:02 +01:00
|
|
|
{misc:unique_timestamp(), self()}.
|