From d5746e7cdb1db2b457688a98004620d42a49f6fa Mon Sep 17 00:00:00 2001 From: David Li Date: Wed, 27 Jan 2016 21:39:14 -0500 Subject: refactor: move code into IrcBot struct --- src/main.rs | 107 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index 90859c2..a230a7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,8 @@ struct WikiQuery { pages: HashMap, } +const CHANNEL: &'static str = "#opensourcecornell"; + 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()) } @@ -71,27 +73,71 @@ 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); +struct IrcBot { + server: IrcServer, +} - 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; +impl IrcBot { + fn new(server: IrcServer) -> IrcBot { + IrcBot { + server: server, } } - else { - server.send_privmsg(origin, "Sorry, I couldn't retrieve the wiki page.").unwrap(); - println!("{:?}", response); + + fn wiki_query(&self, 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); + self.server.send_privmsg(origin, &format!("{}: {}", page.title, url)).unwrap(); + self.server.send_privmsg(origin, &page.extract).unwrap(); + break; + } + } + else { + self.server.send_privmsg(origin, "Sorry, I couldn't retrieve the wiki page.").unwrap(); + println!("{:?}", response); + } + } + + fn emote(&self, username: &str, action: &str) { + if let Some(reply) = emote_action(action) { + self.server.send_privmsg( + CHANNEL, + &(username.to_owned() + " is " + reply)[..]).unwrap(); + } } -} -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 handle_privmsg(&self, origin: &str, username: &str, msg: &str) { + if msg.starts_with("!wiki") { + let article = msg[5..].trim(); + self.wiki_query(origin, article); + } + else if msg.starts_with("\u{1}ACTION") { + let action = msg[8..msg.len() - 1].trim(); + self.emote(&username, action); + } + else if msg.starts_with("/me") { + let action = msg[3..].trim(); + self.emote(&username, action); + } + } + + fn process_messages(&self) { + for message in self.server.iter() { + let message = message.unwrap(); + println!("{:?}", message); + if &message.command[..] == "PRIVMSG" { + if let Some(msg) = message.suffix { + if let Some(username) = extract_username(message.prefix) { + let origin = &message.args[0]; + + self.handle_privmsg(origin, &username, &msg); + } + } + } + } } } @@ -99,32 +145,13 @@ fn main() { let config = Config { nickname: Some(format!("lidavidm_prime")), server: Some(format!("irc.freenode.net")), - channels: Some(vec![format!("#opensourcecornell")]), + channels: Some(vec![CHANNEL.to_string()]), .. 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. - println!("{:?}", message); - if &message.command[..] == "PRIVMSG" { - if let Some(msg) = message.suffix { - 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 if msg.starts_with("/me") { - let action = msg[3..].trim(); - emote(&server, &username, action); - } - } - } - } - } + + let bot = IrcBot::new(server); + bot.process_messages(); } -- cgit v1.2.3