2005-04-17 20:08:34 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_shared_roster.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2005-04-17 20:08:34 +02:00
|
|
|
%%% Purpose : Shared roster management
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 5 Mar 2005 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 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
|
|
|
%%%
|
2005-04-17 20:08:34 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_shared_roster).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, export/1,
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info/0, webadmin_menu/3, webadmin_page/3,
|
2017-05-17 13:47:35 +02:00
|
|
|
get_user_roster/2,
|
2016-11-22 14:48:01 +01:00
|
|
|
get_jid_info/4, import/5, process_item/2, import_start/2,
|
2018-01-26 13:02:06 +01:00
|
|
|
in_subscription/2, out_subscription/1, c2s_self_presence/1,
|
2015-06-01 14:38:27 +02:00
|
|
|
unset_presence/4, register_user/2, remove_user/2,
|
|
|
|
list_groups/1, create_group/2, create_group/3,
|
|
|
|
delete_group/2, get_group_opts/2, set_group_opts/3,
|
|
|
|
get_group_users/2, get_group_explicit_users/2,
|
2016-04-14 10:45:43 +02:00
|
|
|
is_user_in_group/3, add_user_to_group/3, opts_to_binary/1,
|
2020-01-08 10:24:51 +01:00
|
|
|
remove_user_from_group/3, mod_opt_type/1, mod_options/1, mod_doc/0, depends/2]).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-07-30 07:37:34 +02:00
|
|
|
-include("xmpp.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").
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2016-04-14 10:45:43 +02:00
|
|
|
-include("mod_shared_roster.hrl").
|
|
|
|
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
|
|
|
|
2016-04-14 10:45:43 +02:00
|
|
|
-type group_options() :: [{atom(), any()}].
|
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
2016-11-22 14:48:01 +01:00
|
|
|
-callback import(binary(), binary(), [binary()]) -> ok.
|
2016-04-14 10:45:43 +02:00
|
|
|
-callback list_groups(binary()) -> [binary()].
|
|
|
|
-callback groups_with_opts(binary()) -> [{binary(), group_options()}].
|
|
|
|
-callback create_group(binary(), binary(), group_options()) -> {atomic, any()}.
|
|
|
|
-callback delete_group(binary(), binary()) -> {atomic, any()}.
|
|
|
|
-callback get_group_opts(binary(), binary()) -> group_options() | error.
|
|
|
|
-callback set_group_opts(binary(), binary(), group_options()) -> {atomic, any()}.
|
|
|
|
-callback get_user_groups({binary(), binary()}, binary()) -> [binary()].
|
|
|
|
-callback get_group_explicit_users(binary(), binary()) -> [{binary(), binary()}].
|
|
|
|
-callback get_user_displayed_groups(binary(), binary(), group_options()) ->
|
|
|
|
[{binary(), group_options()}].
|
|
|
|
-callback is_user_in_group({binary(), binary()}, binary(), binary()) -> boolean().
|
2017-02-18 07:36:27 +01:00
|
|
|
-callback add_user_to_group(binary(), {binary(), binary()}, binary()) -> any().
|
2016-04-14 10:45:43 +02:00
|
|
|
-callback remove_user_from_group(binary(), {binary(), binary()}, binary()) -> {atomic, any()}.
|
2020-03-17 14:35:43 +01:00
|
|
|
-callback use_cache(binary()) -> boolean().
|
|
|
|
-callback cache_nodes(binary()) -> [node()].
|
|
|
|
|
|
|
|
-optional_callbacks([use_cache/1, cache_nodes/1]).
|
|
|
|
|
|
|
|
-define(GROUP_OPTS_CACHE, shared_roster_group_opts_cache).
|
|
|
|
-define(USER_GROUPS_CACHE, shared_roster_user_groups_cache).
|
|
|
|
-define(GROUP_EXPLICIT_USERS_CACHE, shared_roster_group_explicit_cache).
|
|
|
|
-define(SPECIAL_GROUPS_CACHE, shared_roster_special_groups_cache).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
start(Host, Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Mod = gen_mod:db_mod(Opts, ?MODULE),
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod:init(Host, Opts),
|
2020-03-17 14:35:43 +01:00
|
|
|
init_cache(Mod, Host, Opts),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:add(webadmin_menu_host, Host, ?MODULE,
|
|
|
|
webadmin_menu, 70),
|
|
|
|
ejabberd_hooks:add(webadmin_page_host, Host, ?MODULE,
|
|
|
|
webadmin_page, 50),
|
|
|
|
ejabberd_hooks:add(roster_get, Host, ?MODULE,
|
|
|
|
get_user_roster, 70),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_in_subscription, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
?MODULE, in_subscription, 30),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(roster_out_subscription, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
?MODULE, out_subscription, 30),
|
|
|
|
ejabberd_hooks:add(roster_get_jid_info, Host, ?MODULE,
|
|
|
|
get_jid_info, 70),
|
|
|
|
ejabberd_hooks:add(roster_process_item, Host, ?MODULE,
|
|
|
|
process_item, 50),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(c2s_self_presence, Host, ?MODULE,
|
|
|
|
c2s_self_presence, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:add(unset_presence_hook, Host, ?MODULE,
|
|
|
|
unset_presence, 50),
|
|
|
|
ejabberd_hooks:add(register_user, Host, ?MODULE,
|
|
|
|
register_user, 50),
|
|
|
|
ejabberd_hooks:add(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(webadmin_menu_host, Host, ?MODULE,
|
|
|
|
webadmin_menu, 70),
|
|
|
|
ejabberd_hooks:delete(webadmin_page_host, Host, ?MODULE,
|
|
|
|
webadmin_page, 50),
|
|
|
|
ejabberd_hooks:delete(roster_get, Host, ?MODULE,
|
|
|
|
get_user_roster, 70),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_in_subscription, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
?MODULE, in_subscription, 30),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_out_subscription, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
?MODULE, out_subscription, 30),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:delete(roster_get_jid_info, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
?MODULE, get_jid_info, 70),
|
2006-07-06 16:08:09 +02:00
|
|
|
ejabberd_hooks:delete(roster_process_item, Host,
|
2008-04-26 19:37:43 +02:00
|
|
|
?MODULE, process_item, 50),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(c2s_self_presence, Host,
|
|
|
|
?MODULE, c2s_self_presence, 50),
|
2011-02-21 22:33:23 +01:00
|
|
|
ejabberd_hooks:delete(unset_presence_hook, Host,
|
|
|
|
?MODULE, unset_presence, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(register_user, Host, ?MODULE,
|
|
|
|
register_user, 50),
|
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE,
|
|
|
|
remove_user,
|
2015-10-07 00:06:58 +02:00
|
|
|
50).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
|
|
|
reload(Host, NewOpts, OldOpts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
NewMod = gen_mod:db_mod(NewOpts, ?MODULE),
|
|
|
|
OldMod = gen_mod:db_mod(OldOpts, ?MODULE),
|
2017-02-22 17:46:47 +01:00
|
|
|
if NewMod /= OldMod ->
|
|
|
|
NewMod:init(Host, NewOpts);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ok.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2020-03-17 14:35:43 +01:00
|
|
|
-spec init_cache(module(), binary(), gen_mod:opts()) -> ok.
|
|
|
|
init_cache(Mod, Host, Opts) ->
|
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
CacheOpts = cache_opts(Opts),
|
|
|
|
ets_cache:new(?GROUP_OPTS_CACHE, CacheOpts),
|
|
|
|
ets_cache:new(?USER_GROUPS_CACHE, CacheOpts),
|
|
|
|
ets_cache:new(?GROUP_EXPLICIT_USERS_CACHE, CacheOpts),
|
|
|
|
ets_cache:new(?SPECIAL_GROUPS_CACHE, CacheOpts);
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?GROUP_OPTS_CACHE),
|
|
|
|
ets_cache:delete(?USER_GROUPS_CACHE),
|
|
|
|
ets_cache:delete(?GROUP_EXPLICIT_USERS_CACHE),
|
|
|
|
ets_cache:delete(?SPECIAL_GROUPS_CACHE)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_opts(gen_mod:opts()) -> [proplists:property()].
|
|
|
|
cache_opts(Opts) ->
|
|
|
|
MaxSize = mod_private_opt:cache_size(Opts),
|
|
|
|
CacheMissed = mod_private_opt:cache_missed(Opts),
|
|
|
|
LifeTime = mod_private_opt:cache_life_time(Opts),
|
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
|
|
|
-spec use_cache(module(), binary()) -> boolean().
|
|
|
|
use_cache(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 1) of
|
|
|
|
true -> Mod:use_cache(Host);
|
|
|
|
false -> mod_shared_roster_opt:use_cache(Host)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_nodes(module(), binary()) -> [node()].
|
|
|
|
cache_nodes(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 1) of
|
|
|
|
true -> Mod:cache_nodes(Host);
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
|
|
|
end.
|
|
|
|
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec get_user_roster([#roster{}], {binary(), binary()}) -> [#roster{}].
|
2005-04-17 20:08:34 +02:00
|
|
|
get_user_roster(Items, US) ->
|
|
|
|
{U, S} = US,
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
2013-03-14 10:33:02 +01:00
|
|
|
SRUsers = lists:foldl(fun (Group, Acc1) ->
|
|
|
|
GroupName = get_group_name(S, Group),
|
|
|
|
lists:foldl(fun (User, Acc2) ->
|
|
|
|
if User == US -> Acc2;
|
|
|
|
true ->
|
|
|
|
dict:append(User,
|
|
|
|
GroupName,
|
|
|
|
Acc2)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Acc1, get_group_users(S, Group))
|
|
|
|
end,
|
|
|
|
dict:new(), DisplayedGroups),
|
|
|
|
{NewItems1, SRUsersRest} = lists:mapfoldl(fun (Item,
|
|
|
|
SRUsers1) ->
|
|
|
|
{_, _, {U1, S1, _}} =
|
|
|
|
Item#roster.usj,
|
|
|
|
US1 = {U1, S1},
|
|
|
|
case dict:find(US1,
|
|
|
|
SRUsers1)
|
|
|
|
of
|
2017-03-09 19:21:45 +01:00
|
|
|
{ok, GroupNames} ->
|
2013-03-14 10:33:02 +01:00
|
|
|
{Item#roster{subscription
|
|
|
|
=
|
|
|
|
both,
|
2017-03-09 19:21:45 +01:00
|
|
|
groups =
|
|
|
|
Item#roster.groups ++ GroupNames,
|
2013-03-14 10:33:02 +01:00
|
|
|
ask =
|
|
|
|
none},
|
|
|
|
dict:erase(US1,
|
|
|
|
SRUsers1)};
|
|
|
|
error ->
|
|
|
|
{Item, SRUsers1}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
SRUsers, Items),
|
|
|
|
SRItems = [#roster{usj = {U, S, {U1, S1, <<"">>}},
|
|
|
|
us = US, jid = {U1, S1, <<"">>},
|
2016-07-30 07:37:34 +02:00
|
|
|
name = get_rosteritem_name(U1, S1),
|
2013-03-14 10:33:02 +01:00
|
|
|
subscription = both, ask = none, groups = GroupNames}
|
|
|
|
|| {{U1, S1}, GroupNames} <- dict:to_list(SRUsersRest)],
|
2005-04-17 20:08:34 +02:00
|
|
|
SRItems ++ NewItems1.
|
|
|
|
|
2016-07-30 07:37:34 +02:00
|
|
|
get_rosteritem_name(U, S) ->
|
|
|
|
case gen_mod:is_loaded(S, mod_vcard) of
|
2013-11-13 11:40:56 +01:00
|
|
|
true ->
|
2016-07-30 07:37:34 +02:00
|
|
|
SubEls = mod_vcard:get_vcard(U, S),
|
|
|
|
get_rosteritem_name_vcard(SubEls);
|
2013-11-13 11:40:56 +01:00
|
|
|
false ->
|
|
|
|
<<"">>
|
2010-02-02 19:47:22 +01:00
|
|
|
end.
|
|
|
|
|
2016-07-30 07:37:34 +02:00
|
|
|
-spec get_rosteritem_name_vcard([xmlel()]) -> binary().
|
|
|
|
get_rosteritem_name_vcard([Vcard|_]) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_path_s(Vcard,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"NICKNAME">>}, cdata])
|
|
|
|
of
|
|
|
|
<<"">> ->
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:get_path_s(Vcard, [{elem, <<"FN">>}, cdata]);
|
2013-03-14 10:33:02 +01:00
|
|
|
Nickname -> Nickname
|
2016-07-30 07:37:34 +02:00
|
|
|
end;
|
|
|
|
get_rosteritem_name_vcard(_) ->
|
|
|
|
<<"">>.
|
2010-02-02 19:47:22 +01:00
|
|
|
|
2008-07-09 21:06:44 +02:00
|
|
|
%% This function rewrites the roster entries when moving or renaming
|
2006-07-06 16:08:09 +02:00
|
|
|
%% them in the user contact list.
|
2016-08-12 12:17:42 +02:00
|
|
|
-spec process_item(#roster{}, binary()) -> #roster{}.
|
2006-07-06 16:08:09 +02:00
|
|
|
process_item(RosterItem, Host) ->
|
2008-07-09 21:06:44 +02:00
|
|
|
USFrom = {UserFrom, ServerFrom} = RosterItem#roster.us,
|
|
|
|
{UserTo, ServerTo, ResourceTo} = RosterItem#roster.jid,
|
2008-10-01 16:52:25 +02:00
|
|
|
NameTo = RosterItem#roster.name,
|
2008-07-09 21:06:44 +02:00
|
|
|
USTo = {UserTo, ServerTo},
|
2006-07-06 16:08:09 +02:00
|
|
|
DisplayedGroups = get_user_displayed_groups(USFrom),
|
2013-03-14 10:33:02 +01:00
|
|
|
CommonGroups = lists:filter(fun (Group) ->
|
2006-07-06 16:08:09 +02:00
|
|
|
is_user_in_group(USTo, Group, Host)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
DisplayedGroups),
|
2006-07-06 16:08:09 +02:00
|
|
|
case CommonGroups of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] -> RosterItem;
|
|
|
|
%% Roster item cannot be removed: We simply reset the original groups:
|
|
|
|
_ when RosterItem#roster.subscription == remove ->
|
|
|
|
GroupNames = lists:map(fun (Group) ->
|
|
|
|
get_group_name(Host, Group)
|
|
|
|
end,
|
|
|
|
CommonGroups),
|
|
|
|
RosterItem#roster{subscription = both, ask = none,
|
|
|
|
groups = GroupNames};
|
|
|
|
%% Both users have at least a common shared group,
|
|
|
|
%% So each user can see the other
|
|
|
|
_ ->
|
|
|
|
case lists:subtract(RosterItem#roster.groups,
|
|
|
|
CommonGroups)
|
|
|
|
of
|
|
|
|
%% If it doesn't, then remove this user from any
|
|
|
|
%% existing roster groups.
|
|
|
|
[] ->
|
2018-01-26 13:02:06 +01:00
|
|
|
Pres = #presence{from = jid:make(UserTo, ServerTo),
|
|
|
|
to = jid:make(UserFrom, ServerFrom),
|
|
|
|
type = unsubscribe},
|
|
|
|
mod_roster:out_subscription(Pres),
|
|
|
|
mod_roster:in_subscription(false, Pres),
|
2013-03-14 10:33:02 +01:00
|
|
|
RosterItem#roster{subscription = both, ask = none};
|
|
|
|
%% If so, it means the user wants to add that contact
|
|
|
|
%% to his personal roster
|
|
|
|
PersonalGroups ->
|
|
|
|
set_new_rosteritems(UserFrom, ServerFrom, UserTo,
|
|
|
|
ServerTo, ResourceTo, NameTo,
|
|
|
|
PersonalGroups)
|
|
|
|
end
|
2006-07-06 16:08:09 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
build_roster_record(User1, Server1, User2, Server2,
|
|
|
|
Name2, Groups) ->
|
|
|
|
USR2 = {User2, Server2, <<"">>},
|
2008-07-09 21:06:44 +02:00
|
|
|
#roster{usj = {User1, Server1, USR2},
|
2013-03-14 10:33:02 +01:00
|
|
|
us = {User1, Server1}, jid = USR2, name = Name2,
|
|
|
|
subscription = both, ask = none, groups = Groups}.
|
|
|
|
|
|
|
|
set_new_rosteritems(UserFrom, ServerFrom, UserTo,
|
|
|
|
ServerTo, ResourceTo, NameTo, GroupsFrom) ->
|
2008-07-09 21:06:44 +02:00
|
|
|
RIFrom = build_roster_record(UserFrom, ServerFrom,
|
2008-10-01 16:52:25 +02:00
|
|
|
UserTo, ServerTo, NameTo, GroupsFrom),
|
2008-07-09 21:06:44 +02:00
|
|
|
set_item(UserFrom, ServerFrom, ResourceTo, RIFrom),
|
2017-02-25 08:01:01 +01:00
|
|
|
JIDTo = jid:make(UserTo, ServerTo),
|
|
|
|
JIDFrom = jid:make(UserFrom, ServerFrom),
|
2013-03-14 10:33:02 +01:00
|
|
|
RITo = build_roster_record(UserTo, ServerTo, UserFrom,
|
|
|
|
ServerFrom, UserFrom, []),
|
|
|
|
set_item(UserTo, ServerTo, <<"">>, RITo),
|
2018-01-26 13:02:06 +01:00
|
|
|
mod_roster:out_subscription(
|
|
|
|
#presence{from = JIDFrom, to = JIDTo, type = subscribe}),
|
|
|
|
mod_roster:in_subscription(
|
|
|
|
false, #presence{to = JIDTo, from = JIDFrom, type = subscribe}),
|
|
|
|
mod_roster:out_subscription(
|
|
|
|
#presence{from = JIDTo, to = JIDFrom, type = subscribed}),
|
|
|
|
mod_roster:in_subscription(
|
|
|
|
false, #presence{to = JIDFrom, from = JIDTo, type = subscribed}),
|
|
|
|
mod_roster:out_subscription(
|
|
|
|
#presence{from = JIDTo, to = JIDFrom, type = subscribe}),
|
|
|
|
mod_roster:in_subscription(
|
|
|
|
false, #presence{to = JIDFrom, from = JIDTo, type = subscribe}),
|
|
|
|
mod_roster:out_subscription(
|
|
|
|
#presence{from = JIDFrom, to = JIDTo, type = subscribed}),
|
|
|
|
mod_roster:in_subscription(
|
|
|
|
false, #presence{to = JIDTo, from = JIDFrom, type = subscribed}),
|
2008-07-09 21:06:44 +02:00
|
|
|
RIFrom.
|
|
|
|
|
|
|
|
set_item(User, Server, Resource, Item) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ResIQ = #iq{from = jid:make(User, Server, Resource),
|
|
|
|
to = jid:make(Server),
|
2018-07-05 10:51:49 +02:00
|
|
|
type = set, id = <<"push", (p1_rand:get_string())/binary>>,
|
2016-07-30 07:37:34 +02:00
|
|
|
sub_els = [#roster_query{
|
|
|
|
items = [mod_roster:encode_item(Item)]}]},
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(ResIQ).
|
2008-07-09 21:06:44 +02:00
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
-spec get_jid_info({subscription(), ask(), [binary()]}, binary(), binary(), jid())
|
|
|
|
-> {subscription(), ask(), [binary()]}.
|
|
|
|
get_jid_info({Subscription, Ask, Groups}, User, Server,
|
2013-03-14 10:33:02 +01:00
|
|
|
JID) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2015-11-24 16:44:13 +01:00
|
|
|
{U1, S1, _} = jid:tolower(JID),
|
2005-04-17 20:08:34 +02:00
|
|
|
US1 = {U1, S1},
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
2013-03-14 10:33:02 +01:00
|
|
|
SRUsers = lists:foldl(fun (Group, Acc1) ->
|
2018-09-10 01:08:00 +02:00
|
|
|
GroupName = get_group_name(LServer, Group),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun (User1, Acc2) ->
|
|
|
|
dict:append(User1,
|
2018-09-10 01:08:00 +02:00
|
|
|
GroupName,
|
2013-03-14 10:33:02 +01:00
|
|
|
Acc2)
|
|
|
|
end,
|
|
|
|
Acc1,
|
|
|
|
get_group_users(LServer, Group))
|
|
|
|
end,
|
|
|
|
dict:new(), DisplayedGroups),
|
2005-04-17 20:08:34 +02:00
|
|
|
case dict:find(US1, SRUsers) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, GroupNames} ->
|
|
|
|
NewGroups = if Groups == [] -> GroupNames;
|
|
|
|
true -> Groups
|
|
|
|
end,
|
2018-01-26 13:02:06 +01:00
|
|
|
{both, none, NewGroups};
|
|
|
|
error -> {Subscription, Ask, Groups}
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
-spec in_subscription(boolean(), presence()) -> boolean().
|
|
|
|
in_subscription(Acc, #presence{to = To, from = JID, type = Type}) ->
|
|
|
|
#jid{user = User, server = Server} = To,
|
2005-04-17 20:08:34 +02:00
|
|
|
process_subscription(in, User, Server, JID, Type, Acc).
|
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
-spec out_subscription(presence()) -> boolean().
|
|
|
|
out_subscription(#presence{from = From, to = To, type = unsubscribed} = Pres) ->
|
|
|
|
#jid{user = User, server = Server} = From,
|
|
|
|
mod_roster:out_subscription(Pres#presence{type = unsubscribe}),
|
|
|
|
mod_roster:in_subscription(false, xmpp:set_from_to(
|
|
|
|
Pres#presence{type = unsubscribe},
|
|
|
|
To, From)),
|
|
|
|
process_subscription(out, User, Server, To, unsubscribed, false);
|
|
|
|
out_subscription(#presence{from = From, to = To, type = Type}) ->
|
|
|
|
#jid{user = User, server = Server} = From,
|
|
|
|
process_subscription(out, User, Server, To, Type, false).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_subscription(Direction, User, Server, JID,
|
|
|
|
_Type, Acc) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
{U1, S1, _} =
|
2015-11-24 16:44:13 +01:00
|
|
|
jid:tolower(jid:remove_resource(JID)),
|
2005-04-17 20:08:34 +02:00
|
|
|
US1 = {U1, S1},
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
2013-03-14 10:33:02 +01:00
|
|
|
SRUsers = lists:usort(lists:flatmap(fun (Group) ->
|
|
|
|
get_group_users(LServer, Group)
|
|
|
|
end,
|
|
|
|
DisplayedGroups)),
|
2005-04-17 20:08:34 +02:00
|
|
|
case lists:member(US1, SRUsers) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
case Direction of
|
|
|
|
in -> {stop, false};
|
|
|
|
out -> stop
|
|
|
|
end;
|
|
|
|
false -> Acc
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
list_groups(Host) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
|
|
|
Mod:list_groups(Host).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2008-04-26 19:37:43 +02:00
|
|
|
groups_with_opts(Host) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
|
|
|
Mod:groups_with_opts(Host).
|
2008-04-26 19:37:43 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
create_group(Host, Group) ->
|
|
|
|
create_group(Host, Group, []).
|
|
|
|
|
|
|
|
create_group(Host, Group, Opts) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2020-03-17 14:35:43 +01:00
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:insert(?GROUP_OPTS_CACHE, {Host, Group}, Opts, cache_nodes(Mod, Host)),
|
|
|
|
ets_cache:clear(?SPECIAL_GROUPS_CACHE, cache_nodes(Mod, Host));
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod:create_group(Host, Group, Opts).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
delete_group(Host, Group) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2020-03-17 14:35:43 +01:00
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?GROUP_OPTS_CACHE, {Host, Group}, cache_nodes(Mod, Host)),
|
|
|
|
ets_cache:clear(?USER_GROUPS_CACHE, cache_nodes(Mod, Host)),
|
|
|
|
ets_cache:clear(?GROUP_EXPLICIT_USERS_CACHE, cache_nodes(Mod, Host)),
|
|
|
|
ets_cache:clear(?SPECIAL_GROUPS_CACHE, cache_nodes(Mod, Host));
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod:delete_group(Host, Group).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
get_group_opts(Host, Group) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2020-03-17 14:35:43 +01:00
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?GROUP_OPTS_CACHE, {Host, Group},
|
|
|
|
fun() ->
|
|
|
|
case Mod:get_group_opts(Host, Group) of
|
|
|
|
error -> error;
|
|
|
|
V -> {cache, V}
|
|
|
|
end
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
Mod:get_group_opts(Host, Group)
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
set_group_opts(Host, Group, Opts) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2020-03-17 14:35:43 +01:00
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:insert(?GROUP_OPTS_CACHE, {Host, Group}, Opts, cache_nodes(Mod, Host)),
|
|
|
|
ets_cache:clear(?SPECIAL_GROUPS_CACHE, cache_nodes(Mod, Host));
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod:set_group_opts(Host, Group, Opts).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
get_user_groups(US) ->
|
|
|
|
Host = element(2, US),
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2020-03-17 14:35:43 +01:00
|
|
|
UG = case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?USER_GROUPS_CACHE, {Host, US},
|
|
|
|
fun() ->
|
|
|
|
{cache, Mod:get_user_groups(US, Host)}
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
Mod:get_user_groups(US, Host)
|
|
|
|
end,
|
|
|
|
UG ++ get_special_users_groups(Host).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2011-07-27 10:20:22 +02:00
|
|
|
is_group_enabled(Host1, Group1) ->
|
|
|
|
{Host, Group} = split_grouphost(Host1, Group1),
|
2012-04-27 11:52:05 +02:00
|
|
|
case get_group_opts(Host, Group) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> false;
|
|
|
|
Opts -> not lists:member(disabled, Opts)
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2009-12-21 15:35:09 +01:00
|
|
|
%% @spec (Host::string(), Group::string(), Opt::atom(), Default) -> OptValue | Default
|
2005-04-17 20:08:34 +02:00
|
|
|
get_group_opt(Host, Group, Opt, Default) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
case get_group_opts(Host, Group) of
|
2013-03-14 10:33:02 +01:00
|
|
|
error -> Default;
|
|
|
|
Opts ->
|
|
|
|
case lists:keysearch(Opt, 1, Opts) of
|
|
|
|
{value, {_, Val}} -> Val;
|
|
|
|
false -> Default
|
|
|
|
end
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2011-02-21 22:33:23 +01:00
|
|
|
get_online_users(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:usort([{U, S}
|
|
|
|
|| {U, S, _} <- ejabberd_sm:get_vh_session_list(Host)]).
|
2011-02-21 22:33:23 +01:00
|
|
|
|
2011-07-27 10:20:22 +02:00
|
|
|
get_group_users(Host1, Group1) ->
|
|
|
|
{Host, Group} = split_grouphost(Host1, Group1),
|
2020-03-16 13:29:05 +01:00
|
|
|
get_group_users(Host, Group, get_group_opts(Host, Group)).
|
2005-09-29 03:04:24 +02:00
|
|
|
|
2011-02-21 22:33:23 +01:00
|
|
|
get_group_users(Host, Group, GroupOpts) ->
|
2008-04-26 19:37:43 +02:00
|
|
|
case proplists:get_value(all_users, GroupOpts, false) of
|
2017-05-11 13:37:21 +02:00
|
|
|
true -> ejabberd_auth:get_users(Host);
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> []
|
|
|
|
end
|
|
|
|
++
|
|
|
|
case proplists:get_value(online_users, GroupOpts, false)
|
|
|
|
of
|
|
|
|
true -> get_online_users(Host);
|
|
|
|
false -> []
|
|
|
|
end
|
|
|
|
++ get_group_explicit_users(Host, Group).
|
|
|
|
|
2005-09-29 03:04:24 +02:00
|
|
|
get_group_explicit_users(Host, Group) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2020-03-17 14:35:43 +01:00
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?GROUP_EXPLICIT_USERS_CACHE, {Host, Group},
|
|
|
|
fun() ->
|
|
|
|
{cache, Mod:get_group_explicit_users(Host, Group)}
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
Mod:get_group_explicit_users(Host, Group)
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2011-07-27 10:20:22 +02:00
|
|
|
get_group_name(Host1, Group1) ->
|
|
|
|
{Host, Group} = split_grouphost(Host1, Group1),
|
2005-04-17 20:08:34 +02:00
|
|
|
get_group_opt(Host, Group, name, Group).
|
|
|
|
|
2011-02-21 22:33:23 +01:00
|
|
|
%% Get list of names of groups that have @all@/@online@/etc in the memberlist
|
2008-04-26 19:37:43 +02:00
|
|
|
get_special_users_groups(Host) ->
|
2020-03-17 14:35:43 +01:00
|
|
|
Extract =
|
|
|
|
fun() ->
|
|
|
|
lists:filtermap(
|
|
|
|
fun({Group, Opts}) ->
|
|
|
|
case proplists:get_value(all_users, Opts, false) orelse
|
|
|
|
proplists:get_value(online_users, Opts, false) of
|
|
|
|
true -> {true, Group};
|
|
|
|
false -> false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
groups_with_opts(Host))
|
|
|
|
end,
|
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?SPECIAL_GROUPS_CACHE, {Host, false},
|
|
|
|
fun() ->
|
|
|
|
{cache, Extract()}
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
Extract()
|
|
|
|
end.
|
|
|
|
|
2005-09-29 03:04:24 +02:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
%% Get list of names of groups that have @online@ in the memberlist
|
2013-03-14 10:33:02 +01:00
|
|
|
get_special_users_groups_online(Host) ->
|
2020-03-17 14:35:43 +01:00
|
|
|
Extract =
|
|
|
|
fun() ->
|
|
|
|
lists:filtermap(
|
|
|
|
fun({Group, Opts}) ->
|
|
|
|
case proplists:get_value(online_users, Opts, false) of
|
|
|
|
true -> {true, Group};
|
|
|
|
false -> false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
groups_with_opts(Host))
|
|
|
|
end,
|
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?SPECIAL_GROUPS_CACHE, {Host, true},
|
|
|
|
fun() ->
|
|
|
|
{cache, Extract()}
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
Extract()
|
|
|
|
end.
|
2008-04-26 19:37:43 +02:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
%% Given two lists of groupnames and their options,
|
|
|
|
%% return the list of displayed groups to the second list
|
2013-03-14 10:33:02 +01:00
|
|
|
displayed_groups(GroupsOpts, SelectedGroupsOpts) ->
|
|
|
|
DisplayedGroups = lists:usort(lists:flatmap(fun
|
|
|
|
({_Group, Opts}) ->
|
|
|
|
[G
|
|
|
|
|| G
|
|
|
|
<- proplists:get_value(displayed_groups,
|
|
|
|
Opts,
|
|
|
|
[]),
|
|
|
|
not
|
|
|
|
lists:member(disabled,
|
|
|
|
Opts)]
|
|
|
|
end,
|
|
|
|
SelectedGroupsOpts)),
|
|
|
|
[G
|
|
|
|
|| G <- DisplayedGroups,
|
|
|
|
not
|
|
|
|
lists:member(disabled,
|
|
|
|
proplists:get_value(G, GroupsOpts, []))].
|
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
%% Given a list of group names with options,
|
|
|
|
%% for those that have @all@ in memberlist,
|
|
|
|
%% get the list of groups displayed
|
2008-04-26 19:37:43 +02:00
|
|
|
get_special_displayed_groups(GroupsOpts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Groups = lists:filter(fun ({_Group, Opts}) ->
|
|
|
|
proplists:get_value(all_users, Opts, false)
|
|
|
|
end,
|
|
|
|
GroupsOpts),
|
2008-04-26 19:37:43 +02:00
|
|
|
displayed_groups(GroupsOpts, Groups).
|
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
%% Given a username and server, and a list of group names with options,
|
|
|
|
%% for the list of groups of that server that user is member
|
|
|
|
%% get the list of groups displayed
|
2008-04-26 19:37:43 +02:00
|
|
|
get_user_displayed_groups(LUser, LServer, GroupsOpts) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Groups = Mod:get_user_displayed_groups(LUser, LServer, GroupsOpts),
|
2008-04-26 19:37:43 +02:00
|
|
|
displayed_groups(GroupsOpts, Groups).
|
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
%% @doc Get the list of groups that are displayed to this user
|
2005-04-17 20:08:34 +02:00
|
|
|
get_user_displayed_groups(US) ->
|
|
|
|
Host = element(2, US),
|
2013-03-14 10:33:02 +01:00
|
|
|
DisplayedGroups1 = lists:usort(lists:flatmap(fun
|
|
|
|
(Group) ->
|
|
|
|
case
|
|
|
|
is_group_enabled(Host,
|
|
|
|
Group)
|
|
|
|
of
|
|
|
|
true ->
|
|
|
|
get_group_opt(Host,
|
|
|
|
Group,
|
|
|
|
displayed_groups,
|
|
|
|
[]);
|
|
|
|
false -> []
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
get_user_groups(US))),
|
|
|
|
[Group
|
|
|
|
|| Group <- DisplayedGroups1,
|
|
|
|
is_group_enabled(Host, Group)].
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2011-07-01 17:11:09 +02:00
|
|
|
is_user_in_group(US, Group, Host) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
|
|
|
case Mod:is_user_in_group(US, Group, Host) of
|
|
|
|
false ->
|
|
|
|
lists:member(US, get_group_users(Host, Group));
|
|
|
|
true ->
|
|
|
|
true
|
2006-07-06 16:08:09 +02:00
|
|
|
end.
|
|
|
|
|
2019-08-22 17:12:52 +02:00
|
|
|
%% @spec (Host::string(), {User::string(), Server::string()}, Group::string()) -> {atomic, ok} | error
|
2005-04-17 20:08:34 +02:00
|
|
|
add_user_to_group(Host, US, Group) ->
|
2019-08-22 17:12:52 +02:00
|
|
|
{_LUser, LServer} = US,
|
2019-08-22 17:56:08 +02:00
|
|
|
case lists:member(LServer, ejabberd_config:get_option(hosts)) of
|
2019-08-22 17:12:52 +02:00
|
|
|
true -> add_user_to_group2(Host, US, Group);
|
|
|
|
false ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("Attempted adding to shared roster user of inexistent vhost ~ts", [LServer]),
|
2019-08-22 17:12:52 +02:00
|
|
|
error
|
|
|
|
end.
|
|
|
|
add_user_to_group2(Host, US, Group) ->
|
2008-12-23 20:15:33 +01:00
|
|
|
{LUser, LServer} = US,
|
2015-10-07 00:06:58 +02:00
|
|
|
case ejabberd_regexp:run(LUser, <<"^@.+@\$">>) of
|
2013-03-14 10:33:02 +01:00
|
|
|
match ->
|
2020-03-16 14:57:55 +01:00
|
|
|
GroupOpts = get_group_opts(Host, Group),
|
2013-03-14 10:33:02 +01:00
|
|
|
MoreGroupOpts = case LUser of
|
|
|
|
<<"@all@">> -> [{all_users, true}];
|
|
|
|
<<"@online@">> -> [{online_users, true}];
|
|
|
|
_ -> []
|
|
|
|
end,
|
2020-03-16 14:57:55 +01:00
|
|
|
set_group_opts(Host, Group,
|
|
|
|
GroupOpts ++ MoreGroupOpts);
|
2013-03-14 10:33:02 +01:00
|
|
|
nomatch ->
|
2015-01-07 13:11:53 +01:00
|
|
|
DisplayedToGroups = displayed_to_groups(Group, Host),
|
|
|
|
DisplayedGroups = get_displayed_groups(Group, LServer),
|
|
|
|
push_user_to_displayed(LUser, LServer, Group, Host, both, DisplayedToGroups),
|
|
|
|
push_displayed_to_user(LUser, LServer, Host, both, DisplayedGroups),
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2020-03-17 14:35:43 +01:00
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?USER_GROUPS_CACHE, {Host, US}, cache_nodes(Mod, Host)),
|
|
|
|
ets_cache:delete(?GROUP_EXPLICIT_USERS_CACHE, {Host, Group}, cache_nodes(Mod, Host));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod:add_user_to_group(Host, US, Group)
|
2009-04-22 12:40:11 +02:00
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2015-01-07 13:11:53 +01:00
|
|
|
get_displayed_groups(Group, LServer) ->
|
2020-03-16 13:29:05 +01:00
|
|
|
get_group_opt(LServer, Group, displayed_groups, []).
|
2015-01-07 13:11:53 +01:00
|
|
|
|
|
|
|
push_displayed_to_user(LUser, LServer, Host, Subscription, DisplayedGroups) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[push_members_to_user(LUser, LServer, DGroup, Host,
|
|
|
|
Subscription)
|
|
|
|
|| DGroup <- DisplayedGroups].
|
2008-12-23 20:15:33 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user_from_group(Host, US, Group) ->
|
2008-12-23 20:15:33 +01:00
|
|
|
{LUser, LServer} = US,
|
2015-10-07 00:06:58 +02:00
|
|
|
case ejabberd_regexp:run(LUser, <<"^@.+@\$">>) of
|
2013-03-14 10:33:02 +01:00
|
|
|
match ->
|
2020-03-16 14:57:55 +01:00
|
|
|
GroupOpts = get_group_opts(Host, Group),
|
2013-03-14 10:33:02 +01:00
|
|
|
NewGroupOpts = case LUser of
|
|
|
|
<<"@all@">> ->
|
|
|
|
lists:filter(fun (X) -> X /= {all_users, true}
|
|
|
|
end,
|
|
|
|
GroupOpts);
|
|
|
|
<<"@online@">> ->
|
|
|
|
lists:filter(fun (X) -> X /= {online_users, true}
|
|
|
|
end,
|
|
|
|
GroupOpts)
|
|
|
|
end,
|
2020-03-16 14:57:55 +01:00
|
|
|
set_group_opts(Host, Group, NewGroupOpts);
|
2013-03-14 10:33:02 +01:00
|
|
|
nomatch ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
2020-03-17 14:35:43 +01:00
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?USER_GROUPS_CACHE, {Host, US}, cache_nodes(Mod, Host)),
|
|
|
|
ets_cache:delete(?GROUP_EXPLICIT_USERS_CACHE, {Host, Group}, cache_nodes(Mod, Host));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
2016-04-14 10:45:43 +02:00
|
|
|
Result = Mod:remove_user_from_group(Host, US, Group),
|
2015-01-07 13:11:53 +01:00
|
|
|
DisplayedToGroups = displayed_to_groups(Group, Host),
|
|
|
|
DisplayedGroups = get_displayed_groups(Group, LServer),
|
|
|
|
push_user_to_displayed(LUser, LServer, Group, Host, remove, DisplayedToGroups),
|
|
|
|
push_displayed_to_user(LUser, LServer, Host, remove, DisplayedGroups),
|
2013-03-14 10:33:02 +01:00
|
|
|
Result
|
2009-04-22 12:40:11 +02:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
push_members_to_user(LUser, LServer, Group, Host,
|
|
|
|
Subscription) ->
|
2020-03-16 13:29:05 +01:00
|
|
|
GroupOpts = get_group_opts(LServer, Group),
|
2008-12-23 20:15:33 +01:00
|
|
|
GroupName = proplists:get_value(name, GroupOpts, Group),
|
|
|
|
Members = get_group_users(Host, Group),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun ({U, S}) ->
|
|
|
|
push_roster_item(LUser, LServer, U, S, GroupName,
|
|
|
|
Subscription)
|
|
|
|
end,
|
|
|
|
Members).
|
2008-12-23 20:15:33 +01:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec register_user(binary(), binary()) -> ok.
|
2008-12-23 20:15:33 +01:00
|
|
|
register_user(User, Server) ->
|
|
|
|
Groups = get_user_groups({User, Server}),
|
2013-03-14 10:33:02 +01:00
|
|
|
[push_user_to_displayed(User, Server, Group, Server,
|
2015-01-07 13:11:53 +01:00
|
|
|
both, displayed_to_groups(Group, Server))
|
2016-08-09 09:56:32 +02:00
|
|
|
|| Group <- Groups],
|
|
|
|
ok.
|
2008-12-23 20:15:33 +01:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec remove_user(binary(), binary()) -> ok.
|
2008-12-23 20:15:33 +01:00
|
|
|
remove_user(User, Server) ->
|
|
|
|
push_user_to_members(User, Server, remove).
|
2007-08-23 02:51:54 +02:00
|
|
|
|
2008-12-23 20:15:33 +01:00
|
|
|
push_user_to_members(User, Server, Subscription) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2008-04-26 19:37:43 +02:00
|
|
|
GroupsOpts = groups_with_opts(LServer),
|
2013-03-14 10:33:02 +01:00
|
|
|
SpecialGroups =
|
|
|
|
get_special_displayed_groups(GroupsOpts),
|
|
|
|
UserGroups = get_user_displayed_groups(LUser, LServer,
|
|
|
|
GroupsOpts),
|
|
|
|
lists:foreach(fun (Group) ->
|
|
|
|
remove_user_from_group(LServer, {LUser, LServer},
|
|
|
|
Group),
|
|
|
|
GroupOpts = proplists:get_value(Group, GroupsOpts,
|
|
|
|
[]),
|
|
|
|
GroupName = proplists:get_value(name, GroupOpts,
|
|
|
|
Group),
|
|
|
|
lists:foreach(fun ({U, S}) ->
|
|
|
|
push_roster_item(U, S, LUser,
|
|
|
|
LServer,
|
|
|
|
GroupName,
|
|
|
|
Subscription)
|
|
|
|
end,
|
|
|
|
get_group_users(LServer, Group,
|
|
|
|
GroupOpts))
|
|
|
|
end,
|
|
|
|
lists:usort(SpecialGroups ++ UserGroups)).
|
|
|
|
|
2015-01-07 13:11:53 +01:00
|
|
|
push_user_to_displayed(LUser, LServer, Group, Host, Subscription, DisplayedToGroupsOpts) ->
|
2020-03-16 13:29:05 +01:00
|
|
|
GroupName = get_group_opt(Host, Group, name, Group),
|
2013-03-14 10:33:02 +01:00
|
|
|
[push_user_to_group(LUser, LServer, GroupD, Host,
|
|
|
|
GroupName, Subscription)
|
2015-06-04 15:47:02 +02:00
|
|
|
|| GroupD <- DisplayedToGroupsOpts].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
push_user_to_group(LUser, LServer, Group, Host,
|
|
|
|
GroupName, Subscription) ->
|
|
|
|
lists:foreach(fun ({U, S})
|
|
|
|
when (U == LUser) and (S == LServer) ->
|
|
|
|
ok;
|
|
|
|
({U, S}) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case lists:member(S, ejabberd_option:hosts()) of
|
2019-02-18 12:37:29 +01:00
|
|
|
true ->
|
|
|
|
push_roster_item(U, S, LUser, LServer, GroupName,
|
|
|
|
Subscription);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
get_group_users(Host, Group)).
|
2008-12-23 20:15:33 +01:00
|
|
|
|
|
|
|
%% Get list of groups to which this group is displayed
|
|
|
|
displayed_to_groups(GroupName, LServer) ->
|
|
|
|
GroupsOpts = groups_with_opts(LServer),
|
2015-06-04 15:47:02 +02:00
|
|
|
Gs = lists:filter(fun ({_Group, Opts}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:member(GroupName,
|
|
|
|
proplists:get_value(displayed_groups,
|
|
|
|
Opts, []))
|
|
|
|
end,
|
2015-06-04 15:47:02 +02:00
|
|
|
GroupsOpts),
|
|
|
|
[Name || {Name, _} <- Gs].
|
2008-12-23 20:15:33 +01:00
|
|
|
|
2015-01-07 13:11:53 +01:00
|
|
|
push_item(User, Server, Item) ->
|
2018-03-19 10:32:43 +01:00
|
|
|
mod_roster:push_item(jid:make(User, Server),
|
|
|
|
Item#roster{subscription = none},
|
|
|
|
Item).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
push_roster_item(User, Server, ContactU, ContactS,
|
|
|
|
GroupName, Subscription) ->
|
|
|
|
Item = #roster{usj =
|
|
|
|
{User, Server, {ContactU, ContactS, <<"">>}},
|
|
|
|
us = {User, Server}, jid = {ContactU, ContactS, <<"">>},
|
|
|
|
name = <<"">>, subscription = Subscription, ask = none,
|
2008-12-23 20:15:33 +01:00
|
|
|
groups = [GroupName]},
|
2015-01-07 13:11:53 +01:00
|
|
|
push_item(User, Server, Item).
|
2008-12-23 20:15:33 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec c2s_self_presence({presence(), ejabberd_c2s:state()})
|
|
|
|
-> {presence(), ejabberd_c2s:state()}.
|
|
|
|
c2s_self_presence(Acc) ->
|
|
|
|
Acc.
|
2011-02-21 22:33:23 +01:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec unset_presence(binary(), binary(), binary(), binary()) -> ok.
|
2011-02-21 22:33:23 +01:00
|
|
|
unset_presence(LUser, LServer, Resource, Status) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Resources = ejabberd_sm:get_user_resources(LUser,
|
|
|
|
LServer),
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Unset_presence for ~p @ ~p / ~p -> ~p "
|
2013-03-14 10:33:02 +01:00
|
|
|
"(~p resources)",
|
2011-02-21 22:33:23 +01:00
|
|
|
[LUser, LServer, Resource, Status, length(Resources)]),
|
|
|
|
case length(Resources) of
|
2013-03-14 10:33:02 +01:00
|
|
|
0 ->
|
|
|
|
OnlineGroups = get_special_users_groups_online(LServer),
|
2020-03-17 14:35:43 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(OG) ->
|
|
|
|
DisplayedToGroups = displayed_to_groups(OG, LServer),
|
|
|
|
push_user_to_displayed(LUser, LServer, OG,
|
|
|
|
LServer, remove, DisplayedToGroups),
|
|
|
|
push_displayed_to_user(LUser, LServer,
|
|
|
|
LServer, remove, DisplayedToGroups)
|
|
|
|
end,
|
|
|
|
OnlineGroups);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> ok
|
2011-02-21 22:33:23 +01:00
|
|
|
end.
|
2007-08-23 02:51:54 +02:00
|
|
|
|
|
|
|
%%---------------------
|
|
|
|
%% Web Admin
|
|
|
|
%%---------------------
|
|
|
|
|
2007-12-26 15:53:37 +01:00
|
|
|
webadmin_menu(Acc, _Host, Lang) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
[{<<"shared-roster">>, translate:translate(Lang, ?T("Shared Roster Groups"))}
|
2013-03-14 10:33:02 +01:00
|
|
|
| Acc].
|
2007-08-23 02:51:54 +02:00
|
|
|
|
|
|
|
webadmin_page(_, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
#request{us = _US, path = [<<"shared-roster">>],
|
|
|
|
q = Query, lang = Lang} =
|
|
|
|
_Request) ->
|
2007-08-23 02:51:54 +02:00
|
|
|
Res = list_shared_roster_groups(Host, Query, Lang),
|
|
|
|
{stop, Res};
|
|
|
|
webadmin_page(_, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
#request{us = _US, path = [<<"shared-roster">>, Group],
|
|
|
|
q = Query, lang = Lang} =
|
|
|
|
_Request) ->
|
2007-08-23 02:51:54 +02:00
|
|
|
Res = shared_roster_group(Host, Group, Query, Lang),
|
|
|
|
{stop, Res};
|
|
|
|
webadmin_page(Acc, _, _) -> Acc.
|
|
|
|
|
|
|
|
list_shared_roster_groups(Host, Query, Lang) ->
|
|
|
|
Res = list_sr_groups_parse_query(Host, Query),
|
2020-03-16 14:57:55 +01:00
|
|
|
SRGroups = list_groups(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
FGroups = (?XAE(<<"table">>, [],
|
|
|
|
[?XE(<<"tbody">>,
|
|
|
|
(lists:map(fun (Group) ->
|
|
|
|
?XE(<<"tr">>,
|
|
|
|
[?XE(<<"td">>,
|
|
|
|
[?INPUT(<<"checkbox">>,
|
|
|
|
<<"selected">>,
|
|
|
|
Group)]),
|
|
|
|
?XE(<<"td">>,
|
|
|
|
[?AC(<<Group/binary, "/">>,
|
|
|
|
Group)])])
|
|
|
|
end,
|
|
|
|
lists:sort(SRGroups))
|
|
|
|
++
|
|
|
|
[?XE(<<"tr">>,
|
|
|
|
[?X(<<"td">>),
|
|
|
|
?XE(<<"td">>,
|
|
|
|
[?INPUT(<<"text">>, <<"namenew">>,
|
|
|
|
<<"">>)]),
|
|
|
|
?XE(<<"td">>,
|
|
|
|
[?INPUTT(<<"submit">>, <<"addnew">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("Add New"))])])]))])),
|
|
|
|
(?H1GL((translate:translate(Lang, ?T("Shared Roster Groups"))),
|
2019-08-27 12:29:05 +02:00
|
|
|
<<"mod-shared-roster">>, <<"mod_shared_roster">>))
|
2013-03-14 10:33:02 +01:00
|
|
|
++
|
|
|
|
case Res of
|
2019-06-22 16:08:45 +02:00
|
|
|
ok -> [?XREST(?T("Submitted"))];
|
|
|
|
error -> [?XREST(?T("Bad format"))];
|
2013-03-14 10:33:02 +01:00
|
|
|
nothing -> []
|
|
|
|
end
|
|
|
|
++
|
|
|
|
[?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
[FGroups, ?BR,
|
|
|
|
?INPUTT(<<"submit">>, <<"delete">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
?T("Delete Selected"))])].
|
2007-08-23 02:51:54 +02:00
|
|
|
|
|
|
|
list_sr_groups_parse_query(Host, Query) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:keysearch(<<"addnew">>, 1, Query) of
|
|
|
|
{value, _} -> list_sr_groups_parse_addnew(Host, Query);
|
|
|
|
_ ->
|
|
|
|
case lists:keysearch(<<"delete">>, 1, Query) of
|
|
|
|
{value, _} -> list_sr_groups_parse_delete(Host, Query);
|
|
|
|
_ -> nothing
|
|
|
|
end
|
2007-08-23 02:51:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
list_sr_groups_parse_addnew(Host, Query) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:keysearch(<<"namenew">>, 1, Query) of
|
|
|
|
{value, {_, Group}} when Group /= <<"">> ->
|
2020-03-16 14:57:55 +01:00
|
|
|
create_group(Host, Group),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
error
|
2007-08-23 02:51:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
list_sr_groups_parse_delete(Host, Query) ->
|
2020-03-16 14:57:55 +01:00
|
|
|
SRGroups = list_groups(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (Group) ->
|
|
|
|
case lists:member({<<"selected">>, Group}, Query) of
|
2020-03-16 14:57:55 +01:00
|
|
|
true -> delete_group(Host, Group);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
SRGroups),
|
2007-08-23 02:51:54 +02:00
|
|
|
ok.
|
|
|
|
|
|
|
|
shared_roster_group(Host, Group, Query, Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Res = shared_roster_group_parse_query(Host, Group,
|
|
|
|
Query),
|
2020-03-16 14:57:55 +01:00
|
|
|
GroupOpts = get_group_opts(Host, Group),
|
2013-03-14 10:33:02 +01:00
|
|
|
Name = get_opt(GroupOpts, name, <<"">>),
|
|
|
|
Description = get_opt(GroupOpts, description, <<"">>),
|
2007-08-23 02:51:54 +02:00
|
|
|
AllUsers = get_opt(GroupOpts, all_users, false),
|
2011-02-21 22:33:23 +01:00
|
|
|
OnlineUsers = get_opt(GroupOpts, online_users, false),
|
2013-03-14 10:33:02 +01:00
|
|
|
DisplayedGroups = get_opt(GroupOpts, displayed_groups,
|
|
|
|
[]),
|
2020-03-16 14:57:55 +01:00
|
|
|
Members = get_group_explicit_users(Host,
|
|
|
|
Group),
|
2013-03-14 10:33:02 +01:00
|
|
|
FMembers = iolist_to_binary(
|
|
|
|
[if AllUsers -> <<"@all@\n">>;
|
|
|
|
true -> <<"">>
|
|
|
|
end,
|
|
|
|
if OnlineUsers -> <<"@online@\n">>;
|
|
|
|
true -> <<"">>
|
|
|
|
end,
|
|
|
|
[[us_to_list(Member), $\n] || Member <- Members]]),
|
|
|
|
FDisplayedGroups = [<<DG/binary, $\n>> || DG <- DisplayedGroups],
|
|
|
|
DescNL = length(ejabberd_regexp:split(Description,
|
|
|
|
<<"\n">>)),
|
|
|
|
FGroup = (?XAE(<<"table">>,
|
|
|
|
[{<<"class">>, <<"withtextareas">>}],
|
|
|
|
[?XE(<<"tbody">>,
|
|
|
|
[?XE(<<"tr">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
[?XCT(<<"td">>, ?T("Name:")),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"td">>,
|
|
|
|
[?INPUT(<<"text">>, <<"name">>, Name)])]),
|
|
|
|
?XE(<<"tr">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
[?XCT(<<"td">>, ?T("Description:")),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"td">>,
|
|
|
|
[?TEXTAREA(<<"description">>,
|
2016-09-24 22:34:28 +02:00
|
|
|
integer_to_binary(lists:max([3,
|
2013-03-14 10:33:02 +01:00
|
|
|
DescNL])),
|
|
|
|
<<"20">>, Description)])]),
|
|
|
|
?XE(<<"tr">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
[?XCT(<<"td">>, ?T("Members:")),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"td">>,
|
|
|
|
[?TEXTAREA(<<"members">>,
|
2016-09-24 22:34:28 +02:00
|
|
|
integer_to_binary(lists:max([3,
|
2017-01-13 12:16:23 +01:00
|
|
|
length(Members)+3])),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"20">>, FMembers)])]),
|
|
|
|
?XE(<<"tr">>,
|
2019-06-22 16:08:45 +02:00
|
|
|
[?XCT(<<"td">>, ?T("Displayed Groups:")),
|
2013-03-14 10:33:02 +01:00
|
|
|
?XE(<<"td">>,
|
|
|
|
[?TEXTAREA(<<"dispgroups">>,
|
2016-09-24 22:34:28 +02:00
|
|
|
integer_to_binary(lists:max([3, length(FDisplayedGroups)])),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"20">>,
|
|
|
|
list_to_binary(FDisplayedGroups))])])])])),
|
2019-06-22 16:08:45 +02:00
|
|
|
(?H1GL((translate:translate(Lang, ?T("Shared Roster Groups"))),
|
2019-08-27 12:29:05 +02:00
|
|
|
<<"mod-shared-roster">>, <<"mod_shared_roster">>))
|
2013-03-14 10:33:02 +01:00
|
|
|
++
|
2019-06-22 16:08:45 +02:00
|
|
|
[?XC(<<"h2">>, <<(translate:translate(Lang, ?T("Group ")))/binary, Group/binary>>)] ++
|
2007-08-23 02:51:54 +02:00
|
|
|
case Res of
|
2019-06-22 16:08:45 +02:00
|
|
|
ok -> [?XREST(?T("Submitted"))];
|
2019-08-22 17:12:52 +02:00
|
|
|
{error_jids, NonAddedList1} ->
|
|
|
|
NonAddedList2 = [jid:encode({U,S,<<>>}) || {U,S} <- NonAddedList1],
|
|
|
|
NonAddedList3 = str:join(NonAddedList2, <<", ">>),
|
|
|
|
NonAddedText1 = translate:translate(Lang, ?T("Members not added (inexistent vhost): ")),
|
|
|
|
NonAddedText2 = str:concat(NonAddedText1, NonAddedList3),
|
|
|
|
[?XRES(NonAddedText2)];
|
2019-06-22 16:08:45 +02:00
|
|
|
error -> [?XREST(?T("Bad format"))];
|
2013-03-14 10:33:02 +01:00
|
|
|
nothing -> []
|
|
|
|
end
|
|
|
|
++
|
|
|
|
[?XAE(<<"form">>,
|
|
|
|
[{<<"action">>, <<"">>}, {<<"method">>, <<"post">>}],
|
|
|
|
[FGroup, ?BR,
|
2019-06-22 16:08:45 +02:00
|
|
|
?INPUTT(<<"submit">>, <<"submit">>, ?T("Submit"))])].
|
2007-08-23 02:51:54 +02:00
|
|
|
|
|
|
|
shared_roster_group_parse_query(Host, Group, Query) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case lists:keysearch(<<"submit">>, 1, Query) of
|
|
|
|
{value, _} ->
|
|
|
|
{value, {_, Name}} = lists:keysearch(<<"name">>, 1,
|
|
|
|
Query),
|
|
|
|
{value, {_, Description}} =
|
|
|
|
lists:keysearch(<<"description">>, 1, Query),
|
|
|
|
{value, {_, SMembers}} = lists:keysearch(<<"members">>,
|
|
|
|
1, Query),
|
|
|
|
{value, {_, SDispGroups}} =
|
|
|
|
lists:keysearch(<<"dispgroups">>, 1, Query),
|
|
|
|
NameOpt = if Name == <<"">> -> [];
|
|
|
|
true -> [{name, Name}]
|
|
|
|
end,
|
|
|
|
DescriptionOpt = if Description == <<"">> -> [];
|
|
|
|
true -> [{description, Description}]
|
|
|
|
end,
|
|
|
|
DispGroups = str:tokens(SDispGroups, <<"\r\n">>),
|
|
|
|
DispGroupsOpt = if DispGroups == [] -> [];
|
|
|
|
true -> [{displayed_groups, DispGroups}]
|
|
|
|
end,
|
2020-03-16 14:57:55 +01:00
|
|
|
OldMembers = get_group_explicit_users(Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
Group),
|
|
|
|
SJIDs = str:tokens(SMembers, <<", \r\n">>),
|
|
|
|
NewMembers = lists:foldl(fun (_SJID, error) -> error;
|
|
|
|
(SJID, USs) ->
|
|
|
|
case SJID of
|
|
|
|
<<"@all@">> -> USs;
|
|
|
|
<<"@online@">> -> USs;
|
|
|
|
_ ->
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(SJID) of
|
|
|
|
JID ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[{JID#jid.luser,
|
|
|
|
JID#jid.lserver}
|
2017-02-26 08:07:12 +01:00
|
|
|
| USs]
|
|
|
|
catch _:{bad_jid, _} ->
|
|
|
|
error
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], SJIDs),
|
|
|
|
AllUsersOpt = case lists:member(<<"@all@">>, SJIDs) of
|
|
|
|
true -> [{all_users, true}];
|
|
|
|
false -> []
|
|
|
|
end,
|
|
|
|
OnlineUsersOpt = case lists:member(<<"@online@">>,
|
|
|
|
SJIDs)
|
|
|
|
of
|
|
|
|
true -> [{online_users, true}];
|
|
|
|
false -> []
|
|
|
|
end,
|
2015-01-07 13:11:53 +01:00
|
|
|
CurrentDisplayedGroups = get_displayed_groups(Group, Host),
|
|
|
|
AddedDisplayedGroups = DispGroups -- CurrentDisplayedGroups,
|
|
|
|
RemovedDisplayedGroups = CurrentDisplayedGroups -- DispGroups,
|
|
|
|
displayed_groups_update(OldMembers, RemovedDisplayedGroups, remove),
|
|
|
|
displayed_groups_update(OldMembers, AddedDisplayedGroups, both),
|
2020-03-16 14:57:55 +01:00
|
|
|
set_group_opts(Host, Group,
|
|
|
|
NameOpt ++
|
|
|
|
DispGroupsOpt ++
|
|
|
|
DescriptionOpt ++
|
|
|
|
AllUsersOpt ++ OnlineUsersOpt),
|
2013-03-14 10:33:02 +01:00
|
|
|
if NewMembers == error -> error;
|
|
|
|
true ->
|
|
|
|
AddedMembers = NewMembers -- OldMembers,
|
|
|
|
RemovedMembers = OldMembers -- NewMembers,
|
2020-03-16 14:57:55 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(US) ->
|
|
|
|
remove_user_from_group(Host,
|
|
|
|
US,
|
|
|
|
Group)
|
|
|
|
end,
|
|
|
|
RemovedMembers),
|
|
|
|
NonAddedMembers = lists:filter(
|
|
|
|
fun(US) ->
|
|
|
|
error == add_user_to_group(Host, US,
|
|
|
|
Group)
|
|
|
|
end,
|
|
|
|
AddedMembers),
|
2019-08-22 17:12:52 +02:00
|
|
|
case NonAddedMembers of
|
|
|
|
[] -> ok;
|
|
|
|
_ -> {error_jids, NonAddedMembers}
|
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
_ -> nothing
|
2007-08-23 02:51:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_opt(Opts, Opt, Default) ->
|
|
|
|
case lists:keysearch(Opt, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, {_, Val}} -> Val;
|
|
|
|
false -> Default
|
2007-08-23 02:51:54 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
us_to_list({User, Server}) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
jid:encode({User, Server, <<"">>}).
|
2011-07-27 10:20:22 +02:00
|
|
|
|
|
|
|
split_grouphost(Host, Group) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case str:tokens(Group, <<"@">>) of
|
|
|
|
[GroupName, HostName] -> {HostName, GroupName};
|
|
|
|
[_] -> {Host, Group}
|
2011-07-27 10:20:22 +02:00
|
|
|
end.
|
2012-04-27 11:52:05 +02:00
|
|
|
|
2015-01-07 13:11:53 +01:00
|
|
|
displayed_groups_update(Members, DisplayedGroups, Subscription) ->
|
2018-03-19 10:32:43 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun({U, S}) ->
|
|
|
|
push_displayed_to_user(U, S, S, Subscription, DisplayedGroups)
|
|
|
|
end, Members).
|
2015-01-07 13:11:53 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
opts_to_binary(Opts) ->
|
|
|
|
lists:map(
|
|
|
|
fun({name, Name}) ->
|
|
|
|
{name, iolist_to_binary(Name)};
|
|
|
|
({description, Desc}) ->
|
|
|
|
{description, iolist_to_binary(Desc)};
|
|
|
|
({displayed_groups, Gs}) ->
|
|
|
|
{displayed_groups, [iolist_to_binary(G) || G <- Gs]};
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Opts).
|
|
|
|
|
2016-04-14 10:45:43 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info() ->
|
|
|
|
[{<<"sr_group">>, 3}, {<<"sr_user">>, 3}].
|
|
|
|
|
|
|
|
import_start(LServer, DBType) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:init(LServer, []).
|
2016-04-14 10:45:43 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, {sql, _}, DBType, Tab, L) ->
|
2016-04-14 10:45:43 +02:00
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
2016-11-22 14:48:01 +01:00
|
|
|
Mod:import(LServer, Tab, L).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(db_type) ->
|
2020-03-17 14:35:43 +01:00
|
|
|
econf:db_type(?MODULE);
|
|
|
|
mod_opt_type(use_cache) ->
|
|
|
|
econf:bool();
|
|
|
|
mod_opt_type(cache_size) ->
|
|
|
|
econf:pos_int(infinity);
|
|
|
|
mod_opt_type(cache_missed) ->
|
|
|
|
econf:bool();
|
|
|
|
mod_opt_type(cache_life_time) ->
|
|
|
|
econf:timeout(second, infinity).
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(Host) ->
|
2020-03-17 14:35:43 +01:00
|
|
|
[{db_type, ejabberd_config:default_db(Host, ?MODULE)},
|
|
|
|
{use_cache, ejabberd_option:use_cache(Host)},
|
|
|
|
{cache_size, ejabberd_option:cache_size(Host)},
|
|
|
|
{cache_missed, ejabberd_option:cache_missed(Host)},
|
|
|
|
{cache_life_time, ejabberd_option:cache_life_time(Host)}].
|
2020-01-08 10:24:51 +01:00
|
|
|
|
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("This module enables you to create shared roster groups: "
|
|
|
|
"groups of accounts that can see members from (other) groups "
|
|
|
|
"in their rosters."), "",
|
|
|
|
?T("The big advantages of this feature are that end users do not "
|
|
|
|
"need to manually add all users to their rosters, and that they "
|
|
|
|
"cannot permanently delete users from the shared roster groups. "
|
|
|
|
"A shared roster group can have members from any XMPP server, "
|
|
|
|
"but the presence will only be available from and to members of "
|
|
|
|
"the same virtual host where the group is created. It still "
|
|
|
|
"allows the users to have / add their own contacts, as it does "
|
|
|
|
"not replace the standard roster. Instead, the shared roster "
|
|
|
|
"contacts are merged to the relevant users at retrieval time. "
|
|
|
|
"The standard user rosters thus stay unmodified."), "",
|
|
|
|
?T("Shared roster groups can be edited only via the Web Admin. "
|
|
|
|
"Each group has unique identification and those parameters:"), "",
|
|
|
|
?T("- Name: The group's name will be displayed in the roster."), "",
|
|
|
|
?T("- Description: of the group, which has no effect."), "",
|
|
|
|
?T("- Members: A list of JIDs of group members, entered one per "
|
|
|
|
"line in the Web Admin. The special member directive '@all@' "
|
|
|
|
"represents all the registered users in the virtual host; "
|
|
|
|
"which is only recommended for a small server with just a few "
|
|
|
|
"hundred users. The special member directive '@online@' "
|
|
|
|
"represents the online users in the virtual host. With those "
|
|
|
|
"two directives, the actual list of members in those shared "
|
|
|
|
"rosters is generated dynamically at retrieval time."), "",
|
|
|
|
?T("- Displayed groups: A list of groups that will be in the "
|
|
|
|
"rosters of this group's members. A group of other vhost can "
|
|
|
|
"be identified with 'groupid@vhost'."), "",
|
|
|
|
?T("This module depends on 'mod_roster'. "
|
|
|
|
"If not enabled, roster queries will return 503 errors.")],
|
|
|
|
opts =>
|
|
|
|
[{db_type,
|
|
|
|
#{value => "mnesia | sql",
|
|
|
|
desc =>
|
|
|
|
?T("Define the type of storage where the module will create "
|
|
|
|
"the tables and store user information. The default is "
|
|
|
|
"the storage defined by the global option 'default_db', "
|
|
|
|
"or 'mnesia' if omitted. If 'sql' value is defined, "
|
2020-03-17 14:35:43 +01:00
|
|
|
"make sure you have defined the database.")}},
|
|
|
|
{use_cache,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'use_cache' option, but applied to this module only.")}},
|
|
|
|
{cache_size,
|
|
|
|
#{value => "pos_integer() | infinity",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'cache_size' option, but applied to this module only.")}},
|
|
|
|
{cache_missed,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'cache_missed' option, but applied to this module only.")}},
|
|
|
|
{cache_life_time,
|
|
|
|
#{value => "timeout()",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'cache_life_time' option, but applied to this module only.")}}],
|
2020-01-08 10:24:51 +01:00
|
|
|
example =>
|
|
|
|
[{?T("Take the case of a computer club that wants all its members "
|
|
|
|
"seeing each other in their rosters. To achieve this, they "
|
|
|
|
"need to create a shared roster group similar to this one:"),
|
|
|
|
["Identification: club_members",
|
|
|
|
"Name: Club Members",
|
|
|
|
"Description: Members from the computer club",
|
|
|
|
"Members: member1@example.org, member2@example.org, member3@example.org",
|
|
|
|
"Displayed Groups: club_members"]},
|
|
|
|
{?T("In another case we have a company which has three divisions: "
|
|
|
|
"Management, Marketing and Sales. All group members should see "
|
|
|
|
"all other members in their rosters. Additionally, all managers "
|
|
|
|
"should have all marketing and sales people in their roster. "
|
|
|
|
"Simultaneously, all marketeers and the whole sales team "
|
|
|
|
"should see all managers. This scenario can be achieved by "
|
|
|
|
"creating shared roster groups as shown in the following lists:"),
|
|
|
|
["First list:",
|
|
|
|
"Identification: management",
|
|
|
|
"Name: Management",
|
|
|
|
"Description: Management",
|
|
|
|
"Members: manager1@example.org, manager2@example.org",
|
|
|
|
"Displayed Groups: management, marketing, sales",
|
|
|
|
"",
|
|
|
|
"Second list:",
|
|
|
|
"Identification: marketing",
|
|
|
|
"Name: Marketing",
|
|
|
|
"Description: Marketing",
|
|
|
|
"Members: marketeer1@example.org, marketeer2@example.org, marketeer3@example.org",
|
|
|
|
"Displayed Groups: management, marketing",
|
|
|
|
"",
|
|
|
|
"Third list:",
|
|
|
|
"Identification: sales",
|
|
|
|
"Name: Sales",
|
|
|
|
"Description: Sales",
|
|
|
|
"Members: salesman1@example.org, salesman2@example.org, salesman3@example.org",
|
|
|
|
"Displayed Groups: management, sales"
|
|
|
|
]}
|
|
|
|
]}.
|