From 5d47cf680b5c4dc14f9a09b90007c9844e80f452 Mon Sep 17 00:00:00 2001 From: famfo Date: Thu, 11 Nov 2021 19:30:10 +0100 Subject: [PATCH] Added crate feature no-tls --- Cargo.toml | 3 ++ src/lib.rs | 89 ++++++++++++++++++++++++++--------------------------- src/main.rs | 1 - 3 files changed, 46 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2f551bc..f405020 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,6 @@ toml = "0.5" serde_derive = "1.0" serde = "1.0" native-tls = "0.2" + +[features] +tls = [] diff --git a/src/lib.rs b/src/lib.rs index a2dfdf5..63167cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,8 @@ //! } #![warn(missing_docs)] + +#[cfg(not(feature = "not-tls"))] use native_tls::TlsConnector; use std::borrow::Cow; @@ -37,8 +39,10 @@ pub mod commands; /// An IRC client pub struct Client { config: Config, - stream: Option, - sslstream: Option>, + #[cfg(feature = "no-tls")] + stream: TcpStream, + #[cfg(not(feature = "not-tls"))] + stream: native_tls::TlsStream, } /// Config for the IRC client @@ -49,7 +53,6 @@ pub struct Config { mode: Option, nickname: Option, port: u16, - ssl: bool, username: String, } @@ -65,24 +68,23 @@ impl Client { /// Returns error if the client could not connect to the host. pub fn new(config: Config) -> Result { let stream = TcpStream::connect(format!("{}:{}", config.host, config.port))?; + #[cfg(not(feature = "not-tls"))] let sslstream: native_tls::TlsStream; - if config.ssl { + #[cfg(not(feature = "not-tls"))] + { let connector = TlsConnector::new().unwrap(); sslstream = connector.connect(config.host.as_str(), stream).unwrap(); - - return Ok(Self { - config, - stream: None, - sslstream: Some(sslstream), - }); - } else { - return Ok(Self { - config, - stream: Some(stream), - sslstream: None, - }); } + + #[cfg(not(feature = "not-tls"))] + return Ok(Self { + config, + stream: sslstream, + }); + + #[cfg(feature = "no-tls")] + return Self { config, stream }; } /// Identify user and joins the in the [`Config`] specified channels. @@ -138,17 +140,17 @@ impl Client { fn read_string(&mut self) -> Option { let mut buffer = [0u8; 512]; - if self.config.ssl { - match self.sslstream.as_mut().unwrap().read(&mut buffer) { - Ok(_) => {} - Err(_) => return None, - }; - } else { - match self.stream.as_mut().unwrap().read(&mut buffer) { - Ok(_) => {} - Err(_) => return None, - }; - } + #[cfg(not(feature = "not-tls"))] + match self.stream.read(&mut buffer) { + Ok(_) => {} + Err(_) => return None, + }; + + #[cfg(feature = "no-tls")] + match self.stream.as_mut().unwrap().read(&mut buffer) { + Ok(_) => {} + Err(_) => return None, + }; Some(String::from_utf8_lossy(&buffer).into()) } @@ -191,15 +193,11 @@ impl Client { Cow::Owned(new) as Cow }; - if self.config.ssl { - self.sslstream - .as_mut() - .unwrap() - .write(formatted.as_bytes()) - .unwrap(); - } else { - self.stream.as_mut().unwrap().write(formatted.as_bytes())?; - } + #[cfg(not(feature = "not-tls"))] + self.stream.write(formatted.as_bytes()).unwrap(); + + #[cfg(feature = "no-tls")] + self.stream.as_mut().unwrap().write(formatted.as_bytes())?; Ok(()) } @@ -507,14 +505,15 @@ impl Client { env!("CARGO_PKG_VERSION") )))?; } - if self.config.ssl { - self.sslstream.as_mut().unwrap().shutdown().unwrap(); - } else { - self.stream - .as_mut() - .unwrap() - .shutdown(std::net::Shutdown::Both)?; - } + + #[cfg(not(feature = "not-tls"))] + self.stream.shutdown().unwrap(); + + #[cfg(feature = "no-tls")] + self.stream + .as_mut() + .unwrap() + .shutdown(std::net::Shutdown::Both)?; Ok(()) } @@ -540,7 +539,6 @@ impl Config { mode: Option<&'static str>, nickname: Option<&'static str>, port: u16, - ssl: bool, username: &str, ) -> Self { // Conversion from &'static str to String @@ -566,7 +564,6 @@ impl Config { mode: mode_config, nickname: nickname_config, port, - ssl, username: username.into(), } } diff --git a/src/main.rs b/src/main.rs index e267bb2..e6e96f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,6 @@ fn main() -> Result<(), std::io::Error> { Some("+B"), Some("circe"), 6697, - true, "circe", ); let mut client = Client::new(config).unwrap();