./ct_report/coverage/mod_mam_muc.COVER.html

1 %%%-------------------------------------------------------------------
2 %%% @author Uvarov Michael <arcusfelis@gmail.com>
3 %%% @copyright (C) 2013, Uvarov Michael
4 %%% @doc XEP-0313: Message Archive Management
5 %%%
6 %%% The module uses several backend modules:
7 %%%
8 %%% <ul>
9 %%% <li>Preference manager ({@link mod_mam_muc_rdbms_prefs});</li>
10 %%% <li>Writer ({@link mod_mam_muc_rdbms_arch} or {@link mod_mam_muc_rdbms_async_pool_writer});</li>
11 %%% <li>Archive manager ({@link mod_mam_muc_rdbms_arch});</li>
12 %%% <li>User's ID generator ({@link mod_mam_muc_user}).</li>
13 %%% </ul>
14 %%%
15 %%% Preferences can be also stored in Mnesia ({@link mod_mam_mnesia_prefs}).
16 %%% This module handles MUC archives.
17 %%%
18 %%% This module should be started for each host.
19 %%% Message archivation is not shaped here (use standard support for this).
20 %%% MAM's IQs are shaped inside {@link opuntia_srv}.
21 %%%
22 %%% Message identifiers (or UIDs in the spec) are generated based on:
23 %%%
24 %%% <ul>
25 %%% <li>date (using `timestamp()');</li>
26 %%% <li>node number (using {@link mongoose_node_num}).</li>
27 %%% </ul>
28 %%% @end
29 %%%-------------------------------------------------------------------
30 -module(mod_mam_muc).
31 %% ----------------------------------------------------------------------
32 %% Exports
33
34 %% Client API
35 -export([delete_archive/2,
36 archive_size/2,
37 archive_id/2]).
38
39 %% gen_mod handlers
40 -export([start/2, stop/1, supported_features/0]).
41
42 %% ejabberd room handlers
43 -export([disco_muc_features/3,
44 filter_room_packet/3,
45 forget_room/3]).
46
47 -export([room_process_mam_iq/5]).
48
49 %% gdpr callback
50 -export([get_personal_data/3]).
51
52 %% private
53 -export([archive_message_for_ct/1]).
54 -export([lookup_messages/2]).
55 -export([archive_id_int/2]).
56
57 -ignore_xref([archive_id/2, archive_message_for_ct/1, archive_size/2, delete_archive/2,
58 start/2, stop/1, supported_features/0]).
59
60 -include_lib("mongoose.hrl").
61 -include_lib("jlib.hrl").
62 -include_lib("exml/include/exml.hrl").
63
64 -callback is_complete_message(Module :: atom(), Dir :: atom(), Packet :: any()) ->
65 boolean().
66
67 %% ----------------------------------------------------------------------
68 %% Other types
69 -type packet() :: any().
70 -type row_batch() :: {TotalCount :: non_neg_integer(),
71 Offset :: non_neg_integer(),
72 MessageRows :: [row()]}.
73 -type row() :: mod_mam:message_row().
74 -type host_type() :: mongooseim:host_type().
75 -type muc_action() :: atom().
76
77 -export_type([row/0, row_batch/0]).
78
79 %% ----------------------------------------------------------------------
80 %% API
81
82 -spec get_personal_data(Acc, Params, Extra) -> {ok, Acc} when
83 Acc :: gdpr:personal_data(),
84 Params :: #{jid := jid:jid()},
85 Extra :: gen_hook:extra().
86 get_personal_data(Acc, #{jid := ArcJID}, #{host_type := HostType}) ->
87 40 Schema = ["id", "message"],
88 40 Entries = mongoose_hooks:get_mam_muc_gdpr_data(HostType, ArcJID),
89 40 {ok, [{mam_muc, Schema, Entries} | Acc]}.
90
91 -spec delete_archive(jid:server(), jid:user()) -> ok.
92 delete_archive(MucHost, RoomName) when is_binary(MucHost), is_binary(RoomName) ->
93 590 ?LOG_DEBUG(#{what => mam_delete_room, room => RoomName, sub_host => MucHost}),
94 590 ArcJID = jid:make_bare(RoomName, MucHost),
95 590 HostType = mod_muc_light_utils:room_jid_to_host_type(ArcJID),
96 590 ArcID = archive_id_int(HostType, ArcJID),
97 590 remove_archive(HostType, ArcID, ArcJID),
98 590 ok.
99
100 -spec archive_size(jid:server(), jid:user()) -> integer().
101 archive_size(MucHost, RoomName) when is_binary(MucHost), is_binary(RoomName) ->
102 639 ArcJID = jid:make_bare(RoomName, MucHost),
103 639 HostType = mod_muc_light_utils:room_jid_to_host_type(ArcJID),
104 639 ArcID = archive_id_int(HostType, ArcJID),
105 639 archive_size(HostType, ArcID, ArcJID).
106
107 -spec archive_id(jid:server(), jid:user()) -> integer().
108 archive_id(MucHost, RoomName) when is_binary(MucHost), is_binary(RoomName) ->
109 28 ArcJID = jid:make_bare(RoomName, MucHost),
110 28 HostType = mod_muc_light_utils:room_jid_to_host_type(ArcJID),
111 28 archive_id_int(HostType, ArcJID).
112
113 %% ----------------------------------------------------------------------
114 %% gen_mod callbacks
115 %% Starting and stopping functions for MUC archives
116
117 -spec start(host_type(), gen_mod:module_opts()) -> any().
118 start(HostType, Opts) ->
119 67 ?LOG_DEBUG(#{what => mam_muc_starting}),
120 67 ensure_metrics(HostType),
121 67 gen_hook:add_handlers(hooks(HostType)),
122 67 add_iq_handlers(HostType, Opts),
123 67 ok.
124
125 -spec stop(host_type()) -> any().
126 stop(HostType) ->
127 67 ?LOG_DEBUG(#{what => mam_muc_stopping}),
128 67 gen_hook:delete_handlers(hooks(HostType)),
129 67 remove_iq_handlers(HostType),
130 67 ok.
131
132 -spec supported_features() -> [atom()].
133 supported_features() ->
134 67 [dynamic_domains].
135
136 %% ----------------------------------------------------------------------
137 %% hooks and handlers for MUC
138
139 -spec disco_muc_features(Acc, Params, Extra) -> {ok, Acc} when
140 Acc :: mongoose_disco:feature_acc(),
141 Params :: map(),
142 Extra :: gen_hook:extra().
143 disco_muc_features(Acc = #{host_type := HostType, node := <<>>}, _Params, _Extra) ->
144 20 {ok, mongoose_disco:add_features(mod_mam_utils:features(?MODULE, HostType), Acc)};
145 disco_muc_features(Acc, _Params, _Extra) ->
146
:-(
{ok, Acc}.
147
148 %% @doc Handle public MUC-message.
149 -spec filter_room_packet(Packet, EventData, Extra) -> {ok, Packet} when
150 Packet :: exml:element(),
151 EventData :: mod_muc:room_event_data(),
152 Extra :: gen_hook:extra().
153 filter_room_packet(Packet, EventData, #{host_type := HostType}) ->
154 569 ?LOG_DEBUG(#{what => mam_room_packet, text => <<"Incoming room packet">>,
155 569 packet => Packet, event_data => EventData}),
156 569 IsArchivable = is_archivable_message(HostType, incoming, Packet),
157 569 case IsArchivable of
158 true ->
159 547 #{from_nick := FromNick, from_jid := FromJID, room_jid := RoomJID,
160 role := Role, affiliation := Affiliation, timestamp := TS} = EventData,
161 547 {ok, archive_room_packet(HostType, Packet, FromNick, FromJID,
162 RoomJID, Role, Affiliation, TS)};
163 false ->
164 22 {ok, Packet}
165 end.
166
167 %% @doc Archive without validation.
168 -spec archive_room_packet(HostType :: host_type(),
169 Packet :: packet(), FromNick :: jid:user(),
170 FromJID :: jid:jid(), RoomJID :: jid:jid(),
171 Role :: mod_muc:role(), Affiliation :: mod_muc:affiliation(),
172 TS :: integer()) -> packet().
173 archive_room_packet(HostType, Packet, FromNick, FromJID = #jid{},
174 RoomJID = #jid{}, Role, Affiliation, TS) ->
175 547 ArcID = archive_id_int(HostType, RoomJID),
176 %% Occupant JID <room@service/nick>
177 547 SrcJID = jid:replace_resource(RoomJID, FromNick),
178 547 IsMamMucEnabled = mod_mam_utils:is_mam_muc_enabled(RoomJID#jid.lserver, HostType),
179 547 IsInteresting =
180 case get_behaviour(HostType, ArcID, RoomJID, SrcJID) of
181 547 always -> true;
182
:-(
never -> false;
183
:-(
roster -> true
184 end,
185 547 case IsInteresting andalso IsMamMucEnabled of
186 true ->
187 543 MessID = mod_mam_utils:generate_message_id(TS),
188 543 Packet1 = mod_mam_utils:replace_x_user_element(FromJID, Role, Affiliation, Packet),
189 543 OriginID = mod_mam_utils:get_origin_id(Packet),
190 543 Params = #{message_id => MessID,
191 archive_id => ArcID,
192 local_jid => RoomJID,
193 remote_jid => FromJID,
194 source_jid => SrcJID,
195 origin_id => OriginID,
196 direction => incoming,
197 packet => Packet1},
198 %% Packet to be broadcasted and packet to be archived are
199 %% not 100% the same
200 543 Result = archive_message(HostType, Params),
201 543 case Result of
202 ok ->
203 543 ExtID = mod_mam_utils:mess_id_to_external_binary(MessID),
204 543 ShouldAdd = mod_mam_params:add_stanzaid_element(?MODULE, HostType),
205 543 mod_mam_utils:maybe_add_arcid_elems(RoomJID, ExtID, Packet, ShouldAdd);
206
:-(
{error, _} -> Packet
207 end;
208 4 false -> Packet
209 end.
210
211 %% @doc `To' is an account or server entity hosting the archive.
212 %% Servers that archive messages on behalf of local users SHOULD expose archives
213 %% to the user on their bare JID (i.e. `From.luser'),
214 %% while a MUC service might allow MAM queries to be sent to the room's bare JID
215 %% (i.e `To.luser').
216 -spec room_process_mam_iq(Acc :: mongoose_acc:t(),
217 From :: jid:jid(),
218 To :: jid:jid(),
219 IQ :: jlib:iq(),
220 Extra :: gen_hook:extra()) -> {mongoose_acc:t(), jlib:iq() | ignore}.
221 room_process_mam_iq(Acc, From, To, IQ, #{host_type := HostType}) ->
222 381 mod_mam_utils:maybe_log_deprecation(IQ),
223 381 Action = mam_iq:action(IQ),
224 381 MucAction = action_to_muc_action(Action),
225 381 case check_action_allowed(HostType, Acc, To#jid.lserver, Action, MucAction, From, To) of
226 ok ->
227 367 case mod_mam_utils:wait_shaper(HostType, To#jid.lserver, MucAction, From) of
228 continue ->
229 367 handle_error_iq(Acc, HostType, To, Action,
230 handle_mam_iq(HostType, Action, From, To, IQ));
231 {error, max_delay_reached} ->
232
:-(
mongoose_metrics:update(HostType, modMucMamDroppedIQ, 1),
233
:-(
{Acc, return_max_delay_reached_error_iq(IQ)}
234 end;
235 {error, Reason} ->
236 14 ?LOG_WARNING(#{what => action_not_allowed,
237 action => Action, acc => Acc, reason => Reason,
238
:-(
can_access_room => can_access_room(HostType, Acc, From, To)}),
239 14 {Acc, return_action_not_allowed_error_iq(Reason, IQ)}
240 end.
241
242 -spec forget_room(Acc, Params, Extra) -> {ok, Acc} when
243 Acc :: term(),
244 Params :: #{muc_host := jid:server(), room := jid:luser()},
245 Extra :: gen_hook:extra().
246 forget_room(Acc, #{muc_host := MucServer, room := RoomName}, _Extra) ->
247 211 delete_archive(MucServer, RoomName),
248 211 {ok, Acc}.
249
250 %% ----------------------------------------------------------------------
251 %% Internal functions
252
253 -spec check_action_allowed(host_type(), mongoose_acc:t(), jid:lserver(), mam_iq:action(), muc_action(),
254 jid:jid(), jid:jid()) -> ok | {error, binary()}.
255 check_action_allowed(HostType, Acc, Domain, Action, MucAction, From, To) ->
256 381 case acl:match_rule(HostType, Domain, MucAction, From, default) of
257
:-(
allow -> ok;
258
:-(
deny -> {false, <<"Blocked by service policy.">>};
259 381 default -> check_room_action_allowed_by_default(HostType, Acc, Action, From, To)
260 end.
261
262 -spec action_to_muc_action(mam_iq:action()) -> atom().
263 action_to_muc_action(Action) ->
264 381 list_to_atom("muc_" ++ atom_to_list(Action)).
265
266 -spec check_room_action_allowed_by_default(HostType :: host_type(),
267 Acc :: mongoose_acc:t(),
268 Action :: mam_iq:action(),
269 From :: jid:jid(),
270 To :: jid:jid()) -> ok | {error, binary()}.
271 check_room_action_allowed_by_default(HostType, Acc, Action, From, To) ->
272 381 case mam_iq:action_type(Action) of
273 set ->
274
:-(
case is_room_owner(HostType, Acc, From, To) of
275
:-(
true -> ok;
276
:-(
false -> {error, <<"Not a room owner.">>}
277 end;
278 get ->
279 381 case can_access_room(HostType, Acc, From, To) of
280 367 true -> ok;
281 14 false -> {error, <<"Not allowed to enter the room.">>}
282 end
283 end.
284
285 -spec is_room_owner(HostType :: host_type(),
286 Acc :: mongoose_acc:t(),
287 UserJid :: jid:jid(),
288 RoomJid :: jid:jid()) -> boolean().
289 is_room_owner(HostType, Acc, UserJid, RoomJid) ->
290
:-(
mongoose_hooks:is_muc_room_owner(HostType, Acc, UserJid, RoomJid).
291
292 %% @doc Return true if user element should be removed from results
293 -spec is_user_identity_hidden(HostType :: host_type(),
294 UserJid :: jid:jid(),
295 RoomJid :: jid:jid()) -> boolean().
296 is_user_identity_hidden(HostType, UserJid, RoomJid) ->
297 276 case mongoose_hooks:can_access_identity(HostType, RoomJid, UserJid) of
298 276 CanAccess when is_boolean(CanAccess) -> not CanAccess
299 end.
300
301 -spec can_access_room(HostType :: host_type(),
302 Acc :: mongoose_acc:t(),
303 UserJid :: jid:jid(),
304 RoomJid :: jid:jid()) -> boolean().
305 can_access_room(HostType, Acc, UserJid, RoomJid) ->
306 395 mongoose_hooks:can_access_room(HostType, Acc, RoomJid, UserJid).
307
308 -spec handle_mam_iq(HostType :: host_type(), mam_iq:action(),
309 From :: jid:jid(), jid:jid(), jlib:iq()) ->
310 jlib:iq() | {error, any(), jlib:iq()} | ignore.
311 handle_mam_iq(HostType, Action, From, To, IQ) ->
312 367 case Action of
313 mam_get_prefs ->
314
:-(
handle_get_prefs(HostType, To, IQ);
315 mam_set_prefs ->
316
:-(
handle_set_prefs(HostType, To, IQ);
317 mam_set_message_form ->
318 367 handle_set_message_form(HostType, From, To, IQ);
319 mam_get_message_form ->
320
:-(
handle_get_message_form(HostType, From, To, IQ)
321 end.
322
323 -spec handle_set_prefs(host_type(), jid:jid(), jlib:iq()) ->
324 jlib:iq() | {error, any(), jlib:iq()}.
325 handle_set_prefs(HostType, ArcJID = #jid{},
326 IQ = #iq{sub_el = PrefsEl}) ->
327
:-(
{DefaultMode, AlwaysJIDs, NeverJIDs} = mod_mam_utils:parse_prefs(PrefsEl),
328
:-(
?LOG_DEBUG(#{what => mam_muc_set_prefs, archive_jid => ArcJID,
329 default_mode => DefaultMode,
330
:-(
always_jids => AlwaysJIDs, never_jids => NeverJIDs, iq => IQ}),
331
:-(
ArcID = archive_id_int(HostType, ArcJID),
332
:-(
Res = set_prefs(HostType, ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs),
333
:-(
handle_set_prefs_result(Res, DefaultMode, AlwaysJIDs, NeverJIDs, IQ).
334
335 handle_set_prefs_result(ok, DefaultMode, AlwaysJIDs, NeverJIDs, IQ) ->
336
:-(
ResultPrefsEl = mod_mam_utils:result_prefs(DefaultMode, AlwaysJIDs, NeverJIDs, IQ#iq.xmlns),
337
:-(
IQ#iq{type = result, sub_el = [ResultPrefsEl]};
338 handle_set_prefs_result({error, Reason},
339 _DefaultMode, _AlwaysJIDs, _NeverJIDs, IQ) ->
340
:-(
return_error_iq(IQ, Reason).
341
342 -spec handle_get_prefs(host_type(), jid:jid(), jlib:iq()) ->
343 jlib:iq() | {error, any(), jlib:iq()}.
344 handle_get_prefs(HostType, ArcJID=#jid{}, IQ=#iq{}) ->
345
:-(
ArcID = archive_id_int(HostType, ArcJID),
346
:-(
Res = get_prefs(HostType, ArcID, ArcJID, always),
347
:-(
handle_get_prefs_result(ArcJID, Res, IQ).
348
349 handle_get_prefs_result(ArcJID, {DefaultMode, AlwaysJIDs, NeverJIDs}, IQ) ->
350
:-(
?LOG_DEBUG(#{what => mam_muc_get_prefs_result, archive_jid => ArcJID,
351 default_mode => DefaultMode,
352
:-(
always_jids => AlwaysJIDs, never_jids => NeverJIDs, iq => IQ}),
353
:-(
ResultPrefsEl = mod_mam_utils:result_prefs(DefaultMode, AlwaysJIDs, NeverJIDs, IQ#iq.xmlns),
354
:-(
IQ#iq{type = result, sub_el = [ResultPrefsEl]};
355 handle_get_prefs_result(_ArcJID, {error, Reason}, IQ) ->
356
:-(
return_error_iq(IQ, Reason).
357
358 -spec handle_set_message_form(HostType :: host_type(),
359 From :: jid:jid(), ArcJID :: jid:jid(),
360 IQ :: jlib:iq()) ->
361 jlib:iq() | ignore | {error, term(), jlib:iq()}.
362 handle_set_message_form(HostType, #jid{} = From, #jid{} = ArcJID, IQ) ->
363 367 ArcID = archive_id_int(HostType, ArcJID),
364 367 ResLimit = mod_mam_params:max_result_limit(?MODULE, HostType),
365 367 DefLimit = mod_mam_params:default_result_limit(?MODULE, HostType),
366 367 ExtMod = mod_mam_params:extra_params_module(?MODULE, HostType),
367 367 Sim = mod_mam_params:enforce_simple_queries(?MODULE, HostType),
368 367 try mam_iq:form_to_lookup_params(IQ, ResLimit, DefLimit, ExtMod, Sim) of
369 Params0 ->
370 360 do_handle_set_message_form(HostType, From, ArcID, ArcJID, IQ, Params0)
371 catch _C:R:S ->
372 7 report_issue({R, S}, mam_lookup_failed, ArcJID, IQ),
373 7 return_error_iq(IQ, R)
374 end.
375
376
377 -spec do_handle_set_message_form(HostType :: mongooseim:host_type(),
378 From :: jid:jid(),
379 ArcId :: mod_mam:archive_id(),
380 ArcJID :: jid:jid(),
381 IQ :: jlib:iq(),
382 Params :: mam_iq:lookup_params()) ->
383 jlib:iq() | ignore | {error, term(), jlib:iq()}.
384 do_handle_set_message_form(HostType, From, ArcID, ArcJID, IQ, Params0) ->
385 360 Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID, From),
386 360 Result = mod_mam_utils:lookup(HostType, Params, fun lookup_messages/2),
387 360 handle_lookup_result(Result, HostType, From, IQ, Params).
388
389 -spec handle_lookup_result({ok, mod_mam:lookup_result()} | {error, term()},
390 host_type(), jid:jid(), jlib:iq(), map()) ->
391 jlib:iq() | ignore | {error, term(), jlib:iq()}.
392 handle_lookup_result(Result, HostType, From, IQ, #{owner_jid := ArcJID} = Params) ->
393 360 case Result of
394 {error, Reason} ->
395 14 report_issue(Reason, mam_muc_lookup_failed, ArcJID, IQ),
396 14 return_error_iq(IQ, Reason);
397 {ok, Res} ->
398 346 send_messages_and_iq_result(Res, HostType, From, IQ, Params)
399 end.
400
401 send_messages_and_iq_result(#{total_count := TotalCount, offset := Offset,
402 messages := MessageRows, is_complete := IsComplete},
403 HostType, From,
404 #iq{xmlns = MamNs, sub_el = QueryEl} = IQ,
405 #{owner_jid := ArcJID} = Params) ->
406 %% Forward messages
407 346 QueryID = exml_query:attr(QueryEl, <<"queryid">>, <<>>),
408 346 {FirstMessID, LastMessID} = forward_messages(HostType, From, ArcJID, MamNs,
409 QueryID, MessageRows, true),
410 %% Make fin iq
411 346 IsStable = true,
412 346 ResultSetEl = mod_mam_utils:result_set(FirstMessID, LastMessID, Offset, TotalCount),
413 346 ExtFinMod = mod_mam_params:extra_fin_element_module(?MODULE, HostType),
414 346 FinElem = mod_mam_utils:make_fin_element(HostType, Params, IQ#iq.xmlns,
415 IsComplete, IsStable,
416 ResultSetEl, ExtFinMod),
417 346 IQ#iq{type = result, sub_el = [FinElem]}.
418
419 forward_messages(HostType, From, ArcJID, MamNs, QueryID, MessageRows, SetClientNs) ->
420 %% Forward messages
421 346 {FirstMessID, LastMessID, HideUser} =
422 case MessageRows of
423 70 [] -> {undefined, undefined, undefined};
424 276 [_ | _] -> {message_row_to_ext_id(hd(MessageRows)),
425 message_row_to_ext_id(lists:last(MessageRows)),
426 is_user_identity_hidden(HostType, From, ArcJID)}
427 end,
428 346 SendModule = mod_mam_params:send_message_mod(?MODULE, HostType),
429 346 [send_message(SendModule, Row, ArcJID, From,
430 message_row_to_xml(MamNs, From, HideUser, SetClientNs, Row,
431 QueryID))
432 346 || Row <- MessageRows],
433 346 {FirstMessID, LastMessID}.
434
435 send_message(SendModule, Row, ArcJID, From, Packet) ->
436 1522 mam_send_message:call_send_message(SendModule, Row, ArcJID, From, Packet).
437
438 -spec handle_get_message_form(host_type(), jid:jid(), jid:jid(), jlib:iq()) ->
439 jlib:iq().
440 handle_get_message_form(HostType,
441 _From = #jid{}, _ArcJID = #jid{}, IQ = #iq{}) ->
442
:-(
return_message_form_iq(HostType, IQ).
443
444 %% ----------------------------------------------------------------------
445 %% Backend wrappers
446
447 -spec archive_id_int(HostType :: host_type(), ArcJID :: jid:jid()) ->
448 integer() | undefined.
449 archive_id_int(HostType, ArcJID = #jid{}) ->
450 2198 mongoose_hooks:mam_muc_archive_id(HostType, ArcJID).
451
452 -spec archive_size(HostType :: host_type(), ArcID :: mod_mam:archive_id(),
453 ArcJID ::jid:jid()) -> non_neg_integer().
454 archive_size(HostType, ArcID, ArcJID = #jid{}) ->
455 639 mongoose_hooks:mam_muc_archive_size(HostType, ArcID, ArcJID).
456
457 -spec get_behaviour(HostType :: host_type(), ArcID :: mod_mam:archive_id(),
458 LocJID :: jid:jid(), RemJID :: jid:jid()) -> any().
459 get_behaviour(HostType, ArcID, LocJID = #jid{}, RemJID = #jid{}) ->
460 547 mongoose_hooks:mam_muc_get_behaviour(HostType, ArcID, LocJID, RemJID).
461
462 -spec set_prefs(HostType :: host_type(), ArcID :: mod_mam:archive_id(),
463 ArcJID :: jid:jid(), DefaultMode :: mod_mam:archive_behaviour(),
464 AlwaysJIDs :: [jid:literal_jid()],
465 NeverJIDs :: [jid:literal_jid()]) -> any().
466 set_prefs(HostType, ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs) ->
467
:-(
mongoose_hooks:mam_muc_set_prefs(HostType, ArcID, ArcJID, DefaultMode,
468 AlwaysJIDs, NeverJIDs).
469
470 %% @doc Load settings from the database.
471 -spec get_prefs(HostType :: host_type(), ArcID :: mod_mam:archive_id(),
472 ArcJID :: jid:jid(), GlobalDefaultMode :: mod_mam:archive_behaviour())
473 -> mod_mam:preference() | {error, Reason :: term()}.
474 get_prefs(HostType, ArcID, ArcJID, GlobalDefaultMode) ->
475
:-(
mongoose_hooks:mam_muc_get_prefs(HostType, GlobalDefaultMode, ArcID, ArcJID).
476
477 -spec remove_archive(host_type(), mod_mam:archive_id() | undefined,
478 jid:jid()) -> ok.
479 remove_archive(HostType, ArcID, ArcJID = #jid{}) ->
480 590 mongoose_hooks:mam_muc_remove_archive(HostType, ArcID, ArcJID),
481 590 ok.
482
483 %% See description in mod_mam_pm.
484 -spec lookup_messages(HostType :: host_type(), Params :: map()) ->
485 {ok, mod_mam:lookup_result()}
486 | {error, 'policy-violation'}
487 | {error, Reason :: term()}.
488 lookup_messages(HostType, Params) ->
489 386 Result = lookup_messages_without_policy_violation_check(HostType, Params),
490 %% If a query returns a number of stanzas greater than this limit and the
491 %% client did not specify a limit using RSM then the server should return
492 %% a policy-violation error to the client.
493 386 mod_mam_utils:check_result_for_policy_violation(Params, Result).
494
495 lookup_messages_without_policy_violation_check(HostType,
496 #{search_text := SearchText} = Params) ->
497 386 case SearchText /= undefined andalso
498 7 not mod_mam_params:has_full_text_search(?MODULE, HostType) of
499 true -> %% Use of disabled full text search
500
:-(
{error, 'not-supported'};
501 false ->
502 386 StartT = erlang:monotonic_time(microsecond),
503 386 R = mongoose_hooks:mam_muc_lookup_messages(HostType, Params),
504 386 Diff = erlang:monotonic_time(microsecond) - StartT,
505 386 mongoose_metrics:update(HostType, [backends, ?MODULE, lookup], Diff),
506 386 R
507 end.
508
509 archive_message_for_ct(Params = #{local_jid := RoomJid}) ->
510 960 HostType = mod_muc_light_utils:room_jid_to_host_type(RoomJid),
511 960 archive_message(HostType, Params).
512
513 -spec archive_message(host_type(), mod_mam:archive_message_params()) -> ok | {error, timeout}.
514 archive_message(HostType, Params) ->
515 1503 mongoose_hooks:mam_muc_archive_message(HostType, Params).
516
517 %% ----------------------------------------------------------------------
518 %% Helpers
519
520 -spec message_row_to_xml(binary(), jid:jid(), boolean(), boolean(), row(), binary() | undefined) ->
521 exml:element().
522 message_row_to_xml(MamNs, ReceiverJID, HideUser, SetClientNs,
523 #{id := MessID, jid := SrcJID, packet := Packet}, QueryID) ->
524 1522 {Microseconds, _NodeMessID} = mod_mam_utils:decode_compact_uuid(MessID),
525 1522 TS = calendar:system_time_to_rfc3339(Microseconds, [{offset, "Z"}, {unit, microsecond}]),
526 1522 BExtMessID = mod_mam_utils:mess_id_to_external_binary(MessID),
527 1522 Packet1 = maybe_delete_x_user_element(HideUser, ReceiverJID, Packet),
528 1522 Packet2 = mod_mam_utils:maybe_set_client_xmlns(SetClientNs, Packet1),
529 1522 Packet3 = replace_from_to_attributes(SrcJID, Packet2),
530 1522 mod_mam_utils:wrap_message(MamNs, Packet3, QueryID, BExtMessID, TS, SrcJID).
531
532 maybe_delete_x_user_element(true, ReceiverJID, Packet) ->
533 122 PacketJID = mod_mam_utils:packet_to_x_user_jid(Packet),
534 122 case jid:are_bare_equal(ReceiverJID, PacketJID) of
535 false ->
536 100 mod_mam_utils:delete_x_user_element(Packet);
537 true -> %% expose identity for user's own messages
538 22 Packet
539 end;
540 maybe_delete_x_user_element(false, _ReceiverJID, Packet) ->
541 1400 Packet.
542
543 %% From XEP-0313:
544 %% When sending out the archives to a requesting client, the 'to' of the
545 %% forwarded stanza MUST be empty, and the 'from' MUST be the occupant JID
546 %% of the sender of the archived message.
547 %% However, Smack crashes if 'to' is present, so it is removed.
548 replace_from_to_attributes(SrcJID, Packet = #xmlel{attrs = Attrs}) ->
549 1522 NewAttrs = jlib:replace_from_to_attrs(jid:to_binary(SrcJID), undefined, Attrs),
550 1522 Packet#xmlel{attrs = NewAttrs}.
551
552 -spec message_row_to_ext_id(row()) -> binary().
553 message_row_to_ext_id(#{id := MessID}) ->
554 552 mod_mam_utils:mess_id_to_external_binary(MessID).
555
556 -spec handle_error_iq(mongoose_acc:t(), host_type(), jid:jid(), atom(),
557 {error, term(), jlib:iq()} | jlib:iq() | ignore) -> {mongoose_acc:t(), jlib:iq() | ignore}.
558 handle_error_iq(Acc, HostType, _To, _Action, {error, _Reason, IQ}) ->
559 21 mongoose_metrics:update(HostType, modMucMamDroppedIQ, 1),
560 21 {Acc, IQ};
561 handle_error_iq(Acc, _HostType, _To, _Action, IQ) ->
562 346 {Acc, IQ}.
563
564 return_error_iq(IQ, {Reason, {stacktrace, _Stacktrace}}) ->
565
:-(
return_error_iq(IQ, Reason);
566 return_error_iq(IQ, timeout) ->
567
:-(
{error, timeout, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:service_unavailable(<<"en">>, <<"Timeout in mod_mam_muc">>)]}};
568 return_error_iq(IQ, invalid_stanza_id) ->
569 7 Text = mongoose_xmpp_errors:not_acceptable(<<"en">>, <<"Invalid stanza id provided">>),
570 7 {error, invalid_stanza_id, IQ#iq{type = error, sub_el = [Text]}};
571 return_error_iq(IQ, item_not_found) ->
572 14 {error, item_not_found, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:item_not_found()]}};
573 return_error_iq(IQ, not_implemented) ->
574
:-(
{error, not_implemented, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:feature_not_implemented(<<"en">>, <<"From mod_mam_muc">>)]}};
575 return_error_iq(IQ, missing_with_jid) ->
576
:-(
Error = mongoose_xmpp_errors:bad_request(<<"en">>,
577 <<"Limited set of queries allowed in the conversation mode.",
578 "Missing with_jid filter">>),
579
:-(
{error, bad_request, IQ#iq{type = error, sub_el = [Error]}};
580 return_error_iq(IQ, Reason) ->
581
:-(
{error, Reason, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:internal_server_error()]}}.
582
583 -spec return_action_not_allowed_error_iq(Reason :: binary(), jlib:iq()) -> jlib:iq().
584 return_action_not_allowed_error_iq(Reason, IQ) ->
585 14 ErrorEl = jlib:stanza_errort(<<"">>, <<"cancel">>, <<"not-allowed">>,
586 <<"en">>, <<"The action is not allowed. ", Reason/binary>>),
587 14 IQ#iq{type = error, sub_el = [ErrorEl]}.
588
589 -spec return_max_delay_reached_error_iq(jlib:iq()) -> jlib:iq().
590 return_max_delay_reached_error_iq(IQ) ->
591 %% Message not found.
592
:-(
ErrorEl = mongoose_xmpp_errors:resource_constraint(
593 <<"en">>, <<"The action is cancelled because of flooding.">>),
594
:-(
IQ#iq{type = error, sub_el = [ErrorEl]}.
595
596 return_message_form_iq(HostType, IQ) ->
597
:-(
Form = mod_mam_utils:message_form(?MODULE, HostType, IQ#iq.xmlns),
598
:-(
IQ#iq{type = result, sub_el = [Form]}.
599
600 % the stacktrace is a big lie
601 report_issue({Reason, {stacktrace, Stacktrace}}, Issue, ArcJID, IQ) ->
602
:-(
report_issue(Reason, Stacktrace, Issue, ArcJID, IQ);
603 report_issue(Reason, Issue, ArcJID, IQ) ->
604 21 report_issue(Reason, [], Issue, ArcJID, IQ).
605
606 report_issue(invalid_stanza_id, _Stacktrace, _Issue, _ArcJID, _IQ) ->
607
:-(
expected;
608 report_issue(item_not_found, _Stacktrace, _Issue, _ArcJID, _IQ) ->
609 14 expected;
610 report_issue(missing_with_jid, _Stacktrace, _Issue, _ArcJID, _IQ) ->
611
:-(
expected;
612 report_issue(not_implemented, _Stacktrace, _Issue, _ArcJID, _IQ) ->
613
:-(
expected;
614 report_issue(timeout, _Stacktrace, _Issue, _ArcJID, _IQ) ->
615
:-(
expected;
616 report_issue(Reason, Stacktrace, Issue, #jid{lserver = LServer, luser = LUser}, IQ) ->
617 7 ?LOG_ERROR(#{what => mam_muc_error, issue => Issue, reason => Reason,
618
:-(
user => LUser, server => LServer, iq => IQ, stacktrace => Stacktrace}).
619
620 -spec is_archivable_message(HostType :: host_type(),
621 Dir :: incoming | outgoing,
622 Packet :: exml:element()) -> boolean().
623 is_archivable_message(HostType, Dir, Packet) ->
624 569 M = mod_mam_params:is_archivable_message_module(?MODULE, HostType),
625 569 ArchiveChatMarkers = mod_mam_params:archive_chat_markers(?MODULE, HostType),
626 569 erlang:apply(M, is_archivable_message, [?MODULE, Dir, Packet, ArchiveChatMarkers]).
627
628 -spec hooks(mongooseim:host_type()) -> gen_hook:hook_list().
629 hooks(HostType) ->
630 134 [{disco_muc_features, HostType, fun ?MODULE:disco_muc_features/3, #{}, 99},
631 {filter_room_packet, HostType, fun ?MODULE:filter_room_packet/3, #{}, 60},
632 {forget_room, HostType, fun ?MODULE:forget_room/3, #{}, 90},
633 {get_personal_data, HostType, fun ?MODULE:get_personal_data/3, #{}, 50}
634 | mongoose_metrics_mam_hooks:get_mam_muc_hooks(HostType)].
635
636 add_iq_handlers(HostType, Opts) ->
637 67 IQDisc = gen_mod:get_opt(iqdisc, Opts, parallel),
638 67 MUCSubdomainPattern = gen_mod:get_module_opt(HostType, ?MODULE, host),
639
640 67 gen_iq_handler:add_iq_handler_for_subdomain(HostType, MUCSubdomainPattern,
641 ?NS_MAM_04, mod_muc_iq,
642 fun ?MODULE:room_process_mam_iq/5,
643 #{}, IQDisc),
644 67 gen_iq_handler:add_iq_handler_for_subdomain(HostType, MUCSubdomainPattern,
645 ?NS_MAM_06, mod_muc_iq,
646 fun ?MODULE:room_process_mam_iq/5,
647 #{}, IQDisc),
648 67 ok.
649
650 remove_iq_handlers(HostType) ->
651 67 MUCSubdomainPattern = gen_mod:get_module_opt(HostType, ?MODULE, host),
652 67 gen_iq_handler:remove_iq_handler_for_subdomain(HostType, MUCSubdomainPattern,
653 ?NS_MAM_04, mod_muc_iq),
654 67 gen_iq_handler:remove_iq_handler_for_subdomain(HostType, MUCSubdomainPattern,
655 ?NS_MAM_06, mod_muc_iq),
656 67 ok.
657
658 ensure_metrics(HostType) ->
659 67 mongoose_metrics:ensure_metric(HostType, [backends, ?MODULE, lookup], histogram),
660 67 lists:foreach(fun(Name) ->
661 603 mongoose_metrics:ensure_metric(HostType, Name, spiral)
662 end,
663 spirals()).
664
665 spirals() ->
666 67 [modMucMamPrefsSets,
667 modMucMamPrefsGets,
668 modMucMamArchiveRemoved,
669 modMucMamLookups,
670 modMucMamForwarded,
671 modMucMamArchived,
672 modMucMamFlushed,
673 modMucMamDropped,
674 modMucMamDroppedIQ].
Line Hits Source