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
- Start (Host, Opts), OK
- Stop (Host), OK
- * Host = string ()
- * Opts = [{Name, Value}]
- * 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
- -module (My_module).
- -author (' [email protected] ').
- -behaviour (Gen_mod).
- -include (' Ejabberd.hrl ').
- Gen_mod Callback
- -export ([start/2, stop/1]).
- Start (_host, _opt), OK.
- Stop (_host), OK.
Let's add a code that makes this "useless" mod clearer
Erlang Code
- Start (_host, _opt)
- ? 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
- {modules,
- [
- {Mod_register, [{Access, Register}]},
- ...
- {my_module, []}% [] is thesecond parameter in my_module:start/2
- ]}.
OK, after everything OK, restart Ejabberd, if your loglevel is set to 5, then you will see the following information:
Erlang Code
- =info report==== 2008-07-: + = =
- 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