aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs36
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();
}