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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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'}).
|
|
|
|
-protocol({xep, 321, '0.1'}).
|
|
|
|
|
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
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([start/2, stop/1, process_iq/3, export/1,
|
|
|
|
import/1, process_local_iq/3, get_user_roster/2,
|
|
|
|
import/3, get_subscription_lists/3, get_roster/2,
|
2013-03-14 10:33:02 +01:00
|
|
|
get_in_pending_subscriptions/3, in_subscription/6,
|
|
|
|
out_subscription/4, set_items/3, remove_user/2,
|
|
|
|
get_jid_info/4, item_to_xml/1, webadmin_page/3,
|
|
|
|
webadmin_user/4, get_versioning_feature/2,
|
|
|
|
roster_versioning_enabled/1, roster_version/2,
|
2016-07-06 13:58:48 +02:00
|
|
|
mod_opt_type/1, set_roster/1, depends/2]).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-include("mod_roster.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("ejabberd_http.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("ejabberd_web_admin.hrl").
|
2002-12-11 21:57:45 +01:00
|
|
|
|
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().
|
|
|
|
-callback import(binary(), #roster{} | #roster_version{}) -> ok | pass.
|
|
|
|
-callback read_roster_version(binary(), binary()) -> binary() | error.
|
|
|
|
-callback write_roster_version(binary(), binary(), boolean(), binary()) -> any().
|
|
|
|
-callback get_roster(binary(), binary()) -> [#roster{}].
|
|
|
|
-callback get_roster_by_jid(binary(), binary(), ljid()) -> #roster{}.
|
|
|
|
-callback get_only_items(binary(), binary()) -> [#roster{}].
|
|
|
|
-callback roster_subscribe(binary(), binary(), ljid(), #roster{}) -> any().
|
|
|
|
-callback transaction(binary(), function()) -> {atomic, any()} | {aborted, any()}.
|
|
|
|
-callback get_roster_by_jid_with_groups(binary(), binary(), ljid()) -> #roster{}.
|
|
|
|
-callback remove_user(binary(), binary()) -> {atomic, any()}.
|
|
|
|
-callback update_roster(binary(), binary(), ljid(), #roster{}) -> any().
|
|
|
|
-callback del_roster(binary(), binary(), ljid()) -> any().
|
|
|
|
-callback read_subscription_and_groups(binary(), binary(), ljid()) ->
|
|
|
|
{subscription(), [binary()]}.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
|
|
|
Mod:init(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),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_get_subscription_lists, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_subscription_lists, 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),
|
|
|
|
ejabberd_hooks:add(anonymous_purge_hook, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
|
|
|
ejabberd_hooks:add(resend_subscription_requests_hook,
|
|
|
|
Host, ?MODULE, get_in_pending_subscriptions, 50),
|
2009-08-06 23:06:16 +02:00
|
|
|
ejabberd_hooks:add(roster_get_versioning_feature, Host,
|
|
|
|
?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,
|
|
|
|
?NS_ROSTER, ?MODULE, process_iq, IQDisc).
|
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),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(roster_get_subscription_lists,
|
|
|
|
Host, ?MODULE, get_subscription_lists, 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),
|
2007-05-12 18:28:34 +02:00
|
|
|
ejabberd_hooks:delete(anonymous_purge_hook, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(resend_subscription_requests_hook,
|
|
|
|
Host, ?MODULE, get_in_pending_subscriptions, 50),
|
|
|
|
ejabberd_hooks:delete(roster_get_versioning_feature,
|
|
|
|
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
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2014-02-26 18:01:47 +01:00
|
|
|
process_iq(From, To, IQ) when ((From#jid.luser == <<"">>) andalso (From#jid.resource == <<"">>)) ->
|
|
|
|
process_iq_manager(From, To, IQ);
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
process_iq(From, To, IQ) ->
|
2016-03-31 10:00:29 +02:00
|
|
|
#iq{sub_el = SubEl, lang = Lang} = IQ,
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{lserver = LServer} = From,
|
2005-04-17 20:08:34 +02:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> process_local_iq(From, To, IQ);
|
|
|
|
_ ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"The query is only allowed from local users">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
IQ#iq{type = error,
|
2016-03-31 10:00:29 +02:00
|
|
|
sub_el = [SubEl, ?ERRT_ITEM_NOT_FOUND(Lang, Txt)]}
|
2002-12-11 21:57:45 +01:00
|
|
|
end.
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
process_local_iq(From, To, #iq{type = Type} = IQ) ->
|
2003-07-21 22:01:22 +02:00
|
|
|
case Type of
|
2013-12-04 14:55:21 +01:00
|
|
|
set -> try_process_iq_set(From, To, IQ);
|
2013-03-14 10:33:02 +01:00
|
|
|
get -> process_iq_get(From, To, IQ)
|
2003-07-21 22:01:22 +02:00
|
|
|
end.
|
|
|
|
|
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) ->
|
2013-06-20 10:40:44 +02:00
|
|
|
p1_sha: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
|
|
|
roster_versioning_enabled(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_mod:get_module_opt(Host, ?MODULE, versioning,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
false).
|
2009-08-06 23:06:16 +02:00
|
|
|
|
|
|
|
roster_version_on_db(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_mod:get_module_opt(Host, ?MODULE, store_current_id,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
false).
|
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.
|
|
|
|
get_versioning_feature(Acc, Host) ->
|
|
|
|
case roster_versioning_enabled(Host) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
Feature = #xmlel{name = <<"ver">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_ROSTER_VER}],
|
|
|
|
children = []},
|
|
|
|
[Feature | Acc];
|
|
|
|
false -> []
|
2009-08-06 23:06:16 +02:00
|
|
|
end.
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
roster_version(LServer, LUser) ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case roster_version_on_db(LServer) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
case read_roster_version(LUser, LServer) of
|
|
|
|
error -> not_found;
|
|
|
|
V -> V
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
roster_hash(ejabberd_hooks:run_fold(roster_get, LServer,
|
|
|
|
[], [US]))
|
2012-04-27 11:52:05 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
read_roster_version(LUser, LServer) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:read_roster_version(LUser, LServer).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
write_roster_version(LUser, LServer) ->
|
|
|
|
write_roster_version(LUser, LServer, false).
|
|
|
|
|
|
|
|
write_roster_version_t(LUser, LServer) ->
|
|
|
|
write_roster_version(LUser, LServer, true).
|
|
|
|
|
|
|
|
write_roster_version(LUser, LServer, InTransaction) ->
|
2015-12-04 15:08:43 +01:00
|
|
|
Ver = p1_sha:sha(term_to_binary(p1_time_compat: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),
|
2012-04-27 11:52:05 +02:00
|
|
|
Ver.
|
|
|
|
|
2015-12-04 15:08:43 +01:00
|
|
|
%% Load roster from DB only if neccesary.
|
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
|
|
|
%% It is neccesary if
|
|
|
|
%% - 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.
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq_get(From, To, #iq{sub_el = SubEl} = IQ) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
LUser = From#jid.luser,
|
|
|
|
LServer = From#jid.lserver,
|
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
try {ItemsToSend, VersionToSend} = case
|
2016-02-03 19:03:17 +01:00
|
|
|
{fxml:get_tag_attr(<<"ver">>, SubEl),
|
2013-03-14 10:33:02 +01:00
|
|
|
roster_versioning_enabled(LServer),
|
|
|
|
roster_version_on_db(LServer)}
|
|
|
|
of
|
|
|
|
{{value, RequestedVersion}, true,
|
|
|
|
true} ->
|
|
|
|
case read_roster_version(LUser,
|
|
|
|
LServer)
|
|
|
|
of
|
|
|
|
error ->
|
|
|
|
RosterVersion =
|
|
|
|
write_roster_version(LUser,
|
|
|
|
LServer),
|
|
|
|
{lists:map(fun item_to_xml/1,
|
|
|
|
ejabberd_hooks:run_fold(roster_get,
|
|
|
|
To#jid.lserver,
|
|
|
|
[],
|
|
|
|
[US])),
|
|
|
|
RosterVersion};
|
|
|
|
RequestedVersion ->
|
|
|
|
{false, false};
|
|
|
|
NewVersion ->
|
|
|
|
{lists:map(fun item_to_xml/1,
|
|
|
|
ejabberd_hooks:run_fold(roster_get,
|
|
|
|
To#jid.lserver,
|
|
|
|
[],
|
|
|
|
[US])),
|
|
|
|
NewVersion}
|
|
|
|
end;
|
|
|
|
{{value, RequestedVersion}, true,
|
|
|
|
false} ->
|
|
|
|
RosterItems =
|
|
|
|
ejabberd_hooks:run_fold(roster_get,
|
|
|
|
To#jid.lserver,
|
|
|
|
[],
|
|
|
|
[US]),
|
|
|
|
case roster_hash(RosterItems) of
|
|
|
|
RequestedVersion ->
|
|
|
|
{false, false};
|
|
|
|
New ->
|
|
|
|
{lists:map(fun item_to_xml/1,
|
|
|
|
RosterItems),
|
|
|
|
New}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{lists:map(fun item_to_xml/1,
|
|
|
|
ejabberd_hooks:run_fold(roster_get,
|
|
|
|
To#jid.lserver,
|
|
|
|
[],
|
|
|
|
[US])),
|
|
|
|
false}
|
|
|
|
end,
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
case {ItemsToSend, VersionToSend} of
|
|
|
|
{false, false} -> [];
|
|
|
|
{Items, false} ->
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_ROSTER}],
|
|
|
|
children = Items}];
|
|
|
|
{Items, Version} ->
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_ROSTER},
|
|
|
|
{<<"ver">>, Version}],
|
|
|
|
children = Items}]
|
|
|
|
end}
|
2012-04-27 11:52:05 +02:00
|
|
|
catch
|
2013-03-14 10:33:02 +01:00
|
|
|
_:_ ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]}
|
2002-12-13 21:58:27 +01:00
|
|
|
end.
|
|
|
|
|
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
|
|
|
|
|
|
|
get_roster(LUser, LServer) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_roster(LUser, LServer).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2016-01-28 12:23:51 +01:00
|
|
|
set_roster(#roster{us = {LUser, LServer}, jid = LJID} = Item) ->
|
|
|
|
transaction(
|
|
|
|
LServer,
|
|
|
|
fun() ->
|
|
|
|
roster_subscribe_t(LUser, LServer, LJID, Item)
|
|
|
|
end).
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
item_to_xml(Item) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs1 = [{<<"jid">>,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(Item#roster.jid)}],
|
2002-12-13 21:58:27 +01:00
|
|
|
Attrs2 = case Item#roster.name of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> Attrs1;
|
|
|
|
Name -> [{<<"name">>, Name} | Attrs1]
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
|
|
|
Attrs3 = case Item#roster.subscription of
|
2013-03-14 10:33:02 +01:00
|
|
|
none -> [{<<"subscription">>, <<"none">>} | Attrs2];
|
|
|
|
from -> [{<<"subscription">>, <<"from">>} | Attrs2];
|
|
|
|
to -> [{<<"subscription">>, <<"to">>} | Attrs2];
|
|
|
|
both -> [{<<"subscription">>, <<"both">>} | Attrs2];
|
|
|
|
remove -> [{<<"subscription">>, <<"remove">>} | Attrs2]
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
2003-12-11 21:31:40 +01:00
|
|
|
Attrs4 = case ask_to_pending(Item#roster.ask) of
|
2013-03-14 10:33:02 +01:00
|
|
|
out -> [{<<"ask">>, <<"subscribe">>} | Attrs3];
|
|
|
|
both -> [{<<"ask">>, <<"subscribe">>} | Attrs3];
|
|
|
|
_ -> Attrs3
|
2002-12-20 21:42:08 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
SubEls1 = lists:map(fun (G) ->
|
|
|
|
#xmlel{name = <<"group">>, attrs = [],
|
|
|
|
children = [{xmlcdata, G}]}
|
|
|
|
end,
|
|
|
|
Item#roster.groups),
|
2002-12-13 21:58:27 +01:00
|
|
|
SubEls = SubEls1 ++ Item#roster.xs,
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"item">>, attrs = Attrs4,
|
|
|
|
children = SubEls}.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_roster_by_jid_t(LUser, LServer, LJID) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_roster_by_jid(LUser, LServer, LJID).
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2016-03-31 10:00:29 +02:00
|
|
|
try_process_iq_set(From, To, #iq{sub_el = SubEl, lang = Lang} = IQ) ->
|
2013-12-04 14:55:21 +01:00
|
|
|
#jid{server = Server} = From,
|
2016-06-21 13:18:24 +02:00
|
|
|
Access = gen_mod:get_module_opt(Server, ?MODULE, access, fun(A) -> A end, all),
|
2013-12-04 14:55:21 +01:00
|
|
|
case acl:match_rule(Server, Access, From) of
|
|
|
|
deny ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Denied by ACL">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]};
|
2013-12-04 14:55:21 +01:00
|
|
|
allow ->
|
|
|
|
process_iq_set(From, To, IQ)
|
|
|
|
end.
|
|
|
|
|
2014-02-26 18:01:47 +01:00
|
|
|
process_iq_set(From, To, #iq{sub_el = SubEl, id = Id} = IQ) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{children = Els} = SubEl,
|
2014-02-26 18:01:47 +01:00
|
|
|
Managed = is_managed_from_id(Id),
|
|
|
|
lists:foreach(fun (El) -> process_item_set(From, To, El, Managed)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
Els),
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result, sub_el = []}.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_item_set(From, To,
|
2014-02-26 18:01:47 +01:00
|
|
|
#xmlel{attrs = Attrs, children = Els}, Managed) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
JID1 = jid:from_string(fxml:get_attr_s(<<"jid">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs)),
|
|
|
|
#jid{user = User, luser = LUser, lserver = LServer} =
|
|
|
|
From,
|
2003-10-07 22:31:44 +02:00
|
|
|
case JID1 of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> ok;
|
|
|
|
_ ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID1),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
Item = get_roster_by_jid_t(LUser, LServer, LJID),
|
2014-02-26 18:01:47 +01:00
|
|
|
Item1 = process_item_attrs_managed(Item, Attrs, Managed),
|
2013-03-14 10:33:02 +01:00
|
|
|
Item2 = process_item_els(Item1, Els),
|
|
|
|
Item3 = ejabberd_hooks:run_fold(roster_process_item,
|
|
|
|
LServer, Item2,
|
|
|
|
[LServer]),
|
2016-05-19 12:45:22 +02:00
|
|
|
case Item3#roster.subscription of
|
|
|
|
remove -> del_roster_t(LUser, LServer, LJID);
|
|
|
|
_ -> update_roster_t(LUser, LServer, LJID, Item3)
|
|
|
|
end,
|
|
|
|
send_itemset_to_managers(From, Item3, Managed),
|
2013-03-14 10:33:02 +01:00
|
|
|
case roster_version_on_db(LServer) of
|
|
|
|
true -> write_roster_version_t(LUser, LServer);
|
|
|
|
false -> ok
|
|
|
|
end,
|
|
|
|
{Item, Item3}
|
|
|
|
end,
|
|
|
|
case transaction(LServer, F) of
|
|
|
|
{atomic, {OldItem, Item}} ->
|
|
|
|
push_item(User, LServer, To, Item),
|
|
|
|
case Item#roster.subscription of
|
|
|
|
remove ->
|
|
|
|
send_unsubscribing_presence(From, OldItem), ok;
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
E ->
|
|
|
|
?DEBUG("ROSTER: roster item set error: ~p~n", [E]), ok
|
|
|
|
end
|
2003-02-20 18:12:03 +01:00
|
|
|
end;
|
2014-02-26 18:01:47 +01:00
|
|
|
process_item_set(_From, _To, _, _Managed) -> ok.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
|
|
|
process_item_attrs(Item, [{Attr, Val} | Attrs]) ->
|
|
|
|
case Attr of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"jid">> ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:from_string(Val) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> process_item_attrs(Item, Attrs);
|
|
|
|
JID1 ->
|
|
|
|
JID = {JID1#jid.luser, JID1#jid.lserver,
|
|
|
|
JID1#jid.lresource},
|
|
|
|
process_item_attrs(Item#roster{jid = JID}, Attrs)
|
|
|
|
end;
|
|
|
|
<<"name">> ->
|
|
|
|
process_item_attrs(Item#roster{name = Val}, Attrs);
|
|
|
|
<<"subscription">> ->
|
|
|
|
case Val of
|
|
|
|
<<"remove">> ->
|
|
|
|
process_item_attrs(Item#roster{subscription = remove},
|
|
|
|
Attrs);
|
|
|
|
_ -> process_item_attrs(Item, Attrs)
|
|
|
|
end;
|
|
|
|
<<"ask">> -> process_item_attrs(Item, Attrs);
|
|
|
|
_ -> process_item_attrs(Item, Attrs)
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
process_item_attrs(Item, []) -> Item.
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_item_els(Item,
|
|
|
|
[#xmlel{name = Name, attrs = Attrs, children = SEls}
|
|
|
|
| Els]) ->
|
2002-12-13 21:58:27 +01:00
|
|
|
case Name of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"group">> ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Groups = [fxml:get_cdata(SEls) | Item#roster.groups],
|
2013-03-14 10:33:02 +01:00
|
|
|
process_item_els(Item#roster{groups = Groups}, Els);
|
|
|
|
_ ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"xmlns">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> process_item_els(Item, Els);
|
|
|
|
_ ->
|
|
|
|
XEls = [#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = SEls}
|
|
|
|
| Item#roster.xs],
|
|
|
|
process_item_els(Item#roster{xs = XEls}, Els)
|
|
|
|
end
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
|
|
|
process_item_els(Item, [{xmlcdata, _} | Els]) ->
|
|
|
|
process_item_els(Item, Els);
|
2013-03-14 10:33:02 +01:00
|
|
|
process_item_els(Item, []) -> Item.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
push_item(User, Server, From, Item) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_sm:route(jid:make(<<"">>, <<"">>, <<"">>),
|
|
|
|
jid:make(User, Server, <<"">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
{broadcast, {item, Item#roster.jid,
|
|
|
|
Item#roster.subscription}}),
|
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
|
|
|
case roster_versioning_enabled(Server) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
push_item_version(Server, User, From, Item,
|
|
|
|
roster_version(Server, User));
|
|
|
|
false ->
|
|
|
|
lists:foreach(fun (Resource) ->
|
|
|
|
push_item(User, Server, Resource, From, Item)
|
|
|
|
end,
|
|
|
|
ejabberd_sm:get_user_resources(User, Server))
|
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
|
|
|
end.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
push_item(User, Server, Resource, From, Item) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
push_item(User, Server, Resource, From, Item,
|
|
|
|
not_found).
|
2011-04-29 20:30:17 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
push_item(User, Server, Resource, From, Item,
|
|
|
|
RosterVersion) ->
|
2011-04-29 20:30:17 +02:00
|
|
|
ExtraAttrs = case RosterVersion of
|
2013-03-14 10:33:02 +01:00
|
|
|
not_found -> [];
|
|
|
|
_ -> [{<<"ver">>, RosterVersion}]
|
|
|
|
end,
|
2003-12-17 21:13:21 +01:00
|
|
|
ResIQ = #iq{type = set, xmlns = ?NS_ROSTER,
|
2009-08-06 23:06:16 +02:00
|
|
|
%% @doc Roster push, calculate and include the version attribute.
|
|
|
|
%% TODO: don't push to those who didn't load roster
|
2013-03-14 10:33:02 +01:00
|
|
|
id = <<"push", (randoms:get_string())/binary>>,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_ROSTER} | ExtraAttrs],
|
|
|
|
children = [item_to_xml(Item)]}]},
|
|
|
|
ejabberd_router:route(From,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(User, Server, Resource),
|
2013-03-14 10:33:02 +01:00
|
|
|
jlib:iq_to_xml(ResIQ)).
|
|
|
|
|
|
|
|
push_item_version(Server, User, From, Item,
|
|
|
|
RosterVersion) ->
|
|
|
|
lists:foreach(fun (Resource) ->
|
|
|
|
push_item(User, Server, Resource, From, Item,
|
|
|
|
RosterVersion)
|
|
|
|
end,
|
|
|
|
ejabberd_sm:get_user_resources(User, Server)).
|
2009-08-06 23:06:16 +02:00
|
|
|
|
2016-04-14 09:58:32 +02:00
|
|
|
get_subscription_lists(_Acc, User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Items = Mod:get_only_items(LUser, LServer),
|
2012-04-27 11:52:05 +02:00
|
|
|
fill_subscription_lists(LServer, Items, [], []).
|
|
|
|
|
2016-04-14 09:58:32 +02:00
|
|
|
fill_subscription_lists(LServer, [I | Is], F, T) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
J = element(3, I#roster.usj),
|
2002-12-17 21:49:45 +01:00
|
|
|
case I#roster.subscription of
|
2016-04-14 09:58:32 +02:00
|
|
|
both ->
|
|
|
|
fill_subscription_lists(LServer, Is, [J | F], [J | T]);
|
|
|
|
from ->
|
|
|
|
fill_subscription_lists(LServer, Is, [J | F], T);
|
|
|
|
to -> fill_subscription_lists(LServer, Is, F, [J | T]);
|
|
|
|
_ -> fill_subscription_lists(LServer, Is, F, T)
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
2016-04-14 09:58:32 +02:00
|
|
|
fill_subscription_lists(_LServer, [], F, T) ->
|
|
|
|
{F, T}.
|
2002-12-17 21:49:45 +01:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
transaction(LServer, F) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:transaction(LServer, F).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
in_subscription(_, User, Server, JID, Type, Reason) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
process_subscription(in, User, Server, JID, Type,
|
|
|
|
Reason).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
out_subscription(User, Server, JID, Type) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
process_subscription(out, User, Server, JID, Type, <<"">>).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
get_roster_by_jid_with_groups_t(LUser, LServer, LJID) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_roster_by_jid_with_groups(LUser, LServer, LJID).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
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),
|
|
|
|
LJID = jid:tolower(JID1),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
Item = get_roster_by_jid_with_groups_t(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
|
2013-03-14 10:33:02 +01: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 =
|
|
|
|
iolist_to_binary(AskMessage)},
|
|
|
|
roster_subscribe_t(LUser, LServer, LJID, NewItem),
|
|
|
|
case roster_version_on_db(LServer) of
|
|
|
|
true -> write_roster_version_t(LUser, LServer);
|
|
|
|
false -> ok
|
|
|
|
end,
|
|
|
|
{{push, NewItem}, AutoReply}
|
2002-12-20 21:42:08 +01:00
|
|
|
end
|
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
case transaction(LServer, F) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, {Push, AutoReply}} ->
|
|
|
|
case AutoReply of
|
|
|
|
none -> ok;
|
|
|
|
_ ->
|
|
|
|
T = case AutoReply of
|
|
|
|
subscribed -> <<"subscribed">>;
|
|
|
|
unsubscribed -> <<"unsubscribed">>
|
2007-04-26 16:45:16 +02:00
|
|
|
end,
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_router:route(jid:make(User, Server,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">>),
|
|
|
|
JID1,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, T}],
|
|
|
|
children = []})
|
|
|
|
end,
|
|
|
|
case Push of
|
|
|
|
{push, Item} ->
|
|
|
|
if Item#roster.subscription == none,
|
|
|
|
Item#roster.ask == in ->
|
|
|
|
ok;
|
|
|
|
true ->
|
|
|
|
push_item(User, Server,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:make(User, Server, <<"">>), Item)
|
2013-03-14 10:33:02 +01: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
|
|
|
|
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),
|
2012-11-06 16:58:08 +01:00
|
|
|
send_unsubscription_to_rosteritems(LUser, LServer),
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:remove_user(LUser, LServer).
|
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.
|
|
|
|
send_unsubscription_to_rosteritems(LUser, LServer) ->
|
|
|
|
RosterItems = get_user_roster([], {LUser, LServer}),
|
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).
|
|
|
|
|
|
|
|
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 ->
|
2015-11-24 16:44:13 +01:00
|
|
|
send_presence_type(jid:remove_resource(From),
|
|
|
|
jid:make(Item#roster.jid),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"unsubscribe">>);
|
2009-02-16 15:49:20 +01:00
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
if IsFrom ->
|
2015-11-24 16:44:13 +01:00
|
|
|
send_presence_type(jid:remove_resource(From),
|
|
|
|
jid:make(Item#roster.jid),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"unsubscribed">>);
|
2009-02-16 15:49:20 +01:00
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
|
|
|
send_presence_type(From, To, Type) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_router:route(From, To,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, Type}], children = []}).
|
2009-02-16 15:49:20 +01:00
|
|
|
|
2003-02-02 20:49:19 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
set_items(User, Server, SubEl) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{children = Els} = SubEl,
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
|
|
|
lists:foreach(fun (El) ->
|
|
|
|
process_item_set_t(LUser, LServer, El)
|
|
|
|
end,
|
|
|
|
Els)
|
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
transaction(LServer, F).
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_item_set_t(LUser, LServer,
|
|
|
|
#xmlel{attrs = Attrs, children = Els}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
JID1 = jid:from_string(fxml:get_attr_s(<<"jid">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
Attrs)),
|
2003-10-07 22:31:44 +02:00
|
|
|
case JID1 of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> ok;
|
|
|
|
_ ->
|
|
|
|
JID = {JID1#jid.user, JID1#jid.server,
|
|
|
|
JID1#jid.resource},
|
|
|
|
LJID = {JID1#jid.luser, JID1#jid.lserver,
|
|
|
|
JID1#jid.lresource},
|
|
|
|
Item = #roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer}, jid = JID},
|
|
|
|
Item1 = process_item_attrs_ws(Item, Attrs),
|
|
|
|
Item2 = process_item_els(Item1, Els),
|
|
|
|
case Item2#roster.subscription of
|
|
|
|
remove -> del_roster_t(LUser, LServer, LJID);
|
|
|
|
_ -> update_roster_t(LUser, LServer, LJID, Item2)
|
|
|
|
end
|
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
|
|
|
|
|
|
|
process_item_attrs_ws(Item, [{Attr, Val} | Attrs]) ->
|
|
|
|
case Attr of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"jid">> ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:from_string(Val) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> process_item_attrs_ws(Item, Attrs);
|
|
|
|
JID1 ->
|
|
|
|
JID = {JID1#jid.luser, JID1#jid.lserver,
|
|
|
|
JID1#jid.lresource},
|
|
|
|
process_item_attrs_ws(Item#roster{jid = JID}, Attrs)
|
|
|
|
end;
|
|
|
|
<<"name">> ->
|
|
|
|
process_item_attrs_ws(Item#roster{name = Val}, Attrs);
|
|
|
|
<<"subscription">> ->
|
|
|
|
case Val of
|
|
|
|
<<"remove">> ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription =
|
|
|
|
remove},
|
|
|
|
Attrs);
|
|
|
|
<<"none">> ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = none},
|
|
|
|
Attrs);
|
|
|
|
<<"both">> ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = both},
|
|
|
|
Attrs);
|
|
|
|
<<"from">> ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = from},
|
|
|
|
Attrs);
|
|
|
|
<<"to">> ->
|
|
|
|
process_item_attrs_ws(Item#roster{subscription = to},
|
|
|
|
Attrs);
|
|
|
|
_ -> process_item_attrs_ws(Item, Attrs)
|
|
|
|
end;
|
|
|
|
<<"ask">> -> process_item_attrs_ws(Item, Attrs);
|
|
|
|
_ -> process_item_attrs_ws(Item, Attrs)
|
2003-02-02 20:49:19 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
process_item_attrs_ws(Item, []) -> Item.
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
get_in_pending_subscriptions(Ls, User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
get_in_pending_subscriptions(Ls, User, Server, Mod).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2016-04-14 09:58:32 +02:00
|
|
|
get_in_pending_subscriptions(Ls, User, Server, Mod) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
JID = jid:make(User, Server, <<"">>),
|
2016-04-14 09:58:32 +02:00
|
|
|
Result = Mod:get_only_items(JID#jid.luser, JID#jid.lserver),
|
2012-11-13 13:56:27 +01:00
|
|
|
Ls ++ lists:map(fun (R) ->
|
|
|
|
Message = R#roster.askmessage,
|
|
|
|
Status = if is_binary(Message) -> (Message);
|
|
|
|
true -> <<"">>
|
|
|
|
end,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"from">>,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(R#roster.jid)},
|
|
|
|
{<<"to">>, jid:to_string(JID)},
|
2012-11-13 13:56:27 +01:00
|
|
|
{<<"type">>, <<"subscribe">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"status">>,
|
|
|
|
attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata, Status}]}]}
|
|
|
|
end,
|
|
|
|
lists:filter(fun (R) ->
|
|
|
|
case R#roster.ask of
|
|
|
|
in -> true;
|
|
|
|
both -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end,
|
2016-04-14 09:58:32 +02:00
|
|
|
Result)).
|
2006-05-23 22:19:37 +02:00
|
|
|
|
2003-08-03 21:09:40 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
read_subscription_and_groups(User, Server, LJID) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:read_subscription_and_groups(LUser, LServer, LJID).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
get_jid_info(_, User, Server, JID) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LJID = jid:tolower(JID),
|
2012-04-27 11:52:05 +02:00
|
|
|
case read_subscription_and_groups(User, Server, LJID) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{Subscription, Groups} -> {Subscription, Groups};
|
|
|
|
error ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LRJID = jid:tolower(jid:remove_resource(JID)),
|
2013-03-14 10:33:02 +01:00
|
|
|
if LRJID == LJID -> {none, []};
|
|
|
|
true ->
|
|
|
|
case read_subscription_and_groups(User, Server, LRJID)
|
|
|
|
of
|
|
|
|
{Subscription, Groups} -> {Subscription, Groups};
|
|
|
|
error -> {none, []}
|
|
|
|
end
|
|
|
|
end
|
2003-08-03 21:09:40 +02:00
|
|
|
end.
|
|
|
|
|
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
|
|
|
|
[] -> [?CT(<<"None">>)];
|
|
|
|
_ ->
|
|
|
|
[?XE(<<"table">>,
|
|
|
|
[?XE(<<"thead">>,
|
|
|
|
[?XE(<<"tr">>,
|
|
|
|
[?XCT(<<"td">>, <<"Jabber ID">>),
|
|
|
|
?XCT(<<"td">>, <<"Nickname">>),
|
|
|
|
?XCT(<<"td">>, <<"Subscription">>),
|
|
|
|
?XCT(<<"td">>, <<"Pending">>),
|
|
|
|
?XCT(<<"td">>, <<"Groups">>)])]),
|
|
|
|
?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>>,
|
|
|
|
<<"Validate">>)]);
|
|
|
|
true -> ?X(<<"td">>)
|
|
|
|
end,
|
|
|
|
?XAE(<<"td">>,
|
|
|
|
[{<<"class">>,
|
|
|
|
<<"valign">>}],
|
|
|
|
[?INPUTT(<<"submit">>,
|
|
|
|
<<"remove",
|
|
|
|
(ejabberd_web_admin:term_to_id(R#roster.jid))/binary>>,
|
|
|
|
<<"Remove">>)])])
|
|
|
|
end,
|
|
|
|
SItems)))])]
|
|
|
|
end,
|
|
|
|
[?XC(<<"h1">>,
|
|
|
|
(<<(?T(<<"Roster of ">>))/binary, (us_to_list(US))/binary>>))]
|
|
|
|
++
|
|
|
|
case Res of
|
|
|
|
ok -> [?XREST(<<"Submitted">>)];
|
|
|
|
error -> [?XREST(<<"Bad format">>)];
|
|
|
|
nothing -> []
|
|
|
|
end
|
|
|
|
++
|
|
|
|
[?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
(FItems ++
|
|
|
|
[?P, ?INPUT(<<"text">>, <<"newjid">>, <<"">>),
|
|
|
|
?C(<<" ">>),
|
|
|
|
?INPUTT(<<"submit">>, <<"addjid">>,
|
|
|
|
<<"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} ->
|
|
|
|
case lists:member(CServer, ?MYHOSTS) of
|
|
|
|
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">>}],
|
2015-11-24 16:44:13 +01:00
|
|
|
(jid:to_string(RosterJID)));
|
2013-03-14 10:33:02 +01:00
|
|
|
URI when is_binary(URI) ->
|
|
|
|
?XAE(<<"td">>, [{<<"class">>, <<"valign">>}],
|
2015-11-24 16:44:13 +01:00
|
|
|
[?AC(JIDURI, (jid:to_string(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}} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:from_string(SJID) of
|
2013-03-14 10:33:02 +01:00
|
|
|
JID when is_record(JID, jid) ->
|
|
|
|
user_roster_subscribe_jid(User, Server, JID), ok;
|
|
|
|
error -> error
|
|
|
|
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) ->
|
|
|
|
out_subscription(User, Server, JID, subscribe),
|
2015-11-24 16:44:13 +01:00
|
|
|
UJID = jid:make(User, Server, <<"">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_router:route(UJID, JID,
|
|
|
|
#xmlel{name = <<"presence">>,
|
|
|
|
attrs = [{<<"type">>, <<"subscribe">>}],
|
|
|
|
children = []}).
|
|
|
|
|
|
|
|
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),
|
2013-03-14 10:33:02 +01:00
|
|
|
out_subscription(User, Server, JID1,
|
|
|
|
subscribed),
|
2015-11-24 16:44:13 +01:00
|
|
|
UJID = jid:make(User, Server, <<"">>),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_router:route(UJID, JID1,
|
|
|
|
#xmlel{name =
|
|
|
|
<<"presence">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"type">>,
|
|
|
|
<<"subscribed">>}],
|
|
|
|
children = []}),
|
|
|
|
throw(submitted);
|
|
|
|
false ->
|
|
|
|
case lists:keysearch(<<"remove",
|
|
|
|
(ejabberd_web_admin:term_to_id(JID))/binary>>,
|
|
|
|
1, Query)
|
|
|
|
of
|
|
|
|
{value, _} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
UJID = jid:make(User, Server,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">>),
|
2013-12-04 14:55:21 +01:00
|
|
|
process_iq_set(UJID, UJID,
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{type = set,
|
|
|
|
sub_el =
|
|
|
|
#xmlel{name =
|
|
|
|
<<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_ROSTER}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name
|
|
|
|
=
|
|
|
|
<<"item">>,
|
|
|
|
attrs
|
|
|
|
=
|
|
|
|
[{<<"jid">>,
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string(JID)},
|
2013-03-14 10:33:02 +01:00
|
|
|
{<<"subscription">>,
|
|
|
|
<<"remove">>}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
[]}]}}),
|
|
|
|
throw(submitted);
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Items),
|
2007-08-24 18:15:05 +02:00
|
|
|
nothing.
|
|
|
|
|
|
|
|
us_to_list({User, Server}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:to_string({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 ++
|
|
|
|
[?XE(<<"h3">>, [?ACT(<<"roster/">>, <<"Roster">>)])].
|
|
|
|
|
2014-02-26 18:01:47 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
%% Implement XEP-0321 Remote Roster Management
|
|
|
|
|
|
|
|
process_iq_manager(From, To, IQ) ->
|
|
|
|
%% Check what access is allowed for From to To
|
|
|
|
MatchDomain = From#jid.lserver,
|
|
|
|
case is_domain_managed(MatchDomain, To#jid.lserver) of
|
|
|
|
true ->
|
|
|
|
process_iq_manager2(MatchDomain, To, IQ);
|
|
|
|
false ->
|
2016-03-31 10:00:29 +02:00
|
|
|
#iq{sub_el = SubEl, lang = Lang} = IQ,
|
|
|
|
Txt = <<"Roster management is not allowed from this domain">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_BAD_REQUEST(Lang, Txt)]}
|
2014-02-26 18:01:47 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
process_iq_manager2(MatchDomain, To, IQ) ->
|
|
|
|
%% If IQ is SET, filter the input IQ
|
|
|
|
IQFiltered = maybe_filter_request(MatchDomain, IQ),
|
|
|
|
%% Call the standard function with reversed JIDs
|
|
|
|
IdInitial = IQFiltered#iq.id,
|
|
|
|
ResIQ = process_iq(To, To, IQFiltered#iq{id = <<"roster-remotely-managed">>}),
|
|
|
|
%% Filter the output IQ
|
|
|
|
filter_stanza(MatchDomain, ResIQ#iq{id = IdInitial}).
|
|
|
|
|
|
|
|
is_domain_managed(ContactHost, UserHost) ->
|
|
|
|
Managers = gen_mod:get_module_opt(UserHost, ?MODULE, managers,
|
|
|
|
fun(B) when is_list(B) -> B end,
|
|
|
|
[]),
|
|
|
|
lists:member(ContactHost, Managers).
|
|
|
|
|
|
|
|
maybe_filter_request(MatchDomain, IQ) when IQ#iq.type == set ->
|
|
|
|
filter_stanza(MatchDomain, IQ);
|
|
|
|
maybe_filter_request(_MatchDomain, IQ) ->
|
|
|
|
IQ.
|
|
|
|
|
|
|
|
filter_stanza(_MatchDomain, #iq{sub_el = []} = IQ) ->
|
|
|
|
IQ;
|
|
|
|
filter_stanza(MatchDomain, #iq{sub_el = [SubEl | _]} = IQ) ->
|
|
|
|
#iq{sub_el = SubElFiltered} = IQRes =
|
|
|
|
filter_stanza(MatchDomain, IQ#iq{sub_el = SubEl}),
|
|
|
|
IQRes#iq{sub_el = [SubElFiltered]};
|
|
|
|
filter_stanza(MatchDomain, #iq{sub_el = SubEl} = IQ) ->
|
|
|
|
#xmlel{name = Type, attrs = Attrs, children = Items} = SubEl,
|
|
|
|
ItemsFiltered = lists:filter(
|
|
|
|
fun(Item) ->
|
|
|
|
is_item_of_domain(MatchDomain, Item) end, Items),
|
|
|
|
SubElFiltered = #xmlel{name=Type, attrs = Attrs, children = ItemsFiltered},
|
|
|
|
IQ#iq{sub_el = SubElFiltered}.
|
|
|
|
|
|
|
|
is_item_of_domain(MatchDomain, #xmlel{} = El) ->
|
|
|
|
lists:any(fun(Attr) -> is_jid_of_domain(MatchDomain, Attr) end, El#xmlel.attrs);
|
|
|
|
is_item_of_domain(_MatchDomain, {xmlcdata, _}) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
is_jid_of_domain(MatchDomain, {<<"jid">>, JIDString}) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
case jid:from_string(JIDString) of
|
2014-02-26 18:01:47 +01:00
|
|
|
JID when JID#jid.lserver == MatchDomain -> true;
|
|
|
|
_ -> false
|
|
|
|
end;
|
|
|
|
is_jid_of_domain(_, _) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
process_item_attrs_managed(Item, Attrs, true) ->
|
|
|
|
process_item_attrs_ws(Item, Attrs);
|
|
|
|
process_item_attrs_managed(Item, _Attrs, false) ->
|
|
|
|
process_item_attrs(Item, _Attrs).
|
|
|
|
|
|
|
|
send_itemset_to_managers(_From, _Item, true) ->
|
|
|
|
ok;
|
|
|
|
send_itemset_to_managers(From, Item, false) ->
|
|
|
|
{_, UserHost} = Item#roster.us,
|
|
|
|
{_ContactUser, ContactHost, _ContactResource} = Item#roster.jid,
|
|
|
|
%% Check if the component is an allowed manager
|
|
|
|
IsManager = is_domain_managed(ContactHost, UserHost),
|
|
|
|
case IsManager of
|
|
|
|
true -> push_item(<<"">>, ContactHost, <<"">>, From, Item);
|
|
|
|
false -> ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_managed_from_id(<<"roster-remotely-managed">>) ->
|
|
|
|
true;
|
|
|
|
is_managed_from_id(_Id) ->
|
|
|
|
false.
|
|
|
|
|
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
|
|
|
|
|
|
|
import(LServer) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:import(LServer).
|
|
|
|
|
|
|
|
import(LServer, DBType, R) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, R).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(access) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2016-04-27 16:10:50 +02:00
|
|
|
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(managers) ->
|
|
|
|
fun (B) when is_list(B) -> B end;
|
|
|
|
mod_opt_type(store_current_id) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(versioning) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(_) ->
|
|
|
|
[access, db_type, iqdisc, managers, store_current_id,
|
|
|
|
versioning].
|