diff --git a/ChangeLog b/ChangeLog index 9e9fc965a..0491d68f4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,8 @@ src/ejabberd_s2s_out.erl: Use -include_lib instead of -include to include exmpp.hrl. + * src/ejabberd_s2s.erl: Convert to exmpp. + 2008-06-27 Jean-Sébastien Pédron * src/ejabberd_c2s.erl, src/ejabberd_s2s_out.erl, diff --git a/src/ejabberd_s2s.erl b/src/ejabberd_s2s.erl index cd878c1b2..424b9fc99 100644 --- a/src/ejabberd_s2s.erl +++ b/src/ejabberd_s2s.erl @@ -46,13 +46,21 @@ -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). +-include_lib("exmpp/include/exmpp.hrl"). + -include("ejabberd.hrl"). --include("jlib.hrl"). -include("ejabberd_ctl.hrl"). -define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER, 1). -define(DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE, 1). +% 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} +]). + -record(s2s, {fromto, pid, key}). -record(state, {}). @@ -66,7 +74,12 @@ start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). -route(From, To, Packet) -> +route(FromOld, ToOld, PacketOld) -> + % XXX OLD FORMAT: From, To, Packet. + From = exmpp_jid:from_ejabberd_jid(FromOld), + To = exmpp_jid:from_ejabberd_jid(ToOld), + Packet = exmpp_xml:xmlelement_to_xmlel(PacketOld, + [?DEFAULT_NS], ?PREFIXED_NS), case catch do_route(From, To, Packet) of {'EXIT', Reason} -> ?ERROR_MSG("~p~nwhen processing: ~p", @@ -201,7 +214,12 @@ handle_cast(_Msg, State) -> handle_info({mnesia_system_event, {mnesia_down, Node}}, State) -> clean_table_from_bad_node(Node), {noreply, State}; -handle_info({route, From, To, Packet}, State) -> +handle_info({route, FromOld, ToOld, PacketOld}, State) -> + % XXX OLD FORMAT: From, To, Packet + From = exmpp_jid:from_ejabberd_jid(FromOld), + To = exmpp_jid:from_ejabberd_jid(ToOld), + Packet = exmpp_xml:xmlelement_to_xmlel(PacketOld, + [?NS_JABBER_CLIENT], [{?NS_XMPP, ?NS_XMPP_pfx}]), case catch do_route(From, To, Packet) of {'EXIT', Reason} -> ?ERROR_MSG("~p~nwhen processing: ~p", @@ -249,35 +267,42 @@ clean_table_from_bad_node(Node) -> do_route(From, To, Packet) -> ?DEBUG("s2s manager~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n", [From, To, Packet, 8]), + % XXX OLD FORMAT: From, To. + FromOld = exmpp_jid:to_ejabberd_jid(From), + ToOld = exmpp_jid:to_ejabberd_jid(To), case find_connection(From, To) of {atomic, Pid} when pid(Pid) -> ?DEBUG("sending to process ~p~n", [Pid]), - {xmlelement, Name, Attrs, Els} = Packet, - NewAttrs = jlib:replace_from_to_attrs(jlib:jid_to_string(From), - jlib:jid_to_string(To), - Attrs), - #jid{lserver = MyServer} = From, + NewPacket1 = exmpp_stanza:set_sender(Packet, From), + NewPacket = exmpp_stanza:set_recipient(NewPacket1, To), + #jid{ldomain = MyServer} = From, + % XXX OLD FORMAT: NewPacket. + NewPacketOld = exmpp_xml:xmlel_to_xmlelement(NewPacket, + [?DEFAULT_NS], ?PREFIXED_NS), ejabberd_hooks:run( s2s_send_packet, MyServer, - [From, To, Packet]), - send_element(Pid, {xmlelement, Name, NewAttrs, Els}), + [FromOld, ToOld, NewPacketOld]), + send_element(Pid, NewPacket), ok; {aborted, _Reason} -> - case xml:get_tag_attr_s("type", Packet) of + case exmpp_stanza:get_type(Packet) of "error" -> ok; "result" -> ok; _ -> - Err = jlib:make_error_reply( - Packet, ?ERR_SERVICE_UNAVAILABLE), - ejabberd_router:route(To, From, Err) + Err = exmpp_stanza:reply_with_error(Packet, + exmpp_stanza:error('service-unavailable')), + % XXX OLD FORMAT: Err. + ErrOld = exmpp_xml:xmlel_to_xmlelement(Err, + [?DEFAULT_NS], ?PREFIXED_NS), + ejabberd_router:route(ToOld, FromOld, ErrOld) end, false end. find_connection(From, To) -> - #jid{lserver = MyServer} = From, - #jid{lserver = Server} = To, + #jid{ldomain = MyServer} = From, + #jid{ldomain = Server} = To, FromTo = {MyServer, Server}, MaxS2SConnectionsNumber = max_s2s_connections_number(FromTo), MaxS2SConnectionsNumberPerNode = @@ -330,7 +355,7 @@ choose_pid(From, Pids) -> % Use sticky connections based on the JID of the sender (whithout % the resource to ensure that a muc room always uses the same % connection) - Pid = lists:nth(erlang:phash(jlib:jid_remove_resource(From), length(Pids1)), + Pid = lists:nth(erlang:phash(exmpp_jid:jid_to_bare_jid(From), length(Pids1)), Pids1), ?DEBUG("Using ejabberd_s2s_out ~p~n", [Pid]), Pid. @@ -381,14 +406,14 @@ new_connection(MyServer, Server, From, FromTo, max_s2s_connections_number({From, To}) -> case acl:match_rule( - From, max_s2s_connections, jlib:make_jid("", To, "")) of + From, max_s2s_connections, exmpp_jid:make_bare_jid(To)) of Max when is_integer(Max) -> Max; _ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER end. max_s2s_connections_number_per_node({From, To}) -> case acl:match_rule( - From, max_s2s_connections_per_node, jlib:make_jid("", To, "")) of + From, max_s2s_connections_per_node, exmpp_jid:make_bare_jid(To)) of Max when is_integer(Max) -> Max; _ -> ?DEFAULT_MAX_S2S_CONNECTIONS_NUMBER_PER_NODE end. @@ -405,12 +430,12 @@ needed_connections_number(Ls, MaxS2SConnectionsNumber, %% service. %% -------------------------------------------------------------------- is_service(From, To) -> - LFromDomain = From#jid.lserver, + LFromDomain = From#jid.ldomain, case ejabberd_config:get_local_option({route_subdomains, LFromDomain}) of s2s -> % bypass RFC 3920 10.3 false; _ -> - LDstDomain = To#jid.lserver, + LDstDomain = To#jid.ldomain, P = fun(Domain) -> is_subdomain(LDstDomain, Domain) end, lists:any(P, ?MYHOSTS) end.