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,
|
|
|
|
unregister_iq_handler/1
|
|
|
|
]).
|
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]),
|
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}),
|
2003-01-01 20:54:44 +01:00
|
|
|
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}),
|
|
|
|
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),
|
|
|
|
mod_disco:unregister_feature(XMLNS),
|
|
|
|
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;
|
|
|
|
_ ->
|
2003-10-19 18:19:55 +02:00
|
|
|
case {Res, Name} of
|
|
|
|
{"announce/online", "message"} ->
|
|
|
|
announce_online(From, To, Packet);
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERR_ITEM_NOT_FOUND),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
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}.
|
|
|
|
|
2003-10-19 18:19:55 +02:00
|
|
|
|
|
|
|
announce_online(From, To, Packet) ->
|
|
|
|
case acl:match_rule(announce, From) of
|
|
|
|
deny ->
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_NOT_ALLOWED),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
allow ->
|
|
|
|
Local = jlib:make_jid("", ?MYNAME, ""),
|
|
|
|
lists:foreach(
|
|
|
|
fun({U, R}) ->
|
|
|
|
Dest = jlib:make_jid(U, ?MYNAME, R),
|
|
|
|
ejabberd_router:route(Local, Dest, Packet)
|
|
|
|
end, ejabberd_sm:dirty_get_sessions_list())
|
|
|
|
end.
|
|
|
|
|