Skip to content

Instantly share code, notes, and snippets.

@zinid zinid/mod_isolation.erl
Last active Aug 6, 2018

Embed
What would you like to do?
-module(mod_isolation).
-behaviour(gen_mod).
%% gen_mod callbacks
-export([start/2, stop/1, mod_options/1, depends/2]).
%% hooks
-export([filter_packet/1]).
-include("xmpp.hrl").
%%%===================================================================
%%% API
%%%===================================================================
start(_Host, _Opts) ->
ejabberd_hooks:add(filter_packet, ?MODULE, filter_packet, 50).
stop(Host) ->
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
false ->
ejabberd_hooks:delete(filter_packet, ?MODULE, filter_packet, 50);
true ->
ok
end.
mod_options(_) ->
[].
depends(_Host, _Opts) ->
[].
%%%===================================================================
%%% Internal functions
%%%===================================================================
filter_packet(drop) ->
drop;
filter_packet(Pkt) ->
case xmpp:get_meta(Pkt, already_filtered, false) of
true ->
Pkt;
false ->
From = xmpp:get_from(Pkt),
To = xmpp:get_to(Pkt),
try {ejabberd_router:host_of_route(From#jid.lserver),
ejabberd_router:host_of_route(To#jid.lserver)} of
{Host, Host} ->
Pkt;
{_Host1, _Host2} ->
Pkt1 = xmpp:put_meta(Pkt, already_filtered, true),
Lang = xmpp:get_lang(Pkt),
%% We already have translations for this phrase
Txt = <<"Access denied by service policy">>,
Err = xmpp:err_forbidden(Txt, Lang),
ejabberd_router:route_error(Pkt1, Err),
{stop, drop}
catch _:{unregistered_route, _} ->
%% This will go to s2s manager
Pkt
end
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.