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>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(gen_mod).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-01-28 20:45:13 +01:00
|
|
|
-export([start/0,
|
|
|
|
start_module/2,
|
2003-01-24 21:18:33 +01:00
|
|
|
stop_module/1,
|
|
|
|
get_opt/2,
|
2003-01-28 20:45:13 +01:00
|
|
|
get_opt/3,
|
|
|
|
loaded_modules/0]).
|
2003-01-24 21:18:33 +01:00
|
|
|
|
|
|
|
-export([behaviour_info/1]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2003-01-28 20:45:13 +01:00
|
|
|
-record(ejabberd_module, {module, opts}).
|
2003-01-24 21:18:33 +01:00
|
|
|
|
|
|
|
behaviour_info(callbacks) ->
|
|
|
|
[{start, 1},
|
|
|
|
{stop, 0}];
|
|
|
|
behaviour_info(Other) ->
|
|
|
|
undefined.
|
|
|
|
|
2003-01-28 20:45:13 +01:00
|
|
|
start() ->
|
|
|
|
ets:new(ejabberd_modules, [named_table,
|
|
|
|
public,
|
|
|
|
{keypos, #ejabberd_module.module}]),
|
|
|
|
ok.
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
|
|
|
|
start_module(Module, Opts) ->
|
|
|
|
case catch Module:start(Opts) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
|
|
|
_ ->
|
2003-01-28 20:45:13 +01:00
|
|
|
ets:insert(ejabberd_modules, #ejabberd_module{module = Module,
|
|
|
|
opts = Opts}),
|
2003-01-24 21:18:33 +01:00
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
stop_module(Module) ->
|
2003-01-28 20:45:13 +01:00
|
|
|
case catch Module:stop() of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
|
|
|
_ ->
|
|
|
|
ets:delete(ejabberd_modules, Module),
|
|
|
|
ok
|
|
|
|
end.
|
2003-01-24 21:18:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
get_opt(Opt, Opts) ->
|
|
|
|
case lists:keysearch(Opt, 1, Opts) of
|
|
|
|
false ->
|
|
|
|
% TODO: replace with more appropriate function
|
|
|
|
[] = {undefined_option, Opt};
|
|
|
|
{value, {_, Val}} ->
|
|
|
|
Val
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_opt(Opt, Opts, Default) ->
|
|
|
|
case lists:keysearch(Opt, 1, Opts) of
|
|
|
|
false ->
|
|
|
|
Default;
|
|
|
|
{value, {_, Val}} ->
|
|
|
|
Val
|
|
|
|
end.
|
|
|
|
|
2003-01-28 20:45:13 +01:00
|
|
|
loaded_modules() ->
|
|
|
|
ets:select(ejabberd_modules,
|
|
|
|
[{#ejabberd_module{_ = '_', module = '$1'}, [],['$1']}]).
|