]> granicus.if.org Git - ejabberd/commitdiff
Validate and set 'version' option at an earlier stage
authorEvgeny Khramtsov <ekhramtsov@process-one.net>
Sun, 23 Jun 2019 09:23:22 +0000 (12:23 +0300)
committerEvgeny Khramtsov <ekhramtsov@process-one.net>
Sun, 23 Jun 2019 09:23:22 +0000 (12:23 +0300)
src/ejabberd_config.erl
src/ejabberd_options.erl

index 964e84ffb796323beb15314376a7ae9de71c4e42..c5984544f3294d3689d8b537ad7fc148f9bf7772 100644 (file)
@@ -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()},
index c203398782df2dbaba2d21b529ec674b097bfde1..b07c7a2697ad42576476c404311e001c8f8c28b1 100644 (file)
@@ -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, <<"|">>).