347 } else { |
361 } else { |
348 Err(LeaveRoomError::NoRoom) |
362 Err(LeaveRoomError::NoRoom) |
349 } |
363 } |
350 } |
364 } |
351 |
365 |
|
366 pub fn change_master( |
|
367 &mut self, |
|
368 client_id: ClientId, |
|
369 room_id: RoomId, |
|
370 new_master_nick: String, |
|
371 ) -> Result<ChangeMasterResult, ChangeMasterError> { |
|
372 let client = &mut self.clients[client_id]; |
|
373 let room = &mut self.rooms[room_id]; |
|
374 |
|
375 if client.is_admin() || room.master_id == Some(client_id) { |
|
376 let new_master_id = self |
|
377 .clients |
|
378 .iter() |
|
379 .find(|(_, c)| c.nick == new_master_nick) |
|
380 .map(|(id, _)| id); |
|
381 |
|
382 match new_master_id { |
|
383 Some(new_master_id) if new_master_id == client_id => { |
|
384 Err(ChangeMasterError::AlreadyMaster) |
|
385 } |
|
386 Some(new_master_id) => { |
|
387 let new_master = &mut self.clients[new_master_id]; |
|
388 if new_master.room_id == Some(room_id) { |
|
389 self.clients[new_master_id].set_is_master(true); |
|
390 let old_master_id = room.master_id; |
|
391 if let Some(master_id) = old_master_id { |
|
392 self.clients[master_id].set_is_master(false); |
|
393 } |
|
394 room.master_id = Some(new_master_id); |
|
395 Ok(ChangeMasterResult { |
|
396 old_master_id, |
|
397 new_master_id, |
|
398 }) |
|
399 } else { |
|
400 Err(ChangeMasterError::ClientNotInRoom) |
|
401 } |
|
402 } |
|
403 None => Err(ChangeMasterError::NoClient), |
|
404 } |
|
405 } else { |
|
406 Err(ChangeMasterError::NoAccess) |
|
407 } |
|
408 } |
|
409 |
352 #[inline] |
410 #[inline] |
353 pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> { |
411 pub fn set_var(&mut self, client_id: ClientId, var: ServerVar) -> Result<(), AccessError> { |
354 if self.clients[client_id].is_admin() { |
412 if self.clients[client_id].is_admin() { |
355 match var { |
413 match var { |
356 ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg, |
414 ServerVar::MOTDNew(msg) => self.greetings.for_latest_protocol = msg, |