2016-04-14 09:58:32 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% @copyright (C) 2016, Evgeny Khramtsov
|
|
|
|
%%% @doc
|
|
|
|
%%%
|
|
|
|
%%% @end
|
|
|
|
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(mod_roster_sql).
|
|
|
|
|
2016-05-04 20:01:05 +02:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2016-04-14 09:58:32 +02:00
|
|
|
-behaviour(mod_roster).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/2, read_roster_version/2, write_roster_version/4,
|
|
|
|
get_roster/2, get_roster_by_jid/3,
|
|
|
|
roster_subscribe/4, get_roster_by_jid_with_groups/3,
|
|
|
|
remove_user/2, update_roster/4, del_roster/3, transaction/2,
|
|
|
|
read_subscription_and_groups/3, get_only_items/2,
|
|
|
|
import/1, import/2, export/1]).
|
|
|
|
|
|
|
|
-include("mod_roster.hrl").
|
2016-05-04 20:01:05 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
read_roster_version(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case sql_queries:get_roster_version(LServer, LUser) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, [{Version}]} -> Version;
|
|
|
|
{selected, []} -> error
|
|
|
|
end.
|
|
|
|
|
|
|
|
write_roster_version(LUser, LServer, InTransaction, Ver) ->
|
|
|
|
if InTransaction ->
|
2016-05-04 20:01:05 +02:00
|
|
|
sql_queries:set_roster_version(LUser, Ver);
|
2016-04-14 09:58:32 +02:00
|
|
|
true ->
|
2016-04-20 11:27:32 +02:00
|
|
|
sql_queries:sql_transaction(
|
2016-04-14 09:58:32 +02:00
|
|
|
LServer,
|
|
|
|
fun () ->
|
2016-05-04 20:01:05 +02:00
|
|
|
sql_queries:set_roster_version(LUser, Ver)
|
2016-04-14 09:58:32 +02:00
|
|
|
end)
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_roster(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch sql_queries:get_roster(LServer, LUser) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, Items} when is_list(Items) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
JIDGroups = case catch sql_queries:get_roster_jid_groups(
|
2016-04-14 09:58:32 +02:00
|
|
|
LServer, LUser) of
|
|
|
|
{selected, JGrps} when is_list(JGrps) ->
|
|
|
|
JGrps;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
GroupsDict = lists:foldl(fun({J, G}, Acc) ->
|
|
|
|
dict:append(J, G, Acc)
|
|
|
|
end,
|
|
|
|
dict:new(), JIDGroups),
|
|
|
|
lists:flatmap(
|
|
|
|
fun(I) ->
|
|
|
|
case raw_to_record(LServer, I) of
|
|
|
|
%% Bad JID in database:
|
|
|
|
error -> [];
|
|
|
|
R ->
|
|
|
|
SJID = jid:to_string(R#roster.jid),
|
|
|
|
Groups = case dict:find(SJID, GroupsDict) of
|
|
|
|
{ok, Gs} -> Gs;
|
|
|
|
error -> []
|
|
|
|
end,
|
|
|
|
[R#roster{groups = Groups}]
|
|
|
|
end
|
|
|
|
end, Items);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_roster_by_jid(LUser, LServer, LJID) ->
|
|
|
|
{selected, Res} =
|
2016-04-20 11:27:32 +02:00
|
|
|
sql_queries:get_roster_by_jid(LServer, LUser, jid:to_string(LJID)),
|
2016-04-14 09:58:32 +02:00
|
|
|
case Res of
|
|
|
|
[] ->
|
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer}, jid = LJID};
|
|
|
|
[I] ->
|
|
|
|
R = raw_to_record(LServer, I),
|
|
|
|
case R of
|
|
|
|
%% Bad JID in database:
|
|
|
|
error ->
|
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer}, jid = LJID};
|
|
|
|
_ ->
|
|
|
|
R#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer}, jid = LJID, name = <<"">>}
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_only_items(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch sql_queries:get_roster(LServer, LUser) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, Is} when is_list(Is) ->
|
|
|
|
lists:map(fun(I) -> raw_to_record(LServer, I) end, Is);
|
|
|
|
_ -> []
|
|
|
|
end.
|
|
|
|
|
|
|
|
roster_subscribe(_LUser, _LServer, _LJID, Item) ->
|
|
|
|
ItemVals = record_to_row(Item),
|
2016-04-20 11:27:32 +02:00
|
|
|
sql_queries: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
|
|
|
|
|
|
|
get_roster_by_jid_with_groups(LUser, LServer, LJID) ->
|
|
|
|
SJID = jid:to_string(LJID),
|
2016-04-20 11:27:32 +02:00
|
|
|
case sql_queries:get_roster_by_jid(LServer, LUser, SJID) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, [I]} ->
|
|
|
|
R = raw_to_record(LServer, I),
|
|
|
|
Groups =
|
2016-04-20 11:27:32 +02:00
|
|
|
case sql_queries:get_roster_groups(LServer, LUser, SJID) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, JGrps} when is_list(JGrps) ->
|
|
|
|
[JGrp || {JGrp} <- JGrps];
|
|
|
|
_ -> []
|
|
|
|
end,
|
|
|
|
R#roster{groups = Groups};
|
|
|
|
{selected, []} ->
|
|
|
|
#roster{usj = {LUser, LServer, LJID},
|
|
|
|
us = {LUser, LServer}, jid = LJID}
|
|
|
|
end.
|
|
|
|
|
|
|
|
remove_user(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
sql_queries:del_user_roster_t(LServer, LUser),
|
2016-04-14 09:58:32 +02:00
|
|
|
{atomic, ok}.
|
|
|
|
|
|
|
|
update_roster(LUser, LServer, LJID, Item) ->
|
|
|
|
SJID = jid:to_string(LJID),
|
|
|
|
ItemVals = record_to_row(Item),
|
|
|
|
ItemGroups = Item#roster.groups,
|
2016-04-20 11:27:32 +02:00
|
|
|
sql_queries:update_roster(LServer, LUser, SJID, ItemVals,
|
2016-04-14 09:58:32 +02:00
|
|
|
ItemGroups).
|
|
|
|
|
|
|
|
del_roster(LUser, LServer, LJID) ->
|
|
|
|
SJID = jid:to_string(LJID),
|
2016-04-20 11:27:32 +02:00
|
|
|
sql_queries:del_roster(LServer, LUser, SJID).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
read_subscription_and_groups(LUser, LServer, LJID) ->
|
|
|
|
SJID = jid:to_string(LJID),
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch sql_queries:get_subscription(LServer, LUser, SJID) of
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, [{SSubscription}]} ->
|
|
|
|
Subscription = case SSubscription of
|
|
|
|
<<"B">> -> both;
|
|
|
|
<<"T">> -> to;
|
|
|
|
<<"F">> -> from;
|
|
|
|
_ -> none
|
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
Groups = case catch sql_queries:get_rostergroup_by_jid(
|
2016-04-14 09:58:32 +02:00
|
|
|
LServer, LUser, SJID) of
|
|
|
|
{selected, JGrps} when is_list(JGrps) ->
|
|
|
|
[JGrp || {JGrp} <- JGrps];
|
|
|
|
_ -> []
|
|
|
|
end,
|
|
|
|
{Subscription, Groups};
|
|
|
|
_ ->
|
|
|
|
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,
|
|
|
|
sql_queries: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 ->
|
2016-05-04 20:01:05 +02:00
|
|
|
[?SQL("delete from roster_version where username=%(LUser)s;"),
|
|
|
|
?SQL("insert into roster_version(username, version) values("
|
|
|
|
" %(LUser)s, %(Ver)s);")];
|
2016-04-14 09:58:32 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(LServer) ->
|
|
|
|
[{<<"select username, jid, nick, subscription, "
|
|
|
|
"ask, askmessage, server, subscribe, type from rosterusers;">>,
|
|
|
|
fun([LUser, JID|_] = Row) ->
|
|
|
|
Item = raw_to_record(LServer, Row),
|
2016-04-20 11:27:32 +02:00
|
|
|
Username = ejabberd_sql:escape(LUser),
|
|
|
|
SJID = ejabberd_sql:escape(JID),
|
2016-04-14 09:58:32 +02:00
|
|
|
{selected, _, Rows} =
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2016-04-14 09:58:32 +02:00
|
|
|
[<<"select grp from rostergroups where username='">>,
|
|
|
|
Username, <<"' and jid='">>, SJID, <<"'">>]),
|
|
|
|
Groups = [Grp || [Grp] <- Rows],
|
|
|
|
Item#roster{groups = Groups}
|
|
|
|
end},
|
|
|
|
{<<"select username, version from roster_version;">>,
|
|
|
|
fun([LUser, Ver]) ->
|
|
|
|
#roster_version{us = {LUser, LServer}, version = Ver}
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(_, _) ->
|
|
|
|
pass.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
raw_to_record(LServer,
|
|
|
|
[User, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
|
|
|
_SServer, _SSubscribe, _SType]) ->
|
|
|
|
raw_to_record(LServer,
|
|
|
|
{User, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
|
|
|
_SServer, _SSubscribe, _SType});
|
|
|
|
raw_to_record(LServer,
|
|
|
|
{User, SJID, Nick, SSubscription, SAsk, SAskMessage,
|
|
|
|
_SServer, _SSubscribe, _SType}) ->
|
|
|
|
case jid:from_string(SJID) of
|
|
|
|
error -> error;
|
|
|
|
JID ->
|
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
Subscription = case SSubscription of
|
|
|
|
<<"B">> -> both;
|
|
|
|
<<"T">> -> to;
|
|
|
|
<<"F">> -> from;
|
|
|
|
_ -> none
|
|
|
|
end,
|
|
|
|
Ask = case SAsk of
|
|
|
|
<<"S">> -> subscribe;
|
|
|
|
<<"U">> -> unsubscribe;
|
|
|
|
<<"B">> -> both;
|
|
|
|
<<"O">> -> out;
|
|
|
|
<<"I">> -> in;
|
|
|
|
_ -> none
|
|
|
|
end,
|
|
|
|
#roster{usj = {User, LServer, LJID},
|
|
|
|
us = {User, LServer}, jid = LJID, name = Nick,
|
|
|
|
subscription = Subscription, ask = Ask,
|
|
|
|
askmessage = SAskMessage}
|
|
|
|
end.
|
|
|
|
|
|
|
|
record_to_row(
|
|
|
|
#roster{us = {LUser, _LServer},
|
|
|
|
jid = JID, name = Name, subscription = Subscription,
|
|
|
|
ask = Ask, askmessage = AskMessage}) ->
|
|
|
|
SJID = jid:to_string(jid:tolower(JID)),
|
|
|
|
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,
|
|
|
|
{LUser, SJID, Name, SSubscription, SAsk, AskMessage}.
|