diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-02-13 21:37:58 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-02-13 21:42:26 +0100 |
| commit | 32e59aa3cc6567b909ab995a4e69beb12bdf0322 (patch) | |
| tree | ef8fdd610c3062debf8e6cae6bc084cc139c12be /src/plugins/url.rs | |
| parent | 2c6351d2c8dea5b782b2e6b52b8847426722a60a (diff) | |
| download | frippy-32e59aa3cc6567b909ab995a4e69beb12bdf0322.tar.gz frippy-32e59aa3cc6567b909ab995a4e69beb12bdf0322.zip | |
Add download function & fromurl command
Diffstat (limited to 'src/plugins/url.rs')
| -rw-r--r-- | src/plugins/url.rs | 61 |
1 files changed, 2 insertions, 59 deletions
diff --git a/src/plugins/url.rs b/src/plugins/url.rs index f6e06f3..6f4a68f 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::Error as 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,58 +38,6 @@ 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); - return None; - } - } - } - fn url(&self, server: &IrcServer, message: &str, target: &str) -> Result<(), IrcError> { let url = match self.grep_url(message) { Some(url) => url, @@ -104,7 +47,7 @@ impl Url { }; - match self.download(&url) { + match utils::download(self.max_kib, &url) { Some(body) => { let doc = Document::from(body.as_ref()); |
