2

I'm currently trying to write an ejabberd module but am falling at the first hurdle so assume I am missing something simple.

The aim for the finished module is to inspect a message body and look for a certain term. If the term is present then the module will drop the message and send a reply to the originator informing them that their message has been dropped due to the violation. I do not want to just obfuscate the offending term.

I have built a skeleton test module using the 'hello world' example from the main ejabberd webpages and it works so I seem to be putting things in the right place and compiling them properly.

Taking inspiration from sources such as mod_pottymouth as well as related stack overflow questions such as Filtering message packet's body in ejabberd and How to filter messages in Ejabberd (not to mention various blogs and other articles) code along the lines of the following should work as far as I can tell:

-module(mod_helloworld).

-behaviour(gen_mod).

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

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


start(_Host, _Opts) ->
    ?INFO_MSG("Hello there, ejabberd world!", []),
    ejabberd_hooks:add(filter_packet, global, ?MODULE, on_filter_packet, 0),
        ok.

stop(_Host) ->
    ?INFO_MSG("Bye bye, ejabberd world!", []),
    ejabberd_hooks:delete(filter_packet, global, ?MODULE, on_filter_packet, 0),
        ok.

on_filter_packet(drop) ->
    ?INFO_MSG("world - Message was dropped",[]),
    drop;
on_filter_packet({_From, _To, Xml} = Packet) ->
    % this clause never fires
    ?INFO_MSG("world - message hit clause", [Xml]),
    Packet;
on_filter_packet(Msg) ->
    % catch-all clause for non-message types
    ?INFO_MSG("world - message hit default", []),
    Msg.

When the module runs every packet triggers the catch-all clause so I know the hooks seem to be doing their job, and if I check the main ejabberd logs then messages are going through just fine. If I remove the catch-all then the filter just crashes.

I've tried a number of variations of the filter clause taken from various examples (plus supporting code) e.g. :

{_From, _To, #xmlel{name = StanzaType}} = Input

or

{_From, _To, {xmlel, <<"message">>, _Attrs, Els} = _Packet} = _Msg

but none of them seem to be working.

I'm using two different Centos 7 set-ups (one with ejabberd installed as a package running on a physical box, the other using Vagrant with ejabberd installed from source) and a number of different XMPP clients, but the result is the same. Ejabberd version is 17.07.31.

What obvious thing am I missing?

CJLWS
  • 21
  • 4

0 Answers0