diff options
Diffstat (limited to 'src/plugins/url.rs')
| -rw-r--r-- | src/plugins/url.rs | 61 |
1 files changed, 3 insertions, 58 deletions
diff --git a/src/plugins/url.rs b/src/plugins/url.rs index 455aa4e..df4fdf2 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -1,5 +1,4 @@ extern crate regex; -extern crate reqwest; extern crate select; use irc::client::prelude::*; @@ -7,15 +6,11 @@ use irc::error::IrcError; use self::regex::Regex; -use std::str; -use std::io::{self, Read}; -use self::reqwest::Client; -use self::reqwest::header::Connection; - use self::select::document::Document; use self::select::predicate::Name; use plugin::*; +use utils; lazy_static! { static ref RE: Regex = Regex::new(r"(^|\s)(https?://\S+)").unwrap(); @@ -43,64 +38,14 @@ impl Url { } } - fn download(&self, url: &str) -> Option<String> { - let response = Client::new().get(url).header(Connection::close()).send(); - - match response { - Ok(mut response) => { - let mut body = String::new(); - - // 500 kilobyte buffer - let mut buf = [0; 500 * 1000]; - let mut written = 0; - // Read until we reach EOF or max_kib KiB - loop { - let len = match response.read(&mut buf) { - Ok(0) => break, - Ok(len) => len, - Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, - Err(e) => { - debug!("Download from {:?} failed: {}", url, e); - return None; - } - }; - - let slice = match str::from_utf8(&buf[..len]) { - Ok(slice) => slice, - Err(e) => { - debug!("Failed to read bytes from {:?} as UTF8: {}", url, e); - return None; - } - }; - - body.push_str(slice); - written += len; - - // Check if the file is too large to download - if written > self.max_kib * 1024 { - debug!( - "Stopping download - File from {:?} is larger than {} KiB", - url, self.max_kib - ); - return None; - } - } - Some(body) // once told me - } - Err(e) => { - debug!("Bad response from {:?}: ({})", url, e); - None - } - } - } - fn url(&self, text: &str) -> Result<String, &str> { let url = match self.grep_url(text) { Some(url) => url, None => return Err("No Url was found."), }; - match self.download(&url) { + + match utils::download(self.max_kib, &url) { Some(body) => { let doc = Document::from(body.as_ref()); if let Some(title) = doc.find(Name("title")).next() { |
