From 3bbca4cdd221d392c8628773201d514b90ed40ce Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 10 Sep 2014 16:41:58 -0400 Subject: turn the client example into a simple karma bot --- examples/client.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/client.rs b/examples/client.rs index db3b244..d4cc997 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -1,13 +1,57 @@ extern crate irc; -pub struct ExampleClient; +use std::collections::HashMap; +use std::io; + +pub struct ExampleClient { + karma: HashMap, +} + +impl ExampleClient { + pub fn new () -> ExampleClient { + ExampleClient { karma: HashMap::new() } + } +} impl irc::ClientCallbacks for ExampleClient { + fn on_rpl_welcome (&mut self, client: &mut irc::Client, _m: &irc::Message) -> io::IoResult<()> { + client.join(["#doytest"], []) + } + + fn on_privmsg (&mut self, client: &mut irc::Client, from: Option<&str>, receivers: &[&str], text: &str) -> io::IoResult<()> { + let incr = if text.ends_with("++") { 1 } + else if text.ends_with("--") { -1 } + else { 0 }; + if incr != 0 { + let text = text.slice(0, text.len() - 2); + let giving_user = String::from_utf8( + from.unwrap().bytes().take_while(|&c| c != b'!').collect() + ).unwrap(); + if giving_user.as_slice() == text { + try!(client.notice(receivers[0], "You can't give karma to yourself!")); + } + else { + self.karma.insert_or_update_with( + text.to_string(), + incr, + |_k, v| { *v += incr } + ); + } + } + else if text.starts_with("karma ") { + let text = text.slice_from(6); + let default = 0; + let karma = self.karma.find(&text.to_string()).unwrap_or(&default); + try!(client.notice(receivers[0], format!("Karma for {} is {}", text, karma).as_slice())); + } + + Ok(()) + } } fn main () { let mut builder = irc::ClientBuilder::new("doytest", "chat.freenode.net"); builder.set_debug(true); let client = builder.connect(); - client.run_loop_with_callbacks(ExampleClient); + client.run_loop_with_callbacks(ExampleClient::new()); } -- cgit v1.2.3