From aac27b02754e2d8b8152a062c112842d9f22478a Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Fri, 21 May 2021 15:47:53 +0200 Subject: fix potential panic when getting usernames while disconnected --- mumd/src/state.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'mumd/src') diff --git a/mumd/src/state.rs b/mumd/src/state.rs index 87a5a0e..ce915ac 100644 --- a/mumd/src/state.rs +++ b/mumd/src/state.rs @@ -300,12 +300,15 @@ impl State { .1 .channel() } - fn get_user_name(&self, user: u32) -> String { + + /// Gets the username of a user with id `user` connected to the same server that we are connected to. + /// If we are connected to the server but the user with the id doesn't exist, the string "Unknown user {id}" + /// is returned instead. If we aren't connected to a server, None is returned instead. + fn get_user_name(&self, user: u32) -> Option { self.server() - .unwrap() - .users() - .get(&user).map(|e| e.name().to_string()) - .unwrap_or(format!("Unknown user {}", user)) + .map(|e| e.users() + .get(&user).map(|e| e.name().to_string()) + .unwrap_or(format!("Unknown user {}", user))) } } -- cgit v1.2.1