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

*** empty log message ***

SVN Revision: 16
This commit is contained in:
Alexey Shchepin 2002-12-13 20:58:27 +00:00
parent 727795239a
commit 496c041b28
10 changed files with 346 additions and 165 deletions

3
TODO
View File

@ -1,8 +1,7 @@
presence presence
iq:register iq:register
S2S S2S timeouts
... ...
iq plugins
iq:browse iq:browse
iq:disco iq:disco
... ...

View File

@ -17,8 +17,7 @@ start() ->
init() -> init() ->
register(ejabberd, self()), register(ejabberd, self()),
{A1, A2, A3} = now(), randoms:start(),
random:seed(A1,A2,A3),
ok = erl_ddll:load_driver(".", expat_erl), ok = erl_ddll:load_driver(".", expat_erl),
Port = open_port({spawn, expat_erl}, [binary]), Port = open_port({spawn, expat_erl}, [binary]),
db_init(), db_init(),

View File

@ -270,7 +270,7 @@ send_element(Pid, El) ->
send_text(Pid, xml:element_to_string(El)). send_text(Pid, xml:element_to_string(El)).
new_id() -> new_id() ->
lists:flatten(io_lib:format("~p", [random:uniform(65536*65536)])). randoms:get_string().
is_auth_packet({xmlelement, Name, Attrs, Els}) when Name == "iq" -> is_auth_packet({xmlelement, Name, Attrs, Els}) when Name == "iq" ->

View File

@ -21,6 +21,7 @@
start() -> start() ->
register(ejabberd_local, spawn(ejabberd_local, init, [])), register(ejabberd_local, spawn(ejabberd_local, init, [])),
mod_register:start(), mod_register:start(),
mod_roster:start(),
ok. ok.
init() -> init() ->

View File

@ -10,9 +10,10 @@
-author('alexey@sevcom.net'). -author('alexey@sevcom.net').
-vsn('$Revision$ '). -vsn('$Revision$ ').
-export([start/0, init/0, open_session/2, close_session/2, -export([start/0, init/0,
have_connection/1, have_connection/1,
get_key/1]). get_key/1,
try_register/1]).
-include_lib("mnemosyne/include/mnemosyne.hrl"). -include_lib("mnemosyne/include/mnemosyne.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
@ -59,58 +60,12 @@ loop() ->
end. end.
open_session(User, Resource) -> %open_session(User, Resource) ->
ejabberd_s2s ! {open_session, User, Resource, self()}. % ejabberd_s2s ! {open_session, User, Resource, self()}.
%
%close_session(User, Resource) ->
% ejabberd_s2s ! {close_session, User, Resource}.
close_session(User, Resource) ->
ejabberd_s2s ! {close_session, User, Resource}.
%replace_alien_connection(User, Resource) ->
% F = fun() ->
% [ID] = mnemosyne:eval(query [X.id || X <- table(user_resource),
% X.user = User,
% X.resource = Resource]
% end),
% Es = mnesia:read({session, ID}),
% mnesia:write(#session{id = ID, node = node()}),
% Es
% end,
% case mnesia:transaction(F) of
% {atomic, Rs} ->
% lists:foreach(
% fun(R) ->
% if R#session.node /= node() ->
% {ejabberd_s2s, R#session.node} !
% {replace, User, Resource};
% true ->
% ok
% end
% end, Rs);
% _ ->
% false
% end.
%
%
%replace_my_connection(User, Resource) ->
% F = fun() ->
% [ID] = mnemosyne:eval(query [X.id || X <- table(user_resource),
% X.user = User,
% X.resource = Resource]
% end),
%
% Es = mnesia:read({mysession, ID}),
% mnesia:delete({mysession, ID}),
% Es
% end,
% case mnesia:transaction(F) of
% {atomic, Rs} ->
% lists:foreach(
% fun(R) ->
% (R#mysession.info)#mysession_info.pid ! replaced
% end, Rs);
% _ ->
% false
% end.
remove_connection(Server) -> remove_connection(Server) ->
F = fun() -> F = fun() ->
@ -119,43 +74,6 @@ remove_connection(Server) ->
end, end,
mnesia:transaction(F). mnesia:transaction(F).
%replace_and_register_my_connection(User, Resource, Pid) ->
% F = fun() ->
% IDs = mnemosyne:eval(query [X.id || X <- table(user_resource),
% X.user = User,
% X.resource = Resource]
% 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
% end,
% case mnesia:transaction(F) of
% {atomic, Rs} ->
% lists:foreach(
% fun(R) ->
% (R#mysession.info)#mysession_info.pid ! replaced
% end, Rs);
% _ ->
% false
% end.
clean_table_from_bad_node(Node) -> clean_table_from_bad_node(Node) ->
@ -190,6 +108,29 @@ get_key(Server) ->
"" ""
end. end.
try_register(Server) ->
Key = randoms:get_string(),
F = fun() ->
case mnesia:read({s2s, Server}) of
[] ->
mnesia:write(#s2s{server = Server,
node = node(),
key = Key}),
mnesia:write(#mys2s{server = Server,
pid = self()}),
{key, Key};
_ ->
false
end
end,
case mnesia:transaction(F) of
{atomic, Res} ->
Res;
_ ->
false
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -197,7 +138,7 @@ do_route(From, To, Packet) ->
?DEBUG("s2s manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n", ?DEBUG("s2s manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n",
[From, To, Packet, 8]), [From, To, Packet, 8]),
{User, Server, Resource} = To, {User, Server, Resource} = To,
Key = lists:flatten(io_lib:format("~p", [random:uniform(65536*65536)])), Key = randoms:get_string(),
F = fun() -> F = fun() ->
case mnesia:read({mys2s, Server}) of case mnesia:read({mys2s, Server}) of
[] -> [] ->

View File

@ -327,7 +327,7 @@ send_queue(Socket, Q) ->
new_id() -> new_id() ->
lists:flatten(io_lib:format("~p", [random:uniform(65536*65536)])). randoms:get_string().
is_key_packet({xmlelement, Name, Attrs, Els}) when Name == "db:result" -> is_key_packet({xmlelement, Name, Attrs, Els}) when Name == "db:result" ->

View File

@ -20,7 +20,6 @@
open_socket/2, open_socket/2,
wait_for_stream/2, wait_for_stream/2,
wait_for_validation/2, wait_for_validation/2,
wait_for_verification/2,
stream_established/2, stream_established/2,
handle_info/3, handle_info/3,
terminate/3]). terminate/3]).
@ -28,7 +27,8 @@
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-record(state, {socket, receiver, streamid, -record(state, {socket, receiver, streamid,
myself = ?MYNAME, server, type, xmlpid, queue}). myself = ?MYNAME, server, xmlpid, queue,
new = false, verify = false}).
-define(DBGFSM, true). -define(DBGFSM, true).
@ -74,9 +74,16 @@ start(Host, Type) ->
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
init([Server, Type]) -> init([Server, Type]) ->
gen_fsm:send_event(self(), init), gen_fsm:send_event(self(), init),
{New, Verify} = case Type of
{new, Key} ->
{Key, false};
{verify, Pid, Key} ->
{false, {Pid, Key}}
end,
{ok, open_socket, #state{queue = queue:new(), {ok, open_socket, #state{queue = queue:new(),
server = Server, server = Server,
type = Type}}. new = New,
verify = Verify}}.
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
%% Func: StateName/2 %% Func: StateName/2
@ -109,25 +116,41 @@ wait_for_stream({xmlstreamstart, Name, Attrs}, StateData) ->
% TODO % TODO
case {xml:get_attr_s("xmlns", Attrs), xml:get_attr_s("xmlns:db", Attrs)} of case {xml:get_attr_s("xmlns", Attrs), xml:get_attr_s("xmlns:db", Attrs)} of
{"jabber:server", "jabber:server:dialback"} -> {"jabber:server", "jabber:server:dialback"} ->
case StateData#state.type of Server = StateData#state.server,
{new, Key} -> New = case StateData#state.new of
false ->
case ejabberd_s2s:try_register(Server) of
{key, Key} ->
Key;
false ->
false
end;
Key ->
Key
end,
case New of
false ->
ok;
Key1 ->
send_element(StateData#state.socket, send_element(StateData#state.socket,
{xmlelement, {xmlelement,
"db:result", "db:result",
[{"from", ?MYNAME}, [{"from", ?MYNAME},
{"to", StateData#state.server}], {"to", Server}],
[{xmlcdata, Key}]}), [{xmlcdata, Key1}]})
% TODO end,
{next_state, wait_for_validation, StateData}; case StateData#state.verify of
{verify, Pid, Key} -> false ->
ok;
{Pid, Key2} ->
send_element(StateData#state.socket, send_element(StateData#state.socket,
{xmlelement, {xmlelement,
"db:verify", "db:verify",
[{"from", ?MYNAME}, [{"from", ?MYNAME},
{"to", StateData#state.server}], {"to", StateData#state.server}],
[{xmlcdata, Key}]}), [{xmlcdata, Key2}]})
{next_state, wait_for_verification, StateData} end,
end; {next_state, wait_for_validation, StateData#state{new = New}};
_ -> _ ->
send_text(StateData#state.socket, ?INVALID_HEADER_ERR), send_text(StateData#state.socket, ?INVALID_HEADER_ERR),
{stop, normal, StateData} {stop, normal, StateData}
@ -143,13 +166,31 @@ wait_for_validation({xmlstreamelement, El}, StateData) ->
{result, To, From, Id, Type} -> {result, To, From, Id, Type} ->
case Type of case Type of
"valid" -> "valid" ->
% TODO
send_queue(StateData#state.socket, StateData#state.queue), send_queue(StateData#state.socket, StateData#state.queue),
{next_state, stream_established, StateData}; {next_state, stream_established, StateData};
_ -> _ ->
% TODO: bounce packets % TODO: bounce packets
{stop, normal, StateData} {stop, normal, StateData}
end; end;
{verify, To, From, Id, Type} ->
case StateData#state.verify of
false ->
{next_state, wait_for_validation, StateData};
{Pid, Key} ->
case Type of
"valid" ->
gen_fsm:send_event(Pid, valid);
_ ->
gen_fsm:send_event(Pid, invalid)
end,
case StateData#state.verify of
false ->
{stop, normal, StateData};
_ ->
{next_state, wait_for_validation,
StateData#state{verify = false}}
end
end;
_ -> _ ->
{next_state, wait_for_validation, StateData} {next_state, wait_for_validation, StateData}
end; end;
@ -162,33 +203,23 @@ wait_for_validation(closed, StateData) ->
{stop, normal, StateData}. {stop, normal, StateData}.
wait_for_verification({xmlstreamelement, El}, StateData) ->
case is_verify_res(El) of
{result, To, From, Id, Type} ->
{verify, Pid, Key} = StateData#state.type,
case Type of
"valid" ->
io:format("VALID KEY~n", []),
gen_fsm:send_event(Pid, valid);
% TODO
_ ->
% TODO
gen_fsm:send_event(Pid, invalid)
end,
{stop, normal, StateData};
_ ->
{next_state, wait_for_verification, StateData}
end;
wait_for_verification({xmlstreamend, Name}, StateData) ->
% TODO
{stop, normal, StateData};
wait_for_verification(closed, StateData) ->
{stop, normal, StateData}.
stream_established({xmlstreamelement, El}, StateData) -> stream_established({xmlstreamelement, El}, StateData) ->
case is_verify_res(El) of
{verify, VTo, VFrom, VId, VType} ->
case StateData#state.verify of
{VPid, VKey} ->
case VType of
"valid" ->
gen_fsm:send_event(VPid, valid);
_ ->
gen_fsm:send_event(VPid, invalid)
end;
_ ->
ok
end;
_ ->
ok
end,
{xmlelement, Name, Attrs, Els} = El, {xmlelement, Name, Attrs, Els} = El,
% TODO % TODO
From = xml:get_attr_s("from", Attrs), From = xml:get_attr_s("from", Attrs),
@ -293,11 +324,11 @@ handle_info({tcp_error, Socket, Reason}, StateName, StateData) ->
%%---------------------------------------------------------------------- %%----------------------------------------------------------------------
terminate(Reason, StateName, StateData) -> terminate(Reason, StateName, StateData) ->
?DEBUG("s2s_out: terminate ~p~n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!~n", [[Reason, StateName, StateData]]), ?DEBUG("s2s_out: terminate ~p~n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!~n", [[Reason, StateName, StateData]]),
case StateData#state.type of case StateData#state.new of
{new, Key} -> false ->
ejabberd_s2s ! {closed_conection, StateData#state.server}; ok;
_ -> Key ->
ok ejabberd_s2s ! {closed_conection, StateData#state.server}
end, end,
case StateData#state.socket of case StateData#state.socket of
undefined -> undefined ->
@ -342,7 +373,7 @@ send_queue(Socket, Q) ->
end. end.
new_id() -> new_id() ->
lists:flatten(io_lib:format("~p", [random:uniform(65536*65536)])). randoms:get_string().
bounce_messages(Reason) -> bounce_messages(Reason) ->
receive receive
@ -385,7 +416,7 @@ is_verify_res({xmlelement, Name, Attrs, Els}) when Name == "db:result" ->
xml:get_attr_s("id", Attrs), xml:get_attr_s("id", Attrs),
xml:get_attr_s("type", Attrs)}; xml:get_attr_s("type", Attrs)};
is_verify_res({xmlelement, Name, Attrs, Els}) when Name == "db:verify" -> is_verify_res({xmlelement, Name, Attrs, Els}) when Name == "db:verify" ->
{result, {verify,
xml:get_attr_s("to", Attrs), xml:get_attr_s("to", Attrs),
xml:get_attr_s("from", Attrs), xml:get_attr_s("from", Attrs),
xml:get_attr_s("id", Attrs), xml:get_attr_s("id", Attrs),

View File

@ -12,6 +12,7 @@
-export([make_result_iq_reply/1, -export([make_result_iq_reply/1,
make_error_reply/3, make_error_reply/3,
make_error_element/2,
make_correct_from_to_attrs/3, make_correct_from_to_attrs/3,
replace_from_to_attrs/3, replace_from_to_attrs/3,
replace_from_to/3, replace_from_to/3,
@ -81,6 +82,11 @@ make_error_reply_attrs(Attrs) ->
Attrs6 = [{"type", "error"} | Attrs5], Attrs6 = [{"type", "error"} | Attrs5],
Attrs6. Attrs6.
make_error_element(Code, Desc) ->
{xmlelement, "error",
[{"code", Code}],
[{xmlcdata, Desc}]}.
make_correct_from_to_attrs(From, To, Attrs) -> make_correct_from_to_attrs(From, To, Attrs) ->
Attrs1 = lists:keydelete("from", 1, Attrs), Attrs1 = lists:keydelete("from", 1, Attrs),
Attrs2 = case xml:get_attr("to", Attrs) of Attrs2 = case xml:get_attr("to", Attrs) of

View File

@ -12,36 +12,198 @@
-export([]). -export([]).
-export([start/0]). -export([start/0, init/0, process_iq/3]).
-record(roster, {user, jid, name, subscription, groups, xs}). -include_lib("mnemosyne/include/mnemosyne.hrl").
-include("ejabberd.hrl").
-record(roster, {user,
jid,
name = "",
subscription = none,
groups = [],
xattrs = [],
xs = []}).
-define(ME, ejabberd_mod_roster).
start() -> start() ->
register(?ME, spawn(mod_roster, init, [])).
init() ->
mnesia:create_table(roster,[{disc_copies, [node()]}, mnesia:create_table(roster,[{disc_copies, [node()]},
{type, bag}, {type, bag},
{attributes, record_info(fields, roster)}]), {attributes, record_info(fields, roster)}]),
mnesia:add_table_index(roster, jid),
ejabberd_local:register_iq_handler("jabber:iq:roster", ejabberd_local:register_iq_handler("jabber:iq:roster",
?MODULE, process_iq). ?MODULE, process_iq),
%spawn(mod_roster, init, []). loop().
%init() -> loop() ->
% loop(). receive
% {process_iq, From, To, {iq, ID, Type, XMLNS, SubEl}} ->
%loop() -> case Type of
% receive set ->
% _ -> ResIQ = process_iq_set(From, To,
% loop() {iq, ID, Type, XMLNS, SubEl}),
% end. ejabberd_router ! {route,
To,
From,
% TODO jlib:iq_to_xml(ResIQ)},
process_iq(From, To, {iq, ID, Type, XMLNS, SubEl}) -> loop();
case Type of get ->
set -> ResIQ = process_iq_get(From, To,
{iq, ID, error, XMLNS, []}; {iq, ID, Type, XMLNS, SubEl}),
get -> ejabberd_router ! {route,
{iq, ID, error, XMLNS, []} To,
From,
jlib:iq_to_xml(ResIQ)},
loop()
end
end. end.
process_iq(From, To, IQ) ->
{iq, ID, Type, XMLNS, SubEl} = IQ,
{_, Server, _} = From,
case ?MYNAME of
Server ->
?ME ! {process_iq, From, To, IQ},
ignore;
_ ->
{iq, ID, error, XMLNS,
[SubEl, jlib:make_error_element("404", "Not Found")]}
end.
process_iq_get(From, To, {iq, ID, Type, XMLNS, SubEl}) ->
{User, _, _} = From,
F = fun() ->
mnesia:read({roster, User})
end,
case mnesia:transaction(F) of
{atomic, Items} ->
XItems = lists:map(fun item_to_xml/1, Items),
{iq, ID, result, XMLNS, XItems};
_ ->
{iq, ID, error, XMLNS,
[SubEl, jlib:make_error_element("500",
"Internal Server Error")]}
end.
item_to_xml(Item) ->
Attrs1 = [{"jid", jlib:jid_to_string(Item#roster.jid)}],
Attrs2 = case Item#roster.name of
"" ->
Attrs1;
Name ->
[{"name", Name} | Attrs1]
end,
Attrs3 = case Item#roster.subscription of
none ->
[{"subscription", "none"} | Attrs2];
_ ->
% TODO
Attrs2
end,
Attrs = Attrs3 ++ Item#roster.xattrs,
SubEls1 = lists:map(fun(G) ->
{xmlelement, "group", [], [{xmlcdata, G}]}
end, Item#roster.groups),
SubEls = SubEls1 ++ Item#roster.xs,
{xmlelement, "item", Attrs, SubEls}.
process_iq_set(From, To, {iq, ID, Type, XMLNS, SubEl}) ->
{User, _, _} = From,
{xmlelement, Name, Attrs, Els} = SubEl,
lists:foreach(fun(El) -> process_item_set(User, To, El) end, Els),
{iq, ID, result, XMLNS, []}.
process_item_set(User, To, XItem) ->
{xmlelement, Name, Attrs, Els} = XItem,
% TODO: load existing item
JID = jlib:string_to_jid(xml:get_attr_s("jid", Attrs)),
case JID of
error ->
ok;
_ ->
F = fun() ->
Res = mnemosyne:eval(query [X || X <- table(roster),
X.user = User,
X.jid = JID]
end),
Item = case Res of
[] ->
#roster{user = User,
jid = JID,
groups = [],
xattrs = [],
xs = []};
[I] ->
mnesia:delete_object(I),
I
end,
Item1 = process_item_attrs(Item, Attrs),
Item2 = process_item_els(Item1, Els),
mnesia:write(Item2),
Item2
end,
case mnesia:transaction(F) of
{atomic, Item} ->
io:format("ROSTER: push for user ~p: ~p~n", [User, Item]),
ok;
_ ->
ok
end
end.
process_item_attrs(Item, [{Attr, Val} | Attrs]) ->
case Attr of
"jid" ->
case jlib:string_to_jid(Val) of
error ->
process_item_attrs(Item, [Attrs]);
JID ->
process_item_attrs(Item#roster{jid = JID}, [Attrs])
end;
"name" ->
process_item_attrs(Item#roster{name = Val}, [Attrs]);
"subscription" ->
case Val of
"remove" ->
process_item_attrs(Item#roster{subscription = remove},
[Attrs]);
_ ->
process_item_attrs(Item, [Attrs])
end;
"ask" ->
process_item_attrs(Item, [Attrs]);
_ ->
XAttrs = Item#roster.xattrs,
process_item_attrs(Item#roster{xattrs = [{Attr, Val} | XAttrs]},
[Attrs])
end;
process_item_attrs(Item, []) ->
Item.
% {user, jid, name, subscription, groups, xattrs, xs}
process_item_els(Item, [{xmlelement, Name, Attrs, SEls} | Els]) ->
case Name of
"group" ->
Groups = [xml:get_cdata(SEls) | Item#roster.groups],
process_item_els(Item#roster{groups = Groups}, Els);
_ ->
case xml:get_attr_s("xmlns", Attrs) of
"" ->
process_item_els(Item, Els);
_ ->
XEls = [{xmlelement, Name, Attrs, SEls} | Item#roster.xs],
process_item_els(Item#roster{xs = XEls}, Els)
end
end;
process_item_els(Item, [{xmlcdata, _} | Els]) ->
process_item_els(Item, Els);
process_item_els(Item, []) ->
Item.

42
src/randoms.erl Normal file
View File

@ -0,0 +1,42 @@
%%%----------------------------------------------------------------------
%%% File : randoms.erl
%%% Author : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose :
%%% Created : 13 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
%%% Id : $Id$
%%%----------------------------------------------------------------------
-module(randoms).
-author('alexey@sevcom.net').
-vsn('$Revision$ ').
-export([get_string/0]).
-export([start/0, init/0]).
start() ->
register(random_generator, spawn(randoms, init, [])).
init() ->
{A1, A2, A3} = now(),
random:seed(A1,A2,A3),
loop().
loop() ->
receive
{From, get_random, N} ->
From ! {random, random:uniform(N)},
loop();
_ ->
loop()
end.
get_string() ->
random_generator ! {self(), get_random, 65536*65536},
receive
{random, R} ->
integer_to_list(R)
end.