From 52b6422e1ab6bb5020554f5f797e332e09dbe27d Mon Sep 17 00:00:00 2001 From: David Westgate Date: Wed, 22 Nov 2023 19:12:10 -0800 Subject: [PATCH] start of client app --- Cargo.lock | 9 ++++++++ Cargo.toml | 1 + src/client.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 23ee3c1..92a5130 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "prompted" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34cf5bf48e2fe6e11fb6b619fb5962530f1eaf278709de5f8ea49afa7379f9b" + [[package]] name = "rust-irc" version = "0.1.0" +dependencies = [ + "prompted", +] diff --git a/Cargo.toml b/Cargo.toml index cba6775..542f364 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +prompted = "0.2.8" diff --git a/src/client.rs b/src/client.rs index 4628d37..e5aca05 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,64 @@ +use prompted::input; +use std::io::{self, Read, Write}; +use std::net::TcpStream; +use std::thread; + +fn read_messages(mut stream: TcpStream) { + let mut buffer: [u8; 1024] = [0; 1024]; + loop { + match stream.read(&mut buffer) { + Ok(size) => { + if size == 0 { + break; //Server closed connection + } + let message: &[u8] = &buffer[..size]; + process_message(message); + } + Err(_) => { + break; + } + } + } +} + +fn process_message(message: &[u8]) { + if let Ok(text) = String::from_utf8(message.to_vec()) { + println!("{}", text); + } +} + pub fn start() { println!("Starting the IRC client"); - todo!(); + // let nick: String = input!("Enter your nickname: "); + let nick: String = "testy".to_string(); + // let host: String = input!("Enter the server host: "); + let host: &str = "localhost"; + + if let Ok(mut stream) = TcpStream::connect(host.to_owned() + ":6667") { + println!("Connected to {}", host); + + let stream_clone: TcpStream = stream.try_clone().expect("Faile to clone stream"); + thread::spawn(move || { + read_messages(stream_clone); + }); + + loop { + let cmd: String = input!(":"); + match cmd.trim() { + "/quit" => {} + "/list" => {} + "/msq" => {} + "/join" => {} + "/show" => {} + "/leave" => {} + "/msg" => {} + + _ => { + stream.write(cmd.as_bytes()); + } + } + } + } else { + println!("Failed to connect to {} with nickname {} ", host, nick); + } }