25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-26 16:26:24 +01:00

Convert to the new #iq record from Exmpp.

SVN Revision: 1524
This commit is contained in:
Jean-Sébastien Pédron 2008-08-14 13:36:11 +00:00
parent 5e78c53572
commit d8c3aae412
9 changed files with 286 additions and 319 deletions

View File

@ -7,6 +7,11 @@
Remove the compatibility layer and always call modules with the new
#iq record from Exmpp.
* src/mod_roster.erl, src/mod_vcard.erl, src/adhoc.erl,
src/mod_adhoc.erl, src/mod_configure.erl, src/mod_configure2.erl,
src/mod_disco.erl, src/mod_last.erl: Convert to the new #iq record
from Exmpp.
2008-08-06 Jean-Sébastien Pédron <js.pedron@meetic-corp.com>
* src/mod_offline.erl, src/mod_offline_odbc.erl, src/mod_echo.erl,

View File

@ -38,18 +38,16 @@
%% Parse an ad-hoc request. Return either an adhoc_request record or
%% an {error, ErrorType} tuple.
parse_request(IQ) ->
parse_request(#iq{type = Type, ns = NS, payload = SubEl, lang = Lang}) ->
try
SubEl = exmpp_iq:get_request(IQ),
case {exmpp_iq:get_type(IQ), SubEl#xmlel.ns} of
case {Type, NS} of
{set, ?NS_ADHOC} ->
?DEBUG("entering parse_request...", []),
Lang = exmpp_stanza:get_lang(IQ),
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
SessionID = exmpp_xml:get_attribute(SubEl, 'sessionid', ""),
Action = exmpp_xml:get_attribute(SubEl, 'action', ""),
XData = find_xdata_el(SubEl),
AllEls = SubEl#xmlel.ns,
AllEls = exmpp_xml:get_child_elements(SubEl),
if XData ->
Others = lists:delete(XData, AllEls);
true ->

View File

@ -50,9 +50,9 @@
start(Host, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_ADHOC_s,
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_ADHOC,
?MODULE, process_local_iq, IQDisc),
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_ADHOC_s,
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_ADHOC,
?MODULE, process_sm_iq, IQDisc),
ejabberd_hooks:add(disco_local_identity, Host, ?MODULE, get_local_identity, 99),
@ -74,8 +74,8 @@ stop(Host) ->
ejabberd_hooks:delete(disco_local_features, Host, ?MODULE, get_local_features, 99),
ejabberd_hooks:delete(disco_local_identity, Host, ?MODULE, get_local_identity, 99),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_ADHOC_s),
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_ADHOC_s).
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_ADHOC),
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_ADHOC).
%-------------------------------------------------------------------------
@ -208,19 +208,19 @@ get_sm_features(Acc, _From, _To, _Node, _Lang) ->
%-------------------------------------------------------------------------
process_local_iq(From, To, IQ) ->
process_adhoc_request(From, To, IQ, adhoc_local_commands).
process_local_iq(From, To, IQ_Rec) ->
process_adhoc_request(From, To, IQ_Rec, adhoc_local_commands).
process_sm_iq(From, To, IQ) ->
process_adhoc_request(From, To, IQ, adhoc_sm_commands).
process_sm_iq(From, To, IQ_Rec) ->
process_adhoc_request(From, To, IQ_Rec, adhoc_sm_commands).
process_adhoc_request(From, To, IQ, Hook) ->
?DEBUG("About to parse ~p...", [IQ]),
case adhoc:parse_request(IQ) of
process_adhoc_request(From, To, IQ_Rec, Hook) ->
?DEBUG("About to parse ~p...", [IQ_Rec]),
case adhoc:parse_request(IQ_Rec) of
{error, Error} ->
exmpp_iq:error(IQ, Error);
exmpp_iq:error(IQ_Rec, Error);
#adhoc_request{} = AdhocRequest ->
Host = To#jid.ldomain,
% XXX OLD FORMAT: From, To.
@ -231,11 +231,11 @@ process_adhoc_request(From, To, IQ, Hook) ->
ignore ->
ignore;
empty ->
exmpp_iq:error(IQ, 'item-not-found');
exmpp_iq:error(IQ_Rec, 'item-not-found');
{error, Error} ->
exmpp_iq:error(IQ, Error);
exmpp_iq:error(IQ_Rec, Error);
Command ->
exmpp_iq:result(IQ, Command)
exmpp_iq:result(IQ_Rec, Command)
end
end.

View File

@ -82,8 +82,8 @@ stop(Host) ->
ejabberd_hooks:delete(disco_local_identity, Host, ?MODULE, get_local_identity, 50),
ejabberd_hooks:delete(disco_local_features, Host, ?MODULE, get_local_features, 50),
ejabberd_hooks:delete(disco_local_items, Host, ?MODULE, get_local_items, 50),
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_ADHOC_s),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_ADHOC_s).
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_ADHOC),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_ADHOC).
%%%-----------------------------------------------------------------------

View File

@ -47,22 +47,22 @@
start(Host, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_ECONFIGURE_s,
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_ECONFIGURE,
?MODULE, process_local_iq, IQDisc),
ok.
stop(Host) ->
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_ECONFIGURE_s).
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_ECONFIGURE).
process_local_iq(From, To, IQ) ->
process_local_iq(From, To, #iq{type = Type, payload = Request} = IQ_Rec) ->
case acl:match_rule(To#jid.ldomain, configure, From) of
deny ->
exmpp_iq:error(IQ, 'not-allowed');
exmpp_iq:error(IQ_Rec, 'not-allowed');
allow ->
case exmpp_iq:get_type(IQ) of
case Type of
set ->
exmpp_iq:error(IQ, 'feature-not-implemented');
exmpp_iq:error(IQ_Rec, 'feature-not-implemented');
%%case xml:get_tag_attr_s("type", SubEl) of
%% "cancel" ->
%% IQ#iq{type = result,
@ -96,11 +96,11 @@ process_local_iq(From, To, IQ) ->
%% sub_el = [SubEl, ?ERR_NOT_ALLOWED]}
%%end;
get ->
case process_get(IQ#xmlel.children) of
case process_get(Request) of
{result, Res} ->
exmpp_iq:result(IQ, Res);
exmpp_iq:result(IQ_Rec, Res);
{error, Error} ->
exmpp_iq:error(IQ, Error)
exmpp_iq:error(IQ_Rec, Error)
end
end
end.

View File

@ -63,13 +63,13 @@ start(Host, Opts) ->
ejabberd_local:refresh_iq_handlers(),
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
gen_iq_handler:add_iq_handler(ejabberd_local, Host, atom_to_list(?NS_DISCO_ITEMS),
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, atom_to_list(?NS_DISCO_INFO),
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, atom_to_list(?NS_DISCO_ITEMS),
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, atom_to_list(?NS_DISCO_INFO),
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]),
@ -101,10 +101,10 @@ stop(Host) ->
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),
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, atom_to_list(?NS_DISCO_ITEMS)),
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, atom_to_list(?NS_DISCO_INFO)),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, atom_to_list(?NS_DISCO_ITEMS)),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, atom_to_list(?NS_DISCO_INFO)),
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),
catch ets:match_delete(disco_features, {{'_', Host}}),
catch ets:match_delete(disco_extra_domains, {{'_', Host}}),
ok.
@ -126,75 +126,67 @@ unregister_extra_domain(Host, Domain) ->
catch ets:new(disco_extra_domains, [named_table, ordered_set, public]),
ets:delete(disco_extra_domains, {Domain, Host}).
process_local_iq_items(From, To, IQ) ->
case exmpp_iq:get_type(IQ) of
set ->
exmpp_iq:error(IQ, 'not-allowed');
get ->
SubEl = exmpp_iq:get_request(IQ),
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
Host = To#jid.ldomain,
Lang = exmpp_stanza:get_lang(IQ),
process_local_iq_items(From, To, #iq{type = get, payload = SubEl,
lang = Lang} = IQ_Rec) ->
Host = To#jid.ldomain,
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
% XXX OLD FORMAT: From, To.
FromOld = jlib:to_old_jid(From),
ToOld = jlib:to_old_jid(To),
case ejabberd_hooks:run_fold(disco_local_items,
Host,
empty,
[FromOld, ToOld, Node, Lang]) of
{result, Items} ->
% XXX OLD FORMAT: Items might be an #xmlelement.
ANode = case Node of
"" -> [];
_ -> [#xmlattr{name = 'node', value = Node}]
% XXX OLD FORMAT: From, To.
FromOld = jlib:to_old_jid(From),
ToOld = jlib:to_old_jid(To),
case ejabberd_hooks:run_fold(disco_local_items,
Host,
empty,
[FromOld, ToOld, Node, Lang]) of
{result, Items} ->
% XXX OLD FORMAT: Items might be an #xmlelement.
ANode = case Node of
"" -> [];
_ -> [#xmlattr{name = 'node', value = Node}]
end,
Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query',
attrs = ANode, children = Items},
exmpp_iq:result(IQ_Rec, Result);
{error, Error} ->
% XXX OLD FORMAT: 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').
process_local_iq_info(From, To, #iq{type = get, payload = SubEl,
lang = Lang} = IQ_Rec) ->
Host = To#jid.ldomain,
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
% XXX OLD FORMAT: From, To.
FromOld = jlib:to_old_jid(From),
ToOld = jlib:to_old_jid(To),
% XXX OLD FORMAT: Identity might be an #xmlelement.
Identity = ejabberd_hooks:run_fold(disco_local_identity,
Host,
[],
[FromOld, ToOld, Node, Lang]),
% XXX OLD FORMAT: From, To.
case ejabberd_hooks:run_fold(disco_local_features,
Host,
empty,
[FromOld, ToOld, Node, Lang]) of
{result, Features} ->
ANode = case Node of
"" -> [];
_ -> [#xmlattr{name = 'node', value = Node}]
end,
Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query',
attrs = ANode, children = Items},
exmpp_iq:result(IQ, Result);
{error, Error} ->
% XXX OLD FORMAT: Error.
exmpp_iq:error(IQ, Error)
end
end.
process_local_iq_info(From, To, IQ) ->
case exmpp_iq:get_type(IQ) of
set ->
exmpp_iq:error(IQ, 'not-allowed');
get ->
Host = To#jid.ldomain,
SubEl = exmpp_iq:get_request(IQ),
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
Lang = exmpp_stanza:get_lang(IQ),
% XXX OLD FORMAT: From, To.
FromOld = jlib:to_old_jid(From),
ToOld = jlib:to_old_jid(To),
% XXX OLD FORMAT: Identity might be an #xmlelement.
Identity = ejabberd_hooks:run_fold(disco_local_identity,
Host,
[],
[FromOld, ToOld, Node, Lang]),
% XXX OLD FORMAT: From, To.
case ejabberd_hooks:run_fold(disco_local_features,
Host,
empty,
[FromOld, ToOld, Node, Lang]) of
{result, Features} ->
ANode = case Node of
"" -> [];
_ -> [#xmlattr{name = 'node', value = Node}]
end,
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
attrs = ANode,
children = Identity ++ lists:map(fun feature_to_xml/1,
Features)},
exmpp_iq:result(IQ, Result);
{error, Error} ->
exmpp_iq:error(IQ, Error)
end
end.
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
attrs = ANode,
children = Identity ++ lists:map(fun feature_to_xml/1,
Features)},
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').
get_local_identity(Acc, _From, _To, [], _Lang) ->
Acc ++ [#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs = [
@ -232,6 +224,10 @@ feature_to_xml({{Feature, _Host}}) ->
feature_to_xml(Feature) when is_list(Feature) ->
#xmlel{ns = ?NS_DISCO_INFO, name = 'feature', attrs = [
#xmlattr{name = 'var', value = Feature}
]};
feature_to_xml(Feature) when is_atom(Feature) ->
#xmlel{ns = ?NS_DISCO_INFO, name = 'feature', attrs = [
#xmlattr{name = 'var', value = atom_to_list(Feature)}
]}.
domain_to_xml({Domain}) ->
@ -283,50 +279,46 @@ get_vh_services(Host) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
process_sm_iq_items(From, To, IQ) ->
SubEl = exmpp_iq:get_request(IQ),
case exmpp_iq:get_type(IQ) of
set ->
#jid{lnode = LTo, ldomain = ToServer} = To,
#jid{lnode = LFrom, ldomain = LServer} = From,
Self = (LTo == LFrom) andalso (ToServer == LServer),
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
if
Self, Node /= [] ->
%% Here, we treat disco publish attempts to your own JID.
Items = SubEl#xmlel.children,
case process_disco_publish({LFrom, LServer}, Node, Items) of
ok ->
exmpp_iq:result(IQ);
{error, Err} ->
exmpp_iq:error(IQ, Err)
end;
true ->
exmpp_iq:error(IQ, 'not-allowed')
process_sm_iq_items(From, To, #iq{type = get, payload = SubEl,
lang = Lang} = IQ_Rec) ->
Host = To#jid.ldomain,
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
% XXX OLD FORMAT: From, To.
FromOld = jlib:to_old_jid(From),
ToOld = jlib:to_old_jid(To),
case ejabberd_hooks:run_fold(disco_sm_items,
Host,
empty,
[FromOld, ToOld, Node, Lang]) of
{result, Items} ->
ANode = case Node of
"" -> [];
_ -> [#xmlattr{name = 'node', value = Node}]
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_sm_iq_items(From, To, #iq{type = set, payload = SubEl} = IQ_Rec) ->
#jid{lnode = LTo, ldomain = ToServer} = To,
#jid{lnode = LFrom, ldomain = LServer} = From,
Self = (LTo == LFrom) andalso (ToServer == LServer),
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
if
Self, Node /= [] ->
%% Here, we treat disco publish attempts to your own JID.
Items = SubEl#xmlel.children,
case process_disco_publish({LFrom, LServer}, Node, Items) of
ok ->
exmpp_iq:result(IQ_Rec);
{error, Err} ->
exmpp_iq:error(IQ_Rec, Err)
end;
get ->
Host = To#jid.ldomain,
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
Lang = exmpp_stanza:get_lang(IQ),
% XXX OLD FORMAT: From, To.
FromOld = jlib:to_old_jid(From),
ToOld = jlib:to_old_jid(To),
case ejabberd_hooks:run_fold(disco_sm_items,
Host,
empty,
[FromOld, ToOld, Node, Lang]) of
{result, Items} ->
ANode = case Node of
"" -> [];
_ -> [#xmlattr{name = 'node', value = Node}]
end,
Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query',
attrs = ANode, children = Items},
exmpp_iq:result(IQ, Result);
{error, Error} ->
exmpp_iq:error(IQ, Error)
end
true ->
exmpp_iq:error(IQ_Rec, 'not-allowed')
end.
get_sm_items({error, _Error} = Acc, _From, _To, _Node, _Lang) ->
@ -359,41 +351,37 @@ get_sm_items(empty, From, To, _Node, _Lang) ->
{error, 'not-allowed'}
end.
process_sm_iq_info(From, To, IQ) ->
case exmpp_iq:get_type(IQ) of
set ->
exmpp_iq:error(IQ, 'not-allowed');
get ->
Host = To#jid.ldomain,
SubEl = exmpp_iq:get_request(IQ),
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
Lang = exmpp_stanza:get_lang(IQ),
% XXX OLD FORMAT: From, To.
FromOld = jlib:to_old_jid(From),
ToOld = jlib:to_old_jid(To),
% XXX OLD FORMAT: Identity might be an #xmlelement.
Identity = ejabberd_hooks:run_fold(disco_sm_identity,
Host,
[],
[FromOld, ToOld, Node, Lang]),
case ejabberd_hooks:run_fold(disco_sm_features,
Host,
empty,
[FromOld, ToOld, Node, Lang]) of
{result, Features} ->
ANode = case Node of
"" -> [];
_ -> [#xmlattr{name = 'node', value = Node}]
end,
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
attrs = ANode,
children = Identity ++ lists:map(fun feature_to_xml/1,
Features)},
exmpp_iq:result(IQ, Result);
{error, Error} ->
exmpp_iq:error(IQ, Error)
end
end.
process_sm_iq_info(From, To, #iq{type = get, payload = SubEl,
lang = Lang} = IQ_Rec) ->
Host = To#jid.ldomain,
Node = exmpp_xml:get_attribute(SubEl, 'node', ""),
% XXX OLD FORMAT: From, To.
FromOld = jlib:to_old_jid(From),
ToOld = jlib:to_old_jid(To),
% XXX OLD FORMAT: Identity might be an #xmlelement.
Identity = ejabberd_hooks:run_fold(disco_sm_identity,
Host,
[],
[FromOld, ToOld, Node, Lang]),
case ejabberd_hooks:run_fold(disco_sm_features,
Host,
empty,
[FromOld, ToOld, Node, Lang]) of
{result, Features} ->
ANode = case Node of
"" -> [];
_ -> [#xmlattr{name = 'node', value = Node}]
end,
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
attrs = ANode,
children = Identity ++ lists:map(fun feature_to_xml/1,
Features)},
exmpp_iq:result(IQ_Rec, Result);
{error, Error} ->
exmpp_iq:error(IQ_Rec, Error)
end;
process_sm_iq_info(_From, _To, #iq{type = set} = IQ_Rec) ->
exmpp_iq:error(IQ_Rec, 'not-allowed').
get_sm_identity(Acc, _From, _To, _Node, _Lang) ->
Acc.

View File

@ -52,9 +52,9 @@ start(Host, Opts) ->
[{disc_copies, [node()]},
{attributes, record_info(fields, last_activity)}]),
update_table(),
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_LAST_ACTIVITY_s,
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_LAST_ACTIVITY,
?MODULE, process_local_iq, IQDisc),
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_LAST_ACTIVITY_s,
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_LAST_ACTIVITY,
?MODULE, process_sm_iq, IQDisc),
ejabberd_hooks:add(remove_user, Host,
?MODULE, remove_user, 50),
@ -66,62 +66,56 @@ stop(Host) ->
?MODULE, remove_user, 50),
ejabberd_hooks:delete(unset_presence_hook, Host,
?MODULE, on_presence_update, 50),
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_LAST_ACTIVITY_s),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_LAST_ACTIVITY_s).
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_LAST_ACTIVITY),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_LAST_ACTIVITY).
process_local_iq(_From, _To, IQ) ->
case exmpp_iq:get_type(IQ) of
set ->
exmpp_iq:error(IQ, 'not-allowed');
get ->
Sec = trunc(element(1, erlang:statistics(wall_clock))/1000),
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query', attrs =
[#xmlattr{name = 'seconds', value = integer_to_list(Sec)}]},
exmpp_iq:result(IQ, Response)
end.
process_local_iq(_From, _To, #iq{type = get} = IQ_Rec) ->
Sec = trunc(element(1, erlang:statistics(wall_clock))/1000),
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query', attrs =
[#xmlattr{name = 'seconds', value = integer_to_list(Sec)}]},
exmpp_iq:result(IQ_Rec, Response);
process_local_iq(_From, _To, #iq{type = set} = IQ_Rec) ->
exmpp_iq:error(IQ_Rec, 'not-allowed').
process_sm_iq(From, To, IQ) ->
case exmpp_iq:get_type(IQ) of
set ->
exmpp_iq:error(IQ, 'not-allowed');
get ->
User = To#jid.lnode,
Server = To#jid.ldomain,
{Subscription, _Groups} =
ejabberd_hooks:run_fold(
roster_get_jid_info, Server,
{none, []}, [User, Server, From]),
if
(Subscription == both) or (Subscription == from) ->
UserListRecord = ejabberd_hooks:run_fold(
privacy_get_user_list, Server,
#userlist{},
[User, Server]),
case ejabberd_hooks:run_fold(
privacy_check_packet, Server,
allow,
[User, Server, UserListRecord,
{From, To,
exmpp_presence:available()},
out]) of
allow ->
get_last(IQ, User, Server);
deny ->
exmpp_iq:error(IQ, 'not-allowed')
end;
true ->
exmpp_iq:error(IQ, 'not-allowed')
end
end.
process_sm_iq(From, To, #iq{type = get} = IQ_Rec) ->
User = To#jid.lnode,
Server = To#jid.ldomain,
{Subscription, _Groups} =
ejabberd_hooks:run_fold(
roster_get_jid_info, Server,
{none, []}, [User, Server, From]),
if
(Subscription == both) or (Subscription == from) ->
UserListRecord = ejabberd_hooks:run_fold(
privacy_get_user_list, Server,
#userlist{},
[User, Server]),
case ejabberd_hooks:run_fold(
privacy_check_packet, Server,
allow,
[User, Server, UserListRecord,
{From, To,
exmpp_presence:available()},
out]) of
allow ->
get_last(IQ_Rec, User, Server);
deny ->
exmpp_iq:error(IQ_Rec, 'not-allowed')
end;
true ->
exmpp_iq:error(IQ_Rec, 'not-allowed')
end;
process_sm_iq(_From, _To, #iq{type = set} = IQ_Rec) ->
exmpp_iq:error(IQ_Rec, 'not-allowed').
%% TODO: This function could use get_last_info/2
get_last(IQ, LUser, LServer) ->
get_last(IQ_Rec, LUser, LServer) ->
case catch mnesia:dirty_read(last_activity, {LUser, LServer}) of
{'EXIT', _Reason} ->
exmpp_iq:error(IQ, 'internal-server-error');
exmpp_iq:error(IQ_Rec, 'internal-server-error');
[] ->
exmpp_iq:error(IQ, 'service-unavailable');
exmpp_iq:error(IQ_Rec, 'service-unavailable');
[#last_activity{timestamp = TimeStamp, status = Status}] ->
{MegaSecs, Secs, _MicroSecs} = now(),
TimeStamp2 = MegaSecs * 1000000 + Secs,
@ -129,7 +123,7 @@ get_last(IQ, LUser, LServer) ->
Response = #xmlel{ns = ?NS_LAST_ACTIVITY, name = 'query',
attrs = [#xmlattr{name = 'seconds', value = integer_to_list(Sec)}],
children = [#xmlcdata{cdata = list_to_binary(Status)}]},
exmpp_iq:result(IQ, Response)
exmpp_iq:result(IQ_Rec, Response)
end.

View File

@ -78,8 +78,7 @@ start(Host, Opts) ->
?MODULE, webadmin_page, 50),
ejabberd_hooks:add(webadmin_user, Host,
?MODULE, webadmin_user, 50),
% XXX OLD FORMAT: NS as string.
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, atom_to_list(?NS_ROSTER),
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_ROSTER,
?MODULE, process_iq, IQDisc).
stop(Host) ->
@ -103,41 +102,38 @@ stop(Host) ->
?MODULE, webadmin_page, 50),
ejabberd_hooks:delete(webadmin_user, Host,
?MODULE, webadmin_user, 50),
% XXX OLD FORMAT: NS as string.
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
atom_to_list(?NS_ROSTER)).
?NS_ROSTER).
process_iq(From, To, IQ) ->
process_iq(From, To, IQ_Rec) ->
#jid{ldomain = LServer} = From,
case lists:member(LServer, ?MYHOSTS) of
true ->
process_local_iq(From, To, IQ);
process_local_iq(From, To, IQ_Rec);
_ ->
exmpp_iq:error(IQ, 'item-not-found')
exmpp_iq:error(IQ_Rec, 'item-not-found')
end.
process_local_iq(From, To, IQ) ->
case exmpp_iq:get_type(IQ) of
set ->
process_iq_set(From, To, IQ);
get ->
process_iq_get(From, To, IQ)
end.
process_local_iq(From, To, #iq{type = get} = IQ_Rec) ->
process_iq_get(From, To, IQ_Rec);
process_local_iq(From, To, #iq{type = set} = IQ_Rec) ->
process_iq_set(From, To, IQ_Rec).
process_iq_get(From, To, IQ) ->
process_iq_get(From, To, IQ_Rec) ->
LUser = From#jid.lnode,
LServer = From#jid.ldomain,
US = {LUser, LServer},
case catch ejabberd_hooks:run_fold(roster_get, To#jid.ldomain, [], [US]) of
Items when is_list(Items) ->
XItems = lists:map(fun item_to_xml/1, Items),
exmpp_iq:result(IQ, #xmlel{ns = ?NS_ROSTER, name = 'query',
children = XItems});
Result = #xmlel{ns = ?NS_ROSTER, name = 'query',
children = XItems},
exmpp_iq:result(IQ_Rec, Result);
_ ->
exmpp_iq:error(IQ, 'internal-server-error')
exmpp_iq:error(IQ_Rec, 'internal-server-error')
end.
get_user_roster(Acc, US) ->
@ -183,14 +179,14 @@ item_to_xml(Item) ->
#xmlel{ns = ?NS_ROSTER, name = 'item', attrs = Attrs4, children = SubEls}.
process_iq_set(From, To, IQ) ->
case exmpp_iq:get_request(IQ) of
process_iq_set(From, To, #iq{payload = Request} = IQ_Rec) ->
case Request of
#xmlel{children = Els} ->
lists:foreach(fun(El) -> process_item_set(From, To, El) end, Els);
_ ->
ok
end,
exmpp_iq:result(IQ).
exmpp_iq:result(IQ_Rec).
process_item_set(From, To, #xmlel{} = Item) ->
try

View File

@ -84,11 +84,9 @@ start(Host, Opts) ->
ejabberd_hooks:add(remove_user, Host,
?MODULE, remove_user, 50),
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
% XXX OLD FORMAT: NS as string.
gen_iq_handler:add_iq_handler(ejabberd_local, Host, atom_to_list(?NS_VCARD),
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_VCARD,
?MODULE, process_local_iq, IQDisc),
% XXX OLD FORMAT: NS as string.
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, atom_to_list(?NS_VCARD),
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_VCARD,
?MODULE, process_sm_iq, IQDisc),
ejabberd_hooks:add(disco_sm_features, Host, ?MODULE, get_sm_features, 50),
MyHost = gen_mod:get_opt_host(Host, Opts, "vjud.@HOST@"),
@ -126,12 +124,10 @@ loop(Host, ServerHost) ->
stop(Host) ->
ejabberd_hooks:delete(remove_user, Host,
?MODULE, remove_user, 50),
% XXX OLD FORMAT: NS as string.
gen_iq_handler:remove_iq_handler(ejabberd_local, Host,
atom_to_list(?NS_VCARD)),
% XXX OLD FORMAT: NS as string.
?NS_VCARD),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
atom_to_list(?NS_VCARD)),
?NS_VCARD),
ejabberd_hooks:delete(disco_sm_features, Host, ?MODULE, get_sm_features, 50),
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
Proc ! stop,
@ -155,63 +151,53 @@ get_sm_features(Acc, _From, _To, Node, _Lang) ->
Acc
end.
process_local_iq(_From, _To, IQ) ->
case exmpp_iq:get_type(IQ) of
set ->
exmpp_iq:error(IQ, 'not-allowed');
get ->
Lang = case exmpp_stanza:get_lang(IQ) of
undefined -> "";
L -> L
end,
Result = #xmlel{ns = ?NS_VCARD, name = 'vCard', children = [
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'FN'},
"ejabberd"),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'URL'},
?EJABBERD_URI),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'DESC'},
translate:translate(Lang, "Erlang Jabber Server") ++
"\nCopyright (c) 2002-2008 ProcessOne"),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'BDAY'},
"2002-11-16")
]},
exmpp_iq:result(IQ, Result)
end.
process_local_iq(_From, _To, #iq{type = get, lang = Lang} = IQ_Rec) ->
Result = #xmlel{ns = ?NS_VCARD, name = 'vCard', children = [
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'FN'},
"ejabberd"),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'URL'},
?EJABBERD_URI),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'DESC'},
translate:translate(Lang, "Erlang Jabber Server") ++
"\nCopyright (c) 2002-2008 ProcessOne"),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'BDAY'},
"2002-11-16")
]},
exmpp_iq:result(IQ_Rec, Result);
process_local_iq(_From, _To, #iq{type = set} = IQ_Rec) ->
exmpp_iq:error(IQ_Rec, 'not-allowed').
process_sm_iq(From, To, IQ) ->
case exmpp_iq:get_type(IQ) of
set ->
#jid{node = User, ldomain = LServer} = From,
case lists:member(LServer, ?MYHOSTS) of
true ->
set_vcard(User, LServer, exmpp_iq:get_request(IQ)),
exmpp_iq:result(IQ);
false ->
exmpp_iq:error(IQ, 'not-allowed')
end;
get ->
#jid{lnode = LUser, ldomain = LServer} = To,
US = {LUser, LServer},
F = fun() ->
mnesia:read({vcard, US})
end,
Els = case mnesia:transaction(F) of
{atomic, Rs} ->
lists:map(fun(R) ->
case R#vcard.vcard of
#xmlel{} = E ->
E;
#xmlelement{} = E ->
% XXX OLD FORMAT: Base contains old elements.
io:format("VCARD: Old element in base: ~p~n", [E]),
exmpp_xml:xmlelement_to_xmlel(E, [?NS_VCARD], [])
end
end, Rs);
{aborted, _Reason} ->
[]
end,
exmpp_iq:result(IQ, Els)
process_sm_iq(_From, To, #iq{type = get} = IQ_Rec) ->
#jid{lnode = LUser, ldomain = LServer} = To,
US = {LUser, LServer},
F = fun() ->
mnesia:read({vcard, US})
end,
[VCard | _] = case mnesia:transaction(F) of
{atomic, Rs} ->
lists:map(fun(R) ->
case R#vcard.vcard of
#xmlel{} = E ->
E;
#xmlelement{} = E ->
% XXX OLD FORMAT: Base contains old elements.
io:format("VCARD: Old element in base: ~p~n", [E]),
exmpp_xml:xmlelement_to_xmlel(E, [?NS_VCARD], [])
end
end, Rs);
{aborted, _Reason} ->
[]
end,
exmpp_iq:result(IQ_Rec, VCard);
process_sm_iq(From, _To, #iq{type = set, payload = Request} = IQ_Rec) ->
#jid{node = User, ldomain = LServer} = From,
case lists:member(LServer, ?MYHOSTS) of
true ->
set_vcard(User, LServer, Request),
exmpp_iq:result(IQ_Rec);
false ->
exmpp_iq:error(IQ_Rec, 'not-allowed')
end.
set_vcard(User, LServer, VCARD) ->