97 lines
3.5 KiB
Rust
Executable File
97 lines
3.5 KiB
Rust
Executable File
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
|
|
}
|
|
}
|