Connections now support non-cloneable Readers (read: all of them). Thanks, @retep998.
This commit is contained in:
parent
7efe3f3fdf
commit
f6915e2e53
3 changed files with 50 additions and 43 deletions
49
src/bot.rs
49
src/bot.rs
|
@ -1,58 +1,57 @@
|
|||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream};
|
||||
use std::vec::Vec;
|
||||
use {Bot, process};
|
||||
use conn::{Conn, Connection};
|
||||
use conn::Connection;
|
||||
use data::{Config, IrcReader, IrcWriter, Message};
|
||||
|
||||
pub struct IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
|
||||
pub conn: RefCell<Connection<T, U>>,
|
||||
pub conn: Connection<T, U>,
|
||||
pub config: Config,
|
||||
process: RefCell<|&IrcBot<T, U>, &str, &str, &[&str]|:'a -> IoResult<()>>,
|
||||
pub chanlists: HashMap<String, Vec<String>>,
|
||||
pub chanlists: RefCell<HashMap<String, Vec<String>>>,
|
||||
}
|
||||
|
||||
impl<'a> IrcBot<'a, BufferedWriter<TcpStream>, TcpStream> {
|
||||
pub fn new(process: |&IrcBot<BufferedWriter<TcpStream>, TcpStream>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, BufferedWriter<TcpStream>, TcpStream>> {
|
||||
impl<'a> IrcBot<'a, BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
|
||||
pub fn new(process: |&IrcBot<BufferedWriter<TcpStream>, BufferedReader<TcpStream>>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, BufferedWriter<TcpStream>, BufferedReader<TcpStream>>> {
|
||||
let config = try!(Config::load());
|
||||
let conn = try!(Connection::connect(config.server.as_slice(), config.port));
|
||||
Ok(IrcBot {
|
||||
conn: RefCell::new(conn),
|
||||
conn: conn,
|
||||
config: config,
|
||||
process: RefCell::new(process),
|
||||
chanlists: HashMap::new(),
|
||||
chanlists: RefCell::new(HashMap::new()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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]))
|
||||
self.conn.send(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]))
|
||||
self.conn.send(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()]))
|
||||
self.conn.send(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()]))
|
||||
self.conn.send(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()]))
|
||||
self.conn.send(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()]))
|
||||
self.conn.send(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()]))
|
||||
self.conn.send(Message::new(None, "PRIVMSG", [chan.as_slice(), msg.as_slice()]))
|
||||
}
|
||||
|
||||
fn identify(&self) -> IoResult<()> {
|
||||
|
@ -61,9 +60,7 @@ impl<'a, T, U> Bot<'a> for IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
|
|||
}
|
||||
|
||||
fn output(&mut self) -> IoResult<()> {
|
||||
let mut reader = match self.conn.borrow_mut().deref_mut() {
|
||||
&Conn(_, ref recv) => BufferedReader::new(recv.clone()),
|
||||
};
|
||||
let mut reader = self.conn.reader();
|
||||
for line in reader.lines() {
|
||||
match line {
|
||||
Ok(ln) => {
|
||||
|
@ -86,17 +83,17 @@ impl<'a, T, U> IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
|
|||
pub fn from_connection(conn: Connection<T, U>, process: |&IrcBot<T, U>, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<IrcBot<'a, T, U>> {
|
||||
let config = try!(Config::load());
|
||||
Ok(IrcBot {
|
||||
conn: RefCell::new(conn),
|
||||
conn: conn,
|
||||
config: config,
|
||||
process: RefCell::new(process),
|
||||
chanlists: HashMap::new(),
|
||||
chanlists: RefCell::new(HashMap::new()),
|
||||
})
|
||||
}
|
||||
|
||||
fn handle_command(&mut self, source: &str, command: &str, args: &[&str]) -> IoResult<()> {
|
||||
fn handle_command(&self, source: &str, command: &str, args: &[&str]) -> IoResult<()> {
|
||||
match (command, args) {
|
||||
("PING", [msg]) => {
|
||||
try!(Connection::send(self.conn.borrow_mut().deref_mut(), Message::new(None, "PONG", [msg])));
|
||||
try!(self.conn.send(Message::new(None, "PONG", [msg])));
|
||||
},
|
||||
("376", _) => { // End of MOTD
|
||||
for chan in self.config.channels.iter() {
|
||||
|
@ -110,19 +107,19 @@ impl<'a, T, U> IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
|
|||
},
|
||||
("353", [_, _, chan, users]) => { // /NAMES
|
||||
for user in users.split_str(" ") {
|
||||
if !match self.chanlists.find_mut(&String::from_str(chan)) {
|
||||
if !match self.chanlists.borrow_mut().find_mut(&String::from_str(chan)) {
|
||||
Some(vec) => {
|
||||
vec.push(String::from_str(user));
|
||||
true
|
||||
},
|
||||
None => false,
|
||||
} {
|
||||
self.chanlists.insert(String::from_str(chan), vec!(String::from_str(user)));
|
||||
self.chanlists.borrow_mut().insert(String::from_str(chan), vec!(String::from_str(user)));
|
||||
}
|
||||
}
|
||||
},
|
||||
("JOIN", [chan]) => {
|
||||
match self.chanlists.find_mut(&String::from_str(chan)) {
|
||||
match self.chanlists.borrow_mut().find_mut(&String::from_str(chan)) {
|
||||
Some(vec) => {
|
||||
match source.find('!') {
|
||||
Some(i) => vec.push(String::from_str(source.slice_to(i))),
|
||||
|
@ -133,7 +130,7 @@ impl<'a, T, U> IrcBot<'a, T, U> where T: IrcWriter, U: IrcReader {
|
|||
}
|
||||
},
|
||||
("PART", [chan, _]) => {
|
||||
match self.chanlists.find_mut(&String::from_str(chan)) {
|
||||
match self.chanlists.borrow_mut().find_mut(&String::from_str(chan)) {
|
||||
Some(vec) => {
|
||||
match source.find('!') {
|
||||
Some(i) => {
|
||||
|
|
40
src/conn.rs
40
src/conn.rs
|
@ -1,34 +1,44 @@
|
|||
use std::io::{BufferedWriter, IoResult, TcpStream, Writer};
|
||||
use std::cell::{RefCell, RefMut};
|
||||
use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream, Writer};
|
||||
use data::{IrcReader, IrcWriter, Message};
|
||||
|
||||
pub enum Connection<T, U> where T: IrcWriter, U: IrcReader {
|
||||
Conn(T, U),
|
||||
pub struct Connection<T, U> where T: IrcWriter, U: IrcReader {
|
||||
writer: RefCell<T>,
|
||||
reader: RefCell<U>,
|
||||
}
|
||||
|
||||
impl Connection<BufferedWriter<TcpStream>, TcpStream> {
|
||||
pub fn connect(host: &str, port: u16) -> IoResult<Connection<BufferedWriter<TcpStream>, TcpStream>> {
|
||||
impl Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
|
||||
pub fn connect(host: &str, port: u16) -> IoResult<Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>>> {
|
||||
let socket = try!(TcpStream::connect(host, port));
|
||||
Ok(Conn(BufferedWriter::new(socket.clone()), socket.clone()))
|
||||
Connection::new(BufferedWriter::new(socket.clone()), BufferedReader::new(socket.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
|
||||
fn send_internal(conn: &mut Connection<T, U>, msg: &str) -> IoResult<()> {
|
||||
match conn {
|
||||
&Conn(ref mut send, _) => {
|
||||
try!(send.write_str(msg));
|
||||
send.flush()
|
||||
}
|
||||
}
|
||||
fn new(writer: T, reader: U) -> IoResult<Connection<T, U>> {
|
||||
Ok(Connection {
|
||||
writer: RefCell::new(writer),
|
||||
reader: RefCell::new(reader),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn send(conn: &mut Connection<T, U>, msg: Message) -> IoResult<()> {
|
||||
fn send_internal(&self, msg: &str) -> IoResult<()> {
|
||||
let mut send = self.writer.borrow_mut();
|
||||
try!(send.write_str(msg));
|
||||
send.flush()
|
||||
}
|
||||
|
||||
pub fn send(&self, msg: Message) -> IoResult<()> {
|
||||
let mut send = msg.command.to_string();
|
||||
send.push_str(" ");
|
||||
send.push_str(msg.args.init().connect(" ").as_slice());
|
||||
send.push_str(" :");
|
||||
send.push_str(*msg.args.last().unwrap());
|
||||
send.push_str("\r\n");
|
||||
Connection::send_internal(conn, send.as_slice())
|
||||
self.send_internal(send.as_slice())
|
||||
}
|
||||
|
||||
pub fn reader<'a>(&'a self) -> RefMut<'a, U> {
|
||||
self.reader.borrow_mut()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ use serialize::json::{decode};
|
|||
|
||||
pub trait IrcWriter: Writer + Sized + 'static {}
|
||||
impl<T> IrcWriter for T where T: Writer + Sized + 'static {}
|
||||
pub trait IrcReader: Reader + Sized + Clone + 'static {}
|
||||
impl<T> IrcReader for T where T: Reader + Sized + Clone + 'static {}
|
||||
pub trait IrcReader: Buffer + Sized + 'static {}
|
||||
impl<T> IrcReader for T where T: Buffer + Sized + 'static {}
|
||||
|
||||
|
||||
#[deriving(Show, PartialEq)]
|
||||
|
|
Loading…
Reference in a new issue