Skip to content

Commit

Permalink
Use gen_mod opts in MAM.
Browse files Browse the repository at this point in the history
  • Loading branch information
kzemek committed Jan 8, 2018
1 parent 5fd618f commit 33a626f
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 160 deletions.
18 changes: 17 additions & 1 deletion src/gen_mod.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
set_opt/3,
get_module_opt/4,
set_module_opt/4,
set_module_opts/3,
get_module_opts/2,
get_opt_subhost/3,
get_opt_subhost/4,
Expand Down Expand Up @@ -302,6 +303,20 @@ set_module_opt(Host, Module, Opt, Value) ->
{#ejabberd_module.opts, Updated})
end.

%% @doc Non-atomic! You have been warned.
-spec set_module_opts(ejabberd:server(), module(), _Opts) -> boolean().
set_module_opts(Host, Module, Opts0) ->
Opts = proplists:unfold(Opts0),
Key = {Module, Host},
OptsList = ets:lookup(ejabberd_modules, Key),
case OptsList of
[] ->
false;
[#ejabberd_module{opts = _Opts}] ->
ets:update_element(ejabberd_modules, Key,
{#ejabberd_module.opts, Opts})
end.

-spec set_module_opt_by_subhost(
SubHost :: ejabberd:server(),
Module :: module(),
Expand Down Expand Up @@ -345,7 +360,8 @@ loaded_modules_with_opts(Host) ->

-spec set_module_opts_mnesia(ejabberd:server(), module(), [any()]
) -> {'aborted', _} | {'atomic', _}.
set_module_opts_mnesia(Host, Module, Opts) ->
set_module_opts_mnesia(Host, Module, Opts0) ->
Opts = proplists:unfold(Opts0),
Modules = case ejabberd_config:get_local_option({modules, Host}) of
undefined ->
[];
Expand Down
91 changes: 21 additions & 70 deletions src/mod_mam.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@
-export([lookup_messages/2]).
-export([archive_id_int/2]).

%% for feature (escalus) tests
-export([set_params/1]).
-export([overwrite_params/1]).

%% ----------------------------------------------------------------------
%% Imports

Expand Down Expand Up @@ -197,7 +193,6 @@ start(Host, Opts) ->
" Consider using a <stanza-id/> element instead")
end,

compile_params_module(Opts),
ejabberd_users:start(Host),
%% `parallel' is the only one recommended here.
IQDisc = gen_mod:get_opt(iqdisc, Opts, parallel), %% Type
Expand Down Expand Up @@ -320,8 +315,8 @@ filter_packet({From, To=#jid{luser=LUser, lserver=LServer}, Acc, Packet}) ->
case process_incoming_packet(From, To, PacketWithoutAmp) of
undefined -> {mam_failed, Packet};
MessID -> {archived, maybe_add_arcid_elems(To, MessID, Packet,
add_archived_element(),
add_stanzaid_element())}
add_archived_element(LServer),
add_stanzaid_element(LServer))}
end
end,
Acc1 = mongoose_acc:put(element, PacketAfterArchive, Acc),
Expand Down Expand Up @@ -440,9 +435,9 @@ handle_lookup_messages(#jid{} = From, #jid{} = ArcJID,
Host = server_host(ArcJID),
ArcID = archive_id_int(Host, ArcJID),
QueryID = exml_query:attr(QueryEl, <<"queryid">>, <<>>),
ExtraParamsModule = gen_mod:get_module_opt(Host, ?MODULE, extra_lookup_params, undefined),
Params0 = mam_iq:query_to_lookup_params(IQ, mod_mam_params:max_result_limit(),
mod_mam_params:default_result_limit(),
ExtraParamsModule = param(Host, extra_lookup_params, undefined),
Params0 = mam_iq:query_to_lookup_params(IQ, param(Host, max_result_limit, 50),
param(Host, default_result_limit, 50),
ExtraParamsModule),
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID),
case lookup_messages(Host, Params) of
Expand Down Expand Up @@ -474,9 +469,9 @@ handle_set_message_form(#jid{} = From, #jid{} = ArcJID,
Host = server_host(ArcJID),
ArcID = archive_id_int(Host, ArcJID),
QueryID = exml_query:attr(QueryEl, <<"queryid">>, <<>>),
ExtraParamsModule = gen_mod:get_module_opt(Host, ?MODULE, extra_lookup_params, undefined),
Params0 = mam_iq:form_to_lookup_params(IQ, mod_mam_params:max_result_limit(),
mod_mam_params:default_result_limit(),
ExtraParamsModule = param(Host, extra_lookup_params, undefined),
Params0 = mam_iq:form_to_lookup_params(IQ, param(Host, max_result_limit, 50),
param(Host, default_result_limit, 50),
ExtraParamsModule),
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID),
PageSize = maps:get(page_size, Params),
Expand Down Expand Up @@ -569,7 +564,7 @@ handle_purge_single_message(ArcJID=#jid{},
determine_amp_strategy(Strategy = #amp_strategy{deliver = [none]},
FromJID, ToJID, Packet, initial_check) ->
#jid{luser = LUser, lserver = LServer} = ToJID,
ShouldBeStored = mod_mam_params:is_archivable_message(?MODULE, incoming, Packet)
ShouldBeStored = is_archivable_message(LServer, incoming, Packet)
andalso is_interesting(ToJID, FromJID)
andalso ejabberd_auth:is_user_exists(LUser, LServer),
case ShouldBeStored of
Expand All @@ -587,7 +582,7 @@ handle_package(Dir, ReturnMessID,
RemJID = #jid{},
SrcJID = #jid{}, Packet) ->
Host = server_host(LocJID),
case mod_mam_params:is_archivable_message(?MODULE, Dir, Packet)
case is_archivable_message(Host, Dir, Packet)
andalso should_archive_if_groupchat(Host, exml_query:attr(Packet, <<"type">>)) of
true ->
ArcID = archive_id_int(Host, LocJID),
Expand Down Expand Up @@ -822,59 +817,15 @@ report_issue(Reason, Stacktrace, Issue, #jid{lserver=LServer, luser=LUser}, IQ)
%% ----------------------------------------------------------------------
%% Dynamic params module

%% compile_params_module([
%% {add_archived_element, boolean()}
%% ])
compile_params_module(Params) ->
CodeStr = params_helper(Params),
{Mod, Code} = dynamic_compile:from_string(CodeStr),
code:load_binary(Mod, "mod_mam_params.erl", Code).

params_helper(Params) ->
%% Try is_complete_message opt for backwards compatibility
{IsArchivableModule, IsArchivableFunction} =
case proplists:get_value(is_archivable_message, Params) of
undefined ->
case proplists:get_value(is_complete_message, Params) of
undefined -> {mod_mam_utils, is_archivable_message};
OldStyleMod -> {OldStyleMod, is_complete_message}
end;

Mod -> {Mod, is_archivable_message}
end,
param(Host, Opt) ->
mod_mam_utils:param(?MODULE, Host, Opt).
param(Host, Opt, Default) ->
mod_mam_utils:param(?MODULE, Host, Opt, Default).
is_archivable_message(Host, Dir, Packet) ->
mod_mam_utils:call_is_archivable_message(?MODULE, Host, Dir, Packet).

add_archived_element(Host) ->
mod_mam_utils:add_archived_element(?MODULE, Host).

Format =
io_lib:format(
"-module(mod_mam_params).~n"
"-compile(export_all).~n"
"add_archived_element() -> ~p.~n"
"add_stanzaid_element() -> not ~p.~n"
"is_archivable_message(Mod, Dir, Packet) -> ~p:~p(Mod, Dir, Packet, archive_chat_markers()).~n"
"archive_chat_markers() -> ~p.~n"
"default_result_limit() -> ~p.~n"
"max_result_limit() -> ~p.~n"
"params() -> ~p.~n",
[proplists:get_bool(add_archived_element, Params),
proplists:get_bool(no_stanzaid_element, Params),
IsArchivableModule, IsArchivableFunction,
proplists:get_bool(archive_chat_markers, Params),
proplists:get_value(default_result_limit, Params, 50),
proplists:get_value(max_result_limit, Params, 50),
Params
]),
binary_to_list(iolist_to_binary(Format)).

set_params(Params) ->
compile_params_module(Params ++ mod_mam_params:params()).

overwrite_params(Params) ->
compile_params_module(Params).

%% @doc Enable support for `<archived/>' element from MAM v0.2
-spec add_archived_element() -> boolean().
add_archived_element() ->
mod_mam_params:add_archived_element().

-spec add_stanzaid_element() -> boolean().
add_stanzaid_element() ->
mod_mam_params:add_stanzaid_element().
add_stanzaid_element(Host) ->
mod_mam_utils:add_stanzaid_element(?MODULE, Host).
76 changes: 18 additions & 58 deletions src/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
-export([archive_message/8]).
-export([lookup_messages/2]).
-export([archive_id_int/2]).
-export([set_params/1]).
-export([handle_set_message_form/3]).
-export([handle_lookup_result/4]).
-export([send_messages_and_iq_result/4]).
Expand Down Expand Up @@ -164,7 +163,6 @@ start(Host, Opts) ->
" It is not recommended to use it."
" Consider using a <stanza-id/> element instead")
end,
compile_params_module(Opts),
%% MUC host.
MUCHost = gen_mod:get_opt_subhost(Host, Opts, mod_muc:default_host()),
IQDisc = gen_mod:get_opt(iqdisc, Opts, parallel), %% Type
Expand Down Expand Up @@ -208,8 +206,9 @@ stop(Host) ->
%% @doc Handle public MUC-message.
-spec filter_room_packet(Packet :: packet(), EventData :: list()) -> packet().
filter_room_packet(Packet, EventData) ->
{room_jid, #jid{lserver = LServer}} = lists:keyfind(room_jid, 1, EventData),
?DEBUG("Incoming room packet.", []),
IsArchivable = mod_mam_muc_params:is_archivable_message(?MODULE, incoming, Packet),
IsArchivable = is_archivable_message(LServer, incoming, Packet),
case IsArchivable of
true ->
{_, FromNick} = lists:keyfind(from_nick, 1, EventData),
Expand Down Expand Up @@ -249,8 +248,8 @@ archive_room_packet(Packet, FromNick, FromJID=#jid{}, RoomJID=#jid{}, Role, Affi
maybe_add_arcid_elems(RoomJID,
mess_id_to_external_binary(MessID),
Packet,
add_archived_element(),
add_stanzaid_element());
add_archived_element(Host),
add_stanzaid_element(Host));
{error, _} -> Packet
end;
false -> Packet
Expand Down Expand Up @@ -404,8 +403,8 @@ handle_lookup_messages(#jid{} = From, #jid{} = ArcJID,
ArcID = archive_id_int(Host, ArcJID),
QueryID = exml_query:attr(QueryEl, <<"queryid">>, <<>>),
ExtraParamsModule = gen_mod:get_module_opt(Host, ?MODULE, extra_lookup_params, undefined),
Params0 = mam_iq:query_to_lookup_params(IQ, mod_mam_muc_params:max_result_limit(),
mod_mam_muc_params:default_result_limit(),
Params0 = mam_iq:query_to_lookup_params(IQ, param(Host, max_result_limit, 50),
param(Host, default_result_limit, 50),
ExtraParamsModule),
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID),
case lookup_messages(Host, Params) of
Expand Down Expand Up @@ -437,8 +436,8 @@ handle_set_message_form(#jid{} = From, #jid{} = ArcJID, IQ) ->
{ok, Host} = mongoose_subhosts:get_host(ArcJID#jid.lserver),
ArcID = archive_id_int(Host, ArcJID),
ExtraParamsModule = gen_mod:get_module_opt(Host, ?MODULE, extra_lookup_params, undefined),
Params0 = mam_iq:form_to_lookup_params(IQ, mod_mam_muc_params:max_result_limit(),
mod_mam_muc_params:default_result_limit(),
Params0 = mam_iq:form_to_lookup_params(IQ, param(Host, max_result_limit, 50),
param(Host, default_result_limit, 50),
ExtraParamsModule),
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID),
Result = lookup_messages(Host, Params),
Expand Down Expand Up @@ -770,53 +769,14 @@ report_issue(Reason, Stacktrace, Issue, #jid{lserver = LServer, luser = LUser},
%% ----------------------------------------------------------------------
%% Dynamic params module

%% compile_params_module([
%% {add_archived_element, boolean()}
%% ])
compile_params_module(Params) ->
CodeStr = params_helper(Params),
{Mod, Code} = dynamic_compile:from_string(CodeStr),
code:load_binary(Mod, "mod_mam_muc_params.erl", Code).

params_helper(Params) ->
%% Try is_complete_message opt for backwards compatibility
{IsArchivableModule, IsArchivableFunction} =
case proplists:get_value(is_archivable_message, Params) of
undefined ->
case proplists:get_value(is_complete_message, Params) of
undefined -> {mod_mam_utils, is_archivable_message};
OldStyleMod -> {OldStyleMod, is_complete_message}
end;

Mod -> {Mod, is_archivable_message}
end,
param(Host, Opt, Default) ->
mod_mam_utils:param(?MODULE, Host, Opt, Default).

is_archivable_message(Host, Dir, Packet) ->
mod_mam_utils:call_is_archivable_message(?MODULE, Host, Dir, Packet).

add_archived_element(Host) ->
mod_mam_utils:add_archived_element(?MODULE, Host).

Format =
io_lib:format(
"-module(mod_mam_muc_params).~n"
"-compile(export_all).~n"
"add_archived_element() -> ~p.~n"
"add_stanzaid_element() -> not ~p.~n"
"is_archivable_message(Mod, Dir, Packet) -> ~p:~p(Mod, Dir, Packet, false).~n" %false as we don't support chat markers for groupchats
"default_result_limit() -> ~p.~n"
"max_result_limit() -> ~p.~n"
"params() -> ~p.~n",
[proplists:get_bool(add_archived_element, Params),
proplists:get_bool(no_stanzaid_element, Params),
IsArchivableModule, IsArchivableFunction,
proplists:get_value(default_result_limit, Params, 50),
proplists:get_value(max_result_limit, Params, 50),
Params]),
binary_to_list(iolist_to_binary(Format)).

set_params(Params) ->
compile_params_module(Params).

%% @doc Enable support for `<archived/>' element from MAM v0.2
-spec add_archived_element() -> boolean().
add_archived_element() ->
mod_mam_muc_params:add_archived_element().

-spec add_stanzaid_element() -> boolean().
add_stanzaid_element() ->
mod_mam_muc_params:add_stanzaid_element().
add_stanzaid_element(Host) ->
mod_mam_utils:add_stanzaid_element(?MODULE, Host).
34 changes: 34 additions & 0 deletions src/mod_mam_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
%% SQL
-export([success_sql_query/2, success_sql_execute/3]).

%% Params
-export([param/3, param/4, call_is_archivable_message/4,
add_archived_element/2, add_stanzaid_element/2]).

%% Other
-export([maybe_integer/2,
maybe_min/2,
Expand Down Expand Up @@ -1062,3 +1066,33 @@ error_on_sql_error(_HostOrConn, _Query, Result) ->
-spec wrapper_id() -> binary().
wrapper_id() ->
uuid:uuid_to_string(uuid:get_v4(), binary_standard).

param(Module, Host, Opt) ->
gen_mod:get_module_opt(Host, Module, Opt).

param(Module, Host, Opt, Default) ->
gen_mod:get_module_opt(Host, Module, Opt, Default).

call_is_archivable_message(Module, Host, Dir, Packet) ->
{IsArchivableModule, IsArchivableFunction} =
case param(Module, Host, is_archivable_message, undefined) of
undefined ->
case param(Module, Host, is_complete_message, undefined) of
undefined -> {mod_mam_utils, is_archivable_message};
OldStyleMod -> {OldStyleMod, is_complete_message}
end;

Mod -> {Mod, is_archivable_message}
end,
ArchiveChatMarkers = param(Module, Host, archive_chat_markers, false),
erlang:apply(IsArchivableModule, IsArchivableFunction,
[Module, Dir, Packet, ArchiveChatMarkers]).

%% @doc Enable support for `<archived/>' element from MAM v0.2
%% -spec add_archived_element(Host :: ejabberd:lserver()) -> boolean().
add_archived_element(Module, Host) ->
param(Module, Host, add_archived_element, false).

%% -spec add_stanzaid_element(Host :: ejabberd:lserver()) -> boolean().
add_stanzaid_element(Module, Host) ->
not param(Module, Host, no_stanzaid_element, false).
Loading

0 comments on commit 33a626f

Please sign in to comment.