2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_config.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2005-06-20 05:18:13 +02:00
|
|
|
%%% Purpose : Load config file
|
2003-01-07 20:10:35 +01:00
|
|
|
%%% Created : 14 Dec 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_config).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
-export([start/0, load_file/1,
|
|
|
|
add_global_option/2, add_local_option/2,
|
|
|
|
get_global_option/1, get_local_option/1]).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
-record(config, {key, value}).
|
|
|
|
-record(local_config, {key, value}).
|
2003-02-01 21:21:28 +01:00
|
|
|
-record(state, {opts = [],
|
2005-06-20 05:18:13 +02:00
|
|
|
hosts = [],
|
2003-02-01 21:21:28 +01:00
|
|
|
override_local = false,
|
|
|
|
override_global = false,
|
|
|
|
override_acls = false}).
|
2003-01-21 21:36:55 +01:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
start() ->
|
2003-01-21 21:36:55 +01:00
|
|
|
mnesia:create_table(config,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, config)}]),
|
|
|
|
mnesia:add_table_copy(config, node(), ram_copies),
|
|
|
|
mnesia:create_table(local_config,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{local_content, true},
|
|
|
|
{attributes, record_info(fields, local_config)}]),
|
|
|
|
mnesia:add_table_copy(local_config, node(), ram_copies),
|
2003-10-12 20:21:16 +02:00
|
|
|
Config = case application:get_env(config) of
|
|
|
|
{ok, Path} -> Path;
|
2004-09-25 22:52:20 +02:00
|
|
|
undefined ->
|
|
|
|
case os:getenv("EJABBERD_CONFIG_PATH") of
|
|
|
|
false ->
|
|
|
|
?CONFIG_PATH;
|
|
|
|
Path ->
|
|
|
|
Path
|
|
|
|
end
|
2003-10-12 20:21:16 +02:00
|
|
|
end,
|
|
|
|
load_file(Config).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
load_file(File) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
case file:consult(File) of
|
|
|
|
{ok, Terms} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State = lists:foldl(fun search_hosts/2, #state{}, Terms),
|
|
|
|
Res = lists:foldl(fun process_term/2, State, Terms),
|
2003-02-01 21:21:28 +01:00
|
|
|
set_opts(Res);
|
2003-01-16 21:24:53 +01:00
|
|
|
{error, Reason} ->
|
2003-10-12 20:21:16 +02:00
|
|
|
?ERROR_MSG("Can't load config file ~p: ~p", [File, Reason]),
|
2003-01-16 21:24:53 +01:00
|
|
|
exit(file:format_error(Reason))
|
2003-01-07 20:10:35 +01:00
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
search_hosts(Term, State) ->
|
|
|
|
case Term of
|
|
|
|
{host, Host} ->
|
|
|
|
if
|
|
|
|
State#state.hosts == [] ->
|
|
|
|
add_option(hosts, [Host], State#state{hosts = [Host]});
|
|
|
|
true ->
|
|
|
|
?ERROR_MSG("Can't load config file: "
|
|
|
|
"too many hosts definitions", []),
|
|
|
|
exit("too many hosts definitions")
|
|
|
|
end;
|
|
|
|
{hosts, Hosts} ->
|
|
|
|
if
|
|
|
|
State#state.hosts == [] ->
|
|
|
|
add_option(hosts, Hosts, State#state{hosts = Hosts});
|
|
|
|
true ->
|
|
|
|
?ERROR_MSG("Can't load config file: "
|
|
|
|
"too many hosts definitions", []),
|
|
|
|
exit("too many hosts definitions")
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
State
|
|
|
|
end.
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
process_term(Term, State) ->
|
2003-01-18 20:42:48 +01:00
|
|
|
case Term of
|
2003-02-01 21:21:28 +01:00
|
|
|
override_global ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{override_global = true};
|
2003-02-01 21:21:28 +01:00
|
|
|
override_local ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{override_local = true};
|
2003-02-01 21:21:28 +01:00
|
|
|
override_acls ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{override_acls = true};
|
2005-06-20 05:18:13 +02:00
|
|
|
{acl, ACLName, ACLData} ->
|
|
|
|
process_host_term(Term, global, State);
|
|
|
|
{access, RuleName, Rules} ->
|
|
|
|
process_host_term(Term, global, State);
|
|
|
|
{shaper, Name, Data} ->
|
|
|
|
lists:foldl(fun(Host, S) -> process_host_term(Term, Host, S) end,
|
|
|
|
State, State#state.hosts);
|
|
|
|
{host, Host} ->
|
|
|
|
State;
|
|
|
|
{hosts, Hosts} ->
|
|
|
|
State;
|
2005-07-31 21:51:52 +02:00
|
|
|
{host_config, Host, Terms} ->
|
|
|
|
lists:foldl(fun(T, S) -> process_host_term(T, Host, S) end,
|
|
|
|
State, Terms);
|
2005-07-03 02:16:34 +02:00
|
|
|
{listen, Val} ->
|
|
|
|
add_option(listen, Val, State);
|
2005-07-13 05:24:13 +02:00
|
|
|
{outgoing_s2s_port, Port} ->
|
|
|
|
add_option(outgoing_s2s_port, Port, State);
|
2005-10-25 03:08:37 +02:00
|
|
|
{s2s_use_starttls, Port} ->
|
|
|
|
add_option(s2s_use_starttls, Port, State);
|
|
|
|
{s2s_certfile, Port} ->
|
|
|
|
add_option(s2s_certfile, Port, State);
|
2005-06-20 05:18:13 +02:00
|
|
|
{Opt, Val} ->
|
|
|
|
lists:foldl(fun(Host, S) -> process_host_term(Term, Host, S) end,
|
|
|
|
State, State#state.hosts)
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_host_term(Term, Host, State) ->
|
|
|
|
case Term of
|
2003-01-18 20:42:48 +01:00
|
|
|
{acl, ACLName, ACLData} ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{opts =
|
2005-06-20 05:18:13 +02:00
|
|
|
[acl:to_record(Host, ACLName, ACLData) | State#state.opts]};
|
2003-01-21 21:36:55 +01:00
|
|
|
{access, RuleName, Rules} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State#state{opts = [#config{key = {access, RuleName, Host},
|
2003-02-02 20:49:19 +01:00
|
|
|
value = Rules} |
|
|
|
|
State#state.opts]};
|
2003-02-09 20:17:23 +01:00
|
|
|
{shaper, Name, Data} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State#state{opts = [#config{key = {shaper, Name, Host},
|
2003-02-09 20:17:23 +01:00
|
|
|
value = Data} |
|
|
|
|
State#state.opts]};
|
2005-04-17 20:08:34 +02:00
|
|
|
{host, Host} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
State;
|
|
|
|
{hosts, Hosts} ->
|
|
|
|
State;
|
2003-01-18 20:42:48 +01:00
|
|
|
{Opt, Val} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
add_option({Opt, Host}, Val, State)
|
2003-01-21 21:36:55 +01:00
|
|
|
end.
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
add_option(Opt, Val, State) ->
|
2003-01-21 21:36:55 +01:00
|
|
|
Table = case Opt of
|
2005-04-17 20:08:34 +02:00
|
|
|
hosts ->
|
2003-01-21 21:36:55 +01:00
|
|
|
config;
|
2004-07-10 00:34:26 +02:00
|
|
|
language ->
|
|
|
|
config;
|
2003-01-21 21:36:55 +01:00
|
|
|
_ ->
|
|
|
|
local_config
|
|
|
|
end,
|
|
|
|
case Table of
|
|
|
|
config ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{opts = [#config{key = Opt, value = Val} |
|
|
|
|
State#state.opts]};
|
2003-01-21 21:36:55 +01:00
|
|
|
local_config ->
|
2003-02-02 20:49:19 +01:00
|
|
|
State#state{opts = [#local_config{key = Opt, value = Val} |
|
|
|
|
State#state.opts]}
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
set_opts(State) ->
|
|
|
|
Opts = lists:reverse(State#state.opts),
|
2003-02-02 20:49:19 +01:00
|
|
|
F = fun() ->
|
|
|
|
if
|
|
|
|
State#state.override_global ->
|
|
|
|
Ksg = mnesia:all_keys(config),
|
|
|
|
lists:foreach(fun(K) ->
|
|
|
|
mnesia:delete({config, K})
|
|
|
|
end, Ksg);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
State#state.override_local ->
|
|
|
|
Ksl = mnesia:all_keys(local_config),
|
|
|
|
lists:foreach(fun(K) ->
|
|
|
|
mnesia:delete({local_config, K})
|
|
|
|
end, Ksl);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
if
|
|
|
|
State#state.override_acls ->
|
|
|
|
Ksa = mnesia:all_keys(acl),
|
|
|
|
lists:foreach(fun(K) ->
|
|
|
|
mnesia:delete({acl, K})
|
|
|
|
end, Ksa);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
lists:foreach(fun(R) ->
|
|
|
|
mnesia:write(R)
|
|
|
|
end, Opts)
|
|
|
|
end,
|
|
|
|
{atomic, _} = mnesia:transaction(F).
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
add_global_option(Opt, Val) ->
|
|
|
|
mnesia:transaction(fun() ->
|
|
|
|
mnesia:write(#config{key = Opt,
|
|
|
|
value = Val})
|
|
|
|
end).
|
|
|
|
|
|
|
|
add_local_option(Opt, Val) ->
|
|
|
|
mnesia:transaction(fun() ->
|
|
|
|
mnesia:write(#local_config{key = Opt,
|
|
|
|
value = Val})
|
|
|
|
end).
|
|
|
|
|
|
|
|
|
|
|
|
get_global_option(Opt) ->
|
|
|
|
case ets:lookup(config, Opt) of
|
|
|
|
[#config{value = Val}] ->
|
|
|
|
Val;
|
|
|
|
_ ->
|
|
|
|
undefined
|
|
|
|
end.
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2003-01-21 21:36:55 +01:00
|
|
|
get_local_option(Opt) ->
|
|
|
|
case ets:lookup(local_config, Opt) of
|
|
|
|
[#local_config{value = Val}] ->
|
2003-01-07 20:10:35 +01:00
|
|
|
Val;
|
|
|
|
_ ->
|
|
|
|
undefined
|
|
|
|
end.
|
|
|
|
|
|
|
|
|