From 9f878dd5785d8493171f9d9d510c68e2a107573d Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Sun, 31 Oct 2021 11:11:53 -0500 Subject: [PATCH] Add support for reading from the socket --- .gitignore | 1 - Cargo.lock | 49 +++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/lib.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 Cargo.lock diff --git a/.gitignore b/.gitignore index 96ef6c0..ea8c4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ /target -Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..66f1507 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,49 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "irsc" +version = "0.1.0" +dependencies = [ + "lazy_static", + "regex", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "regex" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" diff --git a/Cargo.toml b/Cargo.toml index e17f4fb..37d1bf0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +regex = "1" +lazy_static = "1.4.0" diff --git a/src/lib.rs b/src/lib.rs index a60594e..e646d2b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,84 @@ use std::net::TcpStream; -use std::io::Error; +use std::io::{Error, Read, Write}; +use std::time::Duration; +use lazy_static::lazy_static; +use regex::Regex; + +lazy_static! { + static ref PING_RE: Regex = Regex::new(r"^PING\s*:").unwrap(); +} pub struct Client { - stream: TcpStream + stream: TcpStream, + config: Config +} + +pub struct Config { + username: String, + nickname: String, + mode: String +} + +pub enum Command { + PING(String) +} + + +impl Command { + fn from_str(s: &str) -> Option { + let new = s.trim(); + + if new.contains("PING") { + let command = String::from(PING_RE.replace_all(new, "")); + + return Some(Self::PING(command)); + } + + None + } } impl Client { - pub fn new(host: &str, port: &str) -> Result { - let mut stream = TcpStream::connect(format!("{}:{}", host, port))?; + pub fn new(host: &str, port: u16, config: Config) -> Result { + let stream = TcpStream::connect(format!("{}:{}", host, port))?; - Ok(Self { stream }) + Ok(Self { stream, config }) + } + + // temporarily pub, change this later + pub fn read_string(&mut self) -> Option { + let mut buffer = [0u8; 512]; + + match self.stream.read(&mut buffer) { + Ok(_) => {}, + Err(_) => return None + }; + + Some(String::from_utf8_lossy(&buffer).into()) + } + + pub fn read(&mut self) -> Option { + if let Some(string) = self.read_string() { + return Command::from_str(&string); + } + + None // if it's none, there's no new messages + } + + pub fn write(&mut self, data: &str) -> Result<(), Error> { + self.stream.write_all(data.as_bytes())?; + // self.stream.flush()?; + + Ok(()) + } +} + +impl Config { + pub fn new(username: &str, nickname: Option<&str>, mode: Option<&str>) -> Self { + Self { + username: username.into(), + nickname: nickname.unwrap_or(username).into(), + mode: mode.unwrap_or("").into() + } } } \ No newline at end of file