Search code examples
xmppejabberdejabberd-moduleejabberd-hooks

Ejabberd 15.07 custom module on hook user_send_packet


I am willing to develop a custom module by using a hook "user_send_packet"

For now I have deleted custom work from the function and compiled the code. But when I use this module... Server is crashing and restarting continuously. I am not getting whats going wrong ... simplified code is as below :

-module(mod_gpcustom).

-behaviour(gen_mod).

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

-export([user_send_packet/4,
     mod_opt_type/1]).

-include_lib("stdlib/include/ms_transform.hrl").
-include("jlib.hrl").

%%%===================================================================
%%% API
%%%===================================================================
start(Host, _Opts) ->

    ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
                       user_send_packet, 600),
    ok.

stop(Host) ->
    ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
              user_send_packet, 600),
    ok.


user_send_packet(Pkt, C2SState, JID, Peer) ->
    LUser = JID#jid.luser,
    LServer = JID#jid.lserver,
    ok.



mod_opt_type(cache_life_time) ->
    fun (I) when is_integer(I), I > 0 -> I end;
mod_opt_type(cache_size) ->
    fun (I) when is_integer(I), I > 0 -> I end;
mod_opt_type(db_type) -> fun gen_mod:v_db/1;
mod_opt_type(default) ->
    fun (always) -> always;
    (never) -> never;
    (roster) -> roster
    end;
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
mod_opt_type(store_body_only) ->
    fun (B) when is_boolean(B) -> B end;
mod_opt_type(_) ->
    [cache_life_time, cache_size, db_type, default, iqdisc,
     store_body_only].

Solution

  • As described in documentation, the hook you use expect that your function will return an XMPP packet structure:

    user_send_packet(Packet, C2SState, From, To) -> Packet
    

    You can see that in the doc: http://docs.ejabberd.im/developer/hooks/

    So your function should not return ok but a packet:

    user_send_packet(Pkt, _C2SState, _JID, _Peer) ->
        Pkt.
    

    It should be obvious to catch by reading the badmatch error in your ejabberd log file, but unfortunately, you did not post them.