From 7e10df8e0dbd5f74b79b027512628162f94c2f54 Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Tue, 3 Jun 2014 00:53:52 -0400 Subject: Initial Commit --- lib.rs | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.rs | 24 ++++++++++ 2 files changed, 178 insertions(+) create mode 100644 lib.rs create mode 100644 main.rs diff --git a/lib.rs b/lib.rs new file mode 100644 index 0000000..a6dfb7b --- /dev/null +++ b/lib.rs @@ -0,0 +1,154 @@ +//#![crate_id = "irc#0.1"] +//#![crate_type = "lib"] + +use std::io::net::tcp::TcpStream; +use std::io::IoResult; +use std::io::BufferedReader; +use std::fmt; +use std::from_str::FromStr; + +pub struct IrcConnection { + callbacks: IrcCallbacks, + stream: TcpStream +} + +#[deriving(Clone)] +pub enum Command { + Nick, + User, + Quit, + Join, + Part, + PrivMsg, + Notice, + Motd, + Ping, + Pong, + Error, + Away, + Numeric(u16), + UnknownStr(String) +} + +impl fmt::Show for Command { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "{}", + match *self { + Nick => "NICK".into_maybe_owned(), + User => "USER".into_maybe_owned(), + Quit => "QUIT".into_maybe_owned(), + Join => "JOIN".into_maybe_owned(), + Part => "PART".into_maybe_owned(), + PrivMsg => "PRIVMSG".into_maybe_owned(), + Notice => "NOTICE".into_maybe_owned(), + Motd => "MOTD".into_maybe_owned(), + Ping => "PING".into_maybe_owned(), + Pong => "PONG".into_maybe_owned(), + Error => "ERROR".into_maybe_owned(), + Away => "AWAY".into_maybe_owned(), + Numeric(i) => i.to_str().into_maybe_owned(), + UnknownStr(ref s) => s.as_slice().into_maybe_owned(), + } + ) + } +} + +impl FromStr for Command { + fn from_str(s: &str) -> Option { + Some(match s { + "NICK" => Nick, + "USER" => User, + "QUIT" => Quit, + "JOIN" => Join, + "PART" => Part, + "PRIVMSG" => PrivMsg, + "NOTICE" => Notice, + "MOTD" => Motd, + "PING" => Ping, + "PONG" => Pong, + "ERROR" => Error, + "AWAY" => Away, + other => match from_str::(other) { + Some(i) => Numeric(i), + None => UnknownStr(other.to_string()) + } + }) + } +} + +#[deriving(Clone)] +pub struct Message { + pub prefix: Option, + pub command: Command, + pub arguments: Vec, +} + +impl<'a> fmt::Show for Message { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + if self.prefix.is_some() { + try!(write!(formatter, ":{} ", self.prefix.get_ref())); + } + try!(write!(formatter, "{}", self.command)); + for argument in self.arguments.iter() { + try!(write!(formatter, " {}", *argument)); + } + Ok(()) + } +} + +impl FromStr for Message { + fn from_str(s: &str) -> Option { + //TODO: Parse string + unimplemented!(); + } +} + + + +impl IrcConnection { + pub fn connect( + callbacks: IrcCallbacks, host: &str, port: u16, + nick: &str, username: &str, real_name: &str) -> IoResult { + let mut stream = try!(TcpStream::connect(host, port)); + + Ok(IrcConnection { + callbacks: callbacks, + stream: stream + }) + } + + pub fn start_loop(&mut self) { + let stream = self.stream.clone(); +// spawn(proc() { + let mut reader = BufferedReader::new(stream); + loop { + match reader.read_line() { + Ok(line) => { + let mut words: Vec<&str> = line.as_slice().words().collect(); + + println!("{}", words); + if words.is_empty() { continue; } + if (*words.get(0)).starts_with(":") { + words.shift(); + } + match *words.get(0) { + "PING" => println!("PING"), + other => println!("other: {}", other), + } + } + Err(e) => { + println!("An Error occured: {}", e); + break; + } + } + break; + } +// }); + } +} + +pub struct IrcCallbacks { + pub on_connect: fn(&mut IrcConnection)->(), + pub on_numeric: fn(&mut IrcConnection, uint, &str, &[&str])->(), + //TODO: Add the rest +} diff --git a/main.rs b/main.rs new file mode 100644 index 0000000..1d975b2 --- /dev/null +++ b/main.rs @@ -0,0 +1,24 @@ +use std::io::net::tcp::TcpStream; +use std::io::BufferedReader; + +use lib::IrcCallbacks; +use lib::IrcConnection; + +mod lib; + +fn main() { + fn on_connect(_connection: &mut IrcConnection) { + println!("Connected"); + } + fn on_numeric(_connection: &mut IrcConnection, n: uint, origin: &str, params: &[&str]) { + println!("Numeric event \\#{} with params {}", n, params); + } + let callbacks = IrcCallbacks { + on_connect: on_connect, + on_numeric: on_numeric, + }; + let mut connection = IrcConnection::connect(callbacks, "irc.mozilla.org", 6667, "Dr-Emann", "dremann", "Zachary Dremann").unwrap(); + + connection.start_loop(); + +} -- cgit v1.2.3