]> granicus.if.org Git - ejabberd/commitdiff
Set from/to in every routed packet
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>
Mon, 9 Jan 2017 14:51:08 +0000 (17:51 +0300)
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>
Mon, 9 Jan 2017 14:51:08 +0000 (17:51 +0300)
src/ejabberd_router.erl

index 2bef97c01d427b1631c96db8723e05686c92d131..17cb7e27907712f1562b6918b0245d4aae2edbe5 100644 (file)
@@ -74,8 +74,17 @@ start_link() ->
 
 -spec route(jid(), jid(), xmlel() | stanza()) -> ok.
 
-route(From, To, Packet) ->
-    case catch do_route(From, To, Packet) of
+route(#jid{} = From, #jid{} = To, #xmlel{} = El) ->
+    try xmpp:decode(El, ?NS_CLIENT, [ignore_els]) of
+       Pkt -> route(From, To, xmpp:set_from_to(Pkt, From, To))
+    catch _:{xmpp_codec, Why} ->
+           ?ERROR_MSG("failed to decode xml element ~p when "
+                      "routing from ~s to ~s: ~s",
+                      [El, jid:to_string(From), jid:to_string(To),
+                       xmpp:format_error(Why)])
+    end;
+route(#jid{} = From, #jid{} = To, Packet) ->
+    case catch do_route(From, To, xmpp:set_from_to(Packet, From, To)) of
        {'EXIT', Reason} ->
            ?ERROR_MSG("~p~nwhen processing: ~p",
                       [Reason, {From, To, Packet}]);
@@ -390,12 +399,7 @@ do_route(OrigFrom, OrigTo, OrigPacket) ->
          LDstDomain = To#jid.lserver,
          case mnesia:dirty_read(route, LDstDomain) of
            [] ->
-                 try xmpp:decode(Packet, ?NS_CLIENT, [ignore_els]) of
-                     Pkt ->
-                         ejabberd_s2s:route(From, To, Pkt)
-                 catch _:{xmpp_codec, Why} ->
-                         log_decoding_error(From, To, Packet, Why)
-                 end;
+               ejabberd_s2s:route(From, To, Packet);
            [R] ->
                do_route(From, To, Packet, R);
            Rs ->
@@ -422,27 +426,15 @@ do_route(OrigFrom, OrigTo, OrigPacket) ->
 -spec do_route(jid(), jid(), xmlel() | xmpp_element(), #route{}) -> any().
 do_route(From, To, Packet, #route{local_hint = LocalHint,
                                  pid = Pid}) when is_pid(Pid) ->
-    try xmpp:decode(Packet, ?NS_CLIENT, [ignore_els]) of
-       Pkt ->
-           case LocalHint of
-               {apply, Module, Function} when node(Pid) == node() ->
-                   Module:Function(From, To, Pkt);
-               _ ->
-                   Pid ! {route, From, To, Pkt}
-           end
-    catch error:{xmpp_codec, Why} ->
-           log_decoding_error(From, To, Packet, Why)
+    case LocalHint of
+       {apply, Module, Function} when node(Pid) == node() ->
+           Module:Function(From, To, Packet);
+       _ ->
+           Pid ! {route, From, To, Packet}
     end;
 do_route(_From, _To, _Packet, _Route) ->
     drop.
 
--spec log_decoding_error(jid(), jid(), xmlel() | xmpp_element(), term()) -> ok.
-log_decoding_error(From, To, Packet, Reason) ->
-    ?ERROR_MSG("failed to decode xml element ~p when "
-              "routing from ~s to ~s: ~s",
-              [Packet, jid:to_string(From), jid:to_string(To),
-               xmpp:format_error(Reason)]).
-
 -spec get_component_number(binary()) -> pos_integer() | undefined.
 get_component_number(LDomain) ->
     ejabberd_config:get_option(