diff --git a/src/lib.rs b/src/lib.rs index dacec32..7ecadfd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,28 +13,32 @@ pub struct Config { mode: String, } +#[derive(Debug)] pub enum CapMode { LS, END, } +#[derive(Debug)] pub enum Command { PING(String), + PONG(String), CAP(CapMode), USER(String, String, String, String), NICK(String), + OTHER(String), } impl Command { - fn from_str(s: &str) -> Option { + fn from_str(s: &str) -> Self { let new = s.trim(); if new.starts_with("PING") { let command: String = String::from(new.split_whitespace().collect::>()[1]); - return Some(Self::PING(command)); + return Self::PING(command); } - None + Self::OTHER(new.to_string()) } } @@ -70,12 +74,12 @@ impl Client { Some(String::from_utf8_lossy(&buffer).into()) } - pub fn read(&mut self) -> Option { + pub fn read(&mut self) -> Result { if let Some(string) = self.read_string() { - return Command::from_str(&string); + return Ok(Command::from_str(&string)); } - None // if it's none, there's no new messages + Err(()) } fn write(&mut self, data: &str) -> Result<(), Error> { @@ -107,10 +111,20 @@ impl Client { let formatted = format!("NICK {}", nickname); Cow::Owned(formatted) as Cow } - PING(ping) => { - let formatted = format!("PONG {}", ping); + PING(code) => { + let formatted = format!("PING {}", code); Cow::Owned(formatted) as Cow } + PONG(code) => { + let formatted = format!("PONG {}", code); + Cow::Owned(formatted) as Cow + } + OTHER(_) => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Cannot write commands of type OTHER", + )); + } }; self.write(&computed)?; diff --git a/src/main.rs b/src/main.rs index 4188dbe..97bd6b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use irsc::{Client, Config}; +use irsc::{Client, Command, Config}; #[allow(unused_macros)] macro_rules! loop_n { @@ -12,12 +12,17 @@ macro_rules! loop_n { fn main() -> Result<(), std::io::Error> { let config = Config::new("test", Some("test"), Some("+B")); let mut client = - Client::new("192.168.178.100", 6667, config).expect("Unable to connect to IRC server"); + Client::new("192.168.1.28", 6667, config).expect("Unable to connect to IRC server"); client.identify()?; loop { - if let Some(line) = client.read_string() { - print!("{}", line); + if let Ok(ref command) = client.read() { + if let Command::PING(code) = command { + client.write_command(Command::PONG(code.to_string()))?; + } + if let Command::OTHER(line) = command { + print!("{}", line); + } } } }