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$ ').
|
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
-export([start/0, init/0]).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
-export([register_iq_handler/3]).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2002-12-11 21:57:45 +01:00
|
|
|
-record(state, {mydomain, iqtable}).
|
2002-11-30 19:46:16 +01:00
|
|
|
|
|
|
|
start() ->
|
2002-12-11 21:57:45 +01:00
|
|
|
register(ejabberd_local, spawn(ejabberd_local, init, [])),
|
|
|
|
mod_register:start(),
|
2002-12-13 21:58:27 +01:00
|
|
|
mod_roster:start(),
|
2002-12-11 21:57:45 +01:00
|
|
|
ok.
|
2002-11-30 19:46:16 +01:00
|
|
|
|
|
|
|
init() ->
|
2002-12-11 21:57:45 +01:00
|
|
|
MyDomain = ?MYNAME,
|
|
|
|
ejabberd_router:register_local_route(MyDomain),
|
|
|
|
loop(#state{mydomain = MyDomain,
|
|
|
|
iqtable = ets:new(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} ->
|
2002-12-11 21:57:45 +01:00
|
|
|
do_route(State, From, To, Packet),
|
|
|
|
loop(State);
|
|
|
|
{register_iq_handler, XMLNS, Module, Function} ->
|
|
|
|
ets:insert(State#state.iqtable, {XMLNS, Module, Function}),
|
|
|
|
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
|
2002-12-11 21:57:45 +01:00
|
|
|
{"", _, ""} ->
|
|
|
|
{xmlelement, Name, Attrs, Els} = Packet,
|
|
|
|
case Name of
|
|
|
|
"iq" ->
|
|
|
|
process_iq(State, From, To, Packet);
|
|
|
|
"message" ->
|
|
|
|
ok;
|
|
|
|
"presence" ->
|
|
|
|
% TODO
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
% DROP
|
|
|
|
ok
|
|
|
|
end;
|
2002-11-30 19:46:16 +01:00
|
|
|
{"", _, _} ->
|
2002-12-11 21:57:45 +01:00
|
|
|
Err = jlib:make_error_reply(Packet, "404", "Not Found"),
|
|
|
|
ejabberd_router ! {route,
|
|
|
|
{"", State#state.mydomain, ""}, From, Err},
|
2002-11-30 19:46:16 +01:00
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
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
|
|
|
|
{iq, ID, Type, XMLNS, SubEl} ->
|
|
|
|
case jlib:is_iq_request_type(Type) of
|
|
|
|
true ->
|
|
|
|
% TODO
|
|
|
|
case ets:lookup(State#state.iqtable, XMLNS) of
|
|
|
|
[{_, Module, Function}] ->
|
|
|
|
ResIQ = apply(Module, Function, [From, To, IQ]),
|
|
|
|
if
|
|
|
|
ResIQ /= ignore ->
|
|
|
|
ejabberd_router ! {route,
|
|
|
|
To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(ResIQ)};
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
[] ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, "501", "Not Implemented"),
|
|
|
|
ejabberd_router ! {route,
|
|
|
|
{"", State#state.mydomain, ""},
|
|
|
|
From,
|
|
|
|
Err}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
% TODO
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet, "400", "Bad Request"),
|
|
|
|
ejabberd_router ! {route,
|
|
|
|
{"", State#state.mydomain, ""}, From, Err},
|
|
|
|
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}.
|