From ce158fc6129ee5c274c2c6e005bc7e9d8b494829 Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Sat, 27 Jan 2018 20:26:56 +0100 Subject: [PATCH] Made unspecified server or nickname into errors instead of simply panics. --- src/client/conn.rs | 4 ++-- src/client/data/config.rs | 17 ++++++++--------- src/client/server/mod.rs | 9 ++++++--- src/client/server/utils.rs | 2 +- src/error.rs | 11 ++++++++++- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/client/conn.rs b/src/client/conn.rs index 22548a6..106a539 100644 --- a/src/client/conn.rs +++ b/src/client/conn.rs @@ -108,7 +108,7 @@ impl Connection { if config.use_mock_connection() { Ok(ConnectionFuture::Mock(config)) } else if config.use_ssl() { - let domain = format!("{}", config.server()); + let domain = format!("{}", config.server()?); info!("Connecting via SSL to {}.", domain); let mut builder = TlsConnector::builder()?; if let Some(cert_path) = config.cert_path() { @@ -133,7 +133,7 @@ impl Connection { )); Ok(ConnectionFuture::Secured(config, stream)) } else { - info!("Connecting to {}.", config.server()); + info!("Connecting to {}.", config.server()?); Ok(ConnectionFuture::Unsecured( config, TcpStream::connect(&config.socket_addr()?, handle), diff --git a/src/client/data/config.rs b/src/client/data/config.rs index 5833d29..2d34c3f 100644 --- a/src/client/data/config.rs +++ b/src/client/data/config.rs @@ -259,9 +259,8 @@ impl Config { } /// Gets the nickname specified in the configuration. - /// This will panic if not specified. - pub fn nickname(&self) -> &str { - self.nickname.as_ref().map(|s| &s[..]).unwrap() + pub fn nickname(&self) -> Result<&str> { + self.nickname.as_ref().map(|s| &s[..]).ok_or(error::ErrorKind::NicknameNotSpecified.into()) } /// Gets the bot's nickserv password specified in the configuration. @@ -282,19 +281,19 @@ impl Config { /// Gets the username specified in the configuration. /// This defaults to the user's nickname when not specified. pub fn username(&self) -> &str { - self.username.as_ref().map_or(self.nickname(), |s| &s) + self.username.as_ref().map_or(self.nickname().unwrap_or("user"), |s| &s) } /// Gets the real name specified in the configuration. /// This defaults to the user's nickname when not specified. pub fn real_name(&self) -> &str { - self.realname.as_ref().map_or(self.nickname(), |s| &s) + self.realname.as_ref().map_or(self.nickname().unwrap_or("irc"), |s| &s) } /// Gets the address of the server specified in the configuration. - /// This panics when not specified. - pub fn server(&self) -> &str { - self.server.as_ref().map(|s| &s[..]).unwrap() + pub fn server(&self) -> Result<&str> { + self.server.as_ref().map(|s| &s[..]).ok_or(error::ErrorKind::NicknameNotSpecified.into()) + } /// Gets the port of the server specified in the configuration. @@ -310,7 +309,7 @@ impl Config { /// Gets the server and port as a `SocketAddr`. /// This panics when server is not specified or the address is malformed. pub fn socket_addr(&self) -> Result { - format!("{}:{}", self.server(), self.port()).to_socket_addrs() + format!("{}:{}", self.server()?, self.port()).to_socket_addrs() .map(|mut i| i.next().unwrap()) .map_err(|e| e.into()) } diff --git a/src/client/server/mod.rs b/src/client/server/mod.rs index 166ac05..4537fba 100644 --- a/src/client/server/mod.rs +++ b/src/client/server/mod.rs @@ -326,7 +326,9 @@ impl ServerState { let alt_nicks = self.config().alternate_nicknames(); let index = self.alt_nick_index.read().unwrap(); match *index { - 0 => self.config().nickname(), + 0 => self.config().nickname().expect( + "current_nickname should not be callable if nickname is not defined." + ), i => alt_nicks[i - 1], } } @@ -348,6 +350,7 @@ impl ServerState { trace!("[RECV] {}", msg.to_string()); match msg.command { JOIN(ref chan, _, _) => self.handle_join(msg.source_nickname().unwrap_or(""), chan), + /// This will panic if not specified. PART(ref chan, _) => self.handle_part(msg.source_nickname().unwrap_or(""), chan), QUIT(_) => self.handle_quit(msg.source_nickname().unwrap_or("")), NICK(ref new_nick) => { @@ -420,12 +423,12 @@ impl ServerState { self.send(NICKSERV(format!( "{} {} {}", seq, - self.config().nickname(), + self.config().nickname()?, self.config().nick_password() )))?; } *index = 0; - self.send(NICK(self.config().nickname().to_owned()))? + self.send(NICK(self.config().nickname()?.to_owned()))? } self.send(NICKSERV( format!("IDENTIFY {}", self.config().nick_password()), diff --git a/src/client/server/utils.rs b/src/client/server/utils.rs index e5cd779..8105c19 100644 --- a/src/client/server/utils.rs +++ b/src/client/server/utils.rs @@ -95,7 +95,7 @@ pub trait ServerExt: Server { if self.config().password() != "" { self.send(PASS(self.config().password().to_owned()))?; } - self.send(NICK(self.config().nickname().to_owned()))?; + self.send(NICK(self.config().nickname()?.to_owned()))?; self.send(USER( self.config().username().to_owned(), "0".to_owned(), diff --git a/src/error.rs b/src/error.rs index e68a99f..e4fea6b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,4 @@ //! Errors for `irc` crate using `error_chain`. - #![allow(missing_docs)] error_chain! { @@ -54,5 +53,15 @@ error_chain! { description("The connection timed out due to no ping response.") display("The connection timed out due to no ping response.") } + + NicknameNotSpecified { + description("No nickname was specified for use with this IrcServer.") + display("No nickname was specified for use with this IrcServer.") + } + + ServerNotSpecified { + description("No server was specified to connect to.") + display("No server was specified to connect to.") + } } }