Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

erlang - Ejabberd module to send an acknowledge message

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}]}]}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...