From a157e22c2f787276cab1a80a5cff32451f768587 Mon Sep 17 00:00:00 2001 From: Badlop Date: Tue, 13 Aug 2019 11:37:18 +0200 Subject: [PATCH] When join new room with password, set pass and password_protected (#2668) --- src/mod_muc.erl | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/mod_muc.erl b/src/mod_muc.erl index 36cebb664..33e6ff91d 100644 --- a/src/mod_muc.erl +++ b/src/mod_muc.erl @@ -508,6 +508,23 @@ unregister_routes(Hosts, 1) -> unregister_routes(_, _) -> ok. +%% Function copied from mod_muc_room.erl +-spec extract_password(presence() | iq()) -> binary() | false. +extract_password(#presence{} = Pres) -> + case xmpp:get_subtag(Pres, #muc{}) of + #muc{password = Password} when is_binary(Password) -> + Password; + _ -> + false + end; +extract_password(#iq{} = IQ) -> + case xmpp:get_subtag(IQ, #muc_subscribe{}) of + #muc_subscribe{password = Password} when Password /= <<"">> -> + Password; + _ -> + false + end. + -spec route_to_room(stanza(), binary()) -> ok. route_to_room(Packet, ServerHost) -> From = xmpp:get_from(Packet), @@ -527,7 +544,8 @@ route_to_room(Packet, ServerHost) -> {error, notfound} when StartType == start -> case check_create_room(ServerHost, Host, Room, From) of true -> - case start_new_room(RMod, Host, ServerHost, Room, From, Nick) of + Pass = extract_password(Packet), + case start_new_room(RMod, Host, ServerHost, Room, Pass, From, Nick) of {ok, Pid} -> mod_muc_room:route(Pid, Packet); _Err -> @@ -815,10 +833,19 @@ load_room(RMod, Host, ServerHost, Room) -> end end. -start_new_room(RMod, Host, ServerHost, Room, From, Nick) -> +start_new_room(RMod, Host, ServerHost, Room, Pass, From, Nick) -> ?DEBUG("Open new room: ~s", [Room]), DefRoomOpts = mod_muc_opt:default_room_options(ServerHost), - start_room(RMod, Host, ServerHost, Room, DefRoomOpts, From, Nick). + DefRoomOpts2 = add_password_options(Pass, DefRoomOpts), + start_room(RMod, Host, ServerHost, Room, DefRoomOpts2, From, Nick). + +add_password_options(false, DefRoomOpts) -> + DefRoomOpts; +add_password_options(<<>>, DefRoomOpts) -> + DefRoomOpts; +add_password_options(Pass, DefRoomOpts) when is_binary(Pass) -> + O2 = lists:keystore(password, 1, DefRoomOpts, {password, Pass}), + lists:keystore(password_protected, 1, O2, {password_protected, true}). start_room(Mod, Host, ServerHost, Room, DefOpts) -> Access = get_access(ServerHost),