2002-11-30 19:46:16 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_local.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2004-11-05 22:14:31 +01:00
|
|
|
%%% Purpose : Route local packets
|
2002-11-30 19:46:16 +01:00
|
|
|
%%% Created : 30 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_local).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/0]).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
-export([route/3,
|
2003-01-29 18:12:23 +01:00
|
|
|
register_iq_handler/4,
|
2005-06-20 05:18:13 +02:00
|
|
|
register_iq_handler/5,
|
|
|
|
unregister_iq_handler/2,
|
2004-08-13 00:34:19 +02:00
|
|
|
refresh_iq_handlers/0,
|
|
|
|
bounce_resource_packet/3
|
2003-01-29 18:12:23 +01:00
|
|
|
]).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
2002-11-30 19:46:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
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, {}).
|
|
|
|
|
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
|
|
|
|
%%--------------------------------------------------------------------
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
2006-01-29 05:38:31 +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
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{xmlns = XMLNS} ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Host = To#jid.lserver,
|
|
|
|
case ets:lookup(?IQTABLE, {XMLNS, Host}) of
|
2003-12-17 21:13:21 +01:00
|
|
|
[{_, Module, Function}] ->
|
|
|
|
ResIQ = Module:Function(From, To, IQ),
|
|
|
|
if
|
|
|
|
ResIQ /= ignore ->
|
|
|
|
ejabberd_router:route(
|
|
|
|
To, From, jlib:iq_to_xml(ResIQ));
|
|
|
|
true ->
|
|
|
|
ok
|
2002-12-11 21:57:45 +01:00
|
|
|
end;
|
2003-12-17 21:13:21 +01:00
|
|
|
[{_, Module, Function, Opts}] ->
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:handle(Host, Module, Function, Opts,
|
2003-12-17 21:13:21 +01:00
|
|
|
From, To, IQ);
|
|
|
|
[] ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERR_FEATURE_NOT_IMPLEMENTED),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
2002-12-11 21:57:45 +01:00
|
|
|
end;
|
2002-12-17 21:49:45 +01:00
|
|
|
reply ->
|
|
|
|
ok;
|
2002-12-11 21:57:45 +01:00
|
|
|
_ ->
|
2003-03-09 21:46:47 +01:00
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_BAD_REQUEST),
|
2003-10-19 18:19:55 +02:00
|
|
|
ejabberd_router:route(To, From, Err),
|
2002-12-11 21:57:45 +01:00
|
|
|
ok
|
|
|
|
end.
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2004-11-05 22:14:31 +01:00
|
|
|
route(From, To, Packet) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
register_iq_handler(Host, XMLNS, Module, Fun) ->
|
|
|
|
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) ->
|
|
|
|
ejabberd_local ! {register_iq_handler, Host, XMLNS, Module, Fun, Opts}.
|
2003-01-29 18:12:23 +01: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) ->
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_ITEM_NOT_FOUND),
|
|
|
|
ejabberd_router:route(To, From, Err),
|
|
|
|
stop.
|
2006-01-29 05:38:31 +01:00
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([]) ->
|
|
|
|
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),
|
|
|
|
catch ets:new(?IQTABLE, [named_table, public]),
|
|
|
|
{ok, #state{}}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling call messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{reply, Reply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_info({route, From, To, Packet}, State) ->
|
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({register_iq_handler, Host, XMLNS, Module, Function}, State) ->
|
|
|
|
ets:insert(?IQTABLE, {{XMLNS, Host}, Module, Function}),
|
|
|
|
catch mod_disco:register_feature(Host, XMLNS),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({register_iq_handler, Host, XMLNS, Module, Function, Opts}, State) ->
|
|
|
|
ets:insert(?IQTABLE, {{XMLNS, Host}, Module, Function, Opts}),
|
|
|
|
catch mod_disco:register_feature(Host, XMLNS),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info({unregister_iq_handler, Host, XMLNS}, State) ->
|
|
|
|
case ets:lookup(?IQTABLE, {XMLNS, Host}) of
|
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:stop_iq_handler(Module, Function, Opts);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ets:delete(?IQTABLE, {XMLNS, Host}),
|
|
|
|
catch mod_disco:unregister_feature(Host, XMLNS),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(refresh_iq_handlers, State) ->
|
|
|
|
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)),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: terminate(Reason, State) -> void()
|
|
|
|
%% Description: This function is called by a gen_server when it is about to
|
|
|
|
%% terminate. It should be the opposite of Module:init/1 and do any necessary
|
|
|
|
%% cleaning up. When it returns, the gen_server terminates with Reason.
|
|
|
|
%% The return value is ignored.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
terminate(_Reason, _State) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
do_route(From, To, Packet) ->
|
|
|
|
?DEBUG("local route~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n",
|
|
|
|
[From, To, Packet, 8]),
|
|
|
|
if
|
|
|
|
To#jid.luser /= "" ->
|
|
|
|
ejabberd_sm:route(From, To, Packet);
|
|
|
|
To#jid.lresource == "" ->
|
|
|
|
{xmlelement, Name, _Attrs, _Els} = Packet,
|
|
|
|
case Name of
|
|
|
|
"iq" ->
|
|
|
|
process_iq(From, To, Packet);
|
|
|
|
"message" ->
|
|
|
|
ok;
|
|
|
|
"presence" ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
true ->
|
|
|
|
{xmlelement, _Name, Attrs, _Els} = 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.
|
|
|
|
|