Exposed method to add user functionality to bots at construction.
This commit is contained in:
parent
642622752f
commit
8672c257cb
1 changed files with 9 additions and 12 deletions
21
src/lib.rs
21
src/lib.rs
|
@ -3,7 +3,7 @@ extern crate regex;
|
||||||
#[phase(plugin)] extern crate regex_macros;
|
#[phase(plugin)] extern crate regex_macros;
|
||||||
extern crate serialize;
|
extern crate serialize;
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::io::{BufferedReader, InvalidInput, IoError, IoResult};
|
use std::io::{BufferedReader, InvalidInput, IoError, IoResult};
|
||||||
use data::{Message, Config};
|
use data::{Message, Config};
|
||||||
use conn::{Connection, connect, send};
|
use conn::{Connection, connect, send};
|
||||||
|
@ -11,18 +11,20 @@ use conn::{Connection, connect, send};
|
||||||
pub mod conn;
|
pub mod conn;
|
||||||
pub mod data;
|
pub mod data;
|
||||||
|
|
||||||
pub struct Bot {
|
pub struct Bot<'a> {
|
||||||
pub conn: Connection,
|
pub conn: Connection,
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
|
process: RefCell<|&Bot, &str, &str, &[&str]|:'a -> IoResult<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bot {
|
impl<'a> Bot<'a> {
|
||||||
pub fn new() -> IoResult<Bot> {
|
pub fn new(process: |&Bot, &str, &str, &[&str]|:'a -> IoResult<()>) -> IoResult<Bot<'a>> {
|
||||||
let config = try!(Config::load());
|
let config = try!(Config::load());
|
||||||
let conn = try!(connect(config.server.as_slice(), config.port));
|
let conn = try!(connect(config.server.as_slice(), config.port));
|
||||||
Ok(Bot {
|
Ok(Bot {
|
||||||
conn: conn,
|
conn: conn,
|
||||||
config: config,
|
config: config,
|
||||||
|
process: RefCell::new(process),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,15 +73,10 @@ impl Bot {
|
||||||
for chan in self.config.channels.iter() {
|
for chan in self.config.channels.iter() {
|
||||||
try!(self.send_join(chan.as_slice()));
|
try!(self.send_join(chan.as_slice()));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
("PRIVMSG", [chan, msg]) => {
|
|
||||||
if msg.contains("pickles") && msg.contains("hi") {
|
|
||||||
try!(send(&self.conn, Message::new(None, "PRIVMSG", [chan.as_slice(), "hi"])));
|
|
||||||
} else if msg.starts_with(". ") {
|
|
||||||
try!(send(&self.conn, Message::new(None, "PRIVMSG", [chan.as_slice(), msg.slice_from(2)])));
|
|
||||||
};
|
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => {
|
||||||
|
(*self.process.borrow_mut().deref_mut())(self, source, command, args);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue