2003-01-24 21:18:33 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : gen_mod.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 24 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(gen_mod).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
|
2003-01-28 20:45:13 +01:00
|
|
|
-export([start/0,
|
2005-06-20 05:18:13 +02:00
|
|
|
start_module/3,
|
|
|
|
stop_module/2,
|
2003-01-24 21:18:33 +01:00
|
|
|
get_opt/2,
|
2003-01-28 20:45:13 +01:00
|
|
|
get_opt/3,
|
2007-08-25 19:24:00 +02:00
|
|
|
get_opt_host/3,
|
2005-06-20 05:18:13 +02:00
|
|
|
get_module_opt/4,
|
2007-09-11 13:20:36 +02:00
|
|
|
get_module_opt_host/3,
|
2005-06-20 05:18:13 +02:00
|
|
|
loaded_modules/1,
|
|
|
|
loaded_modules_with_opts/1,
|
|
|
|
get_hosts/2,
|
2006-01-19 03:17:31 +01:00
|
|
|
get_module_proc/2,
|
|
|
|
is_loaded/2]).
|
2003-01-24 21:18:33 +01:00
|
|
|
|
|
|
|
-export([behaviour_info/1]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-record(ejabberd_module, {module_host, opts}).
|
2003-01-24 21:18:33 +01:00
|
|
|
|
|
|
|
behaviour_info(callbacks) ->
|
2005-06-20 05:18:13 +02:00
|
|
|
[{start, 2},
|
|
|
|
{stop, 1}];
|
2005-04-17 20:08:34 +02:00
|
|
|
behaviour_info(_Other) ->
|
2003-01-24 21:18:33 +01:00
|
|
|
undefined.
|
|
|
|
|
2003-01-28 20:45:13 +01:00
|
|
|
start() ->
|
|
|
|
ets:new(ejabberd_modules, [named_table,
|
|
|
|
public,
|
2005-06-20 05:18:13 +02:00
|
|
|
{keypos, #ejabberd_module.module_host}]),
|
2003-01-28 20:45:13 +01:00
|
|
|
ok.
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start_module(Host, Module, Opts) ->
|
|
|
|
case catch Module:start(Host, Opts) of
|
2003-01-24 21:18:33 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
|
|
|
_ ->
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:insert(ejabberd_modules,
|
|
|
|
#ejabberd_module{module_host = {Module, Host},
|
|
|
|
opts = Opts}),
|
2003-01-24 21:18:33 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop_module(Host, Module) ->
|
|
|
|
case catch Module:stop(Host) of
|
2003-01-28 20:45:13 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
2005-05-23 02:30:29 +02:00
|
|
|
{wait, ProcList} when is_list(ProcList) ->
|
|
|
|
lists:foreach(fun wait_for_process/1, ProcList),
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
2005-05-23 02:30:29 +02:00
|
|
|
ok;
|
|
|
|
{wait, Process} ->
|
|
|
|
wait_for_process(Process),
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
2005-05-23 02:30:29 +02:00
|
|
|
ok;
|
2003-01-28 20:45:13 +01:00
|
|
|
_ ->
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:delete(ejabberd_modules, {Module, Host}),
|
2003-01-28 20:45:13 +01:00
|
|
|
ok
|
|
|
|
end.
|
2003-01-24 21:18:33 +01:00
|
|
|
|
2005-05-23 02:30:29 +02:00
|
|
|
wait_for_process(Process) ->
|
|
|
|
MonitorReference = erlang:monitor(process, Process),
|
|
|
|
wait_for_stop(Process, MonitorReference).
|
|
|
|
|
|
|
|
wait_for_stop(Process, MonitorReference) ->
|
|
|
|
receive
|
|
|
|
{'DOWN', MonitorReference, _Type, _Object, _Info} ->
|
|
|
|
ok
|
|
|
|
after 5000 ->
|
|
|
|
catch exit(whereis(Process), kill),
|
|
|
|
wait_for_stop1(MonitorReference)
|
|
|
|
end.
|
|
|
|
|
|
|
|
wait_for_stop1(MonitorReference) ->
|
|
|
|
receive
|
|
|
|
{'DOWN', MonitorReference, _Type, _Object, _Info} ->
|
|
|
|
ok
|
|
|
|
after 5000 ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-01-24 21:18:33 +01:00
|
|
|
|
|
|
|
get_opt(Opt, Opts) ->
|
|
|
|
case lists:keysearch(Opt, 1, Opts) of
|
|
|
|
false ->
|
2007-09-11 13:20:36 +02:00
|
|
|
% TODO: replace with more appropriate function
|
2007-07-26 11:56:38 +02:00
|
|
|
throw({undefined_option, Opt});
|
2003-01-24 21:18:33 +01:00
|
|
|
{value, {_, Val}} ->
|
|
|
|
Val
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_opt(Opt, Opts, Default) ->
|
|
|
|
case lists:keysearch(Opt, 1, Opts) of
|
|
|
|
false ->
|
|
|
|
Default;
|
|
|
|
{value, {_, Val}} ->
|
|
|
|
Val
|
|
|
|
end.
|
|
|
|
|
2007-08-03 10:53:05 +02:00
|
|
|
get_module_opt(global, Module, Opt, Default) ->
|
|
|
|
Hosts = ?MYHOSTS,
|
|
|
|
[Value | Values] = lists:map(
|
|
|
|
fun(Host) ->
|
|
|
|
get_module_opt(Host, Module, Opt, Default)
|
|
|
|
end,
|
|
|
|
Hosts),
|
|
|
|
Same_all = lists:all(
|
2007-09-11 13:20:36 +02:00
|
|
|
fun(Other_value) ->
|
2007-08-03 10:53:05 +02:00
|
|
|
Other_value == Value
|
2007-09-11 13:20:36 +02:00
|
|
|
end,
|
2007-08-03 10:53:05 +02:00
|
|
|
Values),
|
|
|
|
case Same_all of
|
|
|
|
true -> Value;
|
|
|
|
false -> Default
|
|
|
|
end;
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
get_module_opt(Host, Module, Opt, Default) ->
|
|
|
|
OptsList = ets:lookup(ejabberd_modules, {Module, Host}),
|
2004-07-11 22:51:54 +02:00
|
|
|
case OptsList of
|
|
|
|
[] ->
|
|
|
|
Default;
|
|
|
|
[#ejabberd_module{opts = Opts} | _] ->
|
|
|
|
get_opt(Opt, Opts, Default)
|
|
|
|
end.
|
|
|
|
|
2007-09-11 13:20:36 +02:00
|
|
|
get_module_opt_host(Host, Module, Default) ->
|
|
|
|
Val = get_module_opt(Host, Module, host, Default),
|
|
|
|
element(2, regexp:gsub(Val, "@HOST@", Host)).
|
|
|
|
|
2007-08-25 19:24:00 +02:00
|
|
|
get_opt_host(Host, Opts, Default) ->
|
|
|
|
Val = get_opt(host, Opts, Default),
|
|
|
|
element(2, regexp:gsub(Val, "@HOST@", Host)).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
loaded_modules(Host) ->
|
2003-01-28 20:45:13 +01:00
|
|
|
ets:select(ejabberd_modules,
|
2005-06-20 05:18:13 +02:00
|
|
|
[{#ejabberd_module{_ = '_', module_host = {'$1', Host}},
|
2005-05-23 02:30:29 +02:00
|
|
|
[],
|
|
|
|
['$1']}]).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
loaded_modules_with_opts(Host) ->
|
2005-05-23 02:30:29 +02:00
|
|
|
ets:select(ejabberd_modules,
|
2005-06-20 05:18:13 +02:00
|
|
|
[{#ejabberd_module{_ = '_', module_host = {'$1', Host},
|
|
|
|
opts = '$2'},
|
2005-05-23 02:30:29 +02:00
|
|
|
[],
|
|
|
|
[{{'$1', '$2'}}]}]).
|
2004-07-11 22:51:54 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_hosts(Opts, Prefix) ->
|
|
|
|
case catch gen_mod:get_opt(hosts, Opts) of
|
|
|
|
{'EXIT', _Error1} ->
|
|
|
|
case catch gen_mod:get_opt(host, Opts) of
|
|
|
|
{'EXIT', _Error2} ->
|
|
|
|
[Prefix ++ Host || Host <- ?MYHOSTS];
|
|
|
|
Host ->
|
|
|
|
[Host]
|
|
|
|
end;
|
|
|
|
Hosts ->
|
|
|
|
Hosts
|
|
|
|
end.
|
2005-06-20 05:18:13 +02:00
|
|
|
|
|
|
|
get_module_proc(Host, Base) ->
|
|
|
|
list_to_atom(atom_to_list(Base) ++ "_" ++ Host).
|
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
is_loaded(Host, Module) ->
|
|
|
|
ets:member(ejabberd_modules, {Module, Host}).
|
|
|
|
|