2015-04-08 14:01:16 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_multicast.erl
|
|
|
|
%%% Author : Badlop <badlop@process-one.net>
|
|
|
|
%%% Purpose : Extended Stanza Addressing (XEP-0033) support
|
|
|
|
%%% Created : 29 May 2007 by Badlop <badlop@process-one.net>
|
2015-10-07 00:06:58 +02:00
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2015-10-07 00:06:58 +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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
|
|
|
%%%
|
2015-04-08 14:01:16 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_multicast).
|
|
|
|
|
|
|
|
-author('badlop@process-one.net').
|
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 33, '1.1'}).
|
|
|
|
|
2015-04-08 14:01:16 +02:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
%% API
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3]).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_info/2, handle_call/3,
|
|
|
|
handle_cast/2, terminate/2, code_change/3]).
|
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([purge_loop/1, mod_opt_type/1, mod_options/1, depends/2]).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
-include("logger.hrl").
|
2018-01-08 09:29:17 +01:00
|
|
|
-include("translate.hrl").
|
2016-08-04 10:49:17 +02:00
|
|
|
-include("xmpp.hrl").
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-record(multicastc, {rserver :: binary(),
|
|
|
|
response,
|
|
|
|
ts :: integer()}).
|
|
|
|
|
|
|
|
-record(dest, {jid_string :: binary() | none,
|
|
|
|
jid_jid :: xmpp:jid(),
|
|
|
|
type :: to | cc | bcc,
|
|
|
|
address :: address()}).
|
|
|
|
|
|
|
|
-type limit_value() :: {default | custom, integer()}.
|
|
|
|
-record(limits, {message :: limit_value(),
|
|
|
|
presence :: limit_value()}).
|
|
|
|
|
|
|
|
-record(service_limits, {local :: #limits{},
|
|
|
|
remote :: #limits{}}).
|
|
|
|
|
|
|
|
-type routing() :: route_single | {route_multicast, binary(), #service_limits{}}.
|
|
|
|
|
|
|
|
-record(group, {server :: binary(),
|
|
|
|
dests :: [#dest{}],
|
|
|
|
multicast :: routing(),
|
|
|
|
others :: [#address{}],
|
|
|
|
addresses :: [#address{}]}).
|
|
|
|
|
|
|
|
-record(state, {lserver :: binary(),
|
|
|
|
lservice :: binary(),
|
|
|
|
access :: atom(),
|
|
|
|
service_limits :: #service_limits{}}).
|
2016-08-04 10:49:17 +02:00
|
|
|
-type state() :: #state{}.
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%% All the elements are of type value()
|
|
|
|
|
|
|
|
-define(VERSION_MULTICAST, <<"$Revision: 440 $ ">>).
|
|
|
|
|
|
|
|
-define(PURGE_PROCNAME,
|
|
|
|
ejabberd_mod_multicast_purgeloop).
|
|
|
|
|
|
|
|
-define(MAXTIME_CACHE_POSITIVE, 86400).
|
|
|
|
|
|
|
|
-define(MAXTIME_CACHE_NEGATIVE, 86400).
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-define(MAXTIME_CACHE_NEGOTIATING, 600).
|
|
|
|
|
2015-04-08 14:01:16 +02:00
|
|
|
-define(CACHE_PURGE_TIMER, 86400000).
|
|
|
|
|
|
|
|
-define(DISCO_QUERY_TIMEOUT, 10000).
|
|
|
|
|
|
|
|
-define(DEFAULT_LIMIT_LOCAL_MESSAGE, 100).
|
|
|
|
|
|
|
|
-define(DEFAULT_LIMIT_LOCAL_PRESENCE, 100).
|
|
|
|
|
|
|
|
-define(DEFAULT_LIMIT_REMOTE_MESSAGE, 20).
|
|
|
|
|
|
|
|
-define(DEFAULT_LIMIT_REMOTE_PRESENCE, 20).
|
|
|
|
|
|
|
|
start(LServerS, Opts) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:start_child(?MODULE, LServerS, Opts).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
stop(LServerS) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:stop_child(?MODULE, LServerS).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(LServerS, NewOpts, OldOpts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(LServerS, ?MODULE),
|
|
|
|
gen_server:cast(Proc, {reload, NewOpts, OldOpts}).
|
|
|
|
|
2015-04-08 14:01:16 +02:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec init(list()) -> {ok, state()}.
|
2015-04-08 14:01:16 +02:00
|
|
|
init([LServerS, Opts]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2018-01-23 08:54:52 +01:00
|
|
|
[LServiceS|_] = gen_mod:get_opt_hosts(LServerS, Opts),
|
|
|
|
Access = gen_mod:get_opt(access, Opts),
|
|
|
|
SLimits = build_service_limit_record(gen_mod:get_opt(limits, Opts)),
|
2015-04-08 14:01:16 +02:00
|
|
|
create_cache(),
|
|
|
|
try_start_loop(),
|
|
|
|
ejabberd_router_multicast:register_route(LServerS),
|
2016-03-13 09:38:40 +01:00
|
|
|
ejabberd_router:register_route(LServiceS, LServerS),
|
2015-04-08 14:01:16 +02:00
|
|
|
{ok,
|
|
|
|
#state{lservice = LServiceS, lserver = LServerS,
|
|
|
|
access = Access, service_limits = SLimits}}.
|
|
|
|
|
|
|
|
handle_call(stop, _From, State) ->
|
|
|
|
try_stop_loop(), {stop, normal, ok, State}.
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
handle_cast({reload, NewOpts, NewOpts},
|
|
|
|
#state{lserver = LServerS, lservice = OldLServiceS} = State) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
Access = gen_mod:get_opt(access, NewOpts),
|
|
|
|
SLimits = build_service_limit_record(gen_mod:get_opt(limits, NewOpts)),
|
|
|
|
[NewLServiceS|_] = gen_mod:get_opt_hosts(LServerS, NewOpts),
|
2017-02-22 17:46:47 +01:00
|
|
|
if NewLServiceS /= OldLServiceS ->
|
|
|
|
ejabberd_router:register_route(NewLServiceS, LServerS),
|
|
|
|
ejabberd_router:unregister_route(OldLServiceS);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
{noreply, State#state{lservice = NewLServiceS,
|
|
|
|
access = Access, service_limits = SLimits}};
|
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("unexpected cast: ~p", [Msg]),
|
|
|
|
{noreply, State}.
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_info({route, #iq{} = Packet}, State) ->
|
|
|
|
case catch handle_iq(Packet, State) of
|
2015-08-05 18:17:58 +02:00
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("Error when processing IQ stanza: ~p",
|
|
|
|
[Reason]);
|
|
|
|
_ -> ok
|
2015-04-08 14:01:16 +02:00
|
|
|
end,
|
|
|
|
{noreply, State};
|
|
|
|
%% XEP33 allows only 'message' and 'presence' stanza type
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_info({route, Packet},
|
2015-04-08 14:01:16 +02:00
|
|
|
#state{lservice = LServiceS, lserver = LServerS,
|
|
|
|
access = Access, service_limits = SLimits} =
|
2016-08-04 10:49:17 +02:00
|
|
|
State) when ?is_stanza(Packet) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_untrusted(LServiceS, LServerS, Access, SLimits, Packet),
|
2015-04-08 14:01:16 +02:00
|
|
|
{noreply, State};
|
|
|
|
%% Handle multicast packets sent by trusted local services
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_info({route_trusted, Destinations, Packet},
|
2015-04-08 14:01:16 +02:00
|
|
|
#state{lservice = LServiceS, lserver = LServerS} =
|
|
|
|
State) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
From = xmpp:get_from(Packet),
|
2015-08-05 18:17:58 +02:00
|
|
|
case catch route_trusted(LServiceS, LServerS, From, Destinations,
|
|
|
|
Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("Error in route_trusted: ~p", [Reason]);
|
|
|
|
_ -> ok
|
|
|
|
end,
|
2015-04-08 14:01:16 +02:00
|
|
|
{noreply, State};
|
|
|
|
handle_info({get_host, Pid}, State) ->
|
2015-08-05 18:17:58 +02:00
|
|
|
Pid ! {my_host, State#state.lservice},
|
|
|
|
{noreply, State};
|
2015-04-08 14:01:16 +02:00
|
|
|
handle_info(_Info, State) -> {noreply, State}.
|
|
|
|
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
ejabberd_router_multicast:unregister_route(State#state.lserver),
|
|
|
|
ejabberd_router:unregister_route(State#state.lservice),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%%%------------------------
|
|
|
|
%%% IQ Request Processing
|
|
|
|
%%%------------------------
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
handle_iq(Packet, State) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
try
|
|
|
|
IQ = xmpp:decode_els(Packet),
|
2017-02-16 09:00:26 +01:00
|
|
|
case process_iq(IQ, State) of
|
2016-08-04 10:49:17 +02:00
|
|
|
{result, SubEl} ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(xmpp:make_iq_result(Packet, SubEl));
|
2016-08-04 10:49:17 +02:00
|
|
|
{error, Error} ->
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Error);
|
2016-08-04 10:49:17 +02:00
|
|
|
reply ->
|
2017-02-16 09:00:26 +01:00
|
|
|
To = xmpp:get_to(IQ),
|
2017-02-26 08:07:12 +01:00
|
|
|
LServiceS = jid:encode(To),
|
2016-08-04 10:49:17 +02:00
|
|
|
case Packet#iq.type of
|
|
|
|
result ->
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iqreply_result(LServiceS, IQ);
|
2016-08-04 10:49:17 +02:00
|
|
|
error ->
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iqreply_error(LServiceS, IQ)
|
2016-08-04 10:49:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
catch _:{xmpp_codec, Why} ->
|
|
|
|
Lang = xmpp:get_lang(Packet),
|
2017-11-14 07:02:43 +01:00
|
|
|
Err = xmpp:err_bad_request(xmpp:io_format_error(Why), Lang),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err)
|
2015-08-05 18:17:58 +02:00
|
|
|
end.
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec process_iq(iq(), state()) -> {result, xmpp_element()} |
|
|
|
|
{error, stanza_error()} | reply.
|
|
|
|
process_iq(#iq{type = get, lang = Lang, from = From,
|
|
|
|
sub_els = [#disco_info{}]}, State) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
{result, iq_disco_info(From, Lang, State)};
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iq(#iq{type = get, sub_els = [#disco_items{}]}, _) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
{result, #disco_items{}};
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iq(#iq{type = get, lang = Lang, sub_els = [#vcard_temp{}]}, _) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
{result, iq_vcard(Lang)};
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iq(#iq{type = T}, _) when T == set; T == get ->
|
2016-08-04 10:49:17 +02:00
|
|
|
{error, xmpp:err_service_unavailable()};
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iq(_, _) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
reply.
|
|
|
|
|
|
|
|
-define(FEATURE(Feat), Feat).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
iq_disco_info(From, Lang, State) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
Name = gen_mod:get_module_opt(State#state.lserver, ?MODULE, name),
|
2016-08-04 10:49:17 +02:00
|
|
|
#disco_info{
|
|
|
|
identities = [#identity{category = <<"service">>,
|
|
|
|
type = <<"multicast">>,
|
2018-01-08 09:29:17 +01:00
|
|
|
name = translate:translate(Lang, Name)}],
|
2016-08-04 10:49:17 +02:00
|
|
|
features = [?NS_DISCO_INFO, ?NS_DISCO_ITEMS, ?NS_VCARD, ?NS_ADDRESS],
|
|
|
|
xdata = iq_disco_info_extras(From, State)}.
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
iq_vcard(Lang) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
#vcard_temp{fn = <<"ejabberd/mod_multicast">>,
|
2018-06-14 13:00:47 +02:00
|
|
|
url = ejabberd_config:get_uri(),
|
|
|
|
desc = misc:get_descr(Lang, <<"ejabberd Multicast service">>)}.
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Route
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec route_trusted(binary(), binary(), jid(), [jid()], stanza()) -> 'ok'.
|
2015-04-08 14:01:16 +02:00
|
|
|
route_trusted(LServiceS, LServerS, FromJID,
|
|
|
|
Destinations, Packet) ->
|
|
|
|
Packet_stripped = Packet,
|
|
|
|
Delivereds = [],
|
2016-08-04 10:49:17 +02:00
|
|
|
Dests2 = lists:map(
|
|
|
|
fun(D) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
#dest{jid_string = jid:encode(D),
|
2018-04-04 12:05:37 +02:00
|
|
|
jid_jid = D, type = bcc,
|
|
|
|
address = #address{type = bcc, jid = D}}
|
2016-08-04 10:49:17 +02:00
|
|
|
end, Destinations),
|
2015-04-08 14:01:16 +02:00
|
|
|
Groups = group_dests(Dests2),
|
|
|
|
route_common(LServerS, LServiceS, FromJID, Groups,
|
2018-04-04 12:05:37 +02:00
|
|
|
Delivereds, Packet_stripped).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec route_untrusted(binary(), binary(), atom(), #service_limits{}, stanza()) -> 'ok'.
|
2017-02-16 09:00:26 +01:00
|
|
|
route_untrusted(LServiceS, LServerS, Access, SLimits, Packet) ->
|
2015-04-08 14:01:16 +02:00
|
|
|
try route_untrusted2(LServiceS, LServerS, Access,
|
2017-02-16 09:00:26 +01:00
|
|
|
SLimits, Packet)
|
2015-04-08 14:01:16 +02:00
|
|
|
catch
|
|
|
|
adenied ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_error(Packet, forbidden,
|
2015-04-08 14:01:16 +02:00
|
|
|
<<"Access denied by service policy">>);
|
|
|
|
eadsele ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_error(Packet, bad_request,
|
2015-04-08 14:01:16 +02:00
|
|
|
<<"No addresses element found">>);
|
|
|
|
eadeles ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_error(Packet, bad_request,
|
2015-04-08 14:01:16 +02:00
|
|
|
<<"No address elements found">>);
|
|
|
|
ewxmlns ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_error(Packet, bad_request,
|
2015-04-08 14:01:16 +02:00
|
|
|
<<"Wrong xmlns">>);
|
|
|
|
etoorec ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_error(Packet, not_acceptable,
|
2015-04-08 14:01:16 +02:00
|
|
|
<<"Too many receiver fields were specified">>);
|
|
|
|
edrelay ->
|
2017-02-16 09:00:26 +01:00
|
|
|
route_error(Packet, forbidden,
|
2015-04-08 14:01:16 +02:00
|
|
|
<<"Packet relay is denied by service policy">>);
|
|
|
|
EType:EReason ->
|
|
|
|
?ERROR_MSG("Multicast unknown error: Type: ~p~nReason: ~p",
|
|
|
|
[EType, EReason]),
|
2017-02-16 09:00:26 +01:00
|
|
|
route_error(Packet, internal_server_error,
|
2015-04-08 14:01:16 +02:00
|
|
|
<<"Unknown problem">>)
|
|
|
|
end.
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec route_untrusted2(binary(), binary(), atom(), #service_limits{}, stanza()) -> 'ok'.
|
2017-02-16 09:00:26 +01:00
|
|
|
route_untrusted2(LServiceS, LServerS, Access, SLimits, Packet) ->
|
|
|
|
FromJID = xmpp:get_from(Packet),
|
2015-04-08 14:01:16 +02:00
|
|
|
ok = check_access(LServerS, Access, FromJID),
|
2016-08-04 10:49:17 +02:00
|
|
|
{ok, Packet_stripped, Addresses} = strip_addresses_element(Packet),
|
|
|
|
{To_deliver, Delivereds} = split_addresses_todeliver(Addresses),
|
2015-04-08 14:01:16 +02:00
|
|
|
Dests = convert_dest_record(To_deliver),
|
|
|
|
{Dests2, Not_jids} = split_dests_jid(Dests),
|
|
|
|
report_not_jid(FromJID, Packet, Not_jids),
|
2016-08-04 10:49:17 +02:00
|
|
|
ok = check_limit_dests(SLimits, FromJID, Packet, Dests2),
|
2015-04-08 14:01:16 +02:00
|
|
|
Groups = group_dests(Dests2),
|
|
|
|
ok = check_relay(FromJID#jid.server, LServerS, Groups),
|
|
|
|
route_common(LServerS, LServiceS, FromJID, Groups,
|
2018-04-04 12:05:37 +02:00
|
|
|
Delivereds, Packet_stripped).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec route_common(binary(), binary(), jid(), [#group{}],
|
2018-04-04 12:05:37 +02:00
|
|
|
[address()], stanza()) -> 'ok'.
|
2015-04-08 14:01:16 +02:00
|
|
|
route_common(LServerS, LServiceS, FromJID, Groups,
|
2018-04-04 12:05:37 +02:00
|
|
|
Delivereds, Packet_stripped) ->
|
|
|
|
Groups2 = look_cached_servers(LServerS, LServiceS, Groups),
|
2015-04-08 14:01:16 +02:00
|
|
|
Groups3 = build_others_xml(Groups2),
|
|
|
|
Groups4 = add_addresses(Delivereds, Groups3),
|
|
|
|
AGroups = decide_action_groups(Groups4),
|
2018-04-04 12:05:37 +02:00
|
|
|
act_groups(FromJID, Packet_stripped, LServiceS,
|
2015-04-08 14:01:16 +02:00
|
|
|
AGroups).
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec act_groups(jid(), stanza(), binary(), [{routing(), #group{}}]) -> 'ok'.
|
|
|
|
act_groups(FromJID, Packet_stripped, LServiceS, AGroups) ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(AGroup) ->
|
|
|
|
perform(FromJID, Packet_stripped, LServiceS,
|
|
|
|
AGroup)
|
|
|
|
end, AGroups).
|
|
|
|
|
|
|
|
-spec perform(jid(), stanza(), binary(),
|
|
|
|
{routing(), #group{}}) -> 'ok'.
|
|
|
|
perform(From, Packet, _,
|
2015-04-08 14:01:16 +02:00
|
|
|
{route_single, Group}) ->
|
2018-04-04 12:05:37 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(ToUser) ->
|
|
|
|
route_packet(From, ToUser, Packet,
|
|
|
|
Group#group.others, Group#group.addresses)
|
|
|
|
end, Group#group.dests);
|
|
|
|
perform(From, Packet, _,
|
2015-04-08 14:01:16 +02:00
|
|
|
{{route_multicast, JID, RLimits}, Group}) ->
|
2018-04-04 12:05:37 +02:00
|
|
|
route_packet_multicast(From, JID, Packet,
|
|
|
|
Group#group.dests, Group#group.addresses, RLimits).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check access permission
|
|
|
|
%%%-------------------------
|
|
|
|
|
|
|
|
check_access(LServerS, Access, From) ->
|
|
|
|
case acl:match_rule(LServerS, Access, From) of
|
|
|
|
allow -> ok;
|
|
|
|
_ -> throw(adenied)
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Strip 'addresses' XML element
|
|
|
|
%%%-------------------------
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec strip_addresses_element(stanza()) -> {ok, stanza(), [address()]}.
|
2015-04-08 14:01:16 +02:00
|
|
|
strip_addresses_element(Packet) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
case xmpp:get_subtag(Packet, #addresses{}) of
|
|
|
|
#addresses{list = Addrs} ->
|
|
|
|
PacketStripped = xmpp:remove_subtag(Packet, #addresses{}),
|
|
|
|
{ok, PacketStripped, Addrs};
|
2017-12-11 07:46:26 +01:00
|
|
|
false ->
|
2016-08-04 10:49:17 +02:00
|
|
|
throw(eadsele)
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Split Addresses
|
|
|
|
%%%-------------------------
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec split_addresses_todeliver([address()]) -> {[address()], [address()]}.
|
2015-04-08 14:01:16 +02:00
|
|
|
split_addresses_todeliver(Addresses) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
lists:partition(
|
|
|
|
fun(#address{delivered = true}) ->
|
|
|
|
false;
|
|
|
|
(#address{type = Type}) ->
|
|
|
|
case Type of
|
|
|
|
to -> true;
|
|
|
|
cc -> true;
|
|
|
|
bcc -> true;
|
|
|
|
_ -> false
|
|
|
|
end
|
|
|
|
end, Addresses).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check does not exceed limit of destinations
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec check_limit_dests(#service_limits{}, jid(), stanza(), [address()]) -> ok.
|
2015-04-08 14:01:16 +02:00
|
|
|
check_limit_dests(SLimits, FromJID, Packet,
|
|
|
|
Addresses) ->
|
|
|
|
SenderT = sender_type(FromJID),
|
|
|
|
Limits = get_slimit_group(SenderT, SLimits),
|
|
|
|
Type_of_stanza = type_of_stanza(Packet),
|
|
|
|
{_Type, Limit_number} = get_limit_number(Type_of_stanza,
|
|
|
|
Limits),
|
|
|
|
case length(Addresses) > Limit_number of
|
|
|
|
false -> ok;
|
|
|
|
true -> throw(etoorec)
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Convert Destination XML to record
|
|
|
|
%%%-------------------------
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec convert_dest_record([address()]) -> [#dest{}].
|
|
|
|
convert_dest_record(Addrs) ->
|
|
|
|
lists:map(
|
|
|
|
fun(#address{jid = undefined} = Addr) ->
|
2018-04-04 12:05:37 +02:00
|
|
|
#dest{jid_string = none, address = Addr};
|
2016-08-04 10:49:17 +02:00
|
|
|
(#address{jid = JID, type = Type} = Addr) ->
|
2017-02-26 08:07:12 +01:00
|
|
|
#dest{jid_string = jid:encode(JID), jid_jid = JID,
|
2018-04-04 12:05:37 +02:00
|
|
|
type = Type, address = Addr}
|
2016-08-04 10:49:17 +02:00
|
|
|
end, Addrs).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Split destinations by existence of JID
|
|
|
|
%%% and send error messages for other dests
|
|
|
|
%%%-------------------------
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec split_dests_jid([#dest{}]) -> {[#dest{}], [#dest{}]}.
|
2015-04-08 14:01:16 +02:00
|
|
|
split_dests_jid(Dests) ->
|
|
|
|
lists:partition(fun (Dest) ->
|
|
|
|
case Dest#dest.jid_string of
|
|
|
|
none -> false;
|
|
|
|
_ -> true
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Dests).
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec report_not_jid(jid(), stanza(), [#dest{}]) -> any().
|
2015-04-08 14:01:16 +02:00
|
|
|
report_not_jid(From, Packet, Dests) ->
|
2018-04-04 12:05:37 +02:00
|
|
|
Dests2 = [fxml:element_to_binary(xmpp:encode(Dest#dest.address))
|
2015-04-08 14:01:16 +02:00
|
|
|
|| Dest <- Dests],
|
2017-02-16 09:00:26 +01:00
|
|
|
[route_error(xmpp:set_from_to(Packet, From, From), jid_malformed,
|
2015-04-08 14:01:16 +02:00
|
|
|
<<"This service can not process the address: ",
|
|
|
|
D/binary>>)
|
|
|
|
|| D <- Dests2].
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Group destinations by their servers
|
|
|
|
%%%-------------------------
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec group_dests([#dest{}]) -> [#group{}].
|
2015-04-08 14:01:16 +02:00
|
|
|
group_dests(Dests) ->
|
|
|
|
D = lists:foldl(fun (Dest, Dict) ->
|
|
|
|
ServerS = (Dest#dest.jid_jid)#jid.server,
|
|
|
|
dict:append(ServerS, Dest, Dict)
|
|
|
|
end,
|
|
|
|
dict:new(), Dests),
|
|
|
|
Keys = dict:fetch_keys(D),
|
|
|
|
[#group{server = Key, dests = dict:fetch(Key, D)}
|
|
|
|
|| Key <- Keys].
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Look for cached responses
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
look_cached_servers(LServerS, LServiceS, Groups) ->
|
|
|
|
[look_cached(LServerS, LServiceS, Group) || Group <- Groups].
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
look_cached(LServerS, LServiceS, G) ->
|
2015-04-08 14:01:16 +02:00
|
|
|
Maxtime_positive = (?MAXTIME_CACHE_POSITIVE),
|
|
|
|
Maxtime_negative = (?MAXTIME_CACHE_NEGATIVE),
|
|
|
|
Cached_response = search_server_on_cache(G#group.server,
|
2018-04-04 12:05:37 +02:00
|
|
|
LServerS, LServiceS,
|
2015-04-08 14:01:16 +02:00
|
|
|
{Maxtime_positive,
|
|
|
|
Maxtime_negative}),
|
|
|
|
G#group{multicast = Cached_response}.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Build delivered XML element
|
|
|
|
%%%-------------------------
|
|
|
|
|
|
|
|
build_others_xml(Groups) ->
|
|
|
|
[Group#group{others =
|
|
|
|
build_other_xml(Group#group.dests)}
|
|
|
|
|| Group <- Groups].
|
|
|
|
|
|
|
|
build_other_xml(Dests) ->
|
|
|
|
lists:foldl(fun (Dest, R) ->
|
2018-04-04 12:05:37 +02:00
|
|
|
XML = Dest#dest.address,
|
2015-04-08 14:01:16 +02:00
|
|
|
case Dest#dest.type of
|
2016-08-04 10:49:17 +02:00
|
|
|
to -> [add_delivered(XML) | R];
|
|
|
|
cc -> [add_delivered(XML) | R];
|
|
|
|
bcc -> R;
|
2015-04-08 14:01:16 +02:00
|
|
|
_ -> [XML | R]
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], Dests).
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec add_delivered(address()) -> address().
|
|
|
|
add_delivered(Addr) ->
|
|
|
|
Addr#address{delivered = true}.
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Add preliminary packets
|
|
|
|
%%%-------------------------
|
|
|
|
|
|
|
|
add_addresses(Delivereds, Groups) ->
|
|
|
|
Ps = [Group#group.others || Group <- Groups],
|
|
|
|
add_addresses2(Delivereds, Groups, [], [], Ps).
|
|
|
|
|
|
|
|
add_addresses2(_, [], Res, _, []) -> Res;
|
|
|
|
add_addresses2(Delivereds, [Group | Groups], Res, Pa,
|
|
|
|
[Pi | Pz]) ->
|
|
|
|
Addresses = lists:append([Delivereds] ++ Pa ++ Pz),
|
|
|
|
Group2 = Group#group{addresses = Addresses},
|
|
|
|
add_addresses2(Delivereds, Groups, [Group2 | Res],
|
|
|
|
[Pi | Pa], Pz).
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Decide action groups
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec decide_action_groups([#group{}]) -> [{routing(), #group{}}].
|
2015-04-08 14:01:16 +02:00
|
|
|
decide_action_groups(Groups) ->
|
2018-04-04 12:05:37 +02:00
|
|
|
[{Group#group.multicast, Group}
|
2015-04-08 14:01:16 +02:00
|
|
|
|| Group <- Groups].
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Route packet
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec route_packet(jid(), #dest{}, xmpp:stanza(), [addresses()], [addresses()]) -> 'ok'.
|
|
|
|
route_packet(From, ToDest, Packet, Others, Addresses) ->
|
2015-04-08 14:01:16 +02:00
|
|
|
Dests = case ToDest#dest.type of
|
2016-08-04 10:49:17 +02:00
|
|
|
bcc -> [];
|
2015-04-08 14:01:16 +02:00
|
|
|
_ -> [ToDest]
|
|
|
|
end,
|
|
|
|
route_packet2(From, ToDest#dest.jid_string, Dests,
|
2018-04-04 12:05:37 +02:00
|
|
|
Packet, {Others, Addresses}).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec route_packet_multicast(jid(), binary(), xmpp:stanza(), [#dest{}], [address()], #limits{}) -> 'ok'.
|
|
|
|
route_packet_multicast(From, ToS, Packet, Dests,
|
2015-04-08 14:01:16 +02:00
|
|
|
Addresses, Limits) ->
|
|
|
|
Type_of_stanza = type_of_stanza(Packet),
|
|
|
|
{_Type, Limit_number} = get_limit_number(Type_of_stanza,
|
|
|
|
Limits),
|
|
|
|
Fragmented_dests = fragment_dests(Dests, Limit_number),
|
2018-04-04 12:05:37 +02:00
|
|
|
lists:foreach(fun(DFragment) ->
|
|
|
|
route_packet2(From, ToS, DFragment, Packet,
|
|
|
|
Addresses)
|
|
|
|
end, Fragmented_dests).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec route_packet2(jid(), binary(), [#dest{}], xmpp:stanza(), {[address()], [address()]} | [address()]) -> 'ok'.
|
|
|
|
route_packet2(From, ToS, Dests, Packet, Addresses) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
Els = case append_dests(Dests, Addresses) of
|
|
|
|
[] ->
|
|
|
|
xmpp:get_els(Packet);
|
|
|
|
ACs ->
|
|
|
|
[#addresses{list = ACs}|xmpp:get_els(Packet)]
|
|
|
|
end,
|
|
|
|
Packet2 = xmpp:set_els(Packet, Els),
|
2015-04-08 14:01:16 +02:00
|
|
|
ToJID = stj(ToS),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(xmpp:set_from_to(Packet2, From, ToJID)).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec append_dests([#dest{}], {[address()], [address()]} | [address()]) -> [address()].
|
2016-03-14 12:53:14 +01:00
|
|
|
append_dests(_Dests, {Others, Addresses}) ->
|
2018-04-04 12:05:37 +02:00
|
|
|
Addresses ++ Others;
|
2015-04-08 14:01:16 +02:00
|
|
|
append_dests([], Addresses) -> Addresses;
|
|
|
|
append_dests([Dest | Dests], Addresses) ->
|
2018-04-04 12:05:37 +02:00
|
|
|
append_dests(Dests, [Dest#dest.address | Addresses]).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check relay
|
|
|
|
%%%-------------------------
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec check_relay(binary(), binary(), [#group{}]) -> ok.
|
2015-04-08 14:01:16 +02:00
|
|
|
check_relay(RS, LS, Gs) ->
|
|
|
|
case check_relay_required(RS, LS, Gs) of
|
|
|
|
false -> ok;
|
|
|
|
true -> throw(edrelay)
|
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec check_relay_required(binary(), binary(), [#group{}]) -> boolean().
|
2015-04-08 14:01:16 +02:00
|
|
|
check_relay_required(RServer, LServerS, Groups) ->
|
2015-08-05 18:17:58 +02:00
|
|
|
case lists:suffix(str:tokens(LServerS, <<".">>),
|
|
|
|
str:tokens(RServer, <<".">>)) of
|
2015-04-08 14:01:16 +02:00
|
|
|
true -> false;
|
|
|
|
false -> check_relay_required(LServerS, Groups)
|
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec check_relay_required(binary(), [#group{}]) -> boolean().
|
2015-04-08 14:01:16 +02:00
|
|
|
check_relay_required(LServerS, Groups) ->
|
|
|
|
lists:any(fun (Group) -> Group#group.server /= LServerS
|
|
|
|
end,
|
|
|
|
Groups).
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check protocol support: Send request
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec send_query_info(binary(), binary(), binary()) -> ok.
|
|
|
|
send_query_info(RServerS, LServiceS, ID) ->
|
2015-04-08 14:01:16 +02:00
|
|
|
case str:str(RServerS, <<"echo.">>) of
|
2018-04-04 12:05:37 +02:00
|
|
|
1 -> ok;
|
|
|
|
_ -> send_query(RServerS, LServiceS, ID, #disco_info{})
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec send_query_items(binary(), binary(), binary()) -> ok.
|
|
|
|
send_query_items(RServerS, LServiceS, ID) ->
|
|
|
|
send_query(RServerS, LServiceS, ID, #disco_items{}).
|
2016-08-04 10:49:17 +02:00
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec send_query(binary(), binary(), binary(), disco_info()|disco_items()) -> ok.
|
|
|
|
send_query(RServerS, LServiceS, ID, SubEl) ->
|
2017-02-16 09:00:26 +01:00
|
|
|
Packet = #iq{from = stj(LServiceS),
|
|
|
|
to = stj(RServerS),
|
2018-04-04 12:05:37 +02:00
|
|
|
id = ID,
|
2016-08-04 10:49:17 +02:00
|
|
|
type = get, sub_els = [SubEl]},
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route(Packet).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check protocol support: Receive response: Error
|
|
|
|
%%%-------------------------
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
process_iqreply_error(LServiceS, Packet) ->
|
|
|
|
FromS = jts(xmpp:get_from(Packet)),
|
2018-04-04 12:05:37 +02:00
|
|
|
ID = Packet#iq.id,
|
|
|
|
case str:tokens(ID, <<"/">>) of
|
|
|
|
[RServer, _] ->
|
|
|
|
case look_server(RServer) of
|
|
|
|
{cached, {_Response, {wait_for_info, ID}}, _TS}
|
|
|
|
when RServer == FromS ->
|
|
|
|
add_response(RServer, not_supported, cached);
|
|
|
|
{cached, {_Response, {wait_for_items, ID}}, _TS}
|
|
|
|
when RServer == FromS ->
|
|
|
|
add_response(RServer, not_supported, cached);
|
|
|
|
{cached, {Response, {wait_for_items_info, ID, Items}},
|
|
|
|
_TS} ->
|
|
|
|
case lists:member(FromS, Items) of
|
|
|
|
true ->
|
|
|
|
received_awaiter(
|
|
|
|
FromS, RServer, Response, ID, Items,
|
|
|
|
LServiceS);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check protocol support: Receive response: Disco
|
|
|
|
%%%-------------------------
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
-spec process_iqreply_result(binary(), iq()) -> any().
|
2018-04-04 12:05:37 +02:00
|
|
|
process_iqreply_result(LServiceS, #iq{from = From, id = ID, sub_els = [SubEl]}) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
case SubEl of
|
|
|
|
#disco_info{} ->
|
2018-04-04 12:05:37 +02:00
|
|
|
process_discoinfo_result(From, LServiceS, ID, SubEl);
|
2016-08-04 10:49:17 +02:00
|
|
|
#disco_items{} ->
|
2018-04-04 12:05:37 +02:00
|
|
|
process_discoitems_result(From, LServiceS, ID, SubEl);
|
2016-08-04 10:49:17 +02:00
|
|
|
_ ->
|
|
|
|
ok
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check protocol support: Receive response: Disco Info
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
process_discoinfo_result(From, LServiceS, ID, DiscoInfo) ->
|
2015-04-08 14:01:16 +02:00
|
|
|
FromS = jts(From),
|
2018-04-04 12:05:37 +02:00
|
|
|
case str:tokens(ID, <<"/">>) of
|
|
|
|
[RServer, _] ->
|
|
|
|
case look_server(RServer) of
|
|
|
|
{cached, {Response, {wait_for_info, ID} = ST}, _TS}
|
|
|
|
when RServer == FromS ->
|
|
|
|
process_discoinfo_result2(
|
|
|
|
From, FromS, LServiceS, DiscoInfo,
|
|
|
|
RServer, Response, ST);
|
|
|
|
{cached, {Response, {wait_for_items_info, ID, Items} = ST},
|
|
|
|
_TS} ->
|
|
|
|
case lists:member(FromS, Items) of
|
|
|
|
true ->
|
|
|
|
process_discoinfo_result2(
|
|
|
|
From, FromS, LServiceS, DiscoInfo,
|
|
|
|
RServer, Response, ST);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
process_discoinfo_result2(From, FromS, LServiceS,
|
|
|
|
#disco_info{features = Feats} = DiscoInfo,
|
2018-04-04 12:05:37 +02:00
|
|
|
RServer, Response, ST) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
Multicast_support = lists:member(?NS_ADDRESS, Feats),
|
2015-04-08 14:01:16 +02:00
|
|
|
case Multicast_support of
|
2015-10-07 00:06:58 +02:00
|
|
|
true ->
|
|
|
|
SenderT = sender_type(From),
|
2016-08-04 10:49:17 +02:00
|
|
|
RLimits = get_limits_xml(DiscoInfo, SenderT),
|
2018-04-04 12:05:37 +02:00
|
|
|
add_response(RServer, {multicast_supported, FromS, RLimits}, cached);
|
2015-10-07 00:06:58 +02:00
|
|
|
false ->
|
2018-04-04 12:05:37 +02:00
|
|
|
case ST of
|
|
|
|
{wait_for_info, _ID} ->
|
|
|
|
Random = randoms:get_string(),
|
|
|
|
ID = <<RServer/binary, $/, Random/binary>>,
|
|
|
|
send_query_items(FromS, LServiceS, ID),
|
|
|
|
add_response(RServer, Response, {wait_for_items, ID});
|
|
|
|
%% We asked a component, and it does not support XEP33
|
|
|
|
{wait_for_items_info, ID, Items} ->
|
|
|
|
received_awaiter(FromS, RServer, Response, ID, Items, LServiceS)
|
|
|
|
end
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
get_limits_xml(DiscoInfo, SenderT) ->
|
|
|
|
LimitOpts = get_limits_els(DiscoInfo),
|
2015-04-08 14:01:16 +02:00
|
|
|
build_remote_limit_record(LimitOpts, SenderT).
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec get_limits_els(disco_info()) -> [{atom(), integer()}].
|
|
|
|
get_limits_els(DiscoInfo) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun(#xdata{type = result} = X) ->
|
|
|
|
get_limits_fields(X);
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, DiscoInfo#disco_info.xdata).
|
|
|
|
|
|
|
|
-spec get_limits_fields(xdata()) -> [{atom(), integer()}].
|
|
|
|
get_limits_fields(X) ->
|
|
|
|
{Head, Tail} = lists:partition(
|
|
|
|
fun(#xdata_field{var = Var, type = Type}) ->
|
|
|
|
Var == <<"FORM_TYPE">> andalso Type == hidden
|
|
|
|
end, X#xdata.fields),
|
2015-04-08 14:01:16 +02:00
|
|
|
case Head of
|
|
|
|
[] -> [];
|
|
|
|
_ -> get_limits_values(Tail)
|
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
-spec get_limits_values([xdata_field()]) -> [{atom(), integer()}].
|
|
|
|
get_limits_values(Fields) ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun(#xdata_field{var = Name, values = [Number]}) ->
|
|
|
|
try
|
|
|
|
[{binary_to_atom(Name, utf8), binary_to_integer(Number)}]
|
|
|
|
catch _:badarg ->
|
|
|
|
[]
|
|
|
|
end;
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Fields).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check protocol support: Receive response: Disco Items
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
process_discoitems_result(From, LServiceS, ID, #disco_items{items = Items}) ->
|
2015-04-08 14:01:16 +02:00
|
|
|
FromS = jts(From),
|
2018-04-04 12:05:37 +02:00
|
|
|
case str:tokens(ID, <<"/">>) of
|
|
|
|
[FromS = RServer, _] ->
|
|
|
|
case look_server(RServer) of
|
|
|
|
{cached, {Response, {wait_for_items, ID}}, _TS} ->
|
|
|
|
List = lists:flatmap(
|
|
|
|
fun(#disco_item{jid = #jid{luser = <<"">>,
|
|
|
|
lserver = LServer,
|
|
|
|
lresource = <<"">>}}) ->
|
|
|
|
[LServer];
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end, Items),
|
|
|
|
case List of
|
|
|
|
[] ->
|
|
|
|
add_response(RServer, not_supported, cached);
|
|
|
|
_ ->
|
|
|
|
Random = randoms:get_string(),
|
|
|
|
ID2 = <<RServer/binary, $/, Random/binary>>,
|
|
|
|
[send_query_info(Item, LServiceS, ID2) || Item <- List],
|
|
|
|
add_response(RServer, Response,
|
|
|
|
{wait_for_items_info, ID2, List})
|
|
|
|
end;
|
2015-08-05 18:17:58 +02:00
|
|
|
_ ->
|
2018-04-04 12:05:37 +02:00
|
|
|
ok
|
2015-08-05 18:17:58 +02:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Check protocol support: Receive response: Received awaiter
|
|
|
|
%%%-------------------------
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
received_awaiter(JID, RServer, Response, ID, JIDs, _LServiceS) ->
|
2015-04-08 14:01:16 +02:00
|
|
|
case lists:delete(JID, JIDs) of
|
2018-04-04 12:05:37 +02:00
|
|
|
[] ->
|
|
|
|
add_response(RServer, not_supported, cached);
|
|
|
|
JIDs2 ->
|
|
|
|
add_response(RServer, Response, {wait_for_items_info, ID, JIDs2})
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Cache
|
|
|
|
%%%-------------------------
|
|
|
|
|
|
|
|
create_cache() ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, multicastc,
|
2015-04-08 14:01:16 +02:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, multicastc)}]).
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
add_response(RServer, Response, State) ->
|
2015-12-04 15:08:43 +01:00
|
|
|
Secs = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
|
2015-04-08 14:01:16 +02:00
|
|
|
mnesia:dirty_write(#multicastc{rserver = RServer,
|
2018-04-04 12:05:37 +02:00
|
|
|
response = {Response, State}, ts = Secs}).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
search_server_on_cache(RServer, LServerS, _LServiceS, _Maxmins)
|
2015-04-08 14:01:16 +02:00
|
|
|
when RServer == LServerS ->
|
2018-04-04 12:05:37 +02:00
|
|
|
route_single;
|
|
|
|
search_server_on_cache(RServer, _LServerS, LServiceS, Maxmins) ->
|
2015-04-08 14:01:16 +02:00
|
|
|
case look_server(RServer) of
|
2018-04-04 12:05:37 +02:00
|
|
|
not_cached ->
|
|
|
|
query_info(RServer, LServiceS, not_supported),
|
|
|
|
route_single;
|
|
|
|
{cached, {Response, State}, TS} ->
|
|
|
|
Now = calendar:datetime_to_gregorian_seconds(calendar:local_time()),
|
|
|
|
Response2 =
|
|
|
|
case State of
|
|
|
|
cached ->
|
|
|
|
case is_obsolete(Response, TS, Now, Maxmins) of
|
|
|
|
false -> ok;
|
|
|
|
true ->
|
|
|
|
query_info(RServer, LServiceS, Response)
|
|
|
|
end,
|
|
|
|
Response;
|
|
|
|
_ ->
|
|
|
|
if
|
|
|
|
Now - TS > ?MAXTIME_CACHE_NEGOTIATING ->
|
|
|
|
query_info(RServer, LServiceS, not_supported),
|
|
|
|
not_supported;
|
|
|
|
true ->
|
|
|
|
Response
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
case Response2 of
|
|
|
|
not_supported -> route_single;
|
|
|
|
{multicast_supported, Service, Limits} ->
|
|
|
|
{route_multicast, Service, Limits}
|
|
|
|
end
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
query_info(RServer, LServiceS, Response) ->
|
|
|
|
Random = randoms:get_string(),
|
|
|
|
ID = <<RServer/binary, $/, Random/binary>>,
|
|
|
|
send_query_info(RServer, LServiceS, ID),
|
|
|
|
add_response(RServer, Response, {wait_for_info, ID}).
|
|
|
|
|
2015-04-08 14:01:16 +02:00
|
|
|
look_server(RServer) ->
|
|
|
|
case mnesia:dirty_read(multicastc, RServer) of
|
|
|
|
[] -> not_cached;
|
|
|
|
[M] -> {cached, M#multicastc.response, M#multicastc.ts}
|
|
|
|
end.
|
|
|
|
|
|
|
|
is_obsolete(Response, Ts, Now, {Max_pos, Max_neg}) ->
|
|
|
|
Max = case Response of
|
|
|
|
multicast_not_supported -> Max_neg;
|
|
|
|
_ -> Max_pos
|
|
|
|
end,
|
|
|
|
Now - Ts > Max.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Purge cache
|
|
|
|
%%%-------------------------
|
|
|
|
|
|
|
|
purge() ->
|
|
|
|
Maxmins_positive = (?MAXTIME_CACHE_POSITIVE),
|
|
|
|
Maxmins_negative = (?MAXTIME_CACHE_NEGATIVE),
|
|
|
|
Now =
|
2015-12-04 15:08:43 +01:00
|
|
|
calendar:datetime_to_gregorian_seconds(calendar:local_time()),
|
2015-04-08 14:01:16 +02:00
|
|
|
purge(Now, {Maxmins_positive, Maxmins_negative}).
|
|
|
|
|
|
|
|
purge(Now, Maxmins) ->
|
|
|
|
F = fun () ->
|
|
|
|
mnesia:foldl(fun (R, _) ->
|
|
|
|
#multicastc{response = Response, ts = Ts} =
|
|
|
|
R,
|
|
|
|
case is_obsolete(Response, Ts, Now,
|
|
|
|
Maxmins)
|
|
|
|
of
|
|
|
|
true -> mnesia:delete_object(R);
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
none, multicastc)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Purge cache loop
|
|
|
|
%%%-------------------------
|
|
|
|
|
|
|
|
try_start_loop() ->
|
|
|
|
case lists:member(?PURGE_PROCNAME, registered()) of
|
|
|
|
true -> ok;
|
|
|
|
false -> start_loop()
|
|
|
|
end,
|
|
|
|
(?PURGE_PROCNAME) ! new_module.
|
|
|
|
|
|
|
|
start_loop() ->
|
|
|
|
register(?PURGE_PROCNAME,
|
|
|
|
spawn(?MODULE, purge_loop, [0])),
|
|
|
|
(?PURGE_PROCNAME) ! purge_now.
|
|
|
|
|
|
|
|
try_stop_loop() -> (?PURGE_PROCNAME) ! try_stop.
|
|
|
|
|
|
|
|
purge_loop(NM) ->
|
|
|
|
receive
|
|
|
|
purge_now ->
|
|
|
|
purge(),
|
|
|
|
timer:send_after(?CACHE_PURGE_TIMER, ?PURGE_PROCNAME,
|
|
|
|
purge_now),
|
|
|
|
purge_loop(NM);
|
|
|
|
new_module -> purge_loop(NM + 1);
|
|
|
|
try_stop when NM > 1 -> purge_loop(NM - 1);
|
|
|
|
try_stop -> purge_loop_finished
|
|
|
|
end.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Limits: utils
|
|
|
|
%%%-------------------------
|
|
|
|
|
|
|
|
%% Type definitions for data structures related with XEP33 limits
|
|
|
|
%% limit() = {Name, Value}
|
|
|
|
%% Name = atom()
|
|
|
|
%% Value = {Type, Number}
|
|
|
|
%% Type = default | custom
|
|
|
|
%% Number = integer() | infinite
|
|
|
|
|
|
|
|
list_of_limits(local) ->
|
|
|
|
[{message, ?DEFAULT_LIMIT_LOCAL_MESSAGE},
|
|
|
|
{presence, ?DEFAULT_LIMIT_LOCAL_PRESENCE}];
|
|
|
|
list_of_limits(remote) ->
|
|
|
|
[{message, ?DEFAULT_LIMIT_REMOTE_MESSAGE},
|
|
|
|
{presence, ?DEFAULT_LIMIT_REMOTE_PRESENCE}].
|
|
|
|
|
|
|
|
build_service_limit_record(LimitOpts) ->
|
|
|
|
LimitOptsL = get_from_limitopts(LimitOpts, local),
|
|
|
|
LimitOptsR = get_from_limitopts(LimitOpts, remote),
|
|
|
|
{service_limits, build_limit_record(LimitOptsL, local),
|
|
|
|
build_limit_record(LimitOptsR, remote)}.
|
|
|
|
|
|
|
|
get_from_limitopts(LimitOpts, SenderT) ->
|
2017-08-18 14:09:49 +02:00
|
|
|
case lists:keyfind(SenderT, 1, LimitOpts) of
|
|
|
|
false -> [];
|
|
|
|
{SenderT, Result} -> Result
|
|
|
|
end.
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
build_remote_limit_record(LimitOpts, SenderT) ->
|
|
|
|
build_limit_record(LimitOpts, SenderT).
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec build_limit_record(any(), local | remote) -> #limits{}.
|
2015-04-08 14:01:16 +02:00
|
|
|
build_limit_record(LimitOpts, SenderT) ->
|
|
|
|
Limits = [get_limit_value(Name, Default, LimitOpts)
|
|
|
|
|| {Name, Default} <- list_of_limits(SenderT)],
|
|
|
|
list_to_tuple([limits | Limits]).
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec get_limit_value(atom(), integer(), any()) -> limit_value().
|
2015-04-08 14:01:16 +02:00
|
|
|
get_limit_value(Name, Default, LimitOpts) ->
|
|
|
|
case lists:keysearch(Name, 1, LimitOpts) of
|
|
|
|
{value, {Name, Number}} -> {custom, Number};
|
|
|
|
false -> {default, Default}
|
|
|
|
end.
|
|
|
|
|
2016-08-04 10:49:17 +02:00
|
|
|
type_of_stanza(Stanza) -> element(1, Stanza).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec get_limit_number(message | presence, #limits{}) -> limit_value().
|
2015-04-08 14:01:16 +02:00
|
|
|
get_limit_number(message, Limits) ->
|
|
|
|
Limits#limits.message;
|
|
|
|
get_limit_number(presence, Limits) ->
|
|
|
|
Limits#limits.presence.
|
|
|
|
|
2018-04-04 12:05:37 +02:00
|
|
|
-spec get_slimit_group(local | remote, #service_limits{}) -> #limits{}.
|
2015-04-08 14:01:16 +02:00
|
|
|
get_slimit_group(local, SLimits) ->
|
|
|
|
SLimits#service_limits.local;
|
|
|
|
get_slimit_group(remote, SLimits) ->
|
|
|
|
SLimits#service_limits.remote.
|
|
|
|
|
|
|
|
fragment_dests(Dests, Limit_number) ->
|
|
|
|
{R, _} = lists:foldl(fun (Dest, {Res, Count}) ->
|
|
|
|
case Count of
|
|
|
|
Limit_number ->
|
|
|
|
Head2 = [Dest], {[Head2 | Res], 0};
|
|
|
|
_ ->
|
|
|
|
[Head | Tail] = Res,
|
|
|
|
Head2 = [Dest | Head],
|
|
|
|
{[Head2 | Tail], Count + 1}
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
{[[]], 0}, Dests),
|
|
|
|
R.
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Limits: XEP-0128 Service Discovery Extensions
|
|
|
|
%%%-------------------------
|
|
|
|
|
|
|
|
%% Some parts of code are borrowed from mod_muc_room.erl
|
|
|
|
|
|
|
|
-define(RFIELDT(Type, Var, Val),
|
2016-08-04 10:49:17 +02:00
|
|
|
#xdata_field{type = Type, var = Var, values = [Val]}).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
-define(RFIELDV(Var, Val),
|
2016-08-04 10:49:17 +02:00
|
|
|
#xdata_field{var = Var, values = [Val]}).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
iq_disco_info_extras(From, State) ->
|
|
|
|
SenderT = sender_type(From),
|
|
|
|
Service_limits = State#state.service_limits,
|
|
|
|
case iq_disco_info_extras2(SenderT, Service_limits) of
|
|
|
|
[] -> [];
|
|
|
|
List_limits_xmpp ->
|
2016-08-04 10:49:17 +02:00
|
|
|
#xdata{type = result,
|
|
|
|
fields = [?RFIELDT(hidden, <<"FORM_TYPE">>, ?NS_ADDRESS)
|
|
|
|
| List_limits_xmpp]}
|
2015-04-08 14:01:16 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
sender_type(From) ->
|
2018-06-14 13:00:47 +02:00
|
|
|
Local_hosts = ejabberd_config:get_myhosts(),
|
2015-04-08 14:01:16 +02:00
|
|
|
case lists:member(From#jid.lserver, Local_hosts) of
|
|
|
|
true -> local;
|
|
|
|
false -> remote
|
|
|
|
end.
|
|
|
|
|
|
|
|
iq_disco_info_extras2(SenderT, SLimits) ->
|
|
|
|
Limits = get_slimit_group(SenderT, SLimits),
|
|
|
|
Stanza_types = [message, presence],
|
|
|
|
lists:foldl(fun (Type_of_stanza, R) ->
|
|
|
|
case get_limit_number(Type_of_stanza, Limits) of
|
|
|
|
{custom, Number} ->
|
|
|
|
[?RFIELDV((to_binary(Type_of_stanza)),
|
|
|
|
(to_binary(Number)))
|
|
|
|
| R];
|
|
|
|
{default, _} -> R
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], Stanza_types).
|
|
|
|
|
|
|
|
to_binary(A) -> list_to_binary(hd(io_lib:format("~p", [A]))).
|
|
|
|
|
|
|
|
%%%-------------------------
|
|
|
|
%%% Error report
|
|
|
|
%%%-------------------------
|
|
|
|
|
2017-02-16 09:00:26 +01:00
|
|
|
route_error(Packet, ErrType, ErrText) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
Lang = xmpp:get_lang(Packet),
|
|
|
|
Err = make_reply(ErrType, Lang, ErrText),
|
2017-02-16 09:00:26 +01:00
|
|
|
ejabberd_router:route_error(Packet, Err).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
|
|
|
make_reply(bad_request, Lang, ErrText) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
xmpp:err_bad_request(ErrText, Lang);
|
2015-04-08 14:01:16 +02:00
|
|
|
make_reply(jid_malformed, Lang, ErrText) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
xmpp:err_jid_malformed(ErrText, Lang);
|
2015-04-08 14:01:16 +02:00
|
|
|
make_reply(not_acceptable, Lang, ErrText) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
xmpp:err_not_acceptable(ErrText, Lang);
|
2015-04-08 14:01:16 +02:00
|
|
|
make_reply(internal_server_error, Lang, ErrText) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
xmpp:err_internal_server_error(ErrText, Lang);
|
2015-04-08 14:01:16 +02:00
|
|
|
make_reply(forbidden, Lang, ErrText) ->
|
2016-08-04 10:49:17 +02:00
|
|
|
xmpp:err_forbidden(ErrText, Lang).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2017-02-26 08:07:12 +01:00
|
|
|
stj(String) -> jid:decode(String).
|
2015-04-08 14:01:16 +02:00
|
|
|
|
2017-02-26 08:07:12 +01:00
|
|
|
jts(String) -> jid:encode(String).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(access) ->
|
2016-06-21 13:18:24 +02:00
|
|
|
fun acl:access_rules_validator/1;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(host) -> fun iolist_to_binary/1;
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_opt_type(hosts) ->
|
|
|
|
fun(L) -> lists:map(fun iolist_to_binary/1, L) end;
|
2018-01-08 09:29:17 +01:00
|
|
|
mod_opt_type(name) -> fun iolist_to_binary/1;
|
2017-06-26 13:39:50 +02:00
|
|
|
mod_opt_type({limits, Type}) when (Type == local) or (Type == remote) ->
|
|
|
|
fun(L) ->
|
|
|
|
lists:map(
|
2017-08-18 14:09:49 +02:00
|
|
|
fun ({message, infinite} = O) -> O;
|
|
|
|
({presence, infinite} = O) -> O;
|
|
|
|
({message, I} = O) when is_integer(I) -> O;
|
|
|
|
({presence, I} = O) when is_integer(I) -> O
|
2017-06-26 13:39:50 +02:00
|
|
|
end, L)
|
2018-01-23 08:54:52 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
mod_options(_Host) ->
|
|
|
|
[{access, all},
|
|
|
|
{host, <<"multicast.@HOST@">>},
|
|
|
|
{hosts, []},
|
|
|
|
{limits, [{local, []}, {remote, []}]},
|
|
|
|
{name, ?T("Multicast")}].
|