Ejabberd Module Development

Source: Internet
Author: User

Reference:
http://anders.conbere.org/journal/
http://www.process-one.net/en/wiki/ejabberd_module_development/

Many of Ejabberd's internal modules work in the form of plug-ins, so we can also develop our own modules to integrate into the ejabberd to complete a variety of our unique needs.

Ejabberd defines a gen_mod behaviour, which requires the following callback:

Erlang Code
    1. Start (Host, Opts), OK
    2. Stop (Host), OK
    3. * Host = string ()
    4. * Opts = [{Name, Value}]
    5. * Name = Value = String ()



Where host is a virtual host that runs this module.
OPTs is the parameter of the MoD specified in the configuration (which is described later) and can be obtained by GEN_MOD:GET_MODULE_OPT/4 (see Gen_mod.erl code that this information is stored in ETS).

It's very easy for us to implement a mod:

Erlang Code
    1. -module (My_module).
    2. -author (' [email protected] ').
    3. -behaviour (Gen_mod).
    4. -include (' Ejabberd.hrl ').
    5. Gen_mod Callback
    6. -export ([start/2, stop/1]).
    7. Start (_host, _opt), OK.
    8. Stop (_host), OK.



Let's add a code that makes this "useless" mod clearer

Erlang Code
    1. Start (_host, _opt)
    2. ?  DEBUG ("EXAMPLE MODULE LOADING").


Compile the My_module and place the My_module.beam in your Ejabberd/ebin directory:
MV My_module.beam/var/lib/ejabberd/ebin

Next, we need to do some configuration on ejabberd.cfg and tell Ejabberd to load our My_module:

Erlang Code
    1. {modules,
    2. [
    3. {Mod_register, [{Access, Register}]},
    4. ...
    5. {my_module, []}% [] is thesecond parameter in my_module:start/2
    6. ]}.


OK, after everything OK, restart Ejabberd, if your loglevel is set to 5, then you will see the following information:

Erlang Code
    1. =info report==== 2008-07-: + = =
    2. D (<0.37. 0>:ejabberd_auth_my_auth:EXAMPLE MODULE LOADING



Our Ejabberd module has been successfully loaded.
Writing here, you might ask, how do you implement a more powerful module?
The original Ejabberd provides us with a lot of APIs that can be used by us, including the following:
Ejabberd core modules (Ejabberd kernel module)
Ejabberd Events and Hooks (Ejabberd event and hooks)
Ejabberd IQ handlers
Ejabberd route table (Ejabberd)
Ejabberd HTTP request handlers (Ejabberd HTTP requests processing)

With these APIs, we can implement whatever functionality we want.
The following section implements a simple HTTP processing module

Citation: http://erlangdisplay.iteye.com/blog/315461#bc2336870

Ejabberd Module Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.