[ejabberd] IQ Handler in external component

Badlop badlop at gmail.com
Tue Jun 9 02:14:57 MSD 2009

2009/6/7 Gustavo Garcia Bernardo <gustavogb at gmail.com>:
> If not, could it be possible, as a workaround, to create a simple
> internal IQ handler to redirect IQ messages with a specific namespace to
> the server external component?

I have a module that sends to a user JID. I didn't try to forward to a

This module with that setup forwards IQ stanzas that have namespace
uri:xxx to testuser at localhost/Home:

%% Example configuration:
%% {mod_iq_forward, [{forward_rules, [{"uri:xxx",
"testuser at localhost/Home"}] }] }



-export([start/2, stop/1, process_local_iq/3]).


start(Host, Opts) ->
    IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
    ForwardRules = gen_mod:get_opt(forward_rules, Opts, []),
    [gen_iq_handler:add_iq_handler(ejabberd_local, Host, NS,
				   ?MODULE, process_local_iq, IQDisc)
     || {NS, _Destination} <- ForwardRules].

stop(Host) ->
    ForwardRules = get_forward_rules(Host),
    [gen_iq_handler:remove_iq_handler(ejabberd_local, Host, NS)
     || {NS, _Destination} <- ForwardRules].

get_forward_rules(Host) ->
    gen_mod:get_module_opt(Host, ?MODULE, forward_rules, []).

get_forward_rules(Host, NS) ->
    proplists:get_value(NS, get_forward_rules(Host), undefined).

process_local_iq(From, To, #iq{xmlns = XMLNS, sub_el = SubEl} = IQ) ->
    case get_forward_rules(To#jid.server, XMLNS) of
	undefined ->
	Dest when is_list(Dest) ->
				  jlib:iq_to_xml(IQ#iq{sub_el = [SubEl]}))
    IQ#iq{type = result, sub_el = [SubEl]}.


More information about the ejabberd mailing list