Skip to content

Commit

Permalink
mod_pubsub_serverinfo: don't send disco_info twice
Browse files Browse the repository at this point in the history
  • Loading branch information
sstrigler committed Jan 8, 2025
1 parent 59b04b4 commit c5e658a
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions mod_pubsub_serverinfo/src/mod_pubsub_serverinfo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,30 @@ init([Host, _Opts]) ->

handle_cast({register_in, Host, LServer, RServer, Pid}, #state{monitors = Mons} = State) ->
Ref = monitor(process, Pid),
NewMons = maps:put(Ref, {incoming, {LServer, RServer, false}}, Mons),
check_if_remote_has_support(Host, LServer, RServer, incoming),
HasSupport = check_if_remote_has_support(Host, LServer, RServer, Mons),
NewMons = maps:put(Ref, {incoming, {LServer, RServer, HasSupport}}, Mons),
{noreply, State#state{monitors = NewMons}};
handle_cast({register_out, Host, LServer, RServer, Pid}, #state{monitors = Mons} = State) ->
Ref = monitor(process, Pid),
NewMons = maps:put(Ref, {outgoing, {LServer, RServer, false}}, Mons),
check_if_remote_has_support(Host, LServer, RServer, outgoing),
HasSupport = check_if_remote_has_support(Host, LServer, RServer, Mons),
NewMons = maps:put(Ref, {outgoing, {LServer, RServer, HasSupport}}, Mons),
{noreply, State#state{monitors = NewMons}};
handle_cast(_, State) ->
{noreply, State}.

handle_call(_Request, _From, State) ->
{noreply, State}.

handle_info({iq_reply, IQReply, {LServer, RServer, Dir, _Pid}},
handle_info({iq_reply, IQReply, {LServer, RServer}},
#state{monitors = Mons} = State) ->
case IQReply of
#iq{type = result, sub_els = [El]} ->
case xmpp:decode(El) of
#disco_info{features = Features} ->
case lists:member(?NS_URN_SERVERINFO, Features) of
true ->
Mons2 = maps:fold(fun(Ref, {Dir0, {LServer0, RServer0, _}}, NewMons)
when Dir == Dir0, LServer == LServer0, RServer == RServer0 ->
Mons2 = maps:fold(fun(Ref, {Dir, {LServer0, RServer0, _}}, NewMons)
when LServer == LServer0, RServer == RServer0 ->
maps:put(Ref, {Dir, {LServer, RServer, true}}, NewMons);
(Ref, Other, NewMons) ->
maps:put(Ref, Other, NewMons)
Expand Down Expand Up @@ -153,12 +153,29 @@ out_auth_result(#{server_host := Host, server := LServer, remote_server := RServ
out_auth_result(State, _) ->
State.

check_if_remote_has_support(Host, LServer, RServer, Dir) ->
%% [FIXME] No need to do this per direction, either the remote host supports it or not.
check_if_remote_has_support(Host, LServer, RServer, Mons) ->
case has_support(LServer, RServer, Mons) of
true ->
true;
false ->
send_disco_info(Host, LServer, RServer),
false
end.

has_support(LServer, RServer, Mons) ->
maps:size(
maps:filter(
fun(_Ref, {_Dir, LServer0, RServer0, HasSupport})
when LServer0 == LServer, RServer0 == RServer -> HasSupport;
(_Ref, _Other) -> false
end,
Mons)) == 0.

send_disco_info(Host, LServer, RServer) ->
Proc = gen_mod:get_module_proc(Host, ?MODULE),
IQ = #iq{type = get, from = jid:make(LServer),
to = jid:make(RServer), sub_els = [#disco_info{}]},
ejabberd_router:route_iq(IQ, {LServer, RServer, Dir, self()}, Proc).
ejabberd_router:route_iq(IQ, {LServer, RServer}, Proc).

update_pubsub(#state{host = Host, pubsub_host = PubsubHost, node = Node, monitors = Mons}) ->
Map = maps:fold(
Expand Down

0 comments on commit c5e658a

Please sign in to comment.