Added concept of bot ownership, improved failure propagation.

This commit is contained in:
Aaron Weiss 2014-10-06 16:33:37 -04:00
parent 3475437fdb
commit 352a692c9b
3 changed files with 13 additions and 8 deletions

View file

@ -11,8 +11,7 @@ pub fn connect(host: &str, port: u16) -> IoResult<Connection> {
fn send_internal(conn: &Connection, msg: &str) -> IoResult<()> {
let &Connection(ref tcp) = conn;
let mut writer = BufferedWriter::new(tcp.clone());
writer.write_str(msg);
writer.flush()
writer.write_str(msg)
}
pub fn send(conn: &Connection, msg: Message) -> IoResult<()> {

View file

@ -20,6 +20,7 @@ impl<'a> Message<'a> {
#[deriving(Decodable)]
pub struct Config {
pub owners: Vec<String>,
pub nickname: String,
pub username: String,
pub realname: String,
@ -39,4 +40,8 @@ impl Config {
detail: Some(e.to_string()),
})
}
pub fn is_owner(&self, nickname: &str) -> bool {
self.owners.as_slice().contains(&String::from_str(nickname))
}
}

View file

@ -61,22 +61,23 @@ impl<'a> Bot<'a> {
}
pub fn identify(&self) -> IoResult<()> {
self.send_nick(self.config.nickname.as_slice());
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) {
pub fn output(&mut self) -> IoResult<()> {
let mut reader = { let Connection(ref tcp) = self.conn; BufferedReader::new(tcp.clone()) };
for line in reader.lines() {
match line {
Ok(ln) => {
let (source, command, args) = process(ln.as_slice()).unwrap();
self.handle_command(source, command, args.as_slice());
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<()> {
@ -137,7 +138,7 @@ impl<'a> Bot<'a> {
}
},
_ => {
(*self.process.borrow_mut().deref_mut())(self, source, command, args);
try!((*self.process.borrow_mut().deref_mut())(self, source, command, args));
},
};
Ok(())