25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00

mod_muc_room: Use ejabberd hooks instead of function calls to mod_muc_log (#4191)

This commit is contained in:
Badlop 2024-05-07 16:09:40 +02:00
parent 80dc95fc03
commit d04f92d44e
2 changed files with 43 additions and 16 deletions

View File

@ -34,8 +34,8 @@
-behaviour(gen_mod).
%% API
-export([start/2, stop/1, reload/3, get_url/1,
check_access_log/2, add_to_log/5]).
-export([start/2, stop/1, reload/3, get_url/2,
check_access_log/3, add_to_log/5]).
-export([init/1, handle_call/3, handle_cast/2,
handle_info/2, terminate/2, code_change/3,
@ -82,7 +82,9 @@ add_to_log(Host, Type, Data, Room, Opts) ->
gen_server:cast(get_proc_name(Host),
{add_to_log, Type, Data, Room, Opts}).
check_access_log(Host, From) ->
check_access_log(allow, _Host, _From) ->
allow;
check_access_log(_Acc, Host, From) ->
case catch gen_server:call(get_proc_name(Host),
{check_access_log, Host, From})
of
@ -90,8 +92,10 @@ check_access_log(Host, From) ->
Res -> Res
end.
-spec get_url(#state{}) -> {ok, binary()} | error.
get_url(#state{room = Room, host = Host, server_host = ServerHost}) ->
-spec get_url(any(), #state{}) -> {ok, binary()} | error.
get_url({ok, _} = Acc, _State) ->
Acc;
get_url(_Acc, #state{room = Room, host = Host, server_host = ServerHost}) ->
try mod_muc_log_opt:url(ServerHost) of
undefined -> error;
URL ->
@ -115,6 +119,9 @@ depends(_Host, _Opts) ->
init([Host|_]) ->
process_flag(trap_exit, true),
Opts = gen_mod:get_module_opts(Host, ?MODULE),
ejabberd_hooks:add(muc_log_add, Host, ?MODULE, add_to_log, 100),
ejabberd_hooks:add(muc_log_check_access_log, Host, ?MODULE, check_access_log, 100),
ejabberd_hooks:add(muc_log_get_url, Host, ?MODULE, get_url, 100),
{ok, init_state(Host, Opts)}.
handle_call({check_access_log, ServerHost, FromJID}, _From, State) ->
@ -137,7 +144,11 @@ handle_cast(Msg, State) ->
handle_info(_Info, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
terminate(_Reason, #state{host = Host}) ->
ejabberd_hooks:delete(muc_log_add, Host, ?MODULE, add_to_log, 100),
ejabberd_hooks:delete(muc_log_check_access_log, Host, ?MODULE, check_access_log, 100),
ejabberd_hooks:delete(muc_log_get_url, Host, ?MODULE, get_url, 100),
ok.
code_change(_OldVsn, State, _Extra) -> {ok, State}.

View File

@ -3716,8 +3716,10 @@ is_allowed_log_change(Options, StateData, From) ->
false -> true;
true ->
allow ==
mod_muc_log:check_access_log(StateData#state.server_host,
From)
ejabberd_hooks:run_fold(muc_log_check_access_log,
StateData#state.server_host,
deny,
[StateData#state.server_host, From])
end.
-spec is_allowed_persistent_change(muc_roomconfig:result(), state(), jid()) -> boolean().
@ -3856,7 +3858,10 @@ get_config(Lang, StateData, From) ->
[]
end
++
case mod_muc_log:check_access_log(StateData#state.server_host, From) of
case ejabberd_hooks:run_fold(muc_log_check_access_log,
StateData#state.server_host,
deny,
[StateData#state.server_host, From]) of
allow -> [{enablelogging, Config#config.logging}];
deny -> []
end,
@ -4555,7 +4560,10 @@ iq_disco_info_extras(Lang, StateData, Static) ->
end,
Fs4 = case Config#config.logging of
true ->
case mod_muc_log:get_url(StateData) of
case ejabberd_hooks:run_fold(muc_log_get_url,
StateData#state.server_host,
error,
[StateData]) of
{ok, URL} ->
[{logs, URL}|Fs3];
error ->
@ -5334,15 +5342,23 @@ handle_roommessage_from_nonparticipant(Packet, StateData, From) ->
add_to_log(Type, Data, StateData)
when Type == roomconfig_change_disabledlogging ->
mod_muc_log:add_to_log(StateData#state.server_host,
roomconfig_change, Data, StateData#state.jid,
make_opts(StateData, false));
ejabberd_hooks:run(muc_log_add,
StateData#state.server_host,
[StateData#state.server_host,
roomconfig_change,
Data,
StateData#state.jid,
make_opts(StateData, false)]);
add_to_log(Type, Data, StateData) ->
case (StateData#state.config)#config.logging of
true ->
mod_muc_log:add_to_log(StateData#state.server_host,
Type, Data, StateData#state.jid,
make_opts(StateData, false));
ejabberd_hooks:run(muc_log_add,
StateData#state.server_host,
[StateData#state.server_host,
Type,
Data,
StateData#state.jid,
make_opts(StateData, false)]);
false -> ok
end.