From 825b5576090156a4ec7746c4b91404eee37af67d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 3 Sep 2014 18:13:59 -0400 Subject: start working on an irc client class --- src/client.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/client.rs diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..9dd5191 --- /dev/null +++ b/src/client.rs @@ -0,0 +1,42 @@ +use std::{io, str}; + +use message::Message; + +pub struct Client { + nick: String, + host: String, + port: u16, + + connection: Option>, +} + +impl Client { + pub fn new (nick: &str, host: &str, port: u16) -> Client { + Client { + nick: nick.to_string(), + host: host.to_string(), + port: port, + connection: None, + } + } + + pub fn connect (&mut self) { + let mut stream = io::TcpStream::connect(self.host.as_slice(), self.port); + self.connection = Some(io::BufferedStream::new(stream.unwrap())); + } + + pub fn read (&mut self) -> Message { + // \n isn't valid inside a message, so this should be fine. if the \n + // we find isn't preceded by a \r, this will be caught by the message + // parser. + match self.connection { + Some(ref mut conn) => { + let buf = conn.read_until(b'\n'); + // XXX handle different encodings + // XXX proper error handling + Message::parse(str::from_utf8(buf.unwrap().as_slice()).unwrap()).unwrap() + }, + None => fail!(), + } + } +} -- cgit v1.2.3-54-g00ecf