Switch to default feature tls

This commit is contained in:
Yash Karandikar 2021-11-11 20:12:57 -06:00
parent 5d47cf680b
commit 6ddf52915e
Signed by: karx
GPG key ID: A794DA2529474BA5
4 changed files with 24 additions and 39 deletions

View file

@ -15,7 +15,12 @@ exclude = ["src/main.rs", "config.toml"]
toml = "0.5"
serde_derive = "1.0"
serde = "1.0"
native-tls = "0.2"
[dependencies.native-tls]
version= "0.2"
optional = true
[features]
tls = []
default = ["tls"]
tls = ["native-tls"]

View file

@ -1,5 +1,5 @@
channels = ["#main", "#circe"]
host = "192.168.178.100"
host = "192.168.1.28"
mode = "+B"
nickname = "circe"
port = 6667

View file

@ -22,7 +22,7 @@
#![warn(missing_docs)]
#[cfg(not(feature = "not-tls"))]
#[cfg(feature = "tls")]
use native_tls::TlsConnector;
use std::borrow::Cow;
@ -39,9 +39,9 @@ pub mod commands;
/// An IRC client
pub struct Client {
config: Config,
#[cfg(feature = "no-tls")]
#[cfg(not(feature = "tls"))]
stream: TcpStream,
#[cfg(not(feature = "not-tls"))]
#[cfg(feature = "tls")]
stream: native_tls::TlsStream<TcpStream>,
}
@ -68,23 +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"))]
#[cfg(feature = "tls")]
let sslstream: native_tls::TlsStream<TcpStream>;
#[cfg(not(feature = "not-tls"))]
#[cfg(feature = "tls")]
{
let connector = TlsConnector::new().unwrap();
sslstream = connector.connect(config.host.as_str(), stream).unwrap();
}
};
#[cfg(not(feature = "not-tls"))]
#[cfg(feature = "tls")]
return Ok(Self {
config,
stream: sslstream,
});
#[cfg(feature = "no-tls")]
return Self { config, stream };
#[cfg(not(feature = "tls"))]
return Ok(Self { config, stream });
}
/// Identify user and joins the in the [`Config`] specified channels.
@ -119,6 +119,7 @@ impl Client {
self.write_command(commands::Command::PONG(code.to_string()))?;
}
commands::Command::OTHER(line) => {
println!("{}", line);
if line.contains("001") {
break;
}
@ -140,18 +141,11 @@ impl Client {
fn read_string(&mut self) -> Option<String> {
let mut buffer = [0u8; 512];
#[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())
}
@ -193,11 +187,7 @@ impl Client {
Cow::Owned(new) as Cow<str>
};
#[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())?;
self.stream.write(formatted.as_bytes())?;
Ok(())
}
@ -506,15 +496,13 @@ impl Client {
)))?;
}
#[cfg(not(feature = "not-tls"))]
self.stream.shutdown().unwrap();
#[cfg(feature = "no-tls")]
#[cfg(not(feature = "tls"))]
self.stream
.as_mut()
.unwrap()
.shutdown(std::net::Shutdown::Both)?;
#[cfg(feature = "tls")]
self.stream.shutdown()?;
Ok(())
}
}

View file

@ -10,15 +10,7 @@ macro_rules! loop_n {
}
fn main() -> Result<(), std::io::Error> {
//let config = Config::from_toml("config.toml")?;
let config = Config::new(
vec!["#main", "#no-normies"],
"karx.xyz",
Some("+B"),
Some("circe"),
6697,
"circe",
);
let config = Config::from_toml("config.toml")?;
let mut client = Client::new(config).unwrap();
client.identify().unwrap();