summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-09-03 18:13:59 -0400
committerJesse Luehrs <doy@tozt.net>2014-09-03 18:13:59 -0400
commit825b5576090156a4ec7746c4b91404eee37af67d (patch)
treeee2e2a20a2d30cb7d6b7e9ed3e50d7f7eaca4f06
parentc314eb730bf080480ef3aca41227a2cd2e54b8fe (diff)
downloadrust-irc-825b5576090156a4ec7746c4b91404eee37af67d.tar.gz
rust-irc-825b5576090156a4ec7746c4b91404eee37af67d.zip
start working on an irc client class
-rw-r--r--src/client.rs42
1 files changed, 42 insertions, 0 deletions
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<io::BufferedStream<io::TcpStream>>,
+}
+
+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!(),
+ }
+ }
+}