diff --git a/CHANGELOG.md b/CHANGELOG.md index 404e83b..10a009d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.0.3] - 2024-01-30 + +### Fixed + +- Added owner checks to SetRoomDataInternal and SetRoomDataExternal and rpcn now only sends notifications on actual modification + + ## [1.0.2] - 2024-01-29 ### Fixed diff --git a/Cargo.lock b/Cargo.lock index 9a9c011..ebad71c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -916,7 +916,7 @@ dependencies = [ [[package]] name = "rpcn" -version = "1.0.2" +version = "1.0.3" dependencies = [ "flatbuffers", "hyper", diff --git a/Cargo.toml b/Cargo.toml index e3879fc..55353c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rpcn" -version = "1.0.2" +version = "1.0.3" authors = ["RipleyTom "] edition = "2021" diff --git a/src/server/client/cmd_room.rs b/src/server/client/cmd_room.rs index 63f6241..bcb8285 100644 --- a/src/server/client/cmd_room.rs +++ b/src/server/client/cmd_room.rs @@ -165,7 +165,7 @@ impl Client { pub fn req_set_roomdata_external(&mut self, data: &mut StreamExtractor) -> Result { let (com_id, setdata_req) = self.get_com_and_fb::(data)?; - if let Err(e) = self.shared.room_manager.write().set_roomdata_external(&com_id, &setdata_req) { + if let Err(e) = self.shared.room_manager.write().set_roomdata_external(&com_id, &setdata_req, self.client_info.user_id) { Ok(e) } else { Ok(ErrorType::NoError) @@ -190,7 +190,7 @@ impl Client { let res = self.shared.room_manager.write().set_roomdata_internal(&com_id, &setdata_req, self.client_info.user_id); match res { - Ok((users, notif_data)) => { + Ok(Some((users, notif_data))) => { let mut n_msg: Vec = Vec::new(); n_msg.extend(&room_id.to_le_bytes()); Client::add_data_packet(&mut n_msg, ¬if_data); @@ -199,6 +199,7 @@ impl Client { self.self_notification(¬if); Ok(ErrorType::NoError) } + Ok(None) => Ok(ErrorType::NoError), Err(e) => Ok(e), } } diff --git a/src/server/room_manager.rs b/src/server/room_manager.rs index ce65d09..4649ba6 100644 --- a/src/server/room_manager.rs +++ b/src/server/room_manager.rs @@ -1205,12 +1205,20 @@ impl RoomManager { builder.finished_data().to_vec() } - pub fn set_roomdata_external(&mut self, com_id: &ComId, req: &SetRoomDataExternalRequest) -> Result<(), ErrorType> { + pub fn set_roomdata_external(&mut self, com_id: &ComId, req: &SetRoomDataExternalRequest, user_id: i64) -> Result<(), ErrorType> { if !self.room_exists(com_id, req.roomId()) { return Err(ErrorType::NotFound); } let room = self.get_mut_room(com_id, req.roomId()); + let member_id = room.get_member_id(user_id)?; + let is_room_owner = room.get_owner() == member_id; + + if !is_room_owner { + // Should this return an error? + return Ok(()); + } + if let Some(vec) = req.roomBinAttrExternal() { for i in 0..vec.len() { let room_binattr_external_from_fb = RoomBinAttr::from_flatbuffer(&vec.get(i)); @@ -1265,17 +1273,48 @@ impl RoomManager { Ok(builder.finished_data().to_vec()) } - pub fn set_roomdata_internal(&mut self, com_id: &ComId, req: &SetRoomDataInternalRequest, user_id: i64) -> Result<(HashSet, Vec), ErrorType> { + pub fn set_roomdata_internal(&mut self, com_id: &ComId, req: &SetRoomDataInternalRequest, user_id: i64) -> Result, Vec)>, ErrorType> { if !self.room_exists(com_id, req.roomId()) { return Err(ErrorType::NotFound); } let room = self.get_mut_room(com_id, req.roomId()); let member_id = room.get_member_id(user_id)?; + let is_room_owner = room.get_owner() == member_id; + let mut has_changed = false; + + let old_password_slot_mask = room.password_slot_mask; let old_flag_attr = room.flag_attr; - let flag_filter = req.flagFilter(); - let flag_attr = req.flagAttr(); - room.flag_attr = (flag_attr & flag_filter) | (room.flag_attr & !flag_filter); + + if is_room_owner { + let flag_filter = req.flagFilter(); + let flag_attr = req.flagAttr(); + let new_room_flag_attr = (flag_attr & flag_filter) | (room.flag_attr & !flag_filter); + + if new_room_flag_attr != room.flag_attr { + room.flag_attr = new_room_flag_attr; + has_changed = true; + } + + // Group stuff TODO + + if old_password_slot_mask != req.passwordSlotMask() { + room.password_slot_mask = req.passwordSlotMask(); + has_changed = true; + } + + if let Some(vec) = req.ownerPrivilegeRank() { + let mut succession_list: VecDeque = VecDeque::new(); + for i in 0..vec.len() { + succession_list.push_back(vec.get(i)); + } + + if succession_list != room.owner_succession { + room.owner_succession = succession_list; + has_changed = true; + } + } + } let new_binattr; if let Some(vec) = req.roomBinAttrInternal() { @@ -1292,44 +1331,36 @@ impl RoomManager { vec_new_binattr.push(id); } new_binattr = Some(vec_new_binattr); + has_changed = true; } else { new_binattr = None; } - // Group stuff TODO + if has_changed { + // Build the notification buffer + let mut builder = flatbuffers::FlatBufferBuilder::with_capacity(1024); + let room_data_internal = room.to_RoomDataInternal(&mut builder); + let fb_new_binattr = new_binattr.map(|vec_new_binattr| builder.create_vector(&vec_new_binattr)); + + let resp = RoomDataInternalUpdateInfo::create( + &mut builder, + &RoomDataInternalUpdateInfoArgs { + newRoomDataInternal: Some(room_data_internal), + prevFlagAttr: old_flag_attr, + prevRoomPasswordSlotMask: old_password_slot_mask, + newRoomGroup: None, // TODO + newRoomBinAttrInternal: fb_new_binattr, + }, + ); + builder.finish(resp, None); - let old_password_slot_mask = room.password_slot_mask; - room.password_slot_mask = req.passwordSlotMask(); + let mut to_notif = room.get_room_user_ids(); + to_notif.remove(&user_id); - if let Some(vec) = req.ownerPrivilegeRank() { - let mut succession_list: VecDeque = VecDeque::new(); - for i in 0..vec.len() { - succession_list.push_back(vec.get(i)); - } - room.owner_succession = succession_list; + return Ok(Some((to_notif, builder.finished_data().to_vec()))); } - // Build the notification buffer - let mut builder = flatbuffers::FlatBufferBuilder::with_capacity(1024); - let room_data_internal = room.to_RoomDataInternal(&mut builder); - let fb_new_binattr = new_binattr.map(|vec_new_binattr| builder.create_vector(&vec_new_binattr)); - - let resp = RoomDataInternalUpdateInfo::create( - &mut builder, - &RoomDataInternalUpdateInfoArgs { - newRoomDataInternal: Some(room_data_internal), - prevFlagAttr: old_flag_attr, - prevRoomPasswordSlotMask: old_password_slot_mask, - newRoomGroup: None, // TODO - newRoomBinAttrInternal: fb_new_binattr, - }, - ); - builder.finish(resp, None); - - let mut to_notif = room.get_room_user_ids(); - to_notif.remove(&user_id); - - Ok((to_notif, builder.finished_data().to_vec())) + Ok(None) } pub fn get_roommemberdata_internal(&self, com_id: &ComId, req: &GetRoomMemberDataInternalRequest) -> Result, ErrorType> {