diff options
author | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2017-08-08 17:46:26 +0300 |
---|---|---|
committer | Evgeniy Khramtsov <ekhramtsov@process-one.net> | 2017-08-08 17:46:26 +0300 |
commit | 3fec782494042ab1e1a015524e01cd63e6757c7e (patch) | |
tree | 55be45f1d6861e5e8de6c33648a447d87e27eb4c /src/mod_echo.erl | |
parent | Fix tests for 8679cfd2f (diff) |
Introduce 'hosts' option
The option can be used as a replacement of 'host' option
when several (sub)domains are needed to be registered for the module.
Note that you cannot combine both 'host' and 'hosts' in the config
because 'host' option is of a higher priority. Example:
mod_pubsub:
...
hosts:
- "pubsub1.@HOST@"
- "pubsub2.@HOST@"
Fixes #1883
Diffstat (limited to 'src/mod_echo.erl')
-rw-r--r-- | src/mod_echo.erl | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/src/mod_echo.erl b/src/mod_echo.erl index 861b1a0ef..79dd59962 100644 --- a/src/mod_echo.erl +++ b/src/mod_echo.erl @@ -43,7 +43,7 @@ -include("xmpp.hrl"). --record(state, {host = <<"">> :: binary()}). +-record(state, {hosts = [] :: [binary()]}). %%==================================================================== %% gen_mod API @@ -62,7 +62,9 @@ depends(_Host, _Opts) -> []. mod_opt_type(host) -> fun iolist_to_binary/1; -mod_opt_type(_) -> [host]. +mod_opt_type(hosts) -> + fun(L) -> lists:map(fun iolist_to_binary/1, L) end; +mod_opt_type(_) -> [host, hosts]. %%==================================================================== %% gen_server callbacks @@ -77,10 +79,13 @@ mod_opt_type(_) -> [host]. %%-------------------------------------------------------------------- init([Host, Opts]) -> process_flag(trap_exit, true), - MyHost = gen_mod:get_opt_host(Host, Opts, + Hosts = gen_mod:get_opt_hosts(Host, Opts, <<"echo.@HOST@">>), - ejabberd_router:register_route(MyHost, Host), - {ok, #state{host = MyHost}}. + lists:foreach( + fun(H) -> + ejabberd_router:register_route(H, Host) + end, Hosts), + {ok, #state{hosts = Hosts}}. %%-------------------------------------------------------------------- %% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} | @@ -101,17 +106,19 @@ handle_call(stop, _From, State) -> %% Description: Handling cast messages %%-------------------------------------------------------------------- handle_cast({reload, Host, NewOpts, OldOpts}, State) -> - NewMyHost = gen_mod:get_opt_host(Host, NewOpts, - <<"echo.@HOST@">>), - OldMyHost = gen_mod:get_opt_host(Host, OldOpts, - <<"echo.@HOST@">>), - if NewMyHost /= OldMyHost -> - ejabberd_router:register_route(NewMyHost, Host), - ejabberd_router:unregister_route(OldMyHost); - true -> - ok - end, - {noreply, State#state{host = NewMyHost}}; + NewMyHosts = gen_mod:get_opt_hosts(Host, NewOpts, + <<"echo.@HOST@">>), + OldMyHosts = gen_mod:get_opt_hosts(Host, OldOpts, + <<"echo.@HOST@">>), + lists:foreach( + fun(H) -> + ejabberd_router:unregister_route(H) + end, OldMyHosts -- NewMyHosts), + lists:foreach( + fun(H) -> + ejabberd_router:register_route(H, Host) + end, NewMyHosts -- OldMyHosts), + {noreply, State#state{hosts = NewMyHosts}}; handle_cast(Msg, State) -> ?WARNING_MSG("unexpected cast: ~p", [Msg]), {noreply, State}. @@ -147,7 +154,7 @@ handle_info(_Info, State) -> {noreply, State}. %% The return value is ignored. %%-------------------------------------------------------------------- terminate(_Reason, State) -> - ejabberd_router:unregister_route(State#state.host), ok. + lists:foreach(fun ejabberd_router:unregister_route/1, State#state.hosts). %%-------------------------------------------------------------------- %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState} |