Ejabberd module to send an acknowledge message

2019-09-20 07:51发布

问题:

This erlang code I am using to send message acknowledgement. While using this I am getting error, error log is given below

My code:

-module(mod_ack).
-behaviour(gen_mod).

%% public methods for this module
-export([start/2, stop/1]).
-export([on_user_send_packet/3]).

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

%%add and remove hook module on startup and close

start(Host, _Opts) ->
    ?INFO_MSG("mod_echo_msg starting", []),
    ejabberd_hooks:add(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
    ok.

stop(Host) ->
    ?INFO_MSG("mod_echo_msg stopping", []),
    ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
    ok.

on_user_send_packet(From, To, Packet) ->
        return_message_reciept_to_sender(From, To, Packet),
        Packet.


return_message_reciept_to_sender(From, _To, Packet) ->
    IDS = xml:get_tag_attr_s("id", Packet),
    ReturnRecieptType = "serverreceipt",
%%    ?INFO_MSG("mod_echo_msg - MsgID: ~p To: ~p From: ~p", [IDS, _To, From]),
    send_message(From, From, ReturnRecieptType, IDS, "").


send_message(From, To, TypeStr, IDS, BodyStr) ->
    XmlBody = {xmlelement, "message",
           [{"type", TypeStr},
        {"from", jlib:jid_to_string(From)},
        {"to", jlib:jid_to_string(To)},
        {"id", IDS},
           [{xmlelement, "body", [],
         [{xmlcdata, BodyStr}]}]},
    ejabberd_router:route(From, To, XmlBody).

I have removed the modules where i used on_user_send hook but still getting error also updated the error log.

Error log:

2015-10-06 07:13:45.796 [error] <0.437.0>@ejabberd_hooks:run_fold1:371 {function_clause,[{xml,get_tag_attr_s,[<<"id">>,{jid,<<"xxxxxx">>,<<"xxxxxx">>,<<>>,<<"xxxxxx">>, <<"xxxxxx">>,<<>>}],[{file,"src/xml.erl"},{line,210}]},{mod_ack,return_message_reciept_to_sender,3, [{file,"src/mod_ack.erl"},{line,36}]},{mod_ack,on_user_send_packet,4,[{file,"src/mod_ack.erl"},{line,30}]},{ejabberd_hooks,safe_apply,3,[{file,"src/ejabberd_hooks.erl"},{line,385}]},{ejabberd_hooks,run_fold1,4,[{file,"src/ejabberd_hooks.erl"},{line,368}]},{ejabberd_c2s,session_established2,2,[{file,"src/ejabberd_c2s.erl"},{line,1296}]},{p1_fsm,handle_msg,10,[{file,"src/p1_fsm.erl"},{line,582}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,237}]}]}

回答1:

You seem to have a module interfering (mod_send_receipt) or you have more code registering to hook. Your module is called mod_ack and it is not the one generating the crash. The crash happen because you have a hook registered to run function mod_send_receipt:on_user_send_packet which is 'undef': It means it does not exist or is not exported.



回答2:

The ejabberd docs define the user_send_packet hook as an arity-4 function:

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

You are registering an arity-3 function, so when ejabberd tries to call your on_user_send_packet function, it passes 4 arguments and gets the undef function exception.

In order for your callback function to actually be called, you will need to match its argument list to what ejabberd will be sending, i.e.:

on_user_send_packet(Packet, _C2SState, From, To)