25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

*** empty log message ***

SVN Revision: 18
This commit is contained in:
Alexey Shchepin 2002-12-17 20:49:45 +00:00
parent 10f47ce78d
commit 00a923b14a
5 changed files with 336 additions and 125 deletions

View File

@ -25,10 +25,19 @@
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-record(state, {socket, sender, receiver, streamid, -define(SETS, gb_sets).
user = "", server = ?MYNAME, resource = ""}).
-define(DBGFSM, true). -record(state, {socket, sender, receiver, streamid,
user = "", server = ?MYNAME, resource = "",
pres_t = ?SETS:new(),
pres_f = ?SETS:new(),
pres_a = ?SETS:new(),
pres_i = ?SETS:new(),
pres_last, pres_pri,
pres_timestamp,
pres_invis = false}).
%-define(DBGFSM, true).
-ifdef(DBGFSM). -ifdef(DBGFSM).
-define(FSMOPTS, [{debug, [trace]}]). -define(FSMOPTS, [{debug, [trace]}]).
@ -110,8 +119,12 @@ wait_for_auth({xmlstreamelement, El}, StateData) ->
ejabberd_sm:open_session(U, R), ejabberd_sm:open_session(U, R),
Res = jlib:make_result_iq_reply(El), Res = jlib:make_result_iq_reply(El),
send_element(StateData#state.sender, Res), send_element(StateData#state.sender, Res),
{Fs, Ts} = mod_roster:get_subscription_lists(U),
{next_state, session_established, {next_state, session_established,
StateData#state{user = U, resource = R}}; StateData#state{user = U,
resource = R,
pres_f = ?SETS:from_list(Fs),
pres_t = ?SETS:from_list(Ts)}};
_ -> _ ->
Err = jlib:make_error_reply(El, "401", "Unauthorized"), Err = jlib:make_error_reply(El, "401", "Unauthorized"),
send_element(StateData#state.sender, Err), send_element(StateData#state.sender, Err),
@ -143,26 +156,39 @@ wait_for_auth(closed, StateData) ->
session_established({xmlstreamelement, El}, StateData) -> session_established({xmlstreamelement, El}, StateData) ->
{xmlelement, Name, Attrs, Els} = El, {xmlelement, Name, Attrs, Els} = El,
% TODO Server = StateData#state.server,
FromJID = {StateData#state.user, FromJID = {StateData#state.user,
StateData#state.server, Server,
StateData#state.resource}, StateData#state.resource},
To = xml:get_attr_s("to", Attrs), To = xml:get_attr_s("to", Attrs),
ToJID = case To of ToJID = case To of
"" -> "" ->
{"", StateData#state.server, ""}; {"", Server, ""};
_ -> _ ->
jlib:string_to_jid(To) jlib:string_to_jid(To)
end, end,
case ToJID of NewState =
error -> case ToJID of
% TODO error ->
error; % TODO
_ -> StateData;
%?DEBUG("FromJID=~w, ToJID=~w, El=~w~n", [FromJID, ToJID, El]), _ ->
ejabberd_router:route(FromJID, ToJID, El) case Name of
end, "presence" ->
{next_state, session_established, StateData}; case ToJID of
{"", Server, ""} ->
?DEBUG("presence_update(~p,~n\t~p,~n\t~p)",
[FromJID, El, StateData]),
presence_update(FromJID, El, StateData);
_ ->
StateData
end;
_ ->
ejabberd_router:route(FromJID, ToJID, El),
StateData
end
end,
{next_state, session_established, NewState};
session_established({xmlstreamend, Name}, StateData) -> session_established({xmlstreamend, Name}, StateData) ->
{stop, normal, StateData}; {stop, normal, StateData};
@ -216,7 +242,51 @@ handle_sync_event(Event, From, StateName, StateData) ->
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
handle_info({send_text, Text}, StateName, StateData) -> handle_info({send_text, Text}, StateName, StateData) ->
send_text(StateData#state.sender, Text), send_text(StateData#state.sender, Text),
{next_state, StateName, StateData}. {next_state, StateName, StateData};
handle_info({route, From, To, Packet}, StateName, StateData) ->
{xmlelement, Name, Attrs, Els} = Packet,
% TODO
{Pass, NewAttrs, NewState} =
case Name of
"presence" ->
case xml:get_attr_s("type", Attrs) of
"probe" ->
process_presence_probe(From, To, StateData),
{false, Attrs, StateData};
"error" ->
case ?SETS:is_element(From, StateData#state.pres_a) of
true ->
A = ?SETS:del_element(From,
StateData#state.pres_a),
{true, Attrs, StateData#state{pres_a = A}};
_ ->
{true, Attrs, StateData}
end;
"invisible" ->
Attrs1 = lists:keydelete("type", 1, Attrs),
{true, [{"type", "unavailable"} | Attrs1], StateData};
"subscribe" ->
% TODO
{true, Attrs, StateData};
"unsubscribe" ->
% TODO
{true, Attrs, StateData};
_ ->
{true, Attrs, StateData}
end;
_ ->
{true, Attrs, StateData}
end,
if Pass ->
Attrs2 = jlib:replace_from_to_attrs(jlib:jid_to_string(From),
jlib:jid_to_string(To),
NewAttrs),
Text = xml:element_to_string({xmlelement, Name, Attrs2, Els}),
send_text(StateData#state.sender, Text);
true ->
ok
end,
{next_state, StateName, NewState}.
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
%% Func: terminate/3 %% Func: terminate/3
@ -314,5 +384,127 @@ get_auth_tags([], U, P, D, R) ->
{U, P, D, R}. {U, P, D, R}.
process_presence_probe(From, To, StateData) ->
case StateData#state.pres_last of
undefined ->
ok;
_ ->
Cond1 = (not StateData#state.pres_invis)
and ?SETS:is_element(From, StateData#state.pres_f)
and (not ?SETS:is_element(From, StateData#state.pres_i)),
Cond2 = StateData#state.pres_invis
and ?SETS:is_element(From, StateData#state.pres_f)
and ?SETS:is_element(From, StateData#state.pres_a),
if
Cond1 ->
ejabberd_router:route(To, From,
StateData#state.pres_last);
Cond2 ->
ejabberd_router:route(To, From,
{xmlelement, "presence",
[],
[]});
true ->
ok
end
end.
presence_update(From, Packet, StateData) ->
{xmlelement, Name, Attrs, Els} = Packet,
case xml:get_attr_s("type", Attrs) of
"unavailable" ->
presence_broadcast(From, StateData#state.pres_a, Packet),
presence_broadcast(From, StateData#state.pres_i, Packet),
StateData#state{pres_last = undefined,
pres_a = ?SETS:new(),
pres_i = ?SETS:new(),
pres_invis = false};
"invisible" ->
NewState =
if
not StateData#state.pres_invis ->
presence_broadcast(From, StateData#state.pres_a,
Packet),
presence_broadcast(From, StateData#state.pres_i,
Packet),
S1 = StateData#state{pres_last = undefined,
pres_a = ?SETS:new(),
pres_i = ?SETS:new(),
pres_invis = false},
presence_broadcast_first(From, S1, Packet);
true ->
StateData
end,
StateData;
"error" ->
StateData;
_ ->
FromUnavail = (StateData#state.pres_last == undefined) or
StateData#state.pres_invis,
?DEBUG("from unavail = ~p~n", [FromUnavail]),
NewState =
if
FromUnavail ->
% TODO: watching ourself
presence_broadcast_first(From, StateData, Packet);
true ->
presence_broadcast_to_trusted(From,
StateData#state.pres_f,
StateData#state.pres_a,
Packet),
StateData
end,
NewState#state{pres_last = Packet,
pres_invis = false
}
end.
presence_broadcast(From, JIDSet, Packet) ->
lists:foreach(fun(JID) ->
ejabberd_router:route(From, JID, Packet)
end, ?SETS:to_list(JIDSet)).
presence_broadcast_to_trusted(From, T, A, Packet) ->
lists:foreach(fun(JID) ->
case ?SETS:is_element(JID, T) of
true ->
ejabberd_router:route(From, JID, Packet);
_ ->
ok
end
end, ?SETS:to_list(A)).
presence_broadcast_first(From, StateData, Packet) ->
{U, S, _} = From,
?SETS:fold(fun(JID, X) ->
ejabberd_router:route({U, S, ""}, JID,
{xmlelement, "presence",
[{"type", "probe"}],
[]}),
X
end,
[],
StateData#state.pres_t),
if
StateData#state.pres_invis ->
StateData;
true ->
As = ?SETS:fold(fun(JID, A) ->
ejabberd_router:route(From, JID, Packet),
?SETS:add_element(JID, A)
end,
StateData#state.pres_a,
StateData#state.pres_f),
StateData#state{pres_a = As}
end.

View File

@ -99,6 +99,8 @@ process_iq(State, From, To, Packet) ->
% TODO % TODO
ok ok
end; end;
reply ->
ok;
_ -> _ ->
Err = jlib:make_error_reply(Packet, "400", "Bad Request"), Err = jlib:make_error_reply(Packet, "400", "Bad Request"),
ejabberd_router ! {route, ejabberd_router ! {route,

View File

@ -16,12 +16,8 @@
-include_lib("mnemosyne/include/mnemosyne.hrl"). -include_lib("mnemosyne/include/mnemosyne.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-record(user_resource, {id, user, resource}). -record(session, {ur, user, node}).
-record(user_resource_id_seq, {name = value, id}). -record(mysession, {ur, pid}).
-record(session, {id, node}).
-record(mysession, {id, info}).
-record(mysession_info, {pid}).
start() -> start() ->
@ -29,17 +25,9 @@ start() ->
init() -> init() ->
register(ejabberd_sm, self()), register(ejabberd_sm, self()),
mnesia:create_table(user_resource_id_seq,
[{ram_copies, [node()]},
{attributes,
record_info(fields, user_resource_id_seq)}]),
init_seq(),
mnesia:create_table(user_resource,[{ram_copies, [node()]},
{attributes,
record_info(fields, user_resource)}]),
mnesia:add_table_index(user_resource, user),
mnesia:create_table(session,[{ram_copies, [node()]}, mnesia:create_table(session,[{ram_copies, [node()]},
{attributes, record_info(fields, session)}]), {attributes, record_info(fields, session)}]),
mnesia:add_table_index(session, user),
mnesia:add_table_index(session, node), mnesia:add_table_index(session, node),
mnesia:create_table(mysession, mnesia:create_table(mysession,
[{ram_copies, [node()]}, [{ram_copies, [node()]},
@ -79,12 +67,9 @@ close_session(User, Resource) ->
replace_alien_connection(User, Resource) -> replace_alien_connection(User, Resource) ->
F = fun() -> F = fun() ->
[ID] = mnemosyne:eval(query [X.id || X <- table(user_resource), UR = {User, Resource},
X.user = User, Es = mnesia:read({session, UR}),
X.resource = Resource] mnesia:write(#session{ur = UR, user = User, node = node()}),
end),
Es = mnesia:read({session, ID}),
mnesia:write(#session{id = ID, node = node()}),
Es Es
end, end,
case mnesia:transaction(F) of case mnesia:transaction(F) of
@ -105,20 +90,16 @@ replace_alien_connection(User, Resource) ->
replace_my_connection(User, Resource) -> replace_my_connection(User, Resource) ->
F = fun() -> F = fun() ->
[ID] = mnemosyne:eval(query [X.id || X <- table(user_resource), UR = {User, Resource},
X.user = User, Es = mnesia:read({mysession, UR}),
X.resource = Resource] mnesia:delete({mysession, UR}),
end),
Es = mnesia:read({mysession, ID}),
mnesia:delete({mysession, ID}),
Es Es
end, end,
case mnesia:transaction(F) of case mnesia:transaction(F) of
{atomic, Rs} -> {atomic, Rs} ->
lists:foreach( lists:foreach(
fun(R) -> fun(R) ->
(R#mysession.info)#mysession_info.pid ! replaced R#mysession.pid ! replaced
end, Rs); end, Rs);
_ -> _ ->
false false
@ -126,50 +107,24 @@ replace_my_connection(User, Resource) ->
remove_connection(User, Resource) -> remove_connection(User, Resource) ->
F = fun() -> F = fun() ->
[ID] = mnemosyne:eval(query [X.id || X <- table(user_resource), UR = {User, Resource},
X.user = User, mnesia:delete({mysession, UR}),
X.resource = Resource] mnesia:delete({session, UR})
end),
mnesia:delete({mysession, ID}),
mnesia:delete({session, ID}),
mnesia:delete({user_resource, ID})
end, end,
mnesia:transaction(F). mnesia:transaction(F).
replace_and_register_my_connection(User, Resource, Pid) -> replace_and_register_my_connection(User, Resource, Pid) ->
F = fun() -> F = fun() ->
IDs = mnemosyne:eval(query [X.id || X <- table(user_resource), UR = {User, Resource},
X.user = User, Es = mnesia:read({mysession, UR}),
X.resource = Resource] mnesia:write(#mysession{ur = UR, pid = Pid}),
end),
ID = case IDs of
[Id] -> Id;
[] ->
[CurID] =
mnemosyne:eval(
query [X.id ||
X <- table(user_resource_id_seq)]
end),
mnesia:write(
#user_resource_id_seq{id = CurID + 1}),
mnesia:write(
#user_resource{id = CurID,
user = User,
resource = Resource}),
CurID
end,
Es = mnesia:read({mysession, ID}),
mnesia:write(#mysession{id = ID,
info = #mysession_info{pid = Pid}}),
Es Es
end, end,
case mnesia:transaction(F) of case mnesia:transaction(F) of
{atomic, Rs} -> {atomic, Rs} ->
lists:foreach( lists:foreach(
fun(R) -> fun(R) ->
(R#mysession.info)#mysession_info.pid ! replaced R#mysession.pid ! replaced
end, Rs); end, Rs);
_ -> _ ->
false false
@ -182,76 +137,98 @@ clean_table_from_bad_node(Node) ->
lists:foreach(fun(E) -> lists:foreach(fun(E) ->
mnesia:delete_object(session, E, write), mnesia:delete_object(session, E, write),
mnesia:delete( mnesia:delete(
{user_resource, E#session.id}) {user_resource, E#session.ur})
end, Es) end, Es)
end, end,
mnesia:transaction(F). mnesia:transaction(F).
init_seq() ->
F = fun() ->
[] = mnesia:read({user_resource_id_seq, value}),
mnesia:write(#user_resource_id_seq{id = 0})
end,
mnesia:transaction(F).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
do_route(From, To, Packet) -> do_route(From, To, Packet) ->
?DEBUG("session manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n", ?DEBUG("session manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n",
[From, To, Packet, 8]), [From, To, Packet, 8]),
{User, _, Resource} = To, {User, Server, Resource} = To,
F = fun() -> F = fun() ->
IDs = mnemosyne:eval(query [X.id || X <- table(user_resource), UR = {User, Resource},
X.user = User, Sess = mnesia:read({session, UR}),
X.resource = Resource] case Sess of
end),
case IDs of
[] -> [] ->
not_exists; not_exists;
[ID] -> [Ses] ->
case mnesia:read({mysession, ID}) of case mnesia:read({mysession, UR}) of
[] -> [] ->
[Er] = mnesia:read({session, ID}), {remote, Ses#session.node};
{remote, Er#session.node};
[El] -> [El] ->
{local, (El#mysession.info)#mysession_info.pid} {local, El#mysession.pid}
end end
end end
end, end,
case mnesia:transaction(F) of {xmlelement, Name, Attrs, Els} = Packet,
{atomic, {local, Pid}} -> case Resource of
?DEBUG("sending to process ~p~n", [Pid]), "" ->
% TODO % TODO
{xmlelement, Name, Attrs, Els} = Packet, case Name of
NewAttrs = jlib:replace_from_to_attrs(jlib:jid_to_string(From), "presence" ->
jlib:jid_to_string(To), lists:foreach(
Attrs), fun(R) ->
ejabberd_c2s:send_element(Pid, {xmlelement, Name, NewAttrs, Els}), if From /= {User, Server, R} ->
?DEBUG("sended~n", []), ejabberd_sm ! {route,
ok; From,
{atomic, {remote, Node}} -> {User, Server, R},
?DEBUG("sending to node ~p~n", [Node]), Packet};
{ejabberd_sm, Node} ! {route, From, To, Packet}, true ->
ok; ok
{atomic, not_exists} -> end
?DEBUG("packet droped~n", []), end, get_user_resources(User)),
ok; ok;
{aborted, Reason} -> "message" ->
?DEBUG("delivery failed: ~p~n", [Reason]), % TODO
false ok;
"iq" ->
% TODO
ok;
_ ->
ok
end;
_ ->
case mnesia:transaction(F) of
{atomic, {local, Pid}} ->
?DEBUG("sending to process ~p~n", [Pid]),
% TODO
Pid ! {route, From, To, Packet},
%NewAttrs =
% jlib:replace_from_to_attrs(jlib:jid_to_string(From),
% jlib:jid_to_string(To),
% Attrs),
%ejabberd_c2s:send_element(
% Pid, {xmlelement, Name, NewAttrs, Els}),
%?DEBUG("sended~n", []),
ok;
{atomic, {remote, Node}} ->
?DEBUG("sending to node ~p~n", [Node]),
{ejabberd_sm, Node} ! {route, From, To, Packet},
ok;
{atomic, not_exists} ->
% TODO
?DEBUG("packet droped~n", []),
ok;
{aborted, Reason} ->
?DEBUG("delivery failed: ~p~n", [Reason]),
false
end
end. end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
get_user_resources(User) -> get_user_resources(User) ->
F = fun() -> F = fun() ->
mnemosyne:eval(query [X.resource || X <- table(user_resource), mnemosyne:eval(query [X.ur || X <- table(session),
X.user = User] X.user = User]
end) end)
end, end,
case mnesia:transaction(F) of case mnesia:transaction(F) of
{atomic, Rs} -> {atomic, Rs} ->
Rs; lists:map(fun(R) -> element(2, R) end, Rs);
{aborted, Reason} -> {aborted, Reason} ->
?DEBUG("delivery failed: ~p~n", [Reason]), ?DEBUG("delivery failed: ~p~n", [Reason]),
[] []

View File

@ -199,13 +199,20 @@ iq_query_info({xmlelement, Name, Attrs, Els}) when Name == "iq" ->
Type1 = case Type of Type1 = case Type of
"set" -> set; "set" -> set;
"get" -> get; "get" -> get;
"result" -> reply;
"error" -> reply;
_ -> invalid _ -> invalid
end, end,
if if
(Type1 /= invalid) and (XMLNS /= "") -> (Type1 /= invalid) and (Type1 /= reply) and (XMLNS /= "") ->
{iq, ID, Type1, XMLNS, {xmlelement, Name2, Attrs2, Els2}}; {iq, ID, Type1, XMLNS, {xmlelement, Name2, Attrs2, Els2}};
true -> true ->
invalid if
Type1 == reply ->
reply;
true ->
invalid
end
end; end;
_ -> _ ->
invalid invalid

View File

@ -12,7 +12,9 @@
-export([]). -export([]).
-export([start/0, init/0, process_iq/3]). -export([start/0, init/0,
process_iq/3,
get_subscription_lists/1]).
-include_lib("mnemosyne/include/mnemosyne.hrl"). -include_lib("mnemosyne/include/mnemosyne.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
@ -105,6 +107,8 @@ item_to_xml(Item) ->
Attrs3 = case Item#roster.subscription of Attrs3 = case Item#roster.subscription of
none -> none ->
[{"subscription", "none"} | Attrs2]; [{"subscription", "none"} | Attrs2];
both ->
[{"subscription", "both"} | Attrs2];
remove -> remove ->
[{"subscription", "remove"} | Attrs2]; [{"subscription", "remove"} | Attrs2];
_ -> _ ->
@ -148,7 +152,8 @@ process_item_set(User, To, XItem) ->
xs = []}; xs = []};
[I] -> [I] ->
mnesia:delete_object(I), mnesia:delete_object(I),
I#roster{groups = [], I#roster{name = "",
groups = [],
xattrs = [], xattrs = [],
xs = []} xs = []}
end, end,
@ -240,3 +245,31 @@ push_item(User, Resource, From, Item) ->
jlib:iq_to_xml(ResIQ)}. jlib:iq_to_xml(ResIQ)}.
get_subscription_lists(User) ->
LUser = jlib:tolower(User),
F = fun() ->
mnesia:read({roster, LUser})
end,
case mnesia:transaction(F) of
{atomic, Items} ->
fill_subscription_lists(Items, [], []);
_ ->
{[], []}
end.
fill_subscription_lists([I | Is], F, T) ->
J = I#roster.jid,
case I#roster.subscription of
both ->
fill_subscription_lists(Is, [J | F], [J | T]);
from ->
fill_subscription_lists(Is, [J | F], T);
to ->
fill_subscription_lists(Is, F, [J | T]);
_ ->
fill_subscription_lists(Is, F, T)
end;
fill_subscription_lists([], F, T) ->
{F, T}.