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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2002-11-24 21:36:57 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_sm).
|
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
|
|
|
|
-export([start_link/0,
|
2004-11-05 22:14:31 +01:00
|
|
|
route/3,
|
2009-01-03 16:15:38 +01:00
|
|
|
open_session/3, close_session/2,
|
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,
|
2009-01-03 16:15:38 +01:00
|
|
|
set_presence/5,
|
|
|
|
unset_presence/4,
|
|
|
|
close_session_unset_presence/3,
|
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,
|
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,
|
2008-10-13 12:11:19 +02:00
|
|
|
connected_users/0,
|
|
|
|
connected_users_number/0,
|
|
|
|
user_resources/2,
|
2009-01-03 16:15:38 +01:00
|
|
|
get_session_pid/1,
|
2008-02-15 17:35:32 +01:00
|
|
|
get_user_info/3,
|
2009-01-03 16:15:38 +01:00
|
|
|
get_user_ip/1
|
2003-01-29 18:12:23 +01:00
|
|
|
]).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
2008-07-01 09:49:57 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2008-10-13 12:11:19 +02:00
|
|
|
-include("ejabberd_commands.hrl").
|
2009-03-24 19:02:13 +01:00
|
|
|
-include("mod_privacy.hrl").
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2007-05-21 05:21:34 +02:00
|
|
|
-record(session, {sid, usr, us, priority, info}).
|
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
|
|
|
|
2008-07-01 09:49:57 +02:00
|
|
|
% These are the namespace already declared by the stream opening. This is
|
|
|
|
% used at serialization time.
|
|
|
|
-define(DEFAULT_NS, ?NS_JABBER_CLIENT).
|
|
|
|
-define(PREFIXED_NS, [
|
|
|
|
{?NS_XMPP, ?NS_XMPP_pfx}, {?NS_DIALBACK, ?NS_DIALBACK_pfx}
|
|
|
|
]).
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
|
|
|
|
-define(IS_BINARY_OR_UNDEF(X),
|
|
|
|
(is_binary(X) orelse X == 'undefined')).
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2006-01-29 05:38:31 +01:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
2008-07-01 17:51:34 +02:00
|
|
|
route(FromOld, ToOld, #xmlelement{} = PacketOld) ->
|
|
|
|
catch throw(for_stacktrace), % To have a stacktrace.
|
|
|
|
io:format("~nSM: old #xmlelement:~n~p~n~p~n~n",
|
|
|
|
[PacketOld, erlang:get_stacktrace()]),
|
2008-07-01 09:49:57 +02:00
|
|
|
% XXX OLD FORMAT: From, To, Packet.
|
|
|
|
From = jlib:from_old_jid(FromOld),
|
|
|
|
To = jlib:from_old_jid(ToOld),
|
2008-07-01 17:51:34 +02:00
|
|
|
Packet = exmpp_xml:xmlelement_to_xmlel(PacketOld, [?NS_JABBER_CLIENT],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
route(From, To, Packet);
|
|
|
|
route(From, To, Packet) ->
|
2006-01-29 05:38:31 +01:00
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
open_session(SID, JID, Info) when ?IS_JID(JID) ->
|
|
|
|
set_session(SID, JID, undefined, Info),
|
|
|
|
check_for_sessions_to_replace(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
ejabberd_hooks:run(sm_register_connection_hook, exmpp_jid:prep_domain(JID),
|
2008-08-26 15:38:49 +02:00
|
|
|
[SID, JID, Info]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
close_session(SID, JID ) when ?IS_JID(JID)->
|
2008-02-15 17:35:32 +01:00
|
|
|
Info = case mnesia:dirty_read({session, SID}) of
|
|
|
|
[] -> [];
|
|
|
|
[#session{info=I}] -> I
|
|
|
|
end,
|
2006-01-29 05:38:31 +01:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({session, SID})
|
2007-12-01 06:16:30 +01:00
|
|
|
end,
|
2006-04-07 02:39:24 +02:00
|
|
|
mnesia:sync_dirty(F),
|
2009-06-01 18:37:15 +02:00
|
|
|
ejabberd_hooks:run(sm_remove_connection_hook, exmpp_jid:prep_domain(JID),
|
2008-08-26 15:38:49 +02:00
|
|
|
[SID, JID, Info]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
check_in_subscription(Acc, User, Server, _JID, _Type, _Reason)
|
|
|
|
when is_binary(User), is_binary(Server)->
|
|
|
|
case ejabberd_auth:is_user_exists(binary_to_list(User), binary_to_list(Server)) of
|
2007-05-03 07:15:12 +02:00
|
|
|
true ->
|
|
|
|
Acc;
|
|
|
|
false ->
|
|
|
|
{stop, false}
|
|
|
|
end.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
bounce_offline_message(From, To, Packet) ->
|
2008-07-01 09:49:57 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(Packet, 'service-unavailable'),
|
2006-01-29 05:38:31 +01:00
|
|
|
ejabberd_router:route(To, From, Err),
|
|
|
|
stop.
|
|
|
|
|
|
|
|
disconnect_removed_user(User, Server) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
ejabberd_sm:route(exmpp_jid:make(),
|
|
|
|
exmpp_jid:make(User,
|
2009-01-03 16:15:38 +01:00
|
|
|
Server),
|
2008-07-01 09:49:57 +02:00
|
|
|
#xmlel{name = 'broadcast',
|
|
|
|
children = [{exit, "User removed"}]}).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_user_resources(User, Server)
|
|
|
|
when is_binary(User), is_binary(Server) ->
|
|
|
|
US = {User, Server},
|
2006-01-29 05:38:31 +01:00
|
|
|
case catch mnesia:dirty_index_read(session, US, #session.us) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
[];
|
|
|
|
Ss ->
|
2009-02-18 19:03:11 +01:00
|
|
|
[element(3, S#session.usr) || S <- clean_session_list(Ss)]
|
2006-01-29 05:38:31 +01:00
|
|
|
end.
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_user_ip(JID) when ?IS_JID(JID) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
USR = {exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:42:07 +02:00
|
|
|
exmpp_jid:prep_resource(JID)},
|
2007-05-21 05:21:34 +02:00
|
|
|
case mnesia:dirty_index_read(session, USR, #session.usr) of
|
|
|
|
[] ->
|
|
|
|
undefined;
|
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
proplists:get_value(ip, Session#session.info)
|
|
|
|
end.
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_user_info(User, Server, Resource)
|
|
|
|
when is_binary(User),
|
|
|
|
is_binary(Server),
|
|
|
|
is_binary(Resource) ->
|
2008-07-01 09:49:57 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
LResource = exmpp_stringprep:resourceprep(Resource),
|
2009-02-18 19:03:11 +01:00
|
|
|
USR = {LUser,
|
|
|
|
LServer,
|
|
|
|
LResource},
|
2008-02-15 17:35:32 +01:00
|
|
|
case mnesia:dirty_index_read(session, USR, #session.usr) of
|
|
|
|
[] ->
|
|
|
|
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
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
set_presence(SID, JID, Priority, Presence, Info) when ?IS_JID(JID) ->
|
|
|
|
set_session(SID, JID, Priority, Info),
|
|
|
|
ejabberd_hooks:run(set_presence_hook,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:42:07 +02:00
|
|
|
exmpp_jid:prep_resource(JID),
|
2009-01-03 16:15:38 +01:00
|
|
|
Presence]).
|
|
|
|
|
|
|
|
unset_presence(SID, JID, Status, Info) when ?IS_JID(JID)->
|
|
|
|
set_session(SID, JID, undefined, Info),
|
|
|
|
ejabberd_hooks:run(unset_presence_hook,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:42:07 +02:00
|
|
|
exmpp_jid:prep_resource(JID),
|
2009-01-03 16:15:38 +01:00
|
|
|
Status]).
|
|
|
|
|
|
|
|
close_session_unset_presence(SID, JID, Status) when ?IS_JID(JID) ->
|
|
|
|
close_session(SID, JID),
|
|
|
|
ejabberd_hooks:run(unset_presence_hook,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:42:07 +02:00
|
|
|
exmpp_jid:prep_resource(JID),
|
2009-01-03 16:15:38 +01:00
|
|
|
Status]).
|
|
|
|
|
|
|
|
get_session_pid(JID) when ?IS_JID(JID) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
USR = {exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:42:07 +02:00
|
|
|
exmpp_jid:prep_resource(JID)},
|
2007-12-01 06:16:30 +01:00
|
|
|
case catch mnesia:dirty_index_read(session, USR, #session.usr) of
|
|
|
|
[#session{sid = {_, Pid}}] -> Pid;
|
|
|
|
_ -> none
|
|
|
|
end.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
dirty_get_sessions_list() ->
|
|
|
|
mnesia:dirty_select(
|
|
|
|
session,
|
|
|
|
[{#session{usr = '$1', _ = '_'},
|
|
|
|
[],
|
|
|
|
['$1']}]).
|
|
|
|
|
|
|
|
dirty_get_my_sessions_list() ->
|
|
|
|
mnesia:dirty_select(
|
|
|
|
session,
|
|
|
|
[{#session{sid = {'_', '$1'}, _ = '_'},
|
|
|
|
[{'==', {node, '$1'}, node()}],
|
|
|
|
['$_']}]).
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_vh_session_list(Server) when is_binary(Server) ->
|
2009-01-22 16:52:30 +01:00
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2006-01-29 05:38:31 +01:00
|
|
|
mnesia:dirty_select(
|
|
|
|
session,
|
|
|
|
[{#session{usr = '$1', _ = '_'},
|
|
|
|
[{'==', {element, 2, '$1'}, LServer}],
|
|
|
|
['$1']}]).
|
|
|
|
|
|
|
|
register_iq_handler(Host, XMLNS, Module, Fun) ->
|
|
|
|
ejabberd_sm ! {register_iq_handler, Host, XMLNS, Module, Fun}.
|
|
|
|
|
|
|
|
register_iq_handler(Host, XMLNS, Module, Fun, Opts) ->
|
|
|
|
ejabberd_sm ! {register_iq_handler, Host, XMLNS, Module, Fun, Opts}.
|
|
|
|
|
|
|
|
unregister_iq_handler(Host, XMLNS) ->
|
|
|
|
ejabberd_sm ! {unregister_iq_handler, Host, XMLNS}.
|
|
|
|
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([]) ->
|
2003-12-14 21:51:01 +01:00
|
|
|
update_tables(),
|
2006-01-24 00:13:06 +01:00
|
|
|
mnesia:create_table(session,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, session)}]),
|
|
|
|
mnesia:add_table_index(session, usr),
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:add_table_index(session, us),
|
2003-12-14 21:51:01 +01:00
|
|
|
mnesia:add_table_copy(session, node(), ram_copies),
|
2002-11-24 21:36:57 +01:00
|
|
|
mnesia:subscribe(system),
|
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) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:add(roster_in_subscription, HostB,
|
2007-05-03 07:15:12 +02:00
|
|
|
ejabberd_sm, check_in_subscription, 20),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(offline_message_hook, HostB,
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_sm, bounce_offline_message, 100),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_sm, disconnect_removed_user, 100)
|
|
|
|
end, ?MYHOSTS),
|
2008-10-13 12:11:19 +02:00
|
|
|
ejabberd_commands:register_commands(commands()),
|
2006-05-21 18:34:32 +02:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling call messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
2008-07-01 17:51:34 +02:00
|
|
|
handle_info({route, FromOld, ToOld, #xmlelement{} = PacketOld}, State) ->
|
|
|
|
catch throw(for_stacktrace), % To have a stacktrace.
|
|
|
|
io:format("~nSM: old #xmlelement:~n~p~n~p~n~n",
|
|
|
|
[PacketOld, erlang:get_stacktrace()]),
|
2008-07-01 09:49:57 +02:00
|
|
|
% XXX OLD FORMAT: From, To, Packet.
|
|
|
|
From = jlib:from_old_jid(FromOld),
|
|
|
|
To = jlib:from_old_jid(ToOld),
|
2008-07-01 17:51:34 +02:00
|
|
|
Packet = exmpp_xml:xmlelement_to_xmlel(PacketOld, [?NS_JABBER_CLIENT],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
handle_info({route, From, To, Packet}, State);
|
|
|
|
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({mnesia_system_event, {mnesia_down, Node}}, State) ->
|
|
|
|
clean_table_from_bad_node(Node),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({register_iq_handler, Host, XMLNS, Module, Function}, State) ->
|
|
|
|
ets:insert(sm_iqtable, {{XMLNS, Host}, Module, Function}),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({register_iq_handler, Host, XMLNS, Module, Function, Opts}, State) ->
|
|
|
|
ets:insert(sm_iqtable, {{XMLNS, Host}, Module, Function, Opts}),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({unregister_iq_handler, Host, XMLNS}, State) ->
|
|
|
|
case ets:lookup(sm_iqtable, {XMLNS, Host}) of
|
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:stop_iq_handler(Module, Function, Opts);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ets:delete(sm_iqtable, {XMLNS, Host}),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: terminate(Reason, State) -> void()
|
|
|
|
%% Description: This function is called by a gen_server when it is about to
|
|
|
|
%% terminate. It should be the opposite of Module:init/1 and do any necessary
|
|
|
|
%% cleaning up. When it returns, the gen_server terminates with Reason.
|
|
|
|
%% The return value is ignored.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
terminate(_Reason, _State) ->
|
2008-10-13 12:11:19 +02:00
|
|
|
ejabberd_commands:unregister_commands(commands()),
|
2006-01-29 05:38:31 +01:00
|
|
|
ok.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
set_session(SID, JID, Priority, Info) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
US = {exmpp_jid:prep_node(JID), exmpp_jid:prep_domain(JID)},
|
|
|
|
USR = {exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:42:07 +02:00
|
|
|
exmpp_jid:prep_resource(JID)},
|
2003-09-19 20:22:44 +02:00
|
|
|
F = fun() ->
|
2006-01-24 00:13:06 +01:00
|
|
|
mnesia:write(#session{sid = SID,
|
|
|
|
usr = USR,
|
|
|
|
us = US,
|
2007-05-21 05:21:34 +02:00
|
|
|
priority = Priority,
|
|
|
|
info = Info})
|
2006-01-24 00:13:06 +01:00
|
|
|
end,
|
2006-05-21 18:34:32 +02:00
|
|
|
mnesia:sync_dirty(F).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
|
|
|
clean_table_from_bad_node(Node) ->
|
|
|
|
F = fun() ->
|
2003-12-14 21:51:01 +01:00
|
|
|
Es = mnesia:select(
|
|
|
|
session,
|
2006-01-24 00:13:06 +01:00
|
|
|
[{#session{sid = {'_', '$1'}, _ = '_'},
|
2003-12-14 21:51:01 +01:00
|
|
|
[{'==', {node, '$1'}, Node}],
|
|
|
|
['$_']}]),
|
2002-11-24 21:36:57 +01:00
|
|
|
lists:foreach(fun(E) ->
|
2006-03-16 10:59:45 +01:00
|
|
|
mnesia:delete({session, E#session.sid})
|
2002-11-24 21:36:57 +01:00
|
|
|
end, Es)
|
2007-12-01 06:16:30 +01:00
|
|
|
end,
|
2006-01-24 00:13:06 +01:00
|
|
|
mnesia:sync_dirty(F).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
do_route(From, To, Packet) ->
|
|
|
|
?DEBUG("session manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n",
|
|
|
|
[From, To, Packet, 8]),
|
2009-06-01 18:42:07 +02:00
|
|
|
case exmpp_jid:prep_resource(To) of
|
2008-07-01 09:49:57 +02:00
|
|
|
undefined ->
|
|
|
|
case Packet of
|
|
|
|
_ when ?IS_PRESENCE(Packet) ->
|
2007-12-07 00:15:04 +01:00
|
|
|
{Pass, _Subsc} =
|
2008-07-01 09:49:57 +02:00
|
|
|
case exmpp_presence:get_type(Packet) of
|
|
|
|
'subscribe' ->
|
2009-03-24 19:02:13 +01:00
|
|
|
Reason = exmpp_presence:get_status(Packet),
|
|
|
|
{is_privacy_allow(From, To, Packet) andalso
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
roster_in_subscription,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(To),
|
2009-03-24 19:02:13 +01:00
|
|
|
false,
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To), From, subscribe, Reason]),
|
2009-03-24 19:02:13 +01:00
|
|
|
true};
|
2008-07-01 09:49:57 +02:00
|
|
|
'subscribed' ->
|
2009-03-24 19:02:13 +01:00
|
|
|
{is_privacy_allow(From, To, Packet) andalso
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
roster_in_subscription,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(To),
|
2009-03-24 19:02:13 +01:00
|
|
|
false,
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To), From, subscribed, <<>>]),
|
2009-03-24 19:02:13 +01:00
|
|
|
true};
|
2008-07-01 09:49:57 +02:00
|
|
|
'unsubscribe' ->
|
2009-03-24 19:02:13 +01:00
|
|
|
{is_privacy_allow(From, To, Packet) andalso
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
roster_in_subscription,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(To),
|
2009-03-24 19:02:13 +01:00
|
|
|
false,
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To), From, unsubscribe, <<>>]),
|
2009-03-24 19:02:13 +01:00
|
|
|
true};
|
2008-07-01 09:49:57 +02:00
|
|
|
'unsubscribed' ->
|
2009-03-24 19:02:13 +01:00
|
|
|
{is_privacy_allow(From, To, Packet) andalso
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
roster_in_subscription,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(To),
|
2009-03-24 19:02:13 +01:00
|
|
|
false,
|
2009-06-01 18:39:36 +02:00
|
|
|
[exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To), From, unsubscribed, <<>>]),
|
2009-03-24 19:02:13 +01:00
|
|
|
true};
|
2003-01-17 20:58:42 +01:00
|
|
|
_ ->
|
|
|
|
{true, false}
|
|
|
|
end,
|
2002-12-20 21:42:08 +01:00
|
|
|
if Pass ->
|
2005-04-17 20:08:34 +02:00
|
|
|
PResources = get_user_present_resources(
|
2009-06-01 18:39:36 +02:00
|
|
|
exmpp_jid:prep_node(To), exmpp_jid:prep_domain(To)),
|
2006-05-23 22:19:37 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({_, R}) ->
|
2007-08-24 05:54:42 +02:00
|
|
|
do_route(
|
|
|
|
From,
|
2009-06-01 18:34:38 +02:00
|
|
|
exmpp_jid:full(To, R),
|
2007-08-24 05:54:42 +02:00
|
|
|
Packet)
|
2007-12-22 01:04:05 +01:00
|
|
|
end, PResources);
|
2002-12-20 21:42:08 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end;
|
2008-07-01 09:49:57 +02:00
|
|
|
_ when ?IS_MESSAGE(Packet) ->
|
2003-01-05 21:24:59 +01:00
|
|
|
route_message(From, To, Packet);
|
2008-07-01 09:49:57 +02:00
|
|
|
_ when ?IS_IQ(Packet) ->
|
2003-01-05 21:24:59 +01:00
|
|
|
process_iq(From, To, Packet);
|
2008-07-01 09:49:57 +02:00
|
|
|
#xmlel{name = 'broadcast'} ->
|
2002-12-22 21:56:29 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
2004-12-15 00:51:21 +01:00
|
|
|
do_route(From,
|
2009-06-01 18:34:38 +02:00
|
|
|
exmpp_jid:full(To, R),
|
2004-12-15 00:51:21 +01:00
|
|
|
Packet)
|
2009-06-01 18:39:36 +02:00
|
|
|
end, get_user_resources(exmpp_jid:prep_node(To),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(To)));
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
2009-06-01 18:39:36 +02:00
|
|
|
USR = {exmpp_jid:prep_node(To),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(To),
|
2009-06-01 18:42:07 +02:00
|
|
|
exmpp_jid:prep_resource(To)},
|
2006-01-24 00:13:06 +01:00
|
|
|
case mnesia:dirty_index_read(session, USR, #session.usr) of
|
2003-01-15 20:54:44 +01:00
|
|
|
[] ->
|
2008-07-01 09:49:57 +02:00
|
|
|
case Packet of
|
|
|
|
_ when ?IS_MESSAGE(Packet) ->
|
2003-01-05 21:24:59 +01:00
|
|
|
route_message(From, To, Packet);
|
2008-07-01 09:49:57 +02:00
|
|
|
_ when ?IS_IQ(Packet) ->
|
|
|
|
case exmpp_iq:get_type(Packet) of
|
|
|
|
'error' -> ok;
|
|
|
|
'result' -> ok;
|
2003-10-28 15:00:45 +01:00
|
|
|
_ ->
|
|
|
|
Err =
|
2008-07-01 09:49:57 +02:00
|
|
|
exmpp_iq:error(Packet,
|
|
|
|
'service-unavailable'),
|
2003-10-28 15:00:45 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
2003-10-07 22:31:44 +02:00
|
|
|
_ ->
|
2003-01-05 21:24:59 +01:00
|
|
|
?DEBUG("packet droped~n", [])
|
2003-01-15 20:54:44 +01:00
|
|
|
end;
|
2006-01-24 00:13:06 +01:00
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
Pid = element(2, Session#session.sid),
|
2003-12-14 21:51:01 +01:00
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
2008-08-26 15:38:49 +02:00
|
|
|
Pid ! {route, From, To, Packet}
|
2002-12-17 21:49:45 +01:00
|
|
|
end
|
2002-11-30 19:46:16 +01:00
|
|
|
end.
|
|
|
|
|
2009-03-24 19:02:13 +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) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
User = exmpp_jid:prep_node(To),
|
2009-06-01 18:37:15 +02:00
|
|
|
Server = exmpp_jid:prep_domain(To),
|
2009-03-24 19:02:13 +01:00
|
|
|
PrivacyList = ejabberd_hooks:run_fold(privacy_get_user_list, Server,
|
|
|
|
#userlist{}, [User, Server]),
|
|
|
|
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) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
User = exmpp_jid:prep_node(To),
|
2009-06-01 18:37:15 +02:00
|
|
|
Server = exmpp_jid:prep_domain(To),
|
2009-03-24 19:02:13 +01:00
|
|
|
allow == ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, Server,
|
|
|
|
allow,
|
|
|
|
[User,
|
|
|
|
Server,
|
|
|
|
PrivacyList,
|
|
|
|
{From, To, Packet},
|
|
|
|
in]).
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
route_message(From, To, Packet) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
LUser = exmpp_jid:prep_node(To),
|
2009-06-01 18:37:15 +02:00
|
|
|
LServer = exmpp_jid:prep_domain(To),
|
2006-04-23 11:31:54 +02:00
|
|
|
PrioRes = get_user_present_resources(LUser, LServer),
|
|
|
|
case catch lists:max(PrioRes) of
|
|
|
|
{Priority, _R} when is_integer(Priority), Priority >= 0 ->
|
|
|
|
lists:foreach(
|
|
|
|
%% Route messages to all priority that equals the max, if
|
|
|
|
%% positive
|
|
|
|
fun({P, R}) when P == Priority ->
|
2009-01-03 16:15:38 +01:00
|
|
|
USR = {LUser, LServer, R},
|
2006-04-23 11:31:54 +02:00
|
|
|
case mnesia:dirty_index_read(session, USR, #session.usr) of
|
|
|
|
[] ->
|
2007-12-07 00:15:04 +01:00
|
|
|
ok; % Race condition
|
2006-04-23 11:31:54 +02:00
|
|
|
Ss ->
|
|
|
|
Session = lists:max(Ss),
|
|
|
|
Pid = element(2, Session#session.sid),
|
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
2008-08-26 15:38:49 +02:00
|
|
|
Pid ! {route, From, To, Packet}
|
2006-04-23 11:31:54 +02:00
|
|
|
end;
|
|
|
|
%% Ignore other priority:
|
|
|
|
({_Prio, _Res}) ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
PrioRes);
|
2004-10-08 22:40:29 +02:00
|
|
|
_ ->
|
2008-07-01 09:49:57 +02:00
|
|
|
case exmpp_message:get_type(Packet) of
|
|
|
|
'error' ->
|
2003-11-23 21:11:21 +01:00
|
|
|
ok;
|
2008-07-01 09:49:57 +02:00
|
|
|
'groupchat' ->
|
2007-04-12 07:31:53 +02:00
|
|
|
bounce_offline_message(From, To, Packet);
|
2008-07-01 09:49:57 +02:00
|
|
|
'headline' ->
|
2007-04-12 07:31:53 +02:00
|
|
|
bounce_offline_message(From, To, Packet);
|
2003-11-23 21:11:21 +01:00
|
|
|
_ ->
|
2009-06-01 18:40:51 +02:00
|
|
|
case ejabberd_auth:is_user_exists(exmpp_jid:prep_node_as_list(To),
|
2009-06-01 18:38:28 +02:00
|
|
|
exmpp_jid:prep_domain_as_list(To)) of
|
2003-11-23 21:11:21 +01:00
|
|
|
true ->
|
2004-08-08 21:07:55 +02:00
|
|
|
ejabberd_hooks:run(offline_message_hook,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(To),
|
2008-08-26 15:38:49 +02:00
|
|
|
[From, To, Packet]);
|
2003-11-23 21:11:21 +01:00
|
|
|
_ ->
|
2008-07-01 09:49:57 +02:00
|
|
|
Err = exmpp_stanza:reply_with_error(
|
|
|
|
Packet, 'service-unaivailable'),
|
2003-11-23 21:11:21 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
2004-01-02 20:36:38 +01:00
|
|
|
end
|
2003-01-05 21:24:59 +01:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
|
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), []).
|
|
|
|
|
|
|
|
clean_session_list([], Res) ->
|
|
|
|
Res;
|
|
|
|
clean_session_list([S], Res) ->
|
|
|
|
[S | Res];
|
|
|
|
clean_session_list([S1, S2 | Rest], Res) ->
|
|
|
|
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.
|
|
|
|
|
2003-01-19 21:17:56 +01:00
|
|
|
|
2002-12-30 21:25:08 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_user_present_resources(LUser, LServer) ->
|
|
|
|
US = {LUser, LServer},
|
2006-01-24 00:13:06 +01:00
|
|
|
case catch mnesia:dirty_index_read(session, US, #session.us) of
|
2003-10-24 21:21:07 +02:00
|
|
|
{'EXIT', _Reason} ->
|
2003-01-19 21:17:56 +01:00
|
|
|
[];
|
2006-01-24 00:13:06 +01:00
|
|
|
Ss ->
|
|
|
|
[{S#session.priority, element(3, S#session.usr)} ||
|
|
|
|
S <- clean_session_list(Ss), is_integer(S#session.priority)]
|
2002-12-30 21:25:08 +01:00
|
|
|
end.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2006-05-21 18:34:32 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
%% On new session, check if some existing connections need to be replace
|
2009-01-03 16:15:38 +01:00
|
|
|
check_for_sessions_to_replace(JID) ->
|
2006-05-21 18:34:32 +02:00
|
|
|
%% TODO: Depending on how this is executed, there could be an unneeded
|
|
|
|
%% replacement for max_sessions. We need to check this at some point.
|
2009-01-03 16:15:38 +01:00
|
|
|
check_existing_resources(JID),
|
|
|
|
check_max_sessions(JID).
|
2006-05-21 18:34:32 +02:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
check_existing_resources(JID) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
USR = {exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID),
|
2009-06-01 18:42:07 +02:00
|
|
|
exmpp_jid:prep_resource(JID)},
|
2006-05-21 18:34:32 +02:00
|
|
|
%% A connection exist with the same resource. We replace it:
|
|
|
|
SIDs = mnesia:dirty_select(
|
|
|
|
session,
|
|
|
|
[{#session{sid = '$1', usr = USR, _ = '_'}, [], ['$1']}]),
|
|
|
|
if
|
|
|
|
SIDs == [] -> ok;
|
|
|
|
true ->
|
|
|
|
MaxSID = lists:max(SIDs),
|
|
|
|
lists:foreach(
|
|
|
|
fun({_, Pid} = S) when S /= MaxSID ->
|
|
|
|
Pid ! replaced;
|
|
|
|
(_) -> ok
|
|
|
|
end, SIDs)
|
|
|
|
end.
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
check_max_sessions(JID) ->
|
2006-05-21 18:34:32 +02:00
|
|
|
%% If the max number of sessions for a given is reached, we replace the
|
|
|
|
%% first one
|
2007-11-25 16:35:20 +01:00
|
|
|
SIDs = mnesia:dirty_select(
|
|
|
|
session,
|
2009-01-03 16:15:38 +01:00
|
|
|
[{#session{sid = '$1',
|
2009-06-01 18:39:36 +02:00
|
|
|
us = {exmpp_jid:prep_node(JID), exmpp_jid:prep_domain(JID)},
|
2009-01-03 16:15:38 +01:00
|
|
|
_ = '_'}, [],
|
2007-12-07 21:57:44 +01:00
|
|
|
['$1']}]),
|
2009-01-03 16:15:38 +01:00
|
|
|
MaxSessions = get_max_user_sessions(JID),
|
2007-11-25 16:35:20 +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
|
2009-01-03 16:15:38 +01:00
|
|
|
get_max_user_sessions(JID) ->
|
2006-11-23 07:34:05 +01:00
|
|
|
case acl:match_rule(
|
2009-06-01 18:38:28 +02:00
|
|
|
exmpp_jid:prep_domain_as_list(JID), max_user_sessions, exmpp_jid:bare(JID)) of
|
2006-11-23 07:34:05 +01:00
|
|
|
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) ->
|
2008-08-14 15:34:30 +02:00
|
|
|
case exmpp_iq:xmlel_to_iq(Packet) of
|
|
|
|
#iq{kind = request, ns = XMLNS} = IQ_Rec ->
|
2009-06-01 18:37:15 +02:00
|
|
|
LServer = exmpp_jid:prep_domain(To),
|
2009-01-03 16:15:38 +01:00
|
|
|
case ets:lookup(sm_iqtable, {XMLNS, LServer}) of
|
2003-01-01 20:54:44 +01:00
|
|
|
[{_, Module, Function}] ->
|
2008-08-14 15:34:30 +02:00
|
|
|
ResIQ = Module:Function(From, To, IQ_Rec),
|
2003-01-01 20:54:44 +01:00
|
|
|
if
|
|
|
|
ResIQ /= ignore ->
|
2008-08-14 15:34:30 +02:00
|
|
|
Reply = exmpp_iq:iq_to_xmlel(ResIQ, To, From),
|
2008-07-01 09:49:57 +02:00
|
|
|
ejabberd_router:route(To, From, Reply);
|
2003-01-01 20:54:44 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end;
|
2003-01-22 21:40:40 +01:00
|
|
|
[{_, Module, Function, Opts}] ->
|
2009-01-03 16:15:38 +01:00
|
|
|
gen_iq_handler:handle(LServer,
|
|
|
|
Module, Function, Opts, From, To, IQ_Rec);
|
2003-01-01 20:54:44 +01:00
|
|
|
[] ->
|
2008-07-01 09:49:57 +02:00
|
|
|
Err = exmpp_iq:error(Packet, 'service-unavailable'),
|
2004-12-15 00:51:21 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
2003-01-01 20:54:44 +01:00
|
|
|
end;
|
2008-10-06 16:56:36 +02:00
|
|
|
#iq{kind = response} ->
|
2003-01-01 20:54:44 +01:00
|
|
|
ok;
|
|
|
|
_ ->
|
2008-07-01 09:49:57 +02:00
|
|
|
Err = exmpp_iq:error(Packet, 'bad-request'),
|
2003-12-17 21:13:21 +01:00
|
|
|
ejabberd_router:route(To, From, Err),
|
2003-01-01 20:54:44 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2003-12-14 21:51:01 +01:00
|
|
|
|
2008-10-13 12:11:19 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%% ejabberd commands
|
|
|
|
|
|
|
|
commands() ->
|
|
|
|
[
|
|
|
|
#ejabberd_commands{name = connected_users,
|
|
|
|
tags = [session],
|
|
|
|
desc = "List all established sessions",
|
|
|
|
module = ?MODULE, function = connected_users,
|
|
|
|
args = [],
|
|
|
|
result = {connected_users, {list, {sessions, string}}}},
|
|
|
|
#ejabberd_commands{name = connected_users_number,
|
|
|
|
tags = [session, stats],
|
|
|
|
desc = "Get the number of established sessions",
|
|
|
|
module = ?MODULE, function = connected_users_number,
|
|
|
|
args = [],
|
|
|
|
result = {num_sessions, integer}},
|
|
|
|
#ejabberd_commands{name = user_resources,
|
|
|
|
tags = [session],
|
|
|
|
desc = "List user's connected resources",
|
|
|
|
module = ?MODULE, function = user_resources,
|
|
|
|
args = [{user, string}, {host, string}],
|
|
|
|
result = {resources, {list, {resource, string}}}}
|
|
|
|
].
|
|
|
|
|
|
|
|
connected_users() ->
|
2006-02-15 05:15:54 +01:00
|
|
|
USRs = dirty_get_sessions_list(),
|
|
|
|
SUSRs = lists:sort(USRs),
|
2008-10-13 12:11:19 +02:00
|
|
|
lists:map(fun({U, S, R}) -> [U, $@, S, $/, R] end, SUSRs).
|
|
|
|
|
|
|
|
connected_users_number() ->
|
|
|
|
length(dirty_get_sessions_list()).
|
|
|
|
|
|
|
|
user_resources(User, Server) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
Resources = get_user_resources(list_to_binary(User),
|
|
|
|
list_to_binary(Server)),
|
2008-10-13 12:11:19 +02:00
|
|
|
lists:sort(Resources).
|
2006-02-15 05:15:54 +01:00
|
|
|
|
2003-12-14 21:51:01 +01:00
|
|
|
|
2008-10-13 12:11:19 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
%%% Update Mnesia tables
|
|
|
|
|
2003-12-14 21:51:01 +01:00
|
|
|
update_tables() ->
|
|
|
|
case catch mnesia:table_info(session, attributes) of
|
|
|
|
[ur, user, node] ->
|
|
|
|
mnesia:delete_table(session);
|
|
|
|
[ur, user, pid] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete_table(session);
|
|
|
|
[usr, us, pid] ->
|
2006-01-24 00:13:06 +01:00
|
|
|
mnesia:delete_table(session);
|
|
|
|
[sid, usr, us, priority] ->
|
2007-05-21 05:21:34 +02:00
|
|
|
mnesia:delete_table(session);
|
|
|
|
[sid, usr, us, priority, info] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
ok;
|
|
|
|
{'EXIT', _} ->
|
|
|
|
ok
|
|
|
|
end,
|
2006-01-24 00:13:06 +01:00
|
|
|
case lists:member(presence, mnesia:system_info(tables)) of
|
|
|
|
true ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete_table(presence);
|
2006-01-24 00:13:06 +01:00
|
|
|
false ->
|
2003-12-14 21:51:01 +01:00
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case lists:member(local_session, mnesia:system_info(tables)) of
|
|
|
|
true ->
|
|
|
|
mnesia:delete_table(local_session);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|