From ed9dff167471dc1e84963e4c56dbfda0a7edbc73 Mon Sep 17 00:00:00 2001 From: David Li Date: Wed, 27 Jan 2016 08:14:40 -0500 Subject: Initial commit --- src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2ec24c0 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,43 @@ +extern crate hyper; +extern crate irc; + +use std::io::Read; + +use hyper::Client; +use hyper::header::Connection; + +use irc::client::prelude::*; + +fn main() { + let config = Config { + nickname: Some(format!("lidavidm_prime")), + server: Some(format!("irc.freenode.net")), + channels: Some(vec![format!("#opensourcecornell")]), + .. Default::default() + }; + let server = IrcServer::from_config(config).unwrap(); + 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()); + if &message.command[..] == "PRIVMSG" { + if let Some(msg) = message.suffix { + if msg.starts_with("!wiki") { + let article = msg[5..].trim(); + let response = format!("Looking up \"{}\" on Wikipedia for you...", article); + server.send_privmsg(&message.args[0], &response).unwrap(); + + let mut client = Client::new(); + let mut res = client.get("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&explaintext=&titles={}&redirects=") + .header(Connection::close()) + .send().unwrap(); + + let mut body = String::new(); + res.read_to_string(&mut body).unwrap(); + + println!("Response: {}", body); + } + } + } + } +} -- cgit v1.2.3