How to filter messages in Ejabberd

Adil picture Adil · Dec 21, 2009 · Viewed 7.3k times · Source

I have Ejabberd up and running with test users, and its working fine. I want to write a module that can intercept messages and modify them, as follows :

  1. intercept "messages"
  2. send them to a php file
  3. get the result from the same php file (immediate)
  4. Modify the message stanza and send it down the wire to the recipient

The ejabberd documentation is weak and tutorials are non-existent. Can you give me some sample code that does this. I can then figure how to configure it for my needs.

Thanks a bundle!

Adil

Answer

gleber picture gleber · Dec 21, 2009

Here's the basic example of such module:

-module(packet_interceptor).
-behaviour(gen_mod).

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

-export([on_filter_packet/1]).


start(Host, _Opts) ->
    ejabberd_hooks:add(filter_packet, global, ?MODULE, on_filter_packet, 0).

on_filter_packet({From, To, XML} = Packet) ->
    %% does something with a packet
    %% should return modified Packet or atom `drop` to drop the packet
    Packet.

And make sure to add this module into ejabberd's configuration into module section:

{modules,
 [...
  ...
  ...
  {packet_interceptor, []}
 ]}.

Just extend on_filter_packet/1 the way you want and return appropriately modified packet.