Commit b8c248a756
Unsigned
Layout: unified · split
src/commands/ignore.rs added +74
| @@ -0,0 +1,74 @@ | |||
| 1 | use std::borrow::Cow; | ||
| 2 | |||
| 3 | use weechat::{ | ||
| 4 | buffer::Buffer, | ||
| 5 | hooks::{CommandRun, CommandRunCallback}, | ||
| 6 | Prefix, ReturnCode, Weechat, | ||
| 7 | }; | ||
| 8 | |||
| 9 | use crate::PLUGIN_NAME; | ||
| 10 | |||
| 11 | pub struct IgnoreCommand; | ||
| 12 | |||
| 13 | #[derive(Debug, Eq, PartialEq)] | ||
| 14 | enum IgnoreDisposition { | ||
| 15 | EatWithUnsupportedMessage, | ||
| 16 | PassThrough, | ||
| 17 | } | ||
| 18 | |||
| 19 | impl IgnoreCommand { | ||
| 20 | pub fn create() -> Result<[CommandRun; 2], ()> { | ||
| 21 | Ok([ | ||
| 22 | CommandRun::new("2000|/ignore", IgnoreCommand)?, | ||
| 23 | CommandRun::new("2000|/ignore *", IgnoreCommand)?, | ||
| 24 | ]) | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | fn disposition(plugin_name: &str) -> IgnoreDisposition { | ||
| 29 | if plugin_name == PLUGIN_NAME { | ||
| 30 | IgnoreDisposition::EatWithUnsupportedMessage | ||
| 31 | } else { | ||
| 32 | IgnoreDisposition::PassThrough | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | impl CommandRunCallback for IgnoreCommand { | ||
| 37 | fn callback( | ||
| 38 | &mut self, | ||
| 39 | _: &Weechat, | ||
| 40 | buffer: &Buffer, | ||
| 41 | _: Cow<str>, | ||
| 42 | ) -> ReturnCode { | ||
| 43 | match disposition(&buffer.plugin_name()) { | ||
| 44 | IgnoreDisposition::EatWithUnsupportedMessage => { | ||
| 45 | buffer.print(&format!( | ||
| 46 | "{}{}: /ignore is not supported in Matrix buffers.", | ||
| 47 | Weechat::prefix(Prefix::Error), | ||
| 48 | PLUGIN_NAME, | ||
| 49 | )); | ||
| 50 | ReturnCode::OkEat | ||
| 51 | } | ||
| 52 | IgnoreDisposition::PassThrough => ReturnCode::Ok, | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | #[cfg(test)] | ||
| 58 | mod tests { | ||
| 59 | use super::{disposition, IgnoreDisposition}; | ||
| 60 | |||
| 61 | #[test] | ||
| 62 | fn eats_ignore_in_matrix_buffers() { | ||
| 63 | assert_eq!( | ||
| 64 | IgnoreDisposition::EatWithUnsupportedMessage, | ||
| 65 | disposition("matrix") | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | |||
| 69 | #[test] | ||
| 70 | fn passes_ignore_through_in_other_buffers() { | ||
| 71 | assert_eq!(IgnoreDisposition::PassThrough, disposition("irc")); | ||
| 72 | assert_eq!(IgnoreDisposition::PassThrough, disposition("core")); | ||
| 73 | } | ||
| 74 | } | ||
src/commands/mod.rs +4
| @@ -9,6 +9,7 @@ use crate::{config::ConfigHandle, Servers}; | |||
| 9 | 9 | ||
| 10 | mod buffer_clear; | 10 | mod buffer_clear; |
| 11 | mod devices; | 11 | mod devices; |
| 12 | mod ignore; | ||
| 12 | mod invite; | 13 | mod invite; |
| 13 | mod join; | 14 | mod join; |
| 14 | mod keys; | 15 | mod keys; |
| @@ -26,6 +27,7 @@ mod verification; | |||
| 26 | 27 | ||
| 27 | use buffer_clear::BufferClearCommand; | 28 | use buffer_clear::BufferClearCommand; |
| 28 | use devices::DevicesCommand; | 29 | use devices::DevicesCommand; |
| 30 | use ignore::IgnoreCommand; | ||
| 29 | use invite::InviteCommand; | 31 | use invite::InviteCommand; |
| 30 | use join::JoinCommand; | 32 | use join::JoinCommand; |
| 31 | use keys::KeysCommand; | 33 | use keys::KeysCommand; |
| @@ -45,6 +47,7 @@ pub struct Commands { | |||
| 45 | _keys: Command, | 47 | _keys: Command, |
| 46 | _devices: Command, | 48 | _devices: Command, |
| 47 | _invite: Command, | 49 | _invite: Command, |
| 50 | _ignore: [CommandRun; 2], | ||
| 48 | _ban: Command, | 51 | _ban: Command, |
| 49 | _kick: Command, | 52 | _kick: Command, |
| 50 | _page_up: CommandRun, | 53 | _page_up: CommandRun, |
| @@ -69,6 +72,7 @@ impl Commands { | |||
| 69 | _matrix: MatrixCommand::create(servers, config)?, | 72 | _matrix: MatrixCommand::create(servers, config)?, |
| 70 | _devices: DevicesCommand::create(servers)?, | 73 | _devices: DevicesCommand::create(servers)?, |
| 71 | _invite: InviteCommand::create(servers)?, | 74 | _invite: InviteCommand::create(servers)?, |
| 75 | _ignore: IgnoreCommand::create()?, | ||
| 72 | _ban: ModerationCommand::ban(servers)?, | 76 | _ban: ModerationCommand::ban(servers)?, |
| 73 | _kick: ModerationCommand::kick(servers)?, | 77 | _kick: ModerationCommand::kick(servers)?, |
| 74 | _keys: KeysCommand::create(servers)?, | 78 | _keys: KeysCommand::create(servers)?, |