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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
|
|
|
-export([start_link/0]).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([route/3, route_iq/4, route_iq/5,
|
|
|
|
process_iq_reply/3, register_iq_handler/4,
|
|
|
|
register_iq_handler/5, register_iq_response_handler/4,
|
|
|
|
register_iq_response_handler/5, unregister_iq_handler/2,
|
|
|
|
unregister_iq_response_handler/2, refresh_iq_handlers/0,
|
|
|
|
bounce_resource_packet/3]).
|
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
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {}).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(iq_response, {id = <<"">> :: binary(),
|
|
|
|
module :: atom(),
|
|
|
|
function :: atom() | fun(),
|
|
|
|
timer = make_ref() :: reference()}).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
-define(IQTABLE, local_iqtable).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
%% This value is used in SIP and Megaco for a transaction lifetime.
|
|
|
|
-define(IQ_TIMEOUT, 32000).
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
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
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
process_iq(From, To, Packet) ->
|
2002-12-11 21:57:45 +01:00
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
2013-03-14 10:33:02 +01:00
|
|
|
#iq{xmlns = XMLNS} ->
|
|
|
|
Host = To#jid.lserver,
|
|
|
|
case ets:lookup(?IQTABLE, {XMLNS, Host}) of
|
|
|
|
[{_, Module, Function}] ->
|
|
|
|
ResIQ = Module:Function(From, To, IQ),
|
|
|
|
if ResIQ /= ignore ->
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ));
|
|
|
|
true -> ok
|
|
|
|
end;
|
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:handle(Host, Module, Function, Opts,
|
|
|
|
From, To, IQ);
|
|
|
|
[] ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end;
|
|
|
|
reply ->
|
|
|
|
IQReply = jlib:iq_query_or_response_info(Packet),
|
|
|
|
process_iq_reply(From, To, IQReply);
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_BAD_REQUEST),
|
|
|
|
ejabberd_router:route(To, From, Err),
|
|
|
|
ok
|
2002-12-11 21:57:45 +01:00
|
|
|
end.
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
process_iq_reply(From, To, #iq{id = ID} = IQ) ->
|
|
|
|
case get_iq_callback(ID) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, undefined, Function} -> Function(IQ), ok;
|
|
|
|
{ok, Module, Function} ->
|
|
|
|
Module:Function(From, To, IQ), ok;
|
|
|
|
_ -> nothing
|
2007-12-01 06:16:30 +01:00
|
|
|
end.
|
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
route(From, To, Packet) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ -> ok
|
2004-11-05 22:14:31 +01:00
|
|
|
end.
|
|
|
|
|
2011-02-21 13:44:30 +01:00
|
|
|
route_iq(From, To, IQ, F) ->
|
|
|
|
route_iq(From, To, IQ, F, undefined).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
route_iq(From, To, #iq{type = Type} = IQ, F, Timeout)
|
|
|
|
when is_function(F) ->
|
2009-07-30 10:58:21 +02:00
|
|
|
Packet = if Type == set; Type == get ->
|
|
|
|
ID = randoms:get_string(),
|
|
|
|
Host = From#jid.lserver,
|
2011-02-21 13:44:30 +01:00
|
|
|
register_iq_response_handler(Host, ID, undefined, F, Timeout),
|
2009-07-30 10:58:21 +02:00
|
|
|
jlib:iq_to_xml(IQ#iq{id = ID});
|
|
|
|
true ->
|
|
|
|
jlib:iq_to_xml(IQ)
|
|
|
|
end,
|
|
|
|
ejabberd_router:route(From, To, Packet).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
register_iq_response_handler(Host, ID, Module,
|
|
|
|
Function) ->
|
|
|
|
register_iq_response_handler(Host, ID, Module, Function,
|
|
|
|
undefined).
|
2011-02-21 13:44:30 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
register_iq_response_handler(_Host, ID, Module,
|
|
|
|
Function, Timeout0) ->
|
2011-02-21 13:44:30 +01:00
|
|
|
Timeout = case Timeout0 of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined -> ?IQ_TIMEOUT;
|
|
|
|
N when is_integer(N), N > 0 -> N
|
2011-02-21 13:44:30 +01:00
|
|
|
end,
|
|
|
|
TRef = erlang:start_timer(Timeout, ejabberd_local, ID),
|
2009-08-13 11:59:31 +02:00
|
|
|
mnesia:dirty_write(#iq_response{id = ID,
|
|
|
|
module = Module,
|
|
|
|
function = Function,
|
|
|
|
timer = TRef}).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
register_iq_handler(Host, XMLNS, Module, Fun) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_local !
|
|
|
|
{register_iq_handler, Host, XMLNS, Module, Fun}.
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
register_iq_handler(Host, XMLNS, Module, Fun, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_local !
|
|
|
|
{register_iq_handler, Host, XMLNS, Module, Fun, Opts}.
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
unregister_iq_response_handler(_Host, ID) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
catch get_iq_callback(ID), ok.
|
2008-07-03 11:56:31 +02:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
unregister_iq_handler(Host, XMLNS) ->
|
|
|
|
ejabberd_local ! {unregister_iq_handler, Host, XMLNS}.
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2004-07-10 00:34:26 +02:00
|
|
|
refresh_iq_handlers() ->
|
|
|
|
ejabberd_local ! refresh_iq_handlers.
|
2003-10-19 18:19:55 +02:00
|
|
|
|
2004-08-13 00:34:19 +02:00
|
|
|
bounce_resource_packet(From, To, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_ITEM_NOT_FOUND),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_router:route(To, From, Err),
|
|
|
|
stop.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
init([]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (Host) ->
|
|
|
|
ejabberd_router:register_route(Host,
|
|
|
|
{apply, ?MODULE,
|
|
|
|
route}),
|
|
|
|
ejabberd_hooks:add(local_send_to_resource_hook, Host,
|
|
|
|
?MODULE, bounce_resource_packet,
|
|
|
|
100)
|
|
|
|
end,
|
|
|
|
?MYHOSTS),
|
2006-01-29 05:38:31 +01:00
|
|
|
catch ets:new(?IQTABLE, [named_table, public]),
|
2009-07-30 10:58:21 +02:00
|
|
|
update_table(),
|
2007-12-01 06:16:30 +01:00
|
|
|
mnesia:create_table(iq_response,
|
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, iq_response)}]),
|
2008-03-25 23:23:38 +01:00
|
|
|
mnesia:add_table_copy(iq_response, node(), ram_copies),
|
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}.
|
|
|
|
|
|
|
|
handle_cast(_Msg, State) -> {noreply, State}.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ -> ok
|
2006-01-29 05:38:31 +01:00
|
|
|
end,
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({register_iq_handler, Host, XMLNS, Module,
|
|
|
|
Function},
|
|
|
|
State) ->
|
2006-01-29 05:38:31 +01:00
|
|
|
ets:insert(?IQTABLE, {{XMLNS, Host}, Module, Function}),
|
|
|
|
catch mod_disco:register_feature(Host, XMLNS),
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({register_iq_handler, Host, XMLNS, Module,
|
|
|
|
Function, Opts},
|
|
|
|
State) ->
|
|
|
|
ets:insert(?IQTABLE,
|
|
|
|
{{XMLNS, Host}, Module, Function, Opts}),
|
2006-01-29 05:38:31 +01:00
|
|
|
catch mod_disco:register_feature(Host, XMLNS),
|
|
|
|
{noreply, State};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({unregister_iq_handler, Host, XMLNS},
|
|
|
|
State) ->
|
2006-01-29 05:38:31 +01:00
|
|
|
case ets:lookup(?IQTABLE, {XMLNS, Host}) 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,
|
|
|
|
ets:delete(?IQTABLE, {XMLNS, Host}),
|
|
|
|
catch mod_disco:unregister_feature(Host, XMLNS),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(refresh_iq_handlers, State) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foreach(fun (T) ->
|
|
|
|
case T of
|
|
|
|
{{XMLNS, Host}, _Module, _Function, _Opts} ->
|
|
|
|
catch mod_disco:register_feature(Host, XMLNS);
|
|
|
|
{{XMLNS, Host}, _Module, _Function} ->
|
|
|
|
catch mod_disco:register_feature(Host, XMLNS);
|
|
|
|
_ -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
ets:tab2list(?IQTABLE)),
|
2006-01-29 05:38:31 +01:00
|
|
|
{noreply, State};
|
2009-07-30 10:58:21 +02:00
|
|
|
handle_info({timeout, _TRef, ID}, State) ->
|
|
|
|
process_iq_timeout(ID),
|
|
|
|
{noreply, State};
|
2015-10-07 00:06:58 +02:00
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-10-07 00:06:58 +02:00
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
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
|
|
|
|
%%--------------------------------------------------------------------
|
2006-01-29 05:38:31 +01:00
|
|
|
do_route(From, To, Packet) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
?DEBUG("local route~n\tfrom ~p~n\tto ~p~n\tpacket "
|
|
|
|
"~P~n",
|
2006-01-29 05:38:31 +01:00
|
|
|
[From, To, Packet, 8]),
|
2013-03-14 10:33:02 +01:00
|
|
|
if To#jid.luser /= <<"">> ->
|
|
|
|
ejabberd_sm:route(From, To, Packet);
|
|
|
|
To#jid.lresource == <<"">> ->
|
|
|
|
#xmlel{name = Name} = Packet,
|
|
|
|
case Name of
|
|
|
|
<<"iq">> -> process_iq(From, To, Packet);
|
|
|
|
<<"message">> -> ok;
|
|
|
|
<<"presence">> -> ok;
|
|
|
|
_ -> ok
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
#xmlel{attrs = Attrs} = Packet,
|
|
|
|
case xml:get_attr_s(<<"type">>, Attrs) of
|
|
|
|
<<"error">> -> ok;
|
|
|
|
<<"result">> -> ok;
|
|
|
|
_ ->
|
|
|
|
ejabberd_hooks:run(local_send_to_resource_hook,
|
|
|
|
To#jid.lserver, [From, To, Packet])
|
|
|
|
end
|
|
|
|
end.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
2009-07-30 10:58:21 +02:00
|
|
|
update_table() ->
|
|
|
|
case catch mnesia:table_info(iq_response, attributes) of
|
|
|
|
[id, module, function] ->
|
|
|
|
mnesia:delete_table(iq_response);
|
|
|
|
[id, module, function, timer] ->
|
|
|
|
ok;
|
|
|
|
{'EXIT', _} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_iq_callback(ID) ->
|
|
|
|
case mnesia:dirty_read(iq_response, ID) of
|
|
|
|
[#iq_response{module = Module, timer = TRef,
|
|
|
|
function = Function}] ->
|
|
|
|
cancel_timer(TRef),
|
|
|
|
mnesia:dirty_delete(iq_response, ID),
|
|
|
|
{ok, Module, Function};
|
|
|
|
_ ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_iq_timeout(ID) ->
|
|
|
|
spawn(fun process_iq_timeout/0) ! ID.
|
|
|
|
|
|
|
|
process_iq_timeout() ->
|
|
|
|
receive
|
|
|
|
ID ->
|
|
|
|
case get_iq_callback(ID) of
|
|
|
|
{ok, undefined, Function} ->
|
|
|
|
Function(timeout);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
after 5000 ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
cancel_timer(TRef) ->
|
|
|
|
case erlang:cancel_timer(TRef) of
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
|
|
|
receive {timeout, TRef, _} -> ok after 0 -> ok end;
|
|
|
|
_ -> ok
|
2009-07-30 10:58:21 +02:00
|
|
|
end.
|