Simple module event not getting handled

I am new in erlang and ejabbered/mongooseIM. I am trying to write a very simple mongoose module that will add an extra child element to the packets before sending. I am using erlc to compile my code. Below is my code:

-module(mod_test).

-behavior(gen_mod).

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

-include("ejabberd.hrl").

start(Host, Opts) ->
    ejabberd_hooks:add(filter_packet, Host, ?MODULE, add_child, 0),
    ok.

stop(Host) ->
    ejabberd_hooks:delete(filter_packet, Host, ?MODULE, add_child, 0),
    ok.

add_child({From, To, XML} = Packet) ->
    Tag = {"a","b"},
    NewPacket = xml:append_subtags(Packet, [Tag]),
    ?INFO_MSG("  To party: ~p~n",[To]),
    NewPacket.

I can compile the code with few warnings

mod_test.erl:3: Warning: behaviour gen_mod undefined
mod_test.erl:11: Warning: variable 'Opts' is unused
mod_test.erl:20: Warning: variable 'From' is unused
mod_test.erl:20: Warning: variable 'XML' is unused
Then when I add the module and run it it gets started but it doesnt make any changes to the packets and doesnt generate any logs either.

Another issue is, if I add a log within my start function, it gets compiled but I see errors while starting the module

2015-03-03 16:36:34.772 [critical] <0.200.0>@gen_mod:start_module:94 Problem starting the module mod_test for host <<"localhost">>
options: []
error: undef
[{lager,info,["  mod_test starting ...",[[]]],[]},
{mod_test,start,2,[{file,"mod_test.erl"},{line,13}]},
{gen_mod,start_module,3,[{file,"src/gen_mod.erl"},{line,83}]},
{lists,foreach,2,[{file,"lists.erl"},{line,1336}]},
{ejabberd_app,start,2,[{file,"src/ejabberd_app.erl"},{line,69}]},
{application_master,start_it_old,4,
                     [{file,"application_master.erl"},{line,272}]}]
2015-03-03 16:36:34.773 [critical] <0.200.0>@gen_mod:start_module:99 ejabberd initialization was aborted because a module start failed.
The trace is [{lager,info,["  mod_test starting ...",[[]]],[]},{mod_test,start,2,[{file,"mod_test.erl"},{line,13}]},{gen_mod,start_module,3,[{file,"src/gen_mod.erl"},{line,83}]},{lists,foreach,2,[{file,"lists.erl"},{line,1336}]},{ejabberd_app,start,2,[{file,"src/ejabberd_app.erl"},{line,69}]},{application_master,start_it_old,4,[{file,"application_master.erl"},{line,272}]}].

Crash dump was written to: erl_crash.dump
Problem starting the module mod_test for host <<"localhost">>
options: []
error: undef
[{lager,info,["  mod_xyz starting ...",[[]]],[]},
{mod_test,start,2,[{file,"mod_timetagg

What am I doing wrong ? Please help…

Thanks in advanced!

Hello, We are focused on pure

Hello,

We are focused on pure ejabberd, so I will reply according to ejabberd 15.02 state of the art.

Please, make sure you build ejabberd (and your module) with make. It will ensure the correct parse transform on the code are applied.

Then, make sure you include the 'logger.hrl' include.

You you build based on the latest master, it should work without issue.

Could you please try as described with the following repository ?
https://github.com/processone/ejabberd

Thanks !

Thanks a lot for your reply.

Thanks a lot for your reply. I compiled the whole source again with make and this time the module is starting normally and generating logs. However, Im getting the below error while filter packet hook is triggered.

2015-03-04 12:16:30.506 [error] <0.760.0>@ejabberd_hooks:run_fold1:259
{function_clause,
[{xml,append_subtags,[{{jid,<<"user2">>,<<"localhost">>,<<"Damien">>,<<"user2">>,<<"localhost">>,<<"Damien">>},{jid,<<"user1">>,<<"localhost">>,<<"Gajim">>,<<"user1">>,<<"localhost">>,<<"Gajim">>},{xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"type">>,<<"chat">>},{<<"to">>,<<"user1@localhost/Gajim">>},{<<"id">>,<<"ab68a">>}],[{xmlcdata,<<"\n">>},{xmlel,<<"body">>,[],[{xmlcdata,<<"mmmmm">>}]},{xmlcdata,<<"\n">>},{xmlel,<<"active">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/chatstates">>}],[]},{xmlcdata,<<"\n">>}]}},
[{"a","b"}]],[{file,"src/xml.erl"},{line,276}]},
{safely,apply,3,[{file,"src/safely.erl"},{line,19}]},
{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,255}]},
{ejabberd_router,do_route,3,[{file,"src/ejabberd_router.erl"},{line,246}]},
{ejabberd_router,route,3,[{file,"src/ejabberd_router.erl"},{line,78}]},
{ejabberd_c2s,session_established2,2,[{file,"src/ejabberd_c2s.erl"},{line,1098}]},
{p1_fsm_old,handle_msg,10,[{file,"src/p1_fsm_old.erl"},{line,542}]},
{proc_lib,wake_up,3,[{file,"proc_lib.erl"},{line,247}]}]}

How to fix this? Please help.

Hello, it seems these logs

Hello, it seems these logs are not from ejabberd. Could you please try with ejabberd and report ?

Hello, dodul. Did you solve

Hello, dodul.
Did you solve this issue? If yes, then please tell us how did you do that. Thx.

I think the problem occured

I think the problem occured because it is not ejabberd. Probably if you use ejabberd it should not occur (I guess).

pashkash, did you try with ejabberd ? which version ? Are you sure this is the same issue ?

Syndicate content