From 06c5b94570e6ce0cbdf7e9876c348aff34c30c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20D=C5=82ugosz?= Date: Wed, 21 Jun 2023 13:41:00 +0200 Subject: [PATCH] Update XEP-0050: Ad-Hoc Commands to version 1.3.0 --- src/adhoc.erl | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/adhoc.erl b/src/adhoc.erl index ca1d9c0466..c0f6da7f9b 100644 --- a/src/adhoc.erl +++ b/src/adhoc.erl @@ -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, @@ -126,21 +126,34 @@ ensure_correct_session_id(_) -> -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">>), + [#xmlel{ + name = <<"actions">>, + children = [#xmlel{name = Action} || Action <- AllActions] + }]; maybe_actions_element(Actions, DefaultAction) -> - ActionsElAttrs = case DefaultAction of - <<"">> -> []; - _ -> [{<<"execute">>, DefaultAction}] - end, + % A form which has an 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 = ActionsElAttrs, - children = [#xmlel{name = Action} || Action <- Actions] + attrs = [{<<"execute">>, DefaultAction}], + children = [#xmlel{name = Action} || Action <- AllActions] }]. +-spec ensure_default_action_present([binary()], binary()) -> [exml:element()]. +ensure_default_action_present(Actions, DefaultAction) -> + case lists:member(Actions, DefaultAction) of + 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}] - }. \ No newline at end of file + }.