2003-01-01 20:54:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_disco.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2009-07-17 22:45:44 +02:00
|
|
|
%%% Purpose : Service Discovery (XEP-0030) support
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 1 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
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
|
|
|
%%%
|
2014-02-22 11:27:40 +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
|
|
|
%%%
|
2003-01-01 20:54:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_disco).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 30, '2.4'}).
|
|
|
|
-protocol({xep, 157, '1.0'}).
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, stop/1, process_local_iq_items/3,
|
|
|
|
process_local_iq_info/3, get_local_identity/5,
|
|
|
|
get_local_features/5, get_local_services/5,
|
|
|
|
process_sm_iq_items/3, process_sm_iq_info/3,
|
|
|
|
get_sm_identity/5, get_sm_features/5, get_sm_items/5,
|
|
|
|
get_info/5, register_feature/2, unregister_feature/2,
|
2013-08-12 14:25:05 +02:00
|
|
|
register_extra_domain/2, unregister_extra_domain/2,
|
2015-06-01 14:38:27 +02:00
|
|
|
transform_module_options/1, mod_opt_type/1]).
|
2003-01-01 20:54:44 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2011-07-13 17:06:17 +02:00
|
|
|
-include("mod_roster.hrl").
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2004-07-10 00:34:26 +02:00
|
|
|
ejabberd_local:refresh_iq_handlers(),
|
2013-03-14 10:33:02 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_DISCO_ITEMS, ?MODULE,
|
|
|
|
process_local_iq_items, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_DISCO_INFO, ?MODULE,
|
|
|
|
process_local_iq_info, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_DISCO_ITEMS, ?MODULE, process_sm_iq_items,
|
|
|
|
IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_DISCO_INFO, ?MODULE, process_sm_iq_info,
|
|
|
|
IQDisc),
|
|
|
|
catch ets:new(disco_features,
|
|
|
|
[named_table, ordered_set, public]),
|
|
|
|
register_feature(Host, <<"iq">>),
|
|
|
|
register_feature(Host, <<"presence">>),
|
|
|
|
catch ets:new(disco_extra_domains,
|
|
|
|
[named_table, ordered_set, public]),
|
|
|
|
ExtraDomains = gen_mod:get_opt(extra_domains, Opts,
|
|
|
|
fun(Hs) ->
|
|
|
|
[iolist_to_binary(H) || H <- Hs]
|
|
|
|
end, []),
|
|
|
|
lists:foreach(fun (Domain) ->
|
|
|
|
register_extra_domain(Host, Domain)
|
|
|
|
end,
|
2005-07-15 01:12:20 +02:00
|
|
|
ExtraDomains),
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:new(disco_sm_features,
|
|
|
|
[named_table, ordered_set, public]),
|
|
|
|
catch ets:new(disco_sm_nodes,
|
|
|
|
[named_table, ordered_set, public]),
|
|
|
|
ejabberd_hooks:add(disco_local_items, Host, ?MODULE,
|
|
|
|
get_local_services, 100),
|
|
|
|
ejabberd_hooks:add(disco_local_features, Host, ?MODULE,
|
|
|
|
get_local_features, 100),
|
|
|
|
ejabberd_hooks:add(disco_local_identity, Host, ?MODULE,
|
|
|
|
get_local_identity, 100),
|
|
|
|
ejabberd_hooks:add(disco_sm_items, Host, ?MODULE,
|
|
|
|
get_sm_items, 100),
|
|
|
|
ejabberd_hooks:add(disco_sm_features, Host, ?MODULE,
|
|
|
|
get_sm_features, 100),
|
|
|
|
ejabberd_hooks:add(disco_sm_identity, Host, ?MODULE,
|
|
|
|
get_sm_identity, 100),
|
|
|
|
ejabberd_hooks:add(disco_info, Host, ?MODULE, get_info,
|
|
|
|
100),
|
2003-01-01 20:54:44 +01:00
|
|
|
ok.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_hooks:delete(disco_sm_identity, Host, ?MODULE,
|
|
|
|
get_sm_identity, 100),
|
|
|
|
ejabberd_hooks:delete(disco_sm_features, Host, ?MODULE,
|
|
|
|
get_sm_features, 100),
|
|
|
|
ejabberd_hooks:delete(disco_sm_items, Host, ?MODULE,
|
|
|
|
get_sm_items, 100),
|
|
|
|
ejabberd_hooks:delete(disco_local_identity, Host,
|
|
|
|
?MODULE, get_local_identity, 100),
|
|
|
|
ejabberd_hooks:delete(disco_local_features, Host,
|
|
|
|
?MODULE, get_local_features, 100),
|
|
|
|
ejabberd_hooks:delete(disco_local_items, Host, ?MODULE,
|
|
|
|
get_local_services, 100),
|
|
|
|
ejabberd_hooks:delete(disco_info, Host, ?MODULE,
|
|
|
|
get_info, 100),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_DISCO_ITEMS),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_DISCO_INFO),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_DISCO_ITEMS),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_DISCO_INFO),
|
2005-07-15 01:12:20 +02:00
|
|
|
catch ets:match_delete(disco_features, {{'_', Host}}),
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:match_delete(disco_extra_domains,
|
|
|
|
{{'_', Host}}),
|
2004-07-10 00:34:26 +02:00
|
|
|
ok.
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
register_feature(Host, Feature) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:new(disco_features,
|
|
|
|
[named_table, ordered_set, public]),
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:insert(disco_features, {{Feature, Host}}).
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
unregister_feature(Host, Feature) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:new(disco_features,
|
|
|
|
[named_table, ordered_set, public]),
|
2005-06-20 05:18:13 +02:00
|
|
|
ets:delete(disco_features, {Feature, Host}).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2005-07-15 01:12:20 +02:00
|
|
|
register_extra_domain(Host, Domain) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:new(disco_extra_domains,
|
|
|
|
[named_table, ordered_set, public]),
|
2005-07-15 01:12:20 +02:00
|
|
|
ets:insert(disco_extra_domains, {{Domain, Host}}).
|
2003-10-09 20:09:05 +02:00
|
|
|
|
2005-07-15 01:12:20 +02:00
|
|
|
unregister_extra_domain(Host, Domain) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
catch ets:new(disco_extra_domains,
|
|
|
|
[named_table, ordered_set, public]),
|
2005-07-15 01:12:20 +02:00
|
|
|
ets:delete(disco_extra_domains, {Domain, Host}).
|
2003-10-09 20:09:05 +02:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_local_iq_items(From, To,
|
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
2003-01-01 20:54:44 +01:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get ->
|
2016-02-03 19:03:17 +01:00
|
|
|
Node = fxml:get_tag_attr_s(<<"node">>, SubEl),
|
2013-03-14 10:33:02 +01:00
|
|
|
Host = To#jid.lserver,
|
|
|
|
case ejabberd_hooks:run_fold(disco_local_items, Host,
|
|
|
|
empty, [From, To, Node, Lang])
|
|
|
|
of
|
|
|
|
{result, Items} ->
|
|
|
|
ANode = case Node of
|
|
|
|
<<"">> -> [];
|
|
|
|
_ -> [{<<"node">>, Node}]
|
|
|
|
end,
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_DISCO_ITEMS} | ANode],
|
|
|
|
children = Items}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
|
|
|
end
|
2003-01-01 20:54:44 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_local_iq_info(From, To,
|
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
2003-01-01 20:54:44 +01:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get ->
|
|
|
|
Host = To#jid.lserver,
|
2016-02-03 19:03:17 +01:00
|
|
|
Node = fxml:get_tag_attr_s(<<"node">>, SubEl),
|
2013-03-14 10:33:02 +01:00
|
|
|
Identity = ejabberd_hooks:run_fold(disco_local_identity,
|
|
|
|
Host, [], [From, To, Node, Lang]),
|
|
|
|
Info = ejabberd_hooks:run_fold(disco_info, Host, [],
|
|
|
|
[Host, ?MODULE, Node, Lang]),
|
|
|
|
case ejabberd_hooks:run_fold(disco_local_features, Host,
|
|
|
|
empty, [From, To, Node, Lang])
|
|
|
|
of
|
|
|
|
{result, Features} ->
|
|
|
|
ANode = case Node of
|
|
|
|
<<"">> -> [];
|
|
|
|
_ -> [{<<"node">>, Node}]
|
|
|
|
end,
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_DISCO_INFO} | ANode],
|
|
|
|
children =
|
|
|
|
Identity ++
|
|
|
|
Info ++ features_to_xml(Features)}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
|
|
|
end
|
2003-01-01 20:54:44 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_identity(Acc, _From, _To, <<>>, _Lang) ->
|
|
|
|
Acc ++
|
|
|
|
[#xmlel{name = <<"identity">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"category">>, <<"server">>}, {<<"type">>, <<"im">>},
|
|
|
|
{<<"name">>, <<"ejabberd">>}],
|
|
|
|
children = []}];
|
2005-09-04 03:58:47 +02:00
|
|
|
get_local_identity(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_features({error, _Error} = Acc, _From, _To,
|
|
|
|
_Node, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Acc;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_features(Acc, _From, To, <<>>, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Feats = case Acc of
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, Features} -> Features;
|
|
|
|
empty -> []
|
2005-09-04 03:58:47 +02:00
|
|
|
end,
|
2005-10-30 21:47:26 +01:00
|
|
|
Host = To#jid.lserver,
|
|
|
|
{result,
|
2013-03-14 10:33:02 +01:00
|
|
|
ets:select(disco_features,
|
|
|
|
[{{{'_', Host}}, [], ['$_']}])
|
|
|
|
++ Feats};
|
2016-04-05 12:09:44 +02:00
|
|
|
get_local_features(Acc, _From, _To, _Node, Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
case Acc of
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, _Features} -> Acc;
|
2016-04-05 12:09:44 +02:00
|
|
|
empty ->
|
|
|
|
Txt = <<"No features available">>,
|
|
|
|
{error, ?ERRT_ITEM_NOT_FOUND(Lang, Txt)}
|
2005-09-04 03:58:47 +02:00
|
|
|
end.
|
|
|
|
|
2010-05-07 18:32:57 +02:00
|
|
|
features_to_xml(FeatureList) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"feature">>,
|
|
|
|
attrs = [{<<"var">>, Feat}], children = []}
|
|
|
|
|| Feat
|
|
|
|
<- lists:usort(lists:map(fun ({{Feature, _Host}}) ->
|
|
|
|
Feature;
|
|
|
|
(Feature) when is_binary(Feature) ->
|
|
|
|
Feature
|
|
|
|
end,
|
|
|
|
FeatureList))].
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2003-10-09 20:09:05 +02:00
|
|
|
domain_to_xml({Domain}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"item">>, attrs = [{<<"jid">>, Domain}],
|
|
|
|
children = []};
|
2003-01-02 22:01:12 +01:00
|
|
|
domain_to_xml(Domain) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"item">>, attrs = [{<<"jid">>, Domain}],
|
|
|
|
children = []}.
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_services({error, _Error} = Acc, _From, _To,
|
|
|
|
_Node, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Acc;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_local_services(Acc, _From, To, <<>>, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Items = case Acc of
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, Its} -> Its;
|
|
|
|
empty -> []
|
2005-09-04 03:58:47 +02:00
|
|
|
end,
|
|
|
|
Host = To#jid.lserver,
|
2003-02-04 21:45:23 +01:00
|
|
|
{result,
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:usort(lists:map(fun domain_to_xml/1,
|
|
|
|
get_vh_services(Host) ++
|
|
|
|
ets:select(disco_extra_domains,
|
|
|
|
[{{{'$1', Host}}, [], ['$1']}])))
|
|
|
|
++ Items};
|
|
|
|
get_local_services({result, _} = Acc, _From, _To, _Node,
|
|
|
|
_Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Acc;
|
2016-04-05 12:09:44 +02:00
|
|
|
get_local_services(empty, _From, _To, _Node, Lang) ->
|
|
|
|
{error, ?ERRT_ITEM_NOT_FOUND(Lang, <<"No services available">>)}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_services(Host) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Hosts = lists:sort(fun (H1, H2) ->
|
|
|
|
byte_size(H1) >= byte_size(H2)
|
|
|
|
end,
|
|
|
|
?MYHOSTS),
|
|
|
|
lists:filter(fun (H) ->
|
|
|
|
case lists:dropwhile(fun (VH) ->
|
|
|
|
not
|
|
|
|
str:suffix(
|
|
|
|
<<".", VH/binary>>,
|
|
|
|
H)
|
|
|
|
end,
|
|
|
|
Hosts)
|
|
|
|
of
|
|
|
|
[] -> false;
|
|
|
|
[VH | _] -> VH == Host
|
2005-04-20 00:19:22 +02:00
|
|
|
end
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
ejabberd_router:dirty_get_all_routes()).
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_sm_iq_items(From, To,
|
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get ->
|
|
|
|
case is_presence_subscribed(From, To) of
|
|
|
|
true ->
|
|
|
|
Host = To#jid.lserver,
|
2016-02-03 19:03:17 +01:00
|
|
|
Node = fxml:get_tag_attr_s(<<"node">>, SubEl),
|
2013-03-14 10:33:02 +01:00
|
|
|
case ejabberd_hooks:run_fold(disco_sm_items, Host,
|
|
|
|
empty, [From, To, Node, Lang])
|
|
|
|
of
|
|
|
|
{result, Items} ->
|
|
|
|
ANode = case Node of
|
|
|
|
<<"">> -> [];
|
|
|
|
_ -> [{<<"node">>, Node}]
|
|
|
|
end,
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_DISCO_ITEMS}
|
|
|
|
| ANode],
|
|
|
|
children = Items}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
|
|
|
end;
|
|
|
|
false ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Not subscribed">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
IQ#iq{type = error,
|
2016-04-05 12:09:44 +02:00
|
|
|
sub_el = [SubEl, ?ERRT_SERVICE_UNAVAILABLE(Lang, Txt)]}
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2003-01-01 20:54:44 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_items({error, _Error} = Acc, _From, _To, _Node,
|
|
|
|
_Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Acc;
|
2010-04-22 15:29:20 +02:00
|
|
|
get_sm_items(Acc, From,
|
2013-03-14 10:33:02 +01:00
|
|
|
#jid{user = User, server = Server} = To, <<>>, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Items = case Acc of
|
2013-03-14 10:33:02 +01:00
|
|
|
{result, Its} -> Its;
|
|
|
|
empty -> []
|
2005-09-04 03:58:47 +02:00
|
|
|
end,
|
2010-04-22 15:29:20 +02:00
|
|
|
Items1 = case is_presence_subscribed(From, To) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> get_user_resources(User, Server);
|
|
|
|
_ -> []
|
|
|
|
end,
|
2005-09-04 03:58:47 +02:00
|
|
|
{result, Items ++ Items1};
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_items({result, _} = Acc, _From, _To, _Node,
|
|
|
|
_Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Acc;
|
2016-04-05 12:09:44 +02:00
|
|
|
get_sm_items(empty, From, To, _Node, Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
#jid{luser = LFrom, lserver = LSFrom} = From,
|
|
|
|
#jid{luser = LTo, lserver = LSTo} = To,
|
|
|
|
case {LFrom, LSFrom} of
|
2013-03-14 10:33:02 +01:00
|
|
|
{LTo, LSTo} -> {error, ?ERR_ITEM_NOT_FOUND};
|
2016-04-05 12:09:44 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Query to another users is forbidden">>,
|
|
|
|
{error, ?ERRT_NOT_ALLOWED(Lang, Txt)}
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
|
|
|
|
2015-04-26 16:36:17 +02:00
|
|
|
is_presence_subscribed(#jid{luser = User, lserver = Server},
|
2015-04-26 16:32:03 +02:00
|
|
|
#jid{luser = User, lserver = Server}) -> true;
|
2015-04-26 16:36:17 +02:00
|
|
|
is_presence_subscribed(#jid{luser = FromUser, lserver = FromServer},
|
|
|
|
#jid{luser = ToUser, lserver = ToServer}) ->
|
|
|
|
lists:any(fun (#roster{jid = {SubUser, SubServer, _}, subscription = Sub})
|
|
|
|
when FromUser == SubUser, FromServer == SubServer,
|
|
|
|
Sub /= none ->
|
|
|
|
true;
|
|
|
|
(_RosterEntry) ->
|
|
|
|
false
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
2015-04-26 16:36:17 +02:00
|
|
|
ejabberd_hooks:run_fold(roster_get, ToServer, [],
|
|
|
|
[{ToUser, ToServer}])).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
process_sm_iq_info(From, To,
|
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get ->
|
|
|
|
case is_presence_subscribed(From, To) of
|
|
|
|
true ->
|
|
|
|
Host = To#jid.lserver,
|
2016-02-03 19:03:17 +01:00
|
|
|
Node = fxml:get_tag_attr_s(<<"node">>, SubEl),
|
2013-03-14 10:33:02 +01:00
|
|
|
Identity = ejabberd_hooks:run_fold(disco_sm_identity,
|
|
|
|
Host, [],
|
|
|
|
[From, To, Node, Lang]),
|
2016-02-09 15:59:54 +01:00
|
|
|
Info = ejabberd_hooks:run_fold(disco_info, Host, [],
|
|
|
|
[From, To, Node, Lang]),
|
2013-03-14 10:33:02 +01:00
|
|
|
case ejabberd_hooks:run_fold(disco_sm_features, Host,
|
|
|
|
empty, [From, To, Node, Lang])
|
|
|
|
of
|
|
|
|
{result, Features} ->
|
|
|
|
ANode = case Node of
|
|
|
|
<<"">> -> [];
|
|
|
|
_ -> [{<<"node">>, Node}]
|
|
|
|
end,
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_DISCO_INFO}
|
|
|
|
| ANode],
|
|
|
|
children =
|
2016-02-09 15:59:54 +01:00
|
|
|
Identity ++ Info ++
|
2013-03-14 10:33:02 +01:00
|
|
|
features_to_xml(Features)}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
|
|
|
end;
|
|
|
|
false ->
|
2016-04-05 12:09:44 +02:00
|
|
|
Txt = <<"Not subscribed">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
IQ#iq{type = error,
|
2016-04-05 12:09:44 +02:00
|
|
|
sub_el = [SubEl, ?ERRT_SERVICE_UNAVAILABLE(Lang, Txt)]}
|
2013-03-14 10:33:02 +01:00
|
|
|
end
|
2003-01-01 20:54:44 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_identity(Acc, _From,
|
|
|
|
#jid{luser = LUser, lserver = LServer}, _Node, _Lang) ->
|
|
|
|
Acc ++
|
|
|
|
case ejabberd_auth:is_user_exists(LUser, LServer) of
|
|
|
|
true ->
|
|
|
|
[#xmlel{name = <<"identity">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"category">>, <<"account">>},
|
|
|
|
{<<"type">>, <<"registered">>}],
|
|
|
|
children = []}];
|
|
|
|
_ -> []
|
|
|
|
end.
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2016-04-05 12:09:44 +02:00
|
|
|
get_sm_features(empty, From, To, _Node, Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
#jid{luser = LFrom, lserver = LSFrom} = From,
|
|
|
|
#jid{luser = LTo, lserver = LSTo} = To,
|
|
|
|
case {LFrom, LSFrom} of
|
2013-03-14 10:33:02 +01:00
|
|
|
{LTo, LSTo} -> {error, ?ERR_ITEM_NOT_FOUND};
|
2016-04-05 12:09:44 +02:00
|
|
|
_ ->
|
|
|
|
Txt = <<"Query to another users is forbidden">>,
|
|
|
|
{error, ?ERRT_NOT_ALLOWED(Lang, Txt)}
|
2005-09-04 03:58:47 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) -> Acc.
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_user_resources(User, Server) ->
|
|
|
|
Rs = ejabberd_sm:get_user_resources(User, Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun (R) ->
|
|
|
|
#xmlel{name = <<"item">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"jid">>,
|
|
|
|
<<User/binary, "@", Server/binary, "/",
|
|
|
|
R/binary>>},
|
|
|
|
{<<"name">>, User}],
|
|
|
|
children = []}
|
|
|
|
end,
|
|
|
|
lists:sort(Rs)).
|
2009-07-17 22:45:44 +02:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_module_options(Opts) ->
|
|
|
|
lists:map(
|
|
|
|
fun({server_info, Infos}) ->
|
|
|
|
NewInfos = lists:map(
|
|
|
|
fun({Modules, Name, URLs}) ->
|
|
|
|
[[{modules, Modules},
|
|
|
|
{name, Name},
|
|
|
|
{urls, URLs}]];
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Infos),
|
|
|
|
{server_info, NewInfos};
|
|
|
|
(Opt) ->
|
|
|
|
Opt
|
|
|
|
end, Opts).
|
|
|
|
|
2009-07-17 22:45:44 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
%%% Support for: XEP-0157 Contact Addresses for XMPP Services
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_info(_A, Host, Mod, Node, _Lang) when Node == <<>> ->
|
2010-05-07 18:32:57 +02:00
|
|
|
Module = case Mod of
|
2013-03-14 10:33:02 +01:00
|
|
|
undefined -> ?MODULE;
|
|
|
|
_ -> Mod
|
2010-05-07 18:32:57 +02:00
|
|
|
end,
|
2009-07-17 22:45:44 +02:00
|
|
|
Serverinfo_fields = get_fields_xml(Host, Module),
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_XDATA}, {<<"type">>, <<"result">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"field">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"var">>, <<"FORM_TYPE">>},
|
|
|
|
{<<"type">>, <<"hidden">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"value">>, attrs = [],
|
|
|
|
children = [{xmlcdata, ?NS_SERVERINFO}]}]}]
|
|
|
|
++ Serverinfo_fields}];
|
|
|
|
get_info(Acc, _, _, _Node, _) -> Acc.
|
2009-07-17 22:45:44 +02:00
|
|
|
|
|
|
|
get_fields_xml(Host, Module) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
Fields = gen_mod:get_module_opt(
|
|
|
|
Host, ?MODULE, server_info,
|
|
|
|
fun(L) ->
|
|
|
|
lists:map(
|
|
|
|
fun(Opts) ->
|
|
|
|
Mods = proplists:get_value(modules, Opts, all),
|
2014-03-25 06:52:39 +01:00
|
|
|
Name = proplists:get_value(name, Opts, <<>>),
|
2013-08-12 14:25:05 +02:00
|
|
|
URLs = proplists:get_value(urls, Opts, []),
|
|
|
|
{Mods, Name, URLs}
|
2014-03-25 06:52:39 +01:00
|
|
|
end, L)
|
2013-08-12 14:25:05 +02:00
|
|
|
end, []),
|
2013-03-14 10:33:02 +01:00
|
|
|
Fields_good = lists:filter(fun ({Modules, _, _}) ->
|
|
|
|
case Modules of
|
|
|
|
all -> true;
|
|
|
|
Modules ->
|
|
|
|
lists:member(Module, Modules)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Fields),
|
2009-07-17 22:45:44 +02:00
|
|
|
fields_to_xml(Fields_good).
|
|
|
|
|
|
|
|
fields_to_xml(Fields) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[field_to_xml(Field) || Field <- Fields].
|
2009-07-17 22:45:44 +02:00
|
|
|
|
|
|
|
field_to_xml({_, Var, Values}) ->
|
|
|
|
Values_xml = values_to_xml(Values),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"field">>, attrs = [{<<"var">>, Var}],
|
|
|
|
children = Values_xml}.
|
2009-07-17 22:45:44 +02:00
|
|
|
|
|
|
|
values_to_xml(Values) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
lists:map(fun (Value) ->
|
|
|
|
#xmlel{name = <<"value">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Value}]}
|
|
|
|
end,
|
|
|
|
Values).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(extra_domains) ->
|
|
|
|
fun (Hs) -> [iolist_to_binary(H) || H <- Hs] end;
|
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(server_info) ->
|
|
|
|
fun (L) ->
|
|
|
|
lists:map(fun (Opts) ->
|
|
|
|
Mods = proplists:get_value(modules, Opts, all),
|
|
|
|
Name = proplists:get_value(name, Opts, <<>>),
|
|
|
|
URLs = proplists:get_value(urls, Opts, []),
|
|
|
|
{Mods, Name, URLs}
|
|
|
|
end,
|
|
|
|
L)
|
|
|
|
end;
|
|
|
|
mod_opt_type(_) -> [extra_domains, iqdisc, server_info].
|