2015-12-15 16:11:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2016-04-20 11:27:32 +02:00
|
|
|
%%% File : nodetree_tree_sql.erl
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% Author : Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% Purpose : Standard node tree plugin with ODBC backend
|
|
|
|
%%% Created : 1 Dec 2007 by Christophe Romain <christophe.romain@process-one.net>
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2009-08-07 10:26:47 +02:00
|
|
|
%%%
|
2015-12-15 16:11:29 +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.
|
2009-08-07 10:26:47 +02:00
|
|
|
%%%
|
2015-12-15 16:11:29 +01:00
|
|
|
%%% 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.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
%%% @doc The module <strong>{@module}</strong> is the default PubSub node tree plugin.
|
|
|
|
%%% <p>It is used as a default for all unknown PubSub node type. It can serve
|
|
|
|
%%% as a developer basis and reference to build its own custom pubsub node tree
|
|
|
|
%%% types.</p>
|
|
|
|
%%% <p>PubSub node tree plugins are using the {@link gen_nodetree} behaviour.</p>
|
|
|
|
%%% <p><strong>The API isn't stabilized yet</strong>. The pubsub plugin
|
|
|
|
%%% development is still a work in progress. However, the system is already
|
|
|
|
%%% useable and useful as is. Please, send us comments, feedback and
|
|
|
|
%%% improvements.</p>
|
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
-module(nodetree_tree_sql).
|
2015-04-08 17:12:05 +02:00
|
|
|
-behaviour(gen_pubsub_nodetree).
|
2009-08-07 10:26:47 +02:00
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
2016-05-24 17:44:07 +02:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
-include("pubsub.hrl").
|
2016-09-08 14:49:27 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-05-24 17:44:07 +02:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/3, terminate/2, options/0, set_node/1,
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node/3, get_node/2, get_node/1, get_nodes/2,
|
|
|
|
get_nodes/1, get_parentnodes/3, get_parentnodes_tree/3,
|
|
|
|
get_subnodes/3, get_subnodes_tree/3, create_node/6,
|
|
|
|
delete_node/2]).
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
-export([raw_to_node/2]).
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
init(_Host, _ServerHost, _Opts) ->
|
|
|
|
ok.
|
2009-08-07 10:26:47 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
terminate(_Host, _ServerHost) ->
|
|
|
|
ok.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
options() ->
|
2016-04-20 11:27:32 +02:00
|
|
|
[{sql, true} | nodetree_tree:options()].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
set_node(Record) when is_record(Record, pubsub_node) ->
|
|
|
|
{Host, Node} = Record#pubsub_node.nodeid,
|
|
|
|
Parent = case Record#pubsub_node.parents of
|
|
|
|
[] -> <<>>;
|
|
|
|
[First | _] -> First
|
|
|
|
end,
|
|
|
|
Type = Record#pubsub_node.type,
|
2016-04-20 11:27:32 +02:00
|
|
|
H = node_flat_sql:encode_host(Host),
|
2015-04-08 17:12:05 +02:00
|
|
|
Nidx = case nodeidx(Host, Node) of
|
|
|
|
{result, OldNidx} ->
|
|
|
|
catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("delete from pubsub_node_option "
|
|
|
|
"where nodeid=%(OldNidx)d")),
|
2015-04-08 17:12:05 +02:00
|
|
|
catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("update pubsub_node set"
|
|
|
|
" host=%(H)s, node=%(Node)s,"
|
|
|
|
" parent=%(Parent)s, plugin=%(Type)s "
|
|
|
|
"where nodeid=%(OldNidx)d")),
|
2015-04-08 17:12:05 +02:00
|
|
|
OldNidx;
|
|
|
|
_ ->
|
|
|
|
catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("insert into pubsub_node(host, node, parent, plugin) "
|
|
|
|
"values(%(H)s, %(Node)s, %(Parent)s, %(Type)s)")),
|
2015-04-08 17:12:05 +02:00
|
|
|
case nodeidx(Host, Node) of
|
|
|
|
{result, NewNidx} -> NewNidx;
|
|
|
|
_ -> none % this should not happen
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case Nidx of
|
|
|
|
none ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Node index not found">>,
|
2018-06-14 13:00:47 +02:00
|
|
|
{error, xmpp:err_internal_server_error(Txt, ejabberd_config:get_mylang())};
|
2015-04-08 17:12:05 +02:00
|
|
|
_ ->
|
|
|
|
lists:foreach(fun ({Key, Value}) ->
|
|
|
|
SKey = iolist_to_binary(atom_to_list(Key)),
|
2017-04-11 12:13:58 +02:00
|
|
|
SValue = misc:term_to_expr(Value),
|
2015-04-08 17:12:05 +02:00
|
|
|
catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("insert into pubsub_node_option(nodeid, name, val) "
|
|
|
|
"values (%(Nidx)d, %(SKey)s, %(SValue)s)"))
|
2015-04-08 17:12:05 +02:00
|
|
|
end,
|
|
|
|
Record#pubsub_node.options),
|
|
|
|
{result, Nidx}
|
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node(Host, Node, _From) ->
|
|
|
|
get_node(Host, Node).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
get_node(Host, Node) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
H = node_flat_sql:encode_host(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d from pubsub_node "
|
|
|
|
"where host=%(H)s and node=%(Node)s"))
|
2015-04-08 17:12:05 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, [RItem]} ->
|
2015-04-08 17:12:05 +02:00
|
|
|
raw_to_node(Host, RItem);
|
|
|
|
{'EXIT', _Reason} ->
|
2018-06-14 13:00:47 +02:00
|
|
|
{error, xmpp:err_internal_server_error(<<"Database failure">>, ejabberd_config:get_mylang())};
|
2015-04-08 17:12:05 +02:00
|
|
|
_ ->
|
2018-06-14 13:00:47 +02:00
|
|
|
{error, xmpp:err_item_not_found(<<"Node not found">>, ejabberd_config:get_mylang())}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node(Nidx) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(host)s, @(node)s, @(parent)s, @(plugin)s from pubsub_node "
|
|
|
|
"where nodeid=%(Nidx)d"))
|
2015-04-08 17:12:05 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, [{Host, Node, Parent, Type}]} ->
|
|
|
|
raw_to_node(Host, {Node, Parent, Type, Nidx});
|
2015-04-08 17:12:05 +02:00
|
|
|
{'EXIT', _Reason} ->
|
2018-06-14 13:00:47 +02:00
|
|
|
{error, xmpp:err_internal_server_error(<<"Database failure">>, ejabberd_config:get_mylang())};
|
2015-04-08 17:12:05 +02:00
|
|
|
_ ->
|
2018-06-14 13:00:47 +02:00
|
|
|
{error, xmpp:err_item_not_found(<<"Node not found">>, ejabberd_config:get_mylang())}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_nodes(Host, _From) ->
|
|
|
|
get_nodes(Host).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
get_nodes(Host) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
H = node_flat_sql:encode_host(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d from pubsub_node "
|
|
|
|
"where host=%(H)s"))
|
2015-04-08 17:12:05 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, RItems} ->
|
2015-04-08 17:12:05 +02:00
|
|
|
[raw_to_node(Host, Item) || Item <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2017-11-13 18:13:31 +01:00
|
|
|
get_parentnodes(Host, Node, _From) ->
|
|
|
|
case get_node(Host, Node) of
|
|
|
|
Record when is_record(Record, pubsub_node) ->
|
|
|
|
Record#pubsub_node.parents;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_parentnodes_tree(Host, Node, _From) ->
|
|
|
|
get_parentnodes_tree(Host, Node, 0, []).
|
|
|
|
get_parentnodes_tree(Host, Node, Level, Acc) ->
|
|
|
|
case get_node(Host, Node) of
|
|
|
|
Record when is_record(Record, pubsub_node) ->
|
|
|
|
Tree = [{Level, [Record]}|Acc],
|
|
|
|
case Record#pubsub_node.parents of
|
|
|
|
[Parent] -> get_parentnodes_tree(Host, Parent, Level+1, Tree);
|
|
|
|
_ -> Tree
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Acc
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-17 19:16:43 +02:00
|
|
|
get_subnodes(Host, Node, _From) ->
|
|
|
|
get_subnodes(Host, Node).
|
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
get_subnodes(Host, Node) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
H = node_flat_sql:encode_host(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d from pubsub_node "
|
|
|
|
"where host=%(H)s and parent=%(Node)s"))
|
2015-04-08 17:12:05 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, RItems} ->
|
2015-04-08 17:12:05 +02:00
|
|
|
[raw_to_node(Host, Item) || Item <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2009-08-17 19:16:43 +02:00
|
|
|
get_subnodes_tree(Host, Node, _From) ->
|
|
|
|
get_subnodes_tree(Host, Node).
|
|
|
|
|
2009-08-07 10:26:47 +02:00
|
|
|
get_subnodes_tree(Host, Node) ->
|
2016-07-05 15:43:59 +02:00
|
|
|
case get_node(Host, Node) of
|
|
|
|
{error, _} ->
|
|
|
|
[];
|
|
|
|
Rec ->
|
2017-11-16 11:38:04 +01:00
|
|
|
Type = Rec#pubsub_node.type,
|
2016-07-05 15:43:59 +02:00
|
|
|
H = node_flat_sql:encode_host(Host),
|
|
|
|
N = <<(ejabberd_sql:escape_like_arg_circumflex(Node))/binary, "/%">>,
|
|
|
|
Sub = case catch
|
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d from pubsub_node "
|
|
|
|
"where host=%(H)s and plugin=%(Type)s and"
|
|
|
|
" (parent=%(Node)s or parent like %(N)s escape '^')"))
|
2016-07-05 15:43:59 +02:00
|
|
|
of
|
|
|
|
{selected, RItems} ->
|
|
|
|
[raw_to_node(Host, Item) || Item <- RItems];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
[Rec|Sub]
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2009-10-20 17:03:07 +02:00
|
|
|
create_node(Host, Node, Type, Owner, Options, Parents) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
BJID = jid:tolower(jid:remove_resource(Owner)),
|
2015-04-08 17:12:05 +02:00
|
|
|
case nodeidx(Host, Node) of
|
2016-03-31 10:00:29 +02:00
|
|
|
{error, not_found} ->
|
2015-04-08 17:12:05 +02:00
|
|
|
ParentExists = case Host of
|
|
|
|
{_U, _S, _R} ->
|
|
|
|
%% This is special case for PEP handling
|
|
|
|
%% PEP does not uses hierarchy
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
case Parents of
|
|
|
|
[] ->
|
|
|
|
true;
|
|
|
|
[Parent | _] ->
|
|
|
|
case nodeidx(Host, Parent) of
|
|
|
|
{result, PNode} ->
|
|
|
|
case nodeowners(PNode) of
|
|
|
|
[{<<>>, Host, <<>>}] -> true;
|
|
|
|
Owners -> lists:member(BJID, Owners)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case ParentExists of
|
|
|
|
true ->
|
|
|
|
case set_node(#pubsub_node{nodeid = {Host, Node},
|
|
|
|
parents = Parents, type = Type,
|
|
|
|
options = Options})
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
2015-04-08 17:12:05 +02:00
|
|
|
{result, Nidx} -> {ok, Nidx};
|
|
|
|
Other -> Other
|
|
|
|
end;
|
|
|
|
false ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()}
|
2015-04-08 17:12:05 +02:00
|
|
|
end;
|
|
|
|
{result, _} ->
|
2018-06-14 13:00:47 +02:00
|
|
|
{error, xmpp:err_conflict(<<"Node already exists">>, ejabberd_config:get_mylang())};
|
2016-03-31 10:00:29 +02:00
|
|
|
{error, db_fail} ->
|
2018-06-14 13:00:47 +02:00
|
|
|
{error, xmpp:err_internal_server_error(<<"Database failure">>, ejabberd_config:get_mylang())}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
delete_node(Host, Node) ->
|
2017-11-16 11:38:04 +01:00
|
|
|
lists:map(
|
|
|
|
fun(Rec) ->
|
|
|
|
Nidx = Rec#pubsub_node.id,
|
|
|
|
catch ejabberd_sql:sql_query_t(
|
|
|
|
?SQL("delete from pubsub_node where nodeid=%(Nidx)d")),
|
|
|
|
Rec
|
|
|
|
end, get_subnodes_tree(Host, Node)).
|
2009-08-07 10:26:47 +02:00
|
|
|
|
|
|
|
%% helpers
|
2015-04-08 17:12:05 +02:00
|
|
|
raw_to_node(Host, [Node, Parent, Type, Nidx]) ->
|
2016-05-24 17:44:07 +02:00
|
|
|
raw_to_node(Host, {Node, Parent, Type, binary_to_integer(Nidx)});
|
|
|
|
raw_to_node(Host, {Node, Parent, Type, Nidx}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Options = case catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(name)s, @(val)s from pubsub_node_option "
|
|
|
|
"where nodeid=%(Nidx)d"))
|
2015-04-08 17:12:05 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, ROptions} ->
|
|
|
|
DbOpts = lists:map(fun ({Key, Value}) ->
|
2017-04-11 12:13:58 +02:00
|
|
|
RKey = misc:binary_to_atom(Key),
|
2015-04-08 17:12:05 +02:00
|
|
|
Tokens = element(2, erl_scan:string(binary_to_list(<<Value/binary, ".">>))),
|
|
|
|
RValue = element(2, erl_parse:parse_term(Tokens)),
|
|
|
|
{RKey, RValue}
|
|
|
|
end,
|
|
|
|
ROptions),
|
2017-04-11 12:13:58 +02:00
|
|
|
Module = misc:binary_to_atom(<<"node_", Type/binary, "_sql">>),
|
2015-04-08 17:12:05 +02:00
|
|
|
StdOpts = Module:options(),
|
|
|
|
lists:foldl(fun ({Key, Value}, Acc) ->
|
2017-10-25 15:40:42 +02:00
|
|
|
lists:keystore(Key, 1, Acc, {Key, Value})
|
2015-04-08 17:12:05 +02:00
|
|
|
end,
|
|
|
|
StdOpts, DbOpts);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2013-08-27 13:59:29 +02:00
|
|
|
Parents = case Parent of
|
2015-04-08 17:12:05 +02:00
|
|
|
<<>> -> [];
|
|
|
|
_ -> [Parent]
|
|
|
|
end,
|
2017-11-16 11:38:04 +01:00
|
|
|
#pubsub_node{nodeid = {Host, Node}, id = Nidx,
|
|
|
|
parents = Parents, type = Type, options = Options}.
|
2015-04-08 17:12:05 +02:00
|
|
|
|
|
|
|
nodeidx(Host, Node) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
H = node_flat_sql:encode_host(Host),
|
2013-03-14 10:33:02 +01:00
|
|
|
case catch
|
2016-05-24 17:44:07 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2017-11-16 11:38:04 +01:00
|
|
|
?SQL("select @(nodeid)d from pubsub_node "
|
|
|
|
"where host=%(H)s and node=%(Node)s"))
|
2015-04-08 17:12:05 +02:00
|
|
|
of
|
2016-05-24 17:44:07 +02:00
|
|
|
{selected, [{Nidx}]} ->
|
2015-04-08 17:12:05 +02:00
|
|
|
{result, Nidx};
|
|
|
|
{'EXIT', _Reason} ->
|
2016-03-31 10:00:29 +02:00
|
|
|
{error, db_fail};
|
2015-04-08 17:12:05 +02:00
|
|
|
_ ->
|
2016-03-31 10:00:29 +02:00
|
|
|
{error, not_found}
|
2009-08-07 10:26:47 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
nodeowners(Nidx) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
{result, Res} = node_flat_sql:get_node_affiliations(Nidx),
|
2015-04-08 17:12:05 +02:00
|
|
|
[LJID || {LJID, Aff} <- Res, Aff =:= owner].
|