2015-12-15 16:11:29 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : nodetree_tree.erl
|
|
|
|
%%% Author : Christophe Romain <christophe.romain@process-one.net>
|
|
|
|
%%% Purpose : Standard node tree plugin
|
|
|
|
%%% Created : 1 Dec 2007 by Christophe Romain <christophe.romain@process-one.net>
|
2013-03-14 10:33:02 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 ProcessOne
|
2007-12-01 06:16:30 +01: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.
|
2007-12-24 14:57:53 +01: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.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
2007-12-01 06:16:30 +01: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>
|
|
|
|
|
2009-05-29 00:30:43 +02:00
|
|
|
-module(nodetree_tree).
|
2015-04-08 17:12:05 +02:00
|
|
|
-behaviour(gen_pubsub_nodetree).
|
2007-12-01 06:16:30 +01:00
|
|
|
-author('christophe.romain@process-one.net').
|
|
|
|
|
2009-06-15 15:45:40 +02:00
|
|
|
-include_lib("stdlib/include/qlc.hrl").
|
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
-include("pubsub.hrl").
|
2016-09-08 14:49:27 +02:00
|
|
|
-include("xmpp.hrl").
|
2007-12-01 06:16:30 +01: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]).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2010-10-21 11:14:24 +02:00
|
|
|
init(_Host, _ServerHost, _Options) ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, pubsub_node,
|
2015-04-08 17:12:05 +02:00
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, pubsub_node)}]),
|
2009-05-07 02:09:02 +02:00
|
|
|
mnesia:add_table_index(pubsub_node, id),
|
2007-12-01 06:16:30 +01:00
|
|
|
NodesFields = record_info(fields, pubsub_node),
|
|
|
|
case mnesia:table_info(pubsub_node, attributes) of
|
2015-04-08 17:12:05 +02:00
|
|
|
NodesFields -> ok;
|
|
|
|
_ -> ok
|
2007-12-01 06:16:30 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
%% mnesia:transform_table(pubsub_state, ignore, StatesFields)
|
2007-12-01 06:16:30 +01:00
|
|
|
ok.
|
|
|
|
|
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() ->
|
|
|
|
[{virtual_tree, false}].
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2010-10-21 11:14:24 +02:00
|
|
|
set_node(Node) when is_record(Node, pubsub_node) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia:write(Node).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node(Host, Node, _From) ->
|
|
|
|
get_node(Host, Node).
|
2009-08-17 19:16:43 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node(Host, Node) ->
|
2016-03-31 10:00:29 +02:00
|
|
|
case mnesia:read({pubsub_node, {Host, Node}}) of
|
2015-04-08 17:12:05 +02:00
|
|
|
[Record] when is_record(Record, pubsub_node) -> Record;
|
2016-09-08 14:49:27 +02:00
|
|
|
_ -> {error, xmpp:err_item_not_found(<<"Node not found">>, ?MYLANG)}
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
2010-10-21 11:14:24 +02:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_node(Nidx) ->
|
2016-03-31 10:00:29 +02:00
|
|
|
case mnesia:index_read(pubsub_node, Nidx, #pubsub_node.id) of
|
2015-04-08 17:12:05 +02:00
|
|
|
[Record] when is_record(Record, pubsub_node) -> Record;
|
2016-09-08 14:49:27 +02:00
|
|
|
_ -> {error, xmpp:err_item_not_found(<<"Node not found">>, ?MYLANG)}
|
2009-05-26 23:50:13 +02:00
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_nodes(Host, _From) ->
|
|
|
|
get_nodes(Host).
|
2009-08-17 19:16:43 +02:00
|
|
|
|
2009-04-30 07:18:06 +02:00
|
|
|
get_nodes(Host) ->
|
|
|
|
mnesia:match_object(#pubsub_node{nodeid = {Host, '_'}, _ = '_'}).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_parentnodes(_Host, _Node, _From) ->
|
|
|
|
[].
|
2009-06-15 15:45:40 +02:00
|
|
|
|
|
|
|
%% @doc <p>Default node tree does not handle parents, return a list
|
|
|
|
%% containing just this node.</p>
|
2015-04-08 17:12:05 +02:00
|
|
|
get_parentnodes_tree(Host, Node, _From) ->
|
|
|
|
case catch mnesia:read({pubsub_node, {Host, Node}}) of
|
|
|
|
[Record] when is_record(Record, pubsub_node) -> [{0, [Record]}];
|
|
|
|
_ -> []
|
2009-06-15 15:45:40 +02:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subnodes(Host, Node, _From) ->
|
|
|
|
get_subnodes(Host, Node).
|
2010-10-21 11:14:24 +02:00
|
|
|
|
2009-10-20 17:03:07 +02:00
|
|
|
get_subnodes(Host, <<>>) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Q = qlc:q([N
|
2015-04-08 17:12:05 +02:00
|
|
|
|| #pubsub_node{nodeid = {NHost, _},
|
|
|
|
parents = Parents} =
|
|
|
|
N
|
|
|
|
<- mnesia:table(pubsub_node),
|
|
|
|
Host == NHost, Parents == []]),
|
2009-10-20 17:03:07 +02:00
|
|
|
qlc:e(Q);
|
2009-04-30 07:18:06 +02:00
|
|
|
get_subnodes(Host, Node) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Q = qlc:q([N
|
2015-04-08 17:12:05 +02:00
|
|
|
|| #pubsub_node{nodeid = {NHost, _},
|
|
|
|
parents = Parents} =
|
|
|
|
N
|
|
|
|
<- mnesia:table(pubsub_node),
|
|
|
|
Host == NHost, lists:member(Node, Parents)]),
|
2009-06-15 15:45:40 +02:00
|
|
|
qlc:e(Q).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-08-17 19:16:43 +02:00
|
|
|
get_subnodes_tree(Host, Node, _From) ->
|
|
|
|
get_subnodes_tree(Host, Node).
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
get_subnodes_tree(Host, Node) ->
|
|
|
|
case get_node(Host, Node) of
|
|
|
|
{error, _} ->
|
|
|
|
[];
|
|
|
|
Rec ->
|
|
|
|
BasePlugin = jlib:binary_to_atom(<<"node_",
|
|
|
|
(Rec#pubsub_node.type)/binary>>),
|
|
|
|
BasePath = BasePlugin:node_to_path(Node),
|
|
|
|
mnesia:foldl(fun (#pubsub_node{nodeid = {H, N}} = R, Acc) ->
|
|
|
|
Plugin = jlib:binary_to_atom(<<"node_",
|
|
|
|
(R#pubsub_node.type)/binary>>),
|
|
|
|
Path = Plugin:node_to_path(N),
|
|
|
|
case lists:prefix(BasePath, Path) and (H == Host) of
|
|
|
|
true -> [R | Acc];
|
|
|
|
false -> Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], pubsub_node)
|
2009-10-20 17:03:07 +02:00
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2015-04-08 17:12:05 +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)),
|
2016-03-31 10:00:29 +02:00
|
|
|
case mnesia:read({pubsub_node, {Host, Node}}) of
|
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 catch mnesia:read({pubsub_node, {Host, Parent}}) of
|
2016-04-19 15:18:32 +02:00
|
|
|
[#pubsub_node{owners = [{<<>>, Host, <<>>}]}] ->
|
2015-04-08 17:12:05 +02:00
|
|
|
true;
|
|
|
|
[#pubsub_node{owners = Owners}] ->
|
|
|
|
lists:member(BJID, Owners);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case ParentExists of
|
|
|
|
true ->
|
|
|
|
Nidx = pubsub_index:new(node),
|
|
|
|
mnesia:write(#pubsub_node{nodeid = {Host, Node},
|
|
|
|
id = Nidx, parents = Parents,
|
|
|
|
type = Type, owners = [BJID],
|
|
|
|
options = Options}),
|
|
|
|
{ok, Nidx};
|
|
|
|
false ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_forbidden()}
|
2015-04-08 17:12:05 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
2016-09-08 14:49:27 +02:00
|
|
|
{error, xmpp:err_conflict(<<"Node already exists">>, ?MYLANG)}
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
|
|
|
|
2015-04-08 17:12:05 +02:00
|
|
|
delete_node(Host, Node) ->
|
|
|
|
Removed = get_subnodes_tree(Host, Node),
|
|
|
|
lists:foreach(fun (#pubsub_node{nodeid = {_, SubNode}, id = SubNidx}) ->
|
|
|
|
pubsub_index:free(node, SubNidx),
|
|
|
|
mnesia:delete({pubsub_node, {Host, SubNode}})
|
|
|
|
end,
|
|
|
|
Removed),
|
2007-12-01 06:16:30 +01:00
|
|
|
Removed.
|