diff options
author | Till Hoeppner | 2014-11-03 16:39:57 +0100 |
---|---|---|
committer | Till Hoeppner | 2014-11-03 16:39:57 +0100 |
commit | 55b915a75f49957eaddefa74cbbf572ad186ee2f (patch) | |
tree | e18b0bb361802261db96f719eae7f0c9d3f3dbf3 /src/main.rs | |
parent | fc27f8cee888acf70683badca9edadb45544822c (diff) | |
download | irsc-55b915a75f49957eaddefa74cbbf572ad186ee2f.tar.gz irsc-55b915a75f49957eaddefa74cbbf572ad186ee2f.tar.xz irsc-55b915a75f49957eaddefa74cbbf572ad186ee2f.zip |
API improvements, but more copying
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index e9965d8..865234a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,32 +3,36 @@ extern crate irsc; use irsc::server::Server; -use irsc::events::*; use irsc::color::bold; +use irsc::event; +use irsc::event::{ Event, ParseResult, PrivMsg }; static NAME: &'static str = "rusticbot"; static DESC: &'static str = "A bot, written in Rust."; +fn callback(arg: (Server, Event)) { + let (mut server, event) = arg; + match event.command[] { + event::PRIVMSG => { + let privmsg: PrivMsg = ParseResult::parse(event).unwrap(); + let response = format!("You wrote: {}", bold(privmsg.content[])); + server.msg(privmsg.from.nickname[], response[]).unwrap(); + }, + _ => () + } +} + fn main() { - let mut s = Server::new("irc.freenode.org".into_string(), 6667); - let events = s.events(); - s.connect().unwrap(); + let mut s = Server::new(); + s.connect("irc.freenode.org".into_string(), 6667).unwrap(); s.nick(NAME).unwrap(); s.user(NAME, "*", "*", DESC).unwrap(); s.join("#botzoo").unwrap(); s.msg("flan3002", "Hey!").unwrap(); - for e in events.iter() { - match e { - RplWelcome(welcome) => { - println!("{}", welcome) - }, - PrivMsg(from, _to, msg) => { - let response = format!("You wrote: {}", bold(msg[])); - s.msg(from.nickname[], response[]).unwrap(); - } - _ => () - } - } + s.events.lock().register(&callback); + + // Dedicate this thread to listening and event processing + s.listen().unwrap(); } |