2016-04-14 13:16:32 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_privacy_mnesia.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-14 13:16:32 +02:00
|
|
|
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2022-02-10 17:21:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2022 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 13:16:32 +02:00
|
|
|
-module(mod_privacy_mnesia).
|
|
|
|
|
|
|
|
-behaviour(mod_privacy).
|
|
|
|
|
|
|
|
%% API
|
2017-05-20 21:36:32 +02:00
|
|
|
-export([init/2, set_default/3, unset_default/2, set_lists/1,
|
|
|
|
set_list/4, get_lists/2, get_list/3, remove_lists/2,
|
|
|
|
remove_list/3, use_cache/1, import/1]).
|
2017-04-23 15:37:58 +02:00
|
|
|
-export([need_transform/1, transform/1]).
|
2016-04-14 13:16:32 +02:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2016-04-14 13:16:32 +02:00
|
|
|
-include("mod_privacy.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, privacy,
|
2017-05-20 21:36:32 +02:00
|
|
|
[{disc_only_copies, [node()]},
|
2017-04-23 15:37:58 +02:00
|
|
|
{attributes, record_info(fields, privacy)}]).
|
2016-04-14 13:16:32 +02:00
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
use_cache(Host) ->
|
|
|
|
case mnesia:table_info(privacy, storage_type) of
|
|
|
|
disc_only_copies ->
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_privacy_opt:use_cache(Host);
|
2017-05-20 21:36:32 +02:00
|
|
|
_ ->
|
|
|
|
false
|
2016-04-14 13:16:32 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
unset_default(LUser, LServer) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
F = fun () ->
|
|
|
|
case mnesia:read({privacy, {LUser, LServer}}) of
|
|
|
|
[] -> ok;
|
|
|
|
[R] -> mnesia:write(R#privacy{default = none})
|
|
|
|
end
|
|
|
|
end,
|
2017-05-20 21:36:32 +02:00
|
|
|
transaction(F).
|
|
|
|
|
|
|
|
set_default(LUser, LServer, Name) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
F = fun () ->
|
|
|
|
case mnesia:read({privacy, {LUser, LServer}}) of
|
2017-05-20 21:36:32 +02:00
|
|
|
[] ->
|
|
|
|
{error, notfound};
|
2016-04-14 13:16:32 +02:00
|
|
|
[#privacy{lists = Lists} = P] ->
|
|
|
|
case lists:keymember(Name, 1, Lists) of
|
|
|
|
true ->
|
|
|
|
mnesia:write(P#privacy{default = Name,
|
2017-05-20 21:36:32 +02:00
|
|
|
lists = Lists});
|
|
|
|
false ->
|
|
|
|
{error, notfound}
|
2016-04-14 13:16:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2017-05-20 21:36:32 +02:00
|
|
|
transaction(F).
|
2016-04-14 13:16:32 +02:00
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
remove_list(LUser, LServer, Name) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
F = fun () ->
|
|
|
|
case mnesia:read({privacy, {LUser, LServer}}) of
|
2017-05-20 21:36:32 +02:00
|
|
|
[] ->
|
|
|
|
{error, notfound};
|
|
|
|
[#privacy{default = Default, lists = Lists} = P] ->
|
|
|
|
if Name == Default ->
|
|
|
|
{error, conflict};
|
|
|
|
true ->
|
|
|
|
NewLists = lists:keydelete(Name, 1, Lists),
|
|
|
|
mnesia:write(P#privacy{lists = NewLists})
|
|
|
|
end
|
2016-04-14 13:16:32 +02:00
|
|
|
end
|
|
|
|
end,
|
2017-05-20 21:36:32 +02:00
|
|
|
transaction(F).
|
2016-04-14 13:16:32 +02:00
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
set_lists(Privacy) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
mnesia:dirty_write(Privacy).
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
set_list(LUser, LServer, Name, List) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
F = fun () ->
|
|
|
|
case mnesia:wread({privacy, {LUser, LServer}}) of
|
2017-05-20 21:36:32 +02:00
|
|
|
[] ->
|
|
|
|
NewLists = [{Name, List}],
|
|
|
|
mnesia:write(#privacy{us = {LUser, LServer},
|
|
|
|
lists = NewLists});
|
|
|
|
[#privacy{lists = Lists} = P] ->
|
|
|
|
NewLists1 = lists:keydelete(Name, 1, Lists),
|
|
|
|
NewLists = [{Name, List} | NewLists1],
|
|
|
|
mnesia:write(P#privacy{lists = NewLists})
|
2016-04-14 13:16:32 +02:00
|
|
|
end
|
|
|
|
end,
|
2017-05-20 21:36:32 +02:00
|
|
|
transaction(F).
|
|
|
|
|
|
|
|
get_list(LUser, LServer, Name) ->
|
|
|
|
case mnesia:dirty_read(privacy, {LUser, LServer}) of
|
|
|
|
[#privacy{default = Default, lists = Lists}] when Name == default ->
|
|
|
|
case lists:keyfind(Default, 1, Lists) of
|
|
|
|
{_, List} -> {ok, {Default, List}};
|
|
|
|
false -> error
|
|
|
|
end;
|
|
|
|
[#privacy{lists = Lists}] ->
|
|
|
|
case lists:keyfind(Name, 1, Lists) of
|
|
|
|
{_, List} -> {ok, {Name, List}};
|
|
|
|
false -> error
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
error
|
2016-04-14 13:16:32 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
get_lists(LUser, LServer) ->
|
|
|
|
case mnesia:dirty_read(privacy, {LUser, LServer}) of
|
2016-04-14 13:16:32 +02:00
|
|
|
[#privacy{} = P] ->
|
|
|
|
{ok, P};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
remove_lists(LUser, LServer) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
F = fun () -> mnesia:delete({privacy, {LUser, LServer}}) end,
|
2017-05-20 21:36:32 +02:00
|
|
|
transaction(F).
|
2016-04-14 13:16:32 +02:00
|
|
|
|
2016-11-22 16:01:08 +01:00
|
|
|
import(#privacy{} = P) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
mnesia:dirty_write(P).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
need_transform({privacy, {U, S}, _, _}) when is_list(U) orelse is_list(S) ->
|
2017-04-23 15:37:58 +02:00
|
|
|
?INFO_MSG("Mnesia table 'privacy' will be converted to binary", []),
|
|
|
|
true;
|
|
|
|
need_transform(_) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
transform(#privacy{us = {U, S}, default = Def, lists = Lists} = R) ->
|
|
|
|
NewLists = lists:map(
|
|
|
|
fun({Name, Ls}) ->
|
|
|
|
NewLs = lists:map(
|
|
|
|
fun(#listitem{value = Val} = L) ->
|
|
|
|
NewVal = case Val of
|
2016-04-14 13:16:32 +02:00
|
|
|
{LU, LS, LR} ->
|
|
|
|
{iolist_to_binary(LU),
|
|
|
|
iolist_to_binary(LS),
|
|
|
|
iolist_to_binary(LR)};
|
|
|
|
none -> none;
|
|
|
|
both -> both;
|
|
|
|
from -> from;
|
|
|
|
to -> to;
|
|
|
|
_ -> iolist_to_binary(Val)
|
|
|
|
end,
|
2017-04-23 15:37:58 +02:00
|
|
|
L#listitem{value = NewVal}
|
|
|
|
end, Ls),
|
|
|
|
{iolist_to_binary(Name), NewLs}
|
|
|
|
end, Lists),
|
|
|
|
NewDef = case Def of
|
|
|
|
none -> none;
|
|
|
|
_ -> iolist_to_binary(Def)
|
|
|
|
end,
|
|
|
|
NewUS = {iolist_to_binary(U), iolist_to_binary(S)},
|
|
|
|
R#privacy{us = NewUS, default = NewDef, lists = NewLists}.
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2017-05-20 21:36:32 +02:00
|
|
|
transaction(F) ->
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{atomic, Result} ->
|
|
|
|
Result;
|
|
|
|
{aborted, Reason} ->
|
|
|
|
?ERROR_MSG("Mnesia transaction failed: ~p", [Reason]),
|
|
|
|
{error, db_failure}
|
|
|
|
end.
|