2003-01-01 20:54:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_disco.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2003-01-28 20:45:13 +01:00
|
|
|
%%% Purpose : Service Discovery (JEP-0030) support
|
2003-01-02 22:01:12 +01:00
|
|
|
%%% Created : 1 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
2003-01-01 20:54:44 +01:00
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_disco).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
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,
|
2005-09-04 05:31:23 +02:00
|
|
|
get_publish_items/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,
|
2005-09-04 03:58:47 +02:00
|
|
|
unregister_extra_domain/2]).
|
2003-01-01 20:54:44 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2005-09-04 05:31:23 +02:00
|
|
|
-record(disco_publish, {owner_node, jid, name, node}).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2005-09-04 05:31:23 +02:00
|
|
|
mnesia:create_table(disco_publish,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, disco_publish)},
|
|
|
|
{type, bag}]),
|
|
|
|
mnesia:add_table_index(disco_publish, owner_node),
|
|
|
|
|
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),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_DISCO_ITEMS,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_local_iq_items, IQDisc),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_DISCO_INFO,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_local_iq_info, IQDisc),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_DISCO_ITEMS,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_sm_iq_items, IQDisc),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?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]),
|
2005-06-20 05:18:13 +02:00
|
|
|
register_feature(Host, "iq"),
|
|
|
|
register_feature(Host, "presence"),
|
|
|
|
register_feature(Host, "presence-invisible"),
|
2005-09-04 05:31:23 +02:00
|
|
|
register_feature(Host, "http://jabber.org/protocol/disco#publish"),
|
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, []),
|
2005-07-15 01:12:20 +02:00
|
|
|
lists:foreach(fun(Domain) -> register_extra_domain(Host, Domain) end,
|
|
|
|
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]),
|
2005-09-04 03:58:47 +02:00
|
|
|
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),
|
2005-09-04 05:31:23 +02:00
|
|
|
ejabberd_hooks:add(disco_sm_items, Host, ?MODULE, get_publish_items, 75),
|
2003-01-01 20:54:44 +01:00
|
|
|
ok.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2005-09-04 05:31:23 +02:00
|
|
|
ejabberd_hooks:delete(disco_sm_items, Host, ?MODULE, get_publish_items, 75),
|
2005-09-04 03:58:47 +02: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),
|
2005-06-20 05:18:13 +02:00
|
|
|
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}}),
|
|
|
|
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) ->
|
2003-01-01 20:54:44 +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) ->
|
2003-01-29 18:12:23 +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) ->
|
2003-10-09 20:09:05 +02: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) ->
|
2003-10-09 20:09:05 +02: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
|
|
|
|
2004-02-26 23:00:04 +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
|
|
|
|
set ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
2003-01-01 20:54:44 +01:00
|
|
|
get ->
|
2006-01-19 03:17:31 +01:00
|
|
|
Node = xml:get_tag_attr_s("node", SubEl),
|
2005-06-20 05:18:13 +02:00
|
|
|
Host = To#jid.lserver,
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
case ejabberd_hooks:run_fold(disco_local_items,
|
|
|
|
Host,
|
|
|
|
empty,
|
|
|
|
[From, To, Node, Lang]) of
|
|
|
|
{result, Items} ->
|
|
|
|
ANode = case Node of
|
2006-01-19 03:17:31 +01:00
|
|
|
"" -> [];
|
|
|
|
_ -> [{"node", Node}]
|
2005-09-04 03:58:47 +02:00
|
|
|
end,
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
2005-09-04 03:58:47 +02:00
|
|
|
[{"xmlns", ?NS_DISCO_ITEMS} | ANode],
|
|
|
|
Items
|
2003-12-17 21:13:21 +01:00
|
|
|
}]};
|
2005-09-04 03:58:47 +02:00
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
2003-01-01 20:54:44 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
process_local_iq_info(From, To, #iq{type = Type, lang = Lang,
|
2003-12-17 21:13:21 +01:00
|
|
|
sub_el = SubEl} = IQ) ->
|
2003-01-01 20:54:44 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
2003-01-01 20:54:44 +01:00
|
|
|
get ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Host = To#jid.lserver,
|
2006-01-19 03:17:31 +01:00
|
|
|
Node = xml:get_tag_attr_s("node", SubEl),
|
2005-09-04 03:58:47 +02:00
|
|
|
Identity = ejabberd_hooks:run_fold(disco_local_identity,
|
|
|
|
Host,
|
|
|
|
[],
|
|
|
|
[From, To, Node, Lang]),
|
|
|
|
case ejabberd_hooks:run_fold(disco_local_features,
|
|
|
|
Host,
|
|
|
|
empty,
|
|
|
|
[From, To, Node, Lang]) of
|
|
|
|
{result, Features} ->
|
|
|
|
ANode = case Node of
|
2006-01-19 03:17:31 +01:00
|
|
|
"" -> [];
|
|
|
|
_ -> [{"node", Node}]
|
2005-09-04 03:58:47 +02:00
|
|
|
end,
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
2005-09-04 03:58:47 +02:00
|
|
|
[{"xmlns", ?NS_DISCO_INFO} | ANode],
|
|
|
|
Identity ++
|
|
|
|
lists:map(fun feature_to_xml/1, Features)
|
|
|
|
}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
2003-01-01 20:54:44 +01:00
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2007-06-22 16:15:43 +02:00
|
|
|
get_local_identity(Acc, _From, _To, [], _Lang) ->
|
|
|
|
Acc ++ [{xmlelement, "identity",
|
|
|
|
[{"category", "server"},
|
|
|
|
{"type", "im"},
|
|
|
|
{"name", "ejabberd"}], []}];
|
|
|
|
|
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;
|
|
|
|
|
2005-10-30 21:47:26 +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,
|
2005-10-30 21:47:26 +01:00
|
|
|
Host = To#jid.lserver,
|
|
|
|
{result,
|
|
|
|
ets:select(disco_features, [{{{'_', Host}}, [], ['$_']}]) ++ Feats};
|
2005-09-04 03:58:47 +02:00
|
|
|
|
|
|
|
get_local_features(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
case Acc of
|
|
|
|
{result, _Features} ->
|
|
|
|
Acc;
|
|
|
|
empty ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND}
|
|
|
|
end.
|
|
|
|
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
feature_to_xml({{Feature, _Host}}) ->
|
|
|
|
feature_to_xml(Feature);
|
|
|
|
feature_to_xml(Feature) when is_list(Feature) ->
|
2003-01-01 20:54:44 +01:00
|
|
|
{xmlelement, "feature", [{"var", Feature}], []}.
|
|
|
|
|
2003-10-09 20:09:05 +02:00
|
|
|
domain_to_xml({Domain}) ->
|
|
|
|
{xmlelement, "item", [{"jid", Domain}], []};
|
2003-01-02 22:01:12 +01:00
|
|
|
domain_to_xml(Domain) ->
|
|
|
|
{xmlelement, "item", [{"jid", Domain}], []}.
|
|
|
|
|
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
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_local_services(Acc, _From, To, [], _Lang) ->
|
|
|
|
Items = case Acc of
|
|
|
|
{result, Its} -> Its;
|
|
|
|
empty -> []
|
|
|
|
end,
|
|
|
|
Host = To#jid.lserver,
|
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,
|
|
|
|
[{{{'$1', Host}}, [], ['$1']}]))
|
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) ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND}.
|
2003-01-18 20:42:48 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_vh_services(Host) ->
|
2005-04-20 00:19:22 +02:00
|
|
|
Hosts = lists:sort(fun(H1, H2) -> length(H1) >= length(H2) end, ?MYHOSTS),
|
2005-04-17 20:08:34 +02:00
|
|
|
lists:filter(fun(H) ->
|
2005-04-20 00:19:22 +02:00
|
|
|
case lists:dropwhile(
|
|
|
|
fun(VH) ->
|
|
|
|
not lists:suffix("." ++ VH, H)
|
|
|
|
end, Hosts) of
|
|
|
|
[] ->
|
|
|
|
false;
|
|
|
|
[VH | _] ->
|
|
|
|
VH == Host
|
|
|
|
end
|
2005-04-17 20:08:34 +02: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
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
process_sm_iq_items(From, To, #iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
2006-01-19 03:17:31 +01:00
|
|
|
#jid{luser = LTo, lserver = ToServer} = To,
|
2005-09-04 05:31:23 +02:00
|
|
|
#jid{luser = LFrom, lserver = LServer} = From,
|
|
|
|
Self = (LTo == LFrom) andalso (ToServer == LServer),
|
|
|
|
Node = xml:get_tag_attr_s("node", SubEl),
|
|
|
|
if
|
|
|
|
Self, Node /= [] ->
|
|
|
|
%% Here, we treat disco publish attempts to your own JID.
|
|
|
|
{xmlelement, _, _, Items} = SubEl,
|
|
|
|
case process_disco_publish({LFrom, LServer}, Node, Items) of
|
|
|
|
ok ->
|
|
|
|
IQ#iq{type = result, sub_el = []};
|
|
|
|
{error, Err} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Err]}
|
|
|
|
end;
|
|
|
|
|
|
|
|
true ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]}
|
|
|
|
end;
|
2005-09-04 03:58:47 +02:00
|
|
|
get ->
|
|
|
|
Host = To#jid.lserver,
|
2006-01-19 03:17:31 +01:00
|
|
|
Node = xml:get_tag_attr_s("node", SubEl),
|
2005-09-04 03:58:47 +02:00
|
|
|
case ejabberd_hooks:run_fold(disco_sm_items,
|
|
|
|
Host,
|
|
|
|
empty,
|
|
|
|
[From, To, Node, Lang]) of
|
|
|
|
{result, Items} ->
|
|
|
|
ANode = case Node of
|
2006-01-19 03:17:31 +01:00
|
|
|
"" -> [];
|
|
|
|
_ -> [{"node", Node}]
|
2005-09-04 03:58:47 +02:00
|
|
|
end,
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_DISCO_ITEMS} | ANode],
|
|
|
|
Items
|
|
|
|
}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
2003-02-07 20:31:00 +01:00
|
|
|
end
|
2003-01-01 20:54:44 +01:00
|
|
|
end.
|
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_sm_items({error, _Error} = Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc;
|
|
|
|
|
|
|
|
get_sm_items(Acc,
|
2005-09-04 05:31:23 +02:00
|
|
|
#jid{luser = LFrom, lserver = LSFrom},
|
2005-09-04 03:58:47 +02:00
|
|
|
#jid{user = User, server = Server, luser = LTo, lserver = LSTo} = _To,
|
|
|
|
[], _Lang) ->
|
|
|
|
Items = case Acc of
|
|
|
|
{result, Its} -> Its;
|
|
|
|
empty -> []
|
|
|
|
end,
|
|
|
|
Items1 = case {LFrom, LSFrom} of
|
|
|
|
{LTo, LSTo} -> get_user_resources(User, Server);
|
|
|
|
_ -> []
|
|
|
|
end,
|
|
|
|
{result, Items ++ Items1};
|
|
|
|
|
|
|
|
get_sm_items({result, _} = Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc;
|
|
|
|
|
|
|
|
get_sm_items(empty, From, To, _Node, _Lang) ->
|
|
|
|
#jid{luser = LFrom, lserver = LSFrom} = From,
|
|
|
|
#jid{luser = LTo, lserver = LSTo} = To,
|
|
|
|
case {LFrom, LSFrom} of
|
|
|
|
{LTo, LSTo} ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND};
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_NOT_ALLOWED}
|
2003-01-18 20:42:48 +01:00
|
|
|
end.
|
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
process_sm_iq_info(From, To, #iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
|
2005-09-04 03:58:47 +02:00
|
|
|
get ->
|
|
|
|
Host = To#jid.lserver,
|
2006-01-19 03:17:31 +01:00
|
|
|
Node = xml:get_tag_attr_s("node", SubEl),
|
2005-09-04 03:58:47 +02:00
|
|
|
Identity = ejabberd_hooks:run_fold(disco_sm_identity,
|
|
|
|
Host,
|
|
|
|
[],
|
|
|
|
[From, To, Node, Lang]),
|
|
|
|
case ejabberd_hooks:run_fold(disco_sm_features,
|
|
|
|
Host,
|
|
|
|
empty,
|
|
|
|
[From, To, Node, Lang]) of
|
|
|
|
{result, Features} ->
|
|
|
|
ANode = case Node of
|
2006-01-19 03:17:31 +01:00
|
|
|
"" -> [];
|
|
|
|
_ -> [{"node", Node}]
|
2005-09-04 03:58:47 +02:00
|
|
|
end,
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, "query",
|
|
|
|
[{"xmlns", ?NS_DISCO_INFO} | ANode],
|
|
|
|
Identity ++
|
|
|
|
lists:map(fun feature_to_xml/1, Features)
|
|
|
|
}]};
|
|
|
|
{error, Error} ->
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, Error]}
|
|
|
|
end
|
2003-01-01 20:54:44 +01:00
|
|
|
end.
|
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_sm_identity(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
2003-01-01 20:54:44 +01:00
|
|
|
|
2005-09-04 03:58:47 +02:00
|
|
|
get_sm_features(empty, From, To, _Node, _Lang) ->
|
|
|
|
#jid{luser = LFrom, lserver = LSFrom} = From,
|
|
|
|
#jid{luser = LTo, lserver = LSTo} = To,
|
|
|
|
case {LFrom, LSFrom} of
|
|
|
|
{LTo, LSTo} ->
|
|
|
|
{error, ?ERR_ITEM_NOT_FOUND};
|
2004-09-17 21:52:59 +02:00
|
|
|
_ ->
|
2005-09-04 03:58:47 +02:00
|
|
|
{error, ?ERR_NOT_ALLOWED}
|
|
|
|
end;
|
|
|
|
|
|
|
|
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),
|
2003-01-01 20:54:44 +01:00
|
|
|
lists:map(fun(R) ->
|
|
|
|
{xmlelement, "item",
|
2005-09-04 03:58:47 +02:00
|
|
|
[{"jid", User ++ "@" ++ Server ++ "/" ++ R},
|
2003-01-01 20:54:44 +01:00
|
|
|
{"name", User}], []}
|
|
|
|
end, lists:sort(Rs)).
|
|
|
|
|
2005-09-04 05:31:23 +02:00
|
|
|
|
|
|
|
get_publish_items(empty,
|
|
|
|
#jid{luser = LFrom, lserver = LSFrom},
|
2006-01-19 03:17:31 +01:00
|
|
|
#jid{luser = LTo, lserver = LSTo} = _To,
|
2005-09-04 05:31:23 +02:00
|
|
|
Node, _Lang) ->
|
|
|
|
if
|
|
|
|
(LFrom == LTo) and (LSFrom == LSTo) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
retrieve_disco_publish({LTo, LSTo}, Node);
|
2005-09-04 05:31:23 +02:00
|
|
|
true ->
|
|
|
|
empty
|
|
|
|
end;
|
|
|
|
get_publish_items(Acc, _From, _To, _Node, _Lang) ->
|
|
|
|
Acc.
|
|
|
|
|
|
|
|
process_disco_publish(User, Node, Items) ->
|
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({xmlelement, _Name, _Attrs, _SubEls} = Item) ->
|
|
|
|
Action = xml:get_tag_attr_s("action", Item),
|
|
|
|
Jid = xml:get_tag_attr_s("jid", Item),
|
|
|
|
PNode = xml:get_tag_attr_s("node", Item),
|
|
|
|
Name = xml:get_tag_attr_s("name", Item),
|
|
|
|
?INFO_MSG("Disco publish: ~p ~p ~p ~p ~p ~p~n",
|
|
|
|
[User, Action, Node, Jid, PNode, Name]),
|
|
|
|
|
|
|
|
%% The disco_publish table isn't strictly a "bag" table, as
|
|
|
|
%% entries with same jid and node combination are considered
|
|
|
|
%% the same, even if they have different names. Therefore,
|
|
|
|
%% we find a list of items to supersede.
|
|
|
|
SupersededItems = mnesia:match_object(
|
|
|
|
#disco_publish{owner_node = {User, Node},
|
|
|
|
jid = Jid,
|
|
|
|
node = PNode,
|
|
|
|
_ = '_'}),
|
|
|
|
case Action of
|
|
|
|
"update" ->
|
|
|
|
lists:map(
|
|
|
|
fun(O) ->
|
|
|
|
mnesia:delete_object(O)
|
|
|
|
end, SupersededItems),
|
|
|
|
mnesia:write(
|
|
|
|
#disco_publish{owner_node = {User, Node},
|
|
|
|
jid = Jid,
|
|
|
|
name = Name,
|
|
|
|
node = PNode});
|
|
|
|
"remove" ->
|
|
|
|
case SupersededItems of
|
|
|
|
[] ->
|
|
|
|
mnesia:abort({error, ?ERR_ITEM_NOT_FOUND});
|
|
|
|
_ ->
|
|
|
|
lists:map(
|
|
|
|
fun(O) ->
|
|
|
|
mnesia:delete_object(O)
|
|
|
|
end, SupersededItems)
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
%% invalid "action" attribute - return an error
|
|
|
|
mnesia:abort({error, ?ERR_BAD_REQUEST})
|
|
|
|
end;
|
|
|
|
({xmlcdata, _CDATA}) ->
|
|
|
|
ok
|
|
|
|
end, Items)
|
|
|
|
end,
|
|
|
|
case mnesia:transaction(F) of
|
|
|
|
{aborted, {error, _} = Error} ->
|
|
|
|
Error;
|
|
|
|
{atomic, _} ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR}
|
|
|
|
end.
|
|
|
|
|
|
|
|
retrieve_disco_publish(User, Node) ->
|
|
|
|
case catch mnesia:dirty_read({disco_publish, {User, Node}}) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
{error, ?ERR_INTERNAL_SERVER_ERROR};
|
|
|
|
[] ->
|
|
|
|
empty;
|
|
|
|
Items ->
|
|
|
|
{result,
|
|
|
|
lists:map(
|
|
|
|
fun(#disco_publish{jid = Jid,
|
|
|
|
name = Name,
|
|
|
|
node = PNode}) ->
|
|
|
|
{xmlelement, "item",
|
|
|
|
lists:append([[{"jid", Jid}],
|
|
|
|
case Name of
|
|
|
|
"" ->
|
|
|
|
[];
|
|
|
|
_ ->
|
|
|
|
[{"name", Name}]
|
|
|
|
end,
|
|
|
|
case PNode of
|
|
|
|
"" ->
|
|
|
|
[];
|
|
|
|
_ ->
|
|
|
|
[{"node", PNode}]
|
|
|
|
end]), []}
|
|
|
|
end, Items)}
|
|
|
|
end.
|
|
|
|
|