extern crate regex; extern crate select; use irc::client::prelude::*; use self::regex::Regex; use self::select::document::Document; use self::select::predicate::Name; use plugin::*; use utils; use self::error::*; use error::FrippyError; use error::ErrorKind as FrippyErrorKind; use failure::Fail; use failure::ResultExt; lazy_static! { static ref RE: Regex = Regex::new(r"(^|\s)(https?://\S+)").unwrap(); } #[derive(PluginName, Debug)] pub struct Url { max_kib: usize, } impl Url { /// If a file is larger than `max_kib` KiB the download is stopped pub fn new(max_kib: usize) -> Url { Url { max_kib: max_kib } } fn grep_url(&self, msg: &str) -> Option { let captures = RE.captures(msg)?; debug!("Url captures: {:?}", captures); Some(captures.get(2)?.as_str().to_owned()) } fn get_title(&self, body: &str) -> Option { let doc = Document::from(body.as_ref()); let title = doc.find(Name("title")).next()?; let title = title.children().next()?; let title_text = title.as_text()?.trim().replace("\n", "|"); debug!("Title: {:?}", title); debug!("Text: {:?}", title_text); Some(title_text) } fn url(&self, text: &str) -> Result { let url = self.grep_url(text).ok_or(ErrorKind::MissingUrl)?; let body = utils::download(&url, Some(self.max_kib)).context(ErrorKind::Download)?; Ok(self.get_title(&body).ok_or(ErrorKind::MissingTitle)?) } } impl Plugin for Url { fn execute(&self, _: &IrcClient, message: &Message) -> ExecutionStatus { match message.command { Command::PRIVMSG(_, ref msg) => if RE.is_match(msg) { ExecutionStatus::RequiresThread } else { ExecutionStatus::Done }, _ => ExecutionStatus::Done, } } fn execute_threaded(&self, client: &IrcClient, message: &Message) -> Result<(), FrippyError> { Ok(match message.command { Command::PRIVMSG(_, ref content) => match self.url(content) { Ok(title) => client .send_privmsg(message.response_target().unwrap(), &title) .context(FrippyErrorKind::Connection)?, Err(e) => Err(e).context(FrippyErrorKind::Url)?, }, _ => (), }) } fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { Ok(client .send_notice( &command.source, "This Plugin does not implement any commands.", ) .context(FrippyErrorKind::Connection)?) } fn evaluate(&self, _: &IrcClient, command: PluginCommand) -> Result { self.url(&command.tokens[0]) .map_err(|e| e.cause().unwrap().to_string()) } } pub mod error { /// A URL plugin error #[derive(Copy, Clone, Eq, PartialEq, Debug, Fail, Error)] #[error = "UrlError"] pub enum ErrorKind { /// A download error #[fail(display = "A download error occured")] Download, /// Missing URL error #[fail(display = "No URL was found")] MissingUrl, /// Missing title error #[fail(display = "No title was found")] MissingTitle, } }