Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update XEP-0050: Ad-Hoc Commands #4043

Merged
merged 5 commits into from
Jun 28, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions src/adhoc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

-module(adhoc).
-author('henoch@dtek.chalmers.se').
-xep([{xep, 50}, {version, "1.2"}]).
-xep([{xep, 50}, {version, "1.3"}]).
-export([parse_request/1,
produce_response/2,
produce_response/4,
Expand Down Expand Up @@ -105,33 +105,55 @@ produce_response(#adhoc_response{lang = _Lang,
actions = Actions,
notes = Notes,
elements = Elements}) ->
SessionID = if is_binary(ProvidedSessionID), ProvidedSessionID /= <<"">> ->
ProvidedSessionID;
true ->
USec = os:system_time(microsecond),
TS = calendar:system_time_to_rfc3339(USec, [{offset, "Z"},
{unit, microsecond}]),
list_to_binary(TS)
end,
ActionsEls = case Actions of
[] ->
[];
_ ->
ActionsElAttrs = case DefaultAction of
<<"">> -> [];
_ -> [{<<"execute">>, DefaultAction}]
end,
[#xmlel{name = <<"actions">>, attrs = ActionsElAttrs,
children = [#xmlel{name = Action} || Action <- Actions]}]
end,
NotesEls = lists:map(fun({Type, Text}) ->
#xmlel{name = <<"note">>,
attrs = [{<<"type">>, Type}],
children = [#xmlcdata{content = Text}]}
end, Notes),
SessionID = ensure_correct_session_id(ProvidedSessionID),
ActionsEls = maybe_actions_element(Actions, DefaultAction),
NotesEls = lists:map(fun note_to_xmlel/1, Notes),
#xmlel{name = <<"command">>,
attrs = [{<<"xmlns">>, ?NS_COMMANDS},
{<<"sessionid">>, SessionID},
{<<"node">>, Node},
{<<"status">>, list_to_binary(atom_to_list(Status))}],
children = ActionsEls ++ NotesEls ++ Elements}.

-spec ensure_correct_session_id(binary()) -> binary().
ensure_correct_session_id(SessionID) when is_binary(SessionID), SessionID /= <<"">> ->
pawlooss1 marked this conversation as resolved.
Show resolved Hide resolved
SessionID;
ensure_correct_session_id(_) ->
USec = os:system_time(microsecond),
TS = calendar:system_time_to_rfc3339(USec, [{offset, "Z"}, {unit, microsecond}]),
list_to_binary(TS).

-spec maybe_actions_element([binary()], binary()) -> [exml:element()].
maybe_actions_element([], _DefaultAction) ->
[];
maybe_actions_element(Actions, <<>>) ->
% If the "execute" attribute is absent, it defaults to "next".
AllActions = ensure_default_action_present(Actions, <<"next">>),
chrzaszcz marked this conversation as resolved.
Show resolved Hide resolved
[#xmlel{
name = <<"actions">>,
children = [#xmlel{name = Action} || Action <- AllActions]
}];
maybe_actions_element(Actions, DefaultAction) ->
% A form which has an <actions/> element and an "execute" attribute
% which evaluates to an action which is not allowed is invalid.
AllActions = ensure_default_action_present(Actions, DefaultAction),
[#xmlel{
name = <<"actions">>,
attrs = [{<<"execute">>, DefaultAction}],
children = [#xmlel{name = Action} || Action <- AllActions]
}].

-spec ensure_default_action_present([binary()], binary()) -> [exml:element()].
pawlooss1 marked this conversation as resolved.
Show resolved Hide resolved
ensure_default_action_present(Actions, DefaultAction) ->
case lists:member(Actions, DefaultAction) of
pawlooss1 marked this conversation as resolved.
Show resolved Hide resolved
true -> Actions;
false -> [DefaultAction | Actions]
end.

-spec note_to_xmlel({binary(), iodata()}) -> exml:element().
note_to_xmlel({Type, Text}) ->
#xmlel{
name = <<"note">>,
attrs = [{<<"type">>, Type}],
children = [#xmlcdata{content = Text}]
}.