From 55b915a75f49957eaddefa74cbbf572ad186ee2f Mon Sep 17 00:00:00 2001 From: Till Hoeppner Date: Mon, 3 Nov 2014 16:39:57 +0100 Subject: API improvements, but more copying --- src/main.rs | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/main.rs') 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(); } -- cgit v1.2.3