• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

processone / ejabberd / 1212

18 Nov 2025 12:37PM UTC coverage: 33.784% (+0.003%) from 33.781%
1212

push

github

badlop
mod_conversejs: Improve link to conversejs in WebAdmin (#4495)

Until now, the WebAdmin menu included a link to the first request handler
with mod_conversejs that the admin configured in ejabberd.yml
That link included the authentication credentials hashed as URI arguments
if using HTTPS. Then process/2 extracted those arguments and passed them
as autologin options to Converse.

From now, mod_conversejs automatically adds a request_handler nested in
webadmin subpath. The webadmin menu links to that converse URI; this allows
to access the HTTP auth credentials, no need to explicitly pass them.
process/2 extracts this HTTP auth and passes autologin options to Converse.
Now scram password storage is supported too.

This minimum configuration allows WebAdmin to access Converse:

listen:
  -
    port: 5443
    module: ejabberd_http
    tls: true
    request_handlers:
      /admin: ejabberd_web_admin
      /ws: ejabberd_http_ws
modules:
  mod_conversejs:
    conversejs_resources: "/home/conversejs/12.0.0/dist"

0 of 12 new or added lines in 1 file covered. (0.0%)

11290 existing lines in 174 files now uncovered.

15515 of 45924 relevant lines covered (33.78%)

1277.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.29
/src/nodetree_tree_sql.erl
1
%%%----------------------------------------------------------------------
2
%%% File    : nodetree_tree_sql.erl
3
%%% Author  : Christophe Romain <christophe.romain@process-one.net>
4
%%% Purpose : Standard node tree plugin with ODBC backend
5
%%% Created :  1 Dec 2007 by Christophe Romain <christophe.romain@process-one.net>
6
%%%
7
%%%
8
%%% ejabberd, Copyright (C) 2002-2025   ProcessOne
9
%%%
10
%%% This program is free software; you can redistribute it and/or
11
%%% modify it under the terms of the GNU General Public License as
12
%%% published by the Free Software Foundation; either version 2 of the
13
%%% License, or (at your option) any later version.
14
%%%
15
%%% This program is distributed in the hope that it will be useful,
16
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
17
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
%%% General Public License for more details.
19
%%%
20
%%% You should have received a copy of the GNU General Public License along
21
%%% with this program; if not, write to the Free Software Foundation, Inc.,
22
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
%%%
24
%%%----------------------------------------------------------------------
25

26
%%% @doc The module <strong>{@module}</strong> is the default PubSub node tree plugin.
27
%%% <p>It is used as a default for all unknown PubSub node type.  It can serve
28
%%% as a developer basis and reference to build its own custom pubsub node tree
29
%%% types.</p>
30
%%% <p>PubSub node tree plugins are using the {@link gen_nodetree} behaviour.</p>
31
%%% <p><strong>The API isn't stabilized yet</strong>. The pubsub plugin
32
%%% development is still a work in progress. However, the system is already
33
%%% usable and useful as is. Please, send us comments, feedback and
34
%%% improvements.</p>
35

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

40

41
-include("pubsub.hrl").
42
-include_lib("xmpp/include/xmpp.hrl").
43
-include("ejabberd_sql_pt.hrl").
44
-include("translate.hrl").
45

46
-export([init/3, terminate/2, options/0, set_node/1,
47
    get_node/3, get_node/2, get_node/1, get_nodes/2,
48
    get_nodes/1, get_all_nodes/1,
49
    get_parentnodes/3, get_parentnodes_tree/3,
50
    get_subnodes/3, get_subnodes_tree/3, create_node/6,
51
    delete_node/2]).
52

53
-export([raw_to_node/2]).
54

55
init(_Host, _ServerHost, _Opts) ->
UNCOV
56
    ok.
7✔
57

58
terminate(_Host, _ServerHost) ->
UNCOV
59
    ok.
7✔
60

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

64
set_node(Record) when is_record(Record, pubsub_node) ->
UNCOV
65
    {Host, Node} = Record#pubsub_node.nodeid,
252✔
UNCOV
66
    Parent = case Record#pubsub_node.parents of
252✔
UNCOV
67
        [] -> <<>>;
252✔
68
        [First | _] -> First
×
69
    end,
UNCOV
70
    Type = Record#pubsub_node.type,
252✔
UNCOV
71
    H = node_flat_sql:encode_host(Host),
252✔
UNCOV
72
    Nidx = case nodeidx(Host, Node) of
252✔
73
        {result, OldNidx} ->
UNCOV
74
            catch
28✔
75
            ejabberd_sql:sql_query_t(
UNCOV
76
              ?SQL("delete from pubsub_node_option "
40✔
77
                   "where nodeid=%(OldNidx)d")),
UNCOV
78
            catch
28✔
79
            ejabberd_sql:sql_query_t(
UNCOV
80
              ?SQL("update pubsub_node set"
40✔
81
                   " host=%(H)s, node=%(Node)s,"
82
                   " parent=%(Parent)s, plugin=%(Type)s "
83
                   "where nodeid=%(OldNidx)d")),
UNCOV
84
            OldNidx;
28✔
85
        {error, not_found} ->
UNCOV
86
            catch
224✔
87
            ejabberd_sql:sql_query_t(
UNCOV
88
              ?SQL("insert into pubsub_node(host, node, parent, plugin) "
292✔
89
                   "values(%(H)s, %(Node)s, %(Parent)s, %(Type)s)")),
UNCOV
90
            case nodeidx(Host, Node) of
224✔
UNCOV
91
                {result, NewNidx} -> NewNidx;
224✔
92
                {error, not_found} -> none;  % this should not happen
×
93
                {error, _} -> db_error
×
94
            end;
95
        {error, _} ->
96
            db_error
×
97
    end,
UNCOV
98
    case Nidx of
252✔
99
        db_error ->
100
            {error, xmpp:err_internal_server_error(?T("Database failure"), ejabberd_option:language())};
×
101
        none ->
102
            Txt = ?T("Node index not found"),
×
103
            {error, xmpp:err_internal_server_error(Txt, ejabberd_option:language())};
×
104
        _ ->
UNCOV
105
            lists:foreach(fun ({Key, Value}) ->
252✔
UNCOV
106
                        SKey = iolist_to_binary(atom_to_list(Key)),
4,872✔
UNCOV
107
                        SValue = misc:term_to_expr(Value),
4,872✔
UNCOV
108
                        catch
4,872✔
109
                        ejabberd_sql:sql_query_t(
UNCOV
110
                          ?SQL("insert into pubsub_node_option(nodeid, name, val) "
6,268✔
111
                               "values (%(Nidx)d, %(SKey)s, %(SValue)s)"))
112
                end,
113
                Record#pubsub_node.options),
UNCOV
114
            {result, Nidx}
252✔
115
    end.
116

117
get_node(Host, Node, _From) ->
118
    get_node(Host, Node).
×
119

120
get_node(Host, Node) ->
UNCOV
121
    H = node_flat_sql:encode_host(Host),
2,352✔
UNCOV
122
    case catch
2,352✔
123
        ejabberd_sql:sql_query_t(
UNCOV
124
          ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d from pubsub_node "
3,028✔
125
               "where host=%(H)s and node=%(Node)s"))
126
    of
127
        {selected, [RItem]} ->
UNCOV
128
            raw_to_node(Host, RItem);
2,254✔
129
        {'EXIT', _Reason} ->
130
            {error, xmpp:err_internal_server_error(?T("Database failure"), ejabberd_option:language())};
×
131
        _ ->
UNCOV
132
            {error, xmpp:err_item_not_found(?T("Node not found"), ejabberd_option:language())}
98✔
133
    end.
134

135
get_node(Nidx) ->
136
    case catch
×
137
        ejabberd_sql:sql_query_t(
138
          ?SQL("select @(host)s, @(node)s, @(parent)s, @(plugin)s from pubsub_node "
×
139
               "where nodeid=%(Nidx)d"))
140
    of
141
        {selected, [{Host, Node, Parent, Type}]} ->
142
            raw_to_node(Host, {Node, Parent, Type, Nidx});
×
143
        {'EXIT', _Reason} ->
144
            {error, xmpp:err_internal_server_error(?T("Database failure"), ejabberd_option:language())};
×
145
        _ ->
146
            {error, xmpp:err_item_not_found(?T("Node not found"), ejabberd_option:language())}
×
147
    end.
148

149
get_nodes(Host) ->
150
    get_nodes(Host, infinity).
×
151

152
get_nodes(Host, Limit) ->
UNCOV
153
    H = node_flat_sql:encode_host(Host),
350✔
UNCOV
154
    Query = fun(mssql, _) when is_integer(Limit), Limit>=0 ->
350✔
155
                    ejabberd_sql:sql_query_t(
×
156
                      ?SQL("select top %(Limit)d @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
×
157
                           "from pubsub_node where host=%(H)s"));
158
               (_, _) when is_integer(Limit), Limit>=0 ->
159
                    ejabberd_sql:sql_query_t(
×
160
                      ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
×
161
                           "from pubsub_node where host=%(H)s limit %(Limit)d"));
162
               (_, _) ->
UNCOV
163
                    ejabberd_sql:sql_query_t(
350✔
UNCOV
164
                      ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
454✔
165
                           "from pubsub_node where host=%(H)s"))
166
            end,
UNCOV
167
    case ejabberd_sql:sql_query_t(Query) of
350✔
168
        {selected, RItems} ->
UNCOV
169
            [raw_to_node(Host, Item) || Item <- RItems];
350✔
170
        _ ->
171
            []
×
172
    end.
173

174
get_all_nodes({_U, _S, _R} = JID) ->
175
    SubKey = jid:tolower(JID),
×
176
    GenKey = jid:remove_resource(SubKey),
×
177
    EncKey = node_flat_sql:encode_jid(GenKey),
×
178
    Pattern = <<(node_flat_sql:encode_jid_like(GenKey))/binary, "/%">>,
×
179
    case ejabberd_sql:sql_query_t(
×
180
           ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
×
181
                "from pubsub_node where host=%(EncKey)s "
182
                "or host like %(Pattern)s %ESCAPE")) of
183
        {selected, RItems} ->
184
            [raw_to_node(GenKey, Item) || Item <- RItems];
×
185
        _ ->
186
            []
×
187
    end;
188
get_all_nodes(Host) ->
189
    Pattern1 = <<"%@", Host/binary>>,
×
190
    Pattern2 = <<"%@", Host/binary, "/%">>,
×
191
    case ejabberd_sql:sql_query_t(
×
192
           ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
×
193
                "from pubsub_node where host=%(Host)s "
194
                "or host like %(Pattern1)s "
195
                "or host like %(Pattern2)s %ESCAPE")) of
196
        {selected, RItems} ->
197
            [raw_to_node(Host, Item) || Item <- RItems];
×
198
        _ ->
199
            []
×
200
    end.
201

202
get_parentnodes(Host, Node, _From) ->
203
    case get_node(Host, Node) of
×
204
        Record when is_record(Record, pubsub_node) ->
205
            Record#pubsub_node.parents;
×
206
        _ ->
207
            []
×
208
    end.
209

210
get_parentnodes_tree(Host, Node, _From) ->
UNCOV
211
    get_parentnodes_tree(Host, Node, 0, []).
700✔
212
get_parentnodes_tree(Host, Node, Level, Acc) ->
UNCOV
213
    case get_node(Host, Node) of
700✔
214
        Record when is_record(Record, pubsub_node) ->
UNCOV
215
            Tree = [{Level, [Record]}|Acc],
700✔
UNCOV
216
            case Record#pubsub_node.parents of
700✔
217
                [Parent] -> get_parentnodes_tree(Host, Parent, Level+1, Tree);
×
UNCOV
218
                _ -> Tree
700✔
219
            end;
220
        _ ->
221
            Acc
×
222
    end.
223

224
get_subnodes(Host, Node, Limit) ->
225
    H = node_flat_sql:encode_host(Host),
×
226
    Query = fun(mssql, _) when is_integer(Limit), Limit>=0 ->
×
227
                    ejabberd_sql:sql_query_t(
×
228
                      ?SQL("select top %(Limit)d @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
×
229
                           "from pubsub_node where host=%(H)s and parent=%(Node)s"));
230
               (_, _) when is_integer(Limit), Limit>=0 ->
231
                    ejabberd_sql:sql_query_t(
×
232
                      ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
×
233
                           "from pubsub_node where host=%(H)s and parent=%(Node)s "
234
                           "limit %(Limit)d"));
235
               (_, _) ->
236
                    ejabberd_sql:sql_query_t(
×
237
                      ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d "
×
238
                           "from pubsub_node where host=%(H)s and parent=%(Node)s"))
239
            end,
240
    case ejabberd_sql:sql_query_t(Query) of
×
241
        {selected, RItems} ->
242
            [raw_to_node(Host, Item) || Item <- RItems];
×
243
        _ ->
244
            []
×
245
    end.
246

247
get_subnodes_tree(Host, Node, _From) ->
248
    get_subnodes_tree(Host, Node).
×
249

250
get_subnodes_tree(Host, Node) ->
UNCOV
251
    case get_node(Host, Node) of
224✔
252
        {error, _} ->
253
            [];
×
254
        Rec ->
UNCOV
255
            Type = Rec#pubsub_node.type,
224✔
UNCOV
256
            H = node_flat_sql:encode_host(Host),
224✔
UNCOV
257
            N = <<(ejabberd_sql:escape_like_arg(Node))/binary, "/%">>,
224✔
UNCOV
258
            Sub = case catch
224✔
259
                ejabberd_sql:sql_query_t(
UNCOV
260
                ?SQL("select @(node)s, @(parent)s, @(plugin)s, @(nodeid)d from pubsub_node "
292✔
261
                     "where host=%(H)s and plugin=%(Type)s and"
262
                     " (parent=%(Node)s or parent like %(N)s %ESCAPE)"))
263
            of
264
                {selected, RItems} ->
UNCOV
265
                    [raw_to_node(Host, Item) || Item <- RItems];
224✔
266
                _ ->
267
                    []
×
268
            end,
UNCOV
269
            [Rec|Sub]
224✔
270
    end.
271

272
create_node(Host, Node, Type, Owner, Options, Parents) ->
UNCOV
273
    BJID = jid:tolower(jid:remove_resource(Owner)),
224✔
UNCOV
274
    case nodeidx(Host, Node) of
224✔
275
        {error, not_found} ->
UNCOV
276
            ParentExists = case Host of
224✔
277
                {_U, _S, _R} ->
278
                    %% This is special case for PEP handling
279
                    %% PEP does not uses hierarchy
UNCOV
280
                    true;
14✔
281
                _ ->
UNCOV
282
                    case Parents of
210✔
283
                        [] ->
UNCOV
284
                            true;
210✔
285
                        [Parent | _] ->
286
                            case nodeidx(Host, Parent) of
×
287
                                {result, PNode} ->
288
                                    case nodeowners(PNode) of
×
289
                                        [{<<>>, Host, <<>>}] -> true;
×
290
                                        Owners -> lists:member(BJID, Owners)
×
291
                                    end;
292
                                _ ->
293
                                    false
×
294
                            end;
295
                        _ ->
296
                            false
×
297
                    end
298
            end,
UNCOV
299
            case ParentExists of
224✔
300
                true ->
UNCOV
301
                    case set_node(#pubsub_node{nodeid = {Host, Node},
224✔
302
                                parents = Parents, type = Type,
303
                                options = Options})
304
                    of
UNCOV
305
                        {result, Nidx} -> {ok, Nidx};
224✔
306
                        Other -> Other
×
307
                    end;
308
                false ->
309
                    {error, xmpp:err_forbidden()}
×
310
            end;
311
        {result, _} ->
312
            {error, xmpp:err_conflict(?T("Node already exists"), ejabberd_option:language())};
×
313
        {error, db_fail} ->
314
            {error, xmpp:err_internal_server_error(?T("Database failure"), ejabberd_option:language())}
×
315
    end.
316

317
delete_node(Host, Node) ->
UNCOV
318
    lists:map(
224✔
319
        fun(Rec) ->
UNCOV
320
            Nidx = Rec#pubsub_node.id,
224✔
UNCOV
321
            catch ejabberd_sql:sql_query_t(
224✔
UNCOV
322
                    ?SQL("delete from pubsub_node where nodeid=%(Nidx)d")),
292✔
UNCOV
323
            Rec
224✔
324
        end, get_subnodes_tree(Host, Node)).
325

326
%% helpers
327
raw_to_node(Host, [Node, Parent, Type, Nidx]) ->
328
    raw_to_node(Host, {Node, Parent, Type, binary_to_integer(Nidx)});
×
329
raw_to_node(Host, {Node, Parent, Type, Nidx}) ->
UNCOV
330
    Options = case catch
2,422✔
331
        ejabberd_sql:sql_query_t(
UNCOV
332
          ?SQL("select @(name)s, @(val)s from pubsub_node_option "
3,118✔
333
               "where nodeid=%(Nidx)d"))
334
    of
335
        {selected, ROptions} ->
UNCOV
336
            DbOpts = lists:map(
2,422✔
337
                fun({<<"max_items">>, <<"infinity">>}) ->
338
                       {max_items, max};
×
339
                   ({Key, Value}) ->
UNCOV
340
                       RKey = misc:binary_to_atom(Key),
47,075✔
UNCOV
341
                       Tokens = element(2, erl_scan:string(binary_to_list(<<Value/binary, ".">>))),
47,075✔
UNCOV
342
                       RValue = element(2, erl_parse:parse_term(Tokens)),
47,075✔
UNCOV
343
                       {RKey, RValue}
47,075✔
344
                end,
345
                ROptions),
UNCOV
346
            Module = misc:binary_to_atom(<<"node_", Type/binary, "_sql">>),
2,422✔
UNCOV
347
            StdOpts = Module:options(),
2,422✔
UNCOV
348
            lists:foldl(fun ({Key, Value}, Acc) ->
2,422✔
UNCOV
349
                        lists:keystore(Key, 1, Acc, {Key, Value})
47,075✔
350
                end,
351
                StdOpts, DbOpts);
352
        _ ->
353
            []
×
354
    end,
UNCOV
355
    Parents = case Parent of
2,422✔
UNCOV
356
        <<>> -> [];
2,422✔
357
        _ -> [Parent]
×
358
    end,
UNCOV
359
    #pubsub_node{nodeid = {Host, Node}, id = Nidx,
2,422✔
360
        parents = Parents, type = Type, options = Options}.
361

362
nodeidx(Host, Node) ->
UNCOV
363
    H = node_flat_sql:encode_host(Host),
700✔
UNCOV
364
    case catch
700✔
365
        ejabberd_sql:sql_query_t(
UNCOV
366
          ?SQL("select @(nodeid)d from pubsub_node "
904✔
367
               "where host=%(H)s and node=%(Node)s"))
368
    of
369
        {selected, [{Nidx}]} ->
UNCOV
370
            {result, Nidx};
252✔
371
        {'EXIT', _Reason} ->
372
            {error, db_fail};
×
373
        _ ->
UNCOV
374
            {error, not_found}
448✔
375
    end.
376

377
nodeowners(Nidx) ->
378
    {result, Res} = node_flat_sql:get_node_affiliations(Nidx),
×
379
    [LJID || {LJID, Aff} <- Res, Aff =:= owner].
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc