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

processone / ejabberd / 1258

12 Dec 2025 03:57PM UTC coverage: 33.638% (-0.006%) from 33.644%
1258

push

github

badlop
Container: Apply commit a22c88a

ejabberdctl.template: Show meaningful error when ERL_DIST_PORT is in use

15554 of 46240 relevant lines covered (33.64%)

1078.28 hits per line

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

72.73
/src/gen_iq_handler.erl
1
%%%----------------------------------------------------------------------
2
%%% File    : gen_iq_handler.erl
3
%%% Author  : Alexey Shchepin <alexey@process-one.net>
4
%%% Purpose : IQ handler support
5
%%% Created : 22 Jan 2003 by Alexey Shchepin <alexey@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
-module(gen_iq_handler).
27

28
-author('alexey@process-one.net').
29

30
%% API
31
-export([add_iq_handler/5, remove_iq_handler/3, handle/1, handle/2,
32
         start/1, get_features/2]).
33
%% Deprecated functions
34
-export([add_iq_handler/6, handle/5, iqdisc/1]).
35
-deprecated([{add_iq_handler, 6}, {handle, 5}, {iqdisc, 1}]).
36

37
-include("logger.hrl").
38
-include_lib("xmpp/include/xmpp.hrl").
39
-include("translate.hrl").
40

41

42
-type component() :: ejabberd_sm | ejabberd_local.
43

44
%%====================================================================
45
%% API
46
%%====================================================================
47
-spec start(component()) -> ok.
48
start(Component) ->
49
    catch ets:new(Component, [named_table, public, ordered_set,
22✔
50
                              {read_concurrency, true},
51
                              {heir, erlang:group_leader(), none}]),
52
    ok.
22✔
53

54
-spec add_iq_handler(component(), binary(), binary(), module(), atom()) -> ok.
55
add_iq_handler(Component, Host, NS, Module, Function) ->
56
    ets:insert(Component, {{Host, NS}, Module, Function}),
3,854✔
57
    ok.
3,854✔
58

59
-spec remove_iq_handler(component(), binary(), binary()) -> ok.
60
remove_iq_handler(Component, Host, NS) ->
61
    ets:delete(Component, {Host, NS}),
3,890✔
62
    ok.
3,890✔
63

64
-spec handle(iq()) -> ok.
65
handle(#iq{to = To} = IQ) ->
66
    Component = case To#jid.luser of
2,217✔
67
                    <<"">> -> ejabberd_local;
2,217✔
68
                    _ -> ejabberd_sm
×
69
                end,
70
    handle(Component, IQ).
2,217✔
71

72
-spec handle(component(), iq()) -> ok.
73
handle(Component,
74
       #iq{to = To, type = T, lang = Lang, sub_els = [El]} = Packet)
75
  when T == get; T == set ->
76
    XMLNS = xmpp:get_ns(El),
11,322✔
77
    Host = To#jid.lserver,
11,322✔
78
    case ets:lookup(Component, {Host, XMLNS}) of
11,322✔
79
        [{_, Module, Function}] ->
80
            process_iq(Host, Module, Function, Packet);
11,303✔
81
        [] ->
82
            Txt = ?T("No module is handling this query"),
19✔
83
            Err = xmpp:err_service_unavailable(Txt, Lang),
19✔
84
            ejabberd_router:route_error(Packet, Err)
19✔
85
    end;
86
handle(_, #iq{type = T, lang = Lang, sub_els = SubEls} = Packet)
87
  when T == get; T == set ->
88
    Txt = case SubEls of
2✔
89
              [] -> ?T("No child elements found");
1✔
90
              _ -> ?T("Too many child elements")
1✔
91
          end,
92
    Err = xmpp:err_bad_request(Txt, Lang),
2✔
93
    ejabberd_router:route_error(Packet, Err);
2✔
94
handle(_, #iq{type = T}) when T == result; T == error ->
95
    ok.
99✔
96

97
-spec get_features(component(), binary()) -> [binary()].
98
get_features(Component, Host) ->
99
    get_features(Component, ets:next(Component, {Host, <<"">>}), Host, []).
1,882✔
100

101
get_features(Component, {Host, XMLNS}, Host, XMLNSs) ->
102
    get_features(Component,
26,352✔
103
                 ets:next(Component, {Host, XMLNS}), Host, [XMLNS|XMLNSs]);
104
get_features(_, _, _, XMLNSs) ->
105
    XMLNSs.
1,882✔
106

107
-spec process_iq(binary(), atom(), atom(), iq()) -> ok.
108
process_iq(_Host, Module, Function, IQ) ->
109
    try process_iq(Module, Function, IQ) of
11,303✔
110
        #iq{} = ResIQ ->
111
            ejabberd_router:route(ResIQ);
10,926✔
112
        ignore ->
113
            ok
377✔
114
    catch
115
        Class:Reason:StackTrace ->
116
            ?ERROR_MSG("Failed to process iq:~n~ts~n** ~ts",
×
117
                       [xmpp:pp(IQ),
118
                        misc:format_exception(2, Class, Reason, StackTrace)]),
×
119
            Txt = ?T("Module failed to handle the query"),
×
120
            Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang),
×
121
            ejabberd_router:route_error(IQ, Err)
×
122
    end.
123

124
-spec process_iq(module(), atom(), iq()) -> ignore | iq().
125
process_iq(Module, Function, #iq{lang = Lang, sub_els = [El]} = IQ) ->
126
    try
11,303✔
127
        Pkt = case erlang:function_exported(Module, decode_iq_subel, 1) of
11,303✔
128
                  true -> Module:decode_iq_subel(El);
×
129
                  false -> xmpp:decode(El)
11,303✔
130
              end,
131
        Module:Function(IQ#iq{sub_els = [Pkt]})
11,303✔
132
    catch error:{xmpp_codec, Why} ->
133
            Txt = xmpp:io_format_error(Why),
×
134
            xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang))
×
135
    end.
136

137
-spec iqdisc(binary() | global) -> no_queue.
138
iqdisc(_Host) ->
139
    no_queue.
×
140

141
%%====================================================================
142
%% Deprecated API
143
%%====================================================================
144
-spec add_iq_handler(module(), binary(), binary(), module(), atom(), any()) -> ok.
145
add_iq_handler(Component, Host, NS, Module, Function, _Type) ->
146
    add_iq_handler(Component, Host, NS, Module, Function).
×
147

148
-spec handle(binary(), atom(), atom(), any(), iq()) -> any().
149
handle(Host, Module, Function, _Opts, IQ) ->
150
    process_iq(Host, Module, Function, IQ).
×
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

© 2025 Coveralls, Inc