First code on erlang, MUC message filter by regex
2012-02-22 17:01:03 UTC by maris in Erlang, Programming, Linux, Jabber,Done first steps in erlang, created little module for muc chat message filtering.
configuration:
{modules,
[
{mod_msgfilter, [{filter_regex, "sms|pvn|porn|xxx"}]},
...
code:
-module(mod_msgfilter).
-behavior(gen_mod).
-include("ejabberd.hrl").
-include("jlib.hrl").
-export([start/2, stop/1, on_filter_packet/1]).
start(_Host, _Opts) ->
?INFO_MSG("mod_msgfilter starting", []),
ejabberd_hooks:add(filter_packet, global, ?MODULE, on_filter_packet, 50),
ok.
stop(_Host) ->
?INFO_MSG("mod_msgfilter stopping", []),
ejabberd_hooks:delete(filter_packet, global, ?MODULE, on_filter_packet, 50),
ok.
on_filter_packet({_From, _To, Packet} = Input) ->
case Packet of
{xmlelement, "message", Attrs, _Els} ->
%%?INFO_MSG("on_filter_packet mod_msgfilter RECIEVED PACKET", []),
case xml:get_attr_s("type", Attrs) of
"groupchat" ->
%%?INFO_MSG("on_filter_packet RECIEVED GROUPCHAT mod_msgfilter RECIEVED PACKET", []),
Body = xml:get_path_s(Packet, [{elem, "body"}, cdata]),
case check_message(Body) of
drop -> drop_message(Input);
_ -> Input
end;
_ ->
Input
end;
_ ->
Input
end.
drop_message(Input) ->
{From, _To, Packet} = Input,
?INFO_MSG("DROP From: ~s, Message: ~s", [jlib:jid_to_string(From), xml:element_to_string(Packet)]),
drop.
check_message(Message) ->
RegExp = gen_mod:get_module_opt(global, ?MODULE, filter_regex, "[0-9]{4-9}|sms|pvn"),
Options = [],
case re:run(Message, RegExp, Options) of
{match, _} ->
%%?INFO_MSG("DROP mod_msgfilter RECIEVED PACKET Body: ~s", [Message]),
drop;
nomatch -> ok;
_ -> ok
end.
(0 komentāri)