diff options
Diffstat (limited to 'src/gen_iq_handler.erl')
-rw-r--r-- | src/gen_iq_handler.erl | 70 |
1 files changed, 59 insertions, 11 deletions
diff --git a/src/gen_iq_handler.erl b/src/gen_iq_handler.erl index 0982a9f46..15155ac1a 100644 --- a/src/gen_iq_handler.erl +++ b/src/gen_iq_handler.erl @@ -30,9 +30,9 @@ -behaviour(ejabberd_config). %% API --export([add_iq_handler/5, remove_iq_handler/3, handle/4, - process_iq/4, check_type/1, transform_module_options/1, - opt_type/1]). +-export([add_iq_handler/5, remove_iq_handler/3, handle/1, handle/2, + check_type/1, transform_module_options/1, + opt_type/1, 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}]). @@ -46,17 +46,65 @@ %%==================================================================== %% API %%==================================================================== --spec add_iq_handler(module(), binary(), binary(), module(), atom()) -> ok. +-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) -> - Component:register_iq_handler(Host, NS, Module, Function). + ets:insert(Component, {{Host, NS}, Module, Function}), + ok. -spec remove_iq_handler(component(), binary(), binary()) -> ok. remove_iq_handler(Component, Host, NS) -> - Component:unregister_iq_handler(Host, NS). - --spec handle(binary(), atom(), atom(), iq()) -> any(). -handle(Host, Module, Function, IQ) -> - process_iq(Host, Module, Function, IQ). + 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 = <<"No module is handling this query">>, + 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 + [] -> <<"No child elements found">>; + _ -> <<"Too many child elements">> + 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. -spec process_iq(binary(), atom(), atom(), iq()) -> any(). process_iq(_Host, Module, Function, IQ) -> @@ -139,4 +187,4 @@ add_iq_handler(Component, Host, NS, Module, Function, _Type) -> -spec handle(binary(), atom(), atom(), any(), iq()) -> any(). handle(Host, Module, Function, _Opts, IQ) -> - handle(Host, Module, Function, IQ). + process_iq(Host, Module, Function, IQ). |