2002-12-11 21:57:45 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_roster.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-09-10 22:57:00 +02:00
|
|
|
%%% Purpose : Roster management
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 11 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
2002-12-11 21:57:45 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2009-08-06 23:06:16 +02:00
|
|
|
%%% @doc Roster management (Mnesia storage).
|
|
|
|
%%%
|
|
|
|
%%% Includes support for XEP-0237: Roster Versioning.
|
|
|
|
%%% The roster versioning follows an all-or-nothing strategy:
|
|
|
|
%%% - If the version supplied by the client is the latest, return an empty response.
|
|
|
|
%%% - If not, return the entire new roster (with updated version string).
|
|
|
|
%%% Roster version is a hash digest of the entire roster.
|
|
|
|
%%% No additional data is stored in DB.
|
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
-module(mod_roster).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 237, '1.3'}).
|
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, process_iq/1, export/1,
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info/0, process_local_iq/1, get_user_roster/2,
|
2018-03-19 10:32:43 +01:00
|
|
|
import/5, get_roster/2, push_item/3,
|
2018-06-28 09:37:20 +02:00
|
|
|
import_start/2, import_stop/2, is_subscribed/2,
|
2018-01-26 13:02:06 +01:00
|
|
|
c2s_self_presence/1, in_subscription/2,
|
|
|
|
out_subscription/1, set_items/3, remove_user/2,
|
2016-07-18 14:01:32 +02:00
|
|
|
get_jid_info/4, encode_item/1, webadmin_page/3,
|
2013-03-14 10:33:02 +01:00
|
|
|
webadmin_user/4, get_versioning_feature/2,
|
2019-07-09 13:30:59 +02:00
|
|
|
roster_version/2,
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_opt_type/1, mod_options/1, set_roster/1, del_roster/3,
|
2019-05-15 10:57:55 +02:00
|
|
|
process_rosteritems/5,
|
2019-05-24 14:02:10 +02:00
|
|
|
depends/2, set_item_and_notify_clients/3]).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-18 14:01:32 +02:00
|
|
|
-include("xmpp.hrl").
|
2005-04-17 20:08:34 +02:00
|
|
|
-include("mod_roster.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("ejabberd_http.hrl").
|
|
|
|
-include("ejabberd_web_admin.hrl").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2017-05-17 13:47:35 +02:00
|
|
|
-define(ROSTER_CACHE, roster_cache).
|
|
|
|
-define(ROSTER_ITEM_CACHE, roster_item_cache).
|
|
|
|
-define(ROSTER_VERSION_CACHE, roster_version_cache).
|
2016-12-11 13:03:37 +01:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-type c2s_state() :: ejabberd_c2s:state().
|
2013-03-14 10:33:02 +01:00
|
|
|
-export_type([subscription/0]).
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2016-04-14 09:58:32 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
2016-11-22 14:48:01 +01:00
|
|
|
-callback import(binary(), binary(), #roster{} | [binary()]) -> ok.
|
2017-05-17 13:47:35 +02:00
|
|
|
-callback read_roster_version(binary(), binary()) -> {ok, binary()} | error.
|
2016-04-14 09:58:32 +02:00
|
|
|
-callback write_roster_version(binary(), binary(), boolean(), binary()) -> any().
|
2017-05-17 13:47:35 +02:00
|
|
|
-callback get_roster(binary(), binary()) -> {ok, [#roster{}]} | error.
|
|
|
|
-callback get_roster_item(binary(), binary(), ljid()) -> {ok, #roster{}} | error.
|
|
|
|
-callback read_subscription_and_groups(binary(), binary(), ljid())
|
2018-01-26 13:02:06 +01:00
|
|
|
-> {ok, {subscription(), ask(), [binary()]}} | error.
|
2016-04-14 09:58:32 +02:00
|
|
|
-callback roster_subscribe(binary(), binary(), ljid(), #roster{}) -> any().
|
2019-07-09 13:30:59 +02:00
|
|
|
-callback transaction(binary(), fun(() -> T)) -> {atomic, T} | {aborted, any()}.
|
2017-05-17 13:47:35 +02:00
|
|
|
-callback remove_user(binary(), binary()) -> any().
|
2016-04-14 09:58:32 +02:00
|
|
|
-callback update_roster(binary(), binary(), ljid(), #roster{}) -> any().
|
|
|
|
-callback del_roster(binary(), binary(), ljid()) -> any().
|
2017-05-17 13:47:35 +02:00
|
|
|
-callback use_cache(binary(), roster | roster_version) -> boolean().
|
|
|
|
-callback cache_nodes(binary()) -> [node()].
|
|
|
|
|
|
|
|
-optional_callbacks([use_cache/2, cache_nodes/1]).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Mod = gen_mod:db_mod(Opts, ?MODULE),
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod:init(Host, Opts),
|
2017-05-17 13:47:35 +02:00
|
|
|
init_cache(Mod, Host, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:add(roster_get, Host, ?MODULE,
|
|
|
|
get_user_roster, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_in_subscription, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, in_subscription, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_out_subscription, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, out_subscription, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:add(roster_get_jid_info, Host, ?MODULE,
|
|
|
|
get_jid_info, 50),
|
|
|
|
ejabberd_hooks:add(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_self_presence, Host, ?MODULE,
|
|
|
|
c2s_self_presence, 50),
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:add(c2s_post_auth_features, Host,
|
2009-08-06 23:06:16 +02:00
|
|
|
?MODULE, get_versioning_feature, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:add(webadmin_page_host, Host, ?MODULE,
|
|
|
|
webadmin_page, 50),
|
|
|
|
ejabberd_hooks:add(webadmin_user, Host, ?MODULE,
|
|
|
|
webadmin_user, 50),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
2018-02-11 10:54:15 +01:00
|
|
|
?NS_ROSTER, ?MODULE, process_iq).
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(roster_get, Host, ?MODULE,
|
|
|
|
get_user_roster, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_in_subscription, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, in_subscription, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_out_subscription, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, out_subscription, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_get_jid_info, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_jid_info, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_self_presence, Host, ?MODULE,
|
|
|
|
c2s_self_presence, 50),
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:delete(c2s_post_auth_features,
|
2013-03-14 10:33:02 +01:00
|
|
|
Host, ?MODULE, get_versioning_feature, 50),
|
|
|
|
ejabberd_hooks:delete(webadmin_page_host, Host, ?MODULE,
|
|
|
|
webadmin_page, 50),
|
|
|
|
ejabberd_hooks:delete(webadmin_user, Host, ?MODULE,
|
|
|
|
webadmin_user, 50),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_ROSTER).
|
2004-07-10 00:34:26 +02:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(Host, NewOpts, OldOpts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
NewMod = gen_mod:db_mod(NewOpts, ?MODULE),
|
|
|
|
OldMod = gen_mod:db_mod(OldOpts, ?MODULE),
|
2017-02-22 17:46:47 +01:00
|
|
|
if NewMod /= OldMod ->
|
|
|
|
NewMod:init(Host, NewOpts);
|
|
|
|
true ->
|
|
|
|
ok
|
2019-04-02 19:52:07 +02:00
|
|
|
end,
|
|
|
|
init_cache(NewMod, Host, NewOpts).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec process_iq(iq()) -> iq().
|
2016-11-07 08:10:57 +01:00
|
|
|
process_iq(#iq{from = #jid{luser = U, lserver = S},
|
|
|
|
to = #jid{luser = U, lserver = S}} = IQ) ->
|
|
|
|
process_local_iq(IQ);
|
2016-11-12 11:27:15 +01:00
|
|
|
process_iq(#iq{lang = Lang, to = To} = IQ) ->
|
|
|
|
case ejabberd_hooks:run_fold(roster_remote_access,
|
|
|
|
To#jid.lserver, false, [IQ]) of
|
|
|
|
false ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Query to another users is forbidden"),
|
2016-11-12 11:27:15 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang));
|
|
|
|
true ->
|
|
|
|
process_local_iq(IQ)
|
2002-12-11 21:57:45 +01:00
|
|
|
end.
|
2016-11-07 08:10:57 +01:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec process_local_iq(iq()) -> iq().
|
2016-11-07 08:10:57 +01:00
|
|
|
process_local_iq(#iq{type = set,lang = Lang,
|
|
|
|
sub_els = [#roster_query{
|
|
|
|
items = [#roster_item{ask = Ask}]}]} = IQ)
|
|
|
|
when Ask /= undefined ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Possessing 'ask' attribute is not allowed by RFC6121"),
|
2016-11-07 08:10:57 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
|
|
|
|
process_local_iq(#iq{type = set, from = From, lang = Lang,
|
|
|
|
sub_els = [#roster_query{
|
|
|
|
items = [#roster_item{} = Item]}]} = IQ) ->
|
|
|
|
case has_duplicated_groups(Item#roster_item.groups) of
|
|
|
|
true ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Duplicated groups are not allowed by RFC6121"),
|
2016-11-07 08:10:57 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
|
|
|
|
false ->
|
2018-04-13 00:12:07 +02:00
|
|
|
#jid{lserver = LServer} = From,
|
2019-06-14 11:33:26 +02:00
|
|
|
Access = mod_roster_opt:access(LServer),
|
2018-04-13 00:12:07 +02:00
|
|
|
case acl:match_rule(LServer, Access, From) of
|
2016-11-07 08:10:57 +01:00
|
|
|
deny ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Access denied by service policy"),
|
2016-11-07 08:10:57 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_not_allowed(Txt, Lang));
|
|
|
|
allow ->
|
|
|
|
process_iq_set(IQ)
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
process_local_iq(#iq{type = set, lang = Lang,
|
|
|
|
sub_els = [#roster_query{items = [_|_]}]} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Multiple <item/> elements are not allowed by RFC6121"),
|
2016-11-07 08:10:57 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
|
|
|
|
process_local_iq(#iq{type = get, lang = Lang,
|
|
|
|
sub_els = [#roster_query{items = Items}]} = IQ) ->
|
|
|
|
case Items of
|
|
|
|
[] ->
|
|
|
|
process_iq_get(IQ);
|
|
|
|
[_|_] ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("The query must not contain <item/> elements"),
|
2016-11-07 08:10:57 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang))
|
|
|
|
end;
|
|
|
|
process_local_iq(#iq{lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2016-11-07 08:10:57 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2003-07-21 22:01:22 +02:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec roster_hash([#roster{}]) -> binary().
|
Support for roster versioning (EJAB-964)
Introduces two options for mod_roster and mod_roster_odbc:
- {versioning, true | false} Enable or disable roster versioning on ejabberd.
- {store_current_id, true | false} If true, the current roster version is stored on DB (internal or odbc). Otherwise it is calculated on the fly each time.
Performance:
Setting store_current_id to true should help in reducing the load for both ejabberd and the DB.
Details:
If store_current_id is false, the roster version is a hash of the entire roster. If store_current_id is true, the roster version is a hash, but of the current time
(this has to do with transactional semantics; we need to perform both the roster update and the version update on the same transaction, but we don't
have the entire roster when we are changing a single item on DB. Loading it there requires significant changes to be introduced, so I opted for this simpler approach).
In either case, there is no difference for the clients, the roster version ID is opaque.
IMPORTANT:
mod_shared_roster is not compatible with the option 'store_current_id'. Shared roster and roster versioning can be both enabled, but store_current_id MUST be set to false.
SVN Revision: 2428
2009-08-06 17:45:13 +02:00
|
|
|
roster_hash(Items) ->
|
2017-03-14 00:31:51 +01:00
|
|
|
str:sha(term_to_binary(lists:sort([R#roster{groups =
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:sort(Grs)}
|
|
|
|
|| R = #roster{groups = Grs}
|
|
|
|
<- Items]))).
|
|
|
|
|
2009-08-06 23:06:16 +02:00
|
|
|
%% Returns a list that may contain an xmlelement with the XEP-237 feature if it's enabled.
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec get_versioning_feature([xmpp_element()], binary()) -> [xmpp_element()].
|
2009-08-06 23:06:16 +02:00
|
|
|
get_versioning_feature(Acc, Host) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
case gen_mod:is_loaded(Host, ?MODULE) of
|
|
|
|
true ->
|
2019-07-09 13:30:59 +02:00
|
|
|
case mod_roster_opt:versioning(Host) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-07-18 14:01:32 +02:00
|
|
|
[#rosterver_feature{}|Acc];
|
2017-01-09 15:02:17 +01:00
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Acc
|
2009-08-06 23:06:16 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec roster_version(binary(), binary()) -> undefined | binary().
|
2012-04-27 11:52:05 +02:00
|
|
|
roster_version(LServer, LUser) ->
|
|
|
|
US = {LUser, LServer},
|
2019-07-09 13:30:59 +02:00
|
|
|
case mod_roster_opt:store_current_id(LServer) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
case read_roster_version(LUser, LServer) of
|
2018-12-10 21:38:14 +01:00
|
|
|
error -> undefined;
|
2017-05-17 13:47:35 +02:00
|
|
|
{ok, V} -> V
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
roster_hash(ejabberd_hooks:run_fold(roster_get, LServer,
|
|
|
|
[], [US]))
|
2012-04-27 11:52:05 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec read_roster_version(binary(), binary()) -> {ok, binary()} | error.
|
2012-04-27 11:52:05 +02:00
|
|
|
read_roster_version(LUser, LServer) ->
|
2017-05-17 13:47:35 +02:00
|
|
|
ets_cache:lookup(
|
|
|
|
?ROSTER_VERSION_CACHE, {LUser, LServer},
|
|
|
|
fun() ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:read_roster_version(LUser, LServer)
|
|
|
|
end).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec write_roster_version(binary(), binary()) -> binary().
|
2012-04-27 11:52:05 +02:00
|
|
|
write_roster_version(LUser, LServer) ->
|
|
|
|
write_roster_version(LUser, LServer, false).
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec write_roster_version_t(binary(), binary()) -> binary().
|
2012-04-27 11:52:05 +02:00
|
|
|
write_roster_version_t(LUser, LServer) ->
|
|
|
|
write_roster_version(LUser, LServer, true).
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec write_roster_version(binary(), binary(), boolean()) -> binary().
|
2012-04-27 11:52:05 +02:00
|
|
|
write_roster_version(LUser, LServer, InTransaction) ->
|
2019-02-27 09:56:20 +01:00
|
|
|
Ver = str:sha(term_to_binary(erlang:unique_integer())),
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:write_roster_version(LUser, LServer, InTransaction, Ver),
|
2017-05-17 13:47:35 +02:00
|
|
|
if InTransaction -> ok;
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?ROSTER_VERSION_CACHE, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer))
|
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
Ver.
|
|
|
|
|
2018-01-27 17:35:38 +01:00
|
|
|
%% Load roster from DB only if necessary.
|
|
|
|
%% It is necessary if
|
Support for roster versioning (EJAB-964)
Introduces two options for mod_roster and mod_roster_odbc:
- {versioning, true | false} Enable or disable roster versioning on ejabberd.
- {store_current_id, true | false} If true, the current roster version is stored on DB (internal or odbc). Otherwise it is calculated on the fly each time.
Performance:
Setting store_current_id to true should help in reducing the load for both ejabberd and the DB.
Details:
If store_current_id is false, the roster version is a hash of the entire roster. If store_current_id is true, the roster version is a hash, but of the current time
(this has to do with transactional semantics; we need to perform both the roster update and the version update on the same transaction, but we don't
have the entire roster when we are changing a single item on DB. Loading it there requires significant changes to be introduced, so I opted for this simpler approach).
In either case, there is no difference for the clients, the roster version ID is opaque.
IMPORTANT:
mod_shared_roster is not compatible with the option 'store_current_id'. Shared roster and roster versioning can be both enabled, but store_current_id MUST be set to false.
SVN Revision: 2428
2009-08-06 17:45:13 +02:00
|
|
|
%% - roster versioning is disabled in server OR
|
|
|
|
%% - roster versioning is not used by the client OR
|
|
|
|
%% - roster versioning is used by server and client, BUT the server isn't storing versions on db OR
|
|
|
|
%% - the roster version from client don't match current version.
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec process_iq_get(iq()) -> iq().
|
2019-07-07 21:12:14 +02:00
|
|
|
process_iq_get(#iq{to = To,
|
2016-07-18 14:01:32 +02:00
|
|
|
sub_els = [#roster_query{ver = RequestedVersion}]} = IQ) ->
|
2016-11-12 11:27:15 +01:00
|
|
|
LUser = To#jid.luser,
|
|
|
|
LServer = To#jid.lserver,
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2019-07-07 21:12:14 +02:00
|
|
|
{ItemsToSend, VersionToSend} =
|
2019-07-09 13:30:59 +02:00
|
|
|
case {mod_roster_opt:versioning(LServer),
|
|
|
|
mod_roster_opt:store_current_id(LServer)} of
|
2019-07-07 21:12:14 +02:00
|
|
|
{true, true} when RequestedVersion /= undefined ->
|
|
|
|
case read_roster_version(LUser, LServer) of
|
|
|
|
error ->
|
|
|
|
RosterVersion = write_roster_version(LUser, LServer),
|
|
|
|
{lists:map(fun encode_item/1,
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
roster_get, To#jid.lserver, [], [US])),
|
|
|
|
RosterVersion};
|
|
|
|
{ok, RequestedVersion} ->
|
|
|
|
{false, false};
|
|
|
|
{ok, NewVersion} ->
|
|
|
|
{lists:map(fun encode_item/1,
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
roster_get, To#jid.lserver, [], [US])),
|
|
|
|
NewVersion}
|
|
|
|
end;
|
|
|
|
{true, false} when RequestedVersion /= undefined ->
|
|
|
|
RosterItems = ejabberd_hooks:run_fold(
|
|
|
|
roster_get, To#jid.lserver, [], [US]),
|
|
|
|
case roster_hash(RosterItems) of
|
|
|
|
RequestedVersion ->
|
|
|
|
{false, false};
|
|
|
|
New ->
|
|
|
|
{lists:map(fun encode_item/1, RosterItems), New}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{lists:map(fun encode_item/1,
|
|
|
|
ejabberd_hooks:run_fold(
|
|
|
|
roster_get, To#jid.lserver, [], [US])),
|
|
|
|
false}
|
|
|
|
end,
|
|
|
|
xmpp:make_iq_result(
|
|
|
|
IQ,
|
|
|
|
case {ItemsToSend, VersionToSend} of
|
|
|
|
{false, false} ->
|
|
|
|
undefined;
|
|
|
|
{Items, false} ->
|
|
|
|
#roster_query{items = Items};
|
|
|
|
{Items, Version} ->
|
|
|
|
#roster_query{items = Items,
|
|
|
|
ver = Version}
|
|
|
|
end).
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec get_user_roster([#roster{}], {binary(), binary()}) -> [#roster{}].
|
2012-04-27 11:52:05 +02:00
|
|
|
get_user_roster(Acc, {LUser, LServer}) ->
|
|
|
|
Items = get_roster(LUser, LServer),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:filter(fun (#roster{subscription = none,
|
|
|
|
ask = in}) ->
|
|
|
|
false;
|
|
|
|
(_) -> true
|
|
|
|
end,
|
|
|
|
Items)
|
|
|
|
++ Acc.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec get_roster(binary(), binary()) -> [#roster{}].
|
2012-04-27 11:52:05 +02:00
|
|
|
get_roster(LUser, LServer) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2017-05-17 13:47:35 +02:00
|
|
|
R = case use_cache(Mod, LServer, roster) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?ROSTER_CACHE, {LUser, LServer},
|
|
|
|
fun() -> Mod:get_roster(LUser, LServer) end);
|
|
|
|
false ->
|
|
|
|
Mod:get_roster(LUser, LServer)
|
|
|
|
end,
|
|
|
|
case R of
|
|
|
|
{ok, Items} -> Items;
|
|
|
|
error -> []
|
|
|
|
end.
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec get_roster_item(binary(), binary(), ljid()) -> #roster{}.
|
2017-05-17 13:47:35 +02:00
|
|
|
get_roster_item(LUser, LServer, LJID) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
case Mod:get_roster_item(LUser, LServer, LJID) of
|
|
|
|
{ok, Item} ->
|
|
|
|
Item;
|
|
|
|
error ->
|
2017-05-20 21:36:32 +02:00
|
|
|
LBJID = jid:remove_resource(LJID),
|
|
|
|
#roster{usj = {LUser, LServer, LBJID},
|
|
|
|
us = {LUser, LServer}, jid = LBJID}
|
2017-05-17 13:47:35 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec get_subscription_and_groups(binary(), binary(), ljid()) ->
|
|
|
|
{subscription(), ask(), [binary()]}.
|
2017-05-17 13:47:35 +02:00
|
|
|
get_subscription_and_groups(LUser, LServer, LJID) ->
|
2017-05-20 21:36:32 +02:00
|
|
|
LBJID = jid:remove_resource(LJID),
|
2017-05-17 13:47:35 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Res = case use_cache(Mod, LServer, roster) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?ROSTER_ITEM_CACHE, {LUser, LServer, LBJID},
|
|
|
|
fun() ->
|
|
|
|
Items = get_roster(LUser, LServer),
|
|
|
|
case lists:keyfind(LBJID, #roster.jid, Items) of
|
2018-01-26 13:02:06 +01:00
|
|
|
#roster{subscription = Sub,
|
|
|
|
ask = Ask,
|
|
|
|
groups = Groups} ->
|
|
|
|
{ok, {Sub, Ask, Groups}};
|
2017-05-17 13:47:35 +02:00
|
|
|
false ->
|
2017-05-20 21:36:32 +02:00
|
|
|
error
|
2017-05-17 13:47:35 +02:00
|
|
|
end
|
|
|
|
end);
|
|
|
|
false ->
|
2018-01-26 13:02:06 +01:00
|
|
|
case Mod:read_subscription_and_groups(LUser, LServer, LBJID) of
|
|
|
|
{ok, {Sub, Groups}} ->
|
|
|
|
%% Backward compatibility for third-party backends
|
|
|
|
{ok, {Sub, none, Groups}};
|
|
|
|
Other ->
|
|
|
|
Other
|
|
|
|
end
|
2017-05-17 13:47:35 +02:00
|
|
|
end,
|
|
|
|
case Res of
|
|
|
|
{ok, SubAndGroups} ->
|
|
|
|
SubAndGroups;
|
|
|
|
error ->
|
2018-01-26 13:02:06 +01:00
|
|
|
{none, none, []}
|
2017-05-17 13:47:35 +02:00
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec set_roster(#roster{}) -> {atomic | aborted, any()}.
|
2016-01-28 12:23:51 +01:00
|
|
|
set_roster(#roster{us = {LUser, LServer}, jid = LJID} = Item) ->
|
|
|
|
transaction(
|
2017-05-17 13:47:35 +02:00
|
|
|
LUser, LServer, [LJID],
|
2016-01-28 12:23:51 +01:00
|
|
|
fun() ->
|
2016-11-18 11:38:08 +01:00
|
|
|
update_roster_t(LUser, LServer, LJID, Item)
|
2016-01-28 12:23:51 +01:00
|
|
|
end).
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec del_roster(binary(), binary(), ljid()) -> {atomic | aborted, any()}.
|
2016-10-22 12:01:45 +02:00
|
|
|
del_roster(LUser, LServer, LJID) ->
|
|
|
|
transaction(
|
2017-05-17 13:47:35 +02:00
|
|
|
LUser, LServer, [LJID],
|
2016-10-22 12:01:45 +02:00
|
|
|
fun() ->
|
|
|
|
del_roster_t(LUser, LServer, LJID)
|
|
|
|
end).
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec encode_item(#roster{}) -> roster_item().
|
2016-07-18 14:01:32 +02:00
|
|
|
encode_item(Item) ->
|
|
|
|
#roster_item{jid = jid:make(Item#roster.jid),
|
|
|
|
name = Item#roster.name,
|
|
|
|
subscription = Item#roster.subscription,
|
|
|
|
ask = case ask_to_pending(Item#roster.ask) of
|
|
|
|
out -> subscribe;
|
|
|
|
both -> subscribe;
|
|
|
|
_ -> undefined
|
|
|
|
end,
|
|
|
|
groups = Item#roster.groups}.
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec decode_item(roster_item(), #roster{}, boolean()) -> #roster{}.
|
2016-11-07 08:10:57 +01:00
|
|
|
decode_item(#roster_item{subscription = remove} = Item, R, _) ->
|
|
|
|
R#roster{jid = jid:tolower(Item#roster_item.jid),
|
|
|
|
name = <<"">>,
|
|
|
|
subscription = remove,
|
|
|
|
ask = none,
|
|
|
|
groups = [],
|
|
|
|
askmessage = <<"">>,
|
|
|
|
xs = []};
|
2016-09-13 11:30:05 +02:00
|
|
|
decode_item(Item, R, Managed) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
R#roster{jid = jid:tolower(Item#roster_item.jid),
|
|
|
|
name = Item#roster_item.name,
|
|
|
|
subscription = case Item#roster_item.subscription of
|
|
|
|
Sub when Managed -> Sub;
|
2016-09-13 11:30:05 +02:00
|
|
|
_ -> R#roster.subscription
|
2016-07-18 14:01:32 +02:00
|
|
|
end,
|
|
|
|
groups = Item#roster_item.groups}.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec process_iq_set(iq()) -> iq().
|
|
|
|
process_iq_set(#iq{from = _From, to = To, lang = Lang,
|
2017-05-17 13:47:35 +02:00
|
|
|
sub_els = [#roster_query{items = [QueryItem]}]} = IQ) ->
|
2019-05-24 14:02:10 +02:00
|
|
|
case set_item_and_notify_clients(To, QueryItem, false) of
|
|
|
|
ok ->
|
|
|
|
xmpp:make_iq_result(IQ);
|
2019-07-09 13:30:59 +02:00
|
|
|
{error, _} ->
|
|
|
|
Txt = ?T("Database failure"),
|
|
|
|
Err = xmpp:err_internal_server_error(Txt, Lang),
|
|
|
|
xmpp:make_error(IQ, Err)
|
2019-05-24 14:02:10 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec set_item_and_notify_clients(jid(), #roster_item{}, boolean()) -> ok | {error, any()}.
|
2019-05-24 15:18:15 +02:00
|
|
|
set_item_and_notify_clients(To, #roster_item{jid = PeerJID} = RosterItem,
|
2019-05-24 14:02:10 +02:00
|
|
|
OverrideSubscription) ->
|
2018-03-19 10:32:43 +01:00
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
2019-05-24 15:18:15 +02:00
|
|
|
PeerLJID = jid:tolower(PeerJID),
|
2016-07-18 14:01:32 +02:00
|
|
|
F = fun () ->
|
2019-07-09 13:30:59 +02:00
|
|
|
Item1 = get_roster_item(LUser, LServer, PeerLJID),
|
|
|
|
Item2 = decode_item(RosterItem, Item1, OverrideSubscription),
|
|
|
|
Item3 = ejabberd_hooks:run_fold(roster_process_item,
|
|
|
|
LServer, Item2,
|
|
|
|
[LServer]),
|
|
|
|
case Item3#roster.subscription of
|
|
|
|
remove -> del_roster_t(LUser, LServer, PeerLJID);
|
|
|
|
_ -> update_roster_t(LUser, LServer, PeerLJID, Item3)
|
|
|
|
end,
|
|
|
|
case mod_roster_opt:store_current_id(LServer) of
|
|
|
|
true -> write_roster_version_t(LUser, LServer);
|
|
|
|
false -> ok
|
|
|
|
end,
|
|
|
|
{Item1, Item3}
|
2016-07-18 14:01:32 +02:00
|
|
|
end,
|
2019-05-24 15:18:15 +02:00
|
|
|
case transaction(LUser, LServer, [PeerLJID], F) of
|
2019-07-09 13:30:59 +02:00
|
|
|
{atomic, {OldItem, NewItem}} ->
|
|
|
|
push_item(To, OldItem, NewItem),
|
|
|
|
case NewItem#roster.subscription of
|
2017-05-17 13:47:35 +02:00
|
|
|
remove ->
|
|
|
|
send_unsubscribing_presence(To, OldItem);
|
|
|
|
_ ->
|
|
|
|
ok
|
2019-07-09 13:30:59 +02:00
|
|
|
end;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
{error, Reason}
|
2016-09-13 11:30:05 +02:00
|
|
|
end.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec push_item(jid(), #roster{}, #roster{}) -> ok.
|
2018-03-19 10:32:43 +01:00
|
|
|
push_item(To, OldItem, NewItem) ->
|
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
2019-07-09 13:30:59 +02:00
|
|
|
Ver = case mod_roster_opt:versioning(LServer) of
|
2018-03-19 10:32:43 +01:00
|
|
|
true -> roster_version(LServer, LUser);
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> undefined
|
2018-03-19 10:32:43 +01:00
|
|
|
end,
|
2017-05-17 13:47:35 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Resource) ->
|
2018-03-19 10:32:43 +01:00
|
|
|
To1 = jid:replace_resource(To, Resource),
|
|
|
|
push_item(To1, OldItem, NewItem, Ver)
|
|
|
|
end, ejabberd_sm:get_user_resources(LUser, LServer)).
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec push_item(jid(), #roster{}, #roster{}, undefined | binary()) -> ok.
|
2018-03-19 10:32:43 +01:00
|
|
|
push_item(To, OldItem, NewItem, Ver) ->
|
|
|
|
route_presence_change(To, OldItem, NewItem),
|
|
|
|
IQ = #iq{type = set, to = To,
|
|
|
|
from = jid:remove_resource(To),
|
2018-07-05 10:51:49 +02:00
|
|
|
id = <<"push", (p1_rand:get_string())/binary>>,
|
2018-03-19 10:32:43 +01:00
|
|
|
sub_els = [#roster_query{ver = Ver,
|
|
|
|
items = [encode_item(NewItem)]}]},
|
|
|
|
ejabberd_router:route(IQ).
|
2017-05-17 13:47:35 +02:00
|
|
|
|
|
|
|
-spec route_presence_change(jid(), #roster{}, #roster{}) -> ok.
|
|
|
|
route_presence_change(From, OldItem, NewItem) ->
|
|
|
|
OldSub = OldItem#roster.subscription,
|
|
|
|
NewSub = NewItem#roster.subscription,
|
|
|
|
To = jid:make(NewItem#roster.jid),
|
|
|
|
NewIsFrom = NewSub == both orelse NewSub == from,
|
|
|
|
OldIsFrom = OldSub == both orelse OldSub == from,
|
|
|
|
if NewIsFrom andalso not OldIsFrom ->
|
|
|
|
case ejabberd_sm:get_session_pid(
|
|
|
|
From#jid.luser, From#jid.lserver, From#jid.lresource) of
|
|
|
|
none ->
|
|
|
|
ok;
|
|
|
|
Pid ->
|
|
|
|
ejabberd_c2s:resend_presence(Pid, To)
|
|
|
|
end;
|
|
|
|
OldIsFrom andalso not NewIsFrom ->
|
|
|
|
PU = #presence{from = From, to = To, type = unavailable},
|
|
|
|
case ejabberd_hooks:run_fold(
|
|
|
|
privacy_check_packet, allow,
|
|
|
|
[From, PU, out]) of
|
|
|
|
deny ->
|
|
|
|
ok;
|
|
|
|
allow ->
|
|
|
|
ejabberd_router:route(PU)
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
ok
|
2016-12-11 13:03:37 +01:00
|
|
|
end.
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec ask_to_pending(ask()) -> none | in | out | both.
|
2003-12-11 21:31:40 +01:00
|
|
|
ask_to_pending(subscribe) -> out;
|
|
|
|
ask_to_pending(unsubscribe) -> none;
|
|
|
|
ask_to_pending(Ask) -> Ask.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec roster_subscribe_t(binary(), binary(), ljid(), #roster{}) -> any().
|
2012-04-27 11:52:05 +02:00
|
|
|
roster_subscribe_t(LUser, LServer, LJID, Item) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:roster_subscribe(LUser, LServer, LJID, Item).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec transaction(binary(), binary(), [ljid()], fun(() -> T)) -> {atomic, T} | {aborted, any()}.
|
2017-05-17 13:47:35 +02:00
|
|
|
transaction(LUser, LServer, LJIDs, F) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2017-05-17 13:47:35 +02:00
|
|
|
case Mod:transaction(LServer, F) of
|
|
|
|
{atomic, _} = Result ->
|
|
|
|
delete_cache(Mod, LUser, LServer, LJIDs),
|
|
|
|
Result;
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
-spec in_subscription(boolean(), presence()) -> boolean().
|
|
|
|
in_subscription(_, #presence{from = JID, to = To,
|
|
|
|
type = Type, status = Status}) ->
|
|
|
|
#jid{user = User, server = Server} = To,
|
|
|
|
Reason = if Type == subscribe -> xmpp:get_text(Status);
|
|
|
|
true -> <<"">>
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
process_subscription(in, User, Server, JID, Type,
|
|
|
|
Reason).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
-spec out_subscription(presence()) -> boolean().
|
|
|
|
out_subscription(#presence{from = From, to = JID, type = Type}) ->
|
|
|
|
#jid{user = User, server = Server} = From,
|
2013-03-14 10:33:02 +01:00
|
|
|
process_subscription(out, User, Server, JID, Type, <<"">>).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec process_subscription(in | out, binary(), binary(), jid(),
|
|
|
|
subscribe | subscribed | unsubscribe | unsubscribed,
|
|
|
|
binary()) -> boolean().
|
2013-03-14 10:33:02 +01:00
|
|
|
process_subscription(Direction, User, Server, JID1,
|
|
|
|
Type, Reason) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2017-12-27 16:14:03 +01:00
|
|
|
LJID = jid:tolower(jid:remove_resource(JID1)),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2017-05-17 13:47:35 +02:00
|
|
|
Item = get_roster_item(LUser, LServer, LJID),
|
2003-12-11 21:31:40 +01:00
|
|
|
NewState = case Direction of
|
2013-03-14 10:33:02 +01:00
|
|
|
out ->
|
|
|
|
out_state_change(Item#roster.subscription,
|
|
|
|
Item#roster.ask, Type);
|
|
|
|
in ->
|
|
|
|
in_state_change(Item#roster.subscription,
|
|
|
|
Item#roster.ask, Type)
|
2003-12-11 21:31:40 +01:00
|
|
|
end,
|
2004-01-18 21:42:09 +01:00
|
|
|
AutoReply = case Direction of
|
2013-03-14 10:33:02 +01:00
|
|
|
out -> none;
|
|
|
|
in ->
|
|
|
|
in_auto_reply(Item#roster.subscription,
|
|
|
|
Item#roster.ask, Type)
|
2004-01-18 21:42:09 +01:00
|
|
|
end,
|
2006-05-23 22:19:37 +02:00
|
|
|
AskMessage = case NewState of
|
2013-03-14 10:33:02 +01:00
|
|
|
{_, both} -> Reason;
|
|
|
|
{_, in} -> Reason;
|
|
|
|
_ -> <<"">>
|
2006-05-23 22:19:37 +02:00
|
|
|
end,
|
2003-12-11 21:31:40 +01:00
|
|
|
case NewState of
|
2017-05-17 13:47:35 +02:00
|
|
|
none ->
|
|
|
|
{none, AutoReply};
|
|
|
|
{none, none} when Item#roster.subscription == none,
|
|
|
|
Item#roster.ask == in ->
|
|
|
|
del_roster_t(LUser, LServer, LJID), {none, AutoReply};
|
|
|
|
{Subscription, Pending} ->
|
|
|
|
NewItem = Item#roster{subscription = Subscription,
|
|
|
|
ask = Pending,
|
|
|
|
askmessage = AskMessage},
|
|
|
|
roster_subscribe_t(LUser, LServer, LJID, NewItem),
|
2019-07-09 13:30:59 +02:00
|
|
|
case mod_roster_opt:store_current_id(LServer) of
|
2017-05-17 13:47:35 +02:00
|
|
|
true -> write_roster_version_t(LUser, LServer);
|
|
|
|
false -> ok
|
|
|
|
end,
|
|
|
|
{{push, Item, NewItem}, AutoReply}
|
2002-12-20 21:42:08 +01:00
|
|
|
end
|
|
|
|
end,
|
2017-05-17 13:47:35 +02:00
|
|
|
case transaction(LUser, LServer, [LJID], F) of
|
|
|
|
{atomic, {Push, AutoReply}} ->
|
|
|
|
case AutoReply of
|
|
|
|
none -> ok;
|
|
|
|
_ ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
#presence{type = AutoReply,
|
|
|
|
from = jid:make(User, Server),
|
|
|
|
to = JID1})
|
|
|
|
end,
|
|
|
|
case Push of
|
|
|
|
{push, OldItem, NewItem} ->
|
|
|
|
if NewItem#roster.subscription == none,
|
|
|
|
NewItem#roster.ask == in ->
|
|
|
|
ok;
|
|
|
|
true ->
|
2018-03-19 10:32:43 +01:00
|
|
|
push_item(jid:make(User, Server), OldItem, NewItem)
|
2017-05-17 13:47:35 +02:00
|
|
|
end,
|
|
|
|
true;
|
|
|
|
none ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
2002-12-18 21:26:08 +01:00
|
|
|
end.
|
|
|
|
|
2003-12-11 21:31:40 +01:00
|
|
|
%% in_state_change(Subscription, Pending, Type) -> NewState
|
|
|
|
%% NewState = none | {NewSubscription, NewPending}
|
2005-12-11 20:48:31 +01:00
|
|
|
-ifdef(ROSTER_GATEWAY_WORKAROUND).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-12-11 20:48:31 +01:00
|
|
|
-define(NNSD, {to, none}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-12-11 20:48:31 +01:00
|
|
|
-define(NISD, {to, in}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-12-11 20:48:31 +01:00
|
|
|
-else.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-12-11 20:48:31 +01:00
|
|
|
-define(NNSD, none).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-12-11 20:48:31 +01:00
|
|
|
-define(NISD, none).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-12-11 20:48:31 +01:00
|
|
|
-endif.
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
in_state_change(none, none, subscribe) -> {none, in};
|
|
|
|
in_state_change(none, none, subscribed) -> ?NNSD;
|
|
|
|
in_state_change(none, none, unsubscribe) -> none;
|
2003-12-11 21:31:40 +01:00
|
|
|
in_state_change(none, none, unsubscribed) -> none;
|
2013-03-14 10:33:02 +01:00
|
|
|
in_state_change(none, out, subscribe) -> {none, both};
|
|
|
|
in_state_change(none, out, subscribed) -> {to, none};
|
|
|
|
in_state_change(none, out, unsubscribe) -> none;
|
|
|
|
in_state_change(none, out, unsubscribed) ->
|
|
|
|
{none, none};
|
|
|
|
in_state_change(none, in, subscribe) -> none;
|
|
|
|
in_state_change(none, in, subscribed) -> ?NISD;
|
|
|
|
in_state_change(none, in, unsubscribe) -> {none, none};
|
|
|
|
in_state_change(none, in, unsubscribed) -> none;
|
|
|
|
in_state_change(none, both, subscribe) -> none;
|
|
|
|
in_state_change(none, both, subscribed) -> {to, in};
|
|
|
|
in_state_change(none, both, unsubscribe) -> {none, out};
|
2003-12-11 21:31:40 +01:00
|
|
|
in_state_change(none, both, unsubscribed) -> {none, in};
|
2013-03-14 10:33:02 +01:00
|
|
|
in_state_change(to, none, subscribe) -> {to, in};
|
|
|
|
in_state_change(to, none, subscribed) -> none;
|
|
|
|
in_state_change(to, none, unsubscribe) -> none;
|
|
|
|
in_state_change(to, none, unsubscribed) -> {none, none};
|
|
|
|
in_state_change(to, in, subscribe) -> none;
|
|
|
|
in_state_change(to, in, subscribed) -> none;
|
|
|
|
in_state_change(to, in, unsubscribe) -> {to, none};
|
|
|
|
in_state_change(to, in, unsubscribed) -> {none, in};
|
|
|
|
in_state_change(from, none, subscribe) -> none;
|
|
|
|
in_state_change(from, none, subscribed) -> {both, none};
|
|
|
|
in_state_change(from, none, unsubscribe) ->
|
|
|
|
{none, none};
|
2003-12-11 21:31:40 +01:00
|
|
|
in_state_change(from, none, unsubscribed) -> none;
|
2013-03-14 10:33:02 +01:00
|
|
|
in_state_change(from, out, subscribe) -> none;
|
|
|
|
in_state_change(from, out, subscribed) -> {both, none};
|
|
|
|
in_state_change(from, out, unsubscribe) -> {none, out};
|
|
|
|
in_state_change(from, out, unsubscribed) ->
|
|
|
|
{from, none};
|
|
|
|
in_state_change(both, none, subscribe) -> none;
|
|
|
|
in_state_change(both, none, subscribed) -> none;
|
|
|
|
in_state_change(both, none, unsubscribe) -> {to, none};
|
|
|
|
in_state_change(both, none, unsubscribed) ->
|
|
|
|
{from, none}.
|
|
|
|
|
|
|
|
out_state_change(none, none, subscribe) -> {none, out};
|
|
|
|
out_state_change(none, none, subscribed) -> none;
|
|
|
|
out_state_change(none, none, unsubscribe) -> none;
|
2003-12-11 21:31:40 +01:00
|
|
|
out_state_change(none, none, unsubscribed) -> none;
|
2013-03-14 10:33:02 +01:00
|
|
|
out_state_change(none, out, subscribe) ->
|
|
|
|
{none,
|
|
|
|
out}; %% We need to resend query (RFC3921, section 9.2)
|
|
|
|
out_state_change(none, out, subscribed) -> none;
|
|
|
|
out_state_change(none, out, unsubscribe) ->
|
|
|
|
{none, none};
|
|
|
|
out_state_change(none, out, unsubscribed) -> none;
|
|
|
|
out_state_change(none, in, subscribe) -> {none, both};
|
|
|
|
out_state_change(none, in, subscribed) -> {from, none};
|
|
|
|
out_state_change(none, in, unsubscribe) -> none;
|
|
|
|
out_state_change(none, in, unsubscribed) ->
|
|
|
|
{none, none};
|
|
|
|
out_state_change(none, both, subscribe) -> none;
|
|
|
|
out_state_change(none, both, subscribed) -> {from, out};
|
|
|
|
out_state_change(none, both, unsubscribe) -> {none, in};
|
|
|
|
out_state_change(none, both, unsubscribed) ->
|
|
|
|
{none, out};
|
|
|
|
out_state_change(to, none, subscribe) -> none;
|
|
|
|
out_state_change(to, none, subscribed) -> {both, none};
|
|
|
|
out_state_change(to, none, unsubscribe) -> {none, none};
|
|
|
|
out_state_change(to, none, unsubscribed) -> none;
|
|
|
|
out_state_change(to, in, subscribe) -> none;
|
|
|
|
out_state_change(to, in, subscribed) -> {both, none};
|
|
|
|
out_state_change(to, in, unsubscribe) -> {none, in};
|
|
|
|
out_state_change(to, in, unsubscribed) -> {to, none};
|
|
|
|
out_state_change(from, none, subscribe) -> {from, out};
|
|
|
|
out_state_change(from, none, subscribed) -> none;
|
|
|
|
out_state_change(from, none, unsubscribe) -> none;
|
|
|
|
out_state_change(from, none, unsubscribed) ->
|
|
|
|
{none, none};
|
|
|
|
out_state_change(from, out, subscribe) -> none;
|
|
|
|
out_state_change(from, out, subscribed) -> none;
|
|
|
|
out_state_change(from, out, unsubscribe) ->
|
|
|
|
{from, none};
|
|
|
|
out_state_change(from, out, unsubscribed) ->
|
|
|
|
{none, out};
|
|
|
|
out_state_change(both, none, subscribe) -> none;
|
|
|
|
out_state_change(both, none, subscribed) -> none;
|
|
|
|
out_state_change(both, none, unsubscribe) ->
|
|
|
|
{from, none};
|
|
|
|
out_state_change(both, none, unsubscribed) ->
|
|
|
|
{to, none}.
|
|
|
|
|
|
|
|
in_auto_reply(from, none, subscribe) -> subscribed;
|
|
|
|
in_auto_reply(from, out, subscribe) -> subscribed;
|
|
|
|
in_auto_reply(both, none, subscribe) -> subscribed;
|
|
|
|
in_auto_reply(none, in, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(none, both, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(to, in, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(from, none, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(from, out, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(both, none, unsubscribe) -> unsubscribed;
|
|
|
|
in_auto_reply(_, _, _) -> none.
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec remove_user(binary(), binary()) -> ok.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2017-05-17 13:47:35 +02:00
|
|
|
Items = get_user_roster([], {LUser, LServer}),
|
|
|
|
send_unsubscription_to_rosteritems(LUser, LServer, Items),
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2016-08-09 09:56:32 +02:00
|
|
|
Mod:remove_user(LUser, LServer),
|
2017-05-17 13:47:35 +02:00
|
|
|
delete_cache(Mod, LUser, LServer, [Item#roster.jid || Item <- Items]).
|
2003-01-26 21:16:53 +01:00
|
|
|
|
2009-02-16 15:49:20 +01:00
|
|
|
%% For each contact with Subscription:
|
|
|
|
%% Both or From, send a "unsubscribed" presence stanza;
|
|
|
|
%% Both or To, send a "unsubscribe" presence stanza.
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec send_unsubscription_to_rosteritems(binary(), binary(), [#roster{}]) -> ok.
|
2017-05-17 13:47:35 +02:00
|
|
|
send_unsubscription_to_rosteritems(LUser, LServer, RosterItems) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
From = jid:make({LUser, LServer, <<"">>}),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (RosterItem) ->
|
2009-02-16 15:49:20 +01:00
|
|
|
send_unsubscribing_presence(From, RosterItem)
|
|
|
|
end,
|
|
|
|
RosterItems).
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec send_unsubscribing_presence(jid(), #roster{}) -> ok.
|
2009-02-16 15:49:20 +01:00
|
|
|
send_unsubscribing_presence(From, Item) ->
|
|
|
|
IsTo = case Item#roster.subscription of
|
2013-03-14 10:33:02 +01:00
|
|
|
both -> true;
|
|
|
|
to -> true;
|
|
|
|
_ -> false
|
2009-02-16 15:49:20 +01:00
|
|
|
end,
|
|
|
|
IsFrom = case Item#roster.subscription of
|
2013-03-14 10:33:02 +01:00
|
|
|
both -> true;
|
|
|
|
from -> true;
|
|
|
|
_ -> false
|
2009-02-16 15:49:20 +01:00
|
|
|
end,
|
|
|
|
if IsTo ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
#presence{type = unsubscribe,
|
|
|
|
from = jid:remove_resource(From),
|
|
|
|
to = jid:make(Item#roster.jid)});
|
2009-02-16 15:49:20 +01:00
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
if IsFrom ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
#presence{type = unsubscribed,
|
|
|
|
from = jid:remove_resource(From),
|
|
|
|
to = jid:make(Item#roster.jid)});
|
2009-02-16 15:49:20 +01:00
|
|
|
true -> ok
|
2019-07-09 13:30:59 +02:00
|
|
|
end.
|
2009-02-16 15:49:20 +01:00
|
|
|
|
2003-02-02 20:49:19 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec set_items(binary(), binary(), roster_query()) -> {atomic, ok} | {aborted, any()}.
|
2016-08-04 12:37:07 +02:00
|
|
|
set_items(User, Server, #roster_query{items = Items}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2017-05-17 13:47:35 +02:00
|
|
|
LJIDs = [jid:tolower(Item#roster_item.jid) || Item <- Items],
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2017-05-17 13:47:35 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Item) ->
|
|
|
|
process_item_set_t(LUser, LServer, Item)
|
|
|
|
end, Items)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2017-05-17 13:47:35 +02:00
|
|
|
transaction(LUser, LServer, LJIDs, F).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec update_roster_t(binary(), binary(), ljid(), #roster{}) -> any().
|
2012-04-27 11:52:05 +02:00
|
|
|
update_roster_t(LUser, LServer, LJID, Item) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:update_roster(LUser, LServer, LJID, Item).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec del_roster_t(binary(), binary(), ljid()) -> any().
|
2012-04-27 11:52:05 +02:00
|
|
|
del_roster_t(LUser, LServer, LJID) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:del_roster(LUser, LServer, LJID).
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec process_item_set_t(binary(), binary(), roster_item()) -> any().
|
2016-07-18 14:01:32 +02:00
|
|
|
process_item_set_t(LUser, LServer, #roster_item{jid = JID1} = QueryItem) ->
|
2016-11-07 08:10:57 +01:00
|
|
|
JID = {JID1#jid.user, JID1#jid.server, <<>>},
|
|
|
|
LJID = {JID1#jid.luser, JID1#jid.lserver, <<>>},
|
2016-07-18 14:01:32 +02:00
|
|
|
Item = #roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer}, jid = JID},
|
|
|
|
Item2 = decode_item(QueryItem, Item, _Managed = true),
|
|
|
|
case Item2#roster.subscription of
|
|
|
|
remove -> del_roster_t(LUser, LServer, LJID);
|
|
|
|
_ -> update_roster_t(LUser, LServer, LJID, Item2)
|
2003-02-20 18:12:03 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
process_item_set_t(_LUser, _LServer, _) -> ok.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec c2s_self_presence({presence(), c2s_state()}) -> {presence(), c2s_state()}.
|
2017-01-09 15:02:17 +01:00
|
|
|
c2s_self_presence({_, #{pres_last := _}} = Acc) ->
|
|
|
|
Acc;
|
2017-05-17 14:24:32 +02:00
|
|
|
c2s_self_presence({#presence{type = available} = Pkt, State}) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
Prio = get_priority_from_presence(Pkt),
|
|
|
|
if Prio >= 0 ->
|
2017-05-17 13:47:35 +02:00
|
|
|
State1 = resend_pending_subscriptions(State),
|
2017-01-09 15:02:17 +01:00
|
|
|
{Pkt, State1};
|
|
|
|
true ->
|
|
|
|
{Pkt, State}
|
|
|
|
end;
|
|
|
|
c2s_self_presence(Acc) ->
|
|
|
|
Acc.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec resend_pending_subscriptions(c2s_state()) -> c2s_state().
|
2017-05-17 13:47:35 +02:00
|
|
|
resend_pending_subscriptions(#{jid := JID} = State) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
BareJID = jid:remove_resource(JID),
|
2017-05-17 13:47:35 +02:00
|
|
|
Result = get_roster(JID#jid.luser, JID#jid.lserver),
|
2017-01-09 15:02:17 +01:00
|
|
|
lists:foldl(
|
|
|
|
fun(#roster{ask = Ask} = R, AccState) when Ask == in; Ask == both ->
|
2016-07-18 14:01:32 +02:00
|
|
|
Message = R#roster.askmessage,
|
|
|
|
Status = if is_binary(Message) -> (Message);
|
|
|
|
true -> <<"">>
|
|
|
|
end,
|
2017-02-18 07:36:27 +01:00
|
|
|
Sub = #presence{from = jid:make(R#roster.jid),
|
|
|
|
to = BareJID,
|
|
|
|
type = subscribe,
|
2017-01-09 15:02:17 +01:00
|
|
|
status = xmpp:mk_text(Status)},
|
|
|
|
ejabberd_c2s:send(AccState, Sub);
|
|
|
|
(_, AccState) ->
|
|
|
|
AccState
|
|
|
|
end, State, Result).
|
|
|
|
|
|
|
|
-spec get_priority_from_presence(presence()) -> integer().
|
|
|
|
get_priority_from_presence(#presence{priority = Prio}) ->
|
|
|
|
case Prio of
|
|
|
|
undefined -> 0;
|
|
|
|
_ -> Prio
|
|
|
|
end.
|
2006-05-23 22:19:37 +02:00
|
|
|
|
2003-08-03 21:09:40 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2018-01-26 13:02:06 +01:00
|
|
|
-spec get_jid_info({subscription(), ask(), [binary()]}, binary(), binary(), jid())
|
|
|
|
-> {subscription(), ask(), [binary()]}.
|
2012-04-27 11:52:05 +02:00
|
|
|
get_jid_info(_, User, Server, JID) ->
|
2017-05-17 13:47:35 +02:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2017-05-17 13:47:35 +02:00
|
|
|
get_subscription_and_groups(LUser, LServer, LJID).
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2018-06-28 09:37:20 +02:00
|
|
|
%% Check if `From` is subscriberd to `To`s presence
|
|
|
|
%% note 1: partial subscriptions are also considered, i.e.
|
|
|
|
%% `To` has already sent a subscription request to `From`
|
|
|
|
%% note 2: it's assumed a user is subscribed to self
|
|
|
|
%% note 3: `To` MUST be a local user, `From` can be any user
|
|
|
|
-spec is_subscribed(jid(), jid()) -> boolean().
|
|
|
|
is_subscribed(#jid{luser = LUser, lserver = LServer},
|
|
|
|
#jid{luser = LUser, lserver = LServer}) ->
|
|
|
|
true;
|
|
|
|
is_subscribed(From, #jid{luser = LUser, lserver = LServer}) ->
|
|
|
|
{Sub, Ask, _} = ejabberd_hooks:run_fold(
|
|
|
|
roster_get_jid_info, LServer,
|
|
|
|
{none, none, []},
|
|
|
|
[LUser, LServer, From]),
|
|
|
|
(Sub /= none) orelse (Ask == subscribe)
|
|
|
|
orelse (Ask == out) orelse (Ask == both).
|
|
|
|
|
2019-05-15 10:57:55 +02:00
|
|
|
process_rosteritems(ActionS, SubsS, AsksS, UsersS, ContactsS) ->
|
|
|
|
LServer = ejabberd_config:get_myname(),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:process_rosteritems(ActionS, SubsS, AsksS, UsersS, ContactsS).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
webadmin_page(_, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
#request{us = _US, path = [<<"user">>, U, <<"roster">>],
|
|
|
|
q = Query, lang = Lang} =
|
|
|
|
_Request) ->
|
|
|
|
Res = user_roster(U, Host, Query, Lang), {stop, Res};
|
2007-08-24 18:15:05 +02:00
|
|
|
webadmin_page(Acc, _, _) -> Acc.
|
|
|
|
|
|
|
|
user_roster(User, Server, Query, Lang) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2012-04-27 11:52:05 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
Items1 = get_roster(LUser, LServer),
|
2013-03-14 10:33:02 +01:00
|
|
|
Res = user_roster_parse_query(User, Server, Items1,
|
|
|
|
Query),
|
2012-04-27 11:52:05 +02:00
|
|
|
Items = get_roster(LUser, LServer),
|
2007-08-24 18:15:05 +02:00
|
|
|
SItems = lists:sort(Items),
|
2013-03-14 10:33:02 +01:00
|
|
|
FItems = case SItems of
|
2019-06-22 16:08:45 +02:00
|
|
|
[] -> [?CT(?T("None"))];
|
2013-03-14 10:33:02 +01:00
|
|
|
_ ->
|
|
|
|
[?XE(<<"table">>,
|
|
|
|
[?XE(<<"thead">>,
|
|
|
|
[?XE(<<"tr">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
[?XCT(<<"td">>, ?T("Jabber ID")),
|
|
|
|
?XCT(<<"td">>, ?T("Nickname")),
|
|
|
|
?XCT(<<"td">>, ?T("Subscription")),
|
|
|
|
?XCT(<<"td">>, ?T("Pending")),
|
|
|
|
?XCT(<<"td">>, ?T("Groups"))])]),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"tbody">>,
|
|
|
|
(lists:map(fun (R) ->
|
|
|
|
Groups = lists:flatmap(fun
|
|
|
|
(Group) ->
|
|
|
|
[?C(Group),
|
|
|
|
?BR]
|
|
|
|
end,
|
|
|
|
R#roster.groups),
|
|
|
|
Pending =
|
|
|
|
ask_to_pending(R#roster.ask),
|
|
|
|
TDJID =
|
|
|
|
build_contact_jid_td(R#roster.jid),
|
|
|
|
?XE(<<"tr">>,
|
|
|
|
[TDJID,
|
|
|
|
?XAC(<<"td">>,
|
|
|
|
[{<<"class">>,
|
|
|
|
<<"valign">>}],
|
|
|
|
(R#roster.name)),
|
|
|
|
?XAC(<<"td">>,
|
|
|
|
[{<<"class">>,
|
|
|
|
<<"valign">>}],
|
|
|
|
(iolist_to_binary(atom_to_list(R#roster.subscription)))),
|
|
|
|
?XAC(<<"td">>,
|
|
|
|
[{<<"class">>,
|
|
|
|
<<"valign">>}],
|
|
|
|
(iolist_to_binary(atom_to_list(Pending)))),
|
|
|
|
?XAE(<<"td">>,
|
|
|
|
[{<<"class">>,
|
|
|
|
<<"valign">>}],
|
|
|
|
Groups),
|
|
|
|
if Pending == in ->
|
|
|
|
?XAE(<<"td">>,
|
|
|
|
[{<<"class">>,
|
|
|
|
<<"valign">>}],
|
|
|
|
[?INPUTT(<<"submit">>,
|
|
|
|
<<"validate",
|
|
|
|
(ejabberd_web_admin:term_to_id(R#roster.jid))/binary>>,
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("Validate"))]);
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> ?X(<<"td">>)
|
|
|
|
end,
|
|
|
|
?XAE(<<"td">>,
|
|
|
|
[{<<"class">>,
|
|
|
|
<<"valign">>}],
|
|
|
|
[?INPUTT(<<"submit">>,
|
|
|
|
<<"remove",
|
|
|
|
(ejabberd_web_admin:term_to_id(R#roster.jid))/binary>>,
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("Remove"))])])
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
SItems)))])]
|
|
|
|
end,
|
|
|
|
[?XC(<<"h1">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
(<<(translate:translate(Lang, ?T("Roster of ")))/binary, (us_to_list(US))/binary>>))]
|
2013-03-14 10:33:02 +01:00
|
|
|
++
|
|
|
|
case Res of
|
2019-06-22 16:08:45 +02:00
|
|
|
ok -> [?XREST(?T("Submitted"))];
|
|
|
|
error -> [?XREST(?T("Bad format"))];
|
2013-03-14 10:33:02 +01:00
|
|
|
nothing -> []
|
|
|
|
end
|
|
|
|
++
|
|
|
|
[?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
(FItems ++
|
|
|
|
[?P, ?INPUT(<<"text">>, <<"newjid">>, <<"">>),
|
|
|
|
?C(<<" ">>),
|
|
|
|
?INPUTT(<<"submit">>, <<"addjid">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("Add Jabber ID"))]))].
|
2007-08-24 18:15:05 +02:00
|
|
|
|
2009-01-07 02:38:45 +01:00
|
|
|
build_contact_jid_td(RosterJID) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ContactJID = jid:make(RosterJID),
|
2013-03-14 10:33:02 +01:00
|
|
|
JIDURI = case {ContactJID#jid.luser,
|
|
|
|
ContactJID#jid.lserver}
|
|
|
|
of
|
|
|
|
{<<"">>, _} -> <<"">>;
|
|
|
|
{CUser, CServer} ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case lists:member(CServer, ejabberd_option:hosts()) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> <<"">>;
|
|
|
|
true ->
|
|
|
|
<<"/admin/server/", CServer/binary, "/user/",
|
|
|
|
CUser/binary, "/">>
|
|
|
|
end
|
2009-01-07 02:38:45 +01:00
|
|
|
end,
|
|
|
|
case JIDURI of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<>> ->
|
|
|
|
?XAC(<<"td">>, [{<<"class">>, <<"valign">>}],
|
2017-02-26 08:07:12 +01:00
|
|
|
(jid:encode(RosterJID)));
|
2013-03-14 10:33:02 +01:00
|
|
|
URI when is_binary(URI) ->
|
|
|
|
?XAE(<<"td">>, [{<<"class">>, <<"valign">>}],
|
2017-02-26 08:07:12 +01:00
|
|
|
[?AC(JIDURI, (jid:encode(RosterJID)))])
|
2009-01-07 02:38:45 +01:00
|
|
|
end.
|
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
user_roster_parse_query(User, Server, Items, Query) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:keysearch(<<"addjid">>, 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
case lists:keysearch(<<"newjid">>, 1, Query) of
|
|
|
|
{value, {_, SJID}} ->
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(SJID) of
|
|
|
|
JID ->
|
|
|
|
user_roster_subscribe_jid(User, Server, JID), ok
|
|
|
|
catch _:{bad_jid, _} ->
|
|
|
|
error
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
false -> error
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
case catch user_roster_item_parse_query(User, Server,
|
|
|
|
Items, Query)
|
|
|
|
of
|
|
|
|
submitted -> ok;
|
|
|
|
{'EXIT', _Reason} -> error;
|
|
|
|
_ -> nothing
|
|
|
|
end
|
2007-08-24 18:15:05 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
user_roster_subscribe_jid(User, Server, JID) ->
|
2017-02-25 08:01:01 +01:00
|
|
|
UJID = jid:make(User, Server),
|
2018-01-26 13:02:06 +01:00
|
|
|
Presence = #presence{from = UJID, to = JID, type = subscribe},
|
|
|
|
out_subscription(Presence),
|
|
|
|
ejabberd_router:route(Presence).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
user_roster_item_parse_query(User, Server, Items,
|
|
|
|
Query) ->
|
|
|
|
lists:foreach(fun (R) ->
|
|
|
|
JID = R#roster.jid,
|
|
|
|
case lists:keysearch(<<"validate",
|
|
|
|
(ejabberd_web_admin:term_to_id(JID))/binary>>,
|
|
|
|
1, Query)
|
|
|
|
of
|
|
|
|
{value, _} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
JID1 = jid:make(JID),
|
2017-02-25 08:01:01 +01:00
|
|
|
UJID = jid:make(User, Server),
|
2018-01-26 13:02:06 +01:00
|
|
|
Pres = #presence{from = UJID, to = JID1,
|
|
|
|
type = subscribed},
|
|
|
|
out_subscription(Pres),
|
|
|
|
ejabberd_router:route(Pres),
|
2013-03-14 10:33:02 +01:00
|
|
|
throw(submitted);
|
|
|
|
false ->
|
|
|
|
case lists:keysearch(<<"remove",
|
|
|
|
(ejabberd_web_admin:term_to_id(JID))/binary>>,
|
|
|
|
1, Query)
|
|
|
|
of
|
|
|
|
{value, _} ->
|
2016-07-18 14:01:32 +02:00
|
|
|
UJID = jid:make(User, Server),
|
|
|
|
RosterItem = #roster_item{
|
|
|
|
jid = jid:make(JID),
|
|
|
|
subscription = remove},
|
|
|
|
process_iq_set(
|
|
|
|
#iq{type = set,
|
|
|
|
from = UJID,
|
|
|
|
to = UJID,
|
2018-07-05 10:51:49 +02:00
|
|
|
id = p1_rand:get_string(),
|
2016-07-18 14:01:32 +02:00
|
|
|
sub_els = [#roster_query{
|
|
|
|
items = [RosterItem]}]}),
|
2013-03-14 10:33:02 +01:00
|
|
|
throw(submitted);
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Items),
|
2007-08-24 18:15:05 +02:00
|
|
|
nothing.
|
|
|
|
|
|
|
|
us_to_list({User, Server}) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
jid:encode({User, Server, <<"">>}).
|
2007-08-24 18:15:05 +02:00
|
|
|
|
|
|
|
webadmin_user(Acc, _User, _Server, Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Acc ++
|
2019-06-22 16:08:45 +02:00
|
|
|
[?XE(<<"h3">>, [?ACT(<<"roster/">>, ?T("Roster"))])].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2014-02-26 18:01:47 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2019-07-09 13:30:59 +02:00
|
|
|
-spec has_duplicated_groups([binary()]) -> boolean().
|
2016-11-07 08:10:57 +01:00
|
|
|
has_duplicated_groups(Groups) ->
|
|
|
|
GroupsPrep = lists:usort([jid:resourceprep(G) || G <- Groups]),
|
|
|
|
not (length(GroupsPrep) == length(Groups)).
|
|
|
|
|
2017-05-17 13:47:35 +02:00
|
|
|
-spec init_cache(module(), binary(), gen_mod:opts()) -> ok.
|
|
|
|
init_cache(Mod, Host, Opts) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
CacheOpts = cache_opts(Opts),
|
2017-05-17 13:47:35 +02:00
|
|
|
case use_cache(Mod, Host, roster_version) of
|
|
|
|
true ->
|
|
|
|
ets_cache:new(?ROSTER_VERSION_CACHE, CacheOpts);
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?ROSTER_VERSION_CACHE)
|
|
|
|
end,
|
|
|
|
case use_cache(Mod, Host, roster) of
|
|
|
|
true ->
|
|
|
|
ets_cache:new(?ROSTER_CACHE, CacheOpts),
|
|
|
|
ets_cache:new(?ROSTER_ITEM_CACHE, CacheOpts);
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?ROSTER_CACHE),
|
|
|
|
ets_cache:delete(?ROSTER_ITEM_CACHE)
|
|
|
|
end.
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec cache_opts(gen_mod:opts()) -> [proplists:property()].
|
|
|
|
cache_opts(Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = mod_roster_opt:cache_size(Opts),
|
|
|
|
CacheMissed = mod_roster_opt:cache_missed(Opts),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = mod_roster_opt:cache_life_time(Opts),
|
2017-05-17 13:47:35 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
|
|
|
-spec use_cache(module(), binary(), roster | roster_version) -> boolean().
|
|
|
|
use_cache(Mod, Host, Table) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 2) of
|
|
|
|
true -> Mod:use_cache(Host, Table);
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> mod_roster_opt:use_cache(Host)
|
2017-05-17 13:47:35 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_nodes(module(), binary()) -> [node()].
|
|
|
|
cache_nodes(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 1) of
|
|
|
|
true -> Mod:cache_nodes(Host);
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec delete_cache(module(), binary(), binary(), [ljid()]) -> ok.
|
|
|
|
delete_cache(Mod, LUser, LServer, LJIDs) ->
|
|
|
|
case use_cache(Mod, LServer, roster_version) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?ROSTER_VERSION_CACHE, {LUser, LServer},
|
|
|
|
cache_nodes(Mod, LServer));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
case use_cache(Mod, LServer, roster) of
|
|
|
|
true ->
|
|
|
|
Nodes = cache_nodes(Mod, LServer),
|
|
|
|
ets_cache:delete(?ROSTER_CACHE, {LUser, LServer}, Nodes),
|
|
|
|
lists:foreach(
|
|
|
|
fun(LJID) ->
|
|
|
|
ets_cache:delete(
|
|
|
|
?ROSTER_ITEM_CACHE,
|
|
|
|
{LUser, LServer, jid:remove_resource(LJID)},
|
|
|
|
Nodes)
|
|
|
|
end, LJIDs);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2016-04-14 09:58:32 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info() ->
|
|
|
|
[{<<"roster_version">>, 2},
|
|
|
|
{<<"rostergroups">>, 3},
|
|
|
|
{<<"rosterusers">>, 10}].
|
|
|
|
|
|
|
|
import_start(LServer, DBType) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
ets:new(rostergroups_tmp, [private, named_table, bag]),
|
|
|
|
Mod:init(LServer, []),
|
|
|
|
ok.
|
2016-04-14 09:58:32 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import_stop(_LServer, _DBType) ->
|
|
|
|
ets:delete(rostergroups_tmp),
|
|
|
|
ok.
|
|
|
|
|
2018-03-15 15:55:05 +01:00
|
|
|
row_length() ->
|
|
|
|
case ejabberd_sql:use_new_schema() of
|
|
|
|
true -> 10;
|
|
|
|
false -> 9
|
|
|
|
end.
|
2017-11-02 15:03:30 +01:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, {sql, _}, _DBType, <<"rostergroups">>, [LUser, SJID, Group]) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
LJID = jid:tolower(jid:decode(SJID)),
|
2016-11-22 14:48:01 +01:00
|
|
|
ets:insert(rostergroups_tmp, {{LUser, LServer, LJID}, Group}),
|
|
|
|
ok;
|
|
|
|
import(LServer, {sql, _}, DBType, <<"rosterusers">>, Row) ->
|
2018-03-15 15:55:05 +01:00
|
|
|
I = mod_roster_sql:raw_to_record(LServer, lists:sublist(Row, row_length())),
|
2016-11-22 14:48:01 +01:00
|
|
|
Groups = [G || {_, G} <- ets:lookup(rostergroups_tmp, I#roster.usj)],
|
|
|
|
RosterItem = I#roster{groups = Groups},
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, <<"rosterusers">>, RosterItem);
|
|
|
|
import(LServer, {sql, _}, DBType, <<"roster_version">>, [LUser, Ver]) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
2016-11-22 14:48:01 +01:00
|
|
|
Mod:import(LServer, <<"roster_version">>, [LUser, Ver]).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(access) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:acl();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(store_current_id) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(versioning) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
|
|
|
mod_opt_type(db_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:db_type(?MODULE);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(use_cache) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_size) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:pos_int(infinity);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_missed) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_life_time) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:timeout(second, infinity).
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(Host) ->
|
|
|
|
[{access, all},
|
|
|
|
{store_current_id, false},
|
|
|
|
{versioning, false},
|
|
|
|
{db_type, ejabberd_config:default_db(Host, ?MODULE)},
|
2019-06-14 11:33:26 +02:00
|
|
|
{use_cache, ejabberd_option:use_cache(Host)},
|
|
|
|
{cache_size, ejabberd_option:cache_size(Host)},
|
|
|
|
{cache_missed, ejabberd_option:cache_missed(Host)},
|
|
|
|
{cache_life_time, ejabberd_option:cache_life_time(Host)}].
|