aboutsummaryrefslogtreecommitdiff
path: root/src/nodetree_tree_sql.erl
blob: 6c34199484b478f0444362efc8853eec284ad6ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
%%%----------------------------------------------------------------------
%%% File    : nodetree_tree_sql.erl
%%% 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>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2022   ProcessOne
%%%
%%% 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.
%%%
%%%----------------------------------------------------------------------

%%% @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
%%% usable and useful as is. Please, send us comments, feedback and
%%% improvements.</p>

-module(nodetree_tree_sql).
-behaviour(gen_pubsub_nodetree).
-author('christophe.romain@process-one.net').


-include("pubsub.hrl").
-include_lib("xmpp/include/xmpp.hrl").
-include("ejabberd_sql_pt.hrl").
-include("translate.hrl").

-export([init/3, terminate/2, options/0, set_node/1,
    get_node/3, get_node/2, get_node/1, get_nodes/2,
    get_nodes/1, get_all_nodes/1,
    get_parentnodes/3, get_parentnodes_tree/3,
    get_subnodes/3, get_subnodes_tree/3, create_node/6,
    delete_node/2]).

-export([raw_to_node/2]).

init(_Host, _ServerHost, _Opts) ->
    ok.

terminate(_Host, _ServerHost) ->
    ok.

options() ->
    [{sql, true} | nodetree_tree:options()].

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,
    H = node_flat_sql:encode_host(Host),
    Nidx = case nodeidx(Host, Node) of
	{result, OldNidx} ->
	    catch
	    ejabberd_sql:sql_query_t(
	      ?SQL("delete from pubsub_node_option "
		   "where nodeid=%(OldNidx)d")),
	    catch
	    ejabberd_sql:sql_query_t(
	      ?SQL("update pubsub_node set"
		   " host=%(H)s, node=%(Node)s,"
		   " parent=%(Parent)s, plugin=%(Type)s "
		   "where nodeid=%(OldNidx)d")),
	    OldNidx;
	_ ->
	    catch
	    ejabberd_sql:sql_query_t(
	      ?SQL("insert into pubsub_node(host, node, parent, plugin) "
		   "values(%(H)s, %(Node)s, %(Parent)s, %(Type)s)")),
	    case nodeidx(Host, Node) of
		{result, NewNidx} -> NewNidx;
		_ -> none  % this should not happen
	    end
    end,
    case Nidx of
	none ->
	    Txt = ?T("Node index not found"),
	    {error, xmpp:err_internal_server_error(Txt, ejabberd_option:language())};
	_ ->
	    lists:foreach(fun ({Key, Value}) ->
			SKey = iolist_to_binary(atom_to_list(Key)),
			SValue = misc:term_to_expr(Value),
			catch
			ejabberd_sql:sql_query_t(
			  ?SQL("insert into pubsub_node_option(nodeid, name, val) "
			       "values (%(Nidx)d, %(SKey)s, %(SValue)s)"))
		end,
		Record#pubsub_node.options),
	    {result, Nidx}
    end.

get_node(Host, Node, _From) ->
    get_node(Host, Node).

get_node(Host, Node) ->
    H = node_flat_sql:encode_host(Host),
    case catch
	ejabberd_sql:sql_query_t(
	  ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d from pubsub_node "
	       "where host=%(H)s and node=%(Node)s"))
    of
	{selected, [RItem]} ->
	    raw_to_node(Host, RItem);
	{'EXIT', _Reason} ->
	    {error, xmpp:err_internal_server_error(?T("Database failure"), ejabberd_option:language())};
	_ ->
	    {error, xmpp:err_item_not_found(?T("Node not found"), ejabberd_option:language())}
    end.

get_node(Nidx) ->
    case catch
	ejabberd_sql:sql_query_t(
	  ?SQL("select @(host)s, @(node)s, @(parent)s, @(plugin)s from pubsub_node "
	       "where nodeid=%(Nidx)d"))
    of
	{selected, [{Host, Node, Parent, Type}]} ->
	    raw_to_node(Host, {Node, Parent, Type, Nidx});
	{'EXIT', _Reason} ->
	    {error, xmpp:err_internal_server_error(?T("Database failure"), ejabberd_option:language())};
	_ ->
	    {error, xmpp:err_item_not_found(?T("Node not found"), ejabberd_option:language())}
    end.

get_nodes(Host) ->
    get_nodes(Host, infinity).

get_nodes(Host, Limit) ->
    H = node_flat_sql:encode_host(Host),
    Query = fun(mssql, _) when is_integer(Limit), Limit>=0 ->
		    ejabberd_sql:sql_query_t(
		      ?SQL("select top %(Limit)d @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
			   "from pubsub_node where host=%(H)s"));
	       (_, _) when is_integer(Limit), Limit>=0 ->
		    ejabberd_sql:sql_query_t(
		      ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
			   "from pubsub_node where host=%(H)s limit %(Limit)d"));
	       (_, _) ->
		    ejabberd_sql:sql_query_t(
		      ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
			   "from pubsub_node where host=%(H)s"))
	    end,
    case ejabberd_sql:sql_query_t(Query) of
	{selected, RItems} ->
	    [raw_to_node(Host, Item) || Item <- RItems];
	_ ->
	    []
    end.

get_all_nodes({_U, _S, _R} = JID) ->
    SubKey = jid:tolower(JID),
    GenKey = jid:remove_resource(SubKey),
    EncKey = node_flat_sql:encode_jid(GenKey),
    Pattern = <<(node_flat_sql:encode_jid_like(GenKey))/binary, "/%">>,
    case ejabberd_sql:sql_query_t(
	   ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
		"from pubsub_node where host=%(EncKey)s "
		"or host like %(Pattern)s %ESCAPE")) of
	{selected, RItems} ->
	    [raw_to_node(GenKey, Item) || Item <- RItems];
	_ ->
	    []
    end;
get_all_nodes(Host) ->
    Pattern1 = <<"%@", Host/binary>>,
    Pattern2 = <<"%@", Host/binary, "/%">>,
    case ejabberd_sql:sql_query_t(
	   ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
		"from pubsub_node where host=%(Host)s "
		"or host like %(Pattern1)s "
		"or host like %(Pattern2)s %ESCAPE")) of
	{selected, RItems} ->
	    [raw_to_node(Host, Item) || Item <- RItems];
	_ ->
	    []
    end.

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
    end.

get_subnodes(Host, Node, Limit) ->
    H = node_flat_sql:encode_host(Host),
    Query = fun(mssql, _) when is_integer(Limit), Limit>=0 ->
		    ejabberd_sql:sql_query_t(
		      ?SQL("select top %(Limit)d @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
			   "from pubsub_node where host=%(H)s and parent=%(Node)s"));
	       (_, _) when is_integer(Limit), Limit>=0 ->
		    ejabberd_sql:sql_query_t(
		      ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
			   "from pubsub_node where host=%(H)s and parent=%(Node)s "
			   "limit %(Limit)d"));
	       (_, _) ->
		    ejabberd_sql:sql_query_t(
		      ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
			   "from pubsub_node where host=%(H)s and parent=%(Node)s"))
	    end,
    case ejabberd_sql:sql_query_t(Query) of
	{selected, RItems} ->
	    [raw_to_node(Host, Item) || Item <- RItems];
	_ ->
	    []
    end.

get_subnodes_tree(Host, Node, _From) ->
    get_subnodes_tree(Host, Node).

get_subnodes_tree(Host, Node) ->
    case get_node(Host, Node) of
	{error, _} ->
	    [];
	Rec ->
	    Type = Rec#pubsub_node.type,
	    H = node_flat_sql:encode_host(Host),
	    N = <<(ejabberd_sql:escape_like_arg(Node))/binary, "/%">>,
	    Sub = case catch
		ejabberd_sql:sql_query_t(
		?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)"))
	    of
		{selected, RItems} ->
		    [raw_to_node(Host, Item) || Item <- RItems];
		_ ->
		    []
	    end,
	    [Rec|Sub]
    end.

create_node(Host, Node, Type, Owner, Options, Parents) ->
    BJID = jid:tolower(jid:remove_resource(Owner)),
    case nodeidx(Host, Node) of
	{error, not_found} ->
	    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})
		    of
			{result, Nidx} -> {ok, Nidx};
			Other -> Other
		    end;
		false ->
		    {error, xmpp:err_forbidden()}
	    end;
	{result, _} ->
	    {error, xmpp:err_conflict(?T("Node already exists"), ejabberd_option:language())};
	{error, db_fail} ->
	    {error, xmpp:err_internal_server_error(?T("Database failure"), ejabberd_option:language())}
    end.

delete_node(Host, Node) ->
    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)).

%% helpers
raw_to_node(Host, [Node, Parent, Type, Nidx]) ->
    raw_to_node(Host, {Node, Parent, Type, binary_to_integer(Nidx)});
raw_to_node(Host, {Node, Parent, Type, Nidx}) ->
    Options = case catch
	ejabberd_sql:sql_query_t(
	  ?SQL("select @(name)s, @(val)s from pubsub_node_option "
	       "where nodeid=%(Nidx)d"))
    of
	{selected, ROptions} ->
	    DbOpts = lists:map(fun ({Key, Value}) ->
			    RKey = misc:binary_to_atom(Key),
			    Tokens = element(2, erl_scan:string(binary_to_list(<<Value/binary, ".">>))),
			    RValue = element(2, erl_parse:parse_term(Tokens)),
			    {RKey, RValue}
		    end,
		    ROptions),
	    Module = misc:binary_to_atom(<<"node_", Type/binary, "_sql">>),
	    StdOpts = Module:options(),
	    lists:foldl(fun ({Key, Value}, Acc) ->
			lists:keystore(Key, 1, Acc, {Key, Value})
		end,
		StdOpts, DbOpts);
	_ ->
	    []
    end,
    Parents = case Parent of
	<<>> -> [];
	_ -> [Parent]
    end,
    #pubsub_node{nodeid = {Host, Node}, id = Nidx,
	parents = Parents, type = Type, options = Options}.

nodeidx(Host, Node) ->
    H = node_flat_sql:encode_host(Host),
    case catch
	ejabberd_sql:sql_query_t(
	  ?SQL("select @(nodeid)d from pubsub_node "
	       "where host=%(H)s and node=%(Node)s"))
    of
	{selected, [{Nidx}]} ->
	    {result, Nidx};
	{'EXIT', _Reason} ->
	    {error, db_fail};
	_ ->
	    {error, not_found}
    end.

nodeowners(Nidx) ->
    {result, Res} = node_flat_sql:get_node_affiliations(Nidx),
    [LJID || {LJID, Aff} <- Res, Aff =:= owner].