2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_config.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% 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-01-07 20:10:35 +01:00
|
|
|
start() ->
|
2003-01-21 21:36:55 +01:00
|
|
|
%ets:new(ejabberd_config, [named_table, public]),
|
|
|
|
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-01-07 20:10:35 +01:00
|
|
|
load_file(?CONFIG_PATH).
|
|
|
|
|
|
|
|
|
|
|
|
load_file(File) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
case file:consult(File) of
|
|
|
|
{ok, Terms} ->
|
2003-01-18 20:42:48 +01:00
|
|
|
lists:foreach(fun process_term/1, Terms);
|
2003-01-16 21:24:53 +01:00
|
|
|
{error, Reason} ->
|
2003-01-24 21:18:33 +01:00
|
|
|
?ERROR_MSG("~p", [Reason]),
|
2003-01-16 21:24:53 +01:00
|
|
|
exit(file:format_error(Reason))
|
2003-01-07 20:10:35 +01:00
|
|
|
end.
|
|
|
|
|
2003-01-18 20:42:48 +01:00
|
|
|
process_term(Term) ->
|
|
|
|
case Term of
|
|
|
|
{acl, ACLName, ACLData} ->
|
|
|
|
acl:add(ACLName, ACLData);
|
2003-01-21 21:36:55 +01:00
|
|
|
{access, RuleName, Rules} ->
|
|
|
|
add_global_option({access, RuleName}, Rules);
|
2003-01-18 20:42:48 +01:00
|
|
|
{Opt, Val} ->
|
2003-01-21 21:36:55 +01:00
|
|
|
add_option(Opt, Val)
|
|
|
|
end.
|
|
|
|
|
|
|
|
add_option(Opt, Val) ->
|
|
|
|
Table = case Opt of
|
|
|
|
host ->
|
|
|
|
config;
|
|
|
|
_ ->
|
|
|
|
local_config
|
|
|
|
end,
|
|
|
|
case Table of
|
|
|
|
config ->
|
|
|
|
add_global_option(Opt, Val);
|
|
|
|
local_config ->
|
|
|
|
add_local_option(Opt, Val)
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|