Looking for a simple IQ handler module template

I have a small set of custom IQ stanzas I'd like to handle properly, from what I understand in exploring the site and documentation is that I need to have a module (handler) for each of these IQs I would like to support? Is that correct?

I have also stumbled across this "sample" template and I'm wondering if there's any additional detail or suggestions on how to proceed.

-module(mod_iqtest).
-behaviour(gen_mod).

-export([
            start/2, stop/1,
            process_sm_iq/3 %% I assume this is needed to handle JID to JID communication of the IQ?
    ]).

-include("ejabberd.hrl").
-include("jlib.hrl").

-define(NS_TEST, "http://jabber.org/protocol/test").

start(Host, Opt) ->
    IQDisc = gen_mod:get_opt(iqdisc, Opt, one_queue),
    gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_TEST, ?MODULE, process_sm_iq, IQDisc),
    ?INFO_MSG("Loading module 'mod_iqtest' v.01", []).

stop(Host) ->
    gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_TEST),
    ?INFO_MSG("Stoping module 'mod_iqtest' ", []).

process_sm_iq(From, To, #iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
?INFO_MSG("process_sm_iq -> ", [From, To, Lang, SubEl]),
    case {Type, SubEl} of
        {get, {xmlelement, "test", _, _}} ->
            ?INFO_MSG("process_local -> get -> test", []),
            IQ#iq{type = result, sub_el = [{xmlelement, "value", [], [{xmlcdata, "Hello World"}]}]};
        {set, {xmlelement, "test", _, _}} ->
            IQ#iq{type = result, sub_el = []};
        _ ->

           IQ#iq{type = error, sub_el = [SubEl, ?ERR_FEATURE_NOT_IMPLEMENTED]}

    end.

I am looking for nothing fancy at this point, just defining a simple IQ namespace, and having a module that will forward the IQ message from one JID to another. From my limited understanding I would think I don't need the case statement above, however I'm certainly interested in the feedback.

Who knows, perhaps I'm not going about this the right way.

Looking forward to your suggestions.

Thanks!

hz

I've improved it a little. It

I've improved it a little. It works for me.

-module(mod_iqtest).
-behaviour(gen_mod).

-export([start/2, stop/1,
        process_sm_iq/3 %% I assume this is needed to handle JID to JID communication of the IQ?
    ]).

-include("ejabberd.hrl").
-include("jlib.hrl").

-define(NS_TEST, "http://jabber.org/protocol/test").
-define(NS_TEST2, "http://jabber.org/protocol/test2").

start(Host, Opt) ->
    IQDisc = gen_mod:get_opt(iqdisc, Opt, one_queue),
    gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_TEST, ?MODULE, process_sm_iq, IQDisc),
    gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_TEST2, ?MODULE, process_sm_iq, IQDisc),
    ?INFO_MSG("Loading module 'mod_iqtest' v.01", []).

stop(Host) ->
    gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_TEST),
    gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_TEST2),
    ?INFO_MSG("Stoping module 'mod_iqtest' ", []).

process_sm_iq(_From, _To, #iq{type = get, xmlns = ?NS_TEST} = IQ) ->
    ?INFO_MSG("Processing IQ Get query:~n ~p", [IQ]),
    IQ#iq{type = result, sub_el = [{xmlelement, "value", [], [{xmlcdata, "Hello World of Testing."}]}]};
process_sm_iq(_From, _To, #iq{type = get, xmlns = ?NS_TEST2} = IQ) ->
    ?INFO_MSG("Processing IQ Get query of namespace 2:~n ~p", [IQ]),
    IQ#iq{type = result, sub_el = [{xmlelement, "value", [], [{xmlcdata, "Hello World of Test 2."}]}]};
process_sm_iq(_From, _To, #iq{type = set} = IQ) ->
    ?INFO_MSG("Processing IQ Set: it does nothing", []),
    IQ#iq{type = result, sub_el = []};
process_sm_iq(_From, _To, #iq{sub_el = SubEl} = IQ) ->
    ?INFO_MSG("Processing IQ other type: it does nothing", []),
    IQ#iq{type = error, sub_el = [SubEl, ?ERR_FEATURE_NOT_IMPLEMENTED]}.
Syndicate content