From 54e781e6e75f0153046988b907dcf4e4af67ec4d Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Wed, 8 Oct 2014 13:32:37 -0400 Subject: [PATCH] Refactored Bot into IrcBot based on Bot trait. --- src/lib.rs | 126 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 55 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4f026fe..34202a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,18 +13,31 @@ use data::{Config, IrcReader, IrcWriter, Message}; pub mod conn; pub mod data; -pub struct Bot<'a, T, U> where T: IrcWriter, U: IrcReader { +pub trait Bot<'a> { + fn send_nick(&self, nick: &str) -> IoResult<()>; + fn send_user(&self, username: &str, real_name: &str) -> IoResult<()>; + fn send_join(&self, chan: &str) -> IoResult<()>; + fn send_mode(&self, chan: &str, mode: &str) -> IoResult<()>; + fn send_topic(&self, chan: &str, topic: &str) -> IoResult<()>; + fn send_invite(&self, person: &str, chan: &str) -> IoResult<()>; + fn send_privmsg(&self, chan: &str, msg: &str) -> IoResult<()>; + fn identify(&self) -> IoResult<()>; + fn output(&mut self) -> IoResult<()>; +} + +pub struct IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader { pub conn: RefCell>, pub config: Config, - process: RefCell<|&Bot, &str, &str, &[&str]|:'a -> IoResult<()>>, + process: RefCell<|&IrcBot, &str, &str, &[&str]|:'a -> IoResult<()>>, pub chanlists: HashMap>, } -impl<'a, T, U> Bot<'a, T, U> where T: IrcWriter, U: IrcReader { - pub fn new(process: |&Bot, TcpStream>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult, TcpStream>> { + +impl<'a, T, U> IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader { + pub fn new(process: |&IrcBot, TcpStream>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult, TcpStream>> { let config = try!(Config::load()); let conn = try!(Connection::connect(config.server.as_slice(), config.port)); - Ok(Bot { + Ok(IrcBot { conn: RefCell::new(conn), config: config, process: RefCell::new(process), @@ -32,56 +45,6 @@ impl<'a, T, U> Bot<'a, T, U> where T: IrcWriter, U: IrcReader { }) } - pub fn send_nick(&self, nick: &str) -> IoResult<()> { - Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "NICK", [nick])) - } - - pub fn send_user(&self, username: &str, real_name: &str) -> IoResult<()> { - Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "USER", [username, "0", "*", real_name])) - } - - pub fn send_join(&self, chan: &str) -> IoResult<()> { - Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "JOIN", [chan.as_slice()])) - } - - pub fn send_mode(&self, chan: &str, mode: &str) -> IoResult<()> { - Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "MODE", [chan.as_slice(), mode.as_slice()])) - } - - pub fn send_topic(&self, chan: &str, topic: &str) -> IoResult<()> { - Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "TOPIC", [chan.as_slice(), topic.as_slice()])) - } - - pub fn send_invite(&self, person: &str, chan: &str) -> IoResult<()> { - Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "INVITE", [person.as_slice(), chan.as_slice()])) - } - - pub fn send_privmsg(&self, chan: &str, msg: &str) -> IoResult<()> { - Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "PRIVMSG", [chan.as_slice(), msg.as_slice()])) - } - - pub fn identify(&self) -> IoResult<()> { - try!(self.send_nick(self.config.nickname.as_slice())); - self.send_user(self.config.username.as_slice(), self.config.realname.as_slice()) - } - - pub fn output(&mut self) -> IoResult<()> { - let mut reader = match self.conn.borrow_mut().deref_mut() { - &Conn(_, ref recv) => BufferedReader::new(recv.clone()), - }; - for line in reader.lines() { - match line { - Ok(ln) => { - let (source, command, args) = try!(process(ln.as_slice())); - try!(self.handle_command(source, command, args.as_slice())); - println!("{}", ln) - }, - Err(e) => println!("Shit, you're fucked! {}", e), - } - } - Ok(()) - } - fn handle_command(&mut self, source: &str, command: &str, args: &[&str]) -> IoResult<()> { match (command, args) { ("PING", [msg]) => { @@ -147,6 +110,59 @@ impl<'a, T, U> Bot<'a, T, U> where T: IrcWriter, U: IrcReader { } } + +impl<'a, T, U> Bot<'a> for IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader { + fn send_nick(&self, nick: &str) -> IoResult<()> { + Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "NICK", [nick])) + } + + fn send_user(&self, username: &str, real_name: &str) -> IoResult<()> { + Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "USER", [username, "0", "*", real_name])) + } + + fn send_join(&self, chan: &str) -> IoResult<()> { + Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "JOIN", [chan.as_slice()])) + } + + fn send_mode(&self, chan: &str, mode: &str) -> IoResult<()> { + Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "MODE", [chan.as_slice(), mode.as_slice()])) + } + + fn send_topic(&self, chan: &str, topic: &str) -> IoResult<()> { + Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "TOPIC", [chan.as_slice(), topic.as_slice()])) + } + + fn send_invite(&self, person: &str, chan: &str) -> IoResult<()> { + Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "INVITE", [person.as_slice(), chan.as_slice()])) + } + + fn send_privmsg(&self, chan: &str, msg: &str) -> IoResult<()> { + Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "PRIVMSG", [chan.as_slice(), msg.as_slice()])) + } + + fn identify(&self) -> IoResult<()> { + try!(self.send_nick(self.config.nickname.as_slice())); + self.send_user(self.config.username.as_slice(), self.config.realname.as_slice()) + } + + fn output(&mut self) -> IoResult<()> { + let mut reader = match self.conn.borrow_mut().deref_mut() { + &Conn(_, ref recv) => BufferedReader::new(recv.clone()), + }; + for line in reader.lines() { + match line { + Ok(ln) => { + let (source, command, args) = try!(process(ln.as_slice())); + try!(self.handle_command(source, command, args.as_slice())); + println!("{}", ln) + }, + Err(e) => println!("Shit, you're fucked! {}", e), + } + } + Ok(()) + } +} + fn process(msg: &str) -> IoResult<(&str, &str, Vec<&str>)> { let reg = regex!(r"^(?::([^ ]+) )?([^ ]+)(.*)"); let cap = match reg.captures(msg) {