From 094fa47f9f0e10d65d45a9e2101c7f7ae614de01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20P=C3=A9dron?= Date: Tue, 1 Jul 2008 10:21:35 +0000 Subject: [PATCH] Convert gen_iq_handler to exmpp. SVN Revision: 1401 --- ChangeLog | 5 +++++ src/ejabberd_c2s.erl | 7 ++----- src/ejabberd_sm.erl | 2 +- src/gen_iq_handler.erl | 47 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index a73436b6c..13c5f29f5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -26,6 +26,11 @@ * src/ejabberd_router.erl: Use a function to convert to old structures instead of duplicating code inside the module. + * src/gen_iq_handler.erl: Convert to exmpp. + + * src/ejabberd_sm.erl, src/ejabberd_c2s.erl: Don't convert + before calling gen_iq_handler:handle/7. + 2008-06-30 Jean-Sébastien Pédron * src/Makefile.in: Remove the -I flag for exmpp includes; the diff --git a/src/ejabberd_c2s.erl b/src/ejabberd_c2s.erl index 8cc98e0f2..140e23378 100644 --- a/src/ejabberd_c2s.erl +++ b/src/ejabberd_c2s.erl @@ -1158,11 +1158,8 @@ handle_info({route, FromOld, ToOld, PacketOld}, StateName, StateData) -> % for namespaces. case ets:lookup(sm_iqtable, {atom_to_list(?NS_VCARD), Host}) of [{_, Module, Function, Opts}] -> - % XXX OLD FORMAT: IQ is an old #iq, - % From, To. - IQ = jlib:iq_query_info(PacketOld), gen_iq_handler:handle(Host, Module, Function, Opts, - FromOld, ToOld, IQ); + From, To, Packet); [] -> Res = exmpp_iq:error(Packet, 'feature-not-implemented'), ejabberd_router:route(To, From, Res) @@ -1935,7 +1932,7 @@ resend_subscription_requests(#state{user = User, process_unauthenticated_stanza(StateData, El) -> ElOld = exmpp_xml:xmlel_to_xmlelement(El, [?DEFAULT_NS], ?PREFIXED_NS), case jlib:iq_query_info(ElOld) of - #iq{} = IQ -> + IQ when is_record(IQ, iq) -> ResOld = ejabberd_hooks:run_fold(c2s_unauthenticated_iq, StateData#state.server, empty, diff --git a/src/ejabberd_sm.erl b/src/ejabberd_sm.erl index 8ca59bfbc..bae6ce473 100644 --- a/src/ejabberd_sm.erl +++ b/src/ejabberd_sm.erl @@ -688,7 +688,7 @@ process_iq(From, To, Packet) -> [{_, Module, Function, Opts}] -> % XXX OLD FORMAT: From, To, IQ. gen_iq_handler:handle(Host, Module, Function, Opts, - FromOld, ToOld, IQ); + From, To, Packet); [] -> Err = exmpp_iq:error(Packet, 'service-unavailable'), ejabberd_router:route(To, From, Err) diff --git a/src/gen_iq_handler.erl b/src/gen_iq_handler.erl index 912f22c17..aeae283f7 100644 --- a/src/gen_iq_handler.erl +++ b/src/gen_iq_handler.erl @@ -41,12 +41,21 @@ -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). +-include_lib("exmpp/include/exmpp.hrl"). + -include("ejabberd.hrl"). -record(state, {host, module, function}). +% XXX OLD FORMAT: Re-include jlib.hrl (after clean-up). +-record(iq, {id = "", + type, + xmlns = "", + lang = "", + sub_el}). + %%==================================================================== %% API %%==================================================================== @@ -101,10 +110,12 @@ handle(Host, Module, Function, Opts, From, To, IQ) -> no_queue -> process_iq(Host, Module, Function, From, To, IQ); {one_queue, Pid} -> - Pid ! {process_iq, From, To, IQ}; + {FromOld, ToOld, IQ_Rec} = convert_to_old_structs(From, To, IQ), + Pid ! {process_iq, FromOld, ToOld, IQ_Rec}; {queues, Pids} -> Pid = lists:nth(erlang:phash(now(), length(Pids)), Pids), - Pid ! {process_iq, From, To, IQ}; + {FromOld, ToOld, IQ_Rec} = convert_to_old_structs(From, To, IQ), + Pid ! {process_iq, FromOld, ToOld, IQ_Rec}; parallel -> spawn(?MODULE, process_iq, [Host, Module, Function, From, To, IQ]); _ -> @@ -113,14 +124,20 @@ handle(Host, Module, Function, Opts, From, To, IQ) -> process_iq(_Host, Module, Function, From, To, IQ) -> - case catch Module:Function(From, To, IQ) of + {FromOld, ToOld, IQ_Rec} = convert_to_old_structs(From, To, IQ), + case catch Module:Function(FromOld, ToOld, IQ_Rec) of {'EXIT', Reason} -> ?ERROR_MSG("~p", [Reason]); ResIQ -> if ResIQ /= ignore -> + 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, - jlib:iq_to_xml(ResIQ)); + Reply); true -> ok end @@ -199,3 +216,25 @@ code_change(_OldVsn, State, _Extra) -> %%-------------------------------------------------------------------- %%% Internal functions %%-------------------------------------------------------------------- + +convert_to_old_structs(From, To, IQ) -> + if + is_record(IQ, iq) -> + catch throw(for_stacktrace), % To have a stacktrace. + io:format("~nIQ HANDLER: old #iq:~n~p~n~p~n~n", + [IQ, erlang:get_stacktrace()]), + {From, To, IQ}; + true -> + F = jlib:to_old_jid(From), + T = jlib:to_old_jid(To), + Default_NS = case lists:member({IQ#xmlel.ns, none}, + IQ#xmlel.declared_ns) of + true -> []; + false -> [IQ#xmlel.ns] + end, + IQOld = exmpp_xml:xmlel_to_xmlelement(IQ, + Default_NS, + [{?NS_XMPP, ?NS_XMPP_pfx}, {?NS_DIALBACK, ?NS_DIALBACK_pfx}]), + I = jlib:iq_query_info(IQOld), + {F, T, I} + end.