2016-04-14 09:58:32 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_roster_sql.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-14 09:58:32 +02:00
|
|
|
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 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 09:58:32 +02:00
|
|
|
-module(mod_roster_sql).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
|
2016-04-14 09:58:32 +02:00
|
|
|
-behaviour(mod_roster).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/2, read_roster_version/2, write_roster_version/4,
|
2017-05-17 13:47:35 +02:00
|
|
|
get_roster/2, get_roster_item/3, roster_subscribe/4,
|
|
|
|
read_subscription_and_groups/3, remove_user/2,
|
|
|
|
update_roster/4, del_roster/3, transaction/2,
|
2019-05-15 10:57:55 +02:00
|
|
|
process_rosteritems/5,
|
2016-11-22 17:51:21 +01:00
|
|
|
import/3, export/1, raw_to_record/2]).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
-include("mod_roster.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2017-01-18 09:59:46 +01:00
|
|
|
-include("logger.hrl").
|
2019-05-15 10:57:55 +02:00
|
|
|
-include("jid.hrl").
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
read_roster_version(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(version)s from roster_version"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username = %(LUser)s and %(LServer)H")) of
|
2017-05-17 13:47:35 +02:00
|
|
|
{selected, [{Version}]} -> {ok, Version};
|
2017-05-23 11:25:13 +02:00
|
|
|
{selected, []} -> error;
|
|
|
|
_ -> {error, db_failure}
|
2016-04-14 09:58:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
write_roster_version(LUser, LServer, InTransaction, Ver) ->
|
|
|
|
if InTransaction ->
|
2017-11-02 15:03:30 +01:00
|
|
|
set_roster_version(LUser, LServer, Ver);
|
2016-04-14 09:58:32 +02:00
|
|
|
true ->
|
2017-05-23 11:25:13 +02:00
|
|
|
transaction(
|
2016-04-14 09:58:32 +02:00
|
|
|
LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
fun () -> set_roster_version(LUser, LServer, Ver) end)
|
2016-04-14 09:58:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_roster(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
case ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(username)s, @(jid)s, @(nick)s, @(subscription)s, "
|
|
|
|
"@(ask)s, @(askmessage)s, @(server)s, @(subscribe)s, "
|
2017-11-02 15:03:30 +01:00
|
|
|
"@(type)s from rosterusers "
|
|
|
|
"where username=%(LUser)s and %(LServer)H")) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, Items} when is_list(Items) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
JIDGroups = case get_roster_jid_groups(LServer, LUser) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, JGrps} when is_list(JGrps) ->
|
|
|
|
JGrps;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
GroupsDict = lists:foldl(fun({J, G}, Acc) ->
|
|
|
|
dict:append(J, G, Acc)
|
|
|
|
end,
|
|
|
|
dict:new(), JIDGroups),
|
2017-05-17 13:47:35 +02:00
|
|
|
{ok, lists:flatmap(
|
|
|
|
fun(I) ->
|
|
|
|
case raw_to_record(LServer, I) of
|
|
|
|
%% Bad JID in database:
|
|
|
|
error -> [];
|
|
|
|
R ->
|
|
|
|
SJID = jid:encode(R#roster.jid),
|
|
|
|
Groups = case dict:find(SJID, GroupsDict) of
|
|
|
|
{ok, Gs} -> Gs;
|
|
|
|
error -> []
|
|
|
|
end,
|
|
|
|
[R#roster{groups = Groups}]
|
|
|
|
end
|
|
|
|
end, Items)};
|
2016-04-14 09:58:32 +02:00
|
|
|
_ ->
|
2017-05-17 13:47:35 +02:00
|
|
|
error
|
2016-04-14 09:58:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
roster_subscribe(_LUser, _LServer, _LJID, Item) ->
|
|
|
|
ItemVals = record_to_row(Item),
|
2017-05-23 11:25:13 +02:00
|
|
|
roster_subscribe(ItemVals).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
transaction(LServer, F) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(LServer, F).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
2017-05-17 13:47:35 +02:00
|
|
|
get_roster_item(LUser, LServer, LJID) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(LJID),
|
2017-05-23 11:25:13 +02:00
|
|
|
case get_roster_by_jid(LServer, LUser, SJID) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, [I]} ->
|
2017-01-18 09:59:46 +01:00
|
|
|
case raw_to_record(LServer, I) of
|
|
|
|
error ->
|
2017-05-17 13:47:35 +02:00
|
|
|
error;
|
2017-01-18 09:59:46 +01:00
|
|
|
R ->
|
2017-05-23 11:25:13 +02:00
|
|
|
Groups = case get_roster_groups(LServer, LUser, SJID) of
|
|
|
|
{selected, JGrps} when is_list(JGrps) ->
|
|
|
|
[JGrp || {JGrp} <- JGrps];
|
|
|
|
_ -> []
|
|
|
|
end,
|
2017-05-17 13:47:35 +02:00
|
|
|
{ok, R#roster{groups = Groups}}
|
2017-01-18 09:59:46 +01:00
|
|
|
end;
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, []} ->
|
2017-05-17 13:47:35 +02:00
|
|
|
error
|
2016-04-14 09:58:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
remove_user(LUser, LServer) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
transaction(
|
|
|
|
LServer,
|
|
|
|
fun () ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from rosterusers"
|
|
|
|
" where username=%(LUser)s and %(LServer)H")),
|
2017-05-23 11:25:13 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL("delete from rostergroups"
|
|
|
|
" where username=%(LUser)s and %(LServer)H"))
|
2017-05-23 11:25:13 +02:00
|
|
|
end),
|
2017-05-17 13:47:35 +02:00
|
|
|
ok.
|
2016-04-14 09:58:32 +02:00
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
update_roster(LUser, LServer, LJID, Item) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(LJID),
|
2016-04-14 09:58:32 +02:00
|
|
|
ItemVals = record_to_row(Item),
|
|
|
|
ItemGroups = Item#roster.groups,
|
2017-05-23 11:25:13 +02:00
|
|
|
roster_subscribe(ItemVals),
|
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from rostergroups"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H and jid=%(SJID)s")),
|
2017-05-23 11:25:13 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(ItemGroup) ->
|
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-02 15:03:30 +01:00
|
|
|
?SQL_INSERT(
|
|
|
|
"rostergroups",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"jid=%(SJID)s",
|
|
|
|
"grp=%(ItemGroup)s"]))
|
2017-05-23 11:25:13 +02:00
|
|
|
end,
|
|
|
|
ItemGroups).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
del_roster(LUser, LServer, LJID) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(LJID),
|
2017-05-23 11:25:13 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from rosterusers"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H and jid=%(SJID)s")),
|
2017-05-23 11:25:13 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from rostergroups"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H and jid=%(SJID)s")).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
read_subscription_and_groups(LUser, LServer, LJID) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(LJID),
|
2017-05-23 11:25:13 +02:00
|
|
|
case get_subscription(LServer, LUser, SJID) of
|
2018-01-26 13:02:06 +01:00
|
|
|
{selected, [{SSubscription, SAsk}]} ->
|
|
|
|
Subscription = decode_subscription(LUser, LServer, SSubscription),
|
|
|
|
Ask = decode_ask(LUser, LServer, SAsk),
|
2017-05-23 11:25:13 +02:00
|
|
|
Groups = case get_rostergroup_by_jid(LServer, LUser, SJID) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, JGrps} when is_list(JGrps) ->
|
|
|
|
[JGrp || {JGrp} <- JGrps];
|
|
|
|
_ -> []
|
|
|
|
end,
|
2018-01-26 13:02:06 +01:00
|
|
|
{ok, {Subscription, Ask, Groups}};
|
2016-04-14 09:58:32 +02:00
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{roster,
|
2016-05-04 20:01:05 +02:00
|
|
|
fun(Host, #roster{usj = {_LUser, LServer, _LJID}} = R)
|
2016-04-14 09:58:32 +02:00
|
|
|
when LServer == Host ->
|
2016-05-04 20:01:05 +02:00
|
|
|
ItemVals = record_to_row(R),
|
|
|
|
ItemGroups = R#roster.groups,
|
2017-05-23 11:25:13 +02:00
|
|
|
update_roster_sql(ItemVals, ItemGroups);
|
2016-04-14 09:58:32 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end},
|
|
|
|
{roster_version,
|
|
|
|
fun(Host, #roster_version{us = {LUser, LServer}, version = Ver})
|
|
|
|
when LServer == Host ->
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL("delete from roster_version"
|
|
|
|
" where username=%(LUser)s and %(LServer)H;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"roster_version",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"version=%(Ver)s"])];
|
2016-04-14 09:58:32 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(_, _, _) ->
|
|
|
|
ok.
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2017-11-02 15:03:30 +01:00
|
|
|
set_roster_version(LUser, LServer, Version) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"roster_version",
|
|
|
|
["!username=%(LUser)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(LServer)s",
|
2017-05-23 11:25:13 +02:00
|
|
|
"version=%(Version)s"]).
|
|
|
|
|
|
|
|
get_roster_jid_groups(LServer, LUser) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(jid)s, @(grp)s from rostergroups where "
|
2017-11-02 15:03:30 +01:00
|
|
|
"username=%(LUser)s and %(LServer)H")).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
get_roster_groups(LServer, LUser, SJID) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(grp)s from rostergroups"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H and jid=%(SJID)s")).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
roster_subscribe({LUser, LServer, SJID, Name, SSubscription, SAsk, AskMessage}) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
?SQL_UPSERT_T(
|
|
|
|
"rosterusers",
|
|
|
|
["!username=%(LUser)s",
|
2017-11-02 15:03:30 +01:00
|
|
|
"!server_host=%(LServer)s",
|
2017-05-23 11:25:13 +02:00
|
|
|
"!jid=%(SJID)s",
|
|
|
|
"nick=%(Name)s",
|
|
|
|
"subscription=%(SSubscription)s",
|
|
|
|
"ask=%(SAsk)s",
|
|
|
|
"askmessage=%(AskMessage)s",
|
|
|
|
"server='N'",
|
|
|
|
"subscribe=''",
|
|
|
|
"type='item'"]).
|
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
get_roster_by_jid(LServer, LUser, SJID) ->
|
2017-05-23 11:25:13 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("select @(username)s, @(jid)s, @(nick)s, @(subscription)s,"
|
|
|
|
" @(ask)s, @(askmessage)s, @(server)s, @(subscribe)s,"
|
|
|
|
" @(type)s from rosterusers"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H and jid=%(SJID)s")).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
|
|
|
get_rostergroup_by_jid(LServer, LUser, SJID) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(grp)s from rostergroups"
|
2017-11-02 15:03:30 +01:00
|
|
|
" where username=%(LUser)s and %(LServer)H and jid=%(SJID)s")).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
|
|
|
get_subscription(LServer, LUser, SJID) ->
|
|
|
|
ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
2018-01-26 13:02:06 +01:00
|
|
|
?SQL("select @(subscription)s, @(ask)s from rosterusers "
|
2017-11-02 15:03:30 +01:00
|
|
|
"where username=%(LUser)s and %(LServer)H and jid=%(SJID)s")).
|
2017-05-23 11:25:13 +02:00
|
|
|
|
2017-11-02 15:03:30 +01:00
|
|
|
update_roster_sql({LUser, LServer, SJID, Name, SSubscription, SAsk, AskMessage},
|
2017-05-23 11:25:13 +02:00
|
|
|
ItemGroups) ->
|
|
|
|
[?SQL("delete from rosterusers where"
|
2017-11-02 15:03:30 +01:00
|
|
|
" username=%(LUser)s and %(LServer)H and jid=%(SJID)s;"),
|
|
|
|
?SQL_INSERT(
|
|
|
|
"rosterusers",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"jid=%(SJID)s",
|
|
|
|
"nick=%(Name)s",
|
|
|
|
"subscription=%(SSubscription)s",
|
|
|
|
"ask=%(SAsk)s",
|
|
|
|
"askmessage=%(AskMessage)s",
|
|
|
|
"server='N'",
|
|
|
|
"subscribe=''",
|
|
|
|
"type='item'"]),
|
2017-05-23 11:25:13 +02:00
|
|
|
?SQL("delete from rostergroups where"
|
2017-11-02 15:03:30 +01:00
|
|
|
" username=%(LUser)s and %(LServer)H and jid=%(SJID)s;")]
|
2017-05-23 11:25:13 +02:00
|
|
|
++
|
2017-11-02 15:03:30 +01:00
|
|
|
[?SQL_INSERT(
|
|
|
|
"rostergroups",
|
|
|
|
["username=%(LUser)s",
|
|
|
|
"server_host=%(LServer)s",
|
|
|
|
"jid=%(SJID)s",
|
|
|
|
"grp=%(ItemGroup)s"])
|
2017-05-23 11:25:13 +02:00
|
|
|
|| ItemGroup <- ItemGroups].
|
|
|
|
|
2016-04-14 09:58:32 +02:00
|
|
|
raw_to_record(LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
[User, LServer, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
2016-04-14 09:58:32 +02:00
|
|
|
_SServer, _SSubscribe, _SType]) ->
|
|
|
|
raw_to_record(LServer,
|
2017-11-02 15:03:30 +01:00
|
|
|
{User, LServer, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
2016-04-14 09:58:32 +02:00
|
|
|
_SServer, _SSubscribe, _SType});
|
|
|
|
raw_to_record(LServer,
|
|
|
|
{User, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
|
|
|
_SServer, _SSubscribe, _SType}) ->
|
2017-11-02 15:03:30 +01:00
|
|
|
raw_to_record(LServer,
|
|
|
|
{User, LServer, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
|
|
|
_SServer, _SSubscribe, _SType});
|
|
|
|
raw_to_record(LServer,
|
|
|
|
{User, LServer, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
|
|
|
_SServer, _SSubscribe, _SType}) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
try jid:decode(SJID) of
|
2016-04-14 09:58:32 +02:00
|
|
|
JID ->
|
|
|
|
LJID = jid:tolower(JID),
|
2018-01-26 13:02:06 +01:00
|
|
|
Subscription = decode_subscription(User, LServer, SSubscription),
|
|
|
|
Ask = decode_ask(User, LServer, SAsk),
|
2016-04-14 09:58:32 +02:00
|
|
|
#roster{usj = {User, LServer, LJID},
|
|
|
|
us = {User, LServer}, jid = LJID, name = Nick,
|
|
|
|
subscription = Subscription, ask = Ask,
|
|
|
|
askmessage = SAskMessage}
|
2017-02-26 08:07:12 +01:00
|
|
|
catch _:{bad_jid, _} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("~ts", [format_row_error(User, LServer, {jid, SJID})]),
|
2017-02-26 08:07:12 +01:00
|
|
|
error
|
2016-04-14 09:58:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
record_to_row(
|
2017-11-02 15:03:30 +01:00
|
|
|
#roster{us = {LUser, LServer},
|
2016-04-14 09:58:32 +02:00
|
|
|
jid = JID, name = Name, subscription = Subscription,
|
|
|
|
ask = Ask, askmessage = AskMessage}) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
SJID = jid:encode(jid:tolower(JID)),
|
2016-04-14 09:58:32 +02:00
|
|
|
SSubscription = case Subscription of
|
|
|
|
both -> <<"B">>;
|
|
|
|
to -> <<"T">>;
|
|
|
|
from -> <<"F">>;
|
|
|
|
none -> <<"N">>
|
|
|
|
end,
|
|
|
|
SAsk = case Ask of
|
|
|
|
subscribe -> <<"S">>;
|
|
|
|
unsubscribe -> <<"U">>;
|
|
|
|
both -> <<"B">>;
|
|
|
|
out -> <<"O">>;
|
|
|
|
in -> <<"I">>;
|
|
|
|
none -> <<"N">>
|
|
|
|
end,
|
2017-11-02 15:03:30 +01:00
|
|
|
{LUser, LServer, SJID, Name, SSubscription, SAsk, AskMessage}.
|
2017-01-18 09:59:46 +01:00
|
|
|
|
2018-01-26 13:02:06 +01:00
|
|
|
decode_subscription(User, Server, S) ->
|
|
|
|
case S of
|
|
|
|
<<"B">> -> both;
|
|
|
|
<<"T">> -> to;
|
|
|
|
<<"F">> -> from;
|
|
|
|
<<"N">> -> none;
|
|
|
|
<<"">> -> none;
|
|
|
|
_ ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("~ts", [format_row_error(User, Server, {subscription, S})]),
|
2018-01-26 13:02:06 +01:00
|
|
|
none
|
|
|
|
end.
|
|
|
|
|
|
|
|
decode_ask(User, Server, A) ->
|
|
|
|
case A of
|
|
|
|
<<"S">> -> subscribe;
|
|
|
|
<<"U">> -> unsubscribe;
|
|
|
|
<<"B">> -> both;
|
|
|
|
<<"O">> -> out;
|
|
|
|
<<"I">> -> in;
|
|
|
|
<<"N">> -> none;
|
|
|
|
<<"">> -> none;
|
|
|
|
_ ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("~ts", [format_row_error(User, Server, {ask, A})]),
|
2018-01-26 13:02:06 +01:00
|
|
|
none
|
|
|
|
end.
|
|
|
|
|
2017-01-18 09:59:46 +01:00
|
|
|
format_row_error(User, Server, Why) ->
|
|
|
|
[case Why of
|
|
|
|
{jid, JID} -> ["Malformed 'jid' field with value '", JID, "'"];
|
|
|
|
{subscription, Sub} -> ["Malformed 'subscription' field with value '", Sub, "'"];
|
|
|
|
{ask, Ask} -> ["Malformed 'ask' field with value '", Ask, "'"]
|
|
|
|
end,
|
|
|
|
" detected for ", User, "@", Server, " in table 'rosterusers'"].
|
2019-05-15 10:57:55 +02:00
|
|
|
|
|
|
|
process_rosteritems(ActionS, SubsS, AsksS, UsersS, ContactsS) ->
|
|
|
|
process_rosteritems_sql(ActionS, list_to_atom(SubsS), list_to_atom(AsksS),
|
|
|
|
list_to_binary(UsersS), list_to_binary(ContactsS)).
|
|
|
|
|
|
|
|
process_rosteritems_sql(ActionS, Subscription, Ask, SLocalJID, SJID) ->
|
|
|
|
[LUser, LServer] = binary:split(SLocalJID, <<"@">>),
|
|
|
|
SSubscription = case Subscription of
|
|
|
|
any -> <<"_">>;
|
|
|
|
both -> <<"B">>;
|
|
|
|
to -> <<"T">>;
|
|
|
|
from -> <<"F">>;
|
|
|
|
none -> <<"N">>
|
|
|
|
end,
|
|
|
|
SAsk = case Ask of
|
|
|
|
any -> <<"_">>;
|
|
|
|
subscribe -> <<"S">>;
|
|
|
|
unsubscribe -> <<"U">>;
|
|
|
|
both -> <<"B">>;
|
|
|
|
out -> <<"O">>;
|
|
|
|
in -> <<"I">>;
|
|
|
|
none -> <<"N">>
|
|
|
|
end,
|
|
|
|
{selected, List} = ejabberd_sql:sql_query(
|
|
|
|
LServer,
|
|
|
|
?SQL("select @(username)s, @(jid)s from rosterusers "
|
|
|
|
"where username LIKE %(LUser)s"
|
|
|
|
" and %(LServer)H"
|
|
|
|
" and jid LIKE %(SJID)s"
|
|
|
|
" and subscription LIKE %(SSubscription)s"
|
|
|
|
" and ask LIKE %(SAsk)s")),
|
|
|
|
case ActionS of
|
2019-07-09 13:30:59 +02:00
|
|
|
"delete" -> [mod_roster:del_roster(User, LServer, jid:tolower(jid:decode(Contact))) || {User, Contact} <- List];
|
2019-05-15 10:57:55 +02:00
|
|
|
"list" -> ok
|
|
|
|
end,
|
|
|
|
List.
|