summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-09-04 11:54:28 -0400
committerJesse Luehrs <doy@tozt.net>2014-09-04 11:54:28 -0400
commita17de7ec09e41789d469a5c939894c54f494ad4e (patch)
tree2d04e018b6bad9f2eb8dc527da453c4949b89002
parenta9b5293a5f1d8c5333f5ebd4a0b153755edd00b7 (diff)
downloadrust-irc-a17de7ec09e41789d469a5c939894c54f494ad4e.tar.gz
rust-irc-a17de7ec09e41789d469a5c939894c54f494ad4e.zip
move connection initialization into the client
-rw-r--r--examples/client.rs25
-rw-r--r--src/client.rs94
-rw-r--r--src/lib.rs2
3 files changed, 72 insertions, 49 deletions
diff --git a/examples/client.rs b/examples/client.rs
index 90a6b32..98900ef 100644
--- a/examples/client.rs
+++ b/examples/client.rs
@@ -1,30 +1,7 @@
extern crate irc;
-use irc::constants::{CommandMessage, Nick, User};
-
fn main () {
- let mut client = irc::Client::new("doytest", "chat.freenode.net", 6667);
- client.connect();
-
- client.write(
- irc::Message::new(
- None,
- CommandMessage(Nick),
- vec!["doytest".to_string()],
- )
- );
- client.write(
- irc::Message::new(
- None,
- CommandMessage(User),
- vec![
- "doytest".to_string(),
- "localhost".to_string(),
- "localhost".to_string(),
- "doytest".to_string(),
- ],
- )
- );
+ let mut client = irc::ClientBuilder::new("doytest", "chat.freenode.net").connect();
loop {
let res = client.read();
diff --git a/src/client.rs b/src/client.rs
index 86c3857..b26b078 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -1,51 +1,97 @@
use std::{io, str};
+use constants::{CommandMessage, Nick, User};
use message::Message;
-pub struct Client {
+pub struct ClientBuilder {
nick: String,
+ pass: Option<String>,
+ realname: String,
+ username: String,
+
host: String,
port: u16,
+}
- connection: Option<io::BufferedStream<io::TcpStream>>,
+pub struct Client {
+ conn: io::BufferedStream<io::TcpStream>,
}
-impl Client {
- pub fn new (nick: &str, host: &str, port: u16) -> Client {
- Client {
+impl ClientBuilder {
+ pub fn new (nick: &str, host: &str) -> ClientBuilder {
+ ClientBuilder {
nick: nick.to_string(),
+ pass: None,
+ realname: nick.to_string(),
+ username: nick.to_string(),
+
host: host.to_string(),
- port: port,
- connection: None,
+ port: 6667,
}
}
- pub fn connect (&mut self) {
+ pub fn set_pass (&mut self, pass: &str) -> &mut ClientBuilder {
+ self.pass = Some(pass.to_string());
+ self
+ }
+
+ pub fn set_username (&mut self, username: &str) -> &mut ClientBuilder {
+ self.username = username.to_string();
+ self
+ }
+
+ pub fn set_realname (&mut self, realname: &str) -> &mut ClientBuilder {
+ self.realname = realname.to_string();
+ self
+ }
+
+ pub fn set_port (&mut self, port: u16) -> &mut ClientBuilder {
+ self.port = port;
+ self
+ }
+
+ pub fn connect (&mut self) -> Client {
+ let mut client = self.connect_raw();
+ client.write(
+ Message::new(
+ None,
+ CommandMessage(Nick),
+ vec![self.nick.clone()],
+ )
+ );
+ client.write(
+ Message::new(
+ None,
+ CommandMessage(User),
+ vec![
+ self.username.clone(),
+ "localhost".to_string(), // XXX
+ "localhost".to_string(), // XXX
+ self.realname.clone(),
+ ],
+ )
+ );
+ client
+ }
+
+ pub fn connect_raw (&mut self) -> Client {
let mut stream = io::TcpStream::connect(self.host.as_slice(), self.port);
- self.connection = Some(io::BufferedStream::new(stream.unwrap()));
+ Client { conn: io::BufferedStream::new(stream.unwrap()) }
}
+}
+impl Client {
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!(),
- }
+ let buf = self.conn.read_until(b'\n');
+ // XXX handle different encodings
+ // XXX proper error handling
+ Message::parse(str::from_utf8(buf.unwrap().as_slice()).unwrap()).unwrap()
}
pub fn write (&mut self, msg: Message) {
- match self.connection {
- Some(ref mut conn) => {
- msg.write_protocol_string(conn);
- },
- None => fail!(),
- }
+ msg.write_protocol_string(&mut self.conn);
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 8142ee8..847f013 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,7 +3,7 @@
#[phase(plugin)] extern crate regex_macros;
extern crate regex;
-pub use client::Client;
+pub use client::{Client, ClientBuilder};
pub use message::Message;
pub mod client;