2011-05-27 11:43:52 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_blocking.erl
|
|
|
|
%%% Author : Stephan Maka
|
|
|
|
%%% Purpose : XEP-0191: Simple Communications Blocking
|
|
|
|
%%% Created : 24 Aug 2008 by Stephan Maka <stephan@spaceboyz.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2011-05-27 11:43:52 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
2015-08-05 09:52:54 +02:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-05-27 11:43:52 +02:00
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_blocking).
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 191, '1.2'}).
|
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
-export([start/2, stop/1, reload/3, process_iq/1, depends/2,
|
2020-01-08 10:24:51 +01:00
|
|
|
disco_features/5, mod_options/1, mod_doc/0]).
|
2011-05-27 11:43:52 +02:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-07-19 09:07:04 +02:00
|
|
|
-include("xmpp.hrl").
|
2011-05-27 11:43:52 +02:00
|
|
|
-include("mod_privacy.hrl").
|
2019-06-22 16:08:45 +02:00
|
|
|
-include("translate.hrl").
|
2011-05-27 11:43:52 +02:00
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
start(Host, _Opts) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(disco_local_features, Host, ?MODULE, disco_features, 50),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
2018-02-11 10:54:15 +01:00
|
|
|
?NS_BLOCKING, ?MODULE, process_iq).
|
2011-05-27 11:43:52 +02:00
|
|
|
|
|
|
|
stop(Host) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(disco_local_features, Host, ?MODULE, disco_features, 50),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_BLOCKING).
|
2011-05-27 11:43:52 +02:00
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
reload(_Host, _NewOpts, _OldOpts) ->
|
|
|
|
ok.
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[{mod_privacy, hard}].
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec disco_features({error, stanza_error()} | {result, [binary()]} | empty,
|
|
|
|
jid(), jid(), binary(), binary()) ->
|
|
|
|
{error, stanza_error()} | {result, [binary()]}.
|
|
|
|
disco_features({error, Err}, _From, _To, _Node, _Lang) ->
|
|
|
|
{error, Err};
|
|
|
|
disco_features(empty, _From, _To, <<"">>, _Lang) ->
|
|
|
|
{result, [?NS_BLOCKING]};
|
|
|
|
disco_features({result, Feats}, _From, _To, <<"">>, _Lang) ->
|
|
|
|
{result, [?NS_BLOCKING|Feats]};
|
|
|
|
disco_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2016-07-19 09:07:04 +02:00
|
|
|
-spec process_iq(iq()) -> iq().
|
2017-01-09 15:02:17 +01:00
|
|
|
process_iq(#iq{type = Type,
|
|
|
|
from = #jid{luser = U, lserver = S},
|
|
|
|
to = #jid{luser = U, lserver = S}} = IQ) ->
|
|
|
|
case Type of
|
|
|
|
get -> process_iq_get(IQ);
|
|
|
|
set -> process_iq_set(IQ)
|
|
|
|
end;
|
|
|
|
process_iq(#iq{lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Query to another users is forbidden"),
|
2017-01-09 15:02:17 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_forbidden(Txt, Lang)).
|
2011-05-27 11:43:52 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec process_iq_get(iq()) -> iq().
|
|
|
|
process_iq_get(#iq{sub_els = [#block_list{}]} = IQ) ->
|
|
|
|
process_get(IQ);
|
|
|
|
process_iq_get(#iq{lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2017-01-09 15:02:17 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang)).
|
2011-05-27 11:43:52 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec process_iq_set(iq()) -> iq().
|
|
|
|
process_iq_set(#iq{lang = Lang, sub_els = [SubEl]} = IQ) ->
|
2016-09-13 11:30:05 +02:00
|
|
|
case SubEl of
|
|
|
|
#block{items = []} ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No items found in this query"),
|
2017-01-09 15:02:17 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_bad_request(Txt, Lang));
|
2016-09-13 11:30:05 +02:00
|
|
|
#block{items = Items} ->
|
2018-05-02 22:17:32 +02:00
|
|
|
JIDs = [jid:tolower(JID) || #block_item{jid = JID} <- Items],
|
2017-01-09 15:02:17 +01:00
|
|
|
process_block(IQ, JIDs);
|
2016-09-13 11:30:05 +02:00
|
|
|
#unblock{items = []} ->
|
2017-01-09 15:02:17 +01:00
|
|
|
process_unblock_all(IQ);
|
2016-09-13 11:30:05 +02:00
|
|
|
#unblock{items = Items} ->
|
2018-05-02 22:17:32 +02:00
|
|
|
JIDs = [jid:tolower(JID) || #block_item{jid = JID} <- Items],
|
2017-01-09 15:02:17 +01:00
|
|
|
process_unblock(IQ, JIDs);
|
2016-09-13 11:30:05 +02:00
|
|
|
_ ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("No module is handling this query"),
|
2017-01-09 15:02:17 +01:00
|
|
|
xmpp:make_error(IQ, xmpp:err_service_unavailable(Txt, Lang))
|
|
|
|
end.
|
2011-05-27 11:43:52 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec listitems_to_jids([listitem()], [ljid()]) -> [ljid()].
|
|
|
|
listitems_to_jids([], JIDs) ->
|
|
|
|
JIDs;
|
|
|
|
listitems_to_jids([#listitem{type = jid,
|
|
|
|
action = deny, value = JID} = Item | Items],
|
2013-03-14 10:33:02 +01:00
|
|
|
JIDs) ->
|
2016-07-19 09:07:04 +02:00
|
|
|
Match = case Item of
|
|
|
|
#listitem{match_all = true} ->
|
|
|
|
true;
|
|
|
|
#listitem{match_iq = true,
|
|
|
|
match_message = true,
|
|
|
|
match_presence_in = true,
|
|
|
|
match_presence_out = true} ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
if Match -> listitems_to_jids(Items, [JID | JIDs]);
|
|
|
|
true -> listitems_to_jids(Items, JIDs)
|
2011-05-27 11:43:52 +02:00
|
|
|
end;
|
|
|
|
% Skip Privacy List items than cannot be mapped to Blocking items
|
2017-01-09 15:02:17 +01:00
|
|
|
listitems_to_jids([_ | Items], JIDs) ->
|
|
|
|
listitems_to_jids(Items, JIDs).
|
2011-05-27 11:43:52 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec process_block(iq(), [ljid()]) -> iq().
|
2017-05-20 21:36:32 +02:00
|
|
|
process_block(#iq{from = From} = IQ, LJIDs) ->
|
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
|
|
|
case mod_privacy:get_user_list(LUser, LServer, default) of
|
|
|
|
{error, _} ->
|
|
|
|
err_db_failure(IQ);
|
|
|
|
Res ->
|
|
|
|
{Name, List} = case Res of
|
|
|
|
error -> {<<"Blocked contacts">>, []};
|
|
|
|
{ok, NameList} -> NameList
|
|
|
|
end,
|
|
|
|
AlreadyBlocked = listitems_to_jids(List, []),
|
|
|
|
NewList = lists:foldr(
|
|
|
|
fun(LJID, List1) ->
|
|
|
|
case lists:member(LJID, AlreadyBlocked) of
|
|
|
|
true ->
|
|
|
|
List1;
|
|
|
|
false ->
|
|
|
|
[#listitem{type = jid,
|
|
|
|
value = LJID,
|
|
|
|
action = deny,
|
|
|
|
order = 0,
|
|
|
|
match_all = true}|List1]
|
|
|
|
end
|
|
|
|
end, List, LJIDs),
|
|
|
|
case mod_privacy:set_list(LUser, LServer, Name, NewList) of
|
|
|
|
ok ->
|
|
|
|
case (if Res == error ->
|
|
|
|
mod_privacy:set_default_list(
|
|
|
|
LUser, LServer, Name);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end) of
|
|
|
|
ok ->
|
|
|
|
mod_privacy:push_list_update(From, Name),
|
2018-05-02 22:17:32 +02:00
|
|
|
Items = [#block_item{jid = jid:make(LJID)}
|
|
|
|
|| LJID <- LJIDs],
|
2017-05-20 21:36:32 +02:00
|
|
|
broadcast_event(From, #block{items = Items}),
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
{error, notfound} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?ERROR_MSG("Failed to set default list '~ts': "
|
2017-05-20 21:36:32 +02:00
|
|
|
"the list should exist, but not found",
|
|
|
|
[Name]),
|
|
|
|
err_db_failure(IQ);
|
|
|
|
{error, _} ->
|
|
|
|
err_db_failure(IQ)
|
|
|
|
end;
|
|
|
|
{error, _} ->
|
|
|
|
err_db_failure(IQ)
|
|
|
|
end
|
2012-04-27 11:52:05 +02:00
|
|
|
end.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec process_unblock_all(iq()) -> iq().
|
2017-05-20 21:36:32 +02:00
|
|
|
process_unblock_all(#iq{from = From} = IQ) ->
|
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
|
|
|
case mod_privacy:get_user_list(LUser, LServer, default) of
|
|
|
|
{ok, {Name, List}} ->
|
|
|
|
NewList = lists:filter(
|
|
|
|
fun(#listitem{action = A}) ->
|
|
|
|
A /= deny
|
|
|
|
end, List),
|
|
|
|
case mod_privacy:set_list(LUser, LServer, Name, NewList) of
|
|
|
|
ok ->
|
|
|
|
mod_privacy:push_list_update(From, Name),
|
|
|
|
broadcast_event(From, #unblock{}),
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
{error, _} ->
|
|
|
|
err_db_failure(IQ)
|
|
|
|
end;
|
|
|
|
error ->
|
|
|
|
broadcast_event(From, #unblock{}),
|
2017-01-09 15:02:17 +01:00
|
|
|
xmpp:make_iq_result(IQ);
|
2017-05-20 21:36:32 +02:00
|
|
|
{error, _} ->
|
|
|
|
err_db_failure(IQ)
|
2011-05-27 11:43:52 +02:00
|
|
|
end.
|
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec process_unblock(iq(), [ljid()]) -> iq().
|
2017-05-20 21:36:32 +02:00
|
|
|
process_unblock(#iq{from = From} = IQ, LJIDs) ->
|
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
|
|
|
case mod_privacy:get_user_list(LUser, LServer, default) of
|
|
|
|
{ok, {Name, List}} ->
|
|
|
|
NewList = lists:filter(
|
|
|
|
fun(#listitem{action = deny, type = jid,
|
|
|
|
value = LJID}) ->
|
|
|
|
not lists:member(LJID, LJIDs);
|
|
|
|
(_) ->
|
|
|
|
true
|
|
|
|
end, List),
|
|
|
|
case mod_privacy:set_list(LUser, LServer, Name, NewList) of
|
|
|
|
ok ->
|
|
|
|
mod_privacy:push_list_update(From, Name),
|
2018-05-02 22:17:32 +02:00
|
|
|
Items = [#block_item{jid = jid:make(LJID)}
|
|
|
|
|| LJID <- LJIDs],
|
2017-05-20 21:36:32 +02:00
|
|
|
broadcast_event(From, #unblock{items = Items}),
|
|
|
|
xmpp:make_iq_result(IQ);
|
|
|
|
{error, _} ->
|
|
|
|
err_db_failure(IQ)
|
|
|
|
end;
|
|
|
|
error ->
|
2018-05-02 22:17:32 +02:00
|
|
|
Items = [#block_item{jid = jid:make(LJID)}
|
|
|
|
|| LJID <- LJIDs],
|
2017-05-20 21:36:32 +02:00
|
|
|
broadcast_event(From, #unblock{items = Items}),
|
2017-01-09 15:02:17 +01:00
|
|
|
xmpp:make_iq_result(IQ);
|
2017-05-20 21:36:32 +02:00
|
|
|
{error, _} ->
|
|
|
|
err_db_failure(IQ)
|
2011-05-27 11:43:52 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
-spec broadcast_event(jid(), block() | unblock()) -> ok.
|
|
|
|
broadcast_event(#jid{luser = LUser, lserver = LServer} = From, Event) ->
|
2018-02-18 15:58:51 +01:00
|
|
|
BFrom = jid:remove_resource(From),
|
2017-01-09 15:02:17 +01:00
|
|
|
lists:foreach(
|
|
|
|
fun(R) ->
|
|
|
|
To = jid:replace_resource(From, R),
|
2018-02-18 15:58:51 +01:00
|
|
|
IQ = #iq{type = set, from = BFrom, to = To,
|
2018-07-05 10:51:49 +02:00
|
|
|
id = <<"push", (p1_rand:get_string())/binary>>,
|
2017-01-09 15:02:17 +01:00
|
|
|
sub_els = [Event]},
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(IQ)
|
2017-01-09 15:02:17 +01:00
|
|
|
end, ejabberd_sm:get_user_resources(LUser, LServer)).
|
2011-05-27 11:43:52 +02:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec process_get(iq()) -> iq().
|
2017-05-20 21:36:32 +02:00
|
|
|
process_get(#iq{from = #jid{luser = LUser, lserver = LServer}} = IQ) ->
|
|
|
|
case mod_privacy:get_user_list(LUser, LServer, default) of
|
|
|
|
{ok, {_, List}} ->
|
2017-01-09 15:02:17 +01:00
|
|
|
LJIDs = listitems_to_jids(List, []),
|
2018-05-02 22:17:32 +02:00
|
|
|
Items = [#block_item{jid = jid:make(J)} || J <- LJIDs],
|
2017-05-20 21:36:32 +02:00
|
|
|
xmpp:make_iq_result(IQ, #block_list{items = Items});
|
|
|
|
error ->
|
|
|
|
xmpp:make_iq_result(IQ, #block_list{});
|
|
|
|
{error, _} ->
|
|
|
|
err_db_failure(IQ)
|
2012-04-27 11:52:05 +02:00
|
|
|
end.
|
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-spec err_db_failure(iq()) -> iq().
|
2017-05-20 21:36:32 +02:00
|
|
|
err_db_failure(#iq{lang = Lang} = IQ) ->
|
2019-06-22 16:08:45 +02:00
|
|
|
Txt = ?T("Database failure"),
|
2017-05-20 21:36:32 +02:00
|
|
|
xmpp:make_error(IQ, xmpp:err_internal_server_error(Txt, Lang)).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2018-02-11 10:54:15 +01:00
|
|
|
mod_options(_Host) ->
|
|
|
|
[].
|
2020-01-08 10:24:51 +01:00
|
|
|
|
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("The module implements "
|
|
|
|
"https://xmpp.org/extensions/xep-0191.html"
|
|
|
|
"[XEP-0191: Blocking Command]."), "",
|
|
|
|
?T("This module depends on 'mod_privacy' where "
|
|
|
|
"all the configuration is performed.")]}.
|