2006-01-19 03:17:31 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : adhoc.erl
|
|
|
|
%%% Author : Magnus Henoch <henoch@dtek.chalmers.se>
|
2010-04-14 01:00:14 +02:00
|
|
|
%%% Purpose : Provide helper functions for ad-hoc commands (XEP-0050)
|
2006-01-19 03:17:31 +01:00
|
|
|
%%% Created : 31 Oct 2005 by Magnus Henoch <henoch@dtek.chalmers.se>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2013-01-24 15:25:13 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2013 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
|
|
|
%%%
|
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
|
|
|
|
%%%
|
2006-01-19 03:17:31 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(adhoc).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
-author('henoch@dtek.chalmers.se').
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([
|
|
|
|
parse_request/1,
|
|
|
|
produce_response/2,
|
|
|
|
produce_response/1
|
|
|
|
]).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2006-01-19 03:17:31 +01:00
|
|
|
-include("jlib.hrl").
|
|
|
|
-include("adhoc.hrl").
|
|
|
|
|
|
|
|
%% Parse an ad-hoc request. Return either an adhoc_request record or
|
|
|
|
%% an {error, ErrorType} tuple.
|
2013-03-14 10:33:02 +01:00
|
|
|
%%
|
|
|
|
-spec(parse_request/1 ::
|
|
|
|
(
|
|
|
|
IQ :: iq_request())
|
|
|
|
-> adhoc_response()
|
|
|
|
%%
|
|
|
|
| {error, _}
|
|
|
|
).
|
|
|
|
|
2006-01-19 03:17:31 +01:00
|
|
|
parse_request(#iq{type = set, lang = Lang, sub_el = SubEl, xmlns = ?NS_COMMANDS}) ->
|
|
|
|
?DEBUG("entering parse_request...", []),
|
2013-03-14 10:33:02 +01:00
|
|
|
Node = xml:get_tag_attr_s(<<"node">>, SubEl),
|
|
|
|
SessionID = xml:get_tag_attr_s(<<"sessionid">>, SubEl),
|
|
|
|
Action = xml:get_tag_attr_s(<<"action">>, SubEl),
|
2006-01-19 03:17:31 +01:00
|
|
|
XData = find_xdata_el(SubEl),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{children = AllEls} = SubEl,
|
2010-01-09 19:39:17 +01:00
|
|
|
Others = case XData of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> AllEls;
|
|
|
|
_ -> lists:delete(XData, AllEls)
|
2006-01-19 03:17:31 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
#adhoc_request{
|
|
|
|
lang = Lang,
|
|
|
|
node = Node,
|
|
|
|
sessionid = SessionID,
|
|
|
|
action = Action,
|
|
|
|
xdata = XData,
|
|
|
|
others = Others
|
|
|
|
};
|
|
|
|
parse_request(_) -> {error, ?ERR_BAD_REQUEST}.
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
%% Borrowed from mod_vcard.erl
|
2013-03-14 10:33:02 +01:00
|
|
|
find_xdata_el(#xmlel{children = SubEls}) ->
|
2006-01-19 03:17:31 +01:00
|
|
|
find_xdata_el1(SubEls).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
find_xdata_el1([]) -> false;
|
|
|
|
find_xdata_el1([El | Els]) when is_record(El, xmlel) ->
|
|
|
|
case xml:get_tag_attr_s(<<"xmlns">>, El) of
|
|
|
|
?NS_XDATA -> El;
|
|
|
|
_ -> find_xdata_el1(Els)
|
2006-01-19 03:17:31 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
find_xdata_el1([_ | Els]) -> find_xdata_el1(Els).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
%% Produce a <command/> node to use as response from an adhoc_response
|
|
|
|
%% record, filling in values for language, node and session id from
|
|
|
|
%% the request.
|
2013-03-14 10:33:02 +01:00
|
|
|
%%
|
|
|
|
-spec(produce_response/2 ::
|
|
|
|
(
|
|
|
|
Adhoc_Request :: adhoc_request(),
|
|
|
|
Adhoc_Response :: adhoc_response())
|
|
|
|
-> Xmlel::xmlel()
|
|
|
|
).
|
2006-01-19 03:17:31 +01:00
|
|
|
|
|
|
|
%% Produce a <command/> node to use as response from an adhoc_response
|
|
|
|
%% record.
|
2013-03-14 10:33:02 +01:00
|
|
|
produce_response(#adhoc_request{lang = Lang, node = Node, sessionid = SessionID},
|
|
|
|
Adhoc_Response) ->
|
|
|
|
produce_response(Adhoc_Response#adhoc_response{
|
|
|
|
lang = Lang, node = Node, sessionid = SessionID
|
|
|
|
}).
|
|
|
|
|
|
|
|
%%
|
|
|
|
-spec(produce_response/1 ::
|
|
|
|
(
|
|
|
|
Adhoc_Response::adhoc_response())
|
|
|
|
-> Xmlel::xmlel()
|
|
|
|
).
|
|
|
|
|
|
|
|
produce_response(
|
|
|
|
#adhoc_response{
|
|
|
|
%lang = _Lang,
|
|
|
|
node = Node,
|
|
|
|
sessionid = ProvidedSessionID,
|
|
|
|
status = Status,
|
|
|
|
defaultaction = DefaultAction,
|
|
|
|
actions = Actions,
|
|
|
|
notes = Notes,
|
|
|
|
elements = Elements
|
|
|
|
}) ->
|
|
|
|
SessionID = if is_binary(ProvidedSessionID),
|
|
|
|
ProvidedSessionID /= <<"">> -> ProvidedSessionID;
|
|
|
|
true -> jlib:now_to_utc_string(now())
|
|
|
|
end,
|
2006-01-19 03:17:31 +01:00
|
|
|
case Actions of
|
2013-03-14 10:33:02 +01:00
|
|
|
[] ->
|
|
|
|
ActionsEls = [];
|
|
|
|
_ ->
|
|
|
|
case DefaultAction of
|
|
|
|
<<"">> -> ActionsElAttrs = [];
|
|
|
|
_ -> ActionsElAttrs = [{<<"execute">>, DefaultAction}]
|
|
|
|
end,
|
|
|
|
ActionsEls = [
|
|
|
|
#xmlel{
|
|
|
|
name = <<"actions">>,
|
|
|
|
attrs = ActionsElAttrs,
|
|
|
|
children = [
|
|
|
|
#xmlel{name = Action, attrs = [], children = []}
|
|
|
|
|| Action <- Actions]
|
|
|
|
}
|
|
|
|
]
|
2006-01-19 03:17:31 +01:00
|
|
|
end,
|
|
|
|
NotesEls = lists:map(fun({Type, Text}) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{
|
|
|
|
name = <<"note">>,
|
|
|
|
attrs = [{<<"type">>, Type}],
|
|
|
|
children = [{xmlcdata, Text}]
|
|
|
|
}
|
|
|
|
end, Notes),
|
|
|
|
#xmlel{
|
|
|
|
name = <<"command">>,
|
|
|
|
attrs = [
|
|
|
|
{<<"xmlns">>, ?NS_COMMANDS},
|
|
|
|
{<<"sessionid">>, SessionID},
|
|
|
|
{<<"node">>, Node},
|
|
|
|
{<<"status">>, iolist_to_binary(atom_to_list(Status))}
|
|
|
|
],
|
|
|
|
children = ActionsEls ++ NotesEls ++ Elements
|
|
|
|
}.
|