Handling custom IQs

Hi,

I am trying to write a very simple iq handler and followed some of what I saw on the web, here is the code:

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

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

-define(PROCNAME, ?MODULE).

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

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

start(Host, Opts) ->
    ?INFO_MSG("Loading module 'mod_iqtest' v.01", []),
    register(?PROCNAME,spawn(?MODULE, init, [Host, Opts])),
    ok.

init(Host, _Opts) ->
    IQDisc = gen_mod:get_opt(iqdisc, _Opts, 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("added iq handlers 'mod_iqtest' v.01", []),
    ok.

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' ", []),
    ok.

process_sm_iq(_From, _To, IQ) ->
    ?INFO_MSG("Processing IQ Get query:~n ~p", [IQ]),
    IQ#iq{type = result, sub_el = [{xmlelement, "value", [], [{xmlcdata, "Hello World of Testing."}]}]}.

However, when sending the following stanza:

<iq type="get" to="user_56acd30352dc5d206320d9b2@localhost" xmlns="http://jabber.org/protocol/test">
</iq>

I am getting:

<iq from="user_56acd30352dc5d206320d9b2@localhost" type="error" xml:lang="en" to="user_54345c1f5f2f96956338ff53@localhost/MacBook-Pro">
<error type="modify" code="400">
<bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
</error>
</iq>

In the logs I do see that the printout from the start function and that the module was started but I don't see any of the printouts I have in my handler function (process_sm_iq). Here are the relevant logs from ejabberd.log after sending the IQ: http://pastebin.com/UZ9yavx0

Any idea what I am missing?

P.S I am using the latest master.

Any idea guys? Should it work

Any idea guys? Should it work as is?

That code was for ejabberd 2.

That code was for ejabberd 2. In recent ejabberd, I have to make some changes to your code:

--- a/src/mod_iqtest.erl
+++ b/src/mod_iqtest.erl
@@ -13,7 +13,7 @@
 -include("jlib.hrl").
 -include("logger.hrl").
 
--define(NS_TEST, "http://jabber.org/protocol/test").
+-define(NS_TEST, <<"http://jabber.org/protocol/test">>).
 -define(NS_TEST2, "http://jabber.org/protocol/test2").
 
 start(Host, Opts) ->
@@ -22,7 +22,7 @@ start(Host, Opts) ->
     ok.
 
 init(Host, _Opts) ->
-    IQDisc = gen_mod:get_opt(iqdisc, _Opts, one_queue),
+    IQDisc = gen_mod:get_opt(iqdisc, _Opts, fun(A) -> A end, 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("added iq handlers 'mod_iqtest' v.01", []),
@@ -36,4 +36,4 @@ stop(Host) ->
 
 process_sm_iq(_From, _To, IQ) ->
     ?INFO_MSG("Processing IQ Get query:~n ~p", [IQ]),
-    IQ#iq{type = result, sub_el = [{xmlelement, "value", [], [{xmlcdata, "Hello World of Testing."}]}]}.
+    IQ#iq{type = result, sub_el = [{xmlel, <<"value">>, [], [{xmlcdata, <<"Hello World of Testing.">>}]}]}.

Then I send a proper IQ request and works:

<iq type='get'>
  <query xmlns='http://jabber.org/protocol/test'/>
</iq>

<iq from='user1@localhost' to='user1@localhost/tka1' type='result'>
  <value>Hello World of Testing.</value>
</iq>

Syndicate content