How to implement modules in ejabberd?

后端 未结 1 510
迷失自我
迷失自我 2021-01-27 07:18

I am new on the XMPP server ejabberd. I installed ejabberd on ubuntu from this link: https://docs.ejabberd.im/admin/installation/#install-on-linux. I am using the default ejabbe

相关标签:
1条回答
  • 2021-01-27 07:30

    Well, that example source code is six years old, and ejabberd development API has changed since then. I've updated the example, and this compiles and starts correctly with ejabberd 20.07:

    -module(mod_stanza_ack).
    -behaviour(gen_mod).
    
    -include("xmpp.hrl").
    -include("logger.hrl").
    -include("translate.hrl").
    
    -export([start/2, stop/1, mod_options/1, mod_doc/0, depends/2]).
    -export([on_user_send_packet/1]).
    
    start(Host, _Opts) ->
        ?INFO_MSG("mod_stanza_ack starting", []),
        ejabberd_hooks:add(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
        ok.
    
    stop(Host) ->
        ?INFO_MSG("mod_stanza_ack stopping", []),
        ejabberd_hooks:delete(user_send_packet, Host, ?MODULE, on_user_send_packet, 0),
        ok.
    
    on_user_send_packet({#presence{to = To, from = From} = Packet, C2SState}) ->
        ?INFO_MSG("mod_stanza_ack a presence has been sent coming from: ~p", [From]),
        ?INFO_MSG("mod_stanza_ack a presence has been sent to: ~p", [To]),
        ?INFO_MSG("mod_stanza_ack a presence has been sent with the following packet:~n ~p", [Packet]),
        {Packet, C2SState};
    
    on_user_send_packet({#iq{to = To, from = From} = Packet, C2SState}) ->
        ?INFO_MSG("mod_stanza_ack a iq has been sent coming from: ~p", [From]),
        ?INFO_MSG("mod_stanza_ack a iq has been sent to: ~p", [To]),
        ?INFO_MSG("mod_stanza_ack a iq has been sent with the following packet:~n ~p", [Packet]),
        {Packet, C2SState};
    
    on_user_send_packet({#message{to = To, from = From} = Packet, C2SState}) ->
        ?INFO_MSG("mod_stanza_ack a message has been sent coming from: ~p", [From]),
        ?INFO_MSG("mod_stanza_ack a message has been sent to: ~p", [To]),
        ?INFO_MSG("mod_stanza_ack a message has been sent with the following packet:~n ~p", [Packet]),
        {Packet, C2SState}.
    
    depends(_Host, _Opts) ->
        [].
    
    mod_options(_Host) ->
        [].
    
    mod_doc() ->
        #{desc =>
              ?T("This an example module.")}.
    

    Following your detailed step by step installation guide, I get two problems, that I describe here and how to solve them:

    1. Compilation lacks header files.

    I copy mod_stanza_ack.erl to ejabberd-20.07/bin, and then run this command:

    $ ./erlc mod_stanza_ack.erl
    mod_stanza_ack.erl:4: can't find include file "xmpp.hrl"
    mod_stanza_ack.erl:5: can't find include file "logger.hrl"
    mod_stanza_ack.erl:6: can't find include file "translate.hrl"
    mod_stanza_ack.erl:12: undefined macro 'INFO_MSG/2'
    mod_stanza_ack.erl:17: undefined macro 'INFO_MSG/2'
    mod_stanza_ack.erl:22: undefined macro 'INFO_MSG/2'
    mod_stanza_ack.erl:47: undefined macro 'T/1'
    mod_stanza_ack.erl:8: function mod_doc/0 undefined
    mod_stanza_ack.erl:8: function start/2 undefined
    mod_stanza_ack.erl:8: function stop/1 undefined
    mod_stanza_ack.erl:9: function on_user_send_packet/1 undefined
    

    The solution is simple: provide the paths to the header files:

    $ ./erlc -I ../lib/ejabberd-20.07/include/ -I ../lib/xmpp-1.4.9/include/ -I ../lib/fast_xml-1.1.43/include/ mod_stanza_ack.erl
    

    This way the file is compiled correctly.

    1. INFO_MSG in the source code do not produce log messages in ejabberd log file or "ejabberdctl live" console.

    This is because we didn't tell the compiler to use the LAGER library. The solution is quite simple: include -DLAGER in the module compilation. So, this is the perfect compilation call:

    $ ./erlc -I ../lib/ejabberd-20.07/include/ -I ../lib/xmpp-1.4.9/include/ -I ../lib/fast_xml-1.1.43/include/ -DLAGER mod_stanza_ack.erl
    

    Now, you copy the resulting mod_stanza_ack.beam with all the other ejabberd beam files, enable the module in ejabberd.yml, and restart ejabberd, and all will work as expected

    0 讨论(0)
提交回复
热议问题