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>
|
|
|
|
%%%
|
|
|
|
%%%
|
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
|
|
|
|
%%%
|
2005-04-17 20:08:34 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_shared_roster).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2, stop/1,
|
2007-12-26 15:53:37 +01:00
|
|
|
webadmin_menu/3, webadmin_page/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
get_user_roster/2,
|
|
|
|
get_subscription_lists/3,
|
|
|
|
get_jid_info/4,
|
2006-07-06 16:08:09 +02:00
|
|
|
process_item/2,
|
2006-05-26 02:00:32 +02:00
|
|
|
in_subscription/6,
|
2005-04-17 20:08:34 +02:00
|
|
|
out_subscription/4,
|
2009-01-19 11:14:04 +01:00
|
|
|
register_user/2,
|
|
|
|
remove_user/2,
|
2005-04-17 20:08:34 +02:00
|
|
|
list_groups/1,
|
|
|
|
create_group/2,
|
|
|
|
create_group/3,
|
|
|
|
delete_group/2,
|
|
|
|
get_group_opts/2,
|
|
|
|
set_group_opts/3,
|
|
|
|
get_group_users/2,
|
2005-09-29 03:04:24 +02:00
|
|
|
get_group_explicit_users/2,
|
2009-01-19 11:14:04 +01:00
|
|
|
is_user_in_group/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
add_user_to_group/3,
|
|
|
|
remove_user_from_group/3]).
|
|
|
|
|
2008-09-29 13:29:09 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("mod_roster.hrl").
|
2008-07-17 17:33:50 +02:00
|
|
|
-include("web/ejabberd_http.hrl").
|
|
|
|
-include("web/ejabberd_web_admin.hrl").
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
-record(sr_group, {group_host, opts}).
|
|
|
|
-record(sr_user, {us, group_host}).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, _Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:create_table(sr_group,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, sr_group)}]),
|
|
|
|
mnesia:create_table(sr_user,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{attributes, record_info(fields, sr_user)}]),
|
|
|
|
mnesia:add_table_index(sr_user, group_host),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(webadmin_menu_host, HostB,
|
2007-08-23 02:51:54 +02:00
|
|
|
?MODULE, webadmin_menu, 70),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(webadmin_page_host, HostB,
|
2007-08-23 02:51:54 +02:00
|
|
|
?MODULE, webadmin_page, 50),
|
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, 70),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_in_subscription, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, in_subscription, 30),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_out_subscription, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, out_subscription, 30),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_get_subscription_lists, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, get_subscription_lists, 70),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_get_jid_info, HostB,
|
2006-07-06 16:08:09 +02:00
|
|
|
?MODULE, get_jid_info, 70),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(roster_process_item, HostB,
|
2008-04-26 19:37:43 +02:00
|
|
|
?MODULE, process_item, 50),
|
2009-01-19 11:14:04 +01:00
|
|
|
ejabberd_hooks:add(register_user, HostB,
|
|
|
|
?MODULE, register_user, 50),
|
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
|
|
|
?MODULE, remove_user, 50).
|
2009-01-03 16:15:38 +01:00
|
|
|
%%ejabberd_hooks:add(remove_user, HostB,
|
2007-12-07 01:09:48 +01:00
|
|
|
%% ?MODULE, remove_user, 50),
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(webadmin_menu_host, HostB,
|
2007-08-23 02:51:54 +02:00
|
|
|
?MODULE, webadmin_menu, 70),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(webadmin_page_host, HostB,
|
2007-08-23 02:51:54 +02:00
|
|
|
?MODULE, webadmin_page, 50),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_get, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, get_user_roster, 70),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_in_subscription, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, in_subscription, 30),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_out_subscription, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, out_subscription, 30),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_get_subscription_lists, HostB,
|
2005-04-17 20:08:34 +02:00
|
|
|
?MODULE, get_subscription_lists, 70),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_get_jid_info, HostB,
|
2006-07-06 16:08:09 +02:00
|
|
|
?MODULE, get_jid_info, 70),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:delete(roster_process_item, HostB,
|
2008-04-26 19:37:43 +02:00
|
|
|
?MODULE, process_item, 50),
|
2009-01-19 11:14:04 +01:00
|
|
|
ejabberd_hooks:delete(register_user, HostB,
|
|
|
|
?MODULE, register_user, 50),
|
|
|
|
ejabberd_hooks:delete(remove_user, HostB,
|
|
|
|
?MODULE, remove_user, 50).
|
2009-01-03 16:15:38 +01:00
|
|
|
%%ejabberd_hooks:delete(remove_user, HostB,
|
2007-12-07 01:09:48 +01:00
|
|
|
%% ?MODULE, remove_user, 50),
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
|
2010-02-08 21:14:40 +01:00
|
|
|
get_user_roster(Items, {U, S}) when is_binary(U) ->
|
|
|
|
get_user_roster(Items, {binary_to_list(U), binary_to_list(S)});
|
2005-04-17 20:08:34 +02:00
|
|
|
get_user_roster(Items, US) ->
|
|
|
|
{U, S} = US,
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
2006-07-05 11:26:52 +02:00
|
|
|
%% Get shared roster users in all groups and remove self:
|
2005-04-17 20:08:34 +02:00
|
|
|
SRUsers =
|
|
|
|
lists:foldl(
|
|
|
|
fun(Group, Acc1) ->
|
2009-11-26 15:45:39 +01:00
|
|
|
GroupName = get_group_name(S, Group),
|
2005-04-17 20:08:34 +02:00
|
|
|
lists:foldl(
|
|
|
|
fun(User, Acc2) ->
|
2006-07-05 11:26:52 +02:00
|
|
|
if User == US -> Acc2;
|
|
|
|
true -> dict:append(User,
|
2009-11-26 15:45:39 +01:00
|
|
|
GroupName,
|
2006-07-05 11:26:52 +02:00
|
|
|
Acc2)
|
|
|
|
end
|
2005-04-17 20:08:34 +02:00
|
|
|
end, Acc1, get_group_users(S, Group))
|
|
|
|
end, dict:new(), DisplayedGroups),
|
2006-07-05 11:26:52 +02:00
|
|
|
|
|
|
|
%% If partially subscribed users are also in shared roster, show them as
|
|
|
|
%% totally subscribed:
|
2005-04-17 20:08:34 +02:00
|
|
|
{NewItems1, SRUsersRest} =
|
|
|
|
lists:mapfoldl(
|
|
|
|
fun(Item, SRUsers1) ->
|
|
|
|
{_, _, {U1, S1, _}} = Item#roster.usj,
|
|
|
|
US1 = {U1, S1},
|
|
|
|
case dict:find(US1, SRUsers1) of
|
|
|
|
{ok, _GroupNames} ->
|
|
|
|
{Item#roster{subscription = both, ask = none},
|
|
|
|
dict:erase(US1, SRUsers1)};
|
|
|
|
error ->
|
|
|
|
{Item, SRUsers1}
|
|
|
|
end
|
|
|
|
end, SRUsers, Items),
|
2007-12-07 01:09:48 +01:00
|
|
|
|
2006-07-05 11:26:52 +02:00
|
|
|
%% Export items in roster format:
|
2010-02-08 21:31:41 +01:00
|
|
|
ModVcard = get_vcard_module(S),
|
2008-09-29 13:29:09 +02:00
|
|
|
SRItems = [#roster{usj = {U, S, {U1, S1, undefined}},
|
2005-04-17 20:08:34 +02:00
|
|
|
us = US,
|
2008-09-29 13:29:09 +02:00
|
|
|
jid = {U1, S1, undefined},
|
2010-02-08 21:31:41 +01:00
|
|
|
name = get_rosteritem_name(ModVcard, U1, S1),
|
2005-04-17 20:08:34 +02:00
|
|
|
subscription = both,
|
|
|
|
ask = none,
|
|
|
|
groups = GroupNames} ||
|
|
|
|
{{U1, S1}, GroupNames} <- dict:to_list(SRUsersRest)],
|
|
|
|
SRItems ++ NewItems1.
|
|
|
|
|
2010-02-08 21:31:41 +01:00
|
|
|
get_vcard_module(Server) ->
|
|
|
|
Modules = gen_mod:loaded_modules(Server),
|
|
|
|
[M || M <- Modules,
|
|
|
|
(M == mod_vcard) or (M == mod_vcard_odbc) or (M == mod_vcard_ldap)].
|
|
|
|
|
|
|
|
get_rosteritem_name([], _, _) ->
|
|
|
|
<<>>;
|
|
|
|
get_rosteritem_name([ModVcard], U, S) ->
|
|
|
|
From = exmpp_jid:make("", S, "mod_shared_roster"),
|
|
|
|
To = exmpp_jid:make(U, S, ""),
|
|
|
|
Payload = #xmlel{ns = ?NS_VCARD, name = 'vCard'},
|
|
|
|
IQ = #iq{kind = request,
|
|
|
|
type = get,
|
|
|
|
ns = ?NS_VCARD,
|
|
|
|
payload = Payload},
|
|
|
|
IQ_Vcard = ModVcard:process_sm_iq(From, To, IQ),
|
|
|
|
try get_rosteritem_name_vcard(exmpp_iq:get_payload(IQ_Vcard))
|
|
|
|
catch E1:E2 ->
|
|
|
|
?ERROR_MSG("Error ~p found when trying to get the vCard of ~s@~s "
|
|
|
|
"in ~p:~n ~p", [E1, U, S, ModVcard, E2]),
|
|
|
|
<<>>
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_rosteritem_name_vcard(undefined) ->
|
|
|
|
<<>>;
|
|
|
|
get_rosteritem_name_vcard(Vcard) ->
|
|
|
|
case exmpp_xml:get_path(Vcard, [{element, "NICKNAME"}, cdata]) of
|
|
|
|
<<>> -> exmpp_xml:get_path(Vcard, [{element, "FN"}, cdata]);
|
|
|
|
Nickname -> Nickname
|
|
|
|
end.
|
|
|
|
|
2008-07-11 14:48:27 +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.
|
|
|
|
process_item(RosterItem, Host) ->
|
2008-07-11 14:48:27 +02:00
|
|
|
USFrom = {UserFrom, ServerFrom} = RosterItem#roster.us,
|
|
|
|
{UserTo, ServerTo, ResourceTo} = RosterItem#roster.jid,
|
2008-10-08 14:02:30 +02:00
|
|
|
NameTo = RosterItem#roster.name,
|
2008-07-11 14:48:27 +02:00
|
|
|
USTo = {UserTo, ServerTo},
|
2006-07-06 16:08:09 +02:00
|
|
|
DisplayedGroups = get_user_displayed_groups(USFrom),
|
|
|
|
CommonGroups = lists:filter(fun(Group) ->
|
|
|
|
is_user_in_group(USTo, Group, Host)
|
|
|
|
end, DisplayedGroups),
|
|
|
|
case CommonGroups of
|
|
|
|
[] -> 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]};
|
2008-07-11 14:48:27 +02:00
|
|
|
%% Both users have at least a common shared group,
|
|
|
|
%% So each user can see the other
|
|
|
|
_ ->
|
|
|
|
%% Check if the list of groups of the new roster item
|
|
|
|
%% include at least a new one
|
|
|
|
case lists:subtract(RosterItem#roster.groups, CommonGroups) of
|
2009-03-03 19:53:28 +01:00
|
|
|
%% If it doesn't, then remove this user from any
|
|
|
|
%% existing roster groups.
|
2008-07-11 14:48:27 +02:00
|
|
|
[] ->
|
2009-03-03 19:53:28 +01:00
|
|
|
%% Remove pending subscription by setting it
|
|
|
|
%% unsubscribed.
|
|
|
|
Mod = get_roster_mod(ServerFrom),
|
|
|
|
|
|
|
|
%% Remove pending out subscription
|
|
|
|
Mod:out_subscription(UserTo, ServerTo,
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(UserFrom, ServerFrom),
|
2009-03-03 19:53:28 +01:00
|
|
|
unsubscribe),
|
|
|
|
|
|
|
|
%% Remove pending in subscription
|
|
|
|
Mod:in_subscription(aaaa, UserFrom, ServerFrom,
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(UserTo, ServerTo),
|
2009-03-03 19:53:28 +01:00
|
|
|
unsubscribe, ""),
|
|
|
|
|
|
|
|
%% But we're still subscribed, so respond as such.
|
2008-07-11 14:48:27 +02:00
|
|
|
RosterItem#roster{subscription = both, ask = none};
|
|
|
|
%% If so, it means the user wants to add that contact
|
|
|
|
%% to his personal roster
|
|
|
|
PersonalGroups ->
|
|
|
|
%% Store roster items in From and To rosters
|
|
|
|
set_new_rosteritems(UserFrom, ServerFrom,
|
2008-10-08 14:02:30 +02:00
|
|
|
UserTo, ServerTo, ResourceTo, NameTo,
|
2008-07-11 14:48:27 +02:00
|
|
|
PersonalGroups)
|
|
|
|
end
|
2006-07-06 16:08:09 +02:00
|
|
|
end.
|
|
|
|
|
2008-10-08 14:02:30 +02:00
|
|
|
build_roster_record(User1, Server1, User2, Server2, Name2, Groups) ->
|
2008-09-29 13:29:09 +02:00
|
|
|
USR2 = {User2, Server2, undefined},
|
2008-07-11 14:48:27 +02:00
|
|
|
#roster{usj = {User1, Server1, USR2},
|
|
|
|
us = {User1, Server1},
|
|
|
|
jid = USR2,
|
2008-10-08 14:02:30 +02:00
|
|
|
name = Name2,
|
2008-07-11 14:48:27 +02:00
|
|
|
subscription = both,
|
|
|
|
ask = none,
|
|
|
|
groups = Groups
|
|
|
|
}.
|
|
|
|
|
|
|
|
set_new_rosteritems(UserFrom, ServerFrom,
|
2008-10-08 14:02:30 +02:00
|
|
|
UserTo, ServerTo, ResourceTo, NameTo, GroupsFrom) ->
|
2009-03-03 19:53:28 +01:00
|
|
|
Mod = get_roster_mod(ServerFrom),
|
2008-07-11 14:48:27 +02:00
|
|
|
|
|
|
|
RIFrom = build_roster_record(UserFrom, ServerFrom,
|
2008-10-08 14:02:30 +02:00
|
|
|
UserTo, ServerTo, NameTo, GroupsFrom),
|
2008-07-11 14:48:27 +02:00
|
|
|
set_item(UserFrom, ServerFrom, ResourceTo, RIFrom),
|
2009-06-01 18:26:00 +02:00
|
|
|
JIDTo = exmpp_jid:make(UserTo, ServerTo),
|
2008-07-11 14:48:27 +02:00
|
|
|
|
2009-06-01 18:26:00 +02:00
|
|
|
JIDFrom = exmpp_jid:make(UserFrom, ServerFrom),
|
2008-07-11 14:48:27 +02:00
|
|
|
RITo = build_roster_record(UserTo, ServerTo,
|
2008-10-08 14:02:30 +02:00
|
|
|
UserFrom, ServerFrom, UserFrom,[]),
|
2008-09-29 13:29:09 +02:00
|
|
|
set_item(UserTo, ServerTo, undefined, RITo),
|
2008-07-11 14:48:27 +02:00
|
|
|
|
|
|
|
%% From requests
|
|
|
|
Mod:out_subscription(UserFrom, ServerFrom, JIDTo, subscribe),
|
|
|
|
Mod:in_subscription(aaa, UserTo, ServerTo, JIDFrom, subscribe, ""),
|
|
|
|
|
|
|
|
%% To accepts
|
|
|
|
Mod:out_subscription(UserTo, ServerTo, JIDFrom, subscribed),
|
|
|
|
Mod:in_subscription(aaa, UserFrom, ServerFrom, JIDTo, subscribed, ""),
|
|
|
|
|
|
|
|
%% To requests
|
|
|
|
Mod:out_subscription(UserTo, ServerTo, JIDFrom, subscribe),
|
|
|
|
Mod:in_subscription(aaa, UserFrom, ServerFrom, JIDTo, subscribe, ""),
|
|
|
|
|
|
|
|
%% From accepts
|
|
|
|
Mod:out_subscription(UserFrom, ServerFrom, JIDTo, subscribed),
|
|
|
|
Mod:in_subscription(aaa, UserTo, ServerTo, JIDFrom, subscribed, ""),
|
|
|
|
|
|
|
|
RIFrom.
|
|
|
|
|
|
|
|
set_item(User, Server, Resource, Item) ->
|
2008-09-29 13:29:09 +02:00
|
|
|
Request = #xmlel{ns = ?NS_ROSTER, name = 'query',
|
|
|
|
children = [mod_roster:item_to_xml(Item)]},
|
2008-12-01 16:53:30 +01:00
|
|
|
ResIQ = exmpp_iq:set(?NS_JABBER_CLIENT, Request,
|
|
|
|
"push" ++ randoms:get_string()),
|
2008-07-11 14:48:27 +02:00
|
|
|
ejabberd_router:route(
|
2009-06-01 18:26:00 +02:00
|
|
|
exmpp_jid:make(User, Server, Resource),
|
|
|
|
exmpp_jid:make(Server),
|
2008-09-29 13:29:09 +02:00
|
|
|
ResIQ).
|
2008-07-11 14:48:27 +02:00
|
|
|
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_subscription_lists({F, T}, User, Server)
|
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-09-29 13:29:09 +02:00
|
|
|
try
|
2009-01-03 16:15:38 +01:00
|
|
|
LUser = binary_to_list(User),
|
|
|
|
LServer = binary_to_list(Server),
|
2008-09-29 13:29:09 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
|
|
|
SRUsers =
|
|
|
|
lists:usort(
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Group) ->
|
|
|
|
get_group_users(LServer, Group)
|
|
|
|
end, DisplayedGroups)),
|
|
|
|
SRJIDs = [{U1, S1, undefined} || {U1, S1} <- SRUsers],
|
|
|
|
{lists:usort(SRJIDs ++ F), lists:usort(SRJIDs ++ T)}
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
{[], []}
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_jid_info({Subscription, Groups}, User, Server, JID)
|
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-09-29 13:29:09 +02:00
|
|
|
try
|
2009-01-03 16:15:38 +01:00
|
|
|
LUser = binary_to_list(User),
|
|
|
|
LServer = binary_to_list(Server),
|
2008-09-29 13:29:09 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
{U1, S1, _} = jlib:short_prepd_jid(JID),
|
|
|
|
US1 = {U1, S1},
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
|
|
|
SRUsers =
|
|
|
|
lists:foldl(
|
|
|
|
fun(Group, Acc1) ->
|
|
|
|
lists:foldl(
|
|
|
|
fun(User1, Acc2) ->
|
|
|
|
dict:append(
|
|
|
|
User1, get_group_name(LServer, Group), Acc2)
|
|
|
|
end, Acc1, get_group_users(LServer, Group))
|
|
|
|
end, dict:new(), DisplayedGroups),
|
|
|
|
case dict:find(US1, SRUsers) of
|
|
|
|
{ok, GroupNames} ->
|
|
|
|
NewGroups = if
|
|
|
|
Groups == [] -> GroupNames;
|
|
|
|
true -> Groups
|
|
|
|
end,
|
|
|
|
{both, NewGroups};
|
|
|
|
error ->
|
|
|
|
{Subscription, Groups}
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
{[], []}
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2006-05-26 02:00:32 +02:00
|
|
|
in_subscription(Acc, User, Server, JID, Type, _Reason) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
process_subscription(in, User, Server, JID, Type, Acc).
|
|
|
|
|
2009-03-03 19:53:28 +01:00
|
|
|
out_subscription(UserFrom, ServerFrom, JIDTo, unsubscribed) ->
|
|
|
|
Mod = get_roster_mod(ServerFrom),
|
|
|
|
|
|
|
|
%% Remove pending out subscription
|
|
|
|
{UserTo, ServerTo, _} = jlib:short_prepd_bare_jid(JIDTo),
|
2009-06-01 18:26:00 +02:00
|
|
|
JIDFrom = exmpp_jid:make(UserFrom, UserTo),
|
2009-03-03 19:53:28 +01:00
|
|
|
Mod:out_subscription(UserTo, ServerTo, JIDFrom, unsubscribe),
|
|
|
|
|
|
|
|
%% Remove pending in subscription
|
|
|
|
Mod:in_subscription(aaaa, UserFrom, ServerFrom, JIDTo, unsubscribe, ""),
|
|
|
|
|
|
|
|
process_subscription(out, UserFrom, ServerFrom, JIDTo, unsubscribed, false);
|
2005-04-17 20:08:34 +02:00
|
|
|
out_subscription(User, Server, JID, Type) ->
|
|
|
|
process_subscription(out, User, Server, JID, Type, false).
|
|
|
|
|
|
|
|
process_subscription(Direction, User, Server, JID, _Type, Acc) ->
|
2008-09-29 13:29:09 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
US = {LUser, LServer},
|
|
|
|
{U1, S1, _} = jlib:short_prepd_bare_jid(JID),
|
|
|
|
US1 = {U1, S1},
|
|
|
|
DisplayedGroups = get_user_displayed_groups(US),
|
|
|
|
SRUsers =
|
|
|
|
lists:usort(
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Group) ->
|
|
|
|
get_group_users(LServer, Group)
|
|
|
|
end, DisplayedGroups)),
|
|
|
|
case lists:member(US1, SRUsers) of
|
|
|
|
true ->
|
|
|
|
case Direction of
|
|
|
|
in ->
|
|
|
|
{stop, false};
|
|
|
|
out ->
|
|
|
|
stop
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end
|
|
|
|
catch
|
|
|
|
_ ->
|
2005-04-17 20:08:34 +02:00
|
|
|
Acc
|
|
|
|
end.
|
|
|
|
|
|
|
|
list_groups(Host) ->
|
|
|
|
mnesia:dirty_select(
|
|
|
|
sr_group,
|
|
|
|
[{#sr_group{group_host = {'$1', '$2'},
|
|
|
|
_ = '_'},
|
|
|
|
[{'==', '$2', Host}],
|
|
|
|
['$1']}]).
|
|
|
|
|
2008-04-26 19:37:43 +02:00
|
|
|
groups_with_opts(Host) ->
|
|
|
|
Gs = mnesia:dirty_select(
|
|
|
|
sr_group,
|
|
|
|
[{#sr_group{group_host={'$1', Host}, opts='$2', _='_'},
|
|
|
|
[],
|
|
|
|
[['$1','$2']] }]),
|
|
|
|
lists:map(fun([G,O]) -> {G, O} end, Gs).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
create_group(Host, Group) ->
|
|
|
|
create_group(Host, Group, []).
|
|
|
|
|
|
|
|
create_group(Host, Group, Opts) ->
|
|
|
|
R = #sr_group{group_host = {Group, Host}, opts = Opts},
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(R)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
delete_group(Host, Group) ->
|
2006-07-05 10:52:21 +02:00
|
|
|
GroupHost = {Group, Host},
|
2005-04-17 20:08:34 +02:00
|
|
|
F = fun() ->
|
2006-07-05 10:52:21 +02:00
|
|
|
%% Delete the group ...
|
|
|
|
mnesia:delete({sr_group, GroupHost}),
|
|
|
|
%% ... and its users
|
|
|
|
Users = mnesia:index_read(sr_user, GroupHost, #sr_user.group_host),
|
|
|
|
lists:foreach(fun(UserEntry) ->
|
|
|
|
mnesia:delete_object(UserEntry)
|
|
|
|
end, Users)
|
2005-04-17 20:08:34 +02:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
get_group_opts(Host, Group) ->
|
|
|
|
case catch mnesia:dirty_read(sr_group, {Group, Host}) of
|
|
|
|
[#sr_group{opts = Opts}] ->
|
|
|
|
Opts;
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
set_group_opts(Host, Group, Opts) ->
|
|
|
|
R = #sr_group{group_host = {Group, Host}, opts = Opts},
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(R)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
get_user_groups(US) ->
|
|
|
|
Host = element(2, US),
|
|
|
|
case catch mnesia:dirty_read(sr_user, US) of
|
|
|
|
Rs when is_list(Rs) ->
|
|
|
|
[Group || #sr_user{group_host = {Group, H}} <- Rs, H == Host];
|
|
|
|
_ ->
|
|
|
|
[]
|
2008-04-26 19:37:43 +02:00
|
|
|
end ++ get_special_users_groups(Host).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
is_group_enabled(Host, Group) ->
|
|
|
|
case catch mnesia:dirty_read(sr_group, {Group, Host}) of
|
|
|
|
[#sr_group{opts = Opts}] ->
|
|
|
|
not lists:member(disabled, Opts);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
2009-12-21 15:31:10 +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) ->
|
|
|
|
case catch mnesia:dirty_read(sr_group, {Group, Host}) of
|
|
|
|
[#sr_group{opts = Opts}] ->
|
|
|
|
case lists:keysearch(Opt, 1, Opts) of
|
|
|
|
{value, {_, Val}} ->
|
|
|
|
Val;
|
|
|
|
false ->
|
|
|
|
Default
|
|
|
|
end;
|
|
|
|
_ ->
|
2009-12-19 01:19:26 +01:00
|
|
|
Default
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_group_users(Host, Group) ->
|
2005-09-29 03:04:24 +02:00
|
|
|
case get_group_opt(Host, Group, all_users, false) of
|
|
|
|
true ->
|
|
|
|
ejabberd_auth:get_vh_registered_users(Host);
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end ++ get_group_explicit_users(Host, Group).
|
|
|
|
|
2008-04-26 19:37:43 +02:00
|
|
|
get_group_users(_User, Host, Group, GroupOpts) ->
|
|
|
|
case proplists:get_value(all_users, GroupOpts, false) of
|
|
|
|
true ->
|
|
|
|
ejabberd_auth:get_vh_registered_users(Host);
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end ++ get_group_explicit_users(Host, Group).
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% @spec (Host::string(), Group::string()) -> [{User::string(), Server::string()}]
|
2005-09-29 03:04:24 +02:00
|
|
|
get_group_explicit_users(Host, Group) ->
|
2008-04-26 19:37:43 +02:00
|
|
|
Read = (catch mnesia:dirty_index_read(
|
|
|
|
sr_user,
|
|
|
|
{Group, Host},
|
|
|
|
#sr_user.group_host)),
|
|
|
|
case Read of
|
|
|
|
Rs when is_list(Rs) ->
|
|
|
|
[R#sr_user.us || R <- Rs];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
get_group_name(Host, Group) ->
|
|
|
|
get_group_opt(Host, Group, name, Group).
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% Get list of names of groups that have @all@ in the memberlist
|
2008-04-26 19:37:43 +02:00
|
|
|
get_special_users_groups(Host) ->
|
2005-09-29 03:04:24 +02:00
|
|
|
lists:filter(
|
2008-04-26 19:37:43 +02:00
|
|
|
fun(Group) ->
|
|
|
|
get_group_opt(Host, Group, all_users, false)
|
|
|
|
end,
|
2005-09-29 03:04:24 +02:00
|
|
|
list_groups(Host)).
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
%% Given two lists of groupnames and their options,
|
|
|
|
%% return the list of displayed groups to the second list
|
2008-04-26 19:37:43 +02: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, []))].
|
|
|
|
|
2009-01-19 11:14:04 +01: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) ->
|
|
|
|
Groups = lists:filter(
|
|
|
|
fun({_Group, Opts}) ->
|
|
|
|
proplists:get_value(all_users, Opts, false)
|
|
|
|
end, GroupsOpts),
|
|
|
|
displayed_groups(GroupsOpts, Groups).
|
|
|
|
|
2009-01-19 11:14:04 +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) ->
|
|
|
|
Groups = case catch mnesia:dirty_read(sr_user, {LUser, LServer}) of
|
|
|
|
Rs when is_list(Rs) ->
|
|
|
|
[{Group, proplists:get_value(Group, GroupsOpts, [])} ||
|
|
|
|
#sr_user{group_host = {Group, H}} <- Rs, H == LServer];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
displayed_groups(GroupsOpts, Groups).
|
|
|
|
|
2009-01-19 11:14:04 +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),
|
|
|
|
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)].
|
|
|
|
|
2008-07-11 14:48:27 +02:00
|
|
|
is_user_in_group({_U, S} = US, Group, Host) ->
|
2006-09-05 06:26:28 +02:00
|
|
|
case catch mnesia:dirty_match_object(
|
|
|
|
#sr_user{us=US, group_host={Group, Host}}) of
|
2008-07-11 14:48:27 +02:00
|
|
|
[] -> lists:member(US, get_group_users(S, Group));
|
2006-07-06 16:08:09 +02:00
|
|
|
_ -> true
|
|
|
|
end.
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
|
|
|
|
%% @spec (Host::string(), {User::string(), Server::string()}, Group::string()) -> {atomic, ok}
|
2005-04-17 20:08:34 +02:00
|
|
|
add_user_to_group(Host, US, Group) ->
|
2009-01-19 11:14:04 +01:00
|
|
|
{LUser, LServer} = US,
|
2009-09-02 16:26:01 +02:00
|
|
|
case re:run(LUser, "^@.+@$", [{capture, none}]) of
|
|
|
|
match ->
|
2009-04-22 12:40:11 +02:00
|
|
|
GroupOpts = mod_shared_roster:get_group_opts(Host, Group),
|
|
|
|
AllUsersOpt =
|
|
|
|
case LUser == "@all@" of
|
|
|
|
true -> [{all_users, true}];
|
|
|
|
false -> []
|
|
|
|
end,
|
|
|
|
mod_shared_roster:set_group_opts(
|
|
|
|
Host, Group,
|
|
|
|
GroupOpts ++ AllUsersOpt);
|
|
|
|
nomatch ->
|
|
|
|
%% Push this new user to members of groups where this group is displayed
|
|
|
|
push_user_to_displayed(LUser, LServer, Group, both),
|
|
|
|
%% Push members of groups that are displayed to this group
|
|
|
|
push_displayed_to_user(LUser, LServer, Group, Host, both),
|
|
|
|
R = #sr_user{us = US, group_host = {Group, Host}},
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:write(R)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
push_displayed_to_user(LUser, LServer, Group, Host, Subscription) ->
|
|
|
|
GroupsOpts = groups_with_opts(LServer),
|
|
|
|
GroupOpts = proplists:get_value(Group, GroupsOpts, []),
|
|
|
|
DisplayedGroups = proplists:get_value(displayed_groups, GroupOpts, []),
|
|
|
|
[push_members_to_user(LUser, LServer, DGroup, Host, Subscription) || DGroup <- DisplayedGroups].
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user_from_group(Host, US, Group) ->
|
2006-07-05 10:52:21 +02:00
|
|
|
GroupHost = {Group, Host},
|
2009-01-19 11:14:04 +01:00
|
|
|
{LUser, LServer} = US,
|
2009-09-02 16:26:01 +02:00
|
|
|
case re:run(LUser, "^@.+@$", [{capture, none}]) of
|
|
|
|
match ->
|
2009-04-22 12:40:11 +02:00
|
|
|
GroupOpts = mod_shared_roster:get_group_opts(Host, Group),
|
|
|
|
NewGroupOpts =
|
|
|
|
case LUser of
|
|
|
|
"@all@" ->
|
|
|
|
lists:filter(fun(X) -> X/={all_users,true} end, GroupOpts)
|
|
|
|
end,
|
|
|
|
mod_shared_roster:set_group_opts(Host, Group, NewGroupOpts);
|
|
|
|
nomatch ->
|
|
|
|
R = #sr_user{us = US, group_host = GroupHost},
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete_object(R)
|
|
|
|
end,
|
|
|
|
Result = mnesia:transaction(F),
|
|
|
|
%% Push removal of the old user to members of groups where the group that this user was members was displayed
|
|
|
|
push_user_to_displayed(LUser, LServer, Group, remove),
|
|
|
|
%% Push removal of members of groups that where displayed to the group which this user has left
|
|
|
|
push_displayed_to_user(LUser, LServer, Group, Host, remove),
|
|
|
|
Result
|
|
|
|
end.
|
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
|
|
|
|
push_members_to_user(LUser, LServer, Group, Host, Subscription) ->
|
|
|
|
GroupsOpts = groups_with_opts(LServer),
|
|
|
|
GroupOpts = proplists:get_value(Group, GroupsOpts, []),
|
|
|
|
GroupName = proplists:get_value(name, GroupOpts, Group),
|
|
|
|
Members = get_group_users(Host, Group),
|
|
|
|
lists:foreach(
|
|
|
|
fun({U, S}) ->
|
|
|
|
push_roster_item(LUser, LServer, U, S, GroupName, Subscription)
|
|
|
|
end, Members).
|
|
|
|
|
|
|
|
register_user(User, Server) ->
|
|
|
|
%% Get list of groups where this user is member
|
|
|
|
Groups = get_user_groups({User, Server}),
|
|
|
|
%% Push this user to members of groups where is displayed a group which this user is member
|
|
|
|
[push_user_to_displayed(User, Server, Group, both) || Group <- Groups].
|
|
|
|
|
|
|
|
remove_user(User, Server) ->
|
|
|
|
push_user_to_members(User, Server, remove).
|
2007-08-23 02:51:54 +02:00
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
push_user_to_members(User, Server, Subscription) ->
|
2008-09-29 13:29:09 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
GroupsOpts = groups_with_opts(LServer),
|
|
|
|
SpecialGroups = get_special_displayed_groups(GroupsOpts),
|
|
|
|
UserGroups = get_user_displayed_groups(LUser, LServer, GroupsOpts),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Group) ->
|
|
|
|
GroupOpts = proplists:get_value(Group, GroupsOpts, []),
|
|
|
|
GroupName = proplists:get_value(name, GroupOpts, Group),
|
|
|
|
lists:foreach(
|
|
|
|
fun({U, S}) ->
|
2009-01-19 11:14:04 +01:00
|
|
|
push_roster_item(U, S, LUser, LServer, GroupName, Subscription)
|
2008-09-29 13:29:09 +02:00
|
|
|
end, get_group_users(LUser, LServer, Group, GroupOpts))
|
|
|
|
end, lists:usort(SpecialGroups++UserGroups))
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2008-04-26 19:37:43 +02:00
|
|
|
|
2009-01-19 11:14:04 +01:00
|
|
|
push_user_to_displayed(LUser, LServer, Group, Subscription) ->
|
|
|
|
GroupsOpts = groups_with_opts(LServer),
|
|
|
|
GroupOpts = proplists:get_value(Group, GroupsOpts, []),
|
|
|
|
GroupName = proplists:get_value(name, GroupOpts, Group),
|
|
|
|
DisplayedToGroupsOpts = displayed_to_groups(Group, LServer),
|
|
|
|
[push_user_to_group(LUser, LServer, GroupD, GroupName, Subscription) || {GroupD, _Opts} <- DisplayedToGroupsOpts].
|
|
|
|
|
|
|
|
push_user_to_group(LUser, LServer, Group, GroupName, Subscription) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({U, S}) ->
|
|
|
|
push_roster_item(U, S, LUser, LServer, GroupName, Subscription)
|
|
|
|
end, get_group_users(LServer, Group)).
|
|
|
|
|
|
|
|
%% Get list of groups to which this group is displayed
|
|
|
|
displayed_to_groups(GroupName, LServer) ->
|
|
|
|
GroupsOpts = groups_with_opts(LServer),
|
|
|
|
lists:filter(
|
|
|
|
fun({_Group, Opts}) ->
|
|
|
|
lists:member(GroupName, proplists:get_value(displayed_groups, Opts, []))
|
|
|
|
end, GroupsOpts).
|
|
|
|
|
2008-04-26 19:37:43 +02:00
|
|
|
push_item(_User, _Server, _From, none) ->
|
|
|
|
ok;
|
|
|
|
push_item(User, Server, From, Item) ->
|
|
|
|
%% It was
|
|
|
|
%% ejabberd_sm:route(jlib:make_jid("", "", ""),
|
|
|
|
%% jlib:make_jid(User, Server, "")
|
|
|
|
%% why?
|
2009-06-01 18:26:00 +02:00
|
|
|
ejabberd_sm:route(From, exmpp_jid:make(User, Server),
|
2008-09-29 13:29:09 +02:00
|
|
|
#xmlel{name = 'broadcast', children =
|
|
|
|
[{item,
|
|
|
|
Item#roster.jid,
|
2008-04-26 19:37:43 +02:00
|
|
|
Item#roster.subscription}]}),
|
2008-09-29 13:29:09 +02:00
|
|
|
Request = #xmlel{ns = ?NS_ROSTER, name = 'query',
|
|
|
|
children = [item_to_xml(Item)]},
|
2008-12-01 16:53:30 +01:00
|
|
|
Stanza = exmpp_iq:set(?NS_JABBER_CLIENT, Request,
|
|
|
|
"push" ++ randoms:get_string()),
|
2008-04-26 19:37:43 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Resource) ->
|
2009-06-01 18:26:00 +02:00
|
|
|
JID = exmpp_jid:make(User, Server, Resource),
|
2008-04-26 19:37:43 +02:00
|
|
|
ejabberd_router:route(JID, JID, Stanza)
|
2009-01-03 16:15:38 +01:00
|
|
|
end, ejabberd_sm:get_user_resources(list_to_binary(User), list_to_binary(Server))).
|
2008-04-26 19:37:43 +02:00
|
|
|
|
2009-01-19 11:14:04 +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,
|
|
|
|
groups = [GroupName]},
|
2009-06-01 18:26:00 +02:00
|
|
|
push_item(User, Server, exmpp_jid:make(Server), Item).
|
2009-01-19 11:14:04 +01:00
|
|
|
|
2008-04-26 19:37:43 +02:00
|
|
|
item_to_xml(Item) ->
|
2008-09-29 13:29:09 +02:00
|
|
|
{U, S, R} = Item#roster.jid,
|
|
|
|
Attrs1 = exmpp_xml:set_attribute_in_list([],
|
2009-06-01 18:52:14 +02:00
|
|
|
'jid', exmpp_jid:to_list(U, S, R)),
|
2008-04-26 19:37:43 +02:00
|
|
|
Attrs2 = case Item#roster.name of
|
|
|
|
"" ->
|
|
|
|
Attrs1;
|
|
|
|
Name ->
|
2008-09-29 13:29:09 +02:00
|
|
|
exmpp_xml:set_attribute_in_list(Attrs1, 'name', Name)
|
2008-04-26 19:37:43 +02:00
|
|
|
end,
|
2008-09-29 13:29:09 +02:00
|
|
|
Attrs3 = exmpp_xml:set_attribute_in_list(Attrs2,
|
|
|
|
'subscription', Item#roster.subscription),
|
2008-04-26 19:37:43 +02:00
|
|
|
Attrs4 = case ask_to_pending(Item#roster.ask) of
|
|
|
|
out ->
|
2008-09-29 13:29:09 +02:00
|
|
|
exmpp_xml:set_attribute_in_list(Attrs3,
|
|
|
|
'ask', "subscribe");
|
2008-04-26 19:37:43 +02:00
|
|
|
both ->
|
2008-09-29 13:29:09 +02:00
|
|
|
exmpp_xml:set_attribute_in_list(Attrs3,
|
|
|
|
'ask', "subscribe");
|
2008-04-26 19:37:43 +02:00
|
|
|
_ ->
|
|
|
|
Attrs3
|
|
|
|
end,
|
|
|
|
SubEls1 = lists:map(fun(G) ->
|
2008-09-29 13:29:09 +02:00
|
|
|
exmpp_xml:set_cdata(
|
|
|
|
#xmlel{ns = ?NS_ROSTER, name = 'group'}, G)
|
2008-04-26 19:37:43 +02:00
|
|
|
end, Item#roster.groups),
|
|
|
|
SubEls = SubEls1 ++ Item#roster.xs,
|
2008-09-29 13:29:09 +02:00
|
|
|
#xmlel{ns = ?NS_ROSTER, name = 'item', attrs = Attrs4, children = SubEls}.
|
2008-04-26 19:37:43 +02:00
|
|
|
|
|
|
|
ask_to_pending(subscribe) -> out;
|
|
|
|
ask_to_pending(unsubscribe) -> none;
|
|
|
|
ask_to_pending(Ask) -> Ask.
|
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
|
|
|
|
%%---------------------
|
|
|
|
%% Web Admin
|
|
|
|
%%---------------------
|
|
|
|
|
2007-12-26 15:53:37 +01:00
|
|
|
webadmin_menu(Acc, _Host, Lang) ->
|
|
|
|
[{"shared-roster", ?T("Shared Roster Groups")} | Acc].
|
2007-08-23 02:51:54 +02:00
|
|
|
|
|
|
|
webadmin_page(_, Host,
|
2007-12-07 01:09:48 +01:00
|
|
|
#request{us = _US,
|
2007-08-23 02:51:54 +02:00
|
|
|
path = ["shared-roster"],
|
|
|
|
q = Query,
|
2007-12-07 01:09:48 +01:00
|
|
|
lang = Lang} = _Request) ->
|
2007-08-23 02:51:54 +02:00
|
|
|
Res = list_shared_roster_groups(Host, Query, Lang),
|
|
|
|
{stop, Res};
|
|
|
|
|
|
|
|
webadmin_page(_, Host,
|
2007-12-07 01:09:48 +01:00
|
|
|
#request{us = _US,
|
2007-08-23 02:51:54 +02:00
|
|
|
path = ["shared-roster", Group],
|
|
|
|
q = Query,
|
2007-12-07 01:09:48 +01:00
|
|
|
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),
|
|
|
|
SRGroups = mod_shared_roster:list_groups(Host),
|
|
|
|
FGroups =
|
|
|
|
?XAE("table", [],
|
|
|
|
[?XE("tbody",
|
|
|
|
lists:map(
|
|
|
|
fun(Group) ->
|
|
|
|
?XE("tr",
|
|
|
|
[?XE("td", [?INPUT("checkbox", "selected",
|
|
|
|
Group)]),
|
|
|
|
?XE("td", [?AC(Group ++ "/", Group)])
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end, lists:sort(SRGroups)) ++
|
|
|
|
[?XE("tr",
|
|
|
|
[?X("td"),
|
|
|
|
?XE("td", [?INPUT("text", "namenew", "")]),
|
|
|
|
?XE("td", [?INPUTT("submit", "addnew", "Add New")])
|
|
|
|
]
|
|
|
|
)]
|
|
|
|
)]),
|
2009-01-19 12:59:40 +01:00
|
|
|
?H1GL(?T("Shared Roster Groups"), "modsharedroster", "mod_shared_roster") ++
|
2007-08-23 02:51:54 +02:00
|
|
|
case Res of
|
2009-01-19 16:27:07 +01:00
|
|
|
ok -> [?XREST("Submitted")];
|
|
|
|
error -> [?XREST("Bad format")];
|
2007-08-23 02:51:54 +02:00
|
|
|
nothing -> []
|
|
|
|
end ++
|
2009-05-27 19:29:58 +02:00
|
|
|
[?XAE("form", [?XMLATTR('action', <<"">>), ?XMLATTR('method', <<"post">>)],
|
2007-08-23 02:51:54 +02:00
|
|
|
[FGroups,
|
|
|
|
?BR,
|
|
|
|
?INPUTT("submit", "delete", "Delete Selected")
|
|
|
|
])
|
|
|
|
].
|
|
|
|
|
|
|
|
list_sr_groups_parse_query(Host, Query) ->
|
|
|
|
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
|
|
|
|
end.
|
|
|
|
|
|
|
|
list_sr_groups_parse_addnew(Host, Query) ->
|
|
|
|
case lists:keysearch("namenew", 1, Query) of
|
|
|
|
{value, {_, Group}} when Group /= "" ->
|
|
|
|
mod_shared_roster:create_group(Host, Group),
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
list_sr_groups_parse_delete(Host, Query) ->
|
|
|
|
SRGroups = mod_shared_roster:list_groups(Host),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Group) ->
|
|
|
|
case lists:member({"selected", Group}, Query) of
|
|
|
|
true ->
|
|
|
|
mod_shared_roster:delete_group(Host, Group);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, SRGroups),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
|
|
|
|
shared_roster_group(Host, Group, Query, Lang) ->
|
|
|
|
Res = shared_roster_group_parse_query(Host, Group, Query),
|
|
|
|
GroupOpts = mod_shared_roster:get_group_opts(Host, Group),
|
|
|
|
Name = get_opt(GroupOpts, name, ""),
|
|
|
|
Description = get_opt(GroupOpts, description, ""),
|
|
|
|
AllUsers = get_opt(GroupOpts, all_users, false),
|
2007-12-07 01:09:48 +01:00
|
|
|
%%Disabled = false,
|
2007-08-23 02:51:54 +02:00
|
|
|
DisplayedGroups = get_opt(GroupOpts, displayed_groups, []),
|
|
|
|
Members = mod_shared_roster:get_group_explicit_users(Host, Group),
|
|
|
|
FMembers =
|
|
|
|
if
|
|
|
|
AllUsers ->
|
|
|
|
"@all@\n";
|
|
|
|
true ->
|
|
|
|
[]
|
|
|
|
end ++ [[us_to_list(Member), $\n] || Member <- Members],
|
|
|
|
FDisplayedGroups = [[DG, $\n] || DG <- DisplayedGroups],
|
2009-09-02 16:26:01 +02:00
|
|
|
DescNL = length(re:split(Description, "\n", [{return, list}])),
|
2007-08-23 02:51:54 +02:00
|
|
|
FGroup =
|
2009-05-27 19:29:58 +02:00
|
|
|
?XAE("table", [?XMLATTR('class', <<"withtextareas">>)],
|
2007-08-23 02:51:54 +02:00
|
|
|
[?XE("tbody",
|
|
|
|
[?XE("tr",
|
2010-03-01 11:44:02 +01:00
|
|
|
[?XCT("td", "ID:"),
|
|
|
|
?XE("td", [?C(Group)])
|
|
|
|
]
|
|
|
|
),
|
|
|
|
?XE("tr",
|
2007-08-23 02:51:54 +02:00
|
|
|
[?XCT("td", "Name:"),
|
|
|
|
?XE("td", [?INPUT("text", "name", Name)])
|
|
|
|
]
|
|
|
|
),
|
|
|
|
?XE("tr",
|
|
|
|
[?XCT("td", "Description:"),
|
2009-01-19 16:27:07 +01:00
|
|
|
?XE("td", [
|
|
|
|
?TEXTAREA("description", integer_to_list(lists:max([3, DescNL])), "20", Description)
|
|
|
|
]
|
|
|
|
)
|
2007-08-23 02:51:54 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
?XE("tr",
|
|
|
|
[?XCT("td", "Members:"),
|
2009-01-19 16:27:07 +01:00
|
|
|
?XE("td", [
|
|
|
|
?TEXTAREA("members", integer_to_list(lists:max([3, length(FMembers)])), "20", FMembers)
|
|
|
|
]
|
|
|
|
)
|
2007-08-23 02:51:54 +02:00
|
|
|
]
|
|
|
|
),
|
|
|
|
?XE("tr",
|
2010-03-01 11:44:02 +01:00
|
|
|
[?XCT("td", "IDs of Displayed Groups:"),
|
2009-01-19 16:27:07 +01:00
|
|
|
?XE("td", [
|
|
|
|
?TEXTAREA("dispgroups", integer_to_list(lists:max([3, length(FDisplayedGroups)])), "20", FDisplayedGroups)
|
|
|
|
]
|
|
|
|
)
|
2007-08-23 02:51:54 +02:00
|
|
|
]
|
|
|
|
)]
|
|
|
|
)]),
|
2009-01-19 12:59:40 +01:00
|
|
|
?H1GL(?T("Shared Roster Groups"), "modsharedroster", "mod_shared_roster") ++
|
2007-08-23 02:51:54 +02:00
|
|
|
[?XC("h2", ?T("Group ") ++ Group)] ++
|
|
|
|
case Res of
|
2009-01-19 16:27:07 +01:00
|
|
|
ok -> [?XREST("Submitted")];
|
|
|
|
error -> [?XREST("Bad format")];
|
2007-08-23 02:51:54 +02:00
|
|
|
nothing -> []
|
|
|
|
end ++
|
2009-05-27 19:29:58 +02:00
|
|
|
[?XAE("form", [?XMLATTR('action', <<"">>), ?XMLATTR('method', <<"post">>)],
|
2007-08-23 02:51:54 +02:00
|
|
|
[FGroup,
|
|
|
|
?BR,
|
|
|
|
?INPUTT("submit", "submit", "Submit")
|
|
|
|
])
|
|
|
|
].
|
|
|
|
|
|
|
|
shared_roster_group_parse_query(Host, Group, Query) ->
|
|
|
|
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 = string:tokens(SDispGroups, "\r\n"),
|
|
|
|
DispGroupsOpt =
|
|
|
|
if
|
|
|
|
DispGroups == [] -> [];
|
|
|
|
true -> [{displayed_groups, DispGroups}]
|
|
|
|
end,
|
|
|
|
|
|
|
|
OldMembers = mod_shared_roster:get_group_explicit_users(
|
|
|
|
Host, Group),
|
|
|
|
SJIDs = string:tokens(SMembers, ", \r\n"),
|
|
|
|
NewMembers =
|
|
|
|
lists:foldl(
|
|
|
|
fun(_SJID, error) -> error;
|
|
|
|
(SJID, USs) ->
|
|
|
|
case SJID of
|
|
|
|
"@all@" ->
|
|
|
|
USs;
|
|
|
|
_ ->
|
2008-09-29 13:29:09 +02:00
|
|
|
try
|
2009-06-01 18:35:55 +02:00
|
|
|
JID = exmpp_jid:parse(SJID),
|
2009-06-01 18:40:51 +02:00
|
|
|
[{exmpp_jid:prep_node_as_list(JID), exmpp_jid:prep_domain_as_list(JID)} | USs]
|
2008-09-29 13:29:09 +02:00
|
|
|
catch
|
|
|
|
_ ->
|
2007-08-23 02:51:54 +02:00
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end, [], SJIDs),
|
|
|
|
AllUsersOpt =
|
|
|
|
case lists:member("@all@", SJIDs) of
|
|
|
|
true -> [{all_users, true}];
|
|
|
|
false -> []
|
|
|
|
end,
|
|
|
|
|
|
|
|
mod_shared_roster:set_group_opts(
|
|
|
|
Host, Group,
|
|
|
|
NameOpt ++ DispGroupsOpt ++ DescriptionOpt ++ AllUsersOpt),
|
|
|
|
|
|
|
|
if
|
|
|
|
NewMembers == error -> error;
|
|
|
|
true ->
|
|
|
|
AddedMembers = NewMembers -- OldMembers,
|
|
|
|
RemovedMembers = OldMembers -- NewMembers,
|
|
|
|
lists:foreach(
|
|
|
|
fun(US) ->
|
|
|
|
mod_shared_roster:remove_user_from_group(
|
|
|
|
Host, US, Group)
|
|
|
|
end, RemovedMembers),
|
|
|
|
lists:foreach(
|
|
|
|
fun(US) ->
|
|
|
|
mod_shared_roster:add_user_to_group(
|
|
|
|
Host, US, Group)
|
|
|
|
end, AddedMembers),
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
nothing
|
|
|
|
end.
|
|
|
|
|
2009-03-03 19:53:28 +01:00
|
|
|
%% Get the roster module for Server.
|
|
|
|
get_roster_mod(Server) ->
|
|
|
|
case lists:member(mod_roster_odbc,
|
|
|
|
gen_mod:loaded_modules(Server)) of
|
|
|
|
true -> mod_roster_odbc;
|
|
|
|
false -> mod_roster
|
|
|
|
end.
|
|
|
|
|
2007-08-23 02:51:54 +02:00
|
|
|
get_opt(Opts, Opt, Default) ->
|
|
|
|
case lists:keysearch(Opt, 1, Opts) of
|
|
|
|
{value, {_, Val}} ->
|
|
|
|
Val;
|
|
|
|
false ->
|
|
|
|
Default
|
|
|
|
end.
|
|
|
|
|
|
|
|
us_to_list({User, Server}) ->
|
2009-06-01 18:54:33 +02:00
|
|
|
exmpp_jid:bare_to_list(User, Server).
|