diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-02-10 14:13:07 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-02-10 14:13:07 +0100 |
| commit | 2ba26a37d27a637b7c0e02970419342a6f83462b (patch) | |
| tree | c7d2323ddf7a0aa58d1b680200dc0acb9dad8558 /src/plugins/url.rs | |
| parent | 1f69bfef7f2fd5fdc8787485d81461d68aa2d3ba (diff) | |
| download | frippy-2ba26a37d27a637b7c0e02970419342a6f83462b.tar.gz frippy-2ba26a37d27a637b7c0e02970419342a6f83462b.zip | |
Add evaluate function to plugins
This allows plugins to be used in combination with each other.
Diffstat (limited to 'src/plugins/url.rs')
| -rw-r--r-- | src/plugins/url.rs | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/src/plugins/url.rs b/src/plugins/url.rs index f6e06f3..ab36833 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -95,11 +95,11 @@ impl Url { } } - fn url(&self, server: &IrcServer, message: &str, target: &str) -> Result<(), IrcError> { - let url = match self.grep_url(message) { + fn url(&self, text: &str) -> Result<String, &str> { + let url = match self.grep_url(text) { Some(url) => url, None => { - return Ok(()); + return Err("No Url was found.") } }; @@ -109,18 +109,18 @@ impl Url { let doc = Document::from(body.as_ref()); if let Some(title) = doc.find(Name("title")).next() { - let text = title.children().next().unwrap(); - let message = text.as_text().unwrap().trim().replace("\n", "|"); - debug!("Title: {:?}", text); - debug!("Message: {:?}", message); + let title = title.children().next().unwrap(); + let title_text = title.as_text().unwrap().trim().replace("\n", "|"); + debug!("Title: {:?}", title); + debug!("Text: {:?}", title_text); - server.send_privmsg(target, &message) + Ok(title_text) } else { - Ok(()) + Err("No title was found.") } } - None => Ok(()), + None => Err("Failed to download document.") } } } @@ -136,7 +136,10 @@ impl Plugin for Url { fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError> { match message.command { Command::PRIVMSG(_, ref content) => { - self.url(server, content, message.response_target().unwrap()) + match self.url(content) { + Ok(title) => server.send_privmsg(&message.response_target().unwrap(), &title), + Err(_) => Ok(()), + } } _ => Ok(()), } @@ -146,6 +149,10 @@ impl Plugin for Url { server.send_notice(&command.source, "This Plugin does not implement any commands.") } + + fn evaluate(&self, _: &IrcServer, command: PluginCommand) -> Result<String, String> { + self.url(&command.tokens[0]).map_err(|e| String::from(e)) + } } #[cfg(test)] |
