From d01c4e4c880224a4c92b9a8ff90226276ef65695 Mon Sep 17 00:00:00 2001 From: David Li Date: Wed, 27 Jan 2016 21:24:15 -0500 Subject: feat(emote): add emote feature --- src/main.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 69 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8c1d503..90859c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,9 +36,65 @@ struct WikiQueryResponse { #[derive(Debug, PartialEq, Serialize, Deserialize)] struct WikiQuery { redirects: Option>, + normalized: Option>, pages: HashMap, } +fn user_agent() -> hyper::header::UserAgent { + hyper::header::UserAgent("lidavidm_irc_bot/0.1 (https://git.lidavidm.me; li.davidm96@gmail.com) hyper/0.7.2".to_owned()) +} + +fn query_page(article: &str) -> serde_json::Result{ + let client = Client::new(); + let mut res = client + .get(&format!("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&explaintext=&titles={}&redirects=", article.replace(" ", "%20"))) + .header(user_agent()) + .header(Connection::close()) + .send().unwrap(); + + let mut body = String::new(); + res.read_to_string(&mut body).unwrap(); + + serde_json::from_str(&body) +} + +fn emote_action(action: &str) -> Option<&str> { + if action == "shrugs" { + Some("¯\\_(ツ)_/¯") + } + else { + None + } +} + +fn extract_username(prefix: Option) -> Option { + prefix.and_then(|p| p.split("!").next().map(|s| s.to_owned())) +} + +fn wiki_query(server: &IrcServer, origin: &str, article: &str) { + let response = query_page(article); + + if let Ok(q) = response { + for page in q.query.pages.values() { + let url = format!("https://en.wikipedia.org/?curid={}", page.pageid); + server.send_privmsg(origin, &format!("{}: {}", page.title, url)).unwrap(); + server.send_privmsg(origin, &page.extract).unwrap(); + break; + } + } + else { + server.send_privmsg(origin, "Sorry, I couldn't retrieve the wiki page.").unwrap(); + println!("{:?}", response); + } +} + +fn emote(server: &IrcServer, username: &str, action: &str) { + if let Some(reply) = emote_action(action) { + // TODO: don't hardcode channel + server.send_privmsg("#opensourcecornell", &(username.to_owned() + " is " + reply)[..]).unwrap(); + } +} + fn main() { let config = Config { nickname: Some(format!("lidavidm_prime")), @@ -50,34 +106,22 @@ fn main() { server.identify().unwrap(); for message in server.iter() { let message = message.unwrap(); // We'll just panic if there's an error. - print!("{}", message.into_string()); + println!("{:?}", message); if &message.command[..] == "PRIVMSG" { if let Some(msg) = message.suffix { - if msg.starts_with("!wiki") { - let article = msg[5..].trim(); - - let mut client = Client::new(); - let mut res = client - .get(&format!("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&explaintext=&titles={}&redirects=", article.replace(" ", "%20"))) - .header(hyper::header::UserAgent("lidavidm_irc_bot/0.1 (https://git.lidavidm.me; li.davidm96@gmail.com) hyper/0.7.2".to_owned())) - .header(Connection::close()) - .send().unwrap(); - - let mut body = String::new(); - res.read_to_string(&mut body).unwrap(); - - let response: serde_json::Result = serde_json::from_str(&body); - if let Ok(q) = response { - for page in q.query.pages.values() { - let url = format!("https://en.wikipedia.org/?curid={}", page.pageid); - server.send_privmsg(&message.args[0], &format!("{}: {}", page.title, url)).unwrap(); - server.send_privmsg(&message.args[0], &page.extract).unwrap(); - break; - } + if let Some(username) = extract_username(message.prefix) { + let origin = &message.args[0]; + if msg.starts_with("!wiki") { + let article = msg[5..].trim(); + wiki_query(&server, origin, article); + } + else if msg.starts_with("\u{1}ACTION") { + let action = msg[8..msg.len() - 1].trim(); + emote(&server, &username, action); } - else { - server.send_privmsg(&message.args[0], "Sorry, I couldn't retrieve the wiki page.").unwrap(); - println!("{} {:?}", body, response); + else if msg.starts_with("/me") { + let action = msg[3..].trim(); + emote(&server, &username, action); } } } -- cgit v1.2.3