Added crate feature no-tls

This commit is contained in:
famfo 2021-11-11 19:30:10 +01:00 committed by Yash Karandikar
parent bb3d7059b9
commit 5d47cf680b
Signed by: karx
GPG key ID: A794DA2529474BA5
3 changed files with 46 additions and 47 deletions

View file

@ -16,3 +16,6 @@ toml = "0.5"
serde_derive = "1.0"
serde = "1.0"
native-tls = "0.2"
[features]
tls = []

View file

@ -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<TcpStream>,
sslstream: Option<native_tls::TlsStream<TcpStream>>,
#[cfg(feature = "no-tls")]
stream: TcpStream,
#[cfg(not(feature = "not-tls"))]
stream: native_tls::TlsStream<TcpStream>,
}
/// Config for the IRC client
@ -49,7 +53,6 @@ pub struct Config {
mode: Option<String>,
nickname: Option<String>,
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<Self, Error> {
let stream = TcpStream::connect(format!("{}:{}", config.host, config.port))?;
#[cfg(not(feature = "not-tls"))]
let sslstream: native_tls::TlsStream<TcpStream>;
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<String> {
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<str>
};
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(),
}
}

View file

@ -17,7 +17,6 @@ fn main() -> Result<(), std::io::Error> {
Some("+B"),
Some("circe"),
6697,
true,
"circe",
);
let mut client = Client::new(config).unwrap();