2003-01-24 21:18:33 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : gen_mod.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-01-24 21:18:33 +01:00
|
|
|
%%% Purpose :
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 24 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-01-15 18:02:57 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 Process-one
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-24 21:18:33 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(gen_mod).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-24 21:18:33 +01:00
|
|
|
|
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]);
|
|
|
|
_ ->
|
2007-11-27 23:30:51 +01:00
|
|
|
set_module_opts_mnesia(Host, Module, Opts),
|
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),
|
2007-11-27 23:30:51 +01:00
|
|
|
del_module_mnesia(Host, Module),
|
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),
|
2007-11-27 23:30:51 +01:00
|
|
|
del_module_mnesia(Host, Module),
|
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
|
|
|
_ ->
|
2007-11-27 23:30:51 +01:00
|
|
|
del_module_mnesia(Host, Module),
|
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
|
|
|
|
2007-11-27 23:30:51 +01:00
|
|
|
set_module_opts_mnesia(Host, Module, Opts) ->
|
|
|
|
Modules = case ejabberd_config:get_local_option({modules, Host}) of
|
|
|
|
undefined ->
|
|
|
|
[];
|
|
|
|
Ls ->
|
|
|
|
Ls
|
|
|
|
end,
|
|
|
|
Modules1 = lists:keydelete(Module, 1, Modules),
|
|
|
|
Modules2 = [{Module, Opts} | Modules1],
|
|
|
|
ejabberd_config:add_local_option({modules, Host}, Modules2).
|
|
|
|
|
|
|
|
del_module_mnesia(Host, Module) ->
|
|
|
|
Modules = case ejabberd_config:get_local_option({modules, Host}) of
|
|
|
|
undefined ->
|
|
|
|
[];
|
|
|
|
Ls ->
|
|
|
|
Ls
|
|
|
|
end,
|
|
|
|
Modules1 = lists:keydelete(Module, 1, Modules),
|
|
|
|
ejabberd_config:add_local_option({modules, Host}, Modules1).
|
|
|
|
|
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}).
|
|
|
|
|