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 simple 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_pm). |
31 |
|
-behavior(gen_mod). |
32 |
|
-behaviour(mongoose_module_metrics). |
33 |
|
-xep([{xep, 313}, {version, "0.4.1"}]). |
34 |
|
-xep([{xep, 313}, {version, "0.5"}]). |
35 |
|
-xep([{xep, 313}, {version, "0.6"}]). |
36 |
|
%% ---------------------------------------------------------------------- |
37 |
|
%% Exports |
38 |
|
|
39 |
|
%% Client API |
40 |
|
-export([delete_archive/2, |
41 |
|
archive_size/2, |
42 |
|
archive_size_with_host_type/3, |
43 |
|
archive_id/2]). |
44 |
|
|
45 |
|
%% gen_mod handlers |
46 |
|
-export([start/2, stop/1, supported_features/0]). |
47 |
|
|
48 |
|
%% ejabberd handlers |
49 |
|
-export([disco_local_features/1, |
50 |
|
process_mam_iq/5, |
51 |
|
user_send_packet/4, |
52 |
|
remove_user/3, |
53 |
|
filter_packet/1, |
54 |
|
determine_amp_strategy/5, |
55 |
|
sm_filter_offline_message/4]). |
56 |
|
|
57 |
|
%% gdpr callbacks |
58 |
|
-export([get_personal_data/3]). |
59 |
|
|
60 |
|
%%private |
61 |
|
-export([archive_message_from_ct/1]). |
62 |
|
-export([lookup_messages/2]). |
63 |
|
-export([archive_id_int/2]). |
64 |
|
|
65 |
|
-ignore_xref([archive_message_from_ct/1, |
66 |
|
archive_size/2, archive_size_with_host_type/3, delete_archive/2, |
67 |
|
determine_amp_strategy/5, disco_local_features/1, filter_packet/1, |
68 |
|
get_personal_data/3, remove_user/3, sm_filter_offline_message/4, |
69 |
|
user_send_packet/4]). |
70 |
|
|
71 |
|
-type host_type() :: mongooseim:host_type(). |
72 |
|
|
73 |
|
%% ---------------------------------------------------------------------- |
74 |
|
%% Imports |
75 |
|
|
76 |
|
%% UID |
77 |
|
-import(mod_mam_utils, |
78 |
|
[get_or_generate_mam_id/1, |
79 |
|
decode_compact_uuid/1]). |
80 |
|
|
81 |
|
%% XML |
82 |
|
-import(mod_mam_utils, |
83 |
|
[maybe_add_arcid_elems/4, |
84 |
|
wrap_message/6, |
85 |
|
result_set/4, |
86 |
|
result_prefs/4, |
87 |
|
make_fin_element/7, |
88 |
|
parse_prefs/1, |
89 |
|
is_mam_result_message/1, |
90 |
|
features/2]). |
91 |
|
|
92 |
|
%% Forms |
93 |
|
-import(mod_mam_utils, |
94 |
|
[message_form/3]). |
95 |
|
|
96 |
|
%% Other |
97 |
|
-import(mod_mam_utils, |
98 |
|
[mess_id_to_external_binary/1, |
99 |
|
is_complete_result_page/4]). |
100 |
|
|
101 |
|
%% ejabberd |
102 |
|
-import(mod_mam_utils, |
103 |
|
[is_jid_in_user_roster/3]). |
104 |
|
|
105 |
|
|
106 |
|
-include("mongoose.hrl"). |
107 |
|
-include("jlib.hrl"). |
108 |
|
-include("amp.hrl"). |
109 |
|
|
110 |
|
%% ---------------------------------------------------------------------- |
111 |
|
%% API |
112 |
|
|
113 |
|
-spec get_personal_data(gdpr:personal_data(), mongooseim:host_type(), jid:jid()) -> |
114 |
|
gdpr:personal_data(). |
115 |
|
get_personal_data(Acc, HostType, ArcJID) -> |
116 |
66 |
Schema = ["id", "from", "message"], |
117 |
66 |
Entries = mongoose_hooks:get_mam_pm_gdpr_data(HostType, ArcJID), |
118 |
66 |
[{mam_pm, Schema, Entries} | Acc]. |
119 |
|
|
120 |
|
-spec delete_archive(jid:server(), jid:user()) -> 'ok'. |
121 |
|
delete_archive(Server, User) |
122 |
|
when is_binary(Server), is_binary(User) -> |
123 |
186 |
?LOG_DEBUG(#{what => mam_delete_archive, user => User, server => Server}), |
124 |
186 |
ArcJID = jid:make_bare(User, Server), |
125 |
186 |
HostType = jid_to_host_type(ArcJID), |
126 |
186 |
ArcID = archive_id_int(HostType, ArcJID), |
127 |
186 |
remove_archive_hook(HostType, ArcID, ArcJID), |
128 |
186 |
ok. |
129 |
|
|
130 |
|
-spec archive_size(jid:server(), jid:user()) -> integer(). |
131 |
|
archive_size(Server, User) |
132 |
|
when is_binary(Server), is_binary(User) -> |
133 |
328 |
ArcJID = jid:make_bare(User, Server), |
134 |
328 |
HostType = jid_to_host_type(ArcJID), |
135 |
328 |
ArcID = archive_id_int(HostType, ArcJID), |
136 |
328 |
archive_size(HostType, ArcID, ArcJID). |
137 |
|
|
138 |
|
-spec archive_size_with_host_type(host_type(), jid:server(), jid:user()) -> integer(). |
139 |
|
archive_size_with_host_type(HostType, Server, User) -> |
140 |
:-( |
ArcJID = jid:make_bare(User, Server), |
141 |
:-( |
ArcID = archive_id_int(HostType, ArcJID), |
142 |
:-( |
archive_size(HostType, ArcID, ArcJID). |
143 |
|
|
144 |
|
-spec archive_id(jid:server(), jid:user()) -> integer() | undefined. |
145 |
|
archive_id(Server, User) |
146 |
|
when is_binary(Server), is_binary(User) -> |
147 |
45 |
ArcJID = jid:make_bare(User, Server), |
148 |
45 |
HostType = jid_to_host_type(ArcJID), |
149 |
45 |
archive_id_int(HostType, ArcJID). |
150 |
|
|
151 |
|
%% gen_mod callbacks |
152 |
|
%% Starting and stopping functions for users' archives |
153 |
|
|
154 |
|
-spec start(host_type(), gen_mod:module_opts()) -> any(). |
155 |
|
start(HostType, Opts) -> |
156 |
43 |
?LOG_INFO(#{what => mam_starting, host_type => HostType}), |
157 |
43 |
ensure_metrics(HostType), |
158 |
43 |
ejabberd_hooks:add(hooks(HostType)), |
159 |
43 |
add_iq_handlers(HostType, Opts), |
160 |
43 |
ok. |
161 |
|
|
162 |
|
-spec stop(host_type()) -> any(). |
163 |
|
stop(HostType) -> |
164 |
43 |
?LOG_INFO(#{what => mam_stopping, host_type => HostType}), |
165 |
43 |
ejabberd_hooks:delete(hooks(HostType)), |
166 |
43 |
remove_iq_handlers(HostType), |
167 |
43 |
ok. |
168 |
|
|
169 |
|
-spec supported_features() -> [atom()]. |
170 |
|
supported_features() -> |
171 |
:-( |
[dynamic_domains]. |
172 |
|
|
173 |
|
%% ---------------------------------------------------------------------- |
174 |
|
%% hooks and handlers |
175 |
|
|
176 |
|
%% `To' is an account or server entity hosting the archive. |
177 |
|
%% Servers that archive messages on behalf of local users SHOULD expose archives |
178 |
|
%% to the user on their bare JID (i.e. `From.luser'), |
179 |
|
%% while a MUC service might allow MAM queries to be sent to the room's bare JID |
180 |
|
%% (i.e `To.luser'). |
181 |
|
-spec process_mam_iq(Acc :: mongoose_acc:t(), |
182 |
|
From :: jid:jid(), To :: jid:jid(), IQ :: jlib:iq(), |
183 |
|
_Extra) -> {mongoose_acc:t(), jlib:iq() | ignore}. |
184 |
|
process_mam_iq(Acc, From, To, IQ, _Extra) -> |
185 |
315 |
HostType = mongoose_acc:host_type(Acc), |
186 |
315 |
mod_mam_utils:maybe_log_deprecation(IQ), |
187 |
315 |
Action = mam_iq:action(IQ), |
188 |
315 |
case is_action_allowed(HostType, Action, From, To) of |
189 |
|
true -> |
190 |
315 |
case mod_mam_utils:wait_shaper(HostType, To#jid.lserver, Action, From) of |
191 |
|
ok -> |
192 |
315 |
handle_error_iq(HostType, Acc, To, Action, |
193 |
|
handle_mam_iq(Action, From, To, IQ, Acc)); |
194 |
|
{error, max_delay_reached} -> |
195 |
:-( |
?LOG_WARNING(#{what => mam_max_delay_reached, |
196 |
|
text => <<"Return max_delay_reached error IQ from MAM">>, |
197 |
:-( |
action => Action, acc => Acc}), |
198 |
:-( |
mongoose_metrics:update(HostType, modMamDroppedIQ, 1), |
199 |
:-( |
{Acc, return_max_delay_reached_error_iq(IQ)} |
200 |
|
end; |
201 |
|
false -> |
202 |
:-( |
mongoose_metrics:update(HostType, modMamDroppedIQ, 1), |
203 |
:-( |
{Acc, return_action_not_allowed_error_iq(IQ)} |
204 |
|
end. |
205 |
|
|
206 |
|
-spec disco_local_features(mongoose_disco:feature_acc()) -> mongoose_disco:feature_acc(). |
207 |
|
disco_local_features(Acc = #{host_type := HostType, node := <<>>}) -> |
208 |
26 |
mongoose_disco:add_features(features(?MODULE, HostType), Acc); |
209 |
|
disco_local_features(Acc) -> |
210 |
:-( |
Acc. |
211 |
|
|
212 |
|
%% @doc Handle an outgoing message. |
213 |
|
%% |
214 |
|
%% Note: for outgoing messages, the server MUST use the value of the 'to' |
215 |
|
%% attribute as the target JID. |
216 |
|
-spec user_send_packet(Acc :: mongoose_acc:t(), From :: jid:jid(), |
217 |
|
To :: jid:jid(), |
218 |
|
Packet :: exml:element()) -> mongoose_acc:t(). |
219 |
|
user_send_packet(Acc, From, To, Packet) -> |
220 |
1057 |
?LOG_DEBUG(#{what => mam_user_send_packet, acc => Acc}), |
221 |
1057 |
{_, Acc2} = handle_package(outgoing, true, From, To, From, Packet, Acc), |
222 |
1057 |
Acc2. |
223 |
|
|
224 |
|
%% @doc Handle an incoming message. |
225 |
|
%% |
226 |
|
%% Note: For incoming messages, the server MUST use the value of the |
227 |
|
%% 'from' attribute as the target JID. |
228 |
|
%% |
229 |
|
%% Return drop to drop the packet, or the original input to let it through. |
230 |
|
%% From and To are jid records. |
231 |
|
-type fpacket() :: {From :: jid:jid(), |
232 |
|
To :: jid:jid(), |
233 |
|
Acc :: mongoose_acc:t(), |
234 |
|
Packet :: exml:element()}. |
235 |
|
-spec filter_packet(Value :: fpacket() | drop) -> fpacket() | drop. |
236 |
|
filter_packet(drop) -> |
237 |
:-( |
drop; |
238 |
|
filter_packet({From, To, Acc1, Packet}) -> |
239 |
2337 |
?LOG_DEBUG(#{what => mam_user_receive_packet, acc => Acc1}), |
240 |
2337 |
HostType = mongoose_acc:host_type(Acc1), |
241 |
2337 |
Type = mongoose_lib:get_message_type(Acc1), |
242 |
2337 |
{AmpEvent, PacketAfterArchive, Acc3} = |
243 |
|
case mongoose_lib:does_local_user_exist(HostType, To, Type) of |
244 |
|
false -> |
245 |
147 |
{mam_failed, Packet, Acc1}; |
246 |
|
true -> |
247 |
2190 |
case process_incoming_packet(From, To, Packet, Acc1) of |
248 |
|
{undefined, Acc2} -> |
249 |
1875 |
{mam_failed, Packet, Acc2}; |
250 |
|
{MessID, Acc2} -> |
251 |
315 |
Packet2 = maybe_add_arcid_elems( |
252 |
|
To, MessID, Packet, |
253 |
|
mod_mam_params:add_stanzaid_element(?MODULE, HostType)), |
254 |
315 |
{archived, Packet2, Acc2} |
255 |
|
end |
256 |
|
end, |
257 |
2337 |
Acc4 = mongoose_acc:update_stanza(#{ element => PacketAfterArchive, |
258 |
|
from_jid => From, |
259 |
|
to_jid => To }, Acc3), |
260 |
2337 |
Acc5 = mod_amp:check_packet(Acc4, AmpEvent), |
261 |
2337 |
{From, To, Acc5, mongoose_acc:element(Acc5)}. |
262 |
|
|
263 |
|
process_incoming_packet(From, To, Packet, Acc) -> |
264 |
2190 |
handle_package(incoming, true, To, From, From, Packet, Acc). |
265 |
|
|
266 |
|
%% hook handler |
267 |
|
-spec remove_user(mongoose_acc:t(), jid:user(), jid:server()) -> mongoose_acc:t(). |
268 |
|
remove_user(Acc, User, Server) -> |
269 |
114 |
delete_archive(Server, User), |
270 |
114 |
Acc. |
271 |
|
|
272 |
|
sm_filter_offline_message(_Drop=false, _From, _To, Packet) -> |
273 |
|
%% If ... |
274 |
3 |
is_mam_result_message(Packet); |
275 |
|
%% ... than drop the message |
276 |
|
sm_filter_offline_message(Other, _From, _To, _Packet) -> |
277 |
:-( |
Other. |
278 |
|
|
279 |
|
%% ---------------------------------------------------------------------- |
280 |
|
%% Internal functions |
281 |
|
|
282 |
|
-spec jid_to_host_type(jid:jid()) -> host_type(). |
283 |
|
jid_to_host_type(#jid{lserver=LServer}) -> |
284 |
1375 |
lserver_to_host_type(LServer). |
285 |
|
|
286 |
|
lserver_to_host_type(LServer) -> |
287 |
1375 |
case mongoose_domain_api:get_domain_host_type(LServer) of |
288 |
|
{ok, HostType} -> |
289 |
1375 |
HostType; |
290 |
|
{error, not_found} -> |
291 |
:-( |
error({get_domain_host_type_failed, LServer}) |
292 |
|
end. |
293 |
|
|
294 |
|
-spec acc_to_host_type(mongoose_acc:t()) -> host_type(). |
295 |
|
acc_to_host_type(Acc) -> |
296 |
3562 |
case mongoose_acc:host_type(Acc) of |
297 |
|
undefined -> |
298 |
:-( |
lserver_to_host_type(mongoose_acc:lserver(Acc)); |
299 |
|
HostType -> |
300 |
3562 |
HostType |
301 |
|
end. |
302 |
|
|
303 |
|
-spec is_action_allowed(HostType :: host_type(), |
304 |
|
Action :: mam_iq:action(), From :: jid:jid(), |
305 |
|
To :: jid:jid()) -> boolean(). |
306 |
|
is_action_allowed(HostType, Action, From, To) -> |
307 |
315 |
case acl:match_rule(HostType, To#jid.lserver, Action, From, default) of |
308 |
:-( |
allow -> true; |
309 |
:-( |
deny -> false; |
310 |
315 |
default -> is_action_allowed_by_default(Action, From, To) |
311 |
|
end. |
312 |
|
|
313 |
|
-spec is_action_allowed_by_default(Action :: mam_iq:action(), From :: jid:jid(), |
314 |
|
To :: jid:jid()) -> boolean(). |
315 |
|
is_action_allowed_by_default(_Action, From, To) -> |
316 |
315 |
compare_bare_jids(From, To). |
317 |
|
|
318 |
|
-spec compare_bare_jids(jid:simple_jid() | jid:jid(), |
319 |
|
jid:simple_jid() | jid:jid()) -> boolean(). |
320 |
|
compare_bare_jids(JID1, JID2) -> |
321 |
315 |
jid:to_bare(JID1) =:= jid:to_bare(JID2). |
322 |
|
|
323 |
|
-spec handle_mam_iq(mam_iq:action(), From :: jid:jid(), To :: jid:jid(), |
324 |
|
IQ :: jlib:iq(), Acc :: mongoose_acc:t()) -> |
325 |
|
jlib:iq() | {error, term(), jlib:iq()}. |
326 |
|
handle_mam_iq(Action, From, To, IQ, Acc) -> |
327 |
315 |
case Action of |
328 |
|
mam_get_prefs -> |
329 |
46 |
handle_get_prefs(To, IQ, Acc); |
330 |
|
mam_set_prefs -> |
331 |
88 |
handle_set_prefs(To, IQ, Acc); |
332 |
|
mam_set_message_form -> |
333 |
173 |
handle_set_message_form(From, To, IQ, Acc); |
334 |
|
mam_get_message_form -> |
335 |
8 |
handle_get_message_form(From, To, IQ, Acc) |
336 |
|
end. |
337 |
|
|
338 |
|
-spec handle_set_prefs(jid:jid(), jlib:iq(), mongoose_acc:t()) -> |
339 |
|
jlib:iq() | {error, term(), jlib:iq()}. |
340 |
|
handle_set_prefs(ArcJID=#jid{}, IQ=#iq{sub_el = PrefsEl}, Acc) -> |
341 |
88 |
{DefaultMode, AlwaysJIDs, NeverJIDs} = parse_prefs(PrefsEl), |
342 |
88 |
?LOG_DEBUG(#{what => mam_set_prefs, default_mode => DefaultMode, |
343 |
88 |
always_jids => AlwaysJIDs, never_jids => NeverJIDs, iq => IQ}), |
344 |
88 |
HostType = acc_to_host_type(Acc), |
345 |
88 |
ArcID = archive_id_int(HostType, ArcJID), |
346 |
88 |
Res = set_prefs(HostType, ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs), |
347 |
88 |
handle_set_prefs_result(Res, DefaultMode, AlwaysJIDs, NeverJIDs, IQ). |
348 |
|
|
349 |
|
handle_set_prefs_result(ok, DefaultMode, AlwaysJIDs, NeverJIDs, IQ) -> |
350 |
88 |
Namespace = IQ#iq.xmlns, |
351 |
88 |
ResultPrefsEl = result_prefs(DefaultMode, AlwaysJIDs, NeverJIDs, Namespace), |
352 |
88 |
IQ#iq{type = result, sub_el = [ResultPrefsEl]}; |
353 |
|
handle_set_prefs_result({error, Reason}, |
354 |
|
_DefaultMode, _AlwaysJIDs, _NeverJIDs, IQ) -> |
355 |
:-( |
return_error_iq(IQ, Reason). |
356 |
|
|
357 |
|
-spec handle_get_prefs(jid:jid(), IQ :: jlib:iq(), Acc :: mongoose_acc:t()) -> |
358 |
|
jlib:iq() | {error, term(), jlib:iq()}. |
359 |
|
handle_get_prefs(ArcJID=#jid{}, IQ=#iq{}, Acc) -> |
360 |
46 |
HostType = acc_to_host_type(Acc), |
361 |
46 |
ArcID = archive_id_int(HostType, ArcJID), |
362 |
46 |
Res = get_prefs(HostType, ArcID, ArcJID, always), |
363 |
46 |
handle_get_prefs_result(Res, IQ). |
364 |
|
|
365 |
|
handle_get_prefs_result({DefaultMode, AlwaysJIDs, NeverJIDs}, IQ) -> |
366 |
46 |
?LOG_DEBUG(#{what => mam_get_prefs_result, default_mode => DefaultMode, |
367 |
46 |
always_jids => AlwaysJIDs, never_jids => NeverJIDs, iq => IQ}), |
368 |
46 |
Namespace = IQ#iq.xmlns, |
369 |
46 |
ResultPrefsEl = result_prefs(DefaultMode, AlwaysJIDs, NeverJIDs, Namespace), |
370 |
46 |
IQ#iq{type = result, sub_el = [ResultPrefsEl]}; |
371 |
|
handle_get_prefs_result({error, Reason}, IQ) -> |
372 |
:-( |
return_error_iq(IQ, Reason). |
373 |
|
|
374 |
|
-spec handle_set_message_form(From :: jid:jid(), ArcJID :: jid:jid(), |
375 |
|
IQ :: jlib:iq(), Acc :: mongoose_acc:t()) -> |
376 |
|
jlib:iq() | ignore | {error, term(), jlib:iq()}. |
377 |
|
handle_set_message_form(#jid{} = From, #jid{} = ArcJID, #iq{} = IQ, Acc) -> |
378 |
173 |
HostType = acc_to_host_type(Acc), |
379 |
173 |
ArcID = archive_id_int(HostType, ArcJID), |
380 |
173 |
try iq_to_lookup_params(HostType, IQ) of |
381 |
|
Params0 -> |
382 |
169 |
do_handle_set_message_form(Params0, From, ArcID, ArcJID, IQ, HostType) |
383 |
|
catch _C:R:S -> |
384 |
4 |
report_issue({R, S}, mam_lookup_failed, ArcJID, IQ), |
385 |
4 |
return_error_iq(IQ, R) |
386 |
|
end. |
387 |
|
|
388 |
|
|
389 |
|
-spec do_handle_set_message_form(Params :: mam_iq:lookup_params(), |
390 |
|
From :: jid:jid(), |
391 |
|
ArcId :: mod_mam:archive_id(), |
392 |
|
ArcJID :: jid:jid(), |
393 |
|
IQ :: jlib:iq(), |
394 |
|
HostType :: mongooseim:host_type()) -> |
395 |
|
jlib:iq() | ignore | {error, term(), jlib:iq()}. |
396 |
|
do_handle_set_message_form(Params0, From, ArcID, ArcJID, |
397 |
|
#iq{xmlns=MamNs, sub_el = QueryEl} = IQ, |
398 |
|
HostType) -> |
399 |
169 |
QueryID = exml_query:attr(QueryEl, <<"queryid">>, <<>>), |
400 |
169 |
Params = mam_iq:lookup_params_with_archive_details(Params0, ArcID, ArcJID, From), |
401 |
169 |
case lookup_messages(HostType, Params) of |
402 |
|
{error, Reason} -> |
403 |
10 |
report_issue(Reason, mam_lookup_failed, ArcJID, IQ), |
404 |
10 |
return_error_iq(IQ, Reason); |
405 |
|
{ok, {TotalCount, Offset, MessageRows}} -> |
406 |
|
%% Forward messages |
407 |
159 |
{FirstMessID, LastMessID} = forward_messages(HostType, From, ArcJID, MamNs, |
408 |
|
QueryID, MessageRows, true), |
409 |
|
%% Make fin iq |
410 |
159 |
IsComplete = is_complete_result_page(TotalCount, Offset, MessageRows, Params), |
411 |
159 |
IsStable = true, |
412 |
159 |
ResultSetEl = result_set(FirstMessID, LastMessID, Offset, TotalCount), |
413 |
159 |
ExtFinMod = mod_mam_params:extra_fin_element_module(?MODULE, HostType), |
414 |
159 |
FinElem = make_fin_element(HostType, Params, IQ#iq.xmlns, IsComplete, IsStable, ResultSetEl, ExtFinMod), |
415 |
159 |
IQ#iq{type = result, sub_el = [FinElem]} |
416 |
|
end. |
417 |
|
|
418 |
|
iq_to_lookup_params(HostType, IQ) -> |
419 |
173 |
Max = mod_mam_params:max_result_limit(?MODULE, HostType), |
420 |
173 |
Def = mod_mam_params:default_result_limit(?MODULE, HostType), |
421 |
173 |
Ext = mod_mam_params:extra_params_module(?MODULE, HostType), |
422 |
173 |
mam_iq:form_to_lookup_params(IQ, Max, Def, Ext). |
423 |
|
|
424 |
|
forward_messages(HostType, From, ArcJID, MamNs, QueryID, MessageRows, SetClientNs) -> |
425 |
|
%% Forward messages |
426 |
159 |
{FirstMessID, LastMessID} = |
427 |
|
case MessageRows of |
428 |
30 |
[] -> {undefined, undefined}; |
429 |
129 |
[_|_] -> {message_row_to_ext_id(hd(MessageRows)), |
430 |
|
message_row_to_ext_id(lists:last(MessageRows))} |
431 |
|
end, |
432 |
159 |
SendModule = mod_mam_params:send_message_mod(?MODULE, HostType), |
433 |
159 |
[send_message(SendModule, Row, ArcJID, From, |
434 |
|
message_row_to_xml(MamNs, Row, QueryID, SetClientNs)) |
435 |
159 |
|| Row <- MessageRows], |
436 |
159 |
{FirstMessID, LastMessID}. |
437 |
|
|
438 |
|
send_message(SendModule, Row, ArcJID, From, Packet) -> |
439 |
591 |
mam_send_message:call_send_message(SendModule, Row, ArcJID, From, Packet). |
440 |
|
|
441 |
|
-spec handle_get_message_form(jid:jid(), jid:jid(), jlib:iq(), mongoose_acc:t()) -> |
442 |
|
jlib:iq(). |
443 |
|
handle_get_message_form(_From=#jid{}, _ArcJID=#jid{}, IQ=#iq{}, Acc) -> |
444 |
8 |
HostType = acc_to_host_type(Acc), |
445 |
8 |
return_message_form_iq(HostType, IQ). |
446 |
|
|
447 |
|
determine_amp_strategy(Strategy = #amp_strategy{deliver = Deliver}, |
448 |
|
FromJID, ToJID, Packet, initial_check) -> |
449 |
:-( |
HostType = jid_to_host_type(ToJID), |
450 |
:-( |
ShouldBeStored = is_archivable_message(HostType, incoming, Packet) |
451 |
:-( |
andalso is_interesting(ToJID, FromJID) |
452 |
:-( |
andalso ejabberd_auth:does_user_exist(ToJID), |
453 |
:-( |
case ShouldBeStored of |
454 |
:-( |
true -> Strategy#amp_strategy{deliver = amp_deliver_strategy(Deliver)}; |
455 |
:-( |
false -> Strategy |
456 |
|
end; |
457 |
|
determine_amp_strategy(Strategy, _, _, _, _) -> |
458 |
:-( |
Strategy. |
459 |
|
|
460 |
:-( |
amp_deliver_strategy([none]) -> [stored, none]; |
461 |
:-( |
amp_deliver_strategy([direct, none]) -> [direct, stored, none]. |
462 |
|
|
463 |
|
-spec handle_package(Dir :: incoming | outgoing, ReturnMessID :: boolean(), |
464 |
|
LocJID :: jid:jid(), RemJID :: jid:jid(), SrcJID :: jid:jid(), |
465 |
|
Packet :: exml:element(), Acc :: mongoose_acc:t()) -> |
466 |
|
{MaybeMessID :: binary() | undefined, Acc :: mongoose_acc:t()}. |
467 |
|
handle_package(Dir, ReturnMessID, |
468 |
|
LocJID = #jid{}, RemJID = #jid{}, SrcJID = #jid{}, Packet, Acc) -> |
469 |
3247 |
HostType = acc_to_host_type(Acc), |
470 |
3247 |
case is_archivable_message(HostType, Dir, Packet) |
471 |
760 |
andalso should_archive_if_groupchat(HostType, exml_query:attr(Packet, <<"type">>)) of |
472 |
|
true -> |
473 |
664 |
ArcID = archive_id_int(HostType, LocJID), |
474 |
664 |
OriginID = mod_mam_utils:get_origin_id(Packet), |
475 |
664 |
case is_interesting(HostType, LocJID, RemJID, ArcID) of |
476 |
|
true -> |
477 |
580 |
MessID = get_or_generate_mam_id(Acc), |
478 |
580 |
Params = #{message_id => MessID, |
479 |
|
archive_id => ArcID, |
480 |
|
local_jid => LocJID, |
481 |
|
remote_jid => RemJID, |
482 |
|
source_jid => SrcJID, |
483 |
|
origin_id => OriginID, |
484 |
|
direction => Dir, |
485 |
|
packet => Packet}, |
486 |
580 |
Result = archive_message(HostType, Params), |
487 |
580 |
ExtMessId = return_external_message_id_if_ok(ReturnMessID, Result, MessID), |
488 |
580 |
{ExtMessId, return_acc_with_mam_id_if_configured(ExtMessId, HostType, Acc)}; |
489 |
|
false -> |
490 |
84 |
{undefined, Acc} |
491 |
|
end; |
492 |
|
false -> |
493 |
2583 |
{undefined, Acc} |
494 |
|
end. |
495 |
|
|
496 |
|
should_archive_if_groupchat(HostType, <<"groupchat">>) -> |
497 |
146 |
gen_mod:get_module_opt(HostType, ?MODULE, archive_groupchats); |
498 |
|
should_archive_if_groupchat(_, _) -> |
499 |
614 |
true. |
500 |
|
|
501 |
|
-spec return_external_message_id_if_ok(ReturnMessID :: boolean(), |
502 |
|
ArchivingResult :: ok | any(), |
503 |
|
MessID :: integer()) -> binary() | undefined. |
504 |
580 |
return_external_message_id_if_ok(true, ok, MessID) -> mess_id_to_external_binary(MessID); |
505 |
:-( |
return_external_message_id_if_ok(_, _, _MessID) -> undefined. |
506 |
|
|
507 |
|
return_acc_with_mam_id_if_configured(undefined, _, Acc) -> |
508 |
:-( |
Acc; |
509 |
|
return_acc_with_mam_id_if_configured(ExtMessId, HostType, Acc) -> |
510 |
580 |
case gen_mod:get_module_opt(HostType, ?MODULE, same_mam_id_for_peers) of |
511 |
576 |
false -> mongoose_acc:set(mam, mam_id, ExtMessId, Acc); |
512 |
4 |
true -> mongoose_acc:set_permanent(mam, mam_id, ExtMessId, Acc) |
513 |
|
end. |
514 |
|
|
515 |
|
is_interesting(LocJID, RemJID) -> |
516 |
:-( |
HostType = jid_to_host_type(LocJID), |
517 |
:-( |
ArcID = archive_id_int(HostType, LocJID), |
518 |
:-( |
is_interesting(HostType, LocJID, RemJID, ArcID). |
519 |
|
|
520 |
|
is_interesting(HostType, LocJID, RemJID, ArcID) -> |
521 |
664 |
case get_behaviour(HostType, ArcID, LocJID, RemJID) of |
522 |
568 |
always -> true; |
523 |
72 |
never -> false; |
524 |
24 |
roster -> is_jid_in_user_roster(HostType, LocJID, RemJID) |
525 |
|
end. |
526 |
|
|
527 |
|
%% ---------------------------------------------------------------------- |
528 |
|
%% Backend wrappers |
529 |
|
|
530 |
|
-spec archive_id_int(host_type(), jid:jid()) -> |
531 |
|
non_neg_integer() | undefined. |
532 |
|
archive_id_int(HostType, ArcJID=#jid{}) -> |
533 |
1530 |
mongoose_hooks:mam_archive_id(HostType, ArcJID). |
534 |
|
|
535 |
|
-spec archive_size(host_type(), mod_mam:archive_id(), jid:jid()) -> integer(). |
536 |
|
archive_size(HostType, ArcID, ArcJID=#jid{}) -> |
537 |
328 |
mongoose_hooks:mam_archive_size(HostType, ArcID, ArcJID). |
538 |
|
|
539 |
|
-spec get_behaviour(host_type(), mod_mam:archive_id(), LocJID :: jid:jid(), |
540 |
|
RemJID :: jid:jid()) -> atom(). |
541 |
|
get_behaviour(HostType, ArcID, LocJID=#jid{}, RemJID=#jid{}) -> |
542 |
664 |
mongoose_hooks:mam_get_behaviour(HostType, ArcID, LocJID, RemJID). |
543 |
|
|
544 |
|
-spec set_prefs(host_type(), mod_mam:archive_id(), ArcJID :: jid:jid(), |
545 |
|
DefaultMode :: atom(), AlwaysJIDs :: [jid:literal_jid()], |
546 |
|
NeverJIDs :: [jid:literal_jid()]) -> any(). |
547 |
|
set_prefs(HostType, ArcID, ArcJID, DefaultMode, AlwaysJIDs, NeverJIDs) -> |
548 |
88 |
mongoose_hooks:mam_set_prefs(HostType, ArcID, ArcJID, DefaultMode, |
549 |
|
AlwaysJIDs, NeverJIDs). |
550 |
|
|
551 |
|
%% @doc Load settings from the database. |
552 |
|
-spec get_prefs(HostType :: host_type(), ArcID :: mod_mam:archive_id(), |
553 |
|
ArcJID :: jid:jid(), GlobalDefaultMode :: mod_mam:archive_behaviour() |
554 |
|
) -> mod_mam:preference() | {error, Reason :: term()}. |
555 |
|
get_prefs(HostType, ArcID, ArcJID, GlobalDefaultMode) -> |
556 |
46 |
mongoose_hooks:mam_get_prefs(HostType, GlobalDefaultMode, ArcID, ArcJID). |
557 |
|
|
558 |
|
-spec remove_archive_hook(host_type(), mod_mam:archive_id(), jid:jid()) -> 'ok'. |
559 |
|
remove_archive_hook(HostType, ArcID, ArcJID=#jid{}) -> |
560 |
186 |
mongoose_hooks:mam_remove_archive(HostType, ArcID, ArcJID), |
561 |
186 |
ok. |
562 |
|
|
563 |
|
-spec lookup_messages(HostType :: host_type(), Params :: map()) -> |
564 |
|
{ok, mod_mam:lookup_result()} |
565 |
|
| {error, 'policy-violation'} |
566 |
|
| {error, Reason :: term()}. |
567 |
|
lookup_messages(HostType, Params) -> |
568 |
178 |
Result = lookup_messages_without_policy_violation_check(HostType, Params), |
569 |
|
%% If a query returns a number of stanzas greater than this limit and the |
570 |
|
%% client did not specify a limit using RSM then the server should return |
571 |
|
%% a policy-violation error to the client. |
572 |
178 |
mod_mam_utils:check_result_for_policy_violation(Params, Result). |
573 |
|
|
574 |
|
lookup_messages_without_policy_violation_check( |
575 |
|
HostType, #{search_text := SearchText} = Params) -> |
576 |
178 |
case SearchText /= undefined andalso |
577 |
7 |
not mod_mam_params:has_full_text_search(?MODULE, HostType) of |
578 |
|
true -> %% Use of disabled full text search |
579 |
2 |
{error, 'not-supported'}; |
580 |
|
false -> |
581 |
176 |
StartT = erlang:monotonic_time(microsecond), |
582 |
176 |
R = mongoose_hooks:mam_lookup_messages(HostType, Params), |
583 |
176 |
Diff = erlang:monotonic_time(microsecond) - StartT, |
584 |
176 |
mongoose_metrics:update(HostType, [backends, ?MODULE, lookup], Diff), |
585 |
176 |
R |
586 |
|
end. |
587 |
|
|
588 |
|
archive_message_from_ct(Params = #{local_jid := JID}) -> |
589 |
816 |
HostType = jid_to_host_type(JID), |
590 |
816 |
archive_message(HostType, Params). |
591 |
|
|
592 |
|
-spec archive_message(host_type(), mod_mam:archive_message_params()) -> |
593 |
|
ok | {error, timeout}. |
594 |
|
archive_message(HostType, Params) -> |
595 |
1396 |
StartT = erlang:monotonic_time(microsecond), |
596 |
1396 |
R = mongoose_hooks:mam_archive_message(HostType, Params), |
597 |
1396 |
Diff = erlang:monotonic_time(microsecond) - StartT, |
598 |
1396 |
mongoose_metrics:update(HostType, [backends, ?MODULE, archive], Diff), |
599 |
1396 |
R. |
600 |
|
|
601 |
|
%% ---------------------------------------------------------------------- |
602 |
|
%% Helpers |
603 |
|
|
604 |
|
-spec message_row_to_xml(binary(), mod_mam:message_row(), QueryId :: binary(), boolean()) -> |
605 |
|
exml:element(). |
606 |
|
message_row_to_xml(MamNs, #{id := MessID, jid := SrcJID, packet := Packet}, |
607 |
|
QueryID, SetClientNs) -> |
608 |
591 |
{Microseconds, _NodeMessID} = decode_compact_uuid(MessID), |
609 |
591 |
TS = calendar:system_time_to_rfc3339(Microseconds, [{offset, "Z"}, {unit, microsecond}]), |
610 |
591 |
BExtMessID = mess_id_to_external_binary(MessID), |
611 |
591 |
Packet1 = mod_mam_utils:maybe_set_client_xmlns(SetClientNs, Packet), |
612 |
591 |
wrap_message(MamNs, Packet1, QueryID, BExtMessID, TS, SrcJID). |
613 |
|
|
614 |
|
-spec message_row_to_ext_id(mod_mam:message_row()) -> binary(). |
615 |
|
message_row_to_ext_id(#{id := MessID}) -> |
616 |
258 |
mess_id_to_external_binary(MessID). |
617 |
|
|
618 |
|
handle_error_iq(HostType, Acc, _To, _Action, {error, _Reason, IQ}) -> |
619 |
14 |
mongoose_metrics:update(HostType, modMamDroppedIQ, 1), |
620 |
14 |
{Acc, IQ}; |
621 |
|
handle_error_iq(_Host, Acc, _To, _Action, IQ) -> |
622 |
301 |
{Acc, IQ}. |
623 |
|
|
624 |
|
-spec return_action_not_allowed_error_iq(jlib:iq()) -> jlib:iq(). |
625 |
|
return_action_not_allowed_error_iq(IQ) -> |
626 |
:-( |
ErrorEl = jlib:stanza_errort(<<"">>, <<"cancel">>, <<"not-allowed">>, |
627 |
|
<<"en">>, <<"The action is not allowed.">>), |
628 |
:-( |
IQ#iq{type = error, sub_el = [ErrorEl]}. |
629 |
|
|
630 |
|
-spec return_max_delay_reached_error_iq(jlib:iq()) -> jlib:iq(). |
631 |
|
return_max_delay_reached_error_iq(IQ) -> |
632 |
|
%% Message not found. |
633 |
:-( |
ErrorEl = mongoose_xmpp_errors:resource_constraint( |
634 |
|
<<"en">>, <<"The action is cancelled because of flooding.">>), |
635 |
:-( |
IQ#iq{type = error, sub_el = [ErrorEl]}. |
636 |
|
|
637 |
|
-spec return_error_iq(jlib:iq(), Reason :: term()) -> {error, term(), jlib:iq()}. |
638 |
|
return_error_iq(IQ, {Reason, {stacktrace, _Stacktrace}}) -> |
639 |
:-( |
return_error_iq(IQ, Reason); |
640 |
|
return_error_iq(IQ, timeout) -> |
641 |
:-( |
E = mongoose_xmpp_errors:service_unavailable(<<"en">>, <<"Timeout">>), |
642 |
:-( |
{error, timeout, IQ#iq{type = error, sub_el = [E]}}; |
643 |
|
return_error_iq(IQ, invalid_stanza_id) -> |
644 |
4 |
Text = mongoose_xmpp_errors:not_acceptable(<<"en">>, <<"Invalid stanza id provided">>), |
645 |
4 |
{error, invalid_stanza_id, IQ#iq{type = error, sub_el = [Text]}}; |
646 |
|
return_error_iq(IQ, item_not_found) -> |
647 |
8 |
{error, item_not_found, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:item_not_found()]}}; |
648 |
|
return_error_iq(IQ, not_implemented) -> |
649 |
:-( |
{error, not_implemented, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:feature_not_implemented()]}}; |
650 |
|
return_error_iq(IQ, Reason) -> |
651 |
2 |
{error, Reason, IQ#iq{type = error, sub_el = [mongoose_xmpp_errors:internal_server_error()]}}. |
652 |
|
|
653 |
|
return_message_form_iq(HostType, IQ) -> |
654 |
8 |
IQ#iq{type = result, sub_el = [message_form(?MODULE, HostType, IQ#iq.xmlns)]}. |
655 |
|
|
656 |
|
report_issue({Reason, {stacktrace, Stacktrace}}, Issue, ArcJID, IQ) -> |
657 |
:-( |
report_issue(Reason, Stacktrace, Issue, ArcJID, IQ); |
658 |
|
report_issue(Reason, Issue, ArcJID, IQ) -> |
659 |
14 |
report_issue(Reason, [], Issue, ArcJID, IQ). |
660 |
|
|
661 |
|
report_issue(invalid_stanza_id, _Stacktrace, _Issue, _ArcJID, _IQ) -> |
662 |
:-( |
expected; |
663 |
|
report_issue(item_not_found, _Stacktrace, _Issue, _ArcJID, _IQ) -> |
664 |
8 |
expected; |
665 |
|
report_issue(not_implemented, _Stacktrace, _Issue, _ArcJID, _IQ) -> |
666 |
:-( |
expected; |
667 |
|
report_issue(timeout, _Stacktrace, _Issue, _ArcJID, _IQ) -> |
668 |
:-( |
expected; |
669 |
|
report_issue(Reason, Stacktrace, Issue, #jid{lserver=LServer, luser=LUser}, IQ) -> |
670 |
6 |
?LOG_ERROR(#{what => mam_error, |
671 |
|
issue => Issue, server => LServer, user => LUser, |
672 |
:-( |
reason => Reason, iq => IQ, stacktrace => Stacktrace}). |
673 |
|
|
674 |
|
-spec is_archivable_message(HostType :: host_type(), |
675 |
|
Dir :: incoming | outgoing, |
676 |
|
Packet :: exml:element()) -> boolean(). |
677 |
|
is_archivable_message(HostType, Dir, Packet) -> |
678 |
3247 |
M = mod_mam_params:is_archivable_message_module(?MODULE, HostType), |
679 |
3247 |
ArchiveChatMarkers = mod_mam_params:archive_chat_markers(?MODULE, HostType), |
680 |
3247 |
erlang:apply(M, is_archivable_message, [?MODULE, Dir, Packet, ArchiveChatMarkers]). |
681 |
|
|
682 |
|
-spec hooks(jid:lserver()) -> [ejabberd_hooks:hook()]. |
683 |
|
hooks(HostType) -> |
684 |
86 |
[{disco_local_features, HostType, ?MODULE, disco_local_features, 99}, |
685 |
|
{user_send_packet, HostType, ?MODULE, user_send_packet, 60}, |
686 |
|
{rest_user_send_packet, HostType, ?MODULE, user_send_packet, 60}, |
687 |
|
{filter_local_packet, HostType, ?MODULE, filter_packet, 60}, |
688 |
|
{remove_user, HostType, ?MODULE, remove_user, 50}, |
689 |
|
{anonymous_purge_hook, HostType, ?MODULE, remove_user, 50}, |
690 |
|
{amp_determine_strategy, HostType, ?MODULE, determine_amp_strategy, 20}, |
691 |
|
{sm_filter_offline_message, HostType, ?MODULE, sm_filter_offline_message, 50}, |
692 |
|
{get_personal_data, HostType, ?MODULE, get_personal_data, 50} |
693 |
|
| mongoose_metrics_mam_hooks:get_mam_hooks(HostType)]. |
694 |
|
|
695 |
|
add_iq_handlers(HostType, Opts) -> |
696 |
43 |
Component = ejabberd_sm, |
697 |
|
%% `parallel' is the only one recommended here. |
698 |
43 |
ExecutionType = gen_mod:get_opt(iqdisc, Opts, parallel), |
699 |
43 |
IQHandlerFn = fun ?MODULE:process_mam_iq/5, |
700 |
43 |
Extra = #{}, |
701 |
43 |
[gen_iq_handler:add_iq_handler_for_domain(HostType, Namespace, |
702 |
|
Component, IQHandlerFn, |
703 |
|
Extra, ExecutionType) |
704 |
43 |
|| Namespace <- [?NS_MAM_04, ?NS_MAM_06]], |
705 |
43 |
ok. |
706 |
|
|
707 |
|
remove_iq_handlers(HostType) -> |
708 |
43 |
Component = ejabberd_sm, |
709 |
43 |
[gen_iq_handler:remove_iq_handler_for_domain(HostType, Namespace, Component) |
710 |
43 |
|| Namespace <- [?NS_MAM_04, ?NS_MAM_06]], |
711 |
43 |
ok. |
712 |
|
|
713 |
|
ensure_metrics(HostType) -> |
714 |
43 |
mongoose_metrics:ensure_metric(HostType, [backends, ?MODULE, lookup], histogram), |
715 |
43 |
mongoose_metrics:ensure_metric(HostType, [HostType, modMamLookups, simple], spiral), |
716 |
43 |
mongoose_metrics:ensure_metric(HostType, [backends, ?MODULE, archive], histogram), |
717 |
43 |
lists:foreach(fun(Name) -> |
718 |
387 |
mongoose_metrics:ensure_metric(HostType, Name, spiral) |
719 |
|
end, |
720 |
|
spirals()). |
721 |
|
|
722 |
|
spirals() -> |
723 |
43 |
[modMamPrefsSets, |
724 |
|
modMamPrefsGets, |
725 |
|
modMamArchiveRemoved, |
726 |
|
modMamLookups, |
727 |
|
modMamForwarded, |
728 |
|
modMamArchived, |
729 |
|
modMamFlushed, |
730 |
|
modMamDropped, |
731 |
|
modMamDroppedIQ]. |