From f45036677761c4ac49eaa09bb3c888f98b0d236b Mon Sep 17 00:00:00 2001 From: Darren VanBuren Date: Wed, 27 Jul 2016 15:21:14 -0700 Subject: [PATCH] Begin detecting mentions, and slicing to the actual message portion Also convert source_nick to a plain string, instead of an Option. Next should be making weather code get weather for specified locations. --- Cargo.lock | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 24 ++++++++++++++++++----- 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 00e458e..0a8ba81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,18 @@ version = "0.1.0" dependencies = [ "hyper 0.9.10 (registry+https://github.com/rust-lang/crates.io-index)", "irc 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.73 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "aho-corasick" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "bitflags" version = "0.7.0" @@ -192,6 +201,14 @@ name = "matches" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "memchr" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mime" version = "0.2.1" @@ -272,6 +289,23 @@ dependencies = [ "libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "regex" +version = "0.1.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rustc-serialize" version = "0.3.19" @@ -307,6 +341,23 @@ dependencies = [ "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thread-id" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "time" version = "0.1.35" @@ -366,6 +417,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "utf8-ranges" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index 9f42c45..01df7ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ authors = ["Darren VanBuren "] irc = "0.11" hyper = "0.9" rustc-serialize = "0.3" +regex = "0.1" diff --git a/src/main.rs b/src/main.rs index 5b627ed..e685f4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,13 @@ extern crate irc; extern crate hyper; extern crate rustc_serialize; +extern crate regex; use std::io::Read; use irc::client::prelude::*; use hyper::{Client}; use rustc_serialize::json::{self}; +use regex::Regex; static WEATHER_API_BASE: &'static str = "http://api.wunderground.com/api/"; @@ -26,15 +28,27 @@ fn main() { fn handle_privmsg(target: &String, message_body: &String, message_obj: &Message, server: &IrcServer) { println!("Received message in {} from {}: {}", target, message_obj.source_nickname().unwrap(), message_body); - let source_nick = message_obj.source_nickname(); - if message_body.contains("rust-bot") { - server.send_privmsg(target, "Hello!").unwrap(); - } else if message_body.contains("test_get_weather") { + let source_nick = message_obj.source_nickname().unwrap(); + let my_nick = server.current_nickname(); + let mentions_regex = Regex::new(&format!("({}(:|,) )(.+)$", my_nick)).unwrap(); + let mut inner_message = String::new(); + match mentions_regex.captures(message_body) { + Some(capture) => match capture.at(3) { + Some(inner_message_match) => {inner_message = String::from(inner_message_match); println!("mentioned_message: {}", inner_message)}, + _ => (), + }, + _ => (), + } + println!("mentioned_message: {}", inner_message); + + if inner_message == String::from("test") { + server.send_privmsg(target, &format!("{}: Hello!", source_nick)).unwrap(); + } else if inner_message.starts_with("weather") { match server.config().options { None => println!("Options not configured!"), Some(ref options) => match options.get("wunderground_api_key") { None => println!("wunderground_api_key not configured!"), - Some(api_key) => server.send_privmsg(target, &format!("{}: {}", source_nick.unwrap(), &test_get_weather(api_key))).unwrap(), + Some(api_key) => server.send_privmsg(target, &format!("{}: {}", source_nick, &test_get_weather(api_key))).unwrap(), } } } else if message_body.contains("bot-quit") {