25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00
xmpp.chapril.org-ejabberd/src/mod_private_sql.erl

161 lines
5.0 KiB
Erlang
Raw Normal View History

%%%-------------------------------------------------------------------
2016-12-27 10:44:07 +01:00
%%% File : mod_private_sql.erl
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
2016-12-27 10:44:07 +01:00
%%%
%%%
2024-01-22 16:40:01 +01:00
%%% ejabberd, Copyright (C) 2002-2024 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.
%%%
%%%----------------------------------------------------------------------
-module(mod_private_sql).
-behaviour(mod_private).
%% API
2017-05-22 09:34:57 +02:00
-export([init/2, set_data/3, get_data/3, get_all_data/2, del_data/2,
2016-11-22 14:48:01 +01:00
import/3, export/1]).
-export([sql_schemas/0]).
-include_lib("xmpp/include/xmpp.hrl").
-include("mod_private.hrl").
2017-05-23 11:25:13 +02:00
-include("ejabberd_sql_pt.hrl").
2017-05-22 09:34:57 +02:00
-include("logger.hrl").
%%%===================================================================
%%% API
%%%===================================================================
init(Host, _Opts) ->
ejabberd_sql_schema:update_schema(Host, ?MODULE, sql_schemas()),
ok.
sql_schemas() ->
[#sql_schema{
version = 1,
tables =
[#sql_table{
name = <<"private_storage">>,
columns =
[#sql_column{name = <<"username">>, type = text},
#sql_column{name = <<"server_host">>, type = text},
#sql_column{name = <<"namespace">>, type = text},
#sql_column{name = <<"data">>, type = text},
#sql_column{name = <<"created_at">>, type = timestamp,
default = true}],
indices = [#sql_index{
columns = [<<"server_host">>, <<"username">>,
<<"namespace">>],
unique = true}]}]}].
set_data(LUser, LServer, Data) ->
F = fun() ->
lists:foreach(
fun({XMLNS, El}) ->
SData = fxml:element_to_binary(El),
2017-05-23 11:25:13 +02:00
?SQL_UPSERT_T(
"private_storage",
["!username=%(LUser)s",
"!server_host=%(LServer)s",
2017-05-23 11:25:13 +02:00
"!namespace=%(XMLNS)s",
"data=%(SData)s"])
end, Data)
end,
2017-05-22 09:34:57 +02:00
case ejabberd_sql:sql_transaction(LServer, F) of
{atomic, ok} ->
ok;
_ ->
{error, db_failure}
end.
get_data(LUser, LServer, XMLNS) ->
2017-05-23 11:25:13 +02:00
case ejabberd_sql:sql_query(
LServer,
?SQL("select @(data)s from private_storage"
" where username=%(LUser)s and %(LServer)H"
" and namespace=%(XMLNS)s")) of
{selected, [{SData}]} ->
2017-05-22 09:34:57 +02:00
parse_element(LUser, LServer, SData);
{selected, []} ->
error;
_ ->
2017-05-22 09:34:57 +02:00
{error, db_failure}
end.
get_all_data(LUser, LServer) ->
2017-05-23 11:25:13 +02:00
case ejabberd_sql:sql_query(
LServer,
?SQL("select @(namespace)s, @(data)s from private_storage"
" where username=%(LUser)s and %(LServer)H")) of
2017-05-22 09:34:57 +02:00
{selected, []} ->
error;
{selected, Res} ->
2017-05-22 09:34:57 +02:00
{ok, lists:flatmap(
fun({_, SData}) ->
case parse_element(LUser, LServer, SData) of
{ok, El} -> [El];
error -> []
end
end, Res)};
_ ->
2017-05-22 09:34:57 +02:00
{error, db_failure}
end.
2017-05-22 09:34:57 +02:00
del_data(LUser, LServer) ->
2017-05-23 11:25:13 +02:00
case ejabberd_sql:sql_query(
LServer,
?SQL("delete from private_storage"
" where username=%(LUser)s and %(LServer)H")) of
2017-05-22 09:34:57 +02:00
{updated, _} ->
ok;
_ ->
{error, db_failure}
end.
export(_Server) ->
[{private_storage,
fun(Host, #private_storage{usns = {LUser, LServer, XMLNS},
xml = Data})
when LServer == Host ->
2016-05-04 20:01:05 +02:00
SData = fxml:element_to_binary(Data),
2017-05-23 11:25:13 +02:00
[?SQL("delete from private_storage where"
" username=%(LUser)s and %(LServer)H and namespace=%(XMLNS)s;"),
?SQL_INSERT(
"private_storage",
["username=%(LUser)s",
"server_host=%(LServer)s",
"namespace=%(XMLNS)s",
"data=%(SData)s"])];
(_Host, _R) ->
[]
end}].
2016-11-22 14:48:01 +01:00
import(_, _, _) ->
ok.
%%%===================================================================
%%% Internal functions
%%%===================================================================
2017-05-22 09:34:57 +02:00
parse_element(LUser, LServer, XML) ->
case fxml_stream:parse_element(XML) of
El when is_record(El, xmlel) ->
{ok, El};
_ ->
2019-06-24 19:32:34 +02:00
?ERROR_MSG("Malformed XML element in SQL table "
"'private_storage' for user ~ts@~ts: ~ts",
2017-05-22 09:34:57 +02:00
[LUser, LServer, XML]),
error
end.