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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 13:58:05 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2002-12-11 21:57:45 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2009-08-06 23:07:18 +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).
|
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
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2, stop/1,
|
2002-12-17 21:49:45 +01:00
|
|
|
process_iq/3,
|
2003-01-22 21:40:40 +01:00
|
|
|
process_local_iq/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_user_roster/2,
|
|
|
|
get_subscription_lists/3,
|
2006-05-23 22:19:37 +02:00
|
|
|
get_in_pending_subscriptions/3,
|
|
|
|
in_subscription/6,
|
2005-04-17 20:08:34 +02:00
|
|
|
out_subscription/4,
|
|
|
|
set_items/3,
|
|
|
|
remove_user/2,
|
2007-08-24 18:15:05 +02:00
|
|
|
get_jid_info/4,
|
2008-07-11 14:48:27 +02:00
|
|
|
item_to_xml/1,
|
2007-08-24 18:15:05 +02:00
|
|
|
webadmin_page/3,
|
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
|
|
|
webadmin_user/4,
|
2009-08-06 23:07:18 +02:00
|
|
|
get_versioning_feature/2,
|
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_enabled/1]).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2008-07-21 17:29:52 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
-include("ejabberd.hrl").
|
2005-04-17 20:08:34 +02:00
|
|
|
-include("mod_roster.hrl").
|
2008-07-17 17:33:50 +02:00
|
|
|
-include("web/ejabberd_http.hrl").
|
|
|
|
-include("web/ejabberd_web_admin.hrl").
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @type rosteritem() = {roster, USJ, US, Contact_JID, Name, Subscription, Ask, Groups, Askmessage, Xs}
|
|
|
|
%% USJ = {LUser, LServer, Prepd_Contact_JID}
|
|
|
|
%% LUser = binary()
|
|
|
|
%% LServer = binary()
|
|
|
|
%% Prepd_Contact_JID = jlib:shortjid()
|
|
|
|
%% US = {LUser, LServer}
|
|
|
|
%% Contact_JID = jlib:shortjid()
|
|
|
|
%% Name = binary()
|
|
|
|
%% Subscription = none | to | from | both
|
|
|
|
%% Ask = none | out | in | both
|
|
|
|
%% Groups = [binary()]
|
|
|
|
%% Askmessage = binary()
|
|
|
|
%% Xs = [exmpp_xml:xmlel()]
|
|
|
|
|
|
|
|
%% @spec (Host, Opts) -> term()
|
|
|
|
%% Host = string()
|
|
|
|
%% Opts = list()
|
|
|
|
|
|
|
|
start(Host, Opts) when is_list(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2002-12-11 21:57:45 +01:00
|
|
|
mnesia:create_table(roster,[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, roster)}]),
|
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
|
|
|
mnesia:create_table(roster_version, [{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, roster_version)}]),
|
2005-04-17 20:08:34 +02:00
|
|
|
update_table(),
|
|
|
|
mnesia:add_table_index(roster, us),
|
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
|
|
|
mnesia:add_table_index(roster_version, us),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_get, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, get_user_roster, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_in_subscription, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, in_subscription, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_out_subscription, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, out_subscription, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_get_subscription_lists, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_subscription_lists, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_get_jid_info, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_jid_info, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(anonymous_purge_hook, HostB,
|
2007-05-12 18:28:34 +02:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(resend_subscription_requests_hook, HostB,
|
2006-05-23 22:19:37 +02:00
|
|
|
?MODULE, get_in_pending_subscriptions, 50),
|
2009-08-06 23:07:18 +02:00
|
|
|
ejabberd_hooks:add(roster_get_versioning_feature, HostB,
|
|
|
|
?MODULE, get_versioning_feature, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(webadmin_page_host, HostB,
|
2007-08-24 18:15:05 +02:00
|
|
|
?MODULE, webadmin_page, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(webadmin_user, HostB,
|
2007-08-24 18:15:05 +02:00
|
|
|
?MODULE, webadmin_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_ROSTER,
|
2003-07-21 22:01:22 +02:00
|
|
|
?MODULE, process_iq, IQDisc).
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Host) -> term()
|
|
|
|
%% Host = string()
|
|
|
|
|
|
|
|
stop(Host) when is_list(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(roster_get, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, get_user_roster, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_in_subscription, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, in_subscription, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_out_subscription, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, out_subscription, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_get_subscription_lists, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_subscription_lists, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_get_jid_info, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, get_jid_info, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(remove_user, HostB,
|
2007-05-12 18:28:34 +02:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(anonymous_purge_hook, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(resend_subscription_requests_hook, HostB,
|
2007-08-24 18:15:05 +02:00
|
|
|
?MODULE, get_in_pending_subscriptions, 50),
|
2009-08-06 23:07:18 +02:00
|
|
|
ejabberd_hooks:delete(roster_get_versioning_feature, HostB,
|
|
|
|
?MODULE, get_versioning_feature, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(webadmin_page_host, HostB,
|
2007-08-24 18:15:05 +02:00
|
|
|
?MODULE, webadmin_page, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(webadmin_user, HostB,
|
2007-08-24 18:15:05 +02:00
|
|
|
?MODULE, webadmin_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB,
|
2008-08-14 15:36:11 +02:00
|
|
|
?NS_ROSTER).
|
2004-07-10 00:34:26 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (From, To, IQ_Rec) -> IQ_Result
|
|
|
|
%% From = exmpp_jid:jid()
|
|
|
|
%% To = exmpp_jid:jid()
|
|
|
|
%% IQ_Rec = exmpp_iq:iq()
|
|
|
|
%% IQ_Result = exmpp_iq:iq()
|
2004-07-10 00:34:26 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
process_iq(From, To, IQ_Rec)
|
|
|
|
when ?IS_JID(From), ?IS_JID(To), ?IS_IQ_RECORD(IQ_Rec) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(From),
|
2005-04-17 20:08:34 +02:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true ->
|
2008-08-14 15:36:11 +02:00
|
|
|
process_local_iq(From, To, IQ_Rec);
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'item-not-found')
|
2002-12-11 21:57:45 +01:00
|
|
|
end.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (From, To, IQ_Rec) -> IQ_Result
|
|
|
|
%% From = exmpp_jid:jid()
|
|
|
|
%% To = exmpp_jid:jid()
|
|
|
|
%% IQ_Rec = exmpp_iq:iq()
|
|
|
|
%% IQ_Result = exmpp_iq:iq()
|
|
|
|
|
|
|
|
process_local_iq(From, To, #iq{type = get} = IQ_Rec)
|
|
|
|
when ?IS_JID(From), ?IS_JID(To), ?IS_IQ_RECORD(IQ_Rec) ->
|
2008-08-14 15:36:11 +02:00
|
|
|
process_iq_get(From, To, IQ_Rec);
|
2009-02-06 14:43:40 +01:00
|
|
|
process_local_iq(From, To, #iq{type = set} = IQ_Rec)
|
|
|
|
when ?IS_JID(From), ?IS_JID(To), ?IS_IQ_RECORD(IQ_Rec) ->
|
2008-08-14 15:36:11 +02:00
|
|
|
process_iq_set(From, To, IQ_Rec).
|
2003-07-21 22:01:22 +02:00
|
|
|
|
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) ->
|
|
|
|
sha:sha(term_to_binary(
|
|
|
|
lists:sort(
|
|
|
|
[R#roster{groups = lists:sort(Grs)} ||
|
|
|
|
R = #roster{groups = Grs} <- Items]))).
|
|
|
|
|
2009-08-17 19:17:34 +02:00
|
|
|
%% @spec (Host::binary()) -> true | false
|
2009-08-06 23:07:18 +02:00
|
|
|
roster_versioning_enabled(Host) ->
|
|
|
|
gen_mod:get_module_opt(binary_to_list(Host), ?MODULE, versioning, false).
|
|
|
|
|
2009-08-17 19:17:34 +02:00
|
|
|
%% @spec (Host::binary()) -> true | false
|
2009-08-06 23:07:18 +02:00
|
|
|
roster_version_on_db(Host) ->
|
|
|
|
gen_mod:get_module_opt(binary_to_list(Host), ?MODULE, store_current_id, false).
|
|
|
|
|
|
|
|
%% 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
|
|
|
|
true ->
|
2010-02-16 17:33:30 +01:00
|
|
|
Feature = exmpp_xml:element(?NS_ROSTER_VER_s, 'ver', [],
|
|
|
|
[exmpp_xml:element(?NS_ROSTER_VER_s, 'optional')]),
|
2009-08-06 23:07:18 +02:00
|
|
|
[Feature | Acc];
|
|
|
|
false -> []
|
|
|
|
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_version(LServer ,LUser) ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
case roster_version_on_db(LServer) of
|
|
|
|
true ->
|
|
|
|
case mnesia:dirty_read(roster_version, US) of
|
|
|
|
[#roster_version{version =V}] -> V;
|
|
|
|
[] -> not_found
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
roster_hash(ejabberd_hooks:run_fold(roster_get, LServer, [], [US]))
|
|
|
|
end.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (From, To, IQ_Rec) -> IQ_Result
|
|
|
|
%% From = exmpp_jid:jid()
|
|
|
|
%% To = exmpp_jid:jid()
|
|
|
|
%% IQ_Rec = exmpp_iq:iq()
|
|
|
|
%% IQ_Result = exmpp_iq:iq()
|
2009-08-17 19:17:34 +02:00
|
|
|
%% @doc 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 version IDs on db OR
|
|
|
|
%% - the roster version from client don't match current version
|
2008-08-14 15:36:11 +02:00
|
|
|
process_iq_get(From, To, IQ_Rec) ->
|
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
|
|
|
US = {_, LServer} = {exmpp_jid:prep_node(From), exmpp_jid:prep_domain(From)},
|
|
|
|
try
|
|
|
|
{ItemsToSend, VersionToSend} =
|
|
|
|
case {exmpp_xml:get_attribute_as_list(exmpp_iq:get_request(IQ_Rec), ver, not_found),
|
|
|
|
roster_versioning_enabled(LServer),
|
|
|
|
roster_version_on_db(LServer)} of
|
|
|
|
{not_found, _ , _} ->
|
|
|
|
{lists:map(fun item_to_xml/1,
|
|
|
|
ejabberd_hooks:run_fold(roster_get, exmpp_jid:prep_domain(To), [], [US])), false};
|
|
|
|
{_, false, _} ->
|
|
|
|
{lists:map(fun item_to_xml/1,
|
|
|
|
ejabberd_hooks:run_fold(roster_get, exmpp_jid:prep_domain(To), [], [US])), false};
|
|
|
|
|
|
|
|
{RequestedVersion, true, true} ->
|
|
|
|
%% Retrieve version from DB. Only load entire roster
|
|
|
|
%% when neccesary.
|
|
|
|
case mnesia:dirty_read(roster_version, US) of
|
|
|
|
[#roster_version{version = RequestedVersion}] ->
|
|
|
|
{false, false};
|
|
|
|
[#roster_version{version = NewVersion}] ->
|
|
|
|
{lists:map(fun item_to_xml/1,
|
|
|
|
ejabberd_hooks:run_fold(roster_get, exmpp_jid:prep_domain(To), [], [US])), NewVersion};
|
|
|
|
[] ->
|
|
|
|
RosterVersion = sha:sha(term_to_binary(now())),
|
|
|
|
mnesia:dirty_write(#roster_version{us = US, version = RosterVersion}),
|
|
|
|
{lists:map(fun item_to_xml/1,
|
|
|
|
ejabberd_hooks:run_fold(roster_get, exmpp_jid:prep_domain(To), [], [US])), RosterVersion}
|
|
|
|
end;
|
|
|
|
{RequestedVersion, true, false} ->
|
|
|
|
RosterItems = ejabberd_hooks:run_fold(roster_get, exmpp_jid:prep_domain(To), [] , [US]),
|
|
|
|
case roster_hash(RosterItems) of
|
|
|
|
RequestedVersion ->
|
|
|
|
{false, false};
|
|
|
|
New ->
|
|
|
|
{lists:map(fun item_to_xml/1, RosterItems), New}
|
|
|
|
end
|
|
|
|
|
|
|
|
end,
|
|
|
|
case {ItemsToSend, VersionToSend} of
|
|
|
|
{false, false} ->
|
|
|
|
exmpp_iq:result(IQ_Rec);
|
|
|
|
{Items, false} ->
|
|
|
|
exmpp_iq:result(IQ_Rec, exmpp_xml:element(?NS_ROSTER, 'query', [] , Items));
|
|
|
|
{Items, Version} ->
|
|
|
|
exmpp_iq:result(IQ_Rec, exmpp_xml:element(?NS_ROSTER, 'query', [?XMLATTR('ver', Version)], Items))
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_:_ ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'internal-server-error')
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Acc, US) -> New_Acc
|
|
|
|
%% Acc = [rosteritem()]
|
|
|
|
%% US = {User, Server}
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% New_Acc = [rosteritem()]
|
|
|
|
|
|
|
|
get_user_roster(Acc, {U, S} = US) when is_binary(U), is_binary(S) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case catch mnesia:dirty_index_read(roster, US, #roster.us) of
|
|
|
|
Items when is_list(Items) ->
|
2007-04-26 16:45:16 +02:00
|
|
|
lists:filter(fun(#roster{subscription = none, ask = in}) ->
|
|
|
|
false;
|
|
|
|
(_) ->
|
|
|
|
true
|
|
|
|
end, Items) ++ Acc;
|
2005-04-17 20:08:34 +02:00
|
|
|
_ ->
|
|
|
|
Acc
|
|
|
|
end.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Item) -> XML
|
|
|
|
%% Item = rosteritem()
|
|
|
|
%% XML = exmpp_xml:xmlel()
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2002-12-13 21:58:27 +01:00
|
|
|
item_to_xml(Item) ->
|
2008-07-21 17:29:52 +02:00
|
|
|
{U, S, R} = Item#roster.jid,
|
|
|
|
Attrs1 = exmpp_xml:set_attribute_in_list([],
|
2009-06-01 18:59:08 +02:00
|
|
|
'jid', exmpp_jid:to_binary(U, S, R)),
|
2002-12-13 21:58:27 +01:00
|
|
|
Attrs2 = case Item#roster.name of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<>> ->
|
2002-12-13 21:58:27 +01:00
|
|
|
Attrs1;
|
|
|
|
Name ->
|
2008-07-21 17:29:52 +02:00
|
|
|
exmpp_xml:set_attribute_in_list(Attrs1, 'name', Name)
|
2002-12-13 21:58:27 +01:00
|
|
|
end,
|
2008-07-21 17:29:52 +02:00
|
|
|
Attrs3 = exmpp_xml:set_attribute_in_list(Attrs2,
|
|
|
|
'subscription', Item#roster.subscription),
|
2003-12-11 21:31:40 +01:00
|
|
|
Attrs4 = case ask_to_pending(Item#roster.ask) of
|
|
|
|
out ->
|
2008-07-21 17:29:52 +02:00
|
|
|
exmpp_xml:set_attribute_in_list(Attrs3,
|
2009-02-23 20:45:55 +01:00
|
|
|
'ask', <<"subscribe">>);
|
2003-12-11 21:31:40 +01:00
|
|
|
both ->
|
2008-07-21 17:29:52 +02:00
|
|
|
exmpp_xml:set_attribute_in_list(Attrs3,
|
2009-02-23 20:45:55 +01:00
|
|
|
'ask', <<"subscribe">>);
|
2003-12-11 21:31:40 +01:00
|
|
|
_ ->
|
|
|
|
Attrs3
|
2002-12-20 21:42:08 +01:00
|
|
|
end,
|
2002-12-13 21:58:27 +01:00
|
|
|
SubEls1 = lists:map(fun(G) ->
|
2008-07-21 17:29:52 +02:00
|
|
|
exmpp_xml:set_cdata(
|
|
|
|
#xmlel{ns = ?NS_ROSTER, name = 'group'}, G)
|
2002-12-13 21:58:27 +01:00
|
|
|
end, Item#roster.groups),
|
|
|
|
SubEls = SubEls1 ++ Item#roster.xs,
|
2008-07-21 17:29:52 +02:00
|
|
|
#xmlel{ns = ?NS_ROSTER, name = 'item', attrs = Attrs4, children = SubEls}.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (From, To, IQ_Rec) -> IQ_Result
|
|
|
|
%% From = exmpp_jid:jid()
|
|
|
|
%% To = exmpp_jid:jid()
|
|
|
|
%% IQ_Rec = exmpp_iq:iq()
|
|
|
|
%% IQ_Result = exmpp_iq:iq()
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_iq_set(From, To, #iq{payload = Request} = IQ_Rec) ->
|
|
|
|
case Request of
|
2008-07-21 17:29:52 +02:00
|
|
|
#xmlel{children = Els} ->
|
|
|
|
lists:foreach(fun(El) -> process_item_set(From, To, El) end, Els);
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2008-07-21 17:29:52 +02:00
|
|
|
ok
|
|
|
|
end,
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:result(IQ_Rec).
|
2008-07-21 17:29:52 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (From, To, El) -> ok
|
|
|
|
%% From = exmpp_jid:jid()
|
|
|
|
%% To = exmpp_jid:jid()
|
|
|
|
%% El = exmpp_xml:xmlel()
|
|
|
|
|
2008-08-26 15:59:04 +02:00
|
|
|
process_item_set(From, To, #xmlel{} = El) ->
|
2008-07-21 17:29:52 +02:00
|
|
|
try
|
2009-06-01 18:35:55 +02:00
|
|
|
JID1 = exmpp_jid:parse(exmpp_xml:get_attribute_as_binary(El, 'jid', <<>>)),
|
2009-02-06 14:43:40 +01:00
|
|
|
User = exmpp_jid:node(From),
|
2009-06-01 18:39:36 +02:00
|
|
|
LUser = exmpp_jid:prep_node(From),
|
2009-06-01 18:37:15 +02:00
|
|
|
LServer = exmpp_jid:prep_domain(From),
|
2008-08-26 15:59:04 +02:00
|
|
|
JID = jlib:short_jid(JID1),
|
|
|
|
LJID = jlib:short_prepd_jid(JID1),
|
2008-07-21 17:29:52 +02:00
|
|
|
F = fun() ->
|
|
|
|
Res = mnesia:read({roster, {LUser, LServer, LJID}}),
|
|
|
|
Item = case Res of
|
|
|
|
[] ->
|
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
|
|
|
jid = JID};
|
|
|
|
[I] ->
|
|
|
|
I#roster{jid = JID,
|
2009-01-08 15:54:00 +01:00
|
|
|
name = <<>>,
|
2008-07-21 17:29:52 +02:00
|
|
|
groups = [],
|
|
|
|
xs = []}
|
|
|
|
end,
|
2008-08-26 15:59:04 +02:00
|
|
|
Item1 = process_item_attrs(Item, El#xmlel.attrs),
|
|
|
|
Item2 = process_item_els(Item1, El#xmlel.children),
|
2008-07-21 17:29:52 +02:00
|
|
|
case Item2#roster.subscription of
|
2003-01-17 20:58:42 +01:00
|
|
|
remove ->
|
2009-08-05 19:39:47 +02:00
|
|
|
mnesia:delete({roster, {LUser, LServer, LJID}});
|
2003-01-17 20:58:42 +01:00
|
|
|
_ ->
|
2008-07-21 17:29:52 +02:00
|
|
|
mnesia:write(Item2)
|
|
|
|
end,
|
|
|
|
%% If the item exist in shared roster, take the
|
|
|
|
%% subscription information from there:
|
|
|
|
Item3 = ejabberd_hooks:run_fold(roster_process_item,
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(From), Item2, [exmpp_jid:prep_domain(From)]),
|
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_version_on_db(LServer) of
|
|
|
|
true -> mnesia:write(#roster_version{us = {LUser, LServer}, version = sha:sha(term_to_binary(now()))});
|
|
|
|
false -> ok
|
|
|
|
end,
|
2008-07-21 17:29:52 +02:00
|
|
|
{Item, Item3}
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, {OldItem, Item}} ->
|
|
|
|
push_item(User, LServer, To, Item),
|
|
|
|
case Item#roster.subscription of
|
|
|
|
remove ->
|
|
|
|
IsTo = case OldItem#roster.subscription of
|
|
|
|
both -> true;
|
|
|
|
to -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
IsFrom = case OldItem#roster.subscription of
|
|
|
|
both -> true;
|
|
|
|
from -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
{U, S, R} = OldItem#roster.jid,
|
|
|
|
if IsTo ->
|
|
|
|
ejabberd_router:route(
|
2009-06-01 18:30:15 +02:00
|
|
|
exmpp_jid:bare(From),
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(U, S, R),
|
2008-07-21 17:29:52 +02:00
|
|
|
exmpp_presence:unsubscribe());
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
if IsFrom ->
|
|
|
|
ejabberd_router:route(
|
2009-06-01 18:30:15 +02:00
|
|
|
exmpp_jid:bare(From),
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(U, S, R),
|
2008-07-21 17:29:52 +02:00
|
|
|
exmpp_presence:unsubscribed());
|
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
E ->
|
|
|
|
?DEBUG("ROSTER: roster item set error: ~p~n", [E]),
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
2003-02-20 18:12:03 +01:00
|
|
|
end;
|
2003-10-27 20:35:03 +01:00
|
|
|
process_item_set(_From, _To, _) ->
|
2003-02-20 18:12:03 +01:00
|
|
|
ok.
|
2002-12-13 21:58:27 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Item, Attrs) -> New_Item
|
|
|
|
%% Item = rosteritem()
|
|
|
|
%% Attrs = [exmpp_xml:xmlnsattribute()]
|
|
|
|
%% New_Item = rosteritem()
|
|
|
|
|
2008-07-21 17:29:52 +02:00
|
|
|
process_item_attrs(Item, [#xmlattr{name = Attr, value = Val} | Attrs]) ->
|
2002-12-13 21:58:27 +01:00
|
|
|
case Attr of
|
2008-07-21 17:29:52 +02:00
|
|
|
'name' ->
|
2002-12-14 21:07:26 +01:00
|
|
|
process_item_attrs(Item#roster{name = Val}, Attrs);
|
2008-07-21 17:29:52 +02:00
|
|
|
'subscription' ->
|
2002-12-13 21:58:27 +01:00
|
|
|
case Val of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<"remove">> ->
|
2002-12-13 21:58:27 +01:00
|
|
|
process_item_attrs(Item#roster{subscription = remove},
|
2002-12-14 21:07:26 +01:00
|
|
|
Attrs);
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2002-12-14 21:07:26 +01:00
|
|
|
process_item_attrs(Item, Attrs)
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
2008-07-21 17:29:52 +02:00
|
|
|
'ask' ->
|
2002-12-14 21:07:26 +01:00
|
|
|
process_item_attrs(Item, Attrs);
|
2002-12-13 21:58:27 +01:00
|
|
|
_ ->
|
2006-05-23 22:19:37 +02:00
|
|
|
process_item_attrs(Item, Attrs)
|
2002-12-13 21:58:27 +01:00
|
|
|
end;
|
|
|
|
process_item_attrs(Item, []) ->
|
|
|
|
Item.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Item, Els) -> New_Item
|
|
|
|
%% Item = rosteritem()
|
|
|
|
%% Els = [exmpp_xml:xmlel()]
|
|
|
|
%% New_Item = rosteritem()
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2008-07-21 17:29:52 +02:00
|
|
|
process_item_els(Item, [#xmlel{ns = NS, name = Name} = El | Els]) ->
|
2002-12-13 21:58:27 +01:00
|
|
|
case Name of
|
2008-07-21 17:29:52 +02:00
|
|
|
'group' ->
|
|
|
|
Groups = [exmpp_xml:get_cdata(El) | Item#roster.groups],
|
2002-12-13 21:58:27 +01:00
|
|
|
process_item_els(Item#roster{groups = Groups}, Els);
|
|
|
|
_ ->
|
2008-07-21 17:29:52 +02:00
|
|
|
if
|
|
|
|
NS == ?NS_JABBER_CLIENT; NS == ?NS_JABBER_SERVER ->
|
2002-12-13 21:58:27 +01:00
|
|
|
process_item_els(Item, Els);
|
2008-07-21 17:29:52 +02:00
|
|
|
true ->
|
|
|
|
XEls = [El | Item#roster.xs],
|
2002-12-13 21:58:27 +01:00
|
|
|
process_item_els(Item#roster{xs = XEls}, Els)
|
|
|
|
end
|
|
|
|
end;
|
2008-07-21 17:29:52 +02:00
|
|
|
process_item_els(Item, [_ | Els]) ->
|
2002-12-13 21:58:27 +01:00
|
|
|
process_item_els(Item, Els);
|
|
|
|
process_item_els(Item, []) ->
|
|
|
|
Item.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server, From, Item) -> term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% From = exmpp_jid:jid()
|
|
|
|
%% Item = rosteritem()
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
push_item(User, Server, From, Item)
|
|
|
|
when is_binary(User), is_binary(Server), ?IS_JID(From) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
ejabberd_sm:route(exmpp_jid:make(),
|
|
|
|
exmpp_jid:make(User, Server),
|
2008-07-21 17:29:52 +02:00
|
|
|
#xmlel{name = 'broadcast', children =
|
2004-12-19 21:47:35 +01:00
|
|
|
[{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
|
|
|
|
true ->
|
2009-08-06 23:07:18 +02:00
|
|
|
push_item_version(Server, User, From, Item, roster_version(Server, User));
|
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
|
|
|
false ->
|
|
|
|
lists:foreach(fun(Resource) ->
|
|
|
|
push_item(User, Server, Resource, From, Item)
|
|
|
|
end, ejabberd_sm:get_user_resources(User, Server))
|
|
|
|
end.
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server, Resource, From, Item) -> term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% Resource = binary()
|
|
|
|
%% From = exmpp_jid:jid()
|
|
|
|
%% Item = rosteritem()
|
|
|
|
|
2005-11-22 19:43:06 +01:00
|
|
|
% TODO: don't push to those who didn't load roster
|
2009-02-06 14:43:40 +01:00
|
|
|
push_item(User, Server, Resource, From, Item)
|
|
|
|
when is_binary(User), is_binary(Server), is_binary(Resource),
|
|
|
|
?IS_JID(From) ->
|
2008-07-21 17:29:52 +02:00
|
|
|
Request = #xmlel{ns = ?NS_ROSTER, name = 'query',
|
2008-09-29 13:30:25 +02:00
|
|
|
children = [item_to_xml(Item)]},
|
2008-12-01 16:53:30 +01:00
|
|
|
ResIQ = exmpp_iq:set(?NS_JABBER_CLIENT, Request,
|
|
|
|
"push" ++ randoms:get_string()),
|
2003-12-17 21:13:21 +01:00
|
|
|
ejabberd_router:route(
|
|
|
|
From,
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(User, Server, Resource),
|
2008-07-21 17:29:52 +02:00
|
|
|
ResIQ).
|
2002-12-14 21:07:26 +01:00
|
|
|
|
2009-08-06 23:07:18 +02:00
|
|
|
%% @doc Roster push, calculate and include the version attribute.
|
|
|
|
%% TODO: don't push to those who didn't load roster
|
|
|
|
push_item_version(Server, User, From, Item, RosterVersion) ->
|
|
|
|
lists:foreach(fun(Resource) ->
|
|
|
|
push_item_version(User, Server, Resource, From, Item, RosterVersion)
|
|
|
|
end, ejabberd_sm:get_user_resources(User, Server)).
|
|
|
|
|
|
|
|
push_item_version(User, Server, Resource, From, Item, RosterVersion) ->
|
|
|
|
Request = #xmlel{ns = ?NS_ROSTER, name = 'query', attrs = [?XMLATTR('ver', RosterVersion)],
|
|
|
|
children = [mod_roster:item_to_xml(Item)]},
|
|
|
|
ResIQ = exmpp_iq:set(?NS_JABBER_CLIENT, Request,
|
|
|
|
"push" ++ randoms:get_string()),
|
|
|
|
ejabberd_router:route(
|
|
|
|
From,
|
|
|
|
exmpp_jid:make(User, Server, Resource),
|
|
|
|
ResIQ).
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Ignored, User, Server) -> Subscription_Lists
|
|
|
|
%% Ignored = term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% Subscription_Lists = {F, T}
|
|
|
|
%% F = [jlib:shortjid()]
|
|
|
|
%% T = [jlib:shortjid()]
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_subscription_lists(_, User, Server)
|
2009-02-06 14:43:40 +01:00
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
try
|
2009-01-03 16:15:38 +01:00
|
|
|
US = {User,Server},
|
2008-08-26 15:59:04 +02:00
|
|
|
case mnesia:dirty_index_read(roster, US, #roster.us) of
|
|
|
|
Items when is_list(Items) ->
|
|
|
|
fill_subscription_lists(Items, [], []);
|
|
|
|
_ ->
|
|
|
|
{[], []}
|
|
|
|
end
|
|
|
|
catch
|
2002-12-17 21:49:45 +01:00
|
|
|
_ ->
|
|
|
|
{[], []}
|
|
|
|
end.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Items, F, T) -> {New_F, New_T}
|
|
|
|
%% Items = [rosteritem()]
|
|
|
|
%% F = [jlib:shortjid()]
|
|
|
|
%% T = [jlib:shortjid()]
|
|
|
|
%% New_F = [jlib:shortjid()]
|
|
|
|
%% New_T = [jlib:shortjid()]
|
|
|
|
|
2002-12-17 21:49:45 +01:00
|
|
|
fill_subscription_lists([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
|
|
|
|
both ->
|
|
|
|
fill_subscription_lists(Is, [J | F], [J | T]);
|
|
|
|
from ->
|
|
|
|
fill_subscription_lists(Is, [J | F], T);
|
|
|
|
to ->
|
|
|
|
fill_subscription_lists(Is, F, [J | T]);
|
|
|
|
_ ->
|
|
|
|
fill_subscription_lists(Is, F, T)
|
|
|
|
end;
|
|
|
|
fill_subscription_lists([], F, T) ->
|
|
|
|
{F, T}.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
|
|
|
|
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
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Ignored, User, Server, JID, Type, Reason) -> bool()
|
|
|
|
%% Ignored = term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% JID = exmpp_jid:jid()
|
|
|
|
%% Type = subscribe | subscribed | unsubscribe | unsubscribed
|
|
|
|
%% Reason = binary() | undefined
|
2002-12-20 21:42:08 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
in_subscription(_, User, Server, JID, Type, Reason)
|
|
|
|
when is_binary(User), is_binary(Server), ?IS_JID(JID) ->
|
2006-05-23 22:19:37 +02:00
|
|
|
process_subscription(in, User, Server, JID, Type, Reason).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server, JID, Type) -> bool()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% JID = exmpp_jid:jid()
|
|
|
|
%% Type = subscribe | subscribed | unsubscribe | unsubscribed
|
|
|
|
|
|
|
|
out_subscription(User, Server, JID, Type)
|
|
|
|
when is_binary(User), is_binary(Server), ?IS_JID(JID) ->
|
2008-10-06 17:16:09 +02:00
|
|
|
process_subscription(out, User, Server, JID, Type, <<>>).
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Direction, User, Server, JID1, Type, Reason) -> bool()
|
|
|
|
%% Direction = in | out
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% JID1 = exmpp_jid:jid()
|
|
|
|
%% Type = subscribe | subscribed | unsubscribe | unsubscribed
|
|
|
|
%% Reason = binary() | undefined
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
process_subscription(Direction, User, Server, JID1, Type, Reason)
|
2009-02-06 14:43:40 +01:00
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
try
|
2009-01-03 16:15:38 +01:00
|
|
|
US = {User, Server},
|
2008-08-26 15:59:04 +02:00
|
|
|
LJID = jlib:short_prepd_jid(JID1),
|
|
|
|
F = fun() ->
|
2009-01-03 16:15:38 +01:00
|
|
|
Item = case mnesia:read({roster, {User, Server, LJID}}) of
|
2008-08-26 15:59:04 +02:00
|
|
|
[] ->
|
|
|
|
JID = jlib:short_jid(JID1),
|
2009-01-03 16:15:38 +01:00
|
|
|
#roster{usj = {User, Server, LJID},
|
2008-08-26 15:59:04 +02:00
|
|
|
us = US,
|
|
|
|
jid = JID};
|
|
|
|
[I] ->
|
|
|
|
I
|
2003-12-11 21:31:40 +01:00
|
|
|
end,
|
2008-08-26 15:59:04 +02:00
|
|
|
NewState = case Direction of
|
|
|
|
out ->
|
|
|
|
out_state_change(Item#roster.subscription,
|
|
|
|
Item#roster.ask,
|
|
|
|
Type);
|
|
|
|
in ->
|
|
|
|
in_state_change(Item#roster.subscription,
|
|
|
|
Item#roster.ask,
|
|
|
|
Type)
|
|
|
|
end,
|
|
|
|
AutoReply = case Direction of
|
|
|
|
out ->
|
|
|
|
none;
|
|
|
|
in ->
|
|
|
|
in_auto_reply(Item#roster.subscription,
|
|
|
|
Item#roster.ask,
|
|
|
|
Type)
|
|
|
|
end,
|
|
|
|
AskMessage = case NewState of
|
|
|
|
{_, both} -> Reason;
|
|
|
|
{_, in} -> Reason;
|
2008-10-06 17:16:09 +02:00
|
|
|
_ -> <<>>
|
2008-08-26 15:59:04 +02:00
|
|
|
end,
|
|
|
|
case NewState of
|
|
|
|
none ->
|
|
|
|
{none, AutoReply};
|
|
|
|
{none, none} when Item#roster.subscription == none,
|
|
|
|
Item#roster.ask == in ->
|
2009-01-03 16:15:38 +01:00
|
|
|
mnesia:delete({roster, {User, Server, LJID}}),
|
2008-08-26 15:59:04 +02:00
|
|
|
{none, AutoReply};
|
|
|
|
{Subscription, Pending} ->
|
2008-10-06 17:16:09 +02:00
|
|
|
AskBinary = case AskMessage of
|
|
|
|
undefined -> <<>>;
|
|
|
|
B -> B
|
|
|
|
end,
|
2008-08-26 15:59:04 +02:00
|
|
|
NewItem = Item#roster{subscription = Subscription,
|
|
|
|
ask = Pending,
|
2008-10-06 17:16:09 +02:00
|
|
|
askmessage = AskBinary},
|
2008-08-26 15:59:04 +02:00
|
|
|
mnesia:write(NewItem),
|
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_version_on_db(Server) of
|
|
|
|
true -> mnesia:write(#roster_version{us = {User, Server}, version = sha:sha(term_to_binary(now()))});
|
|
|
|
false -> ok
|
|
|
|
end,
|
2008-08-26 15:59:04 +02:00
|
|
|
{{push, NewItem}, AutoReply}
|
|
|
|
end
|
2004-01-18 21:42:09 +01:00
|
|
|
end,
|
2008-08-26 15:59:04 +02:00
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, {Push, AutoReply}} ->
|
|
|
|
case AutoReply of
|
|
|
|
none ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(User, Server), JID1,
|
2008-08-26 15:59:04 +02:00
|
|
|
exmpp_presence:AutoReply())
|
|
|
|
end,
|
|
|
|
case Push of
|
|
|
|
{push, Item} ->
|
|
|
|
if
|
|
|
|
Item#roster.subscription == none,
|
|
|
|
Item#roster.ask == in ->
|
|
|
|
ok;
|
|
|
|
true ->
|
|
|
|
push_item(User, Server,
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(User, Server), Item)
|
2008-08-26 15:59:04 +02:00
|
|
|
end,
|
|
|
|
true;
|
|
|
|
none ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
catch
|
2002-12-18 21:26:08 +01:00
|
|
|
_ ->
|
2002-12-20 21:42:08 +01:00
|
|
|
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).
|
|
|
|
-define(NNSD, {to, none}).
|
|
|
|
-define(NISD, {to, in}).
|
|
|
|
-else.
|
|
|
|
-define(NNSD, none).
|
|
|
|
-define(NISD, none).
|
|
|
|
-endif.
|
2003-12-11 21:31:40 +01:00
|
|
|
|
|
|
|
in_state_change(none, none, subscribe) -> {none, in};
|
2005-12-11 20:48:31 +01:00
|
|
|
in_state_change(none, none, subscribed) -> ?NNSD;
|
2003-12-11 21:31:40 +01:00
|
|
|
in_state_change(none, none, unsubscribe) -> none;
|
|
|
|
in_state_change(none, none, unsubscribed) -> none;
|
|
|
|
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;
|
2005-12-11 20:48:31 +01:00
|
|
|
in_state_change(none, in, subscribed) -> ?NISD;
|
2003-12-11 21:31:40 +01:00
|
|
|
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};
|
|
|
|
in_state_change(none, both, unsubscribed) -> {none, in};
|
|
|
|
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;
|
2010-02-24 13:14:45 +01:00
|
|
|
in_state_change(from, none, subscribed) -> {both, none};
|
2003-12-11 21:31:40 +01:00
|
|
|
in_state_change(from, none, unsubscribe) -> {none, none};
|
|
|
|
in_state_change(from, none, unsubscribed) -> none;
|
|
|
|
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;
|
|
|
|
out_state_change(none, none, unsubscribed) -> none;
|
2006-05-29 18:48:20 +02:00
|
|
|
out_state_change(none, out, subscribe) -> {none, out}; %% We need to resend query (RFC3921, section 9.2)
|
2003-12-11 21:31:40 +01:00
|
|
|
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;
|
2010-02-24 13:14:45 +01:00
|
|
|
out_state_change(to, none, subscribed) -> {both, none};
|
2003-12-11 21:31:40 +01:00
|
|
|
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}.
|
|
|
|
|
2004-01-18 21:42:09 +01:00
|
|
|
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.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server) -> term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
2003-12-11 21:31:40 +01:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
remove_user(User, Server)
|
2009-02-06 14:43:40 +01:00
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
try
|
2009-02-21 19:07:37 +01:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
2008-08-26 15:59:04 +02:00
|
|
|
US = {LUser, LServer},
|
2009-03-03 19:57:47 +01:00
|
|
|
send_unsubscription_to_rosteritems(LUser, LServer),
|
2008-08-26 15:59:04 +02:00
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(fun(R) ->
|
|
|
|
mnesia:delete_object(R)
|
|
|
|
end,
|
|
|
|
mnesia:index_read(roster, US, #roster.us))
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-01-26 21:16:53 +01:00
|
|
|
|
2009-03-03 19:57:47 +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}),
|
2009-06-02 01:44:55 +02:00
|
|
|
From = exmpp_jid:make(LUser, LServer, ""),
|
2009-03-03 19:57:47 +01:00
|
|
|
lists:foreach(fun(RosterItem) ->
|
|
|
|
send_unsubscribing_presence(From, RosterItem)
|
|
|
|
end,
|
|
|
|
RosterItems).
|
|
|
|
|
|
|
|
%% @spec (From::jid(), Item::roster()) -> ok
|
|
|
|
send_unsubscribing_presence(From, Item) ->
|
|
|
|
IsTo = case Item#roster.subscription of
|
|
|
|
both -> true;
|
|
|
|
to -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
IsFrom = case Item#roster.subscription of
|
|
|
|
both -> true;
|
|
|
|
from -> true;
|
|
|
|
_ -> false
|
|
|
|
end,
|
2009-08-05 19:39:47 +02:00
|
|
|
{INode, IDom, IRes} = Item#roster.jid,
|
|
|
|
SendToJID = exmpp_jid:make(INode, IDom, IRes),
|
2009-03-03 19:57:47 +01:00
|
|
|
if IsTo ->
|
2009-08-05 19:39:47 +02:00
|
|
|
ejabberd_router:route(
|
|
|
|
exmpp_jid:bare(From),
|
|
|
|
SendToJID,
|
|
|
|
exmpp_presence:unsubscribe());
|
2009-03-03 19:57:47 +01:00
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
if IsFrom ->
|
2009-08-05 19:39:47 +02:00
|
|
|
ejabberd_router:route(
|
|
|
|
exmpp_jid:bare(From),
|
|
|
|
SendToJID,
|
|
|
|
exmpp_presence:unsubscribed());
|
2009-03-03 19:57:47 +01:00
|
|
|
true -> ok
|
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-02-02 20:49:19 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server, El) -> term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% El = exmpp_xml:xmlel()
|
|
|
|
|
|
|
|
set_items(User, Server, #xmlel{children = Els})
|
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(fun(El) ->
|
|
|
|
process_item_set_t(LUser, LServer, El)
|
|
|
|
end, Els)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (LUser, LServer, El) -> term()
|
|
|
|
%% LUser = binary()
|
|
|
|
%% LServer = binary()
|
|
|
|
%% El = exmpp_xml:xmlel()
|
|
|
|
|
2008-07-21 17:29:52 +02:00
|
|
|
process_item_set_t(LUser, LServer, #xmlel{} = El) ->
|
|
|
|
try
|
2009-06-01 18:35:55 +02:00
|
|
|
JID1 = exmpp_jid:parse(exmpp_xml:get_attribute_as_list(El, 'jid', <<>>)),
|
2008-08-26 15:59:04 +02:00
|
|
|
JID = jlib:short_jid(JID1),
|
|
|
|
LJID = jlib:short_prepd_jid(JID1),
|
2008-07-21 17:29:52 +02:00
|
|
|
Item = #roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer},
|
|
|
|
jid = JID},
|
|
|
|
Item1 = process_item_attrs_ws(Item, El#xmlel.attrs),
|
|
|
|
Item2 = process_item_els(Item1, El#xmlel.children),
|
|
|
|
case Item2#roster.subscription of
|
|
|
|
remove ->
|
|
|
|
mnesia:delete({roster, {LUser, LServer, LJID}});
|
|
|
|
_ ->
|
|
|
|
mnesia:write(Item2)
|
|
|
|
end
|
|
|
|
catch
|
2003-02-02 20:49:19 +01:00
|
|
|
_ ->
|
2008-07-21 17:29:52 +02:00
|
|
|
ok
|
2003-02-20 18:12:03 +01:00
|
|
|
end;
|
2005-04-17 20:08:34 +02:00
|
|
|
process_item_set_t(_LUser, _LServer, _) ->
|
2003-02-20 18:12:03 +01:00
|
|
|
ok.
|
2003-02-02 20:49:19 +01:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Item, Attrs) -> New_Item
|
|
|
|
%% Item = rosteritem()
|
|
|
|
%% Attrs = [exmpp_xml:xmlnsattribute()]
|
|
|
|
%% New_Item = rosteritem()
|
|
|
|
|
2008-07-21 17:29:52 +02:00
|
|
|
process_item_attrs_ws(Item, [#xmlattr{name = Attr, value = Val} | Attrs]) ->
|
2003-02-02 20:49:19 +01:00
|
|
|
case Attr of
|
2008-07-21 17:29:52 +02:00
|
|
|
'name' ->
|
2003-02-02 20:49:19 +01:00
|
|
|
process_item_attrs_ws(Item#roster{name = Val}, Attrs);
|
2008-07-21 17:29:52 +02:00
|
|
|
'subscription' ->
|
2003-02-02 20:49:19 +01:00
|
|
|
case Val of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<"remove">> ->
|
2003-02-02 20:49:19 +01:00
|
|
|
process_item_attrs_ws(Item#roster{subscription = remove},
|
|
|
|
Attrs);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<"none">> ->
|
2003-02-02 20:49:19 +01:00
|
|
|
process_item_attrs_ws(Item#roster{subscription = none},
|
|
|
|
Attrs);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<"both">> ->
|
2003-02-02 20:49:19 +01:00
|
|
|
process_item_attrs_ws(Item#roster{subscription = both},
|
|
|
|
Attrs);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<"from">> ->
|
2003-02-02 20:49:19 +01:00
|
|
|
process_item_attrs_ws(Item#roster{subscription = from},
|
|
|
|
Attrs);
|
2009-01-08 15:54:00 +01:00
|
|
|
<<"to">> ->
|
2003-02-02 20:49:19 +01:00
|
|
|
process_item_attrs_ws(Item#roster{subscription = to},
|
|
|
|
Attrs);
|
|
|
|
_ ->
|
|
|
|
process_item_attrs_ws(Item, Attrs)
|
|
|
|
end;
|
2008-07-21 17:29:52 +02:00
|
|
|
'ask' ->
|
2003-02-02 20:49:19 +01:00
|
|
|
process_item_attrs_ws(Item, Attrs);
|
|
|
|
_ ->
|
2006-05-23 22:19:37 +02:00
|
|
|
process_item_attrs_ws(Item, Attrs)
|
2003-02-02 20:49:19 +01:00
|
|
|
end;
|
|
|
|
process_item_attrs_ws(Item, []) ->
|
|
|
|
Item.
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Ls, User, Server) -> New_Ls
|
|
|
|
%% Ls = [exmpp_xml:xmlel()]
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% New_Ls = [exmpp_xml:xmlel()]
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_in_pending_subscriptions(Ls, User, Server)
|
2009-02-06 14:43:40 +01:00
|
|
|
when is_binary(User), is_binary(Server) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
JID = exmpp_jid:make(User, Server),
|
2009-06-01 18:39:36 +02:00
|
|
|
US = {exmpp_jid:prep_node(JID), exmpp_jid:prep_domain(JID)},
|
2006-05-26 02:00:32 +02:00
|
|
|
case mnesia:dirty_index_read(roster, US, #roster.us) of
|
2009-05-06 18:54:43 +02:00
|
|
|
Result when is_list(Result) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
Ls ++ lists:map(
|
2006-05-26 02:00:32 +02:00
|
|
|
fun(R) ->
|
|
|
|
Message = R#roster.askmessage,
|
2008-10-06 17:16:09 +02:00
|
|
|
{U0, S0, R0} = R#roster.jid,
|
2008-07-21 17:29:52 +02:00
|
|
|
Pres1 = exmpp_presence:subscribe(),
|
2008-09-18 16:55:43 +02:00
|
|
|
Pres2 = exmpp_stanza:set_jids(Pres1,
|
2009-06-01 18:59:08 +02:00
|
|
|
exmpp_jid:to_binary(U0, S0, R0),
|
|
|
|
exmpp_jid:to_binary(JID)),
|
2008-08-26 15:59:04 +02:00
|
|
|
exmpp_presence:set_status(Pres2, Message)
|
2006-05-26 02:00:32 +02:00
|
|
|
end,
|
|
|
|
lists:filter(
|
|
|
|
fun(R) ->
|
|
|
|
case R#roster.ask of
|
|
|
|
in -> true;
|
|
|
|
both -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Result));
|
|
|
|
_ ->
|
|
|
|
Ls
|
2006-05-23 22:19:37 +02:00
|
|
|
end.
|
|
|
|
|
2003-08-03 21:09:40 +02:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Ignored, User, Server, JID) -> {Subscription, Groups}
|
|
|
|
%% Ignored = term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% JID = exmpp_jid:jid()
|
|
|
|
%% Subscription = none | to | from | both
|
|
|
|
%% Groups = [binary()]
|
|
|
|
|
|
|
|
get_jid_info(_, User, Server, JID)
|
|
|
|
when is_binary(User), is_binary(Server), ?IS_JID(JID) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
try
|
|
|
|
LJID = jlib:short_prepd_jid(JID),
|
2009-01-03 16:15:38 +01:00
|
|
|
case catch mnesia:dirty_read(roster, {User, Server, LJID}) of
|
2008-08-26 15:59:04 +02:00
|
|
|
[#roster{subscription = Subscription, groups = Groups}] ->
|
|
|
|
{Subscription, Groups};
|
|
|
|
_ ->
|
|
|
|
LRJID = jlib:short_prepd_bare_jid(JID),
|
|
|
|
if
|
|
|
|
LRJID == LJID ->
|
|
|
|
{none, []};
|
|
|
|
true ->
|
|
|
|
case catch mnesia:dirty_read(
|
2009-01-03 16:15:38 +01:00
|
|
|
roster, {User, Server, LRJID}) of
|
2008-08-26 15:59:04 +02:00
|
|
|
[#roster{subscription = Subscription,
|
|
|
|
groups = Groups}] ->
|
|
|
|
{Subscription, Groups};
|
|
|
|
_ ->
|
|
|
|
{none, []}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
catch
|
2003-08-03 21:09:40 +02:00
|
|
|
_ ->
|
2008-08-26 15:59:04 +02:00
|
|
|
{none, []}
|
2003-08-03 21:09:40 +02:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, roster),
|
|
|
|
case mnesia:table_info(roster, attributes) of
|
|
|
|
Fields ->
|
2008-08-26 15:59:04 +02:00
|
|
|
convert_to_exmpp();
|
2005-04-17 20:08:34 +02:00
|
|
|
[uj, user, jid, name, subscription, ask, groups, xattrs, xs] ->
|
2006-05-23 22:19:37 +02:00
|
|
|
convert_table1(Fields);
|
|
|
|
[usj, us, jid, name, subscription, ask, groups, xattrs, xs] ->
|
|
|
|
convert_table2(Fields);
|
2005-04-17 20:08:34 +02:00
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating roster table", []),
|
|
|
|
mnesia:transform_table(roster, ignore, Fields)
|
|
|
|
end.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
2003-08-03 21:09:40 +02:00
|
|
|
|
2006-05-23 22:19:37 +02:00
|
|
|
%% Convert roster table to support virtual host
|
|
|
|
convert_table1(Fields) ->
|
|
|
|
?INFO_MSG("Virtual host support: converting roster table from "
|
|
|
|
"{uj, user, jid, name, subscription, ask, groups, xattrs, xs} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_roster_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, roster},
|
|
|
|
{attributes, record_info(fields, roster)}]),
|
|
|
|
mnesia:del_table_index(roster, user),
|
|
|
|
mnesia:transform_table(roster, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_roster_tmp_table),
|
|
|
|
mnesia:foldl(
|
2008-08-26 15:59:04 +02:00
|
|
|
fun(#roster{usj = {U, {JID_U, JID_S, JID_R}}, us = U, xs = XS, askmessage = AM} = R, _) ->
|
|
|
|
U1 = convert_jid_to_exmpp(U),
|
|
|
|
JID_U1 = convert_jid_to_exmpp(JID_U),
|
|
|
|
JID_R1 = convert_jid_to_exmpp(JID_R),
|
|
|
|
JID1 = {JID_U1, JID_S, JID_R1},
|
|
|
|
XS1 = convert_xs_to_exmpp(XS),
|
|
|
|
AM1 = convert_askmessage_to_exmpp(AM),
|
2006-05-23 22:19:37 +02:00
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_roster_tmp_table,
|
2008-08-26 15:59:04 +02:00
|
|
|
R#roster{usj = {U1, Host, JID1},
|
|
|
|
us = {U1, Host}, xs = XS1,
|
|
|
|
askmessage = AM1})
|
2006-05-23 22:19:37 +02:00
|
|
|
end, ok, roster)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(roster),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(roster),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_roster_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_roster_tmp_table).
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
2006-05-23 22:19:37 +02:00
|
|
|
|
|
|
|
%% Convert roster table: xattrs fields become
|
|
|
|
convert_table2(Fields) ->
|
|
|
|
?INFO_MSG("Converting roster table from "
|
|
|
|
"{usj, us, jid, name, subscription, ask, groups, xattrs, xs} format", []),
|
2008-08-26 15:59:04 +02:00
|
|
|
mnesia:transform_table(roster, ignore, Fields),
|
|
|
|
convert_to_exmpp().
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
2008-08-26 15:59:04 +02:00
|
|
|
|
|
|
|
convert_to_exmpp() ->
|
|
|
|
Fun = fun() ->
|
|
|
|
case mnesia:first(roster) of
|
2009-02-06 14:43:40 +01:00
|
|
|
{_User, Server, _JID} when is_binary(Server) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
none;
|
2009-02-06 14:43:40 +01:00
|
|
|
{_User, Server, _JID} when is_list(Server) ->
|
|
|
|
mnesia:foldl(fun convert_to_exmpp2/2,
|
|
|
|
done, roster, write);
|
|
|
|
'$end_of_table' ->
|
|
|
|
none
|
2008-08-26 15:59:04 +02:00
|
|
|
end
|
|
|
|
end,
|
2008-08-27 11:45:01 +02:00
|
|
|
mnesia:transaction(Fun).
|
2008-08-26 15:59:04 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2008-08-26 15:59:04 +02:00
|
|
|
convert_to_exmpp2(#roster{
|
|
|
|
usj = {USJ_U, USJ_S, {USJ_JU, USJ_JS, USJ_JR}} = Key,
|
|
|
|
us = {US_U, US_S},
|
|
|
|
jid = {JID_U, JID_S, JID_R},
|
2009-02-06 14:43:40 +01:00
|
|
|
name = N, xs = XS, groups = G, askmessage = AM} = R, Acc) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
% Remove old entry.
|
|
|
|
mnesia:delete({roster, Key}),
|
2009-02-06 14:43:40 +01:00
|
|
|
% Convert "" to undefined in JIDs and string() to binary().
|
2008-08-26 15:59:04 +02:00
|
|
|
USJ_U1 = convert_jid_to_exmpp(USJ_U),
|
2009-02-06 14:43:40 +01:00
|
|
|
USJ_S1 = convert_jid_to_exmpp(USJ_S),
|
2008-08-26 15:59:04 +02:00
|
|
|
USJ_JU1 = convert_jid_to_exmpp(USJ_JU),
|
2009-02-06 14:43:40 +01:00
|
|
|
USJ_JS1 = convert_jid_to_exmpp(USJ_JS),
|
2008-08-26 15:59:04 +02:00
|
|
|
USJ_JR1 = convert_jid_to_exmpp(USJ_JR),
|
|
|
|
US_U1 = convert_jid_to_exmpp(US_U),
|
2009-02-06 14:43:40 +01:00
|
|
|
US_S1 = convert_jid_to_exmpp(US_S),
|
2008-08-26 15:59:04 +02:00
|
|
|
JID_U1 = convert_jid_to_exmpp(JID_U),
|
2009-02-06 14:43:40 +01:00
|
|
|
JID_S1 = convert_jid_to_exmpp(JID_S),
|
2008-08-26 15:59:04 +02:00
|
|
|
JID_R1 = convert_jid_to_exmpp(JID_R),
|
2009-02-06 14:43:40 +01:00
|
|
|
% Convert name.
|
|
|
|
N1 = convert_name_to_exmpp(N),
|
|
|
|
% Convert groups.
|
|
|
|
G1 = convert_groups_to_exmpp(G, []),
|
2008-08-26 15:59:04 +02:00
|
|
|
% Convert xs.
|
|
|
|
XS1 = convert_xs_to_exmpp(XS),
|
|
|
|
% Convert askmessage.
|
|
|
|
AM1 = convert_askmessage_to_exmpp(AM),
|
|
|
|
% Prepare the new record.
|
|
|
|
New_R = R#roster{
|
2009-02-06 14:43:40 +01:00
|
|
|
usj = {USJ_U1, USJ_S1, {USJ_JU1, USJ_JS1, USJ_JR1}},
|
|
|
|
us = {US_U1, US_S1},
|
|
|
|
jid = {JID_U1, JID_S1, JID_R1},
|
|
|
|
name = N1, groups = G1, xs = XS1, askmessage = AM1},
|
2008-08-26 15:59:04 +02:00
|
|
|
% Write the new record.
|
|
|
|
mnesia:write(New_R),
|
|
|
|
Acc.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
|
|
|
|
|
|
|
convert_jid_to_exmpp("") -> undefined;
|
|
|
|
convert_jid_to_exmpp(V) when is_list(V) -> list_to_binary(V).
|
|
|
|
|
|
|
|
%% @hidden
|
|
|
|
|
|
|
|
convert_name_to_exmpp(N) when is_list(N) -> list_to_binary(N).
|
|
|
|
|
|
|
|
%% @hidden
|
|
|
|
|
|
|
|
convert_groups_to_exmpp([G | Rest], New_G) ->
|
|
|
|
convert_groups_to_exmpp(Rest, [list_to_binary(G) | New_G]);
|
|
|
|
convert_groups_to_exmpp([], New_G) ->
|
|
|
|
lists:reverse(New_G).
|
|
|
|
|
|
|
|
%% @hidden
|
2008-08-26 15:59:04 +02:00
|
|
|
|
|
|
|
convert_xs_to_exmpp(Els) ->
|
|
|
|
convert_xs_to_exmpp(Els, []).
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2008-08-26 15:59:04 +02:00
|
|
|
convert_xs_to_exmpp([El | Rest], Result) ->
|
|
|
|
New_El = exmpp_xml:xmlelement_to_xmlel(El,
|
|
|
|
[?NS_JABBER_CLIENT], [{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
convert_xs_to_exmpp(Rest, [New_El | Result]);
|
|
|
|
convert_xs_to_exmpp([], Result) ->
|
|
|
|
lists:reverse(Result).
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @hidden
|
|
|
|
|
2008-08-26 15:59:04 +02:00
|
|
|
convert_askmessage_to_exmpp(AM) when is_binary(AM) ->
|
|
|
|
AM;
|
|
|
|
convert_askmessage_to_exmpp(AM) ->
|
|
|
|
list_to_binary(AM).
|
2007-08-24 18:15:05 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Acc, Host, Request) -> {stop, Result} | Acc
|
|
|
|
%% Acc = term()
|
|
|
|
%% Host = string()
|
|
|
|
%% Request = ejabberd_http:request()
|
|
|
|
%% Result = [ejabberd_web:html()]
|
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
webadmin_page(_, Host,
|
|
|
|
#request{us = _US,
|
|
|
|
path = ["user", U, "roster"],
|
|
|
|
q = Query,
|
2009-02-06 14:43:40 +01:00
|
|
|
lang = Lang} = _Request)
|
|
|
|
when is_list(Host), is_list(U) ->
|
|
|
|
Res = user_roster(list_to_binary(U), list_to_binary(Host), Query, Lang),
|
2007-08-24 18:15:05 +02:00
|
|
|
{stop, Res};
|
|
|
|
|
|
|
|
webadmin_page(Acc, _, _) -> Acc.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server, Query, Lang) -> Result
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% Query = ejabberd_http:query()
|
|
|
|
%% Lang = string()
|
|
|
|
%% Result = [ejabberd_web:html()]
|
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
user_roster(User, Server, Query, Lang) ->
|
2008-08-26 15:59:04 +02:00
|
|
|
try
|
2008-09-18 16:55:43 +02:00
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
2008-08-26 15:59:04 +02:00
|
|
|
Items1 = mnesia:dirty_index_read(roster, US, #roster.us),
|
|
|
|
Res = user_roster_parse_query(User, Server, Items1, Query),
|
|
|
|
Items = mnesia:dirty_index_read(roster, US, #roster.us),
|
|
|
|
SItems = lists:sort(Items),
|
|
|
|
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),
|
2009-01-19 12:59:40 +01:00
|
|
|
TDJID = build_contact_jid_td(R#roster.jid),
|
2008-08-26 15:59:04 +02:00
|
|
|
?XE("tr",
|
2009-01-19 12:59:40 +01:00
|
|
|
[TDJID,
|
2009-05-27 19:29:58 +02:00
|
|
|
?XAC("td", [?XMLATTR('class', <<"valign">>)],
|
2009-01-08 15:54:00 +01:00
|
|
|
binary_to_list(R#roster.name)),
|
2009-05-27 19:29:58 +02:00
|
|
|
?XAC("td", [?XMLATTR('class', <<"valign">>)],
|
2008-08-26 15:59:04 +02:00
|
|
|
atom_to_list(R#roster.subscription)),
|
2009-05-27 19:29:58 +02:00
|
|
|
?XAC("td", [?XMLATTR('class', <<"valign">>)],
|
2008-08-26 15:59:04 +02:00
|
|
|
atom_to_list(Pending)),
|
2009-05-27 19:29:58 +02:00
|
|
|
?XAE("td", [?XMLATTR('class', <<"valign">>)], Groups),
|
2008-08-26 15:59:04 +02:00
|
|
|
if
|
|
|
|
Pending == in ->
|
2009-05-27 19:29:58 +02:00
|
|
|
?XAE("td", [?XMLATTR('class', <<"valign">>)],
|
2008-08-26 15:59:04 +02:00
|
|
|
[?INPUTT("submit",
|
|
|
|
"validate" ++
|
|
|
|
ejabberd_web_admin:term_to_id(R#roster.jid),
|
|
|
|
"Validate")]);
|
|
|
|
true ->
|
|
|
|
?X("td")
|
|
|
|
end,
|
2009-05-27 19:29:58 +02:00
|
|
|
?XAE("td", [?XMLATTR('class', <<"valign">>)],
|
2008-08-26 15:59:04 +02:00
|
|
|
[?INPUTT("submit",
|
|
|
|
"remove" ++
|
|
|
|
ejabberd_web_admin:term_to_id(R#roster.jid),
|
|
|
|
"Remove")])])
|
|
|
|
end, SItems))])]
|
|
|
|
end,
|
|
|
|
[?XC("h1", ?T("Roster of ") ++ us_to_list(US))] ++
|
|
|
|
case Res of
|
2009-01-19 16:27:07 +01:00
|
|
|
ok -> [?XREST("Submitted")];
|
|
|
|
error -> [?XREST("Bad format")];
|
2008-08-26 15:59:04 +02:00
|
|
|
nothing -> []
|
|
|
|
end ++
|
2009-05-27 19:29:58 +02:00
|
|
|
[?XAE("form", [?XMLATTR('action', <<"">>), ?XMLATTR('method', <<"post">>)],
|
2008-08-26 15:59:04 +02:00
|
|
|
FItems ++
|
|
|
|
[?P,
|
|
|
|
?INPUT("text", "newjid", ""), ?C(" "),
|
|
|
|
?INPUTT("submit", "addjid", "Add Jabber ID")
|
|
|
|
])]
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
[?XC("h1", ?T("Roster of ") ++ us_to_list({User, Server}))] ++
|
|
|
|
[?CT("Bad format"), ?P] ++
|
2009-05-27 19:29:58 +02:00
|
|
|
[?XAE("form", [?XMLATTR('action', <<"">>), ?XMLATTR('method', <<"post">>)],
|
2008-08-26 15:59:04 +02:00
|
|
|
[?P,
|
|
|
|
?INPUT("text", "newjid", ""), ?C(" "),
|
|
|
|
?INPUTT("submit", "addjid", "Add Jabber ID")
|
|
|
|
])]
|
|
|
|
end.
|
2007-08-24 18:15:05 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (JID) -> Result
|
|
|
|
%% JID = jlib:shortjid()
|
|
|
|
%% Result = ejabberd_web:html()
|
|
|
|
|
2009-01-19 12:59:40 +01:00
|
|
|
build_contact_jid_td({U, S, R}) ->
|
|
|
|
%% Convert {U, S, R} into {jid, U, S, R, U, S, R}:
|
2009-06-01 18:26:00 +02:00
|
|
|
ContactJID = exmpp_jid:make(U, S, R),
|
2009-06-01 18:39:36 +02:00
|
|
|
JIDURI = case {exmpp_jid:prep_node(ContactJID), exmpp_jid:prep_domain(ContactJID)} of
|
2009-01-19 12:59:40 +01:00
|
|
|
{undefined, _} -> "";
|
|
|
|
{CUser, CServer} ->
|
|
|
|
CUser_S = binary_to_list(CUser),
|
|
|
|
CServer_S = binary_to_list(CServer),
|
|
|
|
case lists:member(CServer_S, ?MYHOSTS) of
|
|
|
|
false -> "";
|
|
|
|
true -> "/admin/server/" ++ CServer_S ++ "/user/" ++ CUser_S ++ "/"
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case JIDURI of
|
|
|
|
[] ->
|
2009-06-01 18:52:14 +02:00
|
|
|
?XAC('td', [?XMLATTR('class', <<"valign">>)], exmpp_jid:to_list(ContactJID));
|
2009-01-19 12:59:40 +01:00
|
|
|
URI when is_list(URI) ->
|
2009-06-01 18:52:14 +02:00
|
|
|
?XAE('td', [?XMLATTR('class', <<"valign">>)], [?AC(JIDURI, exmpp_jid:to_list(ContactJID))])
|
2009-01-19 12:59:40 +01:00
|
|
|
end.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server, Items, Query) -> ok | nothing | error
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% Items = [rosteritem()]
|
|
|
|
%% Query = ejabberd_http:query()
|
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
user_roster_parse_query(User, Server, Items, Query) ->
|
|
|
|
case lists:keysearch("addjid", 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
case lists:keysearch("newjid", 1, Query) of
|
|
|
|
{value, {_, undefined}} ->
|
|
|
|
error;
|
|
|
|
{value, {_, SJID}} ->
|
2008-07-21 17:29:52 +02:00
|
|
|
try
|
2009-06-01 18:35:55 +02:00
|
|
|
JID = exmpp_jid:parse(SJID),
|
2008-07-21 17:29:52 +02:00
|
|
|
user_roster_subscribe_jid(User, Server, JID),
|
|
|
|
ok
|
|
|
|
catch
|
|
|
|
_ ->
|
2007-08-24 18:15:05 +02:00
|
|
|
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
|
|
|
|
end.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server, JID) -> term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% JID = exmpp_jid:jid()
|
2007-08-24 18:15:05 +02:00
|
|
|
|
|
|
|
user_roster_subscribe_jid(User, Server, JID) ->
|
|
|
|
out_subscription(User, Server, JID, subscribe),
|
2009-06-01 18:26:00 +02:00
|
|
|
UJID = exmpp_jid:make(User, Server),
|
2007-08-24 18:15:05 +02:00
|
|
|
ejabberd_router:route(
|
2008-07-21 17:29:52 +02:00
|
|
|
UJID, JID, exmpp_presence:subscribe()).
|
2007-08-24 18:15:05 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (User, Server, Items, Query) -> term()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
%% Items = [rosteritem()]
|
|
|
|
%% Query = ejabberd_http:query()
|
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
user_roster_item_parse_query(User, Server, Items, Query) ->
|
|
|
|
lists:foreach(
|
2009-02-06 14:43:40 +01:00
|
|
|
fun(Roster) ->
|
|
|
|
JID = Roster#roster.jid,
|
2007-08-24 18:15:05 +02:00
|
|
|
case lists:keysearch(
|
|
|
|
"validate" ++ ejabberd_web_admin:term_to_id(JID), 1, Query) of
|
|
|
|
{value, _} ->
|
2008-07-21 17:29:52 +02:00
|
|
|
{U, S, R} = JID,
|
2009-06-01 18:26:00 +02:00
|
|
|
JID1 = exmpp_jid:make(U, S, R),
|
2007-08-24 18:15:05 +02:00
|
|
|
out_subscription(
|
|
|
|
User, Server, JID1, subscribed),
|
2009-06-01 18:26:00 +02:00
|
|
|
UJID = exmpp_jid:make(User, Server),
|
2007-08-24 18:15:05 +02:00
|
|
|
ejabberd_router:route(
|
2008-07-21 17:29:52 +02:00
|
|
|
UJID, JID1, exmpp_presence:subscribed()),
|
2007-08-24 18:15:05 +02:00
|
|
|
throw(submitted);
|
|
|
|
false ->
|
|
|
|
case lists:keysearch(
|
|
|
|
"remove" ++ ejabberd_web_admin:term_to_id(JID), 1, Query) of
|
|
|
|
{value, _} ->
|
2009-02-06 14:43:40 +01:00
|
|
|
{U, S, R} = JID,
|
2009-06-01 18:26:00 +02:00
|
|
|
UJID = exmpp_jid:make(User, Server),
|
2008-07-21 17:29:52 +02:00
|
|
|
Attrs1 = exmpp_xml:set_attribute_in_list([],
|
2009-06-01 18:52:14 +02:00
|
|
|
'jid', exmpp_jid:to_list(U, S, R)),
|
2008-07-21 17:29:52 +02:00
|
|
|
Attrs2 = exmpp_xml:set_attribute_in_list(Attrs1,
|
|
|
|
'subscription', "remove"),
|
|
|
|
Item = #xmlel{ns = ?NS_ROSTER, name = 'item',
|
|
|
|
attrs = Attrs2},
|
|
|
|
Request = #xmlel{
|
|
|
|
ns = ?NS_ROSTER,
|
|
|
|
name = 'query',
|
|
|
|
children = [Item]},
|
2007-08-24 18:15:05 +02:00
|
|
|
process_iq(
|
|
|
|
UJID, UJID,
|
2008-07-21 17:29:52 +02:00
|
|
|
exmpp_iq:set(?NS_JABBER_CLIENT, Request)),
|
2007-08-24 18:15:05 +02:00
|
|
|
throw(submitted);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end, Items),
|
|
|
|
nothing.
|
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec ({User, Server}) -> string()
|
|
|
|
%% User = binary()
|
|
|
|
%% Server = binary()
|
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
us_to_list({User, Server}) ->
|
2009-06-01 18:54:33 +02:00
|
|
|
exmpp_jid:bare_to_list(User, Server).
|
2007-08-24 18:15:05 +02:00
|
|
|
|
2009-02-06 14:43:40 +01:00
|
|
|
%% @spec (Acc, User, Server, Lang) -> New_Acc
|
|
|
|
%% Acc = [ejabberd_web:html()]
|
|
|
|
%% User = string()
|
|
|
|
%% Server = string()
|
|
|
|
%% Lang = string()
|
|
|
|
%% New_Acc = [ejabberd_web:html()]
|
|
|
|
|
2007-08-24 18:15:05 +02:00
|
|
|
webadmin_user(Acc, _User, _Server, Lang) ->
|
2009-02-06 14:43:40 +01:00
|
|
|
% `Lang' is used by the `T' macro, called from the `ACT' macro.
|
2007-08-24 18:15:05 +02:00
|
|
|
Acc ++ [?XE("h3", [?ACT("roster/", "Roster")])].
|
|
|
|
|