diff options
author | David Li <li.davidm96@gmail.com> | 2016-01-27 21:39:14 -0500 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2016-01-27 21:39:14 -0500 |
commit | d5746e7cdb1db2b457688a98004620d42a49f6fa (patch) | |
tree | 44914f847eda36dd2926e4148ed55c3b4afd0c98 /src | |
parent | d01c4e4c880224a4c92b9a8ff90226276ef65695 (diff) |
refactor: move code into IrcBot struct
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 107 |
1 files 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<String, WikiPage>, } +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<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); +struct IrcBot<T: IrcRead, U: IrcWrite> { + server: IrcServer<T, U>, +} - 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<T: IrcRead, U: IrcWrite> IrcBot<T, U> { + fn new(server: IrcServer<T, U>) -> IrcBot<T, U> { + 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<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 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(); } |