25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-28 16:34:13 +01:00

*** empty log message ***

SVN Revision: 9
This commit is contained in:
Alexey Shchepin 2002-11-29 20:55:12 +00:00
parent b2229bffcc
commit 9fdf316aab
3 changed files with 81 additions and 17 deletions

View File

@ -19,7 +19,8 @@
%-export([init/1, state_name/2, state_name/3, handle_event/3, %-export([init/1, state_name/2, state_name/3, handle_event/3,
% handle_sync_event/4, handle_info/3, terminate/3]). % handle_sync_event/4, handle_info/3, terminate/3]).
% %
-export([init/1, wait_for_stream/2, wait_for_auth/2, terminate/3]). -export([init/1, wait_for_stream/2, wait_for_auth/2, session_established/2,
terminate/3]).
-record(state, {socket, sender, receiver, streamid, -record(state, {socket, sender, receiver, streamid,
user = "", server = "localhost", resource = ""}). user = "", server = "localhost", resource = ""}).
@ -89,8 +90,7 @@ wait_for_stream({xmlstreamstart, Name, Attrs}, StateData) ->
% TODO % TODO
{next_state, wait_for_auth, StateData}; {next_state, wait_for_auth, StateData};
_ -> _ ->
send_text(StateData#state.sender, ?INVALID_NS_ERR), send_text(StateData#state.sender, ?INVALID_NS_ERR ?STREAM_TRAILER),
send_text(StateData#state.sender, ?STREAM_TRAILER),
{stop, normal, StateData} {stop, normal, StateData}
end; end;
@ -109,7 +109,7 @@ wait_for_auth({xmlstreamelement, El}, StateData) ->
{next_state, session_established, {next_state, session_established,
StateData#state{user = U, resource = R}}; StateData#state{user = U, resource = R}};
_ -> _ ->
Err = jlib:make_error_iq_reply(El, "404", "Unauthorized"), Err = jlib:make_error_reply(El, "404", "Unauthorized"),
send_element(StateData#state.sender, Err), send_element(StateData#state.sender, Err),
{next_state, wait_for_auth, StateData} {next_state, wait_for_auth, StateData}
end; end;
@ -127,6 +127,9 @@ 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 % TODO
FromJID = {StateData#state.user, "localhost", StateData#state.resource},
ToJID = jlib:string_to_jid(xml:get_attr_s("to", Attrs)),
ejabberd_router:route(FromJID, ToJID, El),
{next_state, session_established, StateData}; {next_state, session_established, StateData};
session_established(closed, StateData) -> session_established(closed, StateData) ->

View File

@ -8,12 +8,15 @@
-module(ejabberd_router). -module(ejabberd_router).
-author('alexey@sevcom.net'). -author('alexey@sevcom.net').
-vsn('$Revision$ ').
%%-export([Function/Arity, ...]). -export([route/3]).
-export([start/0, init/0]). -export([start/0, init/0]).
-record(service, {domain, node, pid}). -include("ejabberd.hrl").
-record(route, {domain, node, pid}).
start() -> start() ->
@ -21,23 +24,82 @@ start() ->
init() -> init() ->
register(ejabberd_router, self()), register(ejabberd_router, self()),
mnesia:create_table(service, mnesia:create_table(route,
[{ram_copies, [node()]}, [{ram_copies, [node()]},
{attributes, {attributes,
record_info(fields, service)}]), record_info(fields, route)}]),
loop(). loop().
loop() -> loop() ->
receive receive
{route, From, To, Packet} -> {route, From, To, Packet} ->
% TODO % TODO
do_route(From, To, Packet),
loop(); loop();
{register_service, Domain, Pid, Node} -> {register_route, Domain, Pid, Node} ->
% TODO F = fun() ->
case mnesia:wread({route, Domain}) of
[] ->
ok;
[Old] ->
% TODO: notify
ok
end,
mnesia:write(#route{domain = Domain,
node = Node,
pid = Pid})
end,
mnesia:transaction(F),
loop(); loop();
{unregister_service, Domain} -> {unregister_route, Domain} ->
% TODO F = fun() ->
case mnesia:wread({route, Domain}) of
[] ->
ok;
[Old] ->
% TODO: notify
ok
end,
mnesia:delete({route, Domain})
end,
mnesia:transaction(F),
loop(); loop();
_ -> _ ->
loop() loop()
end. end.
do_route(From, To, Packet) ->
?DEBUG("route~n\tfrom ~p~n\tto ~p~n\tpacket ~p~n", [From, To, Packet]),
{DstNode, DstDomain, DstResourse} = To,
F = fun() ->
case mnesia:read({route, DstDomain}) of
[] ->
error;
[R] ->
{ok, R#route.node, R#route.pid}
end
end,
case mnesia:transaction(F) of
{atomic, error} ->
% TODO: start s2s instead of below
{xmlelement, Name, Attrs, SubTags} = Packet,
case xml:get_attr_s("type", Attrs) of
"error" ->
ok;
_ ->
Err = jlib:make_error_reply(Packet,
502, "Service Unavailable"),
ejabberd_router ! {route, To, From, Err}
end;
{atomic, {ok, Node, Pid}} ->
{Pid, Node} ! {packet, From, To, Packet};
_ ->
% TODO
error
end.
route(From, To, Packet) ->
ejabberd_router ! {route, From, To, Packet}.

View File

@ -10,21 +10,20 @@
-author('alexey@sevcom.net'). -author('alexey@sevcom.net').
-vsn('$Revision$ '). -vsn('$Revision$ ').
-export([make_error_iq_reply/3, make_correct_from_to_attrs/3, -export([make_error_reply/3, make_correct_from_to_attrs/3,
replace_from_to_attrs/3, string_to_jid/1, tolower/1]). replace_from_to_attrs/3, string_to_jid/1, tolower/1]).
%send_iq(From, To, ID, SubTags) -> %send_iq(From, To, ID, SubTags) ->
% ok. % ok.
make_error_iq_reply({xmlelement, Name, Attrs, SubTags}, Code, Desc) make_error_reply({xmlelement, Name, Attrs, SubTags}, Code, Desc) ->
when Name == "iq" -> NewAttrs = make_error_reply_attrs(Attrs),
NewAttrs = make_error_iq_reply_attrs(Attrs),
{xmlelement, Name, NewAttrs, SubTags ++ [{xmlelement, "error", {xmlelement, Name, NewAttrs, SubTags ++ [{xmlelement, "error",
[{"code", Code}], [{"code", Code}],
[{xmlcdata, Desc}]}]}. [{xmlcdata, Desc}]}]}.
make_error_iq_reply_attrs(Attrs) -> make_error_reply_attrs(Attrs) ->
To = xml:get_attr("to", Attrs), To = xml:get_attr("to", Attrs),
From = xml:get_attr("from", Attrs), From = xml:get_attr("from", Attrs),
Attrs1 = lists:keydelete("to", 1, Attrs), Attrs1 = lists:keydelete("to", 1, Attrs),