2016-04-14 10:45:43 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_shared_roster_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-14 10:45:43 +02:00
|
|
|
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2016-12-27 10:44:07 +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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-14 10:45:43 +02:00
|
|
|
-module(mod_shared_roster_sql).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2016-04-14 10:45:43 +02:00
|
|
|
-behaviour(mod_shared_roster).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/2, list_groups/1, groups_with_opts/1, create_group/3,
|
|
|
|
delete_group/2, get_group_opts/2, set_group_opts/3,
|
|
|
|
get_user_groups/2, get_group_explicit_users/2,
|
|
|
|
get_user_displayed_groups/3, is_user_in_group/3,
|
2016-11-22 14:48:01 +01:00
|
|
|
add_user_to_group/3, remove_user_from_group/3, import/3,
|
|
|
|
export/1]).
|
2016-04-14 10:45:43 +02:00
|
|
|
|
2016-07-30 07:39:30 +02:00
|
|
|
-include("jid.hrl").
|
2016-04-14 10:45:43 +02:00
|
|
|
-include("mod_roster.hrl").
|
|
|
|
-include("mod_shared_roster.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2016-04-14 10:45:43 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
list_groups(Host) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
2016-05-04 20:01:05 +02:00
|
|
|
Host,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(name)s from sr_group where %(Host)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, Rs} -> [G || {G} <- Rs];
|
2016-04-14 10:45:43 +02:00
|
|
|
_ -> []
|
|
|
|
end.
|
|
|
|
|
|
|
|
groups_with_opts(Host) ->
|
2016-05-04 20:01:05 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
Host,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(name)s, @(opts)s from sr_group where %(Host)H"))
|
2016-04-14 10:45:43 +02:00
|
|
|
of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, Rs} ->
|
2016-04-20 11:27:32 +02:00
|
|
|
[{G, mod_shared_roster:opts_to_binary(ejabberd_sql:decode_term(Opts))}
|
2016-05-04 20:01:05 +02:00
|
|
|
|| {G, Opts} <- Rs];
|
2016-04-14 10:45:43 +02:00
|
|
|
_ -> []
|
|
|
|
end.
|
|
|
|
|
|
|
|
create_group(Host, Group, Opts) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
SOpts = misc:term_to_expr(Opts),
|
2016-04-14 10:45:43 +02:00
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"sr_group",
|
|
|
|
["!name=%(Group)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(Host)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"opts=%(SOpts)s"])
|
2016-04-14 10:45:43 +02:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(Host, F).
|
2016-04-14 10:45:43 +02:00
|
|
|
|
|
|
|
delete_group(Host, Group) ->
|
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from sr_group where name=%(Group)s and %(Host)H")),
|
2016-05-04 20:01:05 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from sr_user where grp=%(Group)s and %(Host)H"))
|
2016-04-14 10:45:43 +02:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
case ejabberd_sql:sql_transaction(Host, F) of
|
2016-04-14 10:45:43 +02:00
|
|
|
{atomic,{updated,_}} -> {atomic, ok};
|
|
|
|
Res -> Res
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_group_opts(Host, Group) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
2016-04-14 10:45:43 +02:00
|
|
|
Host,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(opts)s from sr_group"
|
|
|
|
" where name=%(Group)s and %(Host)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, [{SOpts}]} ->
|
2016-04-20 11:27:32 +02:00
|
|
|
mod_shared_roster:opts_to_binary(ejabberd_sql:decode_term(SOpts));
|
2016-04-14 10:45:43 +02:00
|
|
|
_ -> error
|
|
|
|
end.
|
|
|
|
|
|
|
|
set_group_opts(Host, Group, Opts) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
SOpts = misc:term_to_expr(Opts),
|
2016-04-14 10:45:43 +02:00
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"sr_group",
|
|
|
|
["!name=%(Group)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(Host)s",
|
2016-05-04 20:01:05 +02:00
|
|
|
"opts=%(SOpts)s"])
|
2016-04-14 10:45:43 +02:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(Host, F).
|
2016-04-14 10:45:43 +02:00
|
|
|
|
|
|
|
get_user_groups(US, Host) ->
|
|
|
|
SJID = make_jid_s(US),
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
2016-04-14 10:45:43 +02:00
|
|
|
Host,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(grp)s from sr_user"
|
|
|
|
" where jid=%(SJID)s and %(Host)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, Rs} -> [G || {G} <- Rs];
|
2016-04-14 10:45:43 +02:00
|
|
|
_ -> []
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_group_explicit_users(Host, Group) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
2016-04-14 10:45:43 +02:00
|
|
|
Host,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(jid)s from sr_user"
|
|
|
|
" where grp=%(Group)s and %(Host)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, Rs} ->
|
2016-04-14 10:45:43 +02:00
|
|
|
lists:map(
|
2016-05-04 20:01:05 +02:00
|
|
|
fun({JID}) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
{U, S, _} = jid:tolower(jid:decode(JID)),
|
2016-04-14 10:45:43 +02:00
|
|
|
{U, S}
|
|
|
|
end, Rs);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_user_displayed_groups(LUser, LServer, GroupsOpts) ->
|
|
|
|
SJID = make_jid_s(LUser, LServer),
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
2016-04-14 10:45:43 +02:00
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("select @(grp)s from sr_user"
|
|
|
|
" where jid=%(SJID)s and %(LServer)H")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, Rs} ->
|
2016-04-14 10:45:43 +02:00
|
|
|
[{Group, proplists:get_value(Group, GroupsOpts, [])}
|
2016-05-04 20:01:05 +02:00
|
|
|
|| {Group} <- Rs];
|
2016-04-14 10:45:43 +02:00
|
|
|
_ -> []
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_user_in_group(US, Group, Host) ->
|
|
|
|
SJID = make_jid_s(US),
|
2016-05-04 20:01:05 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
|
|
|
Host,
|
|
|
|
?SQL("select @(jid)s from sr_user where jid=%(SJID)s"
|
2017-11-02 15:03:30 +01:00
|
|
|
" and %(Host)H and grp=%(Group)s")) of
|
2016-05-04 20:01:05 +02:00
|
|
|
{selected, []} -> false;
|
2016-04-14 10:45:43 +02:00
|
|
|
_ -> true
|
|
|
|
end.
|
|
|
|
|
|
|
|
add_user_to_group(Host, US, Group) ->
|
|
|
|
SJID = make_jid_s(US),
|
2016-05-04 20:01:05 +02:00
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
Host,
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL_INSERT(
|
|
|
|
"sr_user",
|
|
|
|
["jid=%(SJID)s",
|
|
|
|
"server_host=%(Host)s",
|
|
|
|
"grp=%(Group)s"])).
|
2016-04-14 10:45:43 +02:00
|
|
|
|
|
|
|
remove_user_from_group(Host, US, Group) ->
|
|
|
|
SJID = make_jid_s(US),
|
|
|
|
F = fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from sr_user where jid=%(SJID)s and %(Host)H"
|
|
|
|
" and grp=%(Group)s")),
|
2016-04-14 10:45:43 +02:00
|
|
|
ok
|
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(Host, F).
|
2016-04-14 10:45:43 +02:00
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{sr_group,
|
|
|
|
fun(Host, #sr_group{group_host = {Group, LServer}, opts = Opts})
|
|
|
|
when LServer == Host ->
|
2017-04-11 12:13:58 +02:00
|
|
|
SOpts = misc:term_to_expr(Opts),
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from sr_group where name=%(Group)s and %(Host)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"sr_group",
|
|
|
|
["name=%(Group)s",
|
|
|
|
"server_host=%(Host)s",
|
|
|
|
"opts=%(SOpts)s"])];
|
2016-04-14 10:45:43 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end},
|
|
|
|
{sr_user,
|
|
|
|
fun(Host, #sr_user{us = {U, S}, group_host = {Group, LServer}})
|
|
|
|
when LServer == Host ->
|
2017-02-25 08:01:01 +01:00
|
|
|
SJID = make_jid_s(U, S),
|
2016-05-04 20:01:05 +02:00
|
|
|
[?SQL("select @(jid)s from sr_user where jid=%(SJID)s"
|
2017-11-02 15:03:30 +01:00
|
|
|
" and %(Host)H and grp=%(Group)s;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"sr_user",
|
|
|
|
["jid=%(SJID)s",
|
|
|
|
"server_host=%(Host)s",
|
|
|
|
"grp=%(Group)s"])];
|
2016-04-14 10:45:43 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(_, _, _) ->
|
|
|
|
ok.
|
2016-04-14 10:45:43 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
make_jid_s(U, S) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
jid:encode(jid:tolower(jid:make(U, S))).
|
2016-04-14 10:45:43 +02:00
|
|
|
|
|
|
|
make_jid_s({U, S}) -> make_jid_s(U, S).
|