25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Use xmpp:try_subtag/2 wherever possible

This commit is contained in:
Evgeniy Khramtsov 2017-12-11 09:46:26 +03:00
parent ae66c17ec0
commit bd064fa3fc
5 changed files with 24 additions and 6 deletions

View File

@ -61,11 +61,16 @@ mod_opt_type(_) ->
c2s_state() | {stop, c2s_state()}.
c2s_unauthenticated_packet(State, #iq{type = T, sub_els = [_]} = IQ)
when T == get; T == set ->
case xmpp:get_subtag(IQ, #legacy_auth{}) of
try xmpp:try_subtag(IQ, #legacy_auth{}) of
#legacy_auth{} = Auth ->
{stop, authenticate(State, xmpp:set_els(IQ, [Auth]))};
false ->
State
catch _:{xmpp_codec, Why} ->
Txt = xmpp:io_format_error(Why),
Lang = maps:get(lang, State),
Err = xmpp:make_error(IQ, xmpp:error_bad_request(Txt, Lang)),
{stop, ejabberd_c2s:send(State, Err)}
end;
c2s_unauthenticated_packet(State, _) ->
State.

View File

@ -4006,7 +4006,7 @@ route_invitation(From, Invitation, Lang, StateData) ->
%% Otherwise, an error message is sent to the sender.
-spec handle_roommessage_from_nonparticipant(message(), state(), jid()) -> ok.
handle_roommessage_from_nonparticipant(Packet, StateData, From) ->
case xmpp:get_subtag(Packet, #muc_user{}) of
try xmpp:try_subtag(Packet, #muc_user{}) of
#muc_user{decline = #muc_decline{to = #jid{} = To} = Decline} = XUser ->
NewDecline = Decline#muc_decline{to = undefined, from = From},
NewXUser = XUser#muc_user{decline = NewDecline},
@ -4018,6 +4018,10 @@ handle_roommessage_from_nonparticipant(Packet, StateData, From) ->
"to the conference">>,
Err = xmpp:err_not_acceptable(ErrText, xmpp:get_lang(Packet)),
ejabberd_router:route_error(Packet, Err)
catch _:{xmpp_codec, Why} ->
Txt = xmpp:io_format_error(Why),
Err = xmpp:err_bad_request(Txt, xmpp:get_lang(Packet)),
ejabberd_router:route_error(Packet, Err)
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@ -402,7 +402,7 @@ strip_addresses_element(Packet) ->
#addresses{list = Addrs} ->
PacketStripped = xmpp:remove_subtag(Packet, #addresses{}),
{ok, PacketStripped, Addrs};
undefined ->
false ->
throw(eadsele)
end.

View File

@ -274,7 +274,7 @@ get_permissions(ServerHost) ->
forward_message(#message{to = To} = Msg) ->
ServerHost = To#jid.lserver,
Lang = xmpp:get_lang(Msg),
case xmpp:get_subtag(Msg, #privilege{}) of
try xmpp:try_subtag(Msg, #privilege{}) of
#privilege{forwarded = #forwarded{xml_els = [SubEl]}} ->
try xmpp:decode(SubEl, ?NS_CLIENT, [ignore_els]) of
#message{} = NewMsg ->
@ -297,7 +297,11 @@ forward_message(#message{to = To} = Msg) ->
ejabberd_router:route_error(Msg, Err)
end;
_ ->
Txt = <<"Invalid <forwarded/> element">>,
Txt = <<"No <forwarded/> element found">>,
Err = xmpp:err_bad_request(Txt, Lang),
ejabberd_router:route_error(Msg, Err)
catch _:{xmpp_codec, Why} ->
Txt = xmpp:io_format_error(Why),
Err = xmpp:err_bad_request(Txt, Lang),
ejabberd_router:route_error(Msg, Err)
end.

View File

@ -95,7 +95,7 @@ stream_feature_register(Acc, Host) ->
c2s_unauthenticated_packet(#{ip := IP, server := Server} = State,
#iq{type = T, sub_els = [_]} = IQ)
when T == set; T == get ->
case xmpp:get_subtag(IQ, #register{}) of
try xmpp:try_subtag(IQ, #register{}) of
#register{} = Register ->
{Address, _} = IP,
IQ1 = xmpp:set_els(IQ, [Register]),
@ -105,6 +105,11 @@ c2s_unauthenticated_packet(#{ip := IP, server := Server} = State,
{stop, ejabberd_c2s:send(State, ResIQ1)};
false ->
State
catch _:{xmpp_codec, Why} ->
Txt = xmpp:io_format_error(Why),
Lang = maps:get(lang, State),
Err = xmpp:make_error(IQ, xmpp:error_bad_request(Txt, Lang)),
{stop, ejabberd_c2s:send(State, Err)}
end;
c2s_unauthenticated_packet(State, _) ->
State.