2003-01-22 21:40:40 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : gen_iq_handler.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2006-01-29 05:38:31 +01:00
|
|
|
%%% Purpose : IQ handler support
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 22 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2007-12-24 12:41:41 +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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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-24 12:41:41 +01:00
|
|
|
%%%
|
2003-01-22 21:40:40 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(gen_iq_handler).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%% API
|
2018-05-09 09:30:00 +02:00
|
|
|
-export([add_iq_handler/5, remove_iq_handler/3, handle/1, handle/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
start/1, get_features/2]).
|
2018-02-11 10:54:15 +01:00
|
|
|
%% Deprecated functions
|
|
|
|
-export([add_iq_handler/6, handle/5, iqdisc/1]).
|
|
|
|
-deprecated([{add_iq_handler, 6}, {handle, 5}, {iqdisc, 1}]).
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-18 14:01:32 +02:00
|
|
|
-include("xmpp.hrl").
|
2018-06-20 11:32:10 +02:00
|
|
|
-include("translate.hrl").
|
2018-12-13 11:45:45 +01:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
-type component() :: ejabberd_sm | ejabberd_local.
|
2013-07-08 09:40:39 +02:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
2018-05-09 09:30:00 +02:00
|
|
|
-spec start(component()) -> ok.
|
|
|
|
start(Component) ->
|
|
|
|
catch ets:new(Component, [named_table, public, ordered_set,
|
|
|
|
{read_concurrency, true},
|
|
|
|
{heir, erlang:group_leader(), none}]),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec add_iq_handler(component(), binary(), binary(), module(), atom()) -> ok.
|
2018-02-11 10:54:15 +01:00
|
|
|
add_iq_handler(Component, Host, NS, Module, Function) ->
|
2018-05-09 09:30:00 +02:00
|
|
|
ets:insert(Component, {{Host, NS}, Module, Function}),
|
|
|
|
ok.
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2017-02-13 09:11:41 +01:00
|
|
|
-spec remove_iq_handler(component(), binary(), binary()) -> ok.
|
2005-06-20 05:18:13 +02:00
|
|
|
remove_iq_handler(Component, Host, NS) ->
|
2018-05-09 09:30:00 +02:00
|
|
|
ets:delete(Component, {Host, NS}),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec handle(iq()) -> ok.
|
|
|
|
handle(#iq{to = To} = IQ) ->
|
|
|
|
Component = case To#jid.luser of
|
|
|
|
<<"">> -> ejabberd_local;
|
|
|
|
_ -> ejabberd_sm
|
|
|
|
end,
|
|
|
|
handle(Component, IQ).
|
|
|
|
|
|
|
|
-spec handle(component(), iq()) -> ok.
|
|
|
|
handle(Component,
|
|
|
|
#iq{to = To, type = T, lang = Lang, sub_els = [El]} = Packet)
|
|
|
|
when T == get; T == set ->
|
|
|
|
XMLNS = xmpp:get_ns(El),
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
case ets:lookup(Component, {Host, XMLNS}) of
|
|
|
|
[{_, Module, Function}] ->
|
|
|
|
process_iq(Host, Module, Function, Packet);
|
|
|
|
[] ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2018-05-09 09:30:00 +02:00
|
|
|
Err = xmpp:err_service_unavailable(Txt, Lang),
|
|
|
|
ejabberd_router:route_error(Packet, Err)
|
|
|
|
end;
|
|
|
|
handle(_, #iq{type = T, lang = Lang, sub_els = SubEls} = Packet)
|
|
|
|
when T == get; T == set ->
|
|
|
|
Txt = case SubEls of
|
2018-06-20 11:32:10 +02:00
|
|
|
[] -> ?T("No child elements found");
|
|
|
|
_ -> ?T("Too many child elements")
|
2018-05-09 09:30:00 +02:00
|
|
|
end,
|
|
|
|
Err = xmpp:err_bad_request(Txt, Lang),
|
|
|
|
ejabberd_router:route_error(Packet, Err);
|
|
|
|
handle(_, #iq{type = T}) when T == result; T == error ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec get_features(component(), binary()) -> [binary()].
|
|
|
|
get_features(Component, Host) ->
|
|
|
|
get_features(Component, ets:next(Component, {Host, <<"">>}), Host, []).
|
|
|
|
|
|
|
|
get_features(Component, {Host, XMLNS}, Host, XMLNSs) ->
|
|
|
|
get_features(Component,
|
|
|
|
ets:next(Component, {Host, XMLNS}), Host, [XMLNS|XMLNSs]);
|
|
|
|
get_features(_, _, _, XMLNSs) ->
|
|
|
|
XMLNSs.
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2018-06-14 18:51:50 +02:00
|
|
|
-spec process_iq(binary(), atom(), atom(), iq()) -> ok.
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iq(_Host, Module, Function, IQ) ->
|
2018-06-14 18:49:27 +02:00
|
|
|
try process_iq(Module, Function, IQ) of
|
|
|
|
#iq{} = ResIQ ->
|
|
|
|
ejabberd_router:route(ResIQ);
|
|
|
|
ignore ->
|
|
|
|
ok
|
2019-07-07 21:12:14 +02:00
|
|
|
catch ?EX_RULE(Class, Reason, St) ->
|
2019-06-25 23:05:41 +02:00
|
|
|
StackTrace = ?EX_STACK(St),
|
2019-07-07 21:12:14 +02:00
|
|
|
?ERROR_MSG("Failed to process iq:~n~s~n** ~s",
|
|
|
|
[xmpp:pp(IQ),
|
|
|
|
misc:format_exception(2, Class, Reason, StackTrace)]),
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Module failed to handle the query"),
|
2016-07-18 14:01:32 +02:00
|
|
|
Err = xmpp:err_internal_server_error(Txt, IQ#iq.lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(IQ, Err)
|
2016-07-18 14:01:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec process_iq(module(), atom(), iq()) -> ignore | iq().
|
|
|
|
process_iq(Module, Function, #iq{lang = Lang, sub_els = [El]} = IQ) ->
|
|
|
|
try
|
2016-11-13 12:17:21 +01:00
|
|
|
Pkt = case erlang:function_exported(Module, decode_iq_subel, 1) of
|
|
|
|
true -> Module:decode_iq_subel(El);
|
|
|
|
false -> xmpp:decode(El)
|
2016-07-18 14:01:32 +02:00
|
|
|
end,
|
|
|
|
Module:Function(IQ#iq{sub_els = [Pkt]})
|
|
|
|
catch error:{xmpp_codec, Why} ->
|
2017-11-14 07:02:43 +01:00
|
|
|
Txt = xmpp:io_format_error(Why),
|
2016-07-18 14:01:32 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang))
|
|
|
|
end.
|
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
-spec iqdisc(binary() | global) -> no_queue.
|
|
|
|
iqdisc(_Host) ->
|
|
|
|
no_queue.
|
2017-05-04 11:24:47 +02:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
2018-02-11 10:54:15 +01:00
|
|
|
%% Deprecated API
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
2018-02-11 10:54:15 +01:00
|
|
|
-spec add_iq_handler(module(), binary(), binary(), module(), atom(), any()) -> ok.
|
|
|
|
add_iq_handler(Component, Host, NS, Module, Function, _Type) ->
|
|
|
|
add_iq_handler(Component, Host, NS, Module, Function).
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
-spec handle(binary(), atom(), atom(), any(), iq()) -> any().
|
|
|
|
handle(Host, Module, Function, _Opts, IQ) ->
|
2018-05-09 09:30:00 +02:00
|
|
|
process_iq(Host, Module, Function, IQ).
|