Skip to content

Commit

Permalink
Update XEP-0050: Ad-Hoc Commands to version 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pawlooss1 committed Jun 22, 2023
1 parent ca319ad commit 06c5b94
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/adhoc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

-module(adhoc).
-author('[email protected]').
-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 @@ -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 <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 = 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}]
}.
}.

0 comments on commit 06c5b94

Please sign in to comment.