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

Explicitly convert binary to atom where possible #2445

Merged
merged 1 commit into from Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/auth/ejabberd_auth_jwt.erl
Expand Up @@ -89,7 +89,7 @@ check_password(LUser, LServer, Password) ->
{env, Var} -> list_to_binary(os:getenv(Var))
end,
BinAlg = ejabberd_auth:get_opt(LServer, jwt_algorithm),
Alg = binary_to_atom(stringprep:tolower(BinAlg), latin1),
Alg = binary_to_atom(stringprep:tolower(BinAlg), utf8),
case jwerl:verify(Password, Alg, Key) of
{ok, TokenData} ->
UserKey = ejabberd_auth:get_opt(LServer, jwt_username_key),
Expand Down
4 changes: 2 additions & 2 deletions src/ejabberd_zlib.erl
Expand Up @@ -81,7 +81,7 @@ recv_data2(ZlibSock, Packet) ->
Res
end.

-spec recv_data1(zlibsock(), iolist()) -> {'error', string()} | {'ok', binary()}.
-spec recv_data1(zlibsock(), iolist()) -> {'error', atom()} | {'ok', binary()}.
recv_data1(#zlibsock{zlibport = Port, inflate_size_limit = SizeLimit} = _ZlibSock, Packet) ->
case port_control(Port, SizeLimit bsl 2 + ?INFLATE, Packet) of
<<0, In/binary>> ->
Expand All @@ -90,7 +90,7 @@ recv_data1(#zlibsock{zlibport = Port, inflate_size_limit = SizeLimit} = _ZlibSoc
{error, erlang:binary_to_existing_atom(Error, utf8)}
end.

-spec send(zlibsock(), iolist()) -> ok | {error, string()}.
-spec send(zlibsock(), iolist()) -> ok | {error, atom()}.
send(#zlibsock{sockmod = SockMod, socket = Socket, zlibport = Port},
Packet) ->
case port_control(Port, ?DEFLATE, Packet) of
Expand Down
11 changes: 9 additions & 2 deletions src/mod_auth_token.erl
Expand Up @@ -62,7 +62,6 @@
Owner :: jid:jid().

-define(A2B(A), atom_to_binary(A, utf8)).
-define(B2A(B), binary_to_atom(B, utf8)).

-define(I2B(I), integer_to_binary(I)).
-define(B2I(B), binary_to_integer(B)).
Expand Down Expand Up @@ -348,7 +347,7 @@ default_validity_period(refresh) -> {25, days}.
Token :: token().
get_token_as_record(BToken) ->
[BType, User, Expiry | Rest] = binary:split(BToken, <<(field_separator())>>, [global]),
T = #token{type = ?B2A(BType),
T = #token{type = decode_token_type(BType),
expiry_datetime = seconds_to_datetime(binary_to_integer(Expiry)),
user_jid = jid:from_binary(User)},
T1 = case {BType, Rest} of
Expand All @@ -364,6 +363,14 @@ get_token_as_record(BToken) ->
end,
T1#token{token_body = join_fields(T1)}.

-spec decode_token_type(binary()) -> token_type().
decode_token_type(<<"access">>) ->
access;
decode_token_type(<<"refresh">>) ->
refresh;
decode_token_type(<<"provision">>) ->
provision.

-spec get_key_for_user(token_type(), jid:jid()) -> binary().
get_key_for_user(TokenType, User) ->
UsersHost = User#jid.lserver,
Expand Down
5 changes: 4 additions & 1 deletion src/mod_blocking.erl
Expand Up @@ -61,7 +61,7 @@ process_iq_set(Acc, From, _To, #iq{xmlns = ?NS_BLOCKING, sub_el = SubEl}) ->
%% collect needed data
#jid{luser = LUser, lserver = LServer} = From,
#xmlel{name = BType} = SubEl,
Type = binary_to_existing_atom(BType, latin1),
Type = parse_command_type(BType),
Usrs = exml_query:paths(SubEl, [{element, <<"item">>}, {attr, <<"jid">>}]),
CurrList = case mod_privacy_backend:get_privacy_list(LUser, LServer, <<"blocking">>) of
{ok, List} ->
Expand All @@ -79,6 +79,9 @@ process_iq_set(Acc, From, _To, #iq{xmlns = ?NS_BLOCKING, sub_el = SubEl}) ->
process_iq_set(Val, _, _, _) ->
Val.

parse_command_type(<<"block">>) -> block;
parse_command_type(<<"unblock">>) -> unblock.

%% @doc Set IQ must do the following:
%% * get / create a dedicated privacy list (we call it "blocking")
%% * modify the list
Expand Down
5 changes: 4 additions & 1 deletion src/mod_commands.erl
Expand Up @@ -391,9 +391,12 @@ lookup_recent_messages(ArcJID, WithJID, Before, Limit) ->
L.

subscription(Caller, Other, Action) ->
Act = binary_to_existing_atom(Action, latin1),
Act = decode_action(Action),
run_subscription(Act, jid:from_binary(Caller), jid:from_binary(Other)).

decode_action(<<"subscribe">>) -> subscribe;
decode_action(<<"subscribed">>) -> subscribed.

-spec run_subscription(subscribe | subscribed, jid:jid(), jid:jid()) -> ok.
run_subscription(Type, CallerJid, OtherJid) ->
StanzaType = atom_to_binary(Type, latin1),
Expand Down
2 changes: 1 addition & 1 deletion src/mod_muc_room.erl
Expand Up @@ -3734,7 +3734,7 @@ set_xoption([{<<"muc#roomconfig_getmemberlist">>, Val} | Opts], Config) ->
[<<"none">>] ->
?SET_XOPT(maygetmemberlist, []);
_ ->
?SET_XOPT(maygetmemberlist, [binary_to_existing_atom(V, latin1) || V <- Val])
?SET_XOPT(maygetmemberlist, [binary_to_role(V) || V <- Val])
end;
set_xoption([{<<"muc#roomconfig_enablelogging">>, [Val]} | Opts], Config) ->
?SET_BOOL_XOPT(logging, Val);
Expand Down
17 changes: 16 additions & 1 deletion src/pubsub/mod_pubsub.erl
Expand Up @@ -1353,8 +1353,23 @@ all_metrics() ->
{get, affiliations},
{set, affiliations}].

iq_action_to_metric_name(<<"create">>) -> create;
iq_action_to_metric_name(<<"publish">>) -> publish;
iq_action_to_metric_name(<<"retract">>) -> retract;
iq_action_to_metric_name(<<"subscribe">>) -> subscribe;
iq_action_to_metric_name(<<"unsubscribe">>) -> unsubscribe;
iq_action_to_metric_name(<<"items">>) -> items;
iq_action_to_metric_name(<<"options">>) -> options;
iq_action_to_metric_name(<<"configure">>) -> configure;
iq_action_to_metric_name(<<"default">>) -> default;
iq_action_to_metric_name(<<"delete">>) -> delete;
iq_action_to_metric_name(<<"purge">>) -> purge;
iq_action_to_metric_name(<<"subscriptions">>) -> subscriptions;
iq_action_to_metric_name(<<"affiliations">>) -> affiliations.


metric_name(IQType, Name, MetricSuffix) when is_binary(Name) ->
NameAtom = binary_to_existing_atom(Name, utf8),
NameAtom = iq_action_to_metric_name(Name),
metric_name(IQType, NameAtom, MetricSuffix);
metric_name(IQType, Name, MetricSuffix) when is_atom(Name) ->
[pubsub, IQType, Name, MetricSuffix].
Expand Down
28 changes: 15 additions & 13 deletions src/pubsub/pubsub_form_utils.erl
Expand Up @@ -42,10 +42,10 @@
%%====================================================================

%% Missing options won't have any <value/> elements
%% TODO: Right now
%% TODO: Right now
-spec make_sub_xform(Options :: mod_pubsub:subOptions()) -> {ok, exml:element()}.
make_sub_xform(Options) ->
XFields = [make_field_xml(OptDefinition, Options) || OptDefinition <- sub_form_options()],
XFields = [make_field_xml(OptDefinition, Options) || OptDefinition <- sub_form_options()],
{ok, make_sub_xform_xml(XFields)}.

%% The list of options returned by this function may be a subset of the options schema.
Expand Down Expand Up @@ -76,7 +76,7 @@ make_sub_xform_xml(XFields) ->
make_field_xml({VarName, Key, #{ label := Label, form_type := FormType } = VarProps}, Options) ->
ChoicesEls = make_choices_xml(VarProps),
ValEls = make_values_xml(Key, Options, VarProps),

#xmlel{name = <<"field">>,
attrs = [{<<"var">>, VarName}, {<<"type">>, FormType}, {<<"label">>, Label}],
children = ChoicesEls ++ ValEls}.
Expand Down Expand Up @@ -139,7 +139,7 @@ sub_form_options() ->
label => <<"Whether an entity wants to receive an XMPP message body"
" in addition to the payload format">>
}},

{<<"pubsub#show-values">>, show_values,
#{ form_type => <<"list-multi">>,
possible_choices => [{<<"away">>, <<"Away">>},
Expand All @@ -150,15 +150,16 @@ sub_form_options() ->
data_type => list,
label => <<"The presence states for which an entity wants to receive notifications">>
}},

{<<"pubsub#subscription_type">>, subscription_type,
#{ form_type => <<"list-single">>,
possible_choices => [{<<"items">>, <<"Receive notification of new items only">>},
{<<"nodes">>, <<"Receive notification of new nodes only">>}],
data_type => atom,
data_type => {custom, #{ from_binaries => fun convert_sub_type_from_binary/1,
to_binaries => fun convert_sub_type_to_binary/1 }},
label => <<"Type of notification to receive">>
}},

{<<"pubsub#subscription_depth">>, subscription_depth,
#{ form_type => <<"list-single">>,
possible_choices => [{<<"1">>, <<"Receive notification from direct child nodes only">>},
Expand Down Expand Up @@ -204,9 +205,7 @@ convert_value_from_binaries([Bin], integer) ->
convert_value_from_binaries([Bin], datetime) ->
jlib:datetime_binary_to_timestamp(Bin);
convert_value_from_binaries(Bins, list) when is_list(Bins) ->
Bins;
convert_value_from_binaries([Bin], atom) ->
binary_to_existing_atom(Bin, utf8).
Bins.

-spec convert_value_to_binaries(Value :: any(), field_data_type()) -> [binary()].
convert_value_to_binaries(Value, {custom, #{ to_binaries := ConvertToBinaryFun }}) ->
Expand All @@ -218,9 +217,7 @@ convert_value_to_binaries(Value, integer) ->
convert_value_to_binaries(Value, datetime) ->
jlib:now_to_utc_binary(Value);
convert_value_to_binaries(Value, list) when is_list(Value) ->
Value;
convert_value_to_binaries(Value, atom) ->
atom_to_binary(Value, utf8).
Value.

convert_bool_from_binary(<<"0">>) -> false;
convert_bool_from_binary(<<"1">>) -> true;
Expand All @@ -236,3 +233,8 @@ convert_sub_depth_from_binary([DepthBin]) -> binary_to_integer(DepthBin).
convert_sub_depth_to_binary(all) -> [<<"all">>];
convert_sub_depth_to_binary(Depth) -> [integer_to_binary(Depth)].

convert_sub_type_from_binary(<<"items">>) -> items;
convert_sub_type_from_binary(<<"nodes">>) -> nodes.

convert_sub_type_to_binary(items) -> <<"items">>;
convert_sub_type_to_binary(nodes) -> <<"nodes">>.