2003-01-16 21:24:53 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_private.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Support for private storage.
|
|
|
|
%%% Created : 16 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2024 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-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +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.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
2003-01-16 21:24:53 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_private).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 49, '1.2'}).
|
2022-11-07 12:32:38 +01:00
|
|
|
-protocol({xep, 411, '0.2.0', '18.12', "", ""}).
|
2023-10-02 10:54:24 +02:00
|
|
|
-protocol({xep, 402, '1.1.3', '23.10', "", ""}).
|
2015-05-21 17:02:36 +02:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, process_sm_iq/1, import_info/0,
|
2020-01-08 10:24:51 +01:00
|
|
|
remove_user/2, get_data/2, get_data/3, export/1, mod_doc/0,
|
2018-11-23 11:33:29 +01:00
|
|
|
import/5, import_start/2, mod_opt_type/1, set_data/2,
|
2023-09-27 18:36:30 +02:00
|
|
|
mod_options/1, depends/2, get_sm_features/5, pubsub_publish_item/6,
|
2023-10-02 14:39:30 +02:00
|
|
|
pubsub_delete_item/5, pubsub_tree_call/4]).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2018-12-05 22:04:40 +01:00
|
|
|
-export([get_commands_spec/0, bookmarks_to_pep/2]).
|
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2016-04-13 13:09:34 +02:00
|
|
|
-include("mod_private.hrl").
|
2018-12-05 22:04:40 +01:00
|
|
|
-include("ejabberd_commands.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2023-09-27 18:36:30 +02:00
|
|
|
-include("pubsub.hrl").
|
2016-04-13 13:09:34 +02:00
|
|
|
|
2017-05-22 09:34:57 +02:00
|
|
|
-define(PRIVATE_CACHE, private_cache).
|
|
|
|
|
2016-04-13 13:09:34 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
2016-11-22 14:48:01 +01:00
|
|
|
-callback import(binary(), binary(), [binary()]) -> ok.
|
2017-05-22 09:34:57 +02:00
|
|
|
-callback set_data(binary(), binary(), [{binary(), xmlel()}]) -> ok | {error, any()}.
|
|
|
|
-callback get_data(binary(), binary(), binary()) -> {ok, xmlel()} | error | {error, any()}.
|
|
|
|
-callback get_all_data(binary(), binary()) -> {ok, [xmlel()]} | error | {error, any()}.
|
|
|
|
-callback del_data(binary(), binary()) -> ok | {error, any()}.
|
|
|
|
-callback use_cache(binary()) -> boolean().
|
|
|
|
-callback cache_nodes(binary()) -> [node()].
|
|
|
|
|
|
|
|
-optional_callbacks([use_cache/1, cache_nodes/1]).
|
2011-12-30 15:08:24 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
Mod = gen_mod:db_mod(Opts, ?MODULE),
|
2016-04-13 13:09:34 +02:00
|
|
|
Mod:init(Host, Opts),
|
2017-05-22 09:34:57 +02:00
|
|
|
init_cache(Mod, Host, Opts),
|
2023-08-04 17:53:50 +02:00
|
|
|
ejabberd_commands:register_commands(?MODULE, get_commands_spec()),
|
|
|
|
{ok, [{hook, remove_user, remove_user, 50},
|
|
|
|
{hook, disco_sm_features, get_sm_features, 50},
|
|
|
|
{hook, pubsub_publish_item, pubsub_publish_item, 50},
|
2023-09-27 18:36:30 +02:00
|
|
|
{hook, pubsub_delete_item, pubsub_delete_item, 50},
|
2023-10-02 14:39:30 +02:00
|
|
|
{hook, pubsub_tree_call, pubsub_tree_call, 50},
|
2023-08-04 17:53:50 +02:00
|
|
|
{iq_handler, ejabberd_sm, ?NS_PRIVATE, process_sm_iq}]}.
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2018-12-05 22:04:40 +01:00
|
|
|
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
|
|
|
|
false ->
|
|
|
|
ejabberd_commands:unregister_commands(get_commands_spec());
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(Host, NewOpts, OldOpts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
NewMod = gen_mod:db_mod(NewOpts, ?MODULE),
|
|
|
|
OldMod = gen_mod:db_mod(OldOpts, ?MODULE),
|
2017-02-22 17:46:47 +01:00
|
|
|
if NewMod /= OldMod ->
|
|
|
|
NewMod:init(Host, NewOpts);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2018-02-11 10:54:15 +01:00
|
|
|
init_cache(NewMod, Host, NewOpts).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2018-11-23 11:33:29 +01:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[{mod_pubsub, soft}].
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(db_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:db_type(?MODULE);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(use_cache) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_size) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:pos_int(infinity);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_missed) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_life_time) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:timeout(second, infinity).
|
2018-11-23 11:33:29 +01:00
|
|
|
|
|
|
|
mod_options(Host) ->
|
|
|
|
[{db_type, ejabberd_config:default_db(Host, ?MODULE)},
|
2019-06-14 11:33:26 +02:00
|
|
|
{use_cache, ejabberd_option:use_cache(Host)},
|
|
|
|
{cache_size, ejabberd_option:cache_size(Host)},
|
|
|
|
{cache_missed, ejabberd_option:cache_missed(Host)},
|
|
|
|
{cache_life_time, ejabberd_option:cache_life_time(Host)}].
|
2018-11-23 11:33:29 +01:00
|
|
|
|
2020-01-08 10:24:51 +01:00
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("This module adds support for "
|
|
|
|
"https://xmpp.org/extensions/xep-0049.html"
|
|
|
|
"[XEP-0049: Private XML Storage]."), "",
|
|
|
|
?T("Using this method, XMPP entities can store "
|
|
|
|
"private data on the server, retrieve it "
|
|
|
|
"whenever necessary and share it between multiple "
|
|
|
|
"connected clients of the same user. The data stored "
|
|
|
|
"might be anything, as long as it is a valid XML. "
|
|
|
|
"One typical usage is storing a bookmark of all user's conferences "
|
|
|
|
"(https://xmpp.org/extensions/xep-0048.html"
|
2023-10-02 12:06:21 +02:00
|
|
|
"[XEP-0048: Bookmarks])."), "",
|
|
|
|
?T("It also implements the bookmark conversion described in "
|
|
|
|
"https://xmpp.org/extensions/xep-0402.html[XEP-0402: PEP Native Bookmarks]"
|
2024-03-07 01:09:53 +01:00
|
|
|
", see the command _`bookmarks_to_pep`_ API.")],
|
2020-01-08 10:24:51 +01:00
|
|
|
opts =>
|
|
|
|
[{db_type,
|
|
|
|
#{value => "mnesia | sql",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`default_db`_ option, but applied to this module only.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{use_cache,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`use_cache`_ option, but applied to this module only.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{cache_size,
|
|
|
|
#{value => "pos_integer() | infinity",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`cache_size`_ option, but applied to this module only.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{cache_missed,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`cache_missed`_ option, but applied to this module only.")}},
|
2020-01-08 10:24:51 +01:00
|
|
|
{cache_life_time,
|
|
|
|
#{value => "timeout()",
|
|
|
|
desc =>
|
2021-08-21 18:31:21 +02:00
|
|
|
?T("Same as top-level _`cache_life_time`_ option, but applied to this module only.")}}]}.
|
2020-01-08 10:24:51 +01:00
|
|
|
|
2018-11-23 11:33:29 +01:00
|
|
|
-spec get_sm_features({error, stanza_error()} | empty | {result, [binary()]},
|
|
|
|
jid(), jid(), binary(), binary()) ->
|
|
|
|
{error, stanza_error()} | empty | {result, [binary()]}.
|
|
|
|
get_sm_features({error, _Error} = Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc;
|
2018-11-23 12:01:06 +01:00
|
|
|
get_sm_features(Acc, _From, To, <<"">>, _Lang) ->
|
|
|
|
case gen_mod:is_loaded(To#jid.lserver, mod_pubsub) of
|
|
|
|
true ->
|
2023-09-27 18:36:30 +02:00
|
|
|
{result, [?NS_BOOKMARKS_CONVERSION_0, ?NS_PEP_BOOKMARKS_COMPAT, ?NS_PEP_BOOKMARKS_COMPAT_PEP |
|
2018-11-23 12:01:06 +01:00
|
|
|
case Acc of
|
|
|
|
{result, Features} -> Features;
|
|
|
|
empty -> []
|
|
|
|
end]};
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end;
|
2018-11-23 11:33:29 +01:00
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec process_sm_iq(iq()) -> iq().
|
|
|
|
process_sm_iq(#iq{type = Type, lang = Lang,
|
2018-11-23 11:33:29 +01:00
|
|
|
from = #jid{luser = LUser, lserver = LServer} = From,
|
2016-07-18 14:01:32 +02:00
|
|
|
to = #jid{luser = LUser, lserver = LServer},
|
2018-01-25 18:02:47 +01:00
|
|
|
sub_els = [#private{sub_els = Els0}]} = IQ) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
case filter_xmlels(Els0) of
|
|
|
|
[] ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No private data found in this query"),
|
2016-07-25 12:50:30 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
|
2016-07-18 14:01:32 +02:00
|
|
|
Data when Type == set ->
|
2018-11-23 11:33:29 +01:00
|
|
|
case set_data(From, Data) of
|
2017-05-22 09:34:57 +02:00
|
|
|
ok ->
|
|
|
|
xmpp:make_iq_result(IQ);
|
2018-11-23 11:33:29 +01:00
|
|
|
{error, #stanza_error{} = Err} ->
|
|
|
|
xmpp:make_error(IQ, Err);
|
2017-05-22 09:34:57 +02:00
|
|
|
{error, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2017-05-22 09:34:57 +02:00
|
|
|
Err = xmpp:err_internal_server_error(Txt, Lang),
|
|
|
|
xmpp:make_error(IQ, Err)
|
|
|
|
end;
|
2016-07-18 14:01:32 +02:00
|
|
|
Data when Type == get ->
|
2017-05-22 09:34:57 +02:00
|
|
|
case get_data(LUser, LServer, Data) of
|
|
|
|
{error, _} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2017-05-22 09:34:57 +02:00
|
|
|
Err = xmpp:err_internal_server_error(Txt, Lang),
|
|
|
|
xmpp:make_error(IQ, Err);
|
|
|
|
Els ->
|
2018-01-25 18:02:47 +01:00
|
|
|
xmpp:make_iq_result(IQ, #private{sub_els = Els})
|
2017-05-22 09:34:57 +02:00
|
|
|
end
|
2011-12-30 15:08:24 +01:00
|
|
|
end;
|
2016-07-18 14:01:32 +02:00
|
|
|
process_sm_iq(#iq{lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Query to another users is forbidden"),
|
2016-07-18 14:01:32 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang)).
|
|
|
|
|
|
|
|
-spec filter_xmlels([xmlel()]) -> [{binary(), xmlel()}].
|
|
|
|
filter_xmlels(Els) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun(#xmlel{} = El) ->
|
|
|
|
case fxml:get_tag_attr_s(<<"xmlns">>, El) of
|
|
|
|
<<"">> -> [];
|
|
|
|
NS -> [{NS, El}]
|
|
|
|
end
|
|
|
|
end, Els).
|
|
|
|
|
2018-11-23 11:33:29 +01:00
|
|
|
-spec set_data(jid(), [{binary(), xmlel()}]) -> ok | {error, _}.
|
|
|
|
set_data(JID, Data) ->
|
2023-09-27 18:36:30 +02:00
|
|
|
set_data(JID, Data, true, true).
|
2018-11-23 11:33:29 +01:00
|
|
|
|
2023-09-27 18:36:30 +02:00
|
|
|
-spec set_data(jid(), [{binary(), xmlel()}], boolean(), boolean()) -> ok | {error, _}.
|
|
|
|
set_data(JID, Data, PublishPepStorageBookmarks, PublishPepXmppBookmarks) ->
|
2018-11-23 11:33:29 +01:00
|
|
|
{LUser, LServer, _} = jid:tolower(JID),
|
2016-04-13 13:09:34 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2017-05-22 09:34:57 +02:00
|
|
|
case Mod:set_data(LUser, LServer, Data) of
|
|
|
|
ok ->
|
2018-11-23 11:33:29 +01:00
|
|
|
delete_cache(Mod, LUser, LServer, Data),
|
2023-09-27 18:36:30 +02:00
|
|
|
case PublishPepStorageBookmarks of
|
|
|
|
true -> publish_pep_storage_bookmarks(JID, Data);
|
|
|
|
false -> ok
|
|
|
|
end,
|
|
|
|
case PublishPepXmppBookmarks of
|
2023-10-19 19:16:49 +02:00
|
|
|
true -> publish_pep_native_bookmarks(JID, Data);
|
2018-11-23 11:33:29 +01:00
|
|
|
false -> ok
|
|
|
|
end;
|
2017-05-22 09:34:57 +02:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2011-12-30 15:08:24 +01:00
|
|
|
|
2017-05-22 09:34:57 +02:00
|
|
|
-spec get_data(binary(), binary(), [{binary(), xmlel()}]) -> [xmlel()] | {error, _}.
|
2011-12-30 15:08:24 +01:00
|
|
|
get_data(LUser, LServer, Data) ->
|
2016-04-13 13:09:34 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2017-05-22 09:34:57 +02:00
|
|
|
lists:foldr(
|
|
|
|
fun(_, {error, _} = Err) ->
|
|
|
|
Err;
|
|
|
|
({NS, El}, Els) ->
|
|
|
|
Res = case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?PRIVATE_CACHE, {LUser, LServer, NS},
|
|
|
|
fun() -> Mod:get_data(LUser, LServer, NS) end);
|
|
|
|
false ->
|
|
|
|
Mod:get_data(LUser, LServer, NS)
|
|
|
|
end,
|
|
|
|
case Res of
|
2016-07-18 14:01:32 +02:00
|
|
|
{ok, StorageEl} ->
|
2017-05-22 09:34:57 +02:00
|
|
|
[StorageEl|Els];
|
2016-07-18 14:01:32 +02:00
|
|
|
error ->
|
2017-05-22 09:34:57 +02:00
|
|
|
[El|Els];
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
2016-07-18 14:01:32 +02:00
|
|
|
end
|
2017-05-22 09:34:57 +02:00
|
|
|
end, [], Data).
|
2016-07-18 14:01:32 +02:00
|
|
|
|
2017-05-22 09:34:57 +02:00
|
|
|
-spec get_data(binary(), binary()) -> [xmlel()] | {error, _}.
|
2013-03-14 10:33:02 +01:00
|
|
|
get_data(LUser, LServer) ->
|
2016-04-13 13:09:34 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
2017-05-22 09:34:57 +02:00
|
|
|
case Mod:get_all_data(LUser, LServer) of
|
|
|
|
{ok, Els} -> Els;
|
|
|
|
error -> [];
|
|
|
|
{error, _} = Err -> Err
|
|
|
|
end.
|
2014-07-14 05:49:02 +02:00
|
|
|
|
2017-05-22 09:34:57 +02:00
|
|
|
-spec remove_user(binary(), binary()) -> ok.
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2016-04-13 13:09:34 +02:00
|
|
|
Mod = gen_mod:db_mod(Server, ?MODULE),
|
2017-05-22 09:34:57 +02:00
|
|
|
Data = case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
case Mod:get_all_data(LUser, LServer) of
|
|
|
|
{ok, Els} -> filter_xmlels(Els);
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Mod:del_data(LUser, LServer),
|
|
|
|
delete_cache(Mod, LUser, LServer, Data).
|
|
|
|
|
2018-11-23 11:33:29 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Pubsub
|
|
|
|
%%%===================================================================
|
2023-09-27 18:36:30 +02:00
|
|
|
-spec publish_pep_storage_bookmarks(jid(), [{binary(), xmlel()}]) -> ok | {error, stanza_error()}.
|
|
|
|
publish_pep_storage_bookmarks(JID, Data) ->
|
2018-11-23 11:33:29 +01:00
|
|
|
{_, LServer, _} = LBJID = jid:remove_resource(jid:tolower(JID)),
|
|
|
|
case gen_mod:is_loaded(LServer, mod_pubsub) of
|
|
|
|
true ->
|
|
|
|
case lists:keyfind(?NS_STORAGE_BOOKMARKS, 1, Data) of
|
|
|
|
false -> ok;
|
|
|
|
{_, El} ->
|
2023-10-19 19:16:49 +02:00
|
|
|
case mod_pubsub:get_items(LBJID, ?NS_STORAGE_BOOKMARKS) of
|
|
|
|
{error, #stanza_error{reason = 'item-not-found'}} ->
|
|
|
|
PubOpts = [{persist_items, true},
|
|
|
|
{access_model, whitelist}],
|
|
|
|
case mod_pubsub:publish_item(
|
|
|
|
LBJID, LServer, ?NS_STORAGE_BOOKMARKS, JID,
|
|
|
|
<<"current">>, [El], PubOpts, all) of
|
|
|
|
{result, _} -> ok;
|
|
|
|
{error, _} = Err -> Err
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
case mod_pubsub:publish_item(
|
|
|
|
LBJID, LServer, ?NS_STORAGE_BOOKMARKS, JID,
|
|
|
|
<<"current">>, [El], [], all) of
|
|
|
|
{result, _} -> ok;
|
|
|
|
{error, _} = Err -> Err
|
|
|
|
end
|
2018-11-23 11:33:29 +01:00
|
|
|
end
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2023-10-19 19:16:49 +02:00
|
|
|
-spec publish_pep_native_bookmarks(jid(), [{binary(), xmlel()}]) -> ok | {error, stanza_error()}.
|
|
|
|
publish_pep_native_bookmarks(JID, Data) ->
|
2023-09-27 18:36:30 +02:00
|
|
|
{_, LServer, _} = LBJID = jid:remove_resource(jid:tolower(JID)),
|
|
|
|
case gen_mod:is_loaded(LServer, mod_pubsub) of
|
|
|
|
true ->
|
|
|
|
case lists:keyfind(?NS_STORAGE_BOOKMARKS, 1, Data) of
|
|
|
|
{_, Bookmarks0} ->
|
2024-05-28 21:07:04 +02:00
|
|
|
Bookmarks = try xmpp:decode(Bookmarks0) of
|
2023-09-27 18:36:30 +02:00
|
|
|
#bookmark_storage{conference = C} -> C;
|
|
|
|
_ -> []
|
2024-05-28 21:07:04 +02:00
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
?WARNING_MSG("Failed to decode bookmarks of ~ts: ~ts",
|
|
|
|
[jid:encode(JID), xmpp:format_error(Why)]),
|
|
|
|
[]
|
2023-09-27 18:36:30 +02:00
|
|
|
end,
|
2023-10-19 19:16:49 +02:00
|
|
|
PubOpts = [{persist_items, true}, {access_model, whitelist}, {max_items, max}, {notify_retract,true}, {notify_delete,true}, {send_last_published_item, never}],
|
2023-09-27 18:36:30 +02:00
|
|
|
case mod_pubsub:get_items(LBJID, ?NS_PEP_BOOKMARKS) of
|
|
|
|
PepBookmarks when is_list(PepBookmarks) ->
|
|
|
|
put(mod_private_pep_update, true),
|
|
|
|
PepBookmarksMap = lists:foldl(fun pubsub_item_to_map/2, #{}, PepBookmarks),
|
2023-09-27 23:02:26 +02:00
|
|
|
{ToDelete, Ret} =
|
2023-09-27 18:36:30 +02:00
|
|
|
lists:foldl(
|
2023-09-27 23:02:26 +02:00
|
|
|
fun(#bookmark_conference{jid = BookmarkJID} = Bookmark, {Map2, Ret2}) ->
|
2023-09-27 18:36:30 +02:00
|
|
|
PB = storage_bookmark_to_xmpp_bookmark(Bookmark),
|
|
|
|
case maps:take(jid:tolower(BookmarkJID), Map2) of
|
|
|
|
{StoredBookmark, Map3} when StoredBookmark == PB ->
|
2023-09-27 23:02:26 +02:00
|
|
|
{Map3, Ret2};
|
2023-09-27 18:36:30 +02:00
|
|
|
{_, Map4} ->
|
2023-09-27 23:02:26 +02:00
|
|
|
{Map4,
|
|
|
|
err_ret(Ret2, mod_pubsub:publish_item(
|
|
|
|
LBJID, LServer, ?NS_PEP_BOOKMARKS, JID,
|
2023-10-19 19:16:49 +02:00
|
|
|
jid:encode(BookmarkJID), [xmpp:encode(PB)], [], all))};
|
2023-09-27 18:36:30 +02:00
|
|
|
_ ->
|
2023-09-27 23:02:26 +02:00
|
|
|
{Map2,
|
|
|
|
err_ret(Ret2, mod_pubsub:publish_item(
|
|
|
|
LBJID, LServer, ?NS_PEP_BOOKMARKS, JID,
|
2023-10-19 19:16:49 +02:00
|
|
|
jid:encode(BookmarkJID), [xmpp:encode(PB)], [], all))}
|
2023-09-27 18:36:30 +02:00
|
|
|
end
|
2023-09-27 23:02:26 +02:00
|
|
|
end, {PepBookmarksMap, ok}, Bookmarks),
|
|
|
|
Ret4 =
|
2023-09-27 18:36:30 +02:00
|
|
|
maps:fold(
|
2023-09-27 23:02:26 +02:00
|
|
|
fun(DeleteJid, _, Ret3) ->
|
|
|
|
err_ret(Ret3, mod_pubsub:delete_item(LBJID, ?NS_PEP_BOOKMARKS,
|
|
|
|
JID, jid:encode(DeleteJid)))
|
|
|
|
end, Ret, ToDelete),
|
2023-09-27 18:36:30 +02:00
|
|
|
erase(mod_private_pep_update),
|
2023-09-27 23:02:26 +02:00
|
|
|
Ret4;
|
2023-09-27 18:36:30 +02:00
|
|
|
{error, #stanza_error{reason = 'item-not-found'}} ->
|
|
|
|
put(mod_private_pep_update, true),
|
2023-09-27 23:02:26 +02:00
|
|
|
Ret7 =
|
|
|
|
lists:foldl(
|
|
|
|
fun(#bookmark_conference{jid = BookmarkJID} = Bookmark, Ret5) ->
|
2023-09-27 18:36:30 +02:00
|
|
|
PB = storage_bookmark_to_xmpp_bookmark(Bookmark),
|
2023-09-27 23:02:26 +02:00
|
|
|
err_ret(Ret5, mod_pubsub:publish_item(
|
2023-09-27 18:36:30 +02:00
|
|
|
LBJID, LServer, ?NS_PEP_BOOKMARKS, JID,
|
2023-09-27 23:02:26 +02:00
|
|
|
jid:encode(BookmarkJID), [xmpp:encode(PB)], PubOpts, all))
|
|
|
|
end, ok, Bookmarks),
|
2023-09-27 18:36:30 +02:00
|
|
|
erase(mod_private_pep_update),
|
2023-09-27 23:02:26 +02:00
|
|
|
Ret7;
|
2023-09-27 18:36:30 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2023-09-27 23:02:26 +02:00
|
|
|
err_ret({error, _} = E, _) ->
|
|
|
|
E;
|
2023-10-02 08:32:24 +02:00
|
|
|
err_ret(ok, {error, _} = E) ->
|
|
|
|
E;
|
|
|
|
err_ret(_, _) ->
|
|
|
|
ok.
|
2023-09-27 23:02:26 +02:00
|
|
|
|
2018-11-23 11:33:29 +01:00
|
|
|
-spec pubsub_publish_item(binary(), binary(), jid(), jid(),
|
|
|
|
binary(), [xmlel()]) -> any().
|
|
|
|
pubsub_publish_item(LServer, ?NS_STORAGE_BOOKMARKS,
|
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
|
|
|
#jid{luser = LUser, lserver = LServer},
|
|
|
|
_ItemId, [Payload|_]) ->
|
2023-09-27 18:36:30 +02:00
|
|
|
set_data(From, [{?NS_STORAGE_BOOKMARKS, Payload}], false, true);
|
|
|
|
pubsub_publish_item(LServer, ?NS_PEP_BOOKMARKS,
|
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
|
|
|
#jid{luser = LUser, lserver = LServer},
|
|
|
|
_ItemId, _Payload) ->
|
|
|
|
NotRecursion = get(mod_private_pep_update) == undefined,
|
|
|
|
case mod_pubsub:get_items({LUser, LServer, <<>>}, ?NS_PEP_BOOKMARKS) of
|
|
|
|
Bookmarks when is_list(Bookmarks), NotRecursion ->
|
|
|
|
Bookmarks2 = lists:filtermap(fun pubsub_item_to_storage_bookmark/1, Bookmarks),
|
|
|
|
Payload = xmpp:encode(#bookmark_storage{conference = Bookmarks2}),
|
|
|
|
set_data(From, [{?NS_STORAGE_BOOKMARKS, Payload}], true, false);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2018-11-23 11:33:29 +01:00
|
|
|
pubsub_publish_item(_, _, _, _, _, _) ->
|
|
|
|
ok.
|
|
|
|
|
2023-09-27 18:36:30 +02:00
|
|
|
-spec pubsub_delete_item(binary(), binary(), jid(), jid(), binary()) -> any().
|
|
|
|
pubsub_delete_item(LServer, ?NS_PEP_BOOKMARKS,
|
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
|
|
|
#jid{luser = LUser, lserver = LServer},
|
|
|
|
_ItemId) ->
|
|
|
|
NotRecursion = get(mod_private_pep_update) == undefined,
|
|
|
|
case mod_pubsub:get_items({LUser, LServer, <<>>}, ?NS_PEP_BOOKMARKS) of
|
|
|
|
Bookmarks when is_list(Bookmarks), NotRecursion ->
|
|
|
|
Bookmarks2 = lists:filtermap(fun pubsub_item_to_storage_bookmark/1, Bookmarks),
|
|
|
|
Payload = xmpp:encode(#bookmark_storage{conference = Bookmarks2}),
|
|
|
|
set_data(From, [{?NS_STORAGE_BOOKMARKS, Payload}], true, false);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
pubsub_delete_item(_, _, _, _, _) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec pubsub_item_to_storage_bookmark(#pubsub_item{}) -> {true, bookmark_conference()} | false.
|
|
|
|
pubsub_item_to_storage_bookmark(#pubsub_item{itemid = {Id, _}, payload = [#xmlel{} = B | _]}) ->
|
|
|
|
case xmpp:decode(B) of
|
|
|
|
#pep_bookmarks_conference{name = Name, autojoin = AutoJoin, nick = Nick,
|
|
|
|
password = Password} ->
|
|
|
|
try jid:decode(Id) of
|
|
|
|
#jid{} = Jid ->
|
|
|
|
{true, #bookmark_conference{jid = Jid, name = Name, autojoin = AutoJoin, nick = Nick,
|
|
|
|
password = Password}}
|
|
|
|
catch _:_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
pubsub_item_to_storage_bookmark(_) ->
|
|
|
|
false.
|
|
|
|
|
2023-10-02 14:39:30 +02:00
|
|
|
-spec pubsub_tree_call(Res :: any(), _Tree::any(), atom(), any()) -> any().
|
|
|
|
pubsub_tree_call({error, #stanza_error{reason = 'item-not-found'}} = Res, Tree, get_node,
|
|
|
|
[{User, Server, _}, ?NS_PEP_BOOKMARKS] = Args) ->
|
|
|
|
case get(mod_private_in_pubsub_tree_call) of
|
|
|
|
undefined ->
|
|
|
|
put(mod_private_in_pubsub_tree_call, true),
|
|
|
|
bookmarks_to_pep(User, Server),
|
|
|
|
Res2 = apply(Tree, get_node, Args),
|
|
|
|
erase(mod_private_in_pubsub_tree_call),
|
|
|
|
Res2;
|
|
|
|
_ ->
|
|
|
|
Res
|
|
|
|
end;
|
|
|
|
pubsub_tree_call(Res, _Tree, _Function, _Args) ->
|
|
|
|
Res.
|
|
|
|
|
2023-09-27 18:36:30 +02:00
|
|
|
-spec storage_bookmark_to_xmpp_bookmark(bookmark_conference()) -> pep_bookmarks_conference().
|
|
|
|
storage_bookmark_to_xmpp_bookmark(#bookmark_conference{name = Name, autojoin = AutoJoin, nick = Nick,
|
|
|
|
password = Password}) ->
|
|
|
|
#pep_bookmarks_conference{name = Name, autojoin = AutoJoin, nick = Nick,
|
|
|
|
password = Password}.
|
|
|
|
|
|
|
|
-spec pubsub_item_to_map(#pubsub_item{}, map()) -> map().
|
|
|
|
pubsub_item_to_map(#pubsub_item{itemid = {Id, _}, payload = [#xmlel{} = B | _]}, Map) ->
|
|
|
|
case xmpp:decode(B) of
|
|
|
|
#pep_bookmarks_conference{} = B2 ->
|
|
|
|
try jid:decode(Id) of
|
|
|
|
#jid{} = Jid ->
|
|
|
|
maps:put(jid:tolower(Jid), B2#pep_bookmarks_conference{extensions = undefined}, Map)
|
|
|
|
catch _:_ ->
|
|
|
|
Map
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Map
|
|
|
|
end;
|
|
|
|
pubsub_item_to_map(_, Map) ->
|
|
|
|
Map.
|
|
|
|
|
2018-12-05 22:04:40 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Commands
|
|
|
|
%%%===================================================================
|
|
|
|
-spec get_commands_spec() -> [ejabberd_commands()].
|
|
|
|
get_commands_spec() ->
|
|
|
|
[#ejabberd_commands{name = bookmarks_to_pep, tags = [private],
|
|
|
|
desc = "Export private XML storage bookmarks to PEP",
|
|
|
|
module = ?MODULE, function = bookmarks_to_pep,
|
2019-06-19 09:26:28 +02:00
|
|
|
args = [{user, binary}, {host, binary}],
|
|
|
|
args_rename = [{server, host}],
|
2018-12-05 22:04:40 +01:00
|
|
|
args_desc = ["Username", "Server"],
|
|
|
|
args_example = [<<"bob">>, <<"example.com">>],
|
|
|
|
result = {res, restuple},
|
|
|
|
result_desc = "Result tuple",
|
|
|
|
result_example = {ok, <<"Bookmarks exported">>}}].
|
|
|
|
|
|
|
|
-spec bookmarks_to_pep(binary(), binary())
|
|
|
|
-> {ok, binary()} | {error, binary()}.
|
|
|
|
bookmarks_to_pep(User, Server) ->
|
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Res = case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?PRIVATE_CACHE, {LUser, LServer, ?NS_STORAGE_BOOKMARKS},
|
|
|
|
fun() ->
|
|
|
|
Mod:get_data(LUser, LServer, ?NS_STORAGE_BOOKMARKS)
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
Mod:get_data(LUser, LServer, ?NS_STORAGE_BOOKMARKS)
|
|
|
|
end,
|
|
|
|
case Res of
|
|
|
|
{ok, El} ->
|
|
|
|
Data = [{?NS_STORAGE_BOOKMARKS, El}],
|
2023-09-27 18:36:30 +02:00
|
|
|
case publish_pep_storage_bookmarks(jid:make(User, Server), Data) of
|
2018-12-05 22:04:40 +01:00
|
|
|
ok ->
|
2023-10-19 19:16:49 +02:00
|
|
|
case publish_pep_native_bookmarks(jid:make(User, Server), Data) of
|
2023-09-27 18:36:30 +02:00
|
|
|
ok ->
|
|
|
|
{ok, <<"Bookmarks exported to PEP node">>};
|
|
|
|
{error, Err} ->
|
|
|
|
{error, xmpp:format_stanza_error(Err)}
|
|
|
|
end;
|
2018-12-05 22:04:40 +01:00
|
|
|
{error, Err} ->
|
|
|
|
{error, xmpp:format_stanza_error(Err)}
|
2023-09-27 18:36:30 +02:00
|
|
|
|
2018-12-05 22:04:40 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
{error, <<"Cannot retrieve bookmarks from private XML storage">>}
|
|
|
|
end.
|
|
|
|
|
2018-11-23 11:33:29 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Cache
|
|
|
|
%%%===================================================================
|
2017-05-22 09:34:57 +02:00
|
|
|
-spec delete_cache(module(), binary(), binary(), [{binary(), xmlel()}]) -> ok.
|
|
|
|
delete_cache(Mod, LUser, LServer, Data) ->
|
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
Nodes = cache_nodes(Mod, LServer),
|
|
|
|
lists:foreach(
|
|
|
|
fun({NS, _}) ->
|
|
|
|
ets_cache:delete(?PRIVATE_CACHE,
|
|
|
|
{LUser, LServer, NS},
|
|
|
|
Nodes)
|
|
|
|
end, Data);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec init_cache(module(), binary(), gen_mod:opts()) -> ok.
|
|
|
|
init_cache(Mod, Host, Opts) ->
|
|
|
|
case use_cache(Mod, Host) of
|
|
|
|
true ->
|
2018-01-23 08:54:52 +01:00
|
|
|
CacheOpts = cache_opts(Opts),
|
2017-05-22 09:34:57 +02:00
|
|
|
ets_cache:new(?PRIVATE_CACHE, CacheOpts);
|
|
|
|
false ->
|
|
|
|
ets_cache:delete(?PRIVATE_CACHE)
|
|
|
|
end.
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-spec cache_opts(gen_mod:opts()) -> [proplists:property()].
|
|
|
|
cache_opts(Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = mod_private_opt:cache_size(Opts),
|
|
|
|
CacheMissed = mod_private_opt:cache_missed(Opts),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = mod_private_opt:cache_life_time(Opts),
|
2017-05-22 09:34:57 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
|
|
|
-spec use_cache(module(), binary()) -> boolean().
|
|
|
|
use_cache(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 1) of
|
|
|
|
true -> Mod:use_cache(Host);
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> mod_private_opt:use_cache(Host)
|
2017-05-22 09:34:57 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_nodes(module(), binary()) -> [node()].
|
|
|
|
cache_nodes(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 1) of
|
|
|
|
true -> Mod:cache_nodes(Host);
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
|
|
|
end.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2018-11-23 11:33:29 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Import/Export
|
|
|
|
%%%===================================================================
|
2016-11-22 14:48:01 +01:00
|
|
|
import_info() ->
|
|
|
|
[{<<"private_storage">>, 4}].
|
|
|
|
|
|
|
|
import_start(LServer, DBType) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:init(LServer, []).
|
|
|
|
|
2016-04-13 13:09:34 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, {sql, _}, DBType, Tab, L) ->
|
2016-04-13 13:09:34 +02:00
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
2016-11-22 14:48:01 +01:00
|
|
|
Mod:import(LServer, Tab, L).
|