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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
2015-03-09 14:41:13 +01:00
|
|
|
-export([start/0,
|
|
|
|
start_link/0,
|
2004-11-05 22:14:31 +01:00
|
|
|
route/3,
|
2014-03-19 00:51:33 +01:00
|
|
|
open_session/5,
|
|
|
|
open_session/6,
|
|
|
|
close_session/4,
|
2007-05-03 07:15:12 +02:00
|
|
|
check_in_subscription/6,
|
2004-08-08 21:07:55 +02:00
|
|
|
bounce_offline_message/3,
|
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,
|
2007-05-21 05:21:34 +02:00
|
|
|
set_presence/7,
|
|
|
|
unset_presence/6,
|
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,
|
2003-01-29 18:12:23 +01:00
|
|
|
register_iq_handler/4,
|
2005-06-20 05:18:13 +02:00
|
|
|
register_iq_handler/5,
|
2006-02-15 05:15:54 +01:00
|
|
|
unregister_iq_handler/2,
|
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,
|
2007-12-01 06:16:30 +01:00
|
|
|
get_session_pid/3,
|
2008-02-15 17:35:32 +01:00
|
|
|
get_user_info/3,
|
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,
|
2011-08-17 19:42:11 +02:00
|
|
|
is_existing_resource/3
|
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,
|
2015-06-01 14:38:27 +02:00
|
|
|
handle_info/2, terminate/2, code_change/3, opt_type/1]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-05-09 21:32:59 +02:00
|
|
|
-include("jlib.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2008-10-12 14:04:10 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
2009-03-24 17:22:33 +01:00
|
|
|
-include("mod_privacy.hrl").
|
2015-03-09 12:57:33 +01:00
|
|
|
-include("ejabberd_sm.hrl").
|
|
|
|
|
|
|
|
-callback init() -> ok | {error, any()}.
|
|
|
|
-callback set_session(#session{}) -> ok.
|
2015-03-11 12:46:57 +01:00
|
|
|
-callback delete_session(binary(), binary(), binary(), sid()) ->
|
|
|
|
{ok, #session{}} | {error, notfound}.
|
2015-03-09 12:57:33 +01:00
|
|
|
-callback get_sessions() -> [#session{}].
|
|
|
|
-callback get_sessions(binary()) -> [#session{}].
|
|
|
|
-callback get_sessions(binary(), binary()) -> [#session{}].
|
|
|
|
-callback get_sessions(binary(), binary(), binary()) -> [#session{}].
|
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
|
|
|
|
%%====================================================================
|
2013-03-14 10:33:02 +01:00
|
|
|
-export_type([sid/0]).
|
|
|
|
|
2015-03-09 14:41:13 +01:00
|
|
|
start() ->
|
|
|
|
ChildSpec = {?MODULE, {?MODULE, start_link, []},
|
|
|
|
transient, 1000, worker, [?MODULE]},
|
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2015-10-07 00:06:58 +02:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec route(jid(), jid(), xmlel() | broadcast()) -> ok.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
route(From, To, Packet) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ -> 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-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2015-03-11 12:46:57 +01:00
|
|
|
Info = case Mod:delete_session(LUser, LServer, LResource, SID) of
|
2015-03-09 12:57:33 +01:00
|
|
|
{ok, #session{info = I}} -> I;
|
|
|
|
{error, notfound} -> []
|
|
|
|
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
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
-spec check_in_subscription(any(), binary(), binary(),
|
|
|
|
any(), any(), any()) -> any().
|
|
|
|
|
2007-05-21 05:21:34 +02:00
|
|
|
check_in_subscription(Acc, User, Server, _JID, _Type, _Reason) ->
|
2007-05-03 07:15:12 +02:00
|
|
|
case ejabberd_auth:is_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.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec bounce_offline_message(jid(), jid(), xmlel()) -> stop.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
bounce_offline_message(From, To, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
2006-01-29 05:38:31 +01:00
|
|
|
ejabberd_router:route(To, From, Err),
|
|
|
|
stop.
|
|
|
|
|
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) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_sm:route(jid:make(<<"">>, <<"">>, <<"">>),
|
|
|
|
jid:make(User, Server, <<"">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
{broadcast, {exit, <<"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),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
Ss = Mod:get_sessions(LUser, LServer),
|
|
|
|
[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) ->
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
Ss = Mod:get_sessions(LUser, LServer),
|
|
|
|
[{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),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
case Mod:get_sessions(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.
|
|
|
|
|
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),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
case Mod:get_sessions(LUser, LServer, LResource) of
|
2008-02-15 17:35:32 +01:00
|
|
|
[] ->
|
|
|
|
offline;
|
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
Node = node(element(2, Session#session.sid)),
|
|
|
|
Conn = proplists:get_value(conn, Session#session.info),
|
|
|
|
IP = proplists:get_value(ip, Session#session.info),
|
|
|
|
[{node, Node}, {conn, Conn}, {ip, IP}]
|
|
|
|
end.
|
2007-05-21 05:21:34 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec set_presence(sid(), binary(), binary(), binary(),
|
|
|
|
prio(), xmlel(), info()) -> ok.
|
|
|
|
|
|
|
|
set_presence(SID, User, Server, Resource, Priority,
|
|
|
|
Presence, Info) ->
|
|
|
|
set_session(SID, User, Server, Resource, Priority,
|
|
|
|
Info),
|
|
|
|
ejabberd_hooks:run(set_presence_hook,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:nameprep(Server),
|
2007-05-03 17:45:24 +02:00
|
|
|
[User, Server, Resource, Presence]).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec unset_presence(sid(), binary(), binary(),
|
|
|
|
binary(), binary(), info()) -> ok.
|
|
|
|
|
|
|
|
unset_presence(SID, User, Server, Resource, Status,
|
|
|
|
Info) ->
|
|
|
|
set_session(SID, User, Server, Resource, undefined,
|
|
|
|
Info),
|
|
|
|
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 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) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
LResource = jid:resourceprep(Resource),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
case Mod:get_sessions(LUser, LServer, LResource) of
|
2007-12-01 06:16:30 +01:00
|
|
|
[#session{sid = {_, Pid}}] -> Pid;
|
|
|
|
_ -> none
|
|
|
|
end.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
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() ->
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
[S#session.usr || S <- Mod:get_sessions()].
|
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() ->
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
[S || S <- Mod:get_sessions(), node(element(2, S#session.sid)) == node()].
|
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),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
[S#session.usr || S <- Mod:get_sessions(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() ->
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
[element(2, S#session.sid) || S <- Mod:get_sessions()].
|
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),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
length(Mod:get_sessions(LServer)).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
register_iq_handler(Host, XMLNS, Module, Fun) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
ejabberd_sm ! {register_iq_handler, Host, XMLNS, Module, Fun}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec register_iq_handler(binary(), binary(), atom(), atom(), list()) -> any().
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
register_iq_handler(Host, XMLNS, Module, Fun, Opts) ->
|
2015-10-07 00:06:58 +02:00
|
|
|
ejabberd_sm ! {register_iq_handler, Host, XMLNS, Module, Fun, Opts}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
-spec unregister_iq_handler(binary(), binary()) -> any().
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
unregister_iq_handler(Host, XMLNS) ->
|
|
|
|
ejabberd_sm ! {unregister_iq_handler, Host, XMLNS}.
|
|
|
|
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
init([]) ->
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
Mod:init(),
|
2003-01-01 20:54:44 +01:00
|
|
|
ets:new(sm_iqtable, [named_table]),
|
2005-06-20 05:18:13 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
2007-05-03 07:15:12 +02:00
|
|
|
ejabberd_hooks:add(roster_in_subscription, Host,
|
|
|
|
ejabberd_sm, check_in_subscription, 20),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(offline_message_hook, Host,
|
|
|
|
ejabberd_sm, bounce_offline_message, 100),
|
|
|
|
ejabberd_hooks:add(remove_user, Host,
|
|
|
|
ejabberd_sm, disconnect_removed_user, 100)
|
|
|
|
end, ?MYHOSTS),
|
2008-10-12 14:04:10 +02:00
|
|
|
ejabberd_commands:register_commands(commands()),
|
2006-01-29 05:38:31 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
handle_call(_Request, _From, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = ok, {reply, Reply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_cast(_Msg, State) -> {noreply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
2004-11-05 22:14:31 +01:00
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
2006-01-29 05:38:31 +01:00
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({register_iq_handler, Host, XMLNS, Module, Function}, State) ->
|
|
|
|
ets:insert(sm_iqtable, {{XMLNS, Host}, Module, Function}),
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({register_iq_handler, Host, XMLNS, Module,
|
|
|
|
Function, Opts},
|
|
|
|
State) ->
|
|
|
|
ets:insert(sm_iqtable,
|
|
|
|
{{XMLNS, Host}, Module, Function, Opts}),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({unregister_iq_handler, Host, XMLNS},
|
|
|
|
State) ->
|
2006-01-29 05:38:31 +01:00
|
|
|
case ets:lookup(sm_iqtable, {XMLNS, Host}) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:stop_iq_handler(Module, Function, Opts);
|
|
|
|
_ -> ok
|
2006-01-29 05:38:31 +01:00
|
|
|
end,
|
|
|
|
ets:delete(sm_iqtable, {XMLNS, Host}),
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
terminate(_Reason, _State) ->
|
2008-10-12 14:04:10 +02:00
|
|
|
ejabberd_commands:unregister_commands(commands()),
|
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
|
|
|
|
%%--------------------------------------------------------------------
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
-spec set_session(sid(), binary(), binary(), binary(),
|
|
|
|
prio(), info()) -> ok.
|
|
|
|
|
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},
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
Mod:set_session(#session{sid = SID, usr = USR, us = US,
|
|
|
|
priority = Priority, info = Info}).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2013-06-17 15:41:02 +02:00
|
|
|
do_route(From, To, {broadcast, _} = Packet) ->
|
|
|
|
case To#jid.lresource of
|
|
|
|
<<"">> ->
|
|
|
|
lists:foreach(fun(R) ->
|
|
|
|
do_route(From,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:replace_resource(To, R),
|
2013-06-17 15:41:02 +02:00
|
|
|
Packet)
|
|
|
|
end,
|
|
|
|
get_user_resources(To#jid.user, To#jid.server));
|
|
|
|
_ ->
|
2015-11-24 16:44:13 +01:00
|
|
|
{U, S, R} = jid:tolower(To),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
case Mod:get_sessions(U, S, R) of
|
2013-06-17 15:41:02 +02:00
|
|
|
[] ->
|
|
|
|
?DEBUG("packet dropped~n", []);
|
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
Pid = element(2, Session#session.sid),
|
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
|
|
|
Pid ! {route, From, To, Packet}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
do_route(From, To, #xmlel{} = Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("session manager~n\tfrom ~p~n\tto ~p~n\tpacket "
|
|
|
|
"~P~n",
|
2002-11-30 19:46:16 +01:00
|
|
|
[From, To, Packet, 8]),
|
2005-04-17 20:08:34 +02:00
|
|
|
#jid{user = User, server = Server,
|
2003-10-07 22:31:44 +02:00
|
|
|
luser = LUser, lserver = LServer, lresource = LResource} = To,
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = Name, attrs = Attrs} = Packet,
|
2003-11-07 21:51:23 +01:00
|
|
|
case LResource of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> ->
|
|
|
|
case Name of
|
|
|
|
<<"presence">> ->
|
|
|
|
{Pass, _Subsc} = case xml:get_attr_s(<<"type">>, Attrs)
|
|
|
|
of
|
|
|
|
<<"subscribe">> ->
|
|
|
|
Reason = xml:get_path_s(Packet,
|
|
|
|
[{elem,
|
|
|
|
<<"status">>},
|
|
|
|
cdata]),
|
|
|
|
{is_privacy_allow(From, To, Packet)
|
|
|
|
andalso
|
|
|
|
ejabberd_hooks:run_fold(roster_in_subscription,
|
|
|
|
LServer,
|
|
|
|
false,
|
|
|
|
[User, Server,
|
|
|
|
From,
|
|
|
|
subscribe,
|
|
|
|
Reason]),
|
|
|
|
true};
|
|
|
|
<<"subscribed">> ->
|
|
|
|
{is_privacy_allow(From, To, Packet)
|
|
|
|
andalso
|
|
|
|
ejabberd_hooks:run_fold(roster_in_subscription,
|
|
|
|
LServer,
|
|
|
|
false,
|
|
|
|
[User, Server,
|
|
|
|
From,
|
|
|
|
subscribed,
|
|
|
|
<<"">>]),
|
|
|
|
true};
|
|
|
|
<<"unsubscribe">> ->
|
|
|
|
{is_privacy_allow(From, To, Packet)
|
|
|
|
andalso
|
|
|
|
ejabberd_hooks:run_fold(roster_in_subscription,
|
|
|
|
LServer,
|
|
|
|
false,
|
|
|
|
[User, Server,
|
|
|
|
From,
|
|
|
|
unsubscribe,
|
|
|
|
<<"">>]),
|
|
|
|
true};
|
|
|
|
<<"unsubscribed">> ->
|
|
|
|
{is_privacy_allow(From, To, Packet)
|
|
|
|
andalso
|
|
|
|
ejabberd_hooks:run_fold(roster_in_subscription,
|
|
|
|
LServer,
|
|
|
|
false,
|
|
|
|
[User, Server,
|
|
|
|
From,
|
|
|
|
unsubscribed,
|
|
|
|
<<"">>]),
|
|
|
|
true};
|
|
|
|
_ -> {true, false}
|
|
|
|
end,
|
|
|
|
if Pass ->
|
|
|
|
PResources = get_user_present_resources(LUser, LServer),
|
|
|
|
lists:foreach(fun ({_, R}) ->
|
|
|
|
do_route(From,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:replace_resource(To,
|
2013-03-14 10:33:02 +01:00
|
|
|
R),
|
|
|
|
Packet)
|
|
|
|
end,
|
|
|
|
PResources);
|
|
|
|
true -> ok
|
|
|
|
end;
|
2015-03-25 00:52:49 +01:00
|
|
|
<<"message">> ->
|
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
2015-03-25 01:17:35 +01:00
|
|
|
<<"chat">> -> route_message(From, To, Packet, chat);
|
|
|
|
<<"headline">> -> route_message(From, To, Packet, headline);
|
2015-03-25 00:52:49 +01:00
|
|
|
<<"error">> -> ok;
|
|
|
|
<<"groupchat">> ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
2015-03-25 01:17:35 +01:00
|
|
|
route_message(From, To, Packet, normal)
|
2015-03-25 00:52:49 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"iq">> -> process_iq(From, To, Packet);
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
_ ->
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
case Mod:get_sessions(LUser, LServer, LResource) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] ->
|
|
|
|
case Name of
|
2015-03-25 00:13:05 +01:00
|
|
|
<<"message">> ->
|
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
2015-03-25 01:17:35 +01:00
|
|
|
<<"chat">> -> route_message(From, To, Packet, chat);
|
2015-05-19 09:51:49 +02:00
|
|
|
<<"normal">> -> route_message(From, To, Packet, normal);
|
|
|
|
<<"">> -> route_message(From, To, Packet, normal);
|
2015-03-25 00:13:05 +01:00
|
|
|
<<"error">> -> ok;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"iq">> ->
|
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
|
|
|
<<"error">> -> ok;
|
|
|
|
<<"result">> -> ok;
|
2003-10-07 22:31:44 +02:00
|
|
|
_ ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
2015-02-08 23:49:17 +01:00
|
|
|
_ -> ?DEBUG("packet dropped~n", [])
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
Pid = element(2, Session#session.sid),
|
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
|
|
|
Pid ! {route, From, To, Packet}
|
|
|
|
end
|
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.
|
|
|
|
is_privacy_allow(From, To, Packet) ->
|
|
|
|
User = To#jid.user,
|
|
|
|
Server = To#jid.server,
|
2013-03-14 10:33:02 +01:00
|
|
|
PrivacyList =
|
|
|
|
ejabberd_hooks:run_fold(privacy_get_user_list, Server,
|
|
|
|
#userlist{}, [User, Server]),
|
2009-03-24 17:22:33 +01:00
|
|
|
is_privacy_allow(From, To, Packet, PrivacyList).
|
|
|
|
|
|
|
|
%% Check if privacy rules allow this delivery
|
|
|
|
%% Function copied from ejabberd_c2s.erl
|
|
|
|
is_privacy_allow(From, To, Packet, PrivacyList) ->
|
|
|
|
User = To#jid.user,
|
|
|
|
Server = To#jid.server,
|
2013-03-14 10:33:02 +01:00
|
|
|
allow ==
|
|
|
|
ejabberd_hooks:run_fold(privacy_check_packet, Server,
|
|
|
|
allow,
|
|
|
|
[User, Server, PrivacyList, {From, To, Packet},
|
|
|
|
in]).
|
2009-03-24 17:22:33 +01:00
|
|
|
|
2015-03-25 01:17:35 +01:00
|
|
|
route_message(From, To, Packet, Type) ->
|
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
|
2013-03-14 10:33:02 +01:00
|
|
|
{Priority, _R}
|
|
|
|
when is_integer(Priority), Priority >= 0 ->
|
2015-03-25 01:17:35 +01:00
|
|
|
lists:foreach(fun ({P, R}) when P == Priority;
|
|
|
|
(P >= 0) and (Type == headline) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LResource = jid:resourceprep(R),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
case Mod:get_sessions(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),
|
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
|
|
|
Pid ! {route, From, To, Packet}
|
|
|
|
end;
|
|
|
|
%% Ignore other priority:
|
|
|
|
({_Prio, _Res}) -> ok
|
|
|
|
end,
|
|
|
|
PrioRes);
|
|
|
|
_ ->
|
2015-03-25 01:17:35 +01:00
|
|
|
case Type of
|
|
|
|
headline -> ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
case ejabberd_auth:is_user_exists(LUser, LServer) of
|
|
|
|
true ->
|
|
|
|
case is_privacy_allow(From, To, Packet) of
|
2003-11-23 21:11:21 +01:00
|
|
|
true ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:run(offline_message_hook, LServer,
|
|
|
|
[From, To, Packet]);
|
|
|
|
false -> ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
|
|
|
end
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2002-12-14 21:07:26 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2006-01-24 00:13:06 +01:00
|
|
|
clean_session_list(Ss) ->
|
|
|
|
clean_session_list(lists:keysort(#session.usr, Ss), []).
|
|
|
|
|
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
|
|
|
|
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).
|
|
|
|
|
|
|
|
check_existing_resources(LUser, LServer, LResource) ->
|
2011-08-17 19:42:11 +02:00
|
|
|
SIDs = get_resource_sessions(LUser, LServer, LResource),
|
2013-03-14 10:33:02 +01:00
|
|
|
if SIDs == [] -> ok;
|
|
|
|
true ->
|
|
|
|
MaxSID = lists:max(SIDs),
|
|
|
|
lists:foreach(fun ({_, Pid} = S) when S /= MaxSID ->
|
|
|
|
Pid ! replaced;
|
|
|
|
(_) -> 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).
|
|
|
|
|
|
|
|
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),
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
[S#session.sid || S <- Mod:get_sessions(LUser, LServer, LResource)].
|
2011-08-17 19:42:11 +02:00
|
|
|
|
2006-05-21 18:34:32 +02:00
|
|
|
check_max_sessions(LUser, LServer) ->
|
2015-03-09 12:57:33 +01:00
|
|
|
Mod = get_sm_backend(),
|
|
|
|
SIDs = [S#session.sid || S <- Mod:get_sessions(LUser, LServer)],
|
2006-11-23 07:34:05 +01:00
|
|
|
MaxSessions = get_max_user_sessions(LUser, LServer),
|
2013-03-14 10:33:02 +01:00
|
|
|
if length(SIDs) =< MaxSessions -> ok;
|
|
|
|
true -> {_, Pid} = lists:min(SIDs), 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
|
2006-11-23 07:34:05 +01:00
|
|
|
get_max_user_sessions(LUser, Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case acl:match_rule(Host, max_user_sessions,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(LUser, Host, <<"">>))
|
2013-03-14 10:33:02 +01:00
|
|
|
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
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
process_iq(From, To, Packet) ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{xmlns = XMLNS} ->
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
case ets:lookup(sm_iqtable, {XMLNS, Host}) of
|
|
|
|
[{_, Module, Function}] ->
|
|
|
|
ResIQ = Module:Function(From, To, IQ),
|
|
|
|
if ResIQ /= ignore ->
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ));
|
|
|
|
true -> ok
|
|
|
|
end;
|
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:handle(Host, Module, Function, Opts,
|
|
|
|
From, To, IQ);
|
|
|
|
[] ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
|
|
|
reply -> ok;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_BAD_REQUEST),
|
|
|
|
ejabberd_router:route(To, From, Err),
|
|
|
|
ok
|
2003-01-01 20:54:44 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-spec force_update_presence({binary(), binary()}) -> any().
|
|
|
|
|
2015-03-09 12:57:33 +01:00
|
|
|
force_update_presence({LUser, LServer}) ->
|
|
|
|
Mod = get_sm_backend(),
|
|
|
|
Ss = Mod:get_sessions(LUser, LServer),
|
|
|
|
lists:foreach(fun (#session{sid = {_, Pid}}) ->
|
2015-08-20 18:15:16 +02:00
|
|
|
Pid ! {force_update_presence, LUser, LServer}
|
2015-03-09 12:57:33 +01:00
|
|
|
end,
|
|
|
|
Ss).
|
|
|
|
|
|
|
|
-spec get_sm_backend() -> module().
|
|
|
|
|
|
|
|
get_sm_backend() ->
|
|
|
|
DBType = ejabberd_config:get_option(sm_db_type,
|
|
|
|
fun(mnesia) -> mnesia;
|
2015-03-09 14:41:13 +01:00
|
|
|
(internal) -> mnesia;
|
2015-03-11 12:46:57 +01:00
|
|
|
(odbc) -> odbc;
|
|
|
|
(redis) -> redis
|
2015-03-09 12:57:33 +01:00
|
|
|
end, mnesia),
|
|
|
|
list_to_atom("ejabberd_sm_" ++ atom_to_list(DBType)).
|
2010-04-27 12:33:56 +02:00
|
|
|
|
2008-10-12 14:04:10 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%% ejabberd commands
|
|
|
|
|
|
|
|
commands() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#ejabberd_commands{name = connected_users,
|
|
|
|
tags = [session],
|
|
|
|
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 = [],
|
|
|
|
result = {connected_users, {list, {sessions, string}}}},
|
2008-10-12 14:04:10 +02:00
|
|
|
#ejabberd_commands{name = connected_users_number,
|
2013-03-14 10:33:02 +01:00
|
|
|
tags = [session, stats],
|
|
|
|
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,
|
|
|
|
args = [], result = {num_sessions, integer}},
|
2008-10-12 14:04:10 +02:00
|
|
|
#ejabberd_commands{name = user_resources,
|
2013-03-14 10:33:02 +01:00
|
|
|
tags = [session],
|
|
|
|
desc = "List user's connected resources",
|
2015-09-28 14:25:43 +02:00
|
|
|
policy = user,
|
2013-03-14 10:33:02 +01:00
|
|
|
module = ?MODULE, function = user_resources,
|
2015-09-28 14:25:43 +02:00
|
|
|
args = [],
|
2014-04-29 00:50:43 +02:00
|
|
|
result = {resources, {list, {resource, string}}}},
|
2014-05-12 12:44:40 +02:00
|
|
|
#ejabberd_commands{name = kick_user,
|
2014-04-29 00:50:43 +02:00
|
|
|
tags = [session],
|
|
|
|
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}],
|
|
|
|
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
|
|
|
|
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),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Resource) ->
|
|
|
|
PID = get_session_pid(User, Server, Resource),
|
2014-11-10 20:53:00 +01:00
|
|
|
PID ! kick
|
2014-04-29 00:50:43 +02:00
|
|
|
end, Resources),
|
|
|
|
length(Resources).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(sm_db_type) ->
|
|
|
|
fun (mnesia) -> mnesia;
|
|
|
|
(internal) -> mnesia;
|
|
|
|
(odbc) -> odbc;
|
|
|
|
(redis) -> redis
|
|
|
|
end;
|
|
|
|
opt_type(_) -> [sm_db_type].
|