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