From e477a8c2201cb341aeeca23be49cf64188d96567 Mon Sep 17 00:00:00 2001 From: Evgeny Khramtsov Date: Sun, 23 Jun 2019 12:23:22 +0300 Subject: [PATCH] Validate and set 'version' option at an earlier stage --- src/ejabberd_config.erl | 21 +++++++++++++++++++++ src/ejabberd_options.erl | 18 +----------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/ejabberd_config.erl b/src/ejabberd_config.erl index 964e84ffb..c5984544f 100644 --- a/src/ejabberd_config.erl +++ b/src/ejabberd_config.erl @@ -32,6 +32,7 @@ -export([get_shared_key/0, get_node_start/0]). -export([fsm_limit_opts/1]). -export([codec_options/0]). +-export([version/0]). -export([default_db/2, default_db/3, default_ram_db/2, default_ram_db/3]). -export([beams/1, validators/1, globals/0, may_hide_data/1]). -export([dump/0, dump/1, convert_to_yaml/1, convert_to_yaml/2]). @@ -225,6 +226,23 @@ codec_options() -> false -> [ignore_els] end. +%% Do not use this function in runtime: +%% It's slow and doesn't read 'version' option from the config. +%% Use ejabberd_option:version() instead. +-spec version() -> binary(). +version() -> + case application:get_env(ejabberd, custom_vsn) of + {ok, Vsn0} when is_list(Vsn0) -> + list_to_binary(Vsn0); + {ok, Vsn1} when is_binary(Vsn1) -> + Vsn1; + _ -> + case application:get_key(ejabberd, vsn) of + undefined -> <<"">>; + {ok, Vsn} -> list_to_binary(Vsn) + end + end. + -spec default_db(binary() | global, module()) -> atom(). default_db(Host, Module) -> default_db(default_db, Host, Module, mnesia). @@ -500,9 +518,11 @@ validate(Y1) -> case ejabberd_config_transformer:map_reduce(Y2) of {ok, Y3} -> Hosts = proplists:get_value(hosts, Y3), + Version = proplists:get_value(version, Y3, version()), create_tmp_config(), set_option(hosts, Hosts), set_option(host, hd(Hosts)), + set_option(version, Version), set_option(yaml_config, Y3), {Validators, Required} = validators([]), Validator = econf:options(Validators, @@ -522,6 +542,7 @@ pre_validate(Y1) -> econf:options( #{hosts => ejabberd_options:opt_type(hosts), loglevel => ejabberd_options:opt_type(loglevel), + version => ejabberd_options:opt_type(version), host_config => econf:map(econf:binary(), econf:any()), append_host_config => econf:map(econf:binary(), econf:any()), '_' => econf:any()}, diff --git a/src/ejabberd_options.erl b/src/ejabberd_options.erl index c20339878..b07c7a269 100644 --- a/src/ejabberd_options.erl +++ b/src/ejabberd_options.erl @@ -444,6 +444,7 @@ options() -> {default_db, mnesia}, {default_ram_db, mnesia}, {queue_type, ram}, + {version, ejabberd_config:version()}, %% Other options {acl, []}, {access_rules, []}, @@ -638,7 +639,6 @@ options() -> {sql_username, <<"ejabberd">>}, {trusted_proxies, []}, {validate_stream, false}, - {version, fun version/1}, {websocket_origin, []}, {websocket_ping_interval, timer:seconds(60)}, {websocket_timeout, timer:minutes(5)}]. @@ -743,22 +743,6 @@ fqdn(global) -> fqdn(_) -> ejabberd_config:get_option(fqdn). --spec version(global | binary()) -> binary(). -version(global) -> - case application:get_env(ejabberd, custom_vsn) of - {ok, Vsn0} when is_list(Vsn0) -> - list_to_binary(Vsn0); - {ok, Vsn1} when is_binary(Vsn1) -> - Vsn1; - _ -> - case application:get_key(ejabberd, vsn) of - undefined -> <<"">>; - {ok, Vsn} -> list_to_binary(Vsn) - end - end; -version(_) -> - ejabberd_config:get_option(version). - -spec concat_tls_protocol_options([binary()]) -> binary(). concat_tls_protocol_options(Opts) -> str:join(Opts, <<"|">>). -- 2.40.0