2003-01-22 21:40:40 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : gen_iq_handler.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2006-01-29 05:38:31 +01:00
|
|
|
%%% Purpose : IQ handler support
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 22 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-01-15 18:02:57 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 Process-one
|
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.
|
|
|
|
%%%
|
|
|
|
%%% 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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-22 21:40:40 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(gen_iq_handler).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/3,
|
2005-06-20 05:18:13 +02:00
|
|
|
add_iq_handler/6,
|
|
|
|
remove_iq_handler/3,
|
2003-01-29 18:12:23 +01:00
|
|
|
stop_iq_handler/3,
|
2005-06-20 05:18:13 +02:00
|
|
|
handle/7,
|
2006-01-29 05:38:31 +01:00
|
|
|
process_iq/6]).
|
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2008-07-01 12:21:35 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-01-22 21:40:40 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
-record(state, {host,
|
|
|
|
module,
|
|
|
|
function}).
|
|
|
|
|
2008-07-01 12:21:35 +02:00
|
|
|
% XXX OLD FORMAT: Re-include jlib.hrl (after clean-up).
|
|
|
|
-record(iq, {id = "",
|
|
|
|
type,
|
|
|
|
xmlns = "",
|
|
|
|
lang = "",
|
|
|
|
sub_el}).
|
|
|
|
|
2008-07-21 17:28:44 +02:00
|
|
|
% XXX OLD FORMAT: modules not in the following list will receive
|
|
|
|
% old format strudctures.
|
|
|
|
-define(CONVERTED_MODULES, [
|
2008-07-25 16:26:59 +02:00
|
|
|
mod_adhoc,
|
|
|
|
mod_annouce,
|
2008-07-22 16:51:19 +02:00
|
|
|
mod_caps,
|
2008-07-25 16:26:59 +02:00
|
|
|
mod_configure,
|
2008-08-06 15:44:58 +02:00
|
|
|
mod_configure2,
|
2008-07-22 16:03:11 +02:00
|
|
|
mod_disco,
|
2008-08-06 15:44:58 +02:00
|
|
|
mod_echo,
|
|
|
|
mod_offline,
|
|
|
|
mod_offline_odbc,
|
2008-07-21 17:30:32 +02:00
|
|
|
mod_roster,
|
|
|
|
mod_vcard
|
2008-07-21 17:28:44 +02:00
|
|
|
]).
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start_link(Host, Module, Function) ->
|
|
|
|
gen_server:start_link(?MODULE, [Host, Module, Function], []).
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
add_iq_handler(Component, Host, NS, Module, Function, Type) ->
|
2003-01-22 21:40:40 +01:00
|
|
|
case Type of
|
|
|
|
no_queue ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Component:register_iq_handler(Host, NS, Module, Function, no_queue);
|
2003-01-22 21:40:40 +01:00
|
|
|
one_queue ->
|
2003-07-20 22:35:35 +02:00
|
|
|
{ok, Pid} = supervisor:start_child(ejabberd_iq_sup,
|
2005-06-20 05:18:13 +02:00
|
|
|
[Host, Module, Function]),
|
|
|
|
Component:register_iq_handler(Host, NS, Module, Function,
|
2003-01-22 21:40:40 +01:00
|
|
|
{one_queue, Pid});
|
2006-03-14 05:26:15 +01:00
|
|
|
{queues, N} ->
|
|
|
|
Pids =
|
|
|
|
lists:map(
|
|
|
|
fun(_) ->
|
|
|
|
{ok, Pid} = supervisor:start_child(
|
|
|
|
ejabberd_iq_sup,
|
|
|
|
[Host, Module, Function]),
|
|
|
|
Pid
|
|
|
|
end, lists:seq(1, N)),
|
|
|
|
Component:register_iq_handler(Host, NS, Module, Function,
|
|
|
|
{queues, Pids});
|
2003-01-22 21:40:40 +01:00
|
|
|
parallel ->
|
2005-06-20 05:18:13 +02:00
|
|
|
Component:register_iq_handler(Host, NS, Module, Function, parallel)
|
2003-01-22 21:40:40 +01:00
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
remove_iq_handler(Component, Host, NS) ->
|
2005-10-16 22:42:24 +02:00
|
|
|
Component:unregister_iq_handler(Host, NS).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
stop_iq_handler(_Module, _Function, Opts) ->
|
2003-01-29 18:12:23 +01:00
|
|
|
case Opts of
|
|
|
|
{one_queue, Pid} ->
|
2006-01-29 05:38:31 +01:00
|
|
|
gen_server:call(Pid, stop);
|
2006-03-14 05:26:15 +01:00
|
|
|
{queues, Pids} ->
|
|
|
|
lists:foreach(fun(Pid) ->
|
|
|
|
catch gen_server:call(Pid, stop)
|
|
|
|
end, Pids);
|
2003-01-29 18:12:23 +01:00
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
handle(Host, Module, Function, Opts, From, To, IQ) ->
|
2003-01-22 21:40:40 +01:00
|
|
|
case Opts of
|
|
|
|
no_queue ->
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq(Host, Module, Function, From, To, IQ);
|
2003-01-22 21:40:40 +01:00
|
|
|
{one_queue, Pid} ->
|
2008-07-21 17:28:44 +02:00
|
|
|
Pid ! {process_iq, From, To, IQ};
|
2006-03-14 05:26:15 +01:00
|
|
|
{queues, Pids} ->
|
|
|
|
Pid = lists:nth(erlang:phash(now(), length(Pids)), Pids),
|
2008-07-21 17:28:44 +02:00
|
|
|
Pid ! {process_iq, From, To, IQ};
|
2003-01-22 21:40:40 +01:00
|
|
|
parallel ->
|
2008-07-21 17:28:44 +02:00
|
|
|
spawn(?MODULE, process_iq,
|
|
|
|
[Host, Module, Function, From, To, IQ]);
|
2003-01-22 21:40:40 +01:00
|
|
|
_ ->
|
|
|
|
todo
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2008-07-21 17:28:44 +02:00
|
|
|
process_iq(Host, Module, Function, FromOld, ToOld, IQ_Rec)
|
|
|
|
when is_record(IQ_Rec, iq) ->
|
|
|
|
catch throw(for_stacktrace), % To have a stacktrace.
|
|
|
|
io:format("~nIQ HANDLER: old #iq for ~s:~s:~n~p~n~p~n~n",
|
|
|
|
[Module, Function, IQ_Rec, erlang:get_stacktrace()]),
|
|
|
|
From = jlib:from_old_jid(FromOld),
|
|
|
|
To = jlib:from_old_jid(ToOld),
|
|
|
|
IQOld = jlib:iq_to_xml(IQ_Rec),
|
|
|
|
IQOld1 = IQOld#xmlelement{children = [IQOld#xmlelement.children]},
|
|
|
|
IQ = exmpp_xml:xmlelement_to_xmlel(IQOld1,
|
|
|
|
[?NS_JABBER_CLIENT],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx},
|
|
|
|
{?NS_DIALBACK, ?NS_DIALBACK_pfx}]),
|
|
|
|
process_iq(Host, Module, Function, From, To, IQ);
|
2005-06-20 05:18:13 +02:00
|
|
|
process_iq(_Host, Module, Function, From, To, IQ) ->
|
2008-07-21 17:28:44 +02:00
|
|
|
Ret = case lists:member(Module, ?CONVERTED_MODULES) of
|
|
|
|
true ->
|
|
|
|
catch Module:Function(From, To, IQ);
|
|
|
|
false ->
|
|
|
|
{FromOld, ToOld, IQ_Rec} = convert_to_old_structs(
|
|
|
|
Module, Function, From, To, IQ),
|
|
|
|
catch Module:Function(FromOld, ToOld, IQ_Rec)
|
|
|
|
end,
|
|
|
|
case Ret of
|
2003-01-22 21:40:40 +01:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
2008-07-21 17:28:44 +02:00
|
|
|
ignore ->
|
|
|
|
ok;
|
|
|
|
ResIQ when is_record(ResIQ, iq) ->
|
|
|
|
ReplyOld = jlib:iq_to_xml(ResIQ),
|
|
|
|
Reply = exmpp_xml:xmlelement_to_xmlel(ReplyOld,
|
|
|
|
[?NS_JABBER_CLIENT],
|
|
|
|
[{?NS_XMPP, ?NS_XMPP_pfx},
|
|
|
|
{?NS_DIALBACK, ?NS_DIALBACK_pfx}]),
|
|
|
|
ejabberd_router:route(To, From, Reply);
|
|
|
|
Reply ->
|
|
|
|
ejabberd_router:route(To, From, Reply)
|
2003-01-22 21:40:40 +01:00
|
|
|
end.
|
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([Host, Module, Function]) ->
|
|
|
|
{ok, #state{host = Host,
|
|
|
|
module = Module,
|
|
|
|
function = Function}}.
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2006-01-29 05:38:31 +01:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% 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(stop, _From, State) ->
|
|
|
|
Reply = ok,
|
|
|
|
{stop, normal, 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({process_iq, From, To, IQ},
|
|
|
|
#state{host = Host,
|
|
|
|
module = Module,
|
|
|
|
function = Function} = State) ->
|
|
|
|
process_iq(Host, Module, Function, From, To, IQ),
|
|
|
|
{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
|
|
|
|
%%--------------------------------------------------------------------
|
2008-07-01 12:21:35 +02:00
|
|
|
|
2008-07-21 17:28:44 +02:00
|
|
|
convert_to_old_structs(Mod, Fun, From, To, IQ) ->
|
|
|
|
catch throw(for_stacktrace), % To have a stacktrace.
|
|
|
|
io:format("~nIQ HANDLER: ~s:~s expects old #iq:~n~p~n~p~n~n",
|
|
|
|
[Mod, Fun, IQ, erlang:get_stacktrace()]),
|
2008-07-01 12:21:35 +02:00
|
|
|
if
|
2008-07-21 17:28:44 +02:00
|
|
|
is_record(IQ, iq) ->
|
|
|
|
{From, To, IQ};
|
|
|
|
true ->
|
|
|
|
F = jlib:to_old_jid(From),
|
|
|
|
T = jlib:to_old_jid(To),
|
|
|
|
I_Rec = jlib:iq_query_info(IQ),
|
|
|
|
{F, T, I_Rec}
|
2008-07-01 12:21:35 +02:00
|
|
|
end.
|