diff --git a/src/event/filter.rs b/src/event/filter.rs new file mode 100644 index 0000000..dc5b83f --- /dev/null +++ b/src/event/filter.rs @@ -0,0 +1,96 @@ +use toml::value::Table; +use event::{Envelope, Event}; + +pub trait EventFilter: Sync + Send { + fn accept (&self, envelope: &Envelope) -> bool; +} + +pub struct AttributeEventFilter { + // Attributes that can be filtered out + event_type: Option, + username: Option, + channel: Option, + message: Option +} + +impl AttributeEventFilter { + pub fn new (attributes: &Table) -> AttributeEventFilter { + AttributeEventFilter { + event_type: attributes.get("type").and_then(|value| value.as_str()).map(|value| String::from(value)), + message: attributes.get("message").and_then(|value| value.as_str()).map(|value| String::from(value)), + username: attributes.get("username").and_then(|value| value.as_str()).map(|value| String::from(value)), + channel: attributes.get("channel").and_then(|value| value.as_str()).map(|value| String::from(value)), + } + } +} + +impl EventFilter for AttributeEventFilter { + fn accept (&self, envelope: &Envelope) -> bool { + let mut result = true; + match &envelope.event { + &Event::Message { ref message } => { + if let Some(ref event_type) = self.event_type { + result = result && event_type == "message"; + } + + if let Some(ref channel_name) = self.channel { + match message.channel { + Some(ref channel) => result = result && channel_name == &channel.name, + None => result = false + } + } + + if let Some(ref username) = self.username { + result = result && &message.author.name == username; + } + }, + &Event::SelfJoin { ref channel } => { + if let Some(ref event_type) = self.event_type { + result = result && event_type == "selfjoin"; + } + + if let Some(ref channel_name) = self.channel { + result = result && channel_name == &channel.name; + } + }, + &Event::SelfQuit { ref channel } => { + if let Some(ref event_type) = self.event_type { + result = result && event_type == "selfquit"; + } + + if let Some(ref channel_name) = self.channel { + result = result && channel_name == &channel.name; + } + }, + &Event::UserJoin { ref channel, ref user } => { + if let Some(ref event_type) = self.event_type { + result = result && event_type == "userjoin"; + } + + if let Some(ref channel_name) = self.channel { + result = result && channel_name == &channel.name; + } + + if let Some(ref username) = self.username { + result = result && &user.name == username; + } + }, + &Event::UserQuit { ref channel, ref user } => { + if let Some(ref event_type) = self.event_type { + result = result && event_type == "userquit"; + } + + if let Some(ref channel_name) = self.channel { + result = result && channel_name == &channel.name; + } + + if let Some(ref username) = self.username { + result = result && &user.name == username; + } + }, + _ => {} + } + + result + } +} diff --git a/src/event/mod.rs b/src/event/mod.rs index 68911da..18fc122 100644 --- a/src/event/mod.rs +++ b/src/event/mod.rs @@ -1,3 +1,5 @@ +pub mod filter; + use {Message, Channel, User}; #[derive(Debug)] diff --git a/src/lib.rs b/src/lib.rs index b189186..ce7775f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ use modules::loader::{ModuleLoader, ModuleLoaderError}; mod event; use event::Event; use event::Envelope; +use event::filter::{EventFilter, AttributeEventFilter}; use std::sync::Arc; use std::sync::mpsc; @@ -182,10 +183,6 @@ pub trait MessageSender : Sync + Send + std::fmt::Debug { fn send_message (&self, _: &str) {} } -trait EventFilter: Sync + Send { - fn accept (&self, envelope: &Envelope) -> bool; -} - struct Subscription { pub name: String, pub filters: Vec> @@ -233,93 +230,3 @@ impl Subscription { true } } - -struct AttributeEventFilter { - // Attributes that can be filtered out - event_type: Option, - username: Option, - channel: Option, - message: Option -} - -impl AttributeEventFilter { - pub fn new (attributes: &Table) -> AttributeEventFilter { - AttributeEventFilter { - event_type: attributes.get("type").and_then(|value| value.as_str()).map(|value| String::from(value)), - message: attributes.get("message").and_then(|value| value.as_str()).map(|value| String::from(value)), - username: attributes.get("username").and_then(|value| value.as_str()).map(|value| String::from(value)), - channel: attributes.get("channel").and_then(|value| value.as_str()).map(|value| String::from(value)), - } - } -} - -impl EventFilter for AttributeEventFilter { - fn accept (&self, envelope: &Envelope) -> bool { - let mut result = true; - match &envelope.event { - &Event::Message { ref message } => { - if let Some(ref event_type) = self.event_type { - result = result && event_type == "message"; - } - - if let Some(ref channel_name) = self.channel { - match message.channel { - Some(ref channel) => result = result && channel_name == &channel.name, - None => result = false - } - } - - if let Some(ref username) = self.username { - result = result && &message.author.name == username; - } - }, - &Event::SelfJoin { ref channel } => { - if let Some(ref event_type) = self.event_type { - result = result && event_type == "selfjoin"; - } - - if let Some(ref channel_name) = self.channel { - result = result && channel_name == &channel.name; - } - }, - &Event::SelfQuit { ref channel } => { - if let Some(ref event_type) = self.event_type { - result = result && event_type == "selfquit"; - } - - if let Some(ref channel_name) = self.channel { - result = result && channel_name == &channel.name; - } - }, - &Event::UserJoin { ref channel, ref user } => { - if let Some(ref event_type) = self.event_type { - result = result && event_type == "userjoin"; - } - - if let Some(ref channel_name) = self.channel { - result = result && channel_name == &channel.name; - } - - if let Some(ref username) = self.username { - result = result && &user.name == username; - } - }, - &Event::UserQuit { ref channel, ref user } => { - if let Some(ref event_type) = self.event_type { - result = result && event_type == "userquit"; - } - - if let Some(ref channel_name) = self.channel { - result = result && channel_name == &channel.name; - } - - if let Some(ref username) = self.username { - result = result && &user.name == username; - } - }, - _ => {} - } - - result - } -}