./ct_report/coverage/mod_roster.COVER.html

1 %%%----------------------------------------------------------------------
2 %%% File : mod_roster.erl
3 %%% Author : Alexey Shchepin <alexey@process-one.net>
4 %%% Purpose : Roster management
5 %%% Created : 11 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
6 %%%
7 %%%
8 %%% ejabberd, Copyright (C) 2002-2013 ProcessOne
9 %%%
10 %%% This program is free software; you can redistribute it and/or
11 %%% modify it under the terms of the GNU General Public License as
12 %%% published by the Free Software Foundation; either version 2 of the
13 %%% License, or (at your option) any later version.
14 %%%
15 %%% This program is distributed in the hope that it will be useful,
16 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
17 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 %%% General Public License for more details.
19 %%%
20 %%% You should have received a copy of the GNU General Public License
21 %%% along with this program; if not, write to the Free Software
22 %%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 %%%
24 %%%----------------------------------------------------------------------
25
26 %%% @doc Roster management.
27 %%%
28 %%% Includes support for XEP-0237: Roster Versioning.
29 %%% The roster versioning follows an all-or-nothing strategy:
30 %%% - If the version supplied by the client is the latest, return an empty response.
31 %%% - If not, return the entire new roster (with updated version string).
32 %%% Roster version is a hash digest of the entire roster.
33 %%% No additional data is stored in DB.
34
35 -module(mod_roster).
36 -author('alexey@process-one.net').
37 -xep([{xep, 237}, {version, "1.3"}]).
38 -xep([{xep, 83}, {version, "1.0"}]).
39 -xep([{xep, 93}, {version, "1.2"}]).
40 -behaviour(gen_mod).
41 -behaviour(mongoose_module_metrics).
42
43 -export([start/2,
44 stop/1,
45 config_spec/0,
46 supported_features/0,
47 process_iq/5,
48 get_roster_entry/4,
49 item_to_map/1,
50 set_items/3,
51 set_roster_entry/5,
52 remove_from_roster/3,
53 item_to_xml/1
54 ]).
55
56 % Hook handlers
57 -export([
58 get_user_roster/2,
59 in_subscription/5,
60 out_subscription/4,
61 get_subscription_lists/2,
62 get_jid_info/4,
63 remove_user/3,
64 remove_domain/3,
65 get_versioning_feature/2,
66 get_personal_data/3
67 ]).
68
69 -export([transaction/2,
70 process_subscription_t/6,
71 get_user_rosters_length/2]). % for testing
72
73 -export([config_metrics/1]).
74
75 -ignore_xref([
76 get_jid_info/4, get_personal_data/3, get_subscription_lists/2,
77 get_user_roster/2, get_user_rosters_length/2, get_versioning_feature/2,
78 in_subscription/5, item_to_xml/1, out_subscription/4, process_subscription_t/6,
79 remove_user/3, remove_domain/3, transaction/2
80 ]).
81
82 -include("mongoose.hrl").
83 -include("jlib.hrl").
84 -include("mod_roster.hrl").
85 -include("mongoose_config_spec.hrl").
86
87 -export_type([roster/0, sub_presence/0]).
88
89 -type roster() :: #roster{}.
90
91 -type sub_presence() :: subscribe | subscribed | unsubscribe | unsubscribed.
92
93 -type subscription_state() :: none | from | to | both | remove.
94
95 -type get_user_roster_strategy() :: db_versioning | hash_versioning | no_versioning.
96
97 %% Types used in the backend API
98
99 -type contact() :: jid:simple_jid().
100 -type transaction_state() :: in_transaction | no_transaction.
101 -type entry_format() :: full | short.
102 -type version() :: binary().
103
104 -export_type([contact/0, transaction_state/0, entry_format/0, version/0]).
105
106 %%--------------------------------------------------------------------
107 %% gdpr callback
108 %%--------------------------------------------------------------------
109
110 -spec get_personal_data(gdpr:personal_data(), mongooseim:host_type(), jid:jid()) ->
111 gdpr:personal_data().
112 get_personal_data(Acc, HostType, #jid{ luser = LUser, lserver = LServer }) ->
113 84 Schema = ["jid", "name", "subscription", "ask", "groups", "askmessage", "xs"],
114 84 Records = get_roster(HostType, LUser, LServer),
115 84 SerializedRecords = lists:map(fun roster_record_to_gdpr_entry/1, Records),
116 84 [{roster, Schema, SerializedRecords} | Acc].
117
118 -spec roster_record_to_gdpr_entry(roster()) -> gdpr:entry().
119 roster_record_to_gdpr_entry(#roster{ jid = JID, name = Name,
120 subscription = Subscription, ask = Ask, groups = Groups,
121 askmessage = AskMessage, xs = XS }) ->
122 2 [
123 jid:to_binary(JID),
124 Name,
125 atom_to_binary(Subscription, utf8),
126 atom_to_binary(Ask, utf8),
127
:-(
string:join([ unicode:characters_to_list(G) || G <- Groups ], ", "),
128 AskMessage,
129
:-(
<< (exml:to_binary(X)) || X <- XS >>
130 ].
131
132 %%--------------------------------------------------------------------
133 %% mod_roster's callbacks
134 %%--------------------------------------------------------------------
135
136 -spec start(mongooseim:host_type(), gen_mod:module_opts()) -> any().
137 start(HostType, Opts = #{iqdisc := IQDisc}) ->
138 357 mod_roster_backend:init(HostType, Opts),
139 357 ejabberd_hooks:add(hooks(HostType)),
140 357 gen_iq_handler:add_iq_handler_for_domain(HostType, ?NS_ROSTER, ejabberd_sm,
141 fun ?MODULE:process_iq/5, #{}, IQDisc).
142
143 -spec stop(mongooseim:host_type()) -> any().
144 stop(HostType) ->
145 344 ejabberd_hooks:delete(hooks(HostType)),
146 344 gen_iq_handler:remove_iq_handler_for_domain(HostType, ?NS_ROSTER, ejabberd_sm).
147
148 -spec config_spec() -> mongoose_config_spec:config_section().
149 config_spec() ->
150 166 #section{
151 items = #{<<"iqdisc">> => mongoose_config_spec:iqdisc(),
152 <<"versioning">> => #option{type = boolean},
153 <<"store_current_id">> => #option{type = boolean},
154 <<"backend">> => #option{type = atom,
155 validate = {module, mod_roster}},
156 <<"riak">> => riak_config_spec()
157 },
158 defaults = #{<<"iqdisc">> => one_queue,
159 <<"versioning">> => false,
160 <<"store_current_id">> => false,
161 <<"backend">> => mnesia},
162 process = fun remove_unused_backend_opts/1
163 }.
164
165 riak_config_spec() ->
166 166 #section{items = #{<<"bucket_type">> => #option{type = binary,
167 validate = non_empty},
168 <<"version_bucket_type">> => #option{type = binary,
169 validate = non_empty}},
170 include = always,
171 defaults = #{<<"bucket_type">> => <<"rosters">>,
172 <<"version_bucket_type">> => <<"roster_versions">>}
173 }.
174
175
:-(
remove_unused_backend_opts(Opts = #{backend := riak}) -> Opts;
176 83 remove_unused_backend_opts(Opts) -> maps:remove(riak, Opts).
177
178 148 supported_features() -> [dynamic_domains].
179
180 hooks(HostType) ->
181 701 [{roster_get, HostType, ?MODULE, get_user_roster, 50},
182 {roster_in_subscription, HostType, ?MODULE, in_subscription, 50},
183 {roster_out_subscription, HostType, ?MODULE, out_subscription, 50},
184 {roster_get_subscription_lists, HostType, ?MODULE, get_subscription_lists, 50},
185 {roster_get_jid_info, HostType, ?MODULE, get_jid_info, 50},
186 {remove_user, HostType, ?MODULE, remove_user, 50},
187 {remove_domain, HostType, ?MODULE, remove_domain, 50},
188 {anonymous_purge_hook, HostType, ?MODULE, remove_user, 50},
189 {roster_get_versioning_feature, HostType, ?MODULE, get_versioning_feature, 50},
190 {get_personal_data, HostType, ?MODULE, get_personal_data, 50}].
191
192 -spec process_iq(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq(), map()) ->
193 {mongoose_acc:t(), jlib:iq()}.
194 process_iq(Acc, From = #jid{lserver = LServer, luser = LUser},
195 To = #jid{lserver = LServer, luser = LUser}, IQ, _Extra) ->
196 64 process_local_iq(Acc, From, To, IQ);
197 process_iq(Acc, _From, _To, IQ = #iq{lang = Lang, sub_el = SubEl}, _Extra) ->
198 %% Error type 'forbidden' is specified in Section 2.3.3 of RFC6121 for iq set.
199 %% The behaviour is unspecified for iq get, but it makes sense to handle them consistently.
200 2 ErrorMsg = <<"It is forbidden to query the roster of another user">>,
201 2 ErrorEl = mongoose_xmpp_errors:forbidden(Lang, ErrorMsg),
202 2 {Acc, IQ#iq{type = error, sub_el = [SubEl, ErrorEl]}}.
203
204 -spec process_local_iq(mongoose_acc:t(), jid:jid(), jid:jid(), jlib:iq()) ->
205 {mongoose_acc:t(), jlib:iq()}.
206 process_local_iq(Acc, From, To, #iq{type = Type} = IQ) ->
207 64 HostType = mongoose_acc:host_type(Acc),
208 64 IQReply = case Type of
209 37 set -> process_iq_set(HostType, From, To, IQ);
210 27 get -> process_iq_get(HostType, From, To, IQ)
211 end,
212 64 {Acc, IQReply}.
213
214 roster_hash(Items) ->
215 4 L = [R#roster{groups = lists:sort(Grs)} ||
216 4 R = #roster{groups = Grs} <- Items],
217 4 sha:sha1_hex(term_to_binary(lists:sort(L))).
218
219 -spec roster_versioning_enabled(mongooseim:host_type()) -> boolean().
220 roster_versioning_enabled(HostType) ->
221 3740 gen_mod:get_module_opt(HostType, ?MODULE, versioning, false).
222
223 -spec roster_version_on_db(mongooseim:host_type()) -> boolean().
224 roster_version_on_db(HostType) ->
225 546 gen_mod:get_module_opt(HostType, ?MODULE, store_current_id, false).
226
227 %% Returns a list that may contain an xmlel with the XEP-237 feature if it's enabled.
228 get_versioning_feature(Acc, HostType) ->
229 3248 case roster_versioning_enabled(HostType) of
230 true ->
231 4 Feature = #xmlel{name = <<"ver">>,
232 attrs = [{<<"xmlns">>, ?NS_ROSTER_VER}]},
233 4 [Feature | Acc];
234 3244 false -> []
235 end.
236
237 roster_version(HostType, #jid{luser = LUser, lserver = LServer} = JID) ->
238 2 case roster_version_on_db(HostType) of
239 true ->
240 1 case read_roster_version(HostType, LUser, LServer) of
241
:-(
error -> not_found;
242 1 V -> V
243 end;
244 false ->
245 1 R = get_roster_old(HostType, JID),
246 1 roster_hash(R)
247 end.
248
249 %% Load roster from DB only if neccesary.
250 %% It is neccesary if
251 %% - roster versioning is disabled in server OR
252 %% - roster versioning is not used by the client OR
253 %% - roster versioning is used by server and client,
254 %% BUT the server isn't storing versions on db OR
255 %% - the roster version from client don't match current version.
256 -spec process_iq_get(mongooseim:host_type(), jid:jid(), jid:jid(), jlib:iq()) -> jlib:iq().
257 process_iq_get(HostType, From, To, #iq{sub_el = SubEl} = IQ) ->
258 27 AttrVer = exml_query:attr(SubEl, <<"ver">>), %% type binary() | undefined
259 27 VersioningRequested = is_binary(AttrVer),
260 27 VersioningEnabled = roster_versioning_enabled(HostType),
261 27 VersionOnDb = roster_version_on_db(HostType),
262 27 Strategy = choose_get_user_roster_strategy(VersioningRequested, VersioningEnabled, VersionOnDb),
263 27 {ItemsToSend, VersionToSend} =
264 get_user_roster_based_on_version(HostType, Strategy, AttrVer, From, To),
265 27 IQ#iq{type = result, sub_el = create_sub_el(ItemsToSend, VersionToSend)}.
266
267 -spec choose_get_user_roster_strategy(VersioningRequested :: boolean(),
268 VersioningEnabled :: boolean(),
269 VersionOnDb :: boolean()) ->
270 get_user_roster_strategy().
271 3 choose_get_user_roster_strategy(true, true, true) -> db_versioning;
272 3 choose_get_user_roster_strategy(true, true, false) -> hash_versioning;
273 21 choose_get_user_roster_strategy(_, _, _) -> no_versioning.
274
275 get_user_roster_based_on_version(HostType, db_versioning, RequestedVersion, From, To) ->
276 3 get_user_roster_db_versioning(HostType, RequestedVersion, From, To);
277 get_user_roster_based_on_version(HostType, hash_versioning, RequestedVersion, From, To) ->
278 3 get_user_roster_hash_versioning(HostType, RequestedVersion, From, To);
279 get_user_roster_based_on_version(HostType, no_versioning, _RequestedVersion, From, To) ->
280 21 get_user_roster_no_versioning(HostType, From, To).
281
282 get_user_roster_db_versioning(HostType, RequestedVersion, From, To)
283 when is_binary(RequestedVersion) ->
284 3 LUser = From#jid.luser,
285 3 LServer = From#jid.lserver,
286 3 case read_roster_version(HostType, LUser, LServer) of
287 error ->
288 1 RosterVersion = write_roster_version(HostType, LUser, LServer),
289 1 {lists:map(fun item_to_xml/1,
290 get_roster_old(HostType, To#jid.lserver, From)),
291 RosterVersion};
292 RequestedVersion ->
293 1 {false, false};
294 NewVersion ->
295 1 {lists:map(fun item_to_xml/1,
296 get_roster_old(HostType, To#jid.lserver, From)),
297 NewVersion}
298 end.
299
300 get_user_roster_hash_versioning(HostType, RequestedVersion, From, To)
301 when is_binary(RequestedVersion) ->
302 3 RosterItems = get_roster_old(HostType, To#jid.lserver, From),
303 3 case roster_hash(RosterItems) of
304 RequestedVersion ->
305 1 {false, false};
306 New ->
307 2 {lists:map(fun item_to_xml/1, RosterItems), New}
308 end.
309
310 get_user_roster_no_versioning(HostType, From, To) ->
311 21 {lists:map(fun item_to_xml/1,
312 get_roster_old(HostType, To#jid.lserver, From)),
313 false}.
314
315 create_sub_el(false, false) ->
316 2 [];
317 create_sub_el(Items, false) ->
318 21 [#xmlel{name = <<"query">>,
319 attrs = [{<<"xmlns">>, ?NS_ROSTER}],
320 children = Items}];
321 create_sub_el(Items, Version) ->
322 4 [#xmlel{name = <<"query">>,
323 attrs = [{<<"xmlns">>, ?NS_ROSTER},
324 {<<"ver">>, Version}],
325 children = Items}].
326
327 -spec get_user_roster(mongoose_acc:t(), jid:jid() | {jid:luser(), jid:lserver()}) ->
328 mongoose_acc:t().
329 get_user_roster(Acc, #jid{luser = LUser, lserver = LServer}) ->
330 3075 HostType = mongoose_acc:host_type(Acc),
331 3075 case mongoose_acc:get(roster, show_full_roster, false, Acc) of
332 true ->
333 51 Roster = get_roster(HostType, LUser, LServer),
334 51 mongoose_acc:append(roster, items, Roster, Acc);
335 _ ->
336 3024 Roster = lists:filter(fun (#roster{subscription = none, ask = in}) ->
337 5 false;
338 (_) ->
339 159 true
340 end, get_roster(HostType, LUser, LServer)),
341 3024 mongoose_acc:append(roster, items, Roster, Acc)
342 end.
343
344 item_to_xml(Item) ->
345 416 Attrs1 = [{<<"jid">>,
346 jid:to_binary(Item#roster.jid)}],
347 416 Attrs2 = case Item#roster.name of
348 217 <<"">> -> Attrs1;
349 199 Name -> [{<<"name">>, Name} | Attrs1]
350 end,
351 416 Attrs3 = case Item#roster.subscription of
352 200 none -> [{<<"subscription">>, <<"none">>} | Attrs2];
353 53 from -> [{<<"subscription">>, <<"from">>} | Attrs2];
354 61 to -> [{<<"subscription">>, <<"to">>} | Attrs2];
355 81 both -> [{<<"subscription">>, <<"both">>} | Attrs2];
356 21 remove -> [{<<"subscription">>, <<"remove">>} | Attrs2]
357 end,
358 416 Attrs4 = case ask_to_pending(Item#roster.ask) of
359 83 out -> [{<<"ask">>, <<"subscribe">>} | Attrs3];
360 44 both -> [{<<"ask">>, <<"subscribe">>} | Attrs3];
361 289 _ -> Attrs3
362 end,
363 416 SubEls1 = lists:map(fun (G) ->
364 198 #xmlel{name = <<"group">>, attrs = [],
365 children = [{xmlcdata, G}]}
366 end,
367 Item#roster.groups),
368 416 SubEls = SubEls1 ++ Item#roster.xs,
369 416 #xmlel{name = <<"item">>, attrs = Attrs4,
370 children = SubEls}.
371
372 -spec process_iq_set(mongooseim:host_type(), jid:jid(), jid:jid(), jlib:iq()) -> jlib:iq().
373 process_iq_set(HostType, From, To, #iq{sub_el = SubEl} = IQ) ->
374 37 #xmlel{children = Els} = SubEl,
375 37 mongoose_hooks:roster_set(HostType, From, To, SubEl),
376 37 lists:foreach(fun(El) -> process_item_set(HostType, From, To, El) end, Els),
377 37 IQ#iq{type = result, sub_el = []}.
378
379 -spec process_item_set(mongooseim:host_type(), jid:jid(), jid:jid(), exml:element()) -> ok.
380 process_item_set(HostType, From, To, #xmlel{attrs = Attrs} = El) ->
381 37 JID1 = jid:from_binary(xml:get_attr_s(<<"jid">>, Attrs)),
382 37 do_process_item_set(HostType, JID1, From, To, El);
383
:-(
process_item_set(_HostType, _From, _To, _) -> ok.
384
385 -spec do_process_item_set(mongooseim:host_type(), error | jid:jid(), jid:jid(), jid:jid(),
386 exml:element()) -> ok.
387
:-(
do_process_item_set(_, error, _, _, _) -> ok;
388 do_process_item_set(HostType, #jid{} = JID1, #jid{} = From, #jid{} = To,
389 #xmlel{attrs = Attrs, children = Els}) ->
390 37 MakeItem2 = fun(Item) ->
391 37 Item1 = process_item_attrs(Item, Attrs),
392 37 process_item_els(Item1, Els)
393 end,
394 37 set_roster_item(HostType, JID1, From, To, MakeItem2),
395 37 ok.
396
397 %% @doc this is run when a roster item is to be added, updated or removed
398 %% the interface of this func could probably be a bit simpler
399 -spec set_roster_item(mongooseim:host_type(),
400 ContactJID :: jid:jid(),
401 From :: jid:jid(),
402 To :: jid:jid(),
403 fun((roster()) -> roster())) -> ok | {error, any()}.
404 set_roster_item(HostType, ContactJID, From, To, MakeItem) ->
405 106 ContactLJID = jid:to_lower(ContactJID),
406 106 F = fun() -> set_roster_item_t(HostType, From, ContactLJID, MakeItem) end,
407 106 case transaction(HostType, F) of
408 {atomic, does_not_exist} ->
409 5 {error, does_not_exist};
410 {atomic, {OldItem, NewItem}} ->
411 101 push_item(HostType, From, To, NewItem),
412 101 case NewItem#roster.subscription of
413 remove ->
414 22 send_unsubscribing_presence(From, OldItem),
415 22 ok;
416 79 _ -> ok
417 end;
418 E ->
419
:-(
?LOG_ERROR(#{what => roster_set_item_failed, reason => E}),
420
:-(
{error, E}
421 end.
422
423 -spec set_roster_item_t(mongooseim:host_type(), jid:jid(), contact(),
424 fun((roster()) -> roster())) ->
425 does_not_exist | {roster(), roster()}.
426 set_roster_item_t(HostType, UserJid = #jid{luser = LUser, lserver = LServer},
427 ContactLJID, MakeItem) ->
428 106 InitialItem = get_roster_entry_t(HostType, UserJid, ContactLJID, short),
429 106 Item1 = case InitialItem of
430 74 does_not_exist -> new_roster_item(LUser, LServer, ContactLJID);
431 32 Item -> Item
432 end,
433 106 Item2 = MakeItem(Item1),
434 106 case {InitialItem, Item2} of
435 {does_not_exist, #roster{subscription = remove}} ->
436 %% Cannot remove a non-existing item
437 5 does_not_exist;
438 _ ->
439 101 case Item2#roster.subscription of
440 22 remove -> del_roster_t(HostType, LUser, LServer, ContactLJID);
441 79 _ -> update_roster_t(HostType, Item2)
442 end,
443 101 Item3 = mongoose_hooks:roster_process_item(HostType, LServer, Item2),
444 101 case roster_version_on_db(HostType) of
445 1 true -> write_roster_version_t(HostType, LUser, LServer);
446 100 false -> ok
447 end,
448 101 {Item1, Item3}
449 end.
450
451 -spec new_roster_item(jid:luser(), jid:lserver(), jid:simple_jid()) -> roster().
452 new_roster_item(LUser, LServer, ContactLJID) ->
453 74 #roster{usj = {LUser, LServer, ContactLJID},
454 us = {LUser, LServer},
455 jid = ContactLJID}.
456
457 process_item_attrs(Item, [{<<"jid">>, Val} | Attrs]) ->
458 37 case jid:from_binary(Val) of
459 error ->
460
:-(
process_item_attrs(Item, Attrs);
461 JID1 ->
462 37 JID = jid:to_lower(JID1),
463 37 process_item_attrs(Item#roster{jid = JID}, Attrs)
464 end;
465 process_item_attrs(Item, [{<<"name">>, Val} | Attrs]) ->
466 30 process_item_attrs(Item#roster{name = Val}, Attrs);
467 process_item_attrs(Item, [{<<"subscription">>, <<"remove">>} | Attrs]) ->
468 7 process_item_attrs(Item#roster{subscription = remove}, Attrs);
469 process_item_attrs(Item, [_ | Attrs]) ->
470
:-(
process_item_attrs(Item, Attrs);
471 process_item_attrs(Item, []) ->
472 37 Item.
473
474 process_item_els(Item,
475 [#xmlel{name = Name, attrs = Attrs, children = SEls}
476 | Els]) ->
477 81 case Name of
478 <<"group">> ->
479 81 Groups = [xml:get_cdata(SEls) | Item#roster.groups],
480 81 process_item_els(Item#roster{groups = Groups}, Els);
481 _ ->
482
:-(
case xml:get_attr_s(<<"xmlns">>, Attrs) of
483
:-(
<<"">> -> process_item_els(Item, Els);
484 _ ->
485
:-(
XEls = [#xmlel{name = Name, attrs = Attrs,
486 children = SEls}
487 | Item#roster.xs],
488
:-(
process_item_els(Item#roster{xs = XEls}, Els)
489 end
490 end;
491 process_item_els(Item, [{xmlcdata, _} | Els]) ->
492
:-(
process_item_els(Item, Els);
493 104 process_item_els(Item, []) -> Item.
494
495 push_item(HostType, JID, From, Item) ->
496 465 ejabberd_sm:route(jid:make_noprep(<<>>, <<>>, <<>>), JID,
497 {broadcast, {item, Item#roster.jid, Item#roster.subscription}}),
498 465 case roster_versioning_enabled(HostType) of
499 true ->
500 2 push_item_version(JID, From, Item, roster_version(HostType, JID));
501 false ->
502 463 lists:foreach(fun(Resource) ->
503 384 push_item_without_version(HostType, JID, Resource, From, Item)
504 end,
505 ejabberd_sm:get_user_resources(JID))
506 end.
507
508 push_item_without_version(HostType, JID, Resource, From, Item) ->
509 384 mongoose_hooks:roster_push(HostType, From, Item),
510 384 push_item_final(jid:replace_resource(JID, Resource), From, Item, not_found).
511
512 push_item_version(JID, From, Item, RosterVersion) ->
513 2 lists:foreach(fun(Resource) ->
514 2 push_item_final(jid:replace_resource(JID, Resource),
515 From, Item, RosterVersion)
516 end, ejabberd_sm:get_user_resources(JID)).
517
518 push_item_final(JID, From, Item, RosterVersion) ->
519 386 ExtraAttrs = case RosterVersion of
520 384 not_found -> [];
521 2 _ -> [{<<"ver">>, RosterVersion}]
522 end,
523 386 ResIQ = #iq{type = set, xmlns = ?NS_ROSTER,
524 %% @doc Roster push, calculate and include the version attribute.
525 %% TODO: don't push to those who didn't load roster
526 id = <<"push", (mongoose_bin:gen_from_crypto())/binary>>,
527 sub_el =
528 [#xmlel{name = <<"query">>,
529 attrs = [{<<"xmlns">>, ?NS_ROSTER} | ExtraAttrs],
530 children = [item_to_xml(Item)]}]},
531 386 ejabberd_router:route(From, JID, jlib:iq_to_xml(ResIQ)).
532
533 -spec get_subscription_lists(Acc :: mongoose_acc:t(), JID :: jid:jid()) ->
534 mongoose_acc:t().
535 get_subscription_lists(Acc, #jid{luser = LUser, lserver = LServer} = JID) ->
536 6190 Items = mod_roster_backend:get_subscription_lists(Acc, LUser, LServer),
537 6190 SubLists = fill_subscription_lists(JID, LServer, Items, [], [], []),
538 6190 mongoose_acc:set(roster, subscription_lists, SubLists, Acc).
539
540 fill_subscription_lists(JID, LServer, [#roster{} = I | Is], F, T, P) ->
541 32 J = element(3, I#roster.usj),
542 32 NewP = build_pending(I, JID, P),
543 32 case I#roster.subscription of
544 both ->
545 25 fill_subscription_lists(JID, LServer, Is, [J | F], [J | T], NewP);
546 from ->
547 1 fill_subscription_lists(JID, LServer, Is, [J | F], T, NewP);
548
:-(
to -> fill_subscription_lists(JID, LServer, Is, F, [J | T], NewP);
549 6 _ -> fill_subscription_lists(JID, LServer, Is, F, T, NewP)
550 end;
551 6190 fill_subscription_lists(_, _LServer, [], F, T, P) -> {F, T, P}.
552
553 build_pending(#roster{ask = Ask} = I, JID, P)
554 when Ask == in; Ask == both ->
555 6 Status = case I#roster.askmessage of
556 6 Message when is_binary(Message) -> Message;
557
:-(
true -> <<>>
558 end,
559 6 StatusEl = #xmlel{
560 name = <<"status">>,
561 children = [#xmlcdata{content = Status}]},
562 6 El = #xmlel{
563 name = <<"presence">>,
564 attrs = [{<<"from">>, jid:to_binary(I#roster.jid)},
565 {<<"to">>, jid:to_binary(JID)},
566 {<<"type">>, <<"subscribe">>}],
567 children = [StatusEl]},
568 6 [El | P];
569 build_pending(_, _, P) ->
570 26 P.
571
572
:-(
ask_to_pending(subscribe) -> out;
573
:-(
ask_to_pending(unsubscribe) -> none;
574 416 ask_to_pending(Ask) -> Ask.
575
576 -spec in_subscription(Acc :: mongoose_acc:t(),
577 ToJID :: jid:jid(),
578 FromJID :: jid:jid(),
579 Type :: sub_presence(),
580 Reason :: iodata()) ->
581 mongoose_acc:t().
582 in_subscription(Acc, ToJID, FromJID, Type, Reason) ->
583 275 HostType = mongoose_acc:host_type(Acc),
584 275 Res = process_subscription(HostType, in, ToJID, FromJID, Type, Reason),
585 275 mongoose_acc:set(hook, result, Res, Acc).
586
587 -spec out_subscription(Acc :: mongoose_acc:t(),
588 FromJID :: jid:jid(),
589 ToJID :: jid:jid(),
590 Type :: sub_presence()) ->
591 mongoose_acc:t().
592 out_subscription(Acc, FromJID, ToJID, Type) ->
593 170 HostType = mongoose_acc:host_type(Acc),
594 170 Res = process_subscription(HostType, out, FromJID, ToJID, Type, <<>>),
595 170 mongoose_acc:set(hook, result, Res, Acc).
596
597 -spec process_subscription(mongooseim:host_type(), in | out, jid:jid(), jid:jid(),
598 sub_presence(), iodata()) ->
599 boolean().
600 process_subscription(HostType, Direction, JID, ContactJID, Type, Reason) ->
601 445 TransactionFun =
602 fun() ->
603 455 process_subscription_t(HostType, Direction, JID, ContactJID, Type, Reason)
604 end,
605 445 case transaction(HostType, TransactionFun) of
606 {atomic, {Push, AutoReply}} ->
607 445 case AutoReply of
608 397 none -> ok;
609 _ ->
610 48 PresenceStanza = #xmlel{name = <<"presence">>,
611 attrs = [{<<"type">>, autoreply_to_type(AutoReply)}],
612 children = []},
613 48 ejabberd_router:route(JID, ContactJID, PresenceStanza)
614 end,
615 445 case Push of
616 {push, #roster{subscription = none, ask = in}} ->
617 52 true;
618 {push, Item} ->
619 364 push_item(HostType, JID, JID, Item),
620 364 true;
621 29 none -> false
622 end;
623
:-(
_ -> false
624 end.
625
626
:-(
autoreply_to_type(subscribed) -> <<"subscribed">>;
627 48 autoreply_to_type(unsubscribed) -> <<"unsubscribed">>.
628
629 -spec process_subscription_t(mongooseim:host_type(), in | out, jid:jid(), jid:jid(),
630 sub_presence(), iodata()) ->
631 {Push :: none | {push, roster()},
632 AutoReply :: none | subscribed | unsubscribed}.
633 process_subscription_t(HostType, Direction, JID, ContactJID, Type, Reason) ->
634 455 #jid{luser = LUser, lserver = LServer} = JID,
635 455 ContactLJID = jid:to_lower(ContactJID),
636 455 Item = case get_roster_entry_t(HostType, JID, ContactLJID, full) of
637 does_not_exist ->
638 79 #roster{usj = {LUser, LServer, ContactLJID},
639 us = {LUser, LServer},
640 jid = ContactLJID};
641 376 R -> R
642 end,
643 455 NewState = case Direction of
644 out ->
645 170 out_state_change(Item#roster.subscription,
646 Item#roster.ask, Type);
647 in ->
648 285 in_state_change(Item#roster.subscription,
649 Item#roster.ask, Type)
650 end,
651 455 AutoReply = case Direction of
652 170 out -> none;
653 in ->
654 285 in_auto_reply(Item#roster.subscription,
655 Item#roster.ask, Type)
656 end,
657 455 AskMessage = case NewState of
658 44 {_, both} -> Reason;
659 82 {_, in} -> Reason;
660 329 _ -> <<"">>
661 end,
662 455 case NewState of
663 24 none -> {none, AutoReply};
664 {none, none}
665 when Item#roster.subscription == none,
666 Item#roster.ask == in ->
667 5 del_roster_t(HostType, LUser, LServer, ContactLJID), {none, AutoReply};
668 {Subscription, Pending} ->
669 426 NewItem = Item#roster{subscription = Subscription,
670 ask = Pending,
671 askmessage = iolist_to_binary(AskMessage)},
672 426 roster_subscribe_t(HostType, NewItem),
673 416 case roster_version_on_db(HostType) of
674
:-(
true -> write_roster_version_t(HostType, LUser, LServer);
675 416 false -> ok
676 end,
677 416 {{push, NewItem}, AutoReply}
678 end.
679
680 %% in_state_change(Subscription, Pending, Type) -> NewState
681 %% NewState = none | {NewSubscription, NewPending}
682 -ifdef(ROSTER_GATEWAY_WORKAROUND).
683
684 -define(NNSD, {to, none}).
685
686 -define(NISD, {to, in}).
687
688 -else.
689
690 -define(NNSD, none).
691
692 -define(NISD, none).
693
694 -endif.
695
696 52 in_state_change(none, none, subscribe) -> {none, in};
697 1 in_state_change(none, none, subscribed) -> ?NNSD;
698 4 in_state_change(none, none, unsubscribe) -> none;
699 13 in_state_change(none, none, unsubscribed) -> none;
700 22 in_state_change(none, out, subscribe) -> {none, both};
701 22 in_state_change(none, out, subscribed) -> {to, none};
702
:-(
in_state_change(none, out, unsubscribe) -> none;
703 5 in_state_change(none, out, unsubscribed) -> {none, none};
704 1 in_state_change(none, in, subscribe) -> none;
705
:-(
in_state_change(none, in, subscribed) -> ?NISD;
706
:-(
in_state_change(none, in, unsubscribe) -> {none, none};
707
:-(
in_state_change(none, in, unsubscribed) -> none;
708
:-(
in_state_change(none, both, subscribe) -> none;
709 22 in_state_change(none, both, subscribed) -> {to, in};
710
:-(
in_state_change(none, both, unsubscribe) -> {none, out};
711
:-(
in_state_change(none, both, unsubscribed) -> {none, in};
712 8 in_state_change(to, none, subscribe) -> {to, in};
713
:-(
in_state_change(to, none, subscribed) -> none;
714
:-(
in_state_change(to, none, unsubscribe) -> none;
715 41 in_state_change(to, none, unsubscribed) -> {none, none};
716
:-(
in_state_change(to, in, subscribe) -> none;
717
:-(
in_state_change(to, in, subscribed) -> none;
718
:-(
in_state_change(to, in, unsubscribe) -> {to, none};
719
:-(
in_state_change(to, in, unsubscribed) -> {none, in};
720
:-(
in_state_change(from, none, subscribe) -> none;
721
:-(
in_state_change(from, none, subscribed) -> {both, none};
722 13 in_state_change(from, none, unsubscribe) -> {none, none};
723
:-(
in_state_change(from, none, unsubscribed) -> none;
724
:-(
in_state_change(from, out, subscribe) -> none;
725 30 in_state_change(from, out, subscribed) -> {both, none};
726
:-(
in_state_change(from, out, unsubscribe) -> {none, out};
727
:-(
in_state_change(from, out, unsubscribed) -> {from, none};
728
:-(
in_state_change(both, none, subscribe) -> none;
729
:-(
in_state_change(both, none, subscribed) -> none;
730 44 in_state_change(both, none, unsubscribe) -> {to, none};
731 7 in_state_change(both, none, unsubscribed) -> {from, none}.
732
733 52 out_state_change(none, none, subscribe) -> {none, out};
734 4 out_state_change(none, none, subscribed) -> none;
735 1 out_state_change(none, none, unsubscribe) -> none;
736
:-(
out_state_change(none, none, unsubscribed) -> none;
737 out_state_change(none, out, subscribe) ->
738
:-(
{none, out}; %% We need to resend query (RFC3921, section 9.2)
739
:-(
out_state_change(none, out, subscribed) -> none;
740
:-(
out_state_change(none, out, unsubscribe) -> {none, none};
741
:-(
out_state_change(none, out, unsubscribed) -> none;
742 22 out_state_change(none, in, subscribe) -> {none, both};
743 21 out_state_change(none, in, subscribed) -> {from, none};
744
:-(
out_state_change(none, in, unsubscribe) -> none;
745 5 out_state_change(none, in, unsubscribed) -> {none, none};
746
:-(
out_state_change(none, both, subscribe) -> none;
747 22 out_state_change(none, both, subscribed) -> {from, out};
748
:-(
out_state_change(none, both, unsubscribe) -> {none, in};
749
:-(
out_state_change(none, both, unsubscribed) -> {none, out};
750
:-(
out_state_change(to, none, subscribe) -> none;
751
:-(
out_state_change(to, none, subscribed) -> {both, none};
752 4 out_state_change(to, none, unsubscribe) -> {none, none};
753
:-(
out_state_change(to, none, unsubscribed) -> none;
754
:-(
out_state_change(to, in, subscribe) -> none;
755 30 out_state_change(to, in, subscribed) -> {both, none};
756
:-(
out_state_change(to, in, unsubscribe) -> {none, in};
757
:-(
out_state_change(to, in, unsubscribed) -> {to, none};
758 8 out_state_change(from, none, subscribe) -> {from, out};
759
:-(
out_state_change(from, none, subscribed) -> none;
760
:-(
out_state_change(from, none, unsubscribe) -> none;
761
:-(
out_state_change(from, none, unsubscribed) -> {none, none};
762
:-(
out_state_change(from, out, subscribe) -> none;
763
:-(
out_state_change(from, out, subscribed) -> none;
764
:-(
out_state_change(from, out, unsubscribe) -> {from, none};
765
:-(
out_state_change(from, out, unsubscribed) -> {none, out};
766
:-(
out_state_change(both, none, subscribe) -> none;
767
:-(
out_state_change(both, none, subscribed) -> none;
768
:-(
out_state_change(both, none, unsubscribe) -> {from, none};
769 1 out_state_change(both, none, unsubscribed) -> {to, none}.
770
771
:-(
in_auto_reply(from, none, subscribe) -> subscribed;
772
:-(
in_auto_reply(from, out, subscribe) -> subscribed;
773
:-(
in_auto_reply(both, none, subscribe) -> subscribed;
774
:-(
in_auto_reply(none, in, unsubscribe) -> unsubscribed;
775
:-(
in_auto_reply(none, both, unsubscribe) -> unsubscribed;
776
:-(
in_auto_reply(to, in, unsubscribe) -> unsubscribed;
777 13 in_auto_reply(from, none, unsubscribe) -> unsubscribed;
778
:-(
in_auto_reply(from, out, unsubscribe) -> unsubscribed;
779 44 in_auto_reply(both, none, unsubscribe) -> unsubscribed;
780 228 in_auto_reply(_, _, _) -> none.
781
782 get_user_rosters_length(HostType, JID) ->
783 1 length(get_roster_old(HostType, JID)).
784
785 -spec remove_user(mongoose_acc:t(), jid:luser(), jid:lserver()) -> mongoose_acc:t().
786 remove_user(Acc, LUser, LServer) ->
787 2969 HostType = mongoose_acc:host_type(Acc),
788 2969 JID = jid:make_noprep(LUser, LServer, <<>>),
789 2969 Acc1 = try_send_unsubscription_to_rosteritems(Acc, JID),
790 2969 F = fun() -> mod_roster_backend:remove_user_t(HostType, LUser, LServer) end,
791 2969 case transaction(HostType, F) of
792 {atomic, ok} ->
793 2969 ok;
794 Result ->
795
:-(
?LOG_ERROR(#{what => remove_user_transaction_failed, reason => Result})
796 end,
797 2969 Acc1.
798
799 try_send_unsubscription_to_rosteritems(Acc, JID) ->
800 2969 try
801 2969 send_unsubscription_to_rosteritems(Acc, JID)
802 catch
803 E:R:S ->
804
:-(
?LOG_WARNING(#{what => roster_unsubcribe_failed,
805
:-(
class => E, reason => R, stacktrace => S}),
806
:-(
Acc
807 end.
808
809 %% For each contact with Subscription:
810 %% Both or From, send a "unsubscribed" presence stanza;
811 %% Both or To, send a "unsubscribe" presence stanza.
812 send_unsubscription_to_rosteritems(Acc, JID) ->
813 2969 Acc1 = get_user_roster(Acc, JID),
814 2969 RosterItems = mongoose_acc:get(roster, items, [], Acc1),
815 2969 lists:foreach(fun(RosterItem) ->
816 121 send_unsubscribing_presence(JID, RosterItem)
817 end, RosterItems),
818 2969 Acc1.
819
820 -spec send_unsubscribing_presence(From :: jid:jid(), Item :: roster()) -> ok | mongoose_acc:t().
821 send_unsubscribing_presence(From, #roster{ subscription = Subscription } = Item) ->
822 143 BareFrom = jid:to_bare(From),
823 143 ContactJID = jid:make_noprep(Item#roster.jid),
824 143 IsTo = Subscription == both orelse Subscription == to,
825 143 IsFrom = Subscription == both orelse Subscription == from,
826 143 case IsTo of
827 52 true -> send_presence_type(BareFrom, ContactJID, <<"unsubscribe">>);
828 91 _ -> ok
829 end,
830 143 case IsFrom of
831 47 true -> send_presence_type(BareFrom, ContactJID, <<"unsubscribed">>);
832 96 _ -> ok
833 end.
834
835 send_presence_type(From, To, Type) ->
836 99 ejabberd_router:route(From, To,
837 #xmlel{name = <<"presence">>,
838 attrs = [{<<"type">>, Type}], children = []}).
839
840 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
841
842 -spec remove_domain(mongoose_hooks:simple_acc(),
843 mongooseim:host_type(), jid:lserver()) ->
844 mongoose_hooks:simple_acc().
845 remove_domain(Acc, HostType, Domain) ->
846
:-(
case backend_module:is_exported(mod_roster_backend, remove_domain_t, 2) of
847 true ->
848
:-(
F = fun() -> mod_roster_backend:remove_domain_t(HostType, Domain) end,
849
:-(
case transaction(HostType, F) of
850 {atomic, ok} ->
851
:-(
ok;
852 Result ->
853
:-(
?LOG_ERROR(#{what => remove_domain_transaction_failed,
854
:-(
reason => Result})
855 end;
856 false ->
857
:-(
ok
858 end,
859
:-(
Acc.
860
861 -spec set_items(mongooseim:host_type(), jid:jid(), exml:element()) -> ok | {error, any()}.
862 set_items(HostType, #jid{luser = LUser, lserver = LServer}, SubEl) ->
863 67 F = fun() -> set_items_t(HostType, LUser, LServer, SubEl) end,
864 67 case transaction(HostType, F) of
865 {atomic, _} ->
866 67 ok;
867 Result ->
868
:-(
{error, Result}
869 end.
870
871 set_items_t(HostType, LUser, LServer, #xmlel{children = Els}) ->
872 67 lists:foreach(fun(El) ->
873 67 process_item_set_t(HostType, LUser, LServer, El)
874 end, Els).
875
876 %% @doc add a contact to roster, or update
877 -spec set_roster_entry(mongooseim:host_type(), jid:jid(), jid:jid(), binary(), [binary()]) ->
878 ok | {error, any()}.
879 set_roster_entry(HostType, UserJid, ContactJid, Name, Groups) ->
880 49 UpdateF = fun(Item) -> Item#roster{name = Name, groups = Groups} end,
881 49 set_roster_item(HostType, ContactJid, UserJid, UserJid, UpdateF).
882
883 %% @doc remove from roster - in practice it means changing subscription state to 'remove'
884 -spec remove_from_roster(mongooseim:host_type(), UserJid :: jid:jid(), ContactJid :: jid:jid()) ->
885 ok | {error, any()}.
886 remove_from_roster(HostType, UserJid, ContactJid) ->
887 20 UpdateF = fun(Item) -> Item#roster{subscription = remove} end,
888 20 set_roster_item(HostType, ContactJid, UserJid, UserJid, UpdateF).
889
890 process_item_set_t(HostType, LUser, LServer,
891 #xmlel{attrs = Attrs, children = Els}) ->
892 67 JID1 = jid:from_binary(xml:get_attr_s(<<"jid">>, Attrs)),
893 67 case JID1 of
894
:-(
error -> ok;
895 _ ->
896 67 LJID = {JID1#jid.luser, JID1#jid.lserver, JID1#jid.lresource},
897 67 Item = #roster{usj = {LUser, LServer, LJID},
898 us = {LUser, LServer}, jid = LJID},
899 67 Item1 = process_item_attrs_ws(Item, Attrs),
900 67 Item2 = process_item_els(Item1, Els),
901 67 case Item2#roster.subscription of
902 14 remove -> del_roster_t(HostType, LUser, LServer, LJID);
903 53 _ -> update_roster_t(HostType, Item2)
904 end
905 end;
906
:-(
process_item_set_t(_HostType, _LUser, _LServer, _) -> ok.
907
908 process_item_attrs_ws(Item, [{<<"jid">>, Val} | Attrs]) ->
909 67 case jid:from_binary(Val) of
910 error ->
911
:-(
process_item_attrs_ws(Item, Attrs);
912 JID1 ->
913 67 JID = {JID1#jid.luser, JID1#jid.lserver, JID1#jid.lresource},
914 67 process_item_attrs_ws(Item#roster{jid = JID}, Attrs)
915 end;
916 process_item_attrs_ws(Item, [{<<"name">>, Val} | Attrs]) ->
917 53 process_item_attrs_ws(Item#roster{name = Val}, Attrs);
918 process_item_attrs_ws(Item, [{<<"subscription">>, <<"remove">>} | Attrs]) ->
919 14 process_item_attrs_ws(Item#roster{subscription = remove}, Attrs);
920 process_item_attrs_ws(Item, [{<<"subscription">>, <<"none">>} | Attrs]) ->
921
:-(
process_item_attrs_ws(Item#roster{subscription = none}, Attrs);
922 process_item_attrs_ws(Item, [{<<"subscription">>, <<"both">>} | Attrs]) ->
923 46 process_item_attrs_ws(Item#roster{subscription = both}, Attrs);
924 process_item_attrs_ws(Item, [{<<"subscription">>, <<"from">>} | Attrs]) ->
925 4 process_item_attrs_ws(Item#roster{subscription = from}, Attrs);
926 process_item_attrs_ws(Item, [{<<"subscription">>, <<"to">>} | Attrs]) ->
927 3 process_item_attrs_ws(Item#roster{subscription = to}, Attrs);
928 process_item_attrs_ws(Item, [_ | Attrs]) ->
929
:-(
process_item_attrs_ws(Item, Attrs);
930 process_item_attrs_ws(Item, []) ->
931 67 Item.
932
933 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
934
935 -spec get_jid_info(HookAcc, mongooseim:host_type(),
936 ToJID :: jid:jid(),
937 JID :: jid:jid() | jid:ljid()) -> HookAcc
938 when HookAcc :: {subscription_state(), [binary()]}.
939 get_jid_info(_, HostType, ToJID, JID) ->
940 91 case get_roster_entry(HostType, ToJID, JID, full) of
941
:-(
error -> {none, []};
942 does_not_exist ->
943 64 LRJID = jid:to_bare(jid:to_lower(JID)),
944 64 case get_roster_entry(HostType, ToJID, LRJID, full) of
945
:-(
error -> {none, []};
946 42 does_not_exist -> {none, []};
947 22 R -> {R#roster.subscription, R#roster.groups}
948 end;
949 27 Re -> {Re#roster.subscription, Re#roster.groups}
950 end.
951
952 get_roster_old(HostType, #jid{lserver = LServer} = JID) ->
953 2 get_roster_old(HostType, LServer, JID).
954
955 get_roster_old(HostType, DestServer, JID) ->
956 28 A = mongoose_acc:new(#{ location => ?LOCATION,
957 lserver => DestServer,
958 host_type => HostType,
959 element => undefined }),
960 28 A2 = mongoose_hooks:roster_get(A, JID),
961 28 mongoose_acc:get(roster, items, [], A2).
962
963 -spec item_to_map(roster()) -> map().
964 item_to_map(#roster{} = Roster) ->
965 19 ContactJid = jid:make_noprep(jid:to_bare(Roster#roster.jid)),
966 19 ContactName = Roster#roster.name,
967 19 Subs = Roster#roster.subscription,
968 19 Groups = Roster#roster.groups,
969 19 Ask = Roster#roster.ask,
970 19 #{jid => ContactJid, name => ContactName, subscription => Subs,
971 groups => Groups, ask => Ask}.
972
973 -spec config_metrics(mongooseim:host_type()) -> [{gen_mod:opt_key(), gen_mod:opt_value()}].
974 config_metrics(HostType) ->
975 191 mongoose_module_metrics:opts_for_module(HostType, ?MODULE, [backend]).
976
977 %% Backend API wrappers
978
979 -spec transaction(mongooseim:host_type(), fun(() -> any())) ->
980 {aborted, any()} | {atomic, any()} | {error, any()}.
981 transaction(HostType, F) ->
982 3587 mod_roster_backend:transaction(HostType, F).
983
984 -spec read_roster_version(mongooseim:host_type(), jid:luser(), jid:lserver()) ->
985 binary() | error.
986 read_roster_version(HostType, LUser, LServer) ->
987 4 mod_roster_backend:read_roster_version(HostType, LUser, LServer).
988
989 -spec write_roster_version(mongooseim:host_type(), jid:luser(), jid:lserver()) -> version().
990 write_roster_version(HostType, LUser, LServer) ->
991 1 write_roster_version(HostType, LUser, LServer, no_transaction).
992
993 -spec write_roster_version_t(mongooseim:host_type(), jid:luser(), jid:lserver()) -> version().
994 write_roster_version_t(HostType, LUser, LServer) ->
995 1 write_roster_version(HostType, LUser, LServer, in_transaction).
996
997 -spec write_roster_version(mongooseim:host_type(), jid:luser(), jid:lserver(),
998 transaction_state()) -> version().
999 write_roster_version(HostType, LUser, LServer, TransactionState) ->
1000 2 Ver = sha:sha1_hex(term_to_binary(os:timestamp())),
1001 2 mod_roster_backend:write_roster_version(HostType, LUser, LServer, TransactionState, Ver),
1002 2 Ver.
1003
1004 -spec get_roster(mongooseim:host_type(), jid:luser(), jid:lserver()) -> [roster()].
1005 get_roster(HostType, LUser, LServer) ->
1006 3159 mod_roster_backend:get_roster(HostType, LUser, LServer).
1007
1008 -spec get_roster_entry(mongooseim:host_type(), jid:jid(), contact(), entry_format()) ->
1009 roster() | does_not_exist | error.
1010 get_roster_entry(HostType, #jid{luser = LUser, lserver = LServer}, LJid, Format) ->
1011 182 mod_roster_backend:get_roster_entry(HostType, LUser, LServer, LJid, no_transaction, Format).
1012
1013 -spec get_roster_entry_t(mongooseim:host_type(), jid:jid(), contact(), entry_format()) ->
1014 roster() | does_not_exist | error.
1015 get_roster_entry_t(HostType, #jid{luser = LUser, lserver = LServer}, LJid, Format) ->
1016 561 mod_roster_backend:get_roster_entry(HostType, LUser, LServer, LJid, in_transaction, Format).
1017
1018 -spec roster_subscribe_t(mongooseim:host_type(), roster()) -> ok.
1019 roster_subscribe_t(HostType, Item) ->
1020 426 mod_roster_backend:roster_subscribe_t(HostType, Item).
1021
1022 -spec update_roster_t(mongooseim:host_type(), roster()) -> ok.
1023 update_roster_t(HostType, Item) ->
1024 132 mod_roster_backend:update_roster_t(HostType, Item).
1025
1026 -spec del_roster_t(mongooseim:host_type(), jid:luser(), jid:lserver(), contact()) -> ok.
1027 del_roster_t(HostType, LUser, LServer, LJID) ->
1028 41 mod_roster_backend:del_roster_t(HostType, LUser, LServer, LJID).
Line Hits Source