2002-11-24 21:36:57 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_sm.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 24 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_sm).
|
|
|
|
-author('alexey@sevcom.net').
|
2002-11-30 19:46:16 +01:00
|
|
|
-vsn('$Revision$ ').
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
-export([start_link/0, init/0, open_session/2, close_session/2,
|
2002-12-30 21:25:08 +01:00
|
|
|
get_user_resources/1,
|
|
|
|
set_presence/3,
|
2003-01-01 20:54:44 +01:00
|
|
|
unset_presence/2,
|
|
|
|
dirty_get_sessions_list/0,
|
2003-01-18 20:42:48 +01:00
|
|
|
dirty_get_my_sessions_list/0,
|
2003-01-22 21:40:40 +01:00
|
|
|
register_iq_handler/3,
|
2003-01-29 18:12:23 +01:00
|
|
|
register_iq_handler/4,
|
|
|
|
unregister_iq_handler/1
|
|
|
|
]).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
|
|
|
-include_lib("mnemosyne/include/mnemosyne.hrl").
|
2002-11-30 19:46:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
-record(session, {ur, user, node}).
|
2003-01-21 21:36:55 +01:00
|
|
|
-record(local_session, {ur, pid}).
|
2002-12-30 21:25:08 +01:00
|
|
|
-record(presence, {ur, user, priority}).
|
2002-11-24 21:36:57 +01:00
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
|
|
|
{ok, proc_lib:spawn_link(ejabberd_sm, init, [])}.
|
2002-11-24 21:36:57 +01:00
|
|
|
|
|
|
|
init() ->
|
|
|
|
register(ejabberd_sm, self()),
|
2002-12-30 21:25:08 +01:00
|
|
|
mnesia:create_table(session, [{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, session)}]),
|
2002-12-17 21:49:45 +01:00
|
|
|
mnesia:add_table_index(session, user),
|
2002-11-24 21:36:57 +01:00
|
|
|
mnesia:add_table_index(session, node),
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:create_table(local_session,
|
2002-11-24 21:36:57 +01:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{local_content, true},
|
2003-01-21 21:36:55 +01:00
|
|
|
{attributes, record_info(fields, local_session)}]),
|
|
|
|
mnesia:add_table_copy(local_session, node(), ram_copies),
|
2002-12-30 21:25:08 +01:00
|
|
|
mnesia:create_table(presence,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, presence)}]),
|
|
|
|
mnesia:add_table_index(presence, user),
|
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]),
|
2002-11-24 21:36:57 +01:00
|
|
|
loop().
|
|
|
|
|
|
|
|
loop() ->
|
|
|
|
receive
|
2003-01-15 20:54:44 +01:00
|
|
|
{route, From, To, Packet} ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
loop();
|
2002-11-24 21:36:57 +01:00
|
|
|
{open_session, User, Resource, From} ->
|
|
|
|
replace_and_register_my_connection(User, Resource, From),
|
|
|
|
replace_alien_connection(User, Resource),
|
|
|
|
loop();
|
2002-11-30 19:46:16 +01:00
|
|
|
{close_session, User, Resource} ->
|
|
|
|
remove_connection(User, Resource),
|
|
|
|
loop();
|
2002-11-24 21:36:57 +01:00
|
|
|
{replace, User, Resource} ->
|
|
|
|
replace_my_connection(User, Resource),
|
|
|
|
loop();
|
|
|
|
{mnesia_system_event, {mnesia_down, Node}} ->
|
|
|
|
clean_table_from_bad_node(Node),
|
|
|
|
loop();
|
2003-01-01 20:54:44 +01:00
|
|
|
{register_iq_handler, XMLNS, Module, Function} ->
|
|
|
|
ets:insert(sm_iqtable, {XMLNS, Module, Function}),
|
|
|
|
loop();
|
2003-01-22 21:40:40 +01:00
|
|
|
{register_iq_handler, XMLNS, Module, Function, Opts} ->
|
|
|
|
ets:insert(sm_iqtable, {XMLNS, Module, Function, Opts}),
|
|
|
|
loop();
|
2003-01-29 18:12:23 +01:00
|
|
|
{unregister_iq_handler, XMLNS} ->
|
|
|
|
case ets:lookup(sm_iqtable, XMLNS) of
|
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:stop_iq_handler(Module, Function, Opts);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ets:delete(sm_iqtable, XMLNS),
|
|
|
|
mod_disco:unregister_feature(XMLNS),
|
|
|
|
loop();
|
2002-11-24 21:36:57 +01:00
|
|
|
_ ->
|
|
|
|
loop()
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
open_session(User, Resource) ->
|
|
|
|
ejabberd_sm ! {open_session, User, Resource, self()}.
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
close_session(User, Resource) ->
|
|
|
|
ejabberd_sm ! {close_session, User, Resource}.
|
|
|
|
|
2002-11-24 21:36:57 +01:00
|
|
|
replace_alien_connection(User, Resource) ->
|
2002-12-30 21:25:08 +01:00
|
|
|
LUser = jlib:tolower(User),
|
2002-11-24 21:36:57 +01:00
|
|
|
F = fun() ->
|
2002-12-30 21:25:08 +01:00
|
|
|
UR = {LUser, Resource},
|
2002-12-17 21:49:45 +01:00
|
|
|
Es = mnesia:read({session, UR}),
|
2002-12-30 21:25:08 +01:00
|
|
|
mnesia:write(#session{ur = UR, user = LUser, node = node()}),
|
2002-11-24 21:36:57 +01:00
|
|
|
Es
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
if R#session.node /= node() ->
|
|
|
|
{ejabberd_sm, R#session.node} !
|
|
|
|
{replace, User, Resource};
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
replace_my_connection(User, Resource) ->
|
2002-12-30 21:25:08 +01:00
|
|
|
LUser = jlib:tolower(User),
|
2002-11-24 21:36:57 +01:00
|
|
|
F = fun() ->
|
2002-12-30 21:25:08 +01:00
|
|
|
UR = {LUser, Resource},
|
2003-01-21 21:36:55 +01:00
|
|
|
Es = mnesia:read({local_session, UR}),
|
|
|
|
mnesia:delete({local_session, UR}),
|
2002-11-24 21:36:57 +01:00
|
|
|
Es
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
2003-01-21 21:36:55 +01:00
|
|
|
R#local_session.pid ! replaced
|
2002-11-24 21:36:57 +01:00
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
remove_connection(User, Resource) ->
|
2002-12-30 21:25:08 +01:00
|
|
|
LUser = jlib:tolower(User),
|
2002-11-30 19:46:16 +01:00
|
|
|
F = fun() ->
|
2002-12-30 21:25:08 +01:00
|
|
|
UR = {LUser, Resource},
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:delete({local_session, UR}),
|
2002-12-17 21:49:45 +01:00
|
|
|
mnesia:delete({session, UR})
|
2002-11-30 19:46:16 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2002-11-24 21:36:57 +01:00
|
|
|
replace_and_register_my_connection(User, Resource, Pid) ->
|
2002-12-30 21:25:08 +01:00
|
|
|
LUser = jlib:tolower(User),
|
2002-11-24 21:36:57 +01:00
|
|
|
F = fun() ->
|
2002-12-30 21:25:08 +01:00
|
|
|
UR = {LUser, Resource},
|
2003-01-21 21:36:55 +01:00
|
|
|
Es = mnesia:read({local_session, UR}),
|
|
|
|
mnesia:write(#local_session{ur = UR, pid = Pid}),
|
2002-11-24 21:36:57 +01:00
|
|
|
Es
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
2003-01-21 21:36:55 +01:00
|
|
|
R#local_session.pid ! replaced
|
2002-11-24 21:36:57 +01:00
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
clean_table_from_bad_node(Node) ->
|
|
|
|
F = fun() ->
|
|
|
|
Es = mnesia:index_read(session, Node, #session.node),
|
|
|
|
lists:foreach(fun(E) ->
|
|
|
|
mnesia:delete_object(session, E, write),
|
|
|
|
mnesia:delete(
|
2002-12-17 21:49:45 +01:00
|
|
|
{user_resource, E#session.ur})
|
2002-11-24 21:36:57 +01:00
|
|
|
end, Es)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2003-01-15 20:54:44 +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]),
|
2002-12-17 21:49:45 +01:00
|
|
|
{User, Server, Resource} = To,
|
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
|
|
|
case Resource of
|
|
|
|
"" ->
|
2002-11-30 19:46:16 +01:00
|
|
|
% TODO
|
2002-12-17 21:49:45 +01:00
|
|
|
case Name of
|
|
|
|
"presence" ->
|
2002-12-20 21:42:08 +01:00
|
|
|
{FU, FS, FR} = From,
|
2003-01-17 20:58:42 +01:00
|
|
|
{Pass, Subsc} =
|
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"subscribe" ->
|
|
|
|
{mod_roster:in_subscription(User,
|
|
|
|
{FU, FS, ""},
|
|
|
|
subscribe),
|
|
|
|
true};
|
|
|
|
"subscribed" ->
|
|
|
|
{mod_roster:in_subscription(User,
|
|
|
|
{FU, FS, ""},
|
|
|
|
subscribed),
|
|
|
|
true};
|
|
|
|
"unsubscribe" ->
|
|
|
|
{mod_roster:in_subscription(User,
|
|
|
|
{FU, FS, ""},
|
|
|
|
unsubscribe),
|
|
|
|
true};
|
|
|
|
"unsubscribed" ->
|
|
|
|
{mod_roster:in_subscription(User,
|
|
|
|
{FU, FS, ""},
|
|
|
|
unsubscribed),
|
|
|
|
true};
|
|
|
|
_ ->
|
|
|
|
{true, false}
|
|
|
|
end,
|
2002-12-20 21:42:08 +01:00
|
|
|
if Pass ->
|
|
|
|
LFrom = jlib:jid_tolower(From),
|
|
|
|
LUser = jlib:tolower(User),
|
|
|
|
LServer = jlib:tolower(Server),
|
2003-01-17 20:58:42 +01:00
|
|
|
Resources = get_user_resources(User),
|
|
|
|
if
|
|
|
|
Resources /= [] ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
if LFrom /=
|
|
|
|
{LUser, LServer, R} ->
|
|
|
|
ejabberd_sm !
|
|
|
|
{route,
|
|
|
|
From,
|
|
|
|
{User, Server, R},
|
|
|
|
Packet};
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, Resources);
|
|
|
|
true ->
|
|
|
|
if
|
|
|
|
Subsc ->
|
|
|
|
mod_offline:store_packet(
|
|
|
|
From, To, Packet);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end;
|
2002-12-20 21:42:08 +01:00
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end;
|
2002-12-17 21:49:45 +01:00
|
|
|
"message" ->
|
2003-01-05 21:24:59 +01:00
|
|
|
route_message(From, To, Packet);
|
2002-12-17 21:49:45 +01:00
|
|
|
"iq" ->
|
2003-01-05 21:24:59 +01:00
|
|
|
process_iq(From, To, Packet);
|
2002-12-22 21:56:29 +01:00
|
|
|
"broadcast" ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
ejabberd_sm ! {route,
|
|
|
|
From,
|
|
|
|
{User, Server, R},
|
|
|
|
Packet}
|
|
|
|
end, get_user_resources(User));
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
2003-01-18 20:42:48 +01:00
|
|
|
LUR = {jlib:tolower(User), Resource},
|
|
|
|
Sess = mnesia:dirty_read({session, LUR}),
|
2003-01-15 20:54:44 +01:00
|
|
|
case Sess of
|
|
|
|
[] ->
|
2003-01-05 21:24:59 +01:00
|
|
|
if
|
|
|
|
Name == "message" ->
|
|
|
|
route_message(From, To, Packet);
|
|
|
|
true ->
|
|
|
|
?DEBUG("packet droped~n", [])
|
2003-01-15 20:54:44 +01:00
|
|
|
end;
|
|
|
|
[Ses] ->
|
2003-01-21 21:36:55 +01:00
|
|
|
case mnesia:dirty_read({local_session, LUR}) of
|
2003-01-15 20:54:44 +01:00
|
|
|
[] ->
|
|
|
|
Node = Ses#session.node,
|
|
|
|
?DEBUG("sending to node ~p~n", [Node]),
|
|
|
|
{ejabberd_sm, Node} ! {route, From, To, Packet};
|
|
|
|
[El] ->
|
2003-01-21 21:36:55 +01:00
|
|
|
Pid = El#local_session.pid,
|
2003-01-15 20:54:44 +01:00
|
|
|
?DEBUG("sending to process ~p~n", [Pid]),
|
|
|
|
Pid ! {route, From, To, Packet}
|
|
|
|
end
|
2002-12-17 21:49:45 +01:00
|
|
|
end
|
2002-11-30 19:46:16 +01:00
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
route_message(From, To, Packet) ->
|
|
|
|
{User, Server, Resource} = To,
|
|
|
|
case catch lists:max(get_user_present_resources(User)) of
|
|
|
|
{'EXIT', _} ->
|
|
|
|
case ejabberd_auth:is_user_exists(User) of
|
|
|
|
true ->
|
|
|
|
mod_offline:store_packet(From, To, Packet);
|
|
|
|
_ ->
|
|
|
|
?DEBUG("packet droped~n", [])
|
|
|
|
end;
|
|
|
|
{_, R} ->
|
|
|
|
ejabberd_sm ! {route,
|
|
|
|
From,
|
|
|
|
{User, Server, R},
|
|
|
|
Packet}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2002-12-14 21:07:26 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
get_user_resources(User) ->
|
2002-12-30 21:25:08 +01:00
|
|
|
LUser = jlib:tolower(User),
|
2003-01-19 21:17:56 +01:00
|
|
|
case catch mnesia:dirty_index_read(session, LUser, #session.user) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
[];
|
|
|
|
Rs ->
|
|
|
|
lists:map(fun(R) ->
|
|
|
|
element(2, R#session.ur)
|
|
|
|
end, Rs)
|
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
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
set_presence(User, Resource, Priority) ->
|
|
|
|
LUser = jlib:tolower(User),
|
|
|
|
F = fun() ->
|
|
|
|
UR = {User, Resource},
|
|
|
|
mnesia:write(#presence{ur = UR, user = LUser,
|
|
|
|
priority = Priority})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
unset_presence(User, Resource) ->
|
|
|
|
LUser = jlib:tolower(User),
|
|
|
|
F = fun() ->
|
|
|
|
UR = {User, Resource},
|
|
|
|
mnesia:delete({presence, UR})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
get_user_present_resources(User) ->
|
|
|
|
LUser = jlib:tolower(User),
|
2003-01-19 21:17:56 +01:00
|
|
|
case catch mnesia:dirty_index_read(presence, LUser, #presence.user) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
[];
|
|
|
|
Rs ->
|
2002-12-30 21:25:08 +01:00
|
|
|
lists:map(fun(R) ->
|
|
|
|
{R#presence.priority, element(2, R#presence.ur)}
|
2003-01-19 21:17:56 +01:00
|
|
|
end, Rs)
|
2002-12-30 21:25:08 +01:00
|
|
|
end.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2003-01-01 20:54:44 +01:00
|
|
|
dirty_get_sessions_list() ->
|
|
|
|
mnesia:dirty_all_keys(session).
|
|
|
|
|
2003-01-18 20:42:48 +01:00
|
|
|
dirty_get_my_sessions_list() ->
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:dirty_all_keys(local_session).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2003-01-01 20:54:44 +01:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
process_iq(From, To, Packet) ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
|
|
|
{iq, ID, Type, XMLNS, SubEl} ->
|
|
|
|
case ets:lookup(sm_iqtable, XMLNS) of
|
|
|
|
[{_, Module, Function}] ->
|
|
|
|
ResIQ = apply(Module, Function, [From, To, IQ]),
|
|
|
|
if
|
|
|
|
ResIQ /= ignore ->
|
|
|
|
ejabberd_router ! {route,
|
|
|
|
To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(ResIQ)};
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end;
|
2003-01-22 21:40:40 +01:00
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:handle(Module, Function, Opts,
|
|
|
|
From, To, IQ);
|
2003-01-01 20:54:44 +01:00
|
|
|
[] ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, "501", "Not Implemented"),
|
|
|
|
ejabberd_router ! {route, To, From, Err}
|
|
|
|
end;
|
|
|
|
reply ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet, "400", "Bad Request"),
|
|
|
|
ejabberd_router ! {route, To, From, Err},
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
register_iq_handler(XMLNS, Module, Fun) ->
|
|
|
|
ejabberd_sm ! {register_iq_handler, XMLNS, Module, Fun}.
|
|
|
|
|
2003-01-22 21:40:40 +01:00
|
|
|
register_iq_handler(XMLNS, Module, Fun, Opts) ->
|
|
|
|
ejabberd_sm ! {register_iq_handler, XMLNS, Module, Fun, Opts}.
|
|
|
|
|
2003-01-29 18:12:23 +01:00
|
|
|
unregister_iq_handler(XMLNS) ->
|
|
|
|
ejabberd_sm ! {unregister_iq_handler, XMLNS}.
|
|
|
|
|