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
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([read_caps/1, caps_stream_features/2,
|
|
|
|
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,
|
|
|
|
import_start/2, import_stop/2]).
|
2007-12-01 06:16:30 +01:00
|
|
|
|
|
|
|
%% gen_mod callbacks
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, start_link/2, stop/1]).
|
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
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
-export([user_send_packet/4, user_receive_packet/5,
|
2014-11-14 01:03:26 +01:00
|
|
|
c2s_presence_in/2, c2s_filter_packet/6,
|
2015-06-01 14:38:27 +02:00
|
|
|
c2s_broadcast_recipients/6, mod_opt_type/1]).
|
2009-04-10 09:36:17 +02:00
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-01 06:16:30 +01:00
|
|
|
-include("jlib.hrl").
|
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_mod_caps).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(BAD_HASH_LIFETIME, 600).
|
|
|
|
|
|
|
|
-record(caps,
|
|
|
|
{
|
|
|
|
node = <<"">> :: binary(),
|
|
|
|
version = <<"">> :: binary(),
|
|
|
|
hash = <<"">> :: binary(),
|
|
|
|
exts = [] :: [binary()]
|
|
|
|
}).
|
2010-03-05 10:34:15 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-type caps() :: #caps{}.
|
|
|
|
|
|
|
|
-export_type([caps/0]).
|
|
|
|
|
|
|
|
-record(caps_features,
|
|
|
|
{
|
|
|
|
node_pair = {<<"">>, <<"">>} :: {binary(), binary()},
|
|
|
|
features = [] :: [binary()] | pos_integer()
|
|
|
|
}).
|
|
|
|
|
|
|
|
-record(state, {host = <<"">> :: binary()}).
|
2010-03-05 10:34:15 +01:00
|
|
|
|
2016-04-13 10:41:04 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
|
|
|
-callback caps_read(binary(), {binary(), binary()}) ->
|
|
|
|
{ok, non_neg_integer() | [binary()]} | error.
|
|
|
|
-callback caps_write(binary(), {binary(), binary()},
|
|
|
|
non_neg_integer() | [binary()]) -> any().
|
|
|
|
|
2010-03-05 10:34:15 +01:00
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
gen_server:start_link({local, Proc}, ?MODULE,
|
|
|
|
[Host, Opts], []).
|
2010-03-05 10:34:15 +01:00
|
|
|
|
|
|
|
start(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]},
|
|
|
|
transient, 1000, worker, [?MODULE]},
|
2010-03-05 10:34:15 +01:00
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
gen_server:call(Proc, stop),
|
|
|
|
supervisor:terminate_child(ejabberd_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
|
|
|
|
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],
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:foldl(fun (SubNode, Acc) ->
|
|
|
|
NodePair = {Node, SubNode},
|
|
|
|
case cache_tab:lookup(caps_features, NodePair,
|
2014-07-10 12:16:33 +02:00
|
|
|
caps_read_fun(Host, NodePair))
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
{ok, Features} when is_list(Features) ->
|
|
|
|
Features ++ Acc;
|
|
|
|
_ -> Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
[], SubNodes).
|
|
|
|
|
2014-07-10 12:16:33 +02:00
|
|
|
-spec read_caps([xmlel()]) -> nothing | caps().
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
read_caps(Els) -> read_caps(Els, nothing).
|
|
|
|
|
|
|
|
read_caps([#xmlel{name = <<"c">>, attrs = Attrs}
|
|
|
|
| Tail],
|
|
|
|
Result) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"xmlns">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_CAPS ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Node = fxml:get_attr_s(<<"node">>, Attrs),
|
|
|
|
Version = fxml:get_attr_s(<<"ver">>, Attrs),
|
|
|
|
Hash = fxml:get_attr_s(<<"hash">>, Attrs),
|
|
|
|
Exts = str:tokens(fxml:get_attr_s(<<"ext">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
<<" ">>),
|
|
|
|
read_caps(Tail,
|
|
|
|
#caps{node = Node, hash = Hash, version = Version,
|
|
|
|
exts = Exts});
|
|
|
|
_ -> read_caps(Tail, Result)
|
2007-12-01 06:16:30 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
read_caps([#xmlel{name = <<"x">>, attrs = Attrs}
|
|
|
|
| Tail],
|
|
|
|
Result) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"xmlns">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_MUC_USER -> nothing;
|
|
|
|
_ -> read_caps(Tail, Result)
|
2008-01-11 14:57:29 +01:00
|
|
|
end;
|
|
|
|
read_caps([_ | Tail], Result) ->
|
|
|
|
read_caps(Tail, Result);
|
2013-03-14 10:33:02 +01:00
|
|
|
read_caps([], Result) -> Result.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
user_send_packet(#xmlel{name = <<"presence">>, attrs = Attrs,
|
|
|
|
children = Els} = Pkt,
|
|
|
|
_C2SState,
|
|
|
|
#jid{luser = User, lserver = Server} = From,
|
2014-07-10 12:16:33 +02:00
|
|
|
#jid{luser = User, lserver = Server,
|
2015-06-22 15:56:08 +02:00
|
|
|
lresource = <<"">>}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Type = fxml:get_attr_s(<<"type">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
if Type == <<"">>; Type == <<"available">> ->
|
|
|
|
case read_caps(Els) of
|
|
|
|
nothing -> ok;
|
|
|
|
#caps{version = Version, exts = Exts} = Caps ->
|
|
|
|
feature_request(Server, From, Caps, [Version | Exts])
|
|
|
|
end;
|
|
|
|
true -> ok
|
2014-07-10 12:16:33 +02:00
|
|
|
end,
|
|
|
|
Pkt;
|
2015-06-22 15:56:08 +02:00
|
|
|
user_send_packet(Pkt, _C2SState, _From, _To) ->
|
2014-07-10 12:16:33 +02:00
|
|
|
Pkt.
|
|
|
|
|
2015-06-22 15:56:08 +02:00
|
|
|
user_receive_packet(#xmlel{name = <<"presence">>, attrs = Attrs,
|
|
|
|
children = Els} = Pkt,
|
|
|
|
_C2SState,
|
|
|
|
#jid{lserver = Server},
|
|
|
|
From, _To) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Type = fxml:get_attr_s(<<"type">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
IsRemote = not lists:member(From#jid.lserver, ?MYHOSTS),
|
|
|
|
if IsRemote and
|
|
|
|
((Type == <<"">>) or (Type == <<"available">>)) ->
|
|
|
|
case read_caps(Els) of
|
|
|
|
nothing -> ok;
|
|
|
|
#caps{version = Version, exts = Exts} = Caps ->
|
|
|
|
feature_request(Server, From, Caps, [Version | Exts])
|
|
|
|
end;
|
|
|
|
true -> ok
|
2014-07-10 12:16:33 +02:00
|
|
|
end,
|
|
|
|
Pkt;
|
2015-06-22 15:56:08 +02:00
|
|
|
user_receive_packet(Pkt, _C2SState, _JID, _From, _To) ->
|
2014-07-10 12:16:33 +02:00
|
|
|
Pkt.
|
|
|
|
|
|
|
|
-spec caps_stream_features([xmlel()], binary()) -> [xmlel()].
|
2010-09-28 06:18:57 +02:00
|
|
|
|
2010-05-07 18:32:57 +02:00
|
|
|
caps_stream_features(Acc, MyHost) ->
|
|
|
|
case make_my_disco_hash(MyHost) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> Acc;
|
|
|
|
Hash ->
|
|
|
|
[#xmlel{name = <<"c">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_CAPS}, {<<"hash">>, <<"sha-1">>},
|
|
|
|
{<<"node">>, ?EJABBERD_URI}, {<<"ver">>, Hash}],
|
|
|
|
children = []}
|
|
|
|
| Acc]
|
2010-05-07 18:32:57 +02:00
|
|
|
end.
|
|
|
|
|
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,
|
|
|
|
[From, To, <<"">>, Lang]);
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end.
|
2010-05-07 18:32:57 +02:00
|
|
|
|
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, [],
|
|
|
|
[From, To, <<"">>, Lang]);
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end.
|
2010-05-07 18:32:57 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
disco_info(Acc, Host, Module, Node, Lang) ->
|
|
|
|
case is_valid_node(Node) of
|
|
|
|
true ->
|
|
|
|
ejabberd_hooks:run_fold(disco_info, Host, [],
|
|
|
|
[Host, Module, <<"">>, Lang]);
|
|
|
|
false ->
|
|
|
|
Acc
|
|
|
|
end.
|
2010-05-07 18:32:57 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
c2s_presence_in(C2SState,
|
|
|
|
{From, To, {_, _, Attrs, Els}}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Type = fxml:get_attr_s(<<"type">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
Subscription = ejabberd_c2s:get_subscription(From,
|
|
|
|
C2SState),
|
|
|
|
Insert = ((Type == <<"">>) or (Type == <<"available">>))
|
|
|
|
and ((Subscription == both) or (Subscription == to)),
|
|
|
|
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),
|
2013-03-14 10:33:02 +01:00
|
|
|
Rs = case ejabberd_c2s:get_aux_field(caps_resources,
|
|
|
|
C2SState)
|
|
|
|
of
|
|
|
|
{ok, Rs1} -> Rs1;
|
|
|
|
error -> gb_trees:empty()
|
2010-09-28 06:18:57 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
Caps = read_caps(Els),
|
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,
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_c2s:set_aux_field(caps_resources, NewRs,
|
|
|
|
C2SState);
|
|
|
|
true -> C2SState
|
2010-09-28 06:18:57 +02:00
|
|
|
end.
|
|
|
|
|
2014-11-14 01:03:26 +01:00
|
|
|
c2s_filter_packet(InAcc, Host, C2SState, {pep_message, Feature}, To, _Packet) ->
|
|
|
|
case ejabberd_c2s:get_aux_field(caps_resources, C2SState) of
|
|
|
|
{ok, Rs} ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LTo = jid:tolower(To),
|
2014-11-14 01:03:26 +01:00
|
|
|
case gb_trees:lookup(LTo, Rs) of
|
|
|
|
{value, Caps} ->
|
|
|
|
Drop = not lists:member(Feature, get_features(Host, Caps)),
|
|
|
|
{stop, Drop};
|
|
|
|
none ->
|
|
|
|
{stop, true}
|
|
|
|
end;
|
|
|
|
_ -> InAcc
|
|
|
|
end;
|
|
|
|
c2s_filter_packet(Acc, _, _, _, _, _) -> Acc.
|
|
|
|
|
2014-07-10 12:16:33 +02:00
|
|
|
c2s_broadcast_recipients(InAcc, Host, C2SState,
|
|
|
|
{pep_message, Feature}, _From, _Packet) ->
|
|
|
|
case ejabberd_c2s:get_aux_field(caps_resources,
|
|
|
|
C2SState)
|
|
|
|
of
|
|
|
|
{ok, Rs} ->
|
|
|
|
gb_trees_fold(fun (USR, Caps, Acc) ->
|
|
|
|
case lists:member(Feature,
|
|
|
|
get_features(Host, Caps))
|
|
|
|
of
|
|
|
|
true -> [USR | Acc];
|
|
|
|
false -> Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
InAcc, Rs);
|
|
|
|
_ -> InAcc
|
2010-09-28 06:18:57 +02:00
|
|
|
end;
|
2014-07-10 12:16:33 +02:00
|
|
|
c2s_broadcast_recipients(Acc, _, _, _, _, _) -> Acc.
|
2010-09-28 06:18:57 +02:00
|
|
|
|
2014-07-10 12:16:33 +02:00
|
|
|
init([Host, Opts]) ->
|
2016-04-13 10:41:04 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
|
|
|
Mod:init(Host, Opts),
|
2014-07-10 12:16:33 +02:00
|
|
|
MaxSize = gen_mod:get_opt(cache_size, Opts,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
1000),
|
|
|
|
LifeTime = gen_mod:get_opt(cache_life_time, Opts,
|
|
|
|
fun(I) when is_integer(I), I>0 -> I end,
|
|
|
|
timer:hours(24) div 1000),
|
|
|
|
cache_tab:new(caps_features,
|
|
|
|
[{max_size, MaxSize}, {life_time, LifeTime}]),
|
|
|
|
ejabberd_hooks:add(c2s_presence_in, Host, ?MODULE,
|
|
|
|
c2s_presence_in, 75),
|
2014-11-14 01:03:26 +01:00
|
|
|
ejabberd_hooks:add(c2s_filter_packet, Host, ?MODULE,
|
|
|
|
c2s_filter_packet, 75),
|
2010-09-28 06:18:57 +02:00
|
|
|
ejabberd_hooks:add(c2s_broadcast_recipients, Host,
|
|
|
|
?MODULE, c2s_broadcast_recipients, 75),
|
2014-07-10 12:16:33 +02:00
|
|
|
ejabberd_hooks:add(user_send_packet, Host, ?MODULE,
|
|
|
|
user_send_packet, 75),
|
|
|
|
ejabberd_hooks:add(user_receive_packet, Host, ?MODULE,
|
|
|
|
user_receive_packet, 75),
|
|
|
|
ejabberd_hooks:add(c2s_stream_features, Host, ?MODULE,
|
|
|
|
caps_stream_features, 75),
|
|
|
|
ejabberd_hooks:add(s2s_stream_features, Host, ?MODULE,
|
|
|
|
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};
|
|
|
|
handle_call(_Req, _From, State) ->
|
|
|
|
{reply, {error, badarg}, State}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_cast(_Msg, State) -> {noreply, State}.
|
2007-12-01 06:16:30 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info(_Info, State) -> {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),
|
2014-11-14 01:03:26 +01:00
|
|
|
ejabberd_hooks:delete(c2s_filter_packet, Host, ?MODULE,
|
|
|
|
c2s_filter_packet, 75),
|
2010-09-28 06:18:57 +02:00
|
|
|
ejabberd_hooks:delete(c2s_broadcast_recipients, Host,
|
|
|
|
?MODULE, c2s_broadcast_recipients, 75),
|
2013-03-14 10:33:02 +01:00
|
|
|
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),
|
2010-05-07 18:32:57 +02:00
|
|
|
ejabberd_hooks:delete(c2s_stream_features, Host,
|
|
|
|
?MODULE, caps_stream_features, 75),
|
|
|
|
ejabberd_hooks:delete(s2s_stream_features, Host,
|
|
|
|
?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
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
feature_request(Host, From, Caps,
|
|
|
|
[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},
|
|
|
|
case cache_tab:lookup(caps_features, NodePair,
|
2014-07-10 12:16:33 +02:00
|
|
|
caps_read_fun(Host, NodePair))
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
{ok, Fs} when is_list(Fs) ->
|
|
|
|
feature_request(Host, From, Caps, Tail);
|
|
|
|
Other ->
|
|
|
|
NeedRequest = case Other of
|
|
|
|
{ok, TS} -> now_ts() >= TS + (?BAD_HASH_LIFETIME);
|
|
|
|
_ -> true
|
2010-10-05 13:27:28 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
if NeedRequest ->
|
|
|
|
IQ = #iq{type = get, xmlns = ?NS_DISCO_INFO,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_DISCO_INFO},
|
|
|
|
{<<"node">>,
|
|
|
|
<<Node/binary, "#",
|
|
|
|
SubNode/binary>>}],
|
|
|
|
children = []}]},
|
|
|
|
cache_tab:insert(caps_features, NodePair, now_ts(),
|
2014-07-10 12:16:33 +02:00
|
|
|
caps_write_fun(Host, NodePair, now_ts())),
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun (IQReply) ->
|
|
|
|
feature_response(IQReply, Host, From, Caps,
|
|
|
|
SubNodes)
|
|
|
|
end,
|
2015-11-24 16:44:13 +01:00
|
|
|
ejabberd_local:route_iq(jid:make(<<"">>, Host,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">>),
|
|
|
|
From, IQ, F);
|
|
|
|
true -> feature_request(Host, From, Caps, Tail)
|
|
|
|
end
|
2010-03-05 10:34:15 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
feature_request(_Host, _From, _Caps, []) -> ok.
|
2010-03-05 10:34:15 +01:00
|
|
|
|
|
|
|
feature_response(#iq{type = result,
|
2013-03-14 10:33:02 +01:00
|
|
|
sub_el = [#xmlel{children = Els}]},
|
2010-03-05 10:34:15 +01:00
|
|
|
Host, From, Caps, [SubNode | SubNodes]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NodePair = {Caps#caps.node, SubNode},
|
2010-08-07 14:04:57 +02:00
|
|
|
case check_hash(Caps, Els) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true ->
|
|
|
|
Features = lists:flatmap(fun (#xmlel{name =
|
|
|
|
<<"feature">>,
|
|
|
|
attrs = FAttrs}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
[fxml:get_attr_s(<<"var">>, FAttrs)];
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) -> []
|
|
|
|
end,
|
|
|
|
Els),
|
|
|
|
cache_tab:insert(caps_features, NodePair,
|
|
|
|
Features,
|
2014-07-10 12:16:33 +02:00
|
|
|
caps_write_fun(Host, NodePair, Features));
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> ok
|
2010-05-07 18:32:57 +02:00
|
|
|
end,
|
2010-03-05 10:34:15 +01:00
|
|
|
feature_request(Host, From, Caps, SubNodes);
|
2013-03-14 10:33:02 +01:00
|
|
|
feature_response(_IQResult, Host, From, Caps,
|
|
|
|
[_SubNode | SubNodes]) ->
|
2010-03-05 10:34:15 +01:00
|
|
|
feature_request(Host, From, Caps, SubNodes).
|
|
|
|
|
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
|
|
|
|
2014-07-10 12:16:33 +02:00
|
|
|
caps_write_fun(Host, Node, Features) ->
|
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_write(LServer, Node, Features) end.
|
2010-08-30 06:02:47 +02:00
|
|
|
|
2010-05-07 18:32:57 +02:00
|
|
|
make_my_disco_hash(Host) ->
|
2015-11-24 16:44:13 +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} ->
|
|
|
|
Feats = lists:map(fun ({{Feat, _Host}}) ->
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, Feat}],
|
|
|
|
children = []};
|
|
|
|
(Feat) ->
|
|
|
|
#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, Feat}],
|
|
|
|
children = []}
|
|
|
|
end,
|
|
|
|
Features),
|
|
|
|
make_disco_hash(Identities ++ Info ++ Feats, sha1);
|
|
|
|
_Err -> <<"">>
|
2010-05-07 18:32:57 +02:00
|
|
|
end.
|
|
|
|
|
2010-08-07 14:04:57 +02:00
|
|
|
make_disco_hash(DiscoEls, Algo) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Concat = list_to_binary([concat_identities(DiscoEls),
|
|
|
|
concat_features(DiscoEls), concat_info(DiscoEls)]),
|
|
|
|
jlib:encode_base64(case Algo of
|
2013-11-05 11:05:12 +01:00
|
|
|
md5 -> erlang:md5(Concat);
|
|
|
|
sha1 -> p1_sha:sha1(Concat);
|
2013-06-20 10:40:44 +02:00
|
|
|
sha224 -> p1_sha:sha224(Concat);
|
|
|
|
sha256 -> p1_sha:sha256(Concat);
|
|
|
|
sha384 -> p1_sha:sha384(Concat);
|
|
|
|
sha512 -> p1_sha:sha512(Concat)
|
2013-03-14 10:33:02 +01:00
|
|
|
end).
|
2010-08-07 14:04:57 +02:00
|
|
|
|
|
|
|
check_hash(Caps, Els) ->
|
|
|
|
case Caps#caps.hash of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"md5">> ->
|
|
|
|
Caps#caps.version == make_disco_hash(Els, md5);
|
|
|
|
<<"sha-1">> ->
|
|
|
|
Caps#caps.version == make_disco_hash(Els, sha1);
|
|
|
|
<<"sha-224">> ->
|
|
|
|
Caps#caps.version == make_disco_hash(Els, sha224);
|
|
|
|
<<"sha-256">> ->
|
|
|
|
Caps#caps.version == make_disco_hash(Els, sha256);
|
|
|
|
<<"sha-384">> ->
|
|
|
|
Caps#caps.version == make_disco_hash(Els, sha384);
|
|
|
|
<<"sha-512">> ->
|
|
|
|
Caps#caps.version == make_disco_hash(Els, sha512);
|
|
|
|
_ -> true
|
2010-08-07 14:04:57 +02:00
|
|
|
end.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2010-05-07 18:32:57 +02:00
|
|
|
concat_features(Els) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:usort(lists:flatmap(fun (#xmlel{name =
|
|
|
|
<<"feature">>,
|
|
|
|
attrs = Attrs}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
[[fxml:get_attr_s(<<"var">>, Attrs), $<]];
|
2013-03-14 10:33:02 +01:00
|
|
|
(_) -> []
|
|
|
|
end,
|
|
|
|
Els)).
|
2010-05-07 18:32:57 +02:00
|
|
|
|
|
|
|
concat_identities(Els) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:sort(lists:flatmap(fun (#xmlel{name =
|
|
|
|
<<"identity">>,
|
|
|
|
attrs = Attrs}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
[[fxml:get_attr_s(<<"category">>, Attrs),
|
|
|
|
$/, fxml:get_attr_s(<<"type">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
$/,
|
2016-02-03 19:03:17 +01:00
|
|
|
fxml:get_attr_s(<<"xml:lang">>, Attrs),
|
|
|
|
$/, fxml:get_attr_s(<<"name">>, Attrs),
|
2013-03-14 10:33:02 +01:00
|
|
|
$<]];
|
|
|
|
(_) -> []
|
|
|
|
end,
|
|
|
|
Els)).
|
2010-05-07 18:32:57 +02:00
|
|
|
|
|
|
|
concat_info(Els) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:sort(lists:flatmap(fun (#xmlel{name = <<"x">>,
|
|
|
|
attrs = Attrs, children = Fields}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case {fxml:get_attr_s(<<"xmlns">>, Attrs),
|
|
|
|
fxml:get_attr_s(<<"type">>, Attrs)}
|
2013-03-14 10:33:02 +01:00
|
|
|
of
|
|
|
|
{?NS_XDATA, <<"result">>} ->
|
|
|
|
[concat_xdata_fields(Fields)];
|
|
|
|
_ -> []
|
|
|
|
end;
|
|
|
|
(_) -> []
|
|
|
|
end,
|
|
|
|
Els)).
|
2010-05-07 18:32:57 +02:00
|
|
|
|
|
|
|
concat_xdata_fields(Fields) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[Form, Res] = lists:foldl(fun (#xmlel{name =
|
|
|
|
<<"field">>,
|
|
|
|
attrs = Attrs, children = Els} =
|
|
|
|
El,
|
|
|
|
[FormType, VarFields] = Acc) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"var">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> Acc;
|
|
|
|
<<"FORM_TYPE">> ->
|
2016-02-03 19:03:17 +01:00
|
|
|
[fxml:get_subtag_cdata(El,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"value">>),
|
|
|
|
VarFields];
|
|
|
|
Var ->
|
|
|
|
[FormType,
|
|
|
|
[[[Var, $<],
|
|
|
|
lists:sort(lists:flatmap(fun
|
|
|
|
(#xmlel{name
|
|
|
|
=
|
|
|
|
<<"value">>,
|
|
|
|
children
|
|
|
|
=
|
|
|
|
VEls}) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
[[fxml:get_cdata(VEls),
|
2013-03-14 10:33:02 +01:00
|
|
|
$<]];
|
|
|
|
(_) ->
|
|
|
|
[]
|
|
|
|
end,
|
|
|
|
Els))]
|
|
|
|
| VarFields]]
|
|
|
|
end;
|
|
|
|
(_, Acc) -> Acc
|
|
|
|
end,
|
|
|
|
[<<"">>, []], Fields),
|
2010-05-07 18:32:57 +02:00
|
|
|
[Form, $<, lists:sort(Res)].
|
2010-09-28 06:18:57 +02:00
|
|
|
|
|
|
|
gb_trees_fold(F, Acc, Tree) ->
|
|
|
|
Iter = gb_trees:iterator(Tree),
|
|
|
|
gb_trees_fold_iter(F, Acc, Iter).
|
|
|
|
|
|
|
|
gb_trees_fold_iter(F, Acc, Iter) ->
|
|
|
|
case gb_trees:next(Iter) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{Key, Val, NewIter} ->
|
|
|
|
NewAcc = F(Key, Val, Acc),
|
|
|
|
gb_trees_fold_iter(F, NewAcc, NewIter);
|
|
|
|
_ -> Acc
|
2010-09-28 06:18:57 +02:00
|
|
|
end.
|
2010-10-05 13:27:28 +02:00
|
|
|
|
|
|
|
now_ts() ->
|
2015-12-07 10:26:51 +01:00
|
|
|
p1_time_compat:system_time(seconds).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
is_valid_node(Node) ->
|
|
|
|
case str:tokens(Node, <<"#">>) of
|
|
|
|
[?EJABBERD_URI|_] ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
2013-07-21 14:53:44 +02:00
|
|
|
|
2014-07-14 05:49:02 +02:00
|
|
|
caps_features_schema() ->
|
|
|
|
{record_info(fields, caps_features), #caps_features{}}.
|
|
|
|
|
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]) ->
|
|
|
|
Feature1 = case catch jlib:binary_to_integer(Feature) of
|
|
|
|
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)],
|
|
|
|
case Features of
|
|
|
|
[I] when is_integer(I), DBType == mnesia ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
#caps_features{node_pair = NodePair, features = I});
|
|
|
|
[I] when is_integer(I), DBType == riak ->
|
|
|
|
ejabberd_riak:put(
|
2014-07-14 05:49:02 +02:00
|
|
|
#caps_features{node_pair = NodePair, features = I},
|
|
|
|
caps_features_schema());
|
2014-07-10 12:16:33 +02:00
|
|
|
_ when DBType == mnesia ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
#caps_features{node_pair = NodePair, features = Features});
|
|
|
|
_ when DBType == riak ->
|
|
|
|
ejabberd_riak:put(
|
2014-07-14 05:49:02 +02:00
|
|
|
#caps_features{node_pair = NodePair, features = Features},
|
|
|
|
caps_features_schema());
|
2016-04-20 11:27:32 +02:00
|
|
|
_ when DBType == sql ->
|
2014-07-10 12:16:33 +02:00
|
|
|
ok
|
|
|
|
end,
|
|
|
|
import_next(LServer, DBType, ets:next(caps_features_tmp, NodePair)).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(cache_life_time) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
mod_opt_type(cache_size) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
2016-04-27 16:10:50 +02:00
|
|
|
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(_) ->
|
|
|
|
[cache_life_time, cache_size, db_type].
|