diff --git a/Cargo.toml b/Cargo.toml index f405020..e430c89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] + diff --git a/config.toml b/config.toml index ac31732..1499bf1 100644 --- a/config.toml +++ b/config.toml @@ -1,5 +1,5 @@ channels = ["#main", "#circe"] -host = "192.168.178.100" +host = "192.168.1.28" mode = "+B" nickname = "circe" port = 6667 diff --git a/src/lib.rs b/src/lib.rs index 63167cc..c0541db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, } @@ -68,23 +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"))] + #[cfg(feature = "tls")] let sslstream: native_tls::TlsStream; - #[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 { 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 }; - #[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(()) } } diff --git a/src/main.rs b/src/main.rs index e6e96f1..bd720ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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();