25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-24 17:29:28 +01:00
xmpp.chapril.org-ejabberd/src/gen_iq_handler.erl

151 lines
5.3 KiB
Erlang
Raw Normal View History

%%%----------------------------------------------------------------------
%%% File : gen_iq_handler.erl
%%% Author : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : IQ handler support
%%% 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
%%%
%%% 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.
%%%
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.
%%%
%%%----------------------------------------------------------------------
-module(gen_iq_handler).
-author('alexey@process-one.net').
%% 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]).
%% Deprecated functions
-export([add_iq_handler/6, handle/5, iqdisc/1]).
-deprecated([{add_iq_handler, 6}, {handle, 5}, {iqdisc, 1}]).
-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").
-include("ejabberd_stacktrace.hrl").
2015-10-07 00:06:58 +02:00
-type component() :: ejabberd_sm | ejabberd_local.
2013-07-08 09:40:39 +02: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.
add_iq_handler(Component, Host, NS, Module, Function) ->
2018-05-09 09:30:00 +02:00
ets:insert(Component, {{Host, NS}, Module, Function}),
ok.
-spec remove_iq_handler(component(), binary(), binary()) -> ok.
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);
[] ->
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.
2018-06-14 18:51:50 +02:00
-spec process_iq(binary(), atom(), atom(), iq()) -> ok.
process_iq(_Host, Module, Function, IQ) ->
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) ->
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)]),
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),
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
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} ->
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.
-spec iqdisc(binary() | global) -> no_queue.
iqdisc(_Host) ->
no_queue.
2017-05-04 11:24:47 +02:00
%%====================================================================
%% Deprecated API
%%====================================================================
-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).
-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).