2002-11-30 19:46:16 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_local.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2004-11-05 22:14:31 +01:00
|
|
|
%%% Purpose : Route local packets
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 30 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 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
|
|
|
%%%
|
2002-11-30 19:46:16 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_local).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
2017-01-11 14:25:43 +01:00
|
|
|
-export([start/0, start_link/0]).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
-export([route/1, process_iq/1,
|
|
|
|
get_features/1,
|
|
|
|
register_iq_handler/5,
|
|
|
|
unregister_iq_handler/2,
|
|
|
|
bounce_resource_packet/1,
|
2017-02-23 08:12:19 +01:00
|
|
|
host_up/1, host_down/1]).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%% gen_server callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, terminate/2, code_change/3]).
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
%% deprecated functions: use ejabberd_router:route_iq/3,4
|
|
|
|
-export([route_iq/2, route_iq/3]).
|
|
|
|
-deprecated([{route_iq, 2}, {route_iq, 3}]).
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2017-01-23 11:51:05 +01:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2016-07-18 14:01:32 +02:00
|
|
|
-include("xmpp.hrl").
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {}).
|
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
-define(IQTABLE, local_iqtable).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
2017-01-11 14:25:43 +01:00
|
|
|
start() ->
|
|
|
|
ChildSpec = {?MODULE, {?MODULE, start_link, []},
|
|
|
|
transient, 1000, worker, [?MODULE]},
|
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [],
|
|
|
|
[]).
|
2002-12-11 21:57:45 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec process_iq(iq()) -> any().
|
|
|
|
process_iq(#iq{to = To, type = T, lang = Lang, sub_els = [El]} = Packet)
|
2016-07-18 14:01:32 +02:00
|
|
|
when T == get; T == set ->
|
|
|
|
XMLNS = xmpp:get_ns(El),
|
|
|
|
Host = To#jid.lserver,
|
2017-01-24 08:38:29 +01:00
|
|
|
case ets:lookup(?IQTABLE, {Host, XMLNS}) of
|
2016-07-18 14:01:32 +02:00
|
|
|
[{_, Module, Function, Opts}] ->
|
2017-02-16 09:00:26 +01:00
|
|
|
gen_iq_handler:handle(Host, Module, Function, Opts, Packet);
|
2016-07-18 14:01:32 +02:00
|
|
|
[] ->
|
|
|
|
Txt = <<"No module is handling this query">>,
|
2016-11-25 09:41:24 +01:00
|
|
|
Err = xmpp:err_service_unavailable(Txt, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2016-07-18 14:01:32 +02:00
|
|
|
end;
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iq(#iq{type = T, lang = Lang, sub_els = SubEls} = Packet)
|
2017-01-09 15:02:17 +01:00
|
|
|
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),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err);
|
2017-11-10 16:02:22 +01:00
|
|
|
process_iq(#iq{type = T}) when T == result; T == error ->
|
|
|
|
ok.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec route(stanza()) -> any().
|
|
|
|
route(Packet) ->
|
|
|
|
try do_route(Packet)
|
|
|
|
catch E:R ->
|
|
|
|
?ERROR_MSG("failed to route packet:~n~s~nReason = ~p",
|
|
|
|
[xmpp:pp(Packet), {E, {R, erlang:get_stacktrace()}}])
|
2004-11-05 22:14:31 +01:00
|
|
|
end.
|
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
-spec route_iq(iq(), function()) -> ok.
|
|
|
|
route_iq(IQ, Fun) ->
|
|
|
|
route_iq(IQ, Fun, undefined).
|
|
|
|
|
|
|
|
-spec route_iq(iq(), function(), undefined | non_neg_integer()) -> ok.
|
|
|
|
route_iq(IQ, Fun, Timeout) ->
|
|
|
|
ejabberd_router:route_iq(IQ, Fun, undefined, Timeout).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec register_iq_handler(binary(), binary(), module(), function(),
|
2017-02-13 09:11:41 +01:00
|
|
|
gen_iq_handler:opts()) -> ok.
|
2005-06-20 05:18:13 +02:00
|
|
|
register_iq_handler(Host, XMLNS, Module, Fun, Opts) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
gen_server:cast(?MODULE,
|
|
|
|
{register_iq_handler, Host, XMLNS, Module, Fun, Opts}).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2017-02-13 09:11:41 +01:00
|
|
|
-spec unregister_iq_handler(binary(), binary()) -> ok.
|
2005-06-20 05:18:13 +02:00
|
|
|
unregister_iq_handler(Host, XMLNS) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
gen_server:cast(?MODULE, {unregister_iq_handler, Host, XMLNS}).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec bounce_resource_packet(stanza()) -> ok | stop.
|
|
|
|
bounce_resource_packet(#presence{to = #jid{lresource = <<"">>}}) ->
|
2016-11-18 11:38:08 +01:00
|
|
|
ok;
|
2017-02-16 09:00:26 +01:00
|
|
|
bounce_resource_packet(#message{to = #jid{lresource = <<"">>}, type = headline}) ->
|
2016-11-12 11:27:15 +01:00
|
|
|
ok;
|
2017-02-16 09:00:26 +01:00
|
|
|
bounce_resource_packet(Packet) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"No available resource found">>,
|
2016-11-25 09:41:24 +01:00
|
|
|
Err = xmpp:err_item_not_found(Txt, Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err),
|
2016-11-18 11:38:08 +01:00
|
|
|
stop.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2017-01-23 11:51:05 +01:00
|
|
|
-spec get_features(binary()) -> [binary()].
|
|
|
|
get_features(Host) ->
|
2017-01-24 08:38:29 +01:00
|
|
|
get_features(ets:next(?IQTABLE, {Host, <<"">>}), Host, []).
|
|
|
|
|
|
|
|
get_features({Host, XMLNS}, Host, XMLNSs) ->
|
|
|
|
get_features(ets:next(?IQTABLE, {Host, XMLNS}), Host, [XMLNS|XMLNSs]);
|
|
|
|
get_features(_, _, XMLNSs) ->
|
|
|
|
XMLNSs.
|
2017-01-23 11:51:05 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
init([]) ->
|
2017-03-28 15:31:37 +02:00
|
|
|
process_flag(trap_exit, true),
|
2017-02-23 08:12:19 +01:00
|
|
|
lists:foreach(fun host_up/1, ?MYHOSTS),
|
|
|
|
ejabberd_hooks:add(host_up, ?MODULE, host_up, 10),
|
|
|
|
ejabberd_hooks:add(host_down, ?MODULE, host_down, 100),
|
2017-04-05 14:10:18 +02:00
|
|
|
catch ets:new(?IQTABLE, [named_table, public, ordered_set,
|
|
|
|
{read_concurrency, true}]),
|
2009-07-30 10:58:21 +02:00
|
|
|
update_table(),
|
2006-01-29 05:38:31 +01:00
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
handle_call(_Request, _From, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Reply = ok, {reply, Reply, State}.
|
|
|
|
|
2017-02-14 08:25:08 +01:00
|
|
|
handle_cast({register_iq_handler, Host, XMLNS, Module,
|
2013-03-14 10:33:02 +01:00
|
|
|
Function, Opts},
|
|
|
|
State) ->
|
|
|
|
ets:insert(?IQTABLE,
|
2017-01-24 08:38:29 +01:00
|
|
|
{{Host, XMLNS}, Module, Function, Opts}),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State};
|
2017-02-14 08:25:08 +01:00
|
|
|
handle_cast({unregister_iq_handler, Host, XMLNS},
|
2013-03-14 10:33:02 +01:00
|
|
|
State) ->
|
2017-01-24 08:38:29 +01:00
|
|
|
case ets:lookup(?IQTABLE, {Host, XMLNS}) of
|
2013-03-14 10:33:02 +01:00
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:stop_iq_handler(Module, Function, Opts);
|
|
|
|
_ -> ok
|
2006-01-29 05:38:31 +01:00
|
|
|
end,
|
2017-01-24 08:38:29 +01:00
|
|
|
ets:delete(?IQTABLE, {Host, XMLNS}),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State};
|
2017-02-14 08:25:08 +01:00
|
|
|
handle_cast(_Msg, State) -> {noreply, State}.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_info({route, Packet}, State) ->
|
|
|
|
route(Packet),
|
2017-02-14 08:25:08 +01:00
|
|
|
{noreply, State};
|
|
|
|
handle_info(Info, State) ->
|
|
|
|
?WARNING_MSG("unexpected info: ~p", [Info]),
|
2015-10-07 00:06:58 +02:00
|
|
|
{noreply, State}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
terminate(_Reason, _State) ->
|
2017-02-23 08:12:19 +01:00
|
|
|
lists:foreach(fun host_down/1, ?MYHOSTS),
|
|
|
|
ejabberd_hooks:delete(host_up, ?MODULE, host_up, 10),
|
|
|
|
ejabberd_hooks:delete(host_down, ?MODULE, host_down, 100),
|
2015-10-07 00:06:58 +02:00
|
|
|
ok.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec do_route(stanza()) -> any().
|
|
|
|
do_route(Packet) ->
|
|
|
|
?DEBUG("local route:~n~s", [xmpp:pp(Packet)]),
|
2016-11-12 11:27:15 +01:00
|
|
|
Type = xmpp:get_type(Packet),
|
2017-02-16 09:00:26 +01:00
|
|
|
To = xmpp:get_to(Packet),
|
2013-03-14 10:33:02 +01:00
|
|
|
if To#jid.luser /= <<"">> ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_sm:route(Packet);
|
2016-11-12 11:27:15 +01:00
|
|
|
is_record(Packet, iq), To#jid.lresource == <<"">> ->
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iq(Packet);
|
2016-11-18 11:38:08 +01:00
|
|
|
Type == result; Type == error ->
|
2016-11-12 11:27:15 +01:00
|
|
|
ok;
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
2016-11-12 11:27:15 +01:00
|
|
|
ejabberd_hooks:run(local_send_to_resource_hook,
|
2017-02-16 09:00:26 +01:00
|
|
|
To#jid.lserver, [Packet])
|
2013-03-14 10:33:02 +01:00
|
|
|
end.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec update_table() -> ok.
|
2009-07-30 10:58:21 +02:00
|
|
|
update_table() ->
|
2017-11-10 16:02:22 +01:00
|
|
|
catch mnesia:delete_table(iq_response),
|
|
|
|
ok.
|
2009-07-30 10:58:21 +02:00
|
|
|
|
2017-02-23 08:12:19 +01:00
|
|
|
host_up(Host) ->
|
2017-03-15 08:27:22 +01:00
|
|
|
Owner = case whereis(?MODULE) of
|
|
|
|
undefined -> self();
|
|
|
|
Pid -> Pid
|
|
|
|
end,
|
|
|
|
ejabberd_router:register_route(Host, Host, {apply, ?MODULE, route}, Owner),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:add(local_send_to_resource_hook, Host,
|
|
|
|
?MODULE, bounce_resource_packet, 100).
|
|
|
|
|
|
|
|
host_down(Host) ->
|
2017-03-28 15:31:37 +02:00
|
|
|
Owner = case whereis(?MODULE) of
|
|
|
|
undefined -> self();
|
|
|
|
Pid -> Pid
|
|
|
|
end,
|
|
|
|
ejabberd_router:unregister_route(Host, Owner),
|
2017-02-23 08:12:19 +01:00
|
|
|
ejabberd_hooks:delete(local_send_to_resource_hook, Host,
|
|
|
|
?MODULE, bounce_resource_packet, 100).
|