2007-12-01 06:16:30 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_caps.erl
|
|
|
|
%%% Author : Magnus Henoch <henoch@dtek.chalmers.se>
|
|
|
|
%%% Purpose : Request and cache Entity Capabilities (XEP-0115)
|
|
|
|
%%% Created : 7 Oct 2006 by Magnus Henoch <henoch@dtek.chalmers.se>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 ProcessOne
|
2007-12-24 12:41:41 +01: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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2015-02-25 15:19:33 +01: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.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2009-11-04 21:14:22 +01:00
|
|
|
%%% 2009, improvements from ProcessOne to support correct PEP handling
|
2009-04-30 23:09:45 +02:00
|
|
|
%%% through s2s, use less memory, and speedup global caps handling
|
2007-12-01 06:16:30 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_caps).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
-author('henoch@dtek.chalmers.se').
|
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 115, '1.5'}).
|
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
-behaviour(gen_server).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-export([read_caps/1, list_features/1, caps_stream_features/2,
|
2013-03-14 10:33:02 +01:00
|
|
|
disco_features/5, disco_identity/5, disco_info/5,
|
2014-07-10 12:16:33 +02:00
|
|
|
get_features/2, export/1, import_info/0, import/5,
|
2018-02-12 15:37:36 +01:00
|
|
|
get_user_caps/2, import_start/2, import_stop/2,
|
|
|
|
compute_disco_hash/2, is_valid_node/1]).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
|
|
|
%% gen_mod callbacks
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, depends/2]).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
|
|
|
%% gen_server callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, handle_info/2, handle_call/3,
|
|
|
|
handle_cast/2, terminate/2, code_change/3]).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-export([user_send_packet/1, user_receive_packet/1,
|
2020-01-08 10:24:51 +01:00
|
|
|
c2s_presence_in/2, mod_opt_type/1, mod_options/1, mod_doc/0]).
|
2009-04-10 09:36:17 +02:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2016-11-22 14:48:01 +01:00
|
|
|
-include("mod_caps.hrl").
|
2020-01-08 10:24:51 +01:00
|
|
|
-include("translate.hrl").
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(BAD_HASH_LIFETIME, 600).
|
|
|
|
|
|
|
|
-record(state, {host = <<"">> :: binary()}).
|
2010-03-05 10:34:15 +01:00
|
|
|
|
2019-07-10 09:31:51 +02:00
|
|
|
-type digest_type() :: md5 | sha | sha224 | sha256 | sha384 | sha512.
|
|
|
|
|
2016-04-13 10:41:04 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
2016-11-22 14:48:01 +01:00
|
|
|
-callback import(binary(), {binary(), binary()}, [binary() | pos_integer()]) -> ok.
|
2016-04-13 10:41:04 +02:00
|
|
|
-callback caps_read(binary(), {binary(), binary()}) ->
|
|
|
|
{ok, non_neg_integer() | [binary()]} | error.
|
|
|
|
-callback caps_write(binary(), {binary(), binary()},
|
|
|
|
non_neg_integer() | [binary()]) -> any().
|
2018-02-10 09:36:39 +01:00
|
|
|
-callback use_cache(binary()) -> boolean().
|
|
|
|
|
|
|
|
-optional_callbacks([use_cache/1]).
|
2016-04-13 10:41:04 +02:00
|
|
|
|
2010-03-05 10:34:15 +01:00
|
|
|
start(Host, Opts) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:start_child(?MODULE, Host, Opts).
|
2010-03-05 10:34:15 +01:00
|
|
|
|
|
|
|
stop(Host) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
gen_mod:stop_child(?MODULE, Host).
|
2010-03-05 10:34:15 +01:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec get_features(binary(), nothing | caps()) -> [binary()].
|
2014-07-10 12:16:33 +02:00
|
|
|
get_features(_Host, nothing) -> [];
|
|
|
|
get_features(Host, #caps{node = Node, version = Version,
|
|
|
|
exts = Exts}) ->
|
2010-03-05 10:34:15 +01:00
|
|
|
SubNodes = [Version | Exts],
|
2018-02-10 09:36:39 +01:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
|
|
|
lists:foldl(
|
|
|
|
fun(SubNode, Acc) ->
|
|
|
|
NodePair = {Node, SubNode},
|
|
|
|
Res = case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(caps_features_cache, NodePair,
|
|
|
|
caps_read_fun(Host, NodePair));
|
|
|
|
false ->
|
|
|
|
Mod:caps_read(Host, NodePair)
|
|
|
|
end,
|
|
|
|
case Res of
|
|
|
|
{ok, Features} when is_list(Features) ->
|
|
|
|
Features ++ Acc;
|
|
|
|
_ -> Acc
|
|
|
|
end
|
|
|
|
end, [], SubNodes).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec list_features(ejabberd_c2s:state()) -> [{ljid(), caps()}].
|
|
|
|
list_features(C2SState) ->
|
2017-03-27 16:24:24 +02:00
|
|
|
Rs = maps:get(caps_resources, C2SState, gb_trees:empty()),
|
2016-12-11 13:03:37 +01:00
|
|
|
gb_trees:to_list(Rs).
|
|
|
|
|
2019-07-29 16:13:16 +02:00
|
|
|
-spec get_user_caps(jid() | ljid(), ejabberd_c2s:state()) -> {ok, caps()} | error.
|
2016-12-11 13:03:37 +01:00
|
|
|
get_user_caps(JID, C2SState) ->
|
2017-03-27 16:24:24 +02:00
|
|
|
Rs = maps:get(caps_resources, C2SState, gb_trees:empty()),
|
2016-12-11 13:03:37 +01:00
|
|
|
LJID = jid:tolower(JID),
|
|
|
|
case gb_trees:lookup(LJID, Rs) of
|
|
|
|
{value, Caps} ->
|
|
|
|
{ok, Caps};
|
|
|
|
none ->
|
|
|
|
error
|
|
|
|
end.
|
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec read_caps(#presence{}) -> nothing | caps().
|
|
|
|
read_caps(Presence) ->
|
|
|
|
case xmpp:get_subtag(Presence, #caps{}) of
|
|
|
|
false -> nothing;
|
|
|
|
Caps -> Caps
|
|
|
|
end.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2017-01-09 15:02:17 +01:00
|
|
|
-spec user_send_packet({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}.
|
|
|
|
user_send_packet({#presence{type = available,
|
|
|
|
from = #jid{luser = U, lserver = LServer} = From,
|
|
|
|
to = #jid{luser = U, lserver = LServer,
|
|
|
|
lresource = <<"">>}} = Pkt,
|
2017-11-10 16:02:22 +01:00
|
|
|
#{jid := To} = State}) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
case read_caps(Pkt) of
|
|
|
|
nothing -> ok;
|
|
|
|
#caps{version = Version, exts = Exts} = Caps ->
|
2017-11-10 16:02:22 +01:00
|
|
|
feature_request(LServer, From, To, Caps, [Version | Exts])
|
2014-07-10 12:16:33 +02:00
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
{Pkt, State};
|
|
|
|
user_send_packet(Acc) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
-spec user_receive_packet({stanza(), ejabberd_c2s:state()}) -> {stanza(), ejabberd_c2s:state()}.
|
|
|
|
user_receive_packet({#presence{from = From, type = available} = Pkt,
|
2017-11-10 16:02:22 +01:00
|
|
|
#{lserver := LServer, jid := To} = State}) ->
|
2020-04-27 13:17:45 +02:00
|
|
|
IsRemote = case From#jid.lresource of
|
|
|
|
% Don't store caps for presences sent by our muc rooms
|
|
|
|
<<>> ->
|
|
|
|
try ejabberd_router:host_of_route(From#jid.lserver) of
|
|
|
|
MaybeMuc ->
|
|
|
|
not lists:member(From#jid.lserver,
|
|
|
|
gen_mod:get_module_opt_hosts(MaybeMuc, mod_muc))
|
|
|
|
catch error:{unregistered_route, _} ->
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
not ejabberd_router:is_my_host(From#jid.lserver)
|
|
|
|
end,
|
2016-07-18 14:01:32 +02:00
|
|
|
if IsRemote ->
|
|
|
|
case read_caps(Pkt) of
|
2013-03-14 10:33:02 +01:00
|
|
|
nothing -> ok;
|
|
|
|
#caps{version = Version, exts = Exts} = Caps ->
|
2018-01-23 21:20:10 +01:00
|
|
|
feature_request(LServer, To, From, Caps, [Version | Exts])
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
true -> ok
|
2014-07-10 12:16:33 +02:00
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
{Pkt, State};
|
|
|
|
user_receive_packet(Acc) ->
|
|
|
|
Acc.
|
2014-07-10 12:16:33 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec caps_stream_features([xmpp_element()], binary()) -> [xmpp_element()].
|
2010-05-07 18:32:57 +02:00
|
|
|
caps_stream_features(Acc, MyHost) ->
|
2017-01-09 15:02:17 +01:00
|
|
|
case gen_mod:is_loaded(MyHost, ?MODULE) of
|
|
|
|
true ->
|
2018-01-29 00:02:15 +01:00
|
|
|
case make_my_disco_hash(MyHost) of
|
2017-01-09 15:02:17 +01:00
|
|
|
<<"">> ->
|
|
|
|
Acc;
|
2018-01-29 00:02:15 +01:00
|
|
|
Hash ->
|
2018-06-14 13:00:47 +02:00
|
|
|
[#caps{hash = <<"sha-1">>, node = ejabberd_config:get_uri(),
|
2018-01-29 00:02:15 +01:00
|
|
|
version = Hash} | Acc]
|
2017-01-09 15:02:17 +01:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
Acc
|
2010-05-07 18:32:57 +02:00
|
|
|
end.
|
|
|
|
|
2016-09-08 16:08:48 +02:00
|
|
|
-spec disco_features({error, stanza_error()} | {result, [binary()]} | empty,
|
2016-07-18 14:01:32 +02:00
|
|
|
jid(), jid(),
|
2016-08-05 07:41:08 +02:00
|
|
|
binary(), binary()) ->
|
2016-09-08 16:08:48 +02:00
|
|
|
{error, stanza_error()} | {result, [binary()]} | empty.
|
2013-03-14 10:33:02 +01:00
|
|
|
disco_features(Acc, From, To, Node, Lang) ->
|
|
|
|
case is_valid_node(Node) of
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:run_fold(disco_local_features,
|
|
|
|
To#jid.lserver, empty,
|
2016-08-05 07:41:08 +02:00
|
|
|
[From, To, <<"">>, Lang]);
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end.
|
2010-05-07 18:32:57 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec disco_identity([identity()], jid(), jid(),
|
2016-08-05 07:41:08 +02:00
|
|
|
binary(), binary()) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
[identity()].
|
2013-03-14 10:33:02 +01:00
|
|
|
disco_identity(Acc, From, To, Node, Lang) ->
|
|
|
|
case is_valid_node(Node) of
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:run_fold(disco_local_identity,
|
|
|
|
To#jid.lserver, [],
|
2016-08-05 07:41:08 +02:00
|
|
|
[From, To, <<"">>, Lang]);
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end.
|
2010-05-07 18:32:57 +02:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec disco_info([xdata()], binary(), module(), binary(), binary()) -> [xdata()];
|
|
|
|
([xdata()], jid(), jid(), binary(), binary()) -> [xdata()].
|
|
|
|
disco_info(Acc, Host, Module, Node, Lang) when is_atom(Module) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case is_valid_node(Node) of
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:run_fold(disco_info, Host, [],
|
2016-08-05 07:41:08 +02:00
|
|
|
[Host, Module, <<"">>, Lang]);
|
2013-03-14 10:33:02 +01:00
|
|
|
false ->
|
|
|
|
Acc
|
2016-08-09 09:56:32 +02:00
|
|
|
end;
|
|
|
|
disco_info(Acc, _, _, _Node, _Lang) ->
|
|
|
|
Acc.
|
2010-05-07 18:32:57 +02:00
|
|
|
|
2016-12-11 13:03:37 +01:00
|
|
|
-spec c2s_presence_in(ejabberd_c2s:state(), presence()) -> ejabberd_c2s:state().
|
2013-03-14 10:33:02 +01:00
|
|
|
c2s_presence_in(C2SState,
|
2016-12-11 13:03:37 +01:00
|
|
|
#presence{from = From, to = To, type = Type} = Presence) ->
|
2018-01-26 13:02:06 +01:00
|
|
|
{Subscription, _, _} = ejabberd_hooks:run_fold(
|
|
|
|
roster_get_jid_info, To#jid.lserver,
|
|
|
|
{none, none, []},
|
|
|
|
[To#jid.luser, To#jid.lserver, From]),
|
2017-12-13 18:59:06 +01:00
|
|
|
ToSelf = (From#jid.luser == To#jid.luser)
|
|
|
|
and (From#jid.lserver == To#jid.lserver),
|
2016-07-18 14:01:32 +02:00
|
|
|
Insert = (Type == available)
|
2018-01-29 01:07:38 +01:00
|
|
|
and ((Subscription == both) or (Subscription == from) or ToSelf),
|
2016-07-18 14:01:32 +02:00
|
|
|
Delete = (Type == unavailable) or (Type == error),
|
2010-09-28 06:18:57 +02:00
|
|
|
if Insert or Delete ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LFrom = jid:tolower(From),
|
2017-03-27 16:24:24 +02:00
|
|
|
Rs = maps:get(caps_resources, C2SState, gb_trees:empty()),
|
2016-07-18 14:01:32 +02:00
|
|
|
Caps = read_caps(Presence),
|
2015-07-07 00:24:06 +02:00
|
|
|
NewRs = case Caps of
|
|
|
|
nothing when Insert == true -> Rs;
|
|
|
|
_ when Insert == true ->
|
|
|
|
case gb_trees:lookup(LFrom, Rs) of
|
|
|
|
{value, Caps} -> Rs;
|
|
|
|
none ->
|
|
|
|
ejabberd_hooks:run(caps_add, To#jid.lserver,
|
|
|
|
[From, To,
|
|
|
|
get_features(To#jid.lserver, Caps)]),
|
|
|
|
gb_trees:insert(LFrom, Caps, Rs);
|
|
|
|
_ ->
|
|
|
|
ejabberd_hooks:run(caps_update, To#jid.lserver,
|
|
|
|
[From, To,
|
|
|
|
get_features(To#jid.lserver, Caps)]),
|
|
|
|
gb_trees:update(LFrom, Caps, Rs)
|
|
|
|
end;
|
|
|
|
_ -> gb_trees:delete_any(LFrom, Rs)
|
|
|
|
end,
|
2017-01-09 15:02:17 +01:00
|
|
|
C2SState#{caps_resources => NewRs};
|
2016-12-11 13:03:37 +01:00
|
|
|
true ->
|
|
|
|
C2SState
|
2010-09-28 06:18:57 +02:00
|
|
|
end.
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec depends(binary(), gen_mod:opts()) -> [{module(), hard | soft}].
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(Host, NewOpts, OldOpts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
NewMod = gen_mod:db_mod(NewOpts, ?MODULE),
|
|
|
|
OldMod = gen_mod:db_mod(OldOpts, ?MODULE),
|
2017-02-22 17:46:47 +01:00
|
|
|
if OldMod /= NewMod ->
|
|
|
|
NewMod:init(Host, NewOpts);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2018-02-10 09:36:39 +01:00
|
|
|
init_cache(NewMod, Host, NewOpts).
|
2017-02-22 17:46:47 +01:00
|
|
|
|
2019-08-04 20:46:18 +02:00
|
|
|
init([Host|_]) ->
|
2017-02-14 08:25:08 +01:00
|
|
|
process_flag(trap_exit, true),
|
2019-08-04 20:46:18 +02:00
|
|
|
Opts = gen_mod:get_module_opts(Host, ?MODULE),
|
2019-06-14 11:33:26 +02:00
|
|
|
Mod = gen_mod:db_mod(Opts, ?MODULE),
|
2018-02-10 09:36:39 +01:00
|
|
|
init_cache(Mod, Host, Opts),
|
2016-04-13 10:41:04 +02:00
|
|
|
Mod:init(Host, Opts),
|
2014-07-10 12:16:33 +02:00
|
|
|
ejabberd_hooks:add(c2s_presence_in, Host, ?MODULE,
|
|
|
|
c2s_presence_in, 75),
|
|
|
|
ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
|
|
|
|
user_send_packet, 75),
|
|
|
|
ejabberd_hooks:add(user_receive_packet, Host, ?MODULE,
|
|
|
|
user_receive_packet, 75),
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:add(c2s_post_auth_features, Host, ?MODULE,
|
2014-07-10 12:16:33 +02:00
|
|
|
caps_stream_features, 75),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:add(s2s_in_post_auth_features, Host, ?MODULE,
|
2014-07-10 12:16:33 +02:00
|
|
|
caps_stream_features, 75),
|
|
|
|
ejabberd_hooks:add(disco_local_features, Host, ?MODULE,
|
|
|
|
disco_features, 75),
|
|
|
|
ejabberd_hooks:add(disco_local_identity, Host, ?MODULE,
|
|
|
|
disco_identity, 75),
|
|
|
|
ejabberd_hooks:add(disco_info, Host, ?MODULE,
|
|
|
|
disco_info, 75),
|
2007-12-01 06:16:30 +01:00
|
|
|
{ok, #state{host = Host}}.
|
|
|
|
|
|
|
|
handle_call(stop, _From, State) ->
|
2010-03-05 10:34:15 +01:00
|
|
|
{stop, normal, ok, State};
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_call(Request, From, State) ->
|
|
|
|
?WARNING_MSG("Unexpected call from ~p: ~p", [From, Request]),
|
|
|
|
{noreply, State}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2019-07-12 10:55:36 +02:00
|
|
|
handle_cast(Msg, State) ->
|
|
|
|
?WARNING_MSG("Unexpected cast: ~p", [Msg]),
|
|
|
|
{noreply, State}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
handle_info({iq_reply, IQReply, {Host, From, To, Caps, SubNodes}}, State) ->
|
|
|
|
feature_response(IQReply, Host, From, To, Caps, SubNodes),
|
|
|
|
{noreply, State};
|
|
|
|
handle_info(Info, State) ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Unexpected info: ~p", [Info]),
|
2017-11-10 16:02:22 +01:00
|
|
|
{noreply, State}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2009-04-10 09:36:17 +02:00
|
|
|
terminate(_Reason, State) ->
|
|
|
|
Host = State#state.host,
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(c2s_presence_in, Host, ?MODULE,
|
|
|
|
c2s_presence_in, 75),
|
|
|
|
ejabberd_hooks:delete(user_send_packet, Host, ?MODULE,
|
|
|
|
user_send_packet, 75),
|
2010-09-28 06:18:57 +02:00
|
|
|
ejabberd_hooks:delete(user_receive_packet, Host,
|
|
|
|
?MODULE, user_receive_packet, 75),
|
2016-12-11 13:03:37 +01:00
|
|
|
ejabberd_hooks:delete(c2s_post_auth_features, Host,
|
2010-05-07 18:32:57 +02:00
|
|
|
?MODULE, caps_stream_features, 75),
|
2017-01-09 15:02:17 +01:00
|
|
|
ejabberd_hooks:delete(s2s_in_post_auth_features, Host,
|
2010-05-07 18:32:57 +02:00
|
|
|
?MODULE, caps_stream_features, 75),
|
|
|
|
ejabberd_hooks:delete(disco_local_features, Host,
|
|
|
|
?MODULE, disco_features, 75),
|
|
|
|
ejabberd_hooks:delete(disco_local_identity, Host,
|
|
|
|
?MODULE, disco_identity, 75),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(disco_info, Host, ?MODULE,
|
|
|
|
disco_info, 75),
|
2007-12-01 06:16:30 +01:00
|
|
|
ok.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2010-03-05 10:34:15 +01:00
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
-spec feature_request(binary(), jid(), jid(), caps(), [binary()]) -> any().
|
|
|
|
feature_request(Host, From, To, Caps,
|
2013-03-14 10:33:02 +01:00
|
|
|
[SubNode | Tail] = SubNodes) ->
|
2010-03-05 10:34:15 +01:00
|
|
|
Node = Caps#caps.node,
|
2013-03-14 10:33:02 +01:00
|
|
|
NodePair = {Node, SubNode},
|
2018-02-10 09:36:39 +01:00
|
|
|
Mod = gen_mod:db_mod(Host, ?MODULE),
|
|
|
|
Res = case use_cache(Mod, Host) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(caps_features_cache, NodePair,
|
|
|
|
caps_read_fun(Host, NodePair));
|
|
|
|
false ->
|
|
|
|
Mod:caps_read(Host, NodePair)
|
|
|
|
end,
|
|
|
|
case Res of
|
2017-04-20 17:18:26 +02:00
|
|
|
{ok, Fs} when is_list(Fs) ->
|
2017-11-10 16:02:22 +01:00
|
|
|
feature_request(Host, From, To, Caps, Tail);
|
2017-04-20 17:18:26 +02:00
|
|
|
_ ->
|
2017-11-10 16:02:22 +01:00
|
|
|
LTo = jid:tolower(To),
|
|
|
|
case ets_cache:insert_new(caps_requests_cache, {LTo, NodePair}, ok) of
|
2017-04-20 17:18:26 +02:00
|
|
|
true ->
|
|
|
|
IQ = #iq{type = get,
|
2017-11-10 16:02:22 +01:00
|
|
|
from = From,
|
|
|
|
to = To,
|
2017-04-20 17:18:26 +02:00
|
|
|
sub_els = [#disco_info{node = <<Node/binary, "#",
|
|
|
|
SubNode/binary>>}]},
|
2017-11-10 16:02:22 +01:00
|
|
|
ejabberd_router:route_iq(
|
|
|
|
IQ, {Host, From, To, Caps, SubNodes},
|
|
|
|
gen_mod:get_module_proc(Host, ?MODULE));
|
2017-04-20 17:18:26 +02:00
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end,
|
2017-11-10 16:02:22 +01:00
|
|
|
feature_request(Host, From, To, Caps, Tail)
|
2010-03-05 10:34:15 +01:00
|
|
|
end;
|
2017-11-10 16:02:22 +01:00
|
|
|
feature_request(_Host, _From, _To, _Caps, []) -> ok.
|
2010-03-05 10:34:15 +01:00
|
|
|
|
2017-11-10 16:02:22 +01:00
|
|
|
-spec feature_response(iq(), binary(), jid(), jid(), caps(), [binary()]) -> any().
|
2016-07-18 14:01:32 +02:00
|
|
|
feature_response(#iq{type = result, sub_els = [El]},
|
2017-11-10 16:02:22 +01:00
|
|
|
Host, From, To, Caps, [SubNode | SubNodes]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NodePair = {Caps#caps.node, SubNode},
|
2016-07-18 14:01:32 +02:00
|
|
|
try
|
|
|
|
DiscoInfo = xmpp:decode(El),
|
|
|
|
case check_hash(Caps, DiscoInfo) of
|
|
|
|
true ->
|
|
|
|
Features = DiscoInfo#disco_info.features,
|
2017-04-20 17:18:26 +02:00
|
|
|
LServer = jid:nameprep(Host),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
case Mod:caps_write(LServer, NodePair, Features) of
|
|
|
|
ok ->
|
2018-02-10 09:36:39 +01:00
|
|
|
case use_cache(Mod, LServer) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(caps_features_cache, NodePair);
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
2017-04-20 17:18:26 +02:00
|
|
|
{error, _} ->
|
|
|
|
ok
|
|
|
|
end;
|
2016-07-18 14:01:32 +02:00
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
catch _:{xmpp_codec, _Why} ->
|
|
|
|
ok
|
2010-05-07 18:32:57 +02:00
|
|
|
end,
|
2017-11-10 16:02:22 +01:00
|
|
|
feature_request(Host, From, To, Caps, SubNodes);
|
|
|
|
feature_response(_IQResult, Host, From, To, Caps,
|
2013-03-14 10:33:02 +01:00
|
|
|
[_SubNode | SubNodes]) ->
|
2017-11-10 16:02:22 +01:00
|
|
|
feature_request(Host, From, To, Caps, SubNodes).
|
2010-03-05 10:34:15 +01:00
|
|
|
|
2016-08-30 08:48:08 +02:00
|
|
|
-spec caps_read_fun(binary(), {binary(), binary()})
|
|
|
|
-> fun(() -> {ok, [binary()] | non_neg_integer()} | error).
|
2014-07-10 12:16:33 +02:00
|
|
|
caps_read_fun(Host, Node) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LServer = jid:nameprep(Host),
|
2016-04-13 10:41:04 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
fun() -> Mod:caps_read(LServer, Node) end.
|
2010-08-30 06:02:47 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec make_my_disco_hash(binary()) -> binary().
|
2010-05-07 18:32:57 +02:00
|
|
|
make_my_disco_hash(Host) ->
|
2017-02-25 08:01:01 +01:00
|
|
|
JID = jid:make(Host),
|
2010-05-07 18:32:57 +02:00
|
|
|
case {ejabberd_hooks:run_fold(disco_local_features,
|
2013-03-14 10:33:02 +01:00
|
|
|
Host, empty, [JID, JID, <<"">>, <<"">>]),
|
|
|
|
ejabberd_hooks:run_fold(disco_local_identity, Host, [],
|
|
|
|
[JID, JID, <<"">>, <<"">>]),
|
|
|
|
ejabberd_hooks:run_fold(disco_info, Host, [],
|
|
|
|
[Host, undefined, <<"">>, <<"">>])}
|
|
|
|
of
|
|
|
|
{{result, Features}, Identities, Info} ->
|
2016-07-18 14:01:32 +02:00
|
|
|
Feats = lists:map(fun ({{Feat, _Host}}) -> Feat;
|
|
|
|
(Feat) -> Feat
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
Features),
|
2016-07-18 14:01:32 +02:00
|
|
|
DiscoInfo = #disco_info{identities = Identities,
|
|
|
|
features = Feats,
|
|
|
|
xdata = Info},
|
2018-02-12 15:37:36 +01:00
|
|
|
compute_disco_hash(DiscoInfo, sha);
|
2013-03-14 10:33:02 +01:00
|
|
|
_Err -> <<"">>
|
2010-05-07 18:32:57 +02:00
|
|
|
end.
|
|
|
|
|
2018-02-12 15:37:36 +01:00
|
|
|
-spec compute_disco_hash(disco_info(), digest_type()) -> binary().
|
|
|
|
compute_disco_hash(DiscoInfo, Algo) ->
|
2016-07-18 14:01:32 +02:00
|
|
|
Concat = list_to_binary([concat_identities(DiscoInfo),
|
|
|
|
concat_features(DiscoInfo), concat_info(DiscoInfo)]),
|
2017-05-23 09:43:26 +02:00
|
|
|
base64:encode(case Algo of
|
2019-07-10 09:31:51 +02:00
|
|
|
md5 -> erlang:md5(Concat);
|
|
|
|
sha -> crypto:hash(sha, Concat);
|
|
|
|
sha224 -> crypto:hash(sha224, Concat);
|
|
|
|
sha256 -> crypto:hash(sha256, Concat);
|
|
|
|
sha384 -> crypto:hash(sha384, Concat);
|
|
|
|
sha512 -> crypto:hash(sha512, Concat)
|
|
|
|
end).
|
2010-08-07 14:04:57 +02:00
|
|
|
|
2016-07-18 14:01:32 +02:00
|
|
|
-spec check_hash(caps(), disco_info()) -> boolean().
|
|
|
|
check_hash(Caps, DiscoInfo) ->
|
2010-08-07 14:04:57 +02:00
|
|
|
case Caps#caps.hash of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"md5">> ->
|
2018-02-12 15:37:36 +01:00
|
|
|
Caps#caps.version == compute_disco_hash(DiscoInfo, md5);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"sha-1">> ->
|
2018-02-12 15:37:36 +01:00
|
|
|
Caps#caps.version == compute_disco_hash(DiscoInfo, sha);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"sha-224">> ->
|
2018-02-12 15:37:36 +01:00
|
|
|
Caps#caps.version == compute_disco_hash(DiscoInfo, sha224);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"sha-256">> ->
|
2018-02-12 15:37:36 +01:00
|
|
|
Caps#caps.version == compute_disco_hash(DiscoInfo, sha256);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"sha-384">> ->
|
2018-02-12 15:37:36 +01:00
|
|
|
Caps#caps.version == compute_disco_hash(DiscoInfo, sha384);
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"sha-512">> ->
|
2018-02-12 15:37:36 +01:00
|
|
|
Caps#caps.version == compute_disco_hash(DiscoInfo, sha512);
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> true
|
2010-08-07 14:04:57 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec concat_features(disco_info()) -> iolist().
|
2016-07-18 14:01:32 +02:00
|
|
|
concat_features(#disco_info{features = Features}) ->
|
|
|
|
lists:usort([[Feat, $<] || Feat <- Features]).
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec concat_identities(disco_info()) -> iolist().
|
2016-07-18 14:01:32 +02:00
|
|
|
concat_identities(#disco_info{identities = Identities}) ->
|
|
|
|
lists:sort(
|
|
|
|
[[Cat, $/, T, $/, Lang, $/, Name, $<] ||
|
|
|
|
#identity{category = Cat, type = T,
|
|
|
|
lang = Lang, name = Name} <- Identities]).
|
|
|
|
|
2016-08-09 09:56:32 +02:00
|
|
|
-spec concat_info(disco_info()) -> iolist().
|
2016-07-18 14:01:32 +02:00
|
|
|
concat_info(#disco_info{xdata = Xs}) ->
|
|
|
|
lists:sort(
|
2016-10-07 09:31:03 +02:00
|
|
|
[concat_xdata_fields(X) || #xdata{type = result} = X <- Xs]).
|
|
|
|
|
|
|
|
-spec concat_xdata_fields(xdata()) -> iolist().
|
|
|
|
concat_xdata_fields(#xdata{fields = Fields} = X) ->
|
|
|
|
Form = xmpp_util:get_xdata_values(<<"FORM_TYPE">>, X),
|
2016-07-18 14:01:32 +02:00
|
|
|
Res = [[Var, $<, lists:sort([[Val, $<] || Val <- Values])]
|
|
|
|
|| #xdata_field{var = Var, values = Values} <- Fields,
|
|
|
|
is_binary(Var), Var /= <<"FORM_TYPE">>],
|
2010-05-07 18:32:57 +02:00
|
|
|
[Form, $<, lists:sort(Res)].
|
2010-09-28 06:18:57 +02:00
|
|
|
|
2016-08-05 07:41:08 +02:00
|
|
|
-spec is_valid_node(binary()) -> boolean().
|
2013-03-14 10:33:02 +01:00
|
|
|
is_valid_node(Node) ->
|
|
|
|
case str:tokens(Node, <<"#">>) of
|
2018-06-14 13:00:47 +02:00
|
|
|
[H|_] ->
|
|
|
|
H == ejabberd_config:get_uri();
|
|
|
|
[] ->
|
2013-03-14 10:33:02 +01:00
|
|
|
false
|
|
|
|
end.
|
2013-07-21 14:53:44 +02:00
|
|
|
|
2018-02-10 09:36:39 +01:00
|
|
|
init_cache(Mod, Host, Opts) ->
|
2018-01-23 08:54:52 +01:00
|
|
|
CacheOpts = cache_opts(Opts),
|
2018-02-10 09:36:39 +01:00
|
|
|
case use_cache(Mod, Host) of
|
2017-04-20 17:18:26 +02:00
|
|
|
true ->
|
|
|
|
ets_cache:new(caps_features_cache, CacheOpts);
|
|
|
|
false ->
|
2018-02-10 09:36:39 +01:00
|
|
|
ets_cache:delete(caps_features_cache)
|
2017-04-20 17:18:26 +02:00
|
|
|
end,
|
|
|
|
CacheSize = proplists:get_value(max_size, CacheOpts),
|
|
|
|
ets_cache:new(caps_requests_cache,
|
|
|
|
[{max_size, CacheSize},
|
|
|
|
{life_time, timer:seconds(?BAD_HASH_LIFETIME)}]).
|
|
|
|
|
2018-02-10 09:36:39 +01:00
|
|
|
use_cache(Mod, Host) ->
|
|
|
|
case erlang:function_exported(Mod, use_cache, 1) of
|
|
|
|
true -> Mod:use_cache(Host);
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> mod_caps_opt:use_cache(Host)
|
2018-02-10 09:36:39 +01:00
|
|
|
end.
|
2017-04-20 17:18:26 +02:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
cache_opts(Opts) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
MaxSize = mod_caps_opt:cache_size(Opts),
|
|
|
|
CacheMissed = mod_caps_opt:cache_missed(Opts),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = mod_caps_opt:cache_life_time(Opts),
|
2017-04-20 17:18:26 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
2016-04-13 10:41:04 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-07-21 14:53:44 +02:00
|
|
|
|
2014-07-10 12:16:33 +02:00
|
|
|
import_info() ->
|
|
|
|
[{<<"caps_features">>, 4}].
|
2013-07-21 14:53:44 +02:00
|
|
|
|
2014-07-10 12:16:33 +02:00
|
|
|
import_start(LServer, DBType) ->
|
|
|
|
ets:new(caps_features_tmp, [private, named_table, bag]),
|
2016-04-13 10:41:04 +02:00
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:init(LServer, []),
|
2014-07-10 12:16:33 +02:00
|
|
|
ok.
|
|
|
|
|
2016-04-20 11:27:32 +02:00
|
|
|
import(_LServer, {sql, _}, _DBType, <<"caps_features">>,
|
2014-07-10 12:16:33 +02:00
|
|
|
[Node, SubNode, Feature, _TimeStamp]) ->
|
2016-09-24 22:34:28 +02:00
|
|
|
Feature1 = case catch binary_to_integer(Feature) of
|
2014-07-10 12:16:33 +02:00
|
|
|
I when is_integer(I), I>0 -> I;
|
|
|
|
_ -> Feature
|
|
|
|
end,
|
|
|
|
ets:insert(caps_features_tmp, {{Node, SubNode}, Feature1}),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
import_stop(LServer, DBType) ->
|
|
|
|
import_next(LServer, DBType, ets:first(caps_features_tmp)),
|
|
|
|
ets:delete(caps_features_tmp),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
import_next(_LServer, _DBType, '$end_of_table') ->
|
|
|
|
ok;
|
|
|
|
import_next(LServer, DBType, NodePair) ->
|
|
|
|
Features = [F || {_, F} <- ets:lookup(caps_features_tmp, NodePair)],
|
2016-11-22 14:48:01 +01:00
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, NodePair, Features),
|
2014-07-10 12:16:33 +02:00
|
|
|
import_next(LServer, DBType, ets:next(caps_features_tmp, NodePair)).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(db_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:db_type(?MODULE);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(use_cache) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_size) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:pos_int(infinity);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_missed) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_life_time) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:timeout(second, infinity).
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_options(Host) ->
|
|
|
|
[{db_type, ejabberd_config:default_db(Host, ?MODULE)},
|
2019-06-14 11:33:26 +02:00
|
|
|
{use_cache, ejabberd_option:use_cache(Host)},
|
|
|
|
{cache_size, ejabberd_option:cache_size(Host)},
|
|
|
|
{cache_missed, ejabberd_option:cache_missed(Host)},
|
|
|
|
{cache_life_time, ejabberd_option:cache_life_time(Host)}].
|
2020-01-08 10:24:51 +01:00
|
|
|
|
|
|
|
mod_doc() ->
|
|
|
|
#{desc =>
|
|
|
|
[?T("This module implements "
|
|
|
|
"https://xmpp.org/extensions/xep-0115.html"
|
|
|
|
"[XEP-0115: Entity Capabilities]."),
|
|
|
|
?T("The main purpose of the module is to provide "
|
|
|
|
"PEP functionality (see 'mod_pubsub').")],
|
|
|
|
opts =>
|
|
|
|
[{db_type,
|
|
|
|
#{value => "mnesia | sql",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'default_db' option, but applied to this module only.")}},
|
|
|
|
{use_cache,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'use_cache' option, but applied to this module only.")}},
|
|
|
|
{cache_size,
|
|
|
|
#{value => "pos_integer() | infinity",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'cache_size' option, but applied to this module only.")}},
|
|
|
|
{cache_missed,
|
|
|
|
#{value => "true | false",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'cache_missed' option, but applied to this module only.")}},
|
|
|
|
{cache_life_time,
|
|
|
|
#{value => "timeout()",
|
|
|
|
desc =>
|
|
|
|
?T("Same as top-level 'cache_life_time' option, but applied to this module only.")}}]}.
|