summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2016-01-27 21:24:15 -0500
committerDavid Li <li.davidm96@gmail.com>2016-01-27 21:24:15 -0500
commitd01c4e4c880224a4c92b9a8ff90226276ef65695 (patch)
tree0413ab41926379891c170561d93dbbdce872ef8e
parent63c9c711cf998d7321bf64a097a2386598c35542 (diff)
feat(emote): add emote feature
-rw-r--r--src/main.rs94
1 files 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<Vec<WikiRedirect>>,
+ normalized: Option<Vec<WikiRedirect>>,
pages: HashMap<String, WikiPage>,
}
+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<WikiQueryResponse>{
+ 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<String>) -> Option<String> {
+ prefix.and_then(|p| p.split("!").next().map(|s| s.to_owned()))
+}
+
+fn wiki_query<T: IrcRead, U: IrcWrite>(server: &IrcServer<T, U>, 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<T: IrcRead, U: IrcWrite>(server: &IrcServer<T, U>, 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<WikiQueryResponse> = 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);
}
}
}