Writing your own ejabberd Module

Coming soon:
What is ejabberd?
What is erlang?
Installation details.

I will start with writing the ejabberd module first.

Once you have done all the installations:

sudo make
sudo make install
sudo ejabberdctl start
sudo ejabberdctl stop

You can check the status using:
sudo ejabberdctl status


You can locate logs at: (With default installation)
/var/log/ejabberd

All config related changes will be mainly done at:
/etc/ejabberd/ejabberd.yml

You will need a basic understanding of erlang.

You can take a short crash course at:
http://www.erlang.org/course/course.html

This is a good reference.

Go to your ejabberd-15.07/src directory. This is where you will write the hook for your ejabberd server.

Create a file, say mod_changeBody.erl. Your module name and file name should be same.

Add the following basic contents into your erl file:

-module(mod_changeBody).
-behavior(gen_mod).

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

-export([start/2, stop/1]).

start(_Host,_Opts) ->
?INFO_MSG("Starting the hook : change body",[]),
ok.

stop(_Host)->
?INFO_MSG("Removing the hook : change body", []),
ok.


You need to now add your module to mod_changeBody: {} to the modules section of ejabberd.yml.

Now make and make install. You should see a ebin/mod_changeBody.beam file in your home directory.

Check the logs. You should be able to see Starting the hook and ending hook info msg when starting and stopping the server.

This is a simple module you just wrote. You can add functionality to the module and call it from other modules.

We will only discuss writing hooks.

So lets begin:

Modified file to print received msg:

-module(mod_changeBody).
-behavior(gen_mod).

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

-export([start/2, stop/1, on_filter_packet/1]).

start(_Host,_Opts) ->
?INFO_MSG("Starting the hook : change body",[]),
ejabberd_hooks:add(filter_packet, global, ?MODULE, on_filter_packet, 50),
ok.

stop(_Host)->
?INFO_MSG("Removing the hook : change body", []),
ejabberd_hooks:delete(filter_packet, global, ?MODULE, on_filter_packet, 50),
ok.

on_filter_packet({_From, _To, _XML} = Packet) ->
?INFO_MSG("Received msg : ~p", [Packet]),
Packet.

So yay! You got the message.

Now lets try modifying the body of the XML.

XML is the actual msg that will be passed. Here, Packet is just the variable holding your from, to and message body.

Read on tuples and lists in erlang to understand this better.

Every function call in erlang should have a return value. So for some here, it is ok. For some it might be 'drop', in our case its Packet or can be modified values of {From, To, XML}.
Further ahead:

In case you want to modify the packet, or save to a xml file, or do any processing. I would suggest you to spawn a new process and do it. You can try doing it in the same process to understand the drawback of performing functions in the same process.

I will get back and write an article on modifying the Packet. Its very simple. Just read erlang and you should be good to go.

Please feel free to post your question and suggestions.

Comments

Unknown said…
Excellent ! explanation very short and clear. Could you provide an example of client and server communication from scratch. Thanks....
Unknown said…
Excellent explanation short and clear. Could you provide post of eJabberd client server communication example from scratch..., Thanks.....
Unknown said…
Excellent! explanation short and clear; Can you provide post for eJabberd client server communication example from scratch (if possible please implement client using Javascript)
Richa Vaidya said…
Hi, I will add an example soon. Till then you can read on XMPP Protocol.
Richa Vaidya said…
Hi, I have added a post on guidelines to create a client for ejabberd. Hope that helps.

Popular posts from this blog

npm ECONNREFUSED error

Conditional Flow - Spring Batch Part 6