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>
|
2010-04-14 01:00:14 +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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:47:22 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-01-01 20:54:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_disco).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2003-01-01 20:54:44 +01:00
|
|
|
process_local_iq_items/3,
|
|
|
|
process_local_iq_info/3,
|
2005-09-04 03:58:47 +02:00
|
|
|
get_local_identity/5,
|
|
|
|
get_local_features/5,
|
|
|
|
get_local_services/5,
|
2003-01-01 20:54:44 +01:00
|
|
|
process_sm_iq_items/3,
|
|
|
|
process_sm_iq_info/3,
|
2005-09-04 03:58:47 +02:00
|
|
|
get_sm_identity/5,
|
|
|
|
get_sm_features/5,
|
|
|
|
get_sm_items/5,
|
2009-07-17 22:47:40 +02:00
|
|
|
get_info/5,
|
2005-06-20 05:18:13 +02:00
|
|
|
register_feature/2,
|
|
|
|
unregister_feature/2,
|
2005-07-15 01:12:20 +02:00
|
|
|
register_extra_domain/2,
|
2010-07-22 18:42:18 +02:00
|
|
|
unregister_extra_domain/2,
|
|
|
|
get_vh_services/1]).
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2008-07-22 16:03:11 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-01-01 20:54:44 +01:00
|
|
|
-include("ejabberd.hrl").
|
2011-07-13 17:12:51 +02:00
|
|
|
-include("mod_roster.hrl").
|
2010-07-22 18:42:18 +02:00
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2011-07-11 19:46:26 +02:00
|
|
|
start(Host, Opts) when is_list(Host) ->
|
|
|
|
start(list_to_binary(Host), Opts);
|
|
|
|
start(HostB, Opts) ->
|
2005-09-04 05:31:23 +02:00
|
|
|
|
2004-07-10 00:34:26 +02:00
|
|
|
ejabberd_local:refresh_iq_handlers(),
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_DISCO_ITEMS,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_local_iq_items, IQDisc),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, HostB, ?NS_DISCO_INFO,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_local_iq_info, IQDisc),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_DISCO_ITEMS,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_sm_iq_items, IQDisc),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_DISCO_INFO,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_sm_iq_info, IQDisc),
|
2004-07-10 00:34:26 +02:00
|
|
|
|
|
|
|
catch ets:new(disco_features, [named_table, ordered_set, public]),
|
2010-04-14 00:57:56 +02:00
|
|
|
register_feature(HostB, "iq"),
|
|
|
|
register_feature(HostB, "presence"),
|
|
|
|
register_feature(HostB, "presence-invisible"),
|
2004-07-10 00:34:26 +02:00
|
|
|
|
2003-10-09 20:09:05 +02:00
|
|
|
catch ets:new(disco_extra_domains, [named_table, ordered_set, public]),
|
|
|
|
ExtraDomains = gen_mod:get_opt(extra_domains, Opts, []),
|
2011-07-11 19:46:26 +02:00
|
|
|
lists:foreach(fun(Domain) -> register_extra_domain(HostB, Domain) end,
|
2005-07-15 01:12:20 +02:00
|
|
|
ExtraDomains),
|
2004-09-17 21:52:59 +02:00
|
|
|
catch ets:new(disco_sm_features, [named_table, ordered_set, public]),
|
|
|
|
catch ets:new(disco_sm_nodes, [named_table, ordered_set, public]),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(disco_local_items, HostB, ?MODULE, get_local_services, 100),
|
|
|
|
ejabberd_hooks:add(disco_local_features, HostB, ?MODULE, get_local_features, 100),
|
|
|
|
ejabberd_hooks:add(disco_local_identity, HostB, ?MODULE, get_local_identity, 100),
|
|
|
|
ejabberd_hooks:add(disco_sm_items, HostB, ?MODULE, get_sm_items, 100),
|
|
|
|
ejabberd_hooks:add(disco_sm_features, HostB, ?MODULE, get_sm_features, 100),
|
|
|
|
ejabberd_hooks:add(disco_sm_identity, HostB, ?MODULE, get_sm_identity, 100),
|
2009-07-17 22:47:40 +02:00
|
|
|
ejabberd_hooks:add(disco_info, HostB, ?MODULE, get_info, 100),
|
2003-01-01 20:54:44 +01:00
|
|
|
ok.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(disco_sm_identity, HostB, ?MODULE, get_sm_identity, 100),
|
|
|
|
ejabberd_hooks:delete(disco_sm_features, HostB, ?MODULE, get_sm_features, 100),
|
|
|
|
ejabberd_hooks:delete(disco_sm_items, HostB, ?MODULE, get_sm_items, 100),
|
|
|
|
ejabberd_hooks:delete(disco_local_identity, HostB, ?MODULE, get_local_identity, 100),
|
|
|
|
ejabberd_hooks:delete(disco_local_features, HostB, ?MODULE, get_local_features, 100),
|
|
|
|
ejabberd_hooks:delete(disco_local_items, HostB, ?MODULE, get_local_services, 100),
|
2009-07-17 22:47:40 +02:00
|
|
|
ejabberd_hooks:delete(disco_info, HostB, ?MODULE, get_info, 100),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, HostB, ?NS_DISCO_ITEMS),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, HostB, ?NS_DISCO_INFO),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_DISCO_ITEMS),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_DISCO_INFO),
|
2010-04-14 00:57:56 +02:00
|
|
|
catch ets:match_delete(disco_features, {{'_', HostB}}),
|
|
|
|
catch ets:match_delete(disco_extra_domains, {{'_', HostB}}),
|
2004-07-10 00:34:26 +02:00
|
|
|
ok.
|
2003-01-29 18:12:23 +01:00
|
|
|
|
|
|
|
|
2011-07-11 19:46:26 +02:00
|
|
|
register_feature(HostB, Feature) ->
|
2003-01-01 20:54:44 +01:00
|
|
|
catch ets:new(disco_features, [named_table, ordered_set, public]),
|
2010-04-14 00:57:56 +02:00
|
|
|
ets:insert(disco_features, {{Feature, HostB}}).
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2010-04-14 00:57:56 +02:00
|
|
|
unregister_feature(HostB, Feature) ->
|
2003-01-29 18:12:23 +01:00
|
|
|
catch ets:new(disco_features, [named_table, ordered_set, public]),
|
2010-04-14 00:57:56 +02:00
|
|
|
ets:delete(disco_features, {Feature, HostB}).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2011-07-11 19:46:26 +02:00
|
|
|
register_extra_domain(HostB, Domain) ->
|
2003-10-09 20:09:05 +02:00
|
|
|
catch ets:new(disco_extra_domains, [named_table, ordered_set, public]),
|
2010-04-14 00:57:56 +02:00
|
|
|
ets:insert(disco_extra_domains, {{Domain, HostB}}).
|
2003-10-09 20:09:05 +02:00
|
|
|
|
2011-07-11 19:46:26 +02:00
|
|
|
unregister_extra_domain(HostB, Domain) ->
|
2003-10-09 20:09:05 +02:00
|
|
|
catch ets:new(disco_extra_domains, [named_table, ordered_set, public]),
|
2010-04-14 00:57:56 +02:00
|
|
|
ets:delete(disco_extra_domains, {Domain, HostB}).
|
2003-10-09 20:09:05 +02:00
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_local_iq_items(From, To, #iq{type = get, payload = SubEl,
|
|
|
|
lang = Lang} = IQ_Rec) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Node = exmpp_xml:get_attribute_as_binary(SubEl, <<"node">>, <<>>),
|
2008-08-14 15:36:11 +02:00
|
|
|
|
2010-08-11 23:31:22 +02:00
|
|
|
Host = exmpp_jid:prep_domain(To),
|
|
|
|
case find_items(disco_local_items, Host, From, To, Node, Lang) of
|
2008-08-14 15:36:11 +02:00
|
|
|
{result, Items} ->
|
|
|
|
ANode = case Node of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<>> -> [];
|
2010-11-29 20:44:31 +01:00
|
|
|
_ -> [?XMLATTR(<<"node">>, Node)]
|
2008-08-14 15:36:11 +02:00
|
|
|
end,
|
|
|
|
Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query',
|
|
|
|
attrs = ANode, children = Items},
|
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
|
|
|
end;
|
|
|
|
process_local_iq_items(_From, _To, #iq{type = set} = IQ_Rec) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed').
|
2003-01-01 20:54:44 +01:00
|
|
|
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_local_iq_info(From, To, #iq{type = get, payload = SubEl,
|
|
|
|
lang = Lang} = IQ_Rec) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Node = exmpp_xml:get_attribute_as_binary(SubEl, <<"node">>, <<>>),
|
2009-07-17 22:47:40 +02:00
|
|
|
HostB = exmpp_jid:prep_domain(To),
|
2008-08-14 15:36:11 +02:00
|
|
|
Identity = ejabberd_hooks:run_fold(disco_local_identity,
|
2009-07-17 22:47:40 +02:00
|
|
|
HostB,
|
2008-08-14 15:36:11 +02:00
|
|
|
[],
|
2008-09-29 11:42:46 +02:00
|
|
|
[From, To, Node, Lang]),
|
2009-07-17 22:47:40 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(To),
|
|
|
|
Info = ejabberd_hooks:run_fold(disco_info, HostB, [],
|
|
|
|
[Host, ?MODULE, Node, Lang]),
|
2010-08-11 23:31:22 +02:00
|
|
|
case find_items(disco_local_features, HostB, From, To, Node, Lang) of
|
2008-08-14 15:36:11 +02:00
|
|
|
{result, Features} ->
|
|
|
|
ANode = case Node of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<>> -> [];
|
2010-11-29 20:44:31 +01:00
|
|
|
_ -> [?XMLATTR(<<"node">>, Node)]
|
2008-08-14 15:36:11 +02:00
|
|
|
end,
|
|
|
|
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
|
2010-05-10 08:00:30 +02:00
|
|
|
attrs = ANode,
|
|
|
|
children = Identity ++ Info ++ features_to_xml(Features)},
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
|
|
|
end;
|
|
|
|
process_local_iq_info(_From, _To, #iq{type = set} = IQ_Rec) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed').
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_identity(Acc, _From, _To, <<>>, _Lang) ->
|
2008-07-22 16:03:11 +02:00
|
|
|
Acc ++ [#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs = [
|
2010-11-29 20:44:31 +01:00
|
|
|
?XMLATTR(<<"category">>, <<"server">>),
|
|
|
|
?XMLATTR(<<"type">>, <<"im">>),
|
|
|
|
?XMLATTR(<<"name">>, <<"ejabberd">>)
|
2008-07-22 16:03:11 +02:00
|
|
|
]}];
|
2007-06-22 16:15:43 +02:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_local_identity(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
get_local_features({error, _Error} = Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_features(Acc, _From, To, <<>>, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Feats = case Acc of
|
|
|
|
{result, Features} -> Features;
|
|
|
|
empty -> []
|
|
|
|
end,
|
2010-04-14 00:57:56 +02:00
|
|
|
HostB = exmpp_jid:prep_domain(To),
|
2010-07-22 18:42:18 +02:00
|
|
|
NHostB = ejabberd:normalize_host(HostB),
|
2005-10-30 21:47:26 +01:00
|
|
|
{result,
|
2010-07-22 18:42:18 +02:00
|
|
|
ets:select(disco_features, [{{{'_', NHostB}}, [],
|
|
|
|
['$_']}]) ++
|
|
|
|
ets:select(disco_features, [{{{'_', global}}, [],
|
|
|
|
['$_']}]) ++
|
|
|
|
Feats};
|
2005-09-04 03:58:47 +02:00
|
|
|
|
|
|
|
get_local_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
case Acc of
|
|
|
|
{result, _Features} ->
|
|
|
|
Acc;
|
|
|
|
empty ->
|
2008-07-22 16:03:11 +02:00
|
|
|
{error, 'item-not-found'}
|
2005-09-04 03:58:47 +02:00
|
|
|
end.
|
|
|
|
|
2010-05-10 08:00:30 +02:00
|
|
|
features_to_xml(FeatureList) ->
|
|
|
|
%% Avoid duplicating features
|
|
|
|
[#xmlel{ns = ?NS_DISCO_INFO, name = 'feature',
|
2010-11-29 20:44:31 +01:00
|
|
|
attrs = [?XMLATTR(<<"var">>, Feat)]} ||
|
2010-05-10 08:00:30 +02:00
|
|
|
Feat <- lists:usort(
|
|
|
|
lists:map(
|
|
|
|
fun({{Feature, _Host}}) ->
|
|
|
|
feature_to_xml(Feature);
|
|
|
|
(Feature) ->
|
|
|
|
feature_to_xml(Feature)
|
|
|
|
end, FeatureList))].
|
2009-01-08 15:54:00 +01:00
|
|
|
|
|
|
|
feature_to_xml(Feature) when is_list(Feature) ->
|
|
|
|
feature_to_xml(list_to_binary(Feature));
|
2008-08-14 15:36:11 +02:00
|
|
|
feature_to_xml(Feature) when is_atom(Feature) ->
|
2010-05-10 08:00:30 +02:00
|
|
|
feature_to_xml(atom_to_list(Feature));
|
|
|
|
feature_to_xml(Feature) when is_binary(Feature) ->
|
|
|
|
Feature.
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2003-10-09 20:09:05 +02:00
|
|
|
domain_to_xml({Domain}) ->
|
2009-01-08 15:54:00 +01:00
|
|
|
domain_to_xml(Domain);
|
|
|
|
domain_to_xml(Domain) when is_binary(Domain)->
|
2008-07-22 16:03:11 +02:00
|
|
|
#xmlel{ns = ?NS_DISCO_ITEMS, name = 'item', attrs = [
|
2010-11-29 20:44:31 +01:00
|
|
|
?XMLATTR(<<"jid">>, Domain)
|
2008-07-22 16:03:11 +02:00
|
|
|
]};
|
2009-01-08 15:54:00 +01:00
|
|
|
domain_to_xml(Domain) when is_list(Domain) ->
|
|
|
|
domain_to_xml(list_to_binary(Domain)).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_local_services({error, _Error} = Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc;
|
2003-01-22 21:40:40 +01:00
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_local_services(Acc, _From, To, <<>>, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Items = case Acc of
|
|
|
|
{result, Its} -> Its;
|
|
|
|
empty -> []
|
|
|
|
end,
|
2010-07-22 18:42:18 +02:00
|
|
|
Host = exmpp_jid:prep_domain_as_list(To),
|
2011-07-11 19:46:26 +02:00
|
|
|
NHostB = list_to_binary(ejabberd:normalize_host(Host)),
|
2003-02-04 21:45:23 +01:00
|
|
|
{result,
|
2005-09-04 03:58:47 +02:00
|
|
|
lists:usort(
|
|
|
|
lists:map(fun domain_to_xml/1,
|
|
|
|
get_vh_services(Host) ++
|
2005-11-03 20:58:58 +01:00
|
|
|
ets:select(disco_extra_domains,
|
2010-07-22 18:42:18 +02:00
|
|
|
ets:fun2ms(fun ({{Service, H}})
|
2011-07-11 19:46:26 +02:00
|
|
|
when H =:= NHostB;
|
2010-07-22 18:42:18 +02:00
|
|
|
H =:= global ->
|
|
|
|
Service
|
|
|
|
end)))
|
2005-09-04 03:58:47 +02:00
|
|
|
) ++ Items};
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_local_services({result, _} = Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc;
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_local_services(empty, _From, _To, _Node, _Lang) ->
|
2008-07-22 16:03:11 +02:00
|
|
|
{error, 'item-not-found'}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2010-07-22 18:42:18 +02:00
|
|
|
%% Note: only first-order subdomains are returned.
|
|
|
|
%% For example, if Host = "a",
|
|
|
|
%% and Routes = ["muc.a", "vjud.a", "private.muc.a", "muc.b"]
|
|
|
|
%% only returns ["muc.a", "vjud.a"]
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_services(Host) ->
|
2010-07-22 18:42:18 +02:00
|
|
|
Routes = ejabberd_router:dirty_get_all_routes(),
|
|
|
|
HostTokenized = string:tokens(Host, "."),
|
|
|
|
VhServices =
|
|
|
|
lists:filter(fun(H) ->
|
|
|
|
case string:tokens(H, ".") of
|
|
|
|
[_ | HostTokenized] ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end, Routes),
|
|
|
|
GlobalServices = ejabberd_global_router:expand_routes(Host),
|
|
|
|
VhServices ++ GlobalServices.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_sm_iq_items(From, To, #iq{type = get, payload = SubEl,
|
|
|
|
lang = Lang} = IQ_Rec) ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Node = exmpp_xml:get_attribute_as_binary(SubEl, <<"node">>, <<>>),
|
2010-08-11 23:31:22 +02:00
|
|
|
Host = exmpp_jid:prep_domain(To),
|
|
|
|
case find_items(disco_sm_items, Host, From, To, Node, Lang) of
|
2008-08-14 15:36:11 +02:00
|
|
|
{result, Items} ->
|
|
|
|
ANode = case Node of
|
2009-01-08 15:54:00 +01:00
|
|
|
<<>> -> [];
|
2010-11-29 20:44:31 +01:00
|
|
|
_ -> [?XMLATTR(<<"node">>, Node)]
|
2008-08-14 15:36:11 +02:00
|
|
|
end,
|
2010-04-29 05:38:25 +02:00
|
|
|
AItems = case Node of
|
|
|
|
<<>> ->
|
|
|
|
case is_presence_subscribed(From, To) of
|
|
|
|
true -> Items;
|
|
|
|
false -> []
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2008-08-14 15:36:11 +02:00
|
|
|
Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query',
|
2010-04-29 05:38:25 +02:00
|
|
|
attrs = ANode, children = AItems},
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
|
|
|
end;
|
2009-06-02 20:09:01 +02:00
|
|
|
process_sm_iq_items(_From, _To, #iq{type = set, payload = _SubEl} = IQ_Rec) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed').
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2010-04-29 05:38:25 +02:00
|
|
|
is_presence_subscribed(From, To) ->
|
|
|
|
User = exmpp_jid:prep_node(From),
|
|
|
|
Server = exmpp_jid:prep_domain(From),
|
|
|
|
LUser = exmpp_jid:prep_node(To),
|
|
|
|
LServer = exmpp_jid:prep_domain(To),
|
2011-07-13 17:12:51 +02:00
|
|
|
lists:any(fun(#roster{jid = {TUser, TServer, _}, subscription = S}) ->
|
2010-04-29 05:38:25 +02:00
|
|
|
if
|
|
|
|
LUser == TUser, LServer == TServer, S/=none ->
|
|
|
|
true;
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
ejabberd_hooks:run_fold(roster_get, Server, [], [{User, Server}]))
|
|
|
|
orelse User == LUser andalso Server == LServer.
|
|
|
|
|
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_sm_items({error, _Error} = Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc;
|
|
|
|
|
2009-01-08 15:54:00 +01:00
|
|
|
get_sm_items(Acc, From, To, <<>>, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Items = case Acc of
|
|
|
|
{result, Its} -> Its;
|
|
|
|
empty -> []
|
|
|
|
end,
|
2010-04-29 05:38:25 +02:00
|
|
|
Items1 = case is_presence_subscribed(From, To) of
|
|
|
|
true ->
|
|
|
|
get_user_resources(To);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2005-09-04 03:58:47 +02:00
|
|
|
{result, Items ++ Items1};
|
|
|
|
|
|
|
|
get_sm_items({result, _} = Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc;
|
|
|
|
|
|
|
|
get_sm_items(empty, From, To, _Node, _Lang) ->
|
2009-06-01 18:40:51 +02:00
|
|
|
LFrom = exmpp_jid:prep_node_as_list(From),
|
2009-06-01 18:38:28 +02:00
|
|
|
LSFrom = exmpp_jid:prep_domain_as_list(From),
|
2009-06-01 18:40:51 +02:00
|
|
|
LTo = exmpp_jid:prep_node_as_list(To),
|
2009-06-01 18:38:28 +02:00
|
|
|
LSTo = exmpp_jid:prep_domain_as_list(To),
|
2005-09-04 03:58:47 +02:00
|
|
|
case {LFrom, LSFrom} of
|
|
|
|
{LTo, LSTo} ->
|
2008-07-22 16:03:11 +02:00
|
|
|
{error, 'item-not-found'};
|
2005-09-04 03:58:47 +02:00
|
|
|
_ ->
|
2008-07-22 16:03:11 +02:00
|
|
|
{error, 'not-allowed'}
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_sm_iq_info(From, To, #iq{type = get, payload = SubEl,
|
|
|
|
lang = Lang} = IQ_Rec) ->
|
2010-04-29 05:38:25 +02:00
|
|
|
case is_presence_subscribed(From, To) of
|
|
|
|
true ->
|
2010-11-29 20:44:31 +01:00
|
|
|
Node = exmpp_xml:get_attribute_as_binary(SubEl, <<"node">>, <<>>),
|
2010-04-29 05:38:25 +02:00
|
|
|
Identity = ejabberd_hooks:run_fold(disco_sm_identity,
|
|
|
|
exmpp_jid:prep_domain(To),
|
|
|
|
[],
|
|
|
|
[From, To, Node, Lang]),
|
2010-08-11 23:31:22 +02:00
|
|
|
Host = exmpp_jid:prep_domain(To),
|
|
|
|
case find_items(disco_sm_features, Host, From, To, Node, Lang) of
|
2010-04-29 05:38:25 +02:00
|
|
|
{result, Features} ->
|
|
|
|
ANode = case Node of
|
|
|
|
<<>> -> [];
|
2010-11-29 20:44:31 +01:00
|
|
|
_ -> [?XMLATTR(<<"node">>, Node)]
|
2010-04-29 05:38:25 +02:00
|
|
|
end,
|
|
|
|
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
|
2010-05-10 08:00:30 +02:00
|
|
|
attrs = ANode,
|
|
|
|
children = Identity ++ features_to_xml(Features)},
|
2010-04-29 05:38:25 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Result);
|
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'service-unavailable')
|
2008-08-14 15:36:11 +02:00
|
|
|
end;
|
2010-04-29 05:38:25 +02:00
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_sm_iq_info(_From, _To, #iq{type = set} = IQ_Rec) ->
|
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed').
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2010-04-29 05:38:25 +02:00
|
|
|
get_sm_identity(Acc, _From, To, _Node, _Lang) ->
|
|
|
|
LUser = exmpp_jid:prep_node_as_list(To),
|
|
|
|
LServer = exmpp_jid:prep_domain_as_list(To),
|
|
|
|
Acc ++ case ejabberd_auth:is_user_exists(LUser, LServer) of
|
|
|
|
true ->
|
|
|
|
[{xmlelement, "identity", [{"category", "account"},
|
|
|
|
{"type", "registered"}], []}];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end.
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_sm_features(empty, From, To, _Node, _Lang) ->
|
2010-04-29 05:38:25 +02:00
|
|
|
?DEBUG("DISCO: ~p~n", [empty]),
|
2009-06-01 18:40:51 +02:00
|
|
|
LFrom = exmpp_jid:prep_node_as_list(From),
|
2009-06-01 18:38:28 +02:00
|
|
|
LSFrom = exmpp_jid:prep_domain_as_list(From),
|
2009-06-01 18:40:51 +02:00
|
|
|
LTo = exmpp_jid:prep_node_as_list(To),
|
2009-06-01 18:38:28 +02:00
|
|
|
LSTo = exmpp_jid:prep_domain_as_list(To),
|
2005-09-04 03:58:47 +02:00
|
|
|
case {LFrom, LSFrom} of
|
|
|
|
{LTo, LSTo} ->
|
2008-07-22 16:03:11 +02:00
|
|
|
{error, 'item-not-found'};
|
2004-09-17 21:52:59 +02:00
|
|
|
_ ->
|
2008-07-22 16:03:11 +02:00
|
|
|
{error, 'not-allowed'}
|
2005-09-04 03:58:47 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
get_sm_features(Acc, _From, _To, _Node, _Lang) ->
|
2010-04-29 05:38:25 +02:00
|
|
|
?DEBUG("DISCO: ~p~n", [Acc]),
|
2005-09-04 03:58:47 +02:00
|
|
|
Acc.
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
get_user_resources(JID) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
Rs = ejabberd_sm:get_user_resources(exmpp_jid:prep_node(JID),
|
2009-06-01 18:37:15 +02:00
|
|
|
exmpp_jid:prep_domain(JID)),
|
2003-01-01 20:54:44 +01:00
|
|
|
lists:map(fun(R) ->
|
2008-07-22 16:03:11 +02:00
|
|
|
#xmlel{ns = ?NS_DISCO_ITEMS, name = 'item', attrs = [
|
2010-11-29 20:44:31 +01:00
|
|
|
?XMLATTR(<<"jid">>,
|
2009-06-01 18:59:08 +02:00
|
|
|
exmpp_jid:to_binary(exmpp_jid:full(JID, R))),
|
2010-11-29 20:44:31 +01:00
|
|
|
?XMLATTR(<<"name">>, exmpp_jid:prep_node(JID))
|
2008-07-22 16:03:11 +02:00
|
|
|
]}
|
2003-01-01 20:54:44 +01:00
|
|
|
end, lists:sort(Rs)).
|
2009-07-17 22:47:40 +02:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
%%% Support for: XEP-0157 Contact Addresses for XMPP Services
|
|
|
|
|
2010-05-10 08:00:30 +02:00
|
|
|
get_info(Acc, Host, Mod, Node, _Lang) when Node == <<>> ->
|
|
|
|
Module = case Mod of
|
|
|
|
undefined -> ?MODULE;
|
|
|
|
_ -> Mod
|
|
|
|
end,
|
2009-07-17 22:47:40 +02:00
|
|
|
Serverinfo_fields = get_fields_xml(Host, Module),
|
|
|
|
CData1 = #xmlcdata{cdata = list_to_binary(?NS_SERVERINFO_s)},
|
|
|
|
Value1 = #xmlel{name = 'value', children = [CData1]},
|
|
|
|
Field1 = #xmlel{name = 'field',
|
2010-11-29 20:44:31 +01:00
|
|
|
attrs = [?XMLATTR(<<"type">>, <<"hidden">>),
|
|
|
|
?XMLATTR(<<"var">>, <<"FORM_TYPE">>)],
|
2009-07-17 22:47:40 +02:00
|
|
|
children = [Value1]
|
|
|
|
},
|
|
|
|
X = #xmlel{name = 'x',
|
|
|
|
ns = ?NS_DATA_FORMS,
|
2010-11-29 20:44:31 +01:00
|
|
|
attrs = [?XMLATTR(<<"type">>, <<"result">>)],
|
2009-07-17 22:47:40 +02:00
|
|
|
children = [Field1 | Serverinfo_fields]
|
|
|
|
},
|
|
|
|
[X | Acc];
|
|
|
|
|
2010-05-10 08:00:30 +02:00
|
|
|
get_info(Acc, _, _, _Node, _) ->
|
|
|
|
Acc.
|
2009-07-17 22:47:40 +02:00
|
|
|
|
|
|
|
get_fields_xml(Host, Module) ->
|
|
|
|
Fields = gen_mod:get_module_opt(Host, ?MODULE, server_info, []),
|
|
|
|
|
|
|
|
%% filter, and get only the ones allowed for this module
|
|
|
|
Fields_good = lists:filter(
|
|
|
|
fun({Modules, _, _}) ->
|
|
|
|
case Modules of
|
|
|
|
all -> true;
|
|
|
|
Modules -> lists:member(Module, Modules)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
Fields),
|
|
|
|
|
|
|
|
fields_to_xml(Fields_good).
|
|
|
|
|
|
|
|
fields_to_xml(Fields) ->
|
|
|
|
[ field_to_xml(Field) || Field <- Fields].
|
|
|
|
|
|
|
|
field_to_xml({_, Var, Values}) ->
|
|
|
|
Values_xml = values_to_xml(Values),
|
|
|
|
#xmlel{name = 'field',
|
2010-11-29 20:44:31 +01:00
|
|
|
attrs = [?XMLATTR(<<"var">>, list_to_binary(Var))],
|
2009-07-17 22:47:40 +02:00
|
|
|
children = Values_xml
|
|
|
|
}.
|
|
|
|
|
|
|
|
values_to_xml(Values) ->
|
|
|
|
lists:map(
|
|
|
|
fun(Value) ->
|
|
|
|
CData= #xmlcdata{cdata = list_to_binary(Value)},
|
|
|
|
#xmlel{name = 'value', children = [CData]}
|
|
|
|
end,
|
|
|
|
Values
|
|
|
|
).
|
2010-08-11 23:31:22 +02:00
|
|
|
|
|
|
|
%%%%%% private functions
|
|
|
|
find_items(Hook, global, From, To, Node, Lang) ->
|
|
|
|
ejabberd_hooks:run_fold(Hook, global, empty, [From, To, Node, Lang]);
|
|
|
|
find_items(Hook, Host, From, To, Node, Lang) ->
|
|
|
|
case ejabberd_hooks:run_fold(Hook, Host, empty, [From, To, Node, Lang]) of
|
|
|
|
empty ->
|
|
|
|
find_items(Hook, global, From, To, Node, Lang);
|
|
|
|
Items -> Items
|
|
|
|
end.
|