From 6eac9deb6231ee345d2d19f54761a4820f0c345b Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Mon, 9 Jun 2014 16:06:32 -0400 Subject: tmp --- Makefile | 4 ++-- examples/client.rs | 30 ++++++++++++++++-------------- src/lib.rs | 26 ++++++++++++-------------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index 87397df..199af57 100644 --- a/Makefile +++ b/Makefile @@ -41,9 +41,9 @@ SOURCE_FILES = $(shell test -e src/ && find src -type f) COMPILER = rustc # For release: - COMPILER_FLAGS = -O +# COMPILER_FLAGS = -O # For debugging: -# COMPILER_FLAGS = -g + COMPILER_FLAGS = -g --opt-level=0 RUSTDOC = rustdoc diff --git a/examples/client.rs b/examples/client.rs index 6ec9134..d37bb40 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -1,25 +1,27 @@ extern crate irc; -use std::io::net::tcp::TcpStream; -use std::io::BufferedReader; +use std::io::stdio; -use irc::IrcConnection; +use irc::IrcClient; use irc::msg::Message; -use irc::msg::cmd; +//use irc::msg::cmd; fn main() { - let message = Message { - prefix: None, - command: cmd::PrivMsg("#rust".to_string(), "Hi there everyone".to_string()), - }; - - println!("{}", message); + let mut stderr = stdio::stderr(); + + let mut args = std::os::args().move_iter(); + args.next(); + let host = args.next().expect("No hostname passed"); + let port: u16 = from_str(args.next().unwrap_or_else(|| { let _ = writeln!(stderr, "No port given. Assuming 6667."); "6667".to_string() }).as_slice()) + .expect("Port must be a number"); + + drop(args); - let on_msg = |message: &Message, _sender: &Sender| { + let mut connection = IrcClient::connect(host.as_slice(), port, "Dr-Emann".to_string(), "dremann".to_string(), "Zachary Dremann".to_string()).unwrap(); + + let on_msg = |message: &Message| { println!("{}", *message); }; - let mut connection = IrcConnection::connect("irc.mozilla.org", 6667, "Dr-Emann".to_string(), "dremann".to_string(), "Zachary Dremann".to_string(), on_msg).unwrap(); - - connection.run_loop(); + connection.run_loop(on_msg); } diff --git a/src/lib.rs b/src/lib.rs index 8b385ec..c367185 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,23 +11,20 @@ use msg::cmd; pub mod msg; -pub struct IrcConnection<'a> { +pub struct IrcClient { stream: TcpStream, output_sender: Sender, - msg_callback: |&Message, &Sender|: 'a -> () } -impl<'a> IrcConnection<'a> { - pub fn connect<'b>( - host: &str, port: u16, nick: String, username: String, - real_name: String, msg_callback: |&Message, &Sender|: 'b -> ()) -> IoResult> { +impl IrcClient { + pub fn connect( + host: &str, port: u16, nick: String, username: String, real_name: String) -> IoResult { let (send_writer, rec_writer) = channel(); - let mut connection = IrcConnection { + let mut connection = IrcClient{ stream: try!(TcpStream::connect(host, port)), output_sender: send_writer.clone(), - msg_callback: msg_callback, }; let writer = connection.stream.clone(); @@ -36,12 +33,12 @@ impl<'a> IrcConnection<'a> { spawn(proc() { let mut writer = writer; for msg in rec_writer.iter() { - (write!(writer, "{}", msg)).ok().expect("Unable to write to stream"); + (write!(writer, "{}\r\n", msg)).ok().expect("Unable to write to stream"); } }); connection.send(Message::new(cmd::Nick(nick))); - connection.send(Message::new(cmd::User(username, 0, real_name))); + connection.send(Message::new(cmd::User(username, 8, real_name))); Ok(connection) } @@ -50,7 +47,7 @@ impl<'a> IrcConnection<'a> { } fn on_msg_rec(msg: &Message, sender: &Sender) { - let prefix = &msg.prefix; + let _prefix = &msg.prefix; let cmd = &msg.command; match *cmd { cmd::Ping(ref s) => sender.send(Message::new(cmd::Pong(s.clone()))), @@ -58,7 +55,8 @@ impl<'a> IrcConnection<'a> { }; } - pub fn run_loop(&mut self) { + #[allow(experimental)] + pub fn run_loop(&mut self, on_msg: |&Message| -> ()) { let reader = &mut self.stream; loop { fn reader_by_ref<'a, R: Reader>(reader: &'a mut R) -> std::io::RefReader<'a, R> { reader.by_ref() } @@ -70,8 +68,8 @@ impl<'a> IrcConnection<'a> { match line { Ok(line) => match from_str::(line.as_slice().trim_right()) { Some(msg) => { - IrcConnection::on_msg_rec(&msg, &self.output_sender); - (self.msg_callback)(&msg, &self.output_sender); + IrcClient::on_msg_rec(&msg, &self.output_sender); + on_msg(&msg); }, None => println!("Invalid Message recieved"), }, -- cgit v1.2.3