split off event filtering into own module

This commit is contained in:
Adrian Malacoda 2018-02-23 00:08:13 -06:00
parent 72eadd4549
commit d9cfab7081
3 changed files with 99 additions and 94 deletions

96
src/event/filter.rs Normal file
View File

@ -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<String>,
username: Option<String>,
channel: Option<String>,
message: Option<String>
}
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
}
}

View File

@ -1,3 +1,5 @@
pub mod filter;
use {Message, Channel, User};
#[derive(Debug)]

View File

@ -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<Box<EventFilter>>
@ -233,93 +230,3 @@ impl Subscription {
true
}
}
struct AttributeEventFilter {
// Attributes that can be filtered out
event_type: Option<String>,
username: Option<String>,
channel: Option<String>,
message: Option<String>
}
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
}
}