2002-11-30 19:46:16 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_local.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 30 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_local).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
-export([start_link/0, init/0]).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2003-01-22 21:40:40 +01:00
|
|
|
-export([register_iq_handler/3,
|
2003-01-29 18:12:23 +01:00
|
|
|
register_iq_handler/4,
|
2004-07-10 00:34:26 +02:00
|
|
|
unregister_iq_handler/1,
|
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
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
-record(state, {mydomain, iqtable}).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start_link() ->
|
|
|
|
register(ejabberd_local,
|
|
|
|
Pid = proc_lib:spawn_link(ejabberd_local, init, [])),
|
|
|
|
{ok, Pid}.
|
2002-11-30 19:46:16 +01:00
|
|
|
|
|
|
|
init() ->
|
2002-12-11 21:57:45 +01:00
|
|
|
MyDomain = ?MYNAME,
|
2003-12-14 21:51:01 +01:00
|
|
|
ejabberd_router:register_route(MyDomain),
|
2003-02-04 21:45:23 +01:00
|
|
|
catch ets:new(local_iqtable, [named_table, public]),
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_hooks:add(local_send_to_resource_hook,
|
|
|
|
?MODULE, bounce_resource_packet, 100),
|
2002-12-11 21:57:45 +01:00
|
|
|
loop(#state{mydomain = MyDomain,
|
2003-02-04 21:45:23 +01:00
|
|
|
iqtable = local_iqtable}).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
loop(State) ->
|
2002-11-30 19:46:16 +01:00
|
|
|
receive
|
|
|
|
{route, From, To, Packet} ->
|
2003-05-15 20:16:13 +02:00
|
|
|
case catch do_route(State, From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
2003-10-29 21:09:09 +01:00
|
|
|
?ERROR_MSG("~p~nwhen processing: ~p",
|
|
|
|
[Reason, {From, To, Packet}]);
|
2003-05-15 20:16:13 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2002-12-11 21:57:45 +01:00
|
|
|
loop(State);
|
|
|
|
{register_iq_handler, XMLNS, Module, Function} ->
|
|
|
|
ets:insert(State#state.iqtable, {XMLNS, Module, Function}),
|
2004-07-10 00:34:26 +02:00
|
|
|
catch mod_disco:register_feature(XMLNS),
|
2003-01-22 21:40:40 +01:00
|
|
|
loop(State);
|
|
|
|
{register_iq_handler, XMLNS, Module, Function, Opts} ->
|
|
|
|
ets:insert(State#state.iqtable, {XMLNS, Module, Function, Opts}),
|
2004-07-10 00:34:26 +02:00
|
|
|
catch mod_disco:register_feature(XMLNS),
|
2003-01-29 18:12:23 +01:00
|
|
|
loop(State);
|
|
|
|
{unregister_iq_handler, XMLNS} ->
|
|
|
|
case ets:lookup(State#state.iqtable, XMLNS) of
|
|
|
|
[{_, Module, Function, Opts}] ->
|
|
|
|
gen_iq_handler:stop_iq_handler(Module, Function, Opts);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
ets:delete(State#state.iqtable, XMLNS),
|
2004-07-10 00:34:26 +02:00
|
|
|
catch mod_disco:unregister_feature(XMLNS),
|
|
|
|
loop(State);
|
|
|
|
refresh_iq_handlers ->
|
2004-07-11 22:51:54 +02:00
|
|
|
lists:foreach(
|
2004-07-10 00:34:26 +02:00
|
|
|
fun(T) ->
|
2004-07-11 22:51:54 +02:00
|
|
|
case T of
|
|
|
|
{XMLNS, _Module, _Function, _Opts} ->
|
|
|
|
catch mod_disco:register_feature(XMLNS);
|
|
|
|
{XMLNS, _Module, _Function} ->
|
|
|
|
catch mod_disco:register_feature(XMLNS);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end
|
2004-07-10 00:34:26 +02:00
|
|
|
end, ets:tab2list(State#state.iqtable)),
|
2003-01-29 18:12:23 +01:00
|
|
|
loop(State);
|
|
|
|
_ ->
|
2002-12-11 21:57:45 +01:00
|
|
|
loop(State)
|
2002-11-30 19:46:16 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
do_route(State, From, To, Packet) ->
|
2002-11-30 19:46:16 +01:00
|
|
|
?DEBUG("local route~n\tfrom ~p~n\tto ~p~n\tpacket ~P~n",
|
|
|
|
[From, To, Packet, 8]),
|
|
|
|
case To of
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{luser = "", lresource = ""} ->
|
2003-10-19 18:19:55 +02:00
|
|
|
{xmlelement, Name, Attrs, _Els} = Packet,
|
2002-12-11 21:57:45 +01:00
|
|
|
case Name of
|
|
|
|
"iq" ->
|
|
|
|
process_iq(State, From, To, Packet);
|
|
|
|
"message" ->
|
|
|
|
ok;
|
|
|
|
"presence" ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
2003-10-19 18:19:55 +02:00
|
|
|
#jid{luser = "", lresource = Res} ->
|
|
|
|
{xmlelement, Name, Attrs, _Els} = Packet,
|
2003-10-14 21:34:17 +02:00
|
|
|
case xml:get_attr_s("type", Attrs) of
|
|
|
|
"error" -> ok;
|
|
|
|
"result" -> ok;
|
|
|
|
_ ->
|
2004-08-13 00:34:19 +02:00
|
|
|
ejabberd_hooks:run(local_send_to_resource_hook,
|
|
|
|
[From, To, Packet])
|
2003-10-14 21:34:17 +02:00
|
|
|
end;
|
2002-11-30 19:46:16 +01:00
|
|
|
_ ->
|
|
|
|
ejabberd_sm ! {route, From, To, Packet}
|
2002-12-11 21:57:45 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
process_iq(State, From, To, Packet) ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
2003-12-17 21:13:21 +01:00
|
|
|
#iq{xmlns = XMLNS} ->
|
|
|
|
case ets:lookup(State#state.iqtable, XMLNS) of
|
|
|
|
[{_, 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}] ->
|
|
|
|
gen_iq_handler:handle(Module, Function, Opts,
|
|
|
|
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
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
register_iq_handler(XMLNS, Module, Fun) ->
|
|
|
|
ejabberd_local ! {register_iq_handler, XMLNS, Module, Fun}.
|
2003-01-22 21:40:40 +01:00
|
|
|
|
|
|
|
register_iq_handler(XMLNS, Module, Fun, Opts) ->
|
|
|
|
ejabberd_local ! {register_iq_handler, XMLNS, Module, Fun, Opts}.
|
2003-01-29 18:12:23 +01:00
|
|
|
|
|
|
|
unregister_iq_handler(XMLNS) ->
|
|
|
|
ejabberd_local ! {unregister_iq_handler, XMLNS}.
|
|
|
|
|
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.
|