mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-20 16:15:59 +01:00
Make it possible to get virtual host of a registered route
This commit is contained in:
parent
9ceeaf213b
commit
357e48fb6b
@ -178,6 +178,7 @@ bounce_resource_packet(From, To, Packet) ->
|
||||
init([]) ->
|
||||
lists:foreach(fun (Host) ->
|
||||
ejabberd_router:register_route(Host,
|
||||
Host,
|
||||
{apply, ?MODULE,
|
||||
route}),
|
||||
ejabberd_hooks:add(local_send_to_resource_hook, Host,
|
||||
|
@ -36,7 +36,9 @@
|
||||
route_error/4,
|
||||
register_route/1,
|
||||
register_route/2,
|
||||
register_route/3,
|
||||
register_routes/1,
|
||||
host_of_route/1,
|
||||
unregister_route/1,
|
||||
unregister_routes/1,
|
||||
dirty_get_all_routes/0,
|
||||
@ -55,7 +57,7 @@
|
||||
|
||||
-type local_hint() :: undefined | integer() | {apply, atom(), atom()}.
|
||||
|
||||
-record(route, {domain, pid, local_hint}).
|
||||
-record(route, {domain, server_host, pid, local_hint}).
|
||||
|
||||
-record(state, {}).
|
||||
|
||||
@ -94,19 +96,29 @@ route_error(From, To, ErrPacket, OrigPacket) ->
|
||||
-spec register_route(binary()) -> term().
|
||||
|
||||
register_route(Domain) ->
|
||||
register_route(Domain, undefined).
|
||||
?WARNING_MSG("~s:register_route/1 is deprected, "
|
||||
"use ~s:register_route/2 instead",
|
||||
[?MODULE, ?MODULE]),
|
||||
register_route(Domain, ?MYNAME).
|
||||
|
||||
-spec register_route(binary(), local_hint()) -> term().
|
||||
-spec register_route(binary(), binary()) -> term().
|
||||
|
||||
register_route(Domain, LocalHint) ->
|
||||
case jid:nameprep(Domain) of
|
||||
error -> erlang:error({invalid_domain, Domain});
|
||||
LDomain ->
|
||||
register_route(Domain, ServerHost) ->
|
||||
register_route(Domain, ServerHost, undefined).
|
||||
|
||||
-spec register_route(binary(), binary(), local_hint()) -> term().
|
||||
|
||||
register_route(Domain, ServerHost, LocalHint) ->
|
||||
case {jid:nameprep(Domain), jid:nameprep(ServerHost)} of
|
||||
{error, _} -> erlang:error({invalid_domain, Domain});
|
||||
{_, error} -> erlang:error({invalid_domain, ServerHost});
|
||||
{LDomain, LServerHost} ->
|
||||
Pid = self(),
|
||||
case get_component_number(LDomain) of
|
||||
undefined ->
|
||||
F = fun () ->
|
||||
mnesia:write(#route{domain = LDomain, pid = Pid,
|
||||
server_host = LServerHost,
|
||||
local_hint = LocalHint})
|
||||
end,
|
||||
mnesia:transaction(F);
|
||||
@ -115,31 +127,27 @@ register_route(Domain, LocalHint) ->
|
||||
case mnesia:wread({route, LDomain}) of
|
||||
[] ->
|
||||
mnesia:write(#route{domain = LDomain,
|
||||
server_host = LServerHost,
|
||||
pid = Pid,
|
||||
local_hint = 1}),
|
||||
lists:foreach(fun (I) ->
|
||||
mnesia:write(#route{domain
|
||||
=
|
||||
LDomain,
|
||||
pid
|
||||
=
|
||||
undefined,
|
||||
local_hint
|
||||
=
|
||||
I})
|
||||
lists:foreach(
|
||||
fun (I) ->
|
||||
mnesia:write(
|
||||
#route{domain = LDomain,
|
||||
pid = undefined,
|
||||
server_host = LServerHost,
|
||||
local_hint = I})
|
||||
end,
|
||||
lists:seq(2, N));
|
||||
Rs ->
|
||||
lists:any(fun (#route{pid = undefined,
|
||||
local_hint = I} =
|
||||
R) ->
|
||||
mnesia:write(#route{domain =
|
||||
LDomain,
|
||||
pid =
|
||||
Pid,
|
||||
local_hint
|
||||
=
|
||||
I}),
|
||||
lists:any(
|
||||
fun (#route{pid = undefined,
|
||||
local_hint = I} = R) ->
|
||||
mnesia:write(
|
||||
#route{domain = LDomain,
|
||||
pid = Pid,
|
||||
server_host = LServerHost,
|
||||
local_hint = I}),
|
||||
mnesia:delete_object(R),
|
||||
true;
|
||||
(_) -> false
|
||||
@ -151,10 +159,10 @@ register_route(Domain, LocalHint) ->
|
||||
end
|
||||
end.
|
||||
|
||||
-spec register_routes([binary()]) -> ok.
|
||||
-spec register_routes([{binary(), binary()}]) -> ok.
|
||||
|
||||
register_routes(Domains) ->
|
||||
lists:foreach(fun (Domain) -> register_route(Domain)
|
||||
lists:foreach(fun ({Domain, ServerHost}) -> register_route(Domain, ServerHost)
|
||||
end,
|
||||
Domains).
|
||||
|
||||
@ -183,7 +191,9 @@ unregister_route(Domain) ->
|
||||
of
|
||||
[R] ->
|
||||
I = R#route.local_hint,
|
||||
ServerHost = R#route.server_host,
|
||||
mnesia:write(#route{domain = LDomain,
|
||||
server_host = ServerHost,
|
||||
pid = undefined,
|
||||
local_hint = I}),
|
||||
mnesia:delete_object(R);
|
||||
@ -211,6 +221,20 @@ dirty_get_all_routes() ->
|
||||
dirty_get_all_domains() ->
|
||||
lists:usort(mnesia:dirty_all_keys(route)).
|
||||
|
||||
-spec host_of_route(binary()) -> binary().
|
||||
|
||||
host_of_route(Domain) ->
|
||||
case jid:nameprep(Domain) of
|
||||
error ->
|
||||
erlang:error({invalid_domain, Domain});
|
||||
LDomain ->
|
||||
case mnesia:dirty_read(route, LDomain) of
|
||||
[#route{server_host = ServerHost}|_] ->
|
||||
ServerHost;
|
||||
[] ->
|
||||
erlang:error({unregistered_route, Domain})
|
||||
end
|
||||
end.
|
||||
|
||||
%%====================================================================
|
||||
%% gen_server callbacks
|
||||
@ -283,8 +307,11 @@ handle_info({'DOWN', _Ref, _Type, Pid, _Info}, State) ->
|
||||
if is_integer(E#route.local_hint) ->
|
||||
LDomain = E#route.domain,
|
||||
I = E#route.local_hint,
|
||||
ServerHost = E#route.server_host,
|
||||
mnesia:write(#route{domain =
|
||||
LDomain,
|
||||
server_host =
|
||||
ServerHost,
|
||||
pid =
|
||||
undefined,
|
||||
local_hint =
|
||||
@ -394,12 +421,10 @@ get_component_number(LDomain) ->
|
||||
undefined).
|
||||
|
||||
update_tables() ->
|
||||
case catch mnesia:table_info(route, attributes) of
|
||||
[domain, node, pid] -> mnesia:delete_table(route);
|
||||
[domain, pid] -> mnesia:delete_table(route);
|
||||
[domain, pid, local_hint] -> ok;
|
||||
[domain, pid, local_hint|_] -> mnesia:delete_table(route);
|
||||
{'EXIT', _} -> ok
|
||||
try
|
||||
mnesia:transform_table(route, ignore, record_info(fields, route))
|
||||
catch exit:{aborted, {no_exists, _}} ->
|
||||
ok
|
||||
end,
|
||||
case lists:member(local_route,
|
||||
mnesia:system_info(tables))
|
||||
|
@ -222,7 +222,7 @@ wait_for_handshake({xmlstreamelement, El}, StateData) ->
|
||||
send_text(StateData, <<"<handshake/>">>),
|
||||
lists:foreach(
|
||||
fun (H) ->
|
||||
ejabberd_router:register_route(H),
|
||||
ejabberd_router:register_route(H, ?MYNAME),
|
||||
?INFO_MSG("Route registered for service ~p~n",
|
||||
[H])
|
||||
end, dict:fetch_keys(StateData#state.host_opts)),
|
||||
|
@ -86,7 +86,7 @@ stop(Host) ->
|
||||
init([Host, Opts]) ->
|
||||
MyHost = gen_mod:get_opt_host(Host, Opts,
|
||||
<<"echo.@HOST@">>),
|
||||
ejabberd_router:register_route(MyHost),
|
||||
ejabberd_router:register_route(MyHost, Host),
|
||||
{ok, #state{host = MyHost}}.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
|
@ -306,7 +306,7 @@ init({ServerHost, Opts}) ->
|
||||
false ->
|
||||
ok
|
||||
end,
|
||||
ejabberd_router:register_route(Host),
|
||||
ejabberd_router:register_route(Host, ServerHost),
|
||||
{ok, #state{server_host = ServerHost, host = Host, name = Name,
|
||||
access = Access, max_size = MaxSize,
|
||||
secret_length = SecretLength, jid_in_url = JIDinURL,
|
||||
|
@ -133,7 +133,7 @@ init([Host, Opts]) ->
|
||||
catch ets:new(irc_connection,
|
||||
[named_table, public,
|
||||
{keypos, #irc_connection.jid_server_host}]),
|
||||
ejabberd_router:register_route(MyHost),
|
||||
ejabberd_router:register_route(MyHost, Host),
|
||||
{ok,
|
||||
#state{host = MyHost, server_host = Host,
|
||||
access = Access}}.
|
||||
|
@ -168,7 +168,7 @@ init([ServerHost, Opts]) ->
|
||||
?NS_PUBSUB, mod_pubsub, iq_sm, IQDisc),
|
||||
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
||||
?NS_MIX_0, ?MODULE, process_iq, IQDisc),
|
||||
ejabberd_router:register_route(Host),
|
||||
ejabberd_router:register_route(Host, ServerHost),
|
||||
{ok, #state{server_host = ServerHost, host = Host}}.
|
||||
|
||||
handle_call(_Request, _From, State) ->
|
||||
|
@ -373,7 +373,7 @@ init([Host, Opts]) ->
|
||||
RoomShaper = gen_mod:get_opt(room_shaper, Opts,
|
||||
fun(A) when is_atom(A) -> A end,
|
||||
none),
|
||||
ejabberd_router:register_route(MyHost),
|
||||
ejabberd_router:register_route(MyHost, Host),
|
||||
load_permanent_rooms(MyHost, Host,
|
||||
{Access, AccessCreate, AccessAdmin, AccessPersistent},
|
||||
HistorySize, RoomShaper),
|
||||
|
@ -151,7 +151,7 @@ init([LServerS, Opts]) ->
|
||||
try_start_loop(),
|
||||
create_pool(),
|
||||
ejabberd_router_multicast:register_route(LServerS),
|
||||
ejabberd_router:register_route(LServiceS),
|
||||
ejabberd_router:register_route(LServiceS, LServerS),
|
||||
{ok,
|
||||
#state{lservice = LServiceS, lserver = LServerS,
|
||||
access = Access, service_limits = SLimits}}.
|
||||
|
@ -63,7 +63,7 @@ start_link(Host, Opts) ->
|
||||
|
||||
init([Host, Opts]) ->
|
||||
State = parse_options(Host, Opts),
|
||||
ejabberd_router:register_route(State#state.myhost),
|
||||
ejabberd_router:register_route(State#state.myhost, Host),
|
||||
{ok, State}.
|
||||
|
||||
terminate(_Reason, #state{myhost = MyHost}) ->
|
||||
|
@ -241,6 +241,7 @@ stop(Host) ->
|
||||
init([ServerHost, Opts]) ->
|
||||
?DEBUG("pubsub init ~p ~p", [ServerHost, Opts]),
|
||||
Host = gen_mod:get_opt_host(ServerHost, Opts, <<"pubsub.@HOST@">>),
|
||||
ejabberd_router:register_route(Host, ServerHost),
|
||||
Access = gen_mod:get_opt(access_createnode, Opts,
|
||||
fun(A) when is_atom(A) -> A end, all),
|
||||
PepOffline = gen_mod:get_opt(ignore_pep_from_offline, Opts,
|
||||
@ -309,7 +310,6 @@ init([ServerHost, Opts]) ->
|
||||
false ->
|
||||
ok
|
||||
end,
|
||||
ejabberd_router:register_route(Host),
|
||||
pubsub_migrate:update_node_database(Host, ServerHost),
|
||||
pubsub_migrate:update_state_database(Host, ServerHost),
|
||||
pubsub_migrate:update_lastitem_database(Host, ServerHost),
|
||||
|
@ -105,7 +105,7 @@ init(Host, ServerHost, Search) ->
|
||||
case Search of
|
||||
false -> loop(Host, ServerHost);
|
||||
_ ->
|
||||
ejabberd_router:register_route(Host),
|
||||
ejabberd_router:register_route(Host, ServerHost),
|
||||
loop(Host, ServerHost)
|
||||
end.
|
||||
|
||||
|
@ -173,7 +173,7 @@ init([Host, Opts]) ->
|
||||
State#state.password, State#state.tls_options),
|
||||
case State#state.search of
|
||||
true ->
|
||||
ejabberd_router:register_route(State#state.myhost);
|
||||
ejabberd_router:register_route(State#state.myhost, Host);
|
||||
_ -> ok
|
||||
end,
|
||||
{ok, State}.
|
||||
|
Loading…
Reference in New Issue
Block a user