Cleanup the code
This commit is contained in:
parent
45dc882b49
commit
ea710408d4
3 changed files with 12 additions and 14 deletions
|
@ -14,7 +14,7 @@ pub fn event(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||||
let fn_output = &item.decl.output;
|
let fn_output = &item.decl.output;
|
||||||
let output = quote! {
|
let output = quote! {
|
||||||
fn #fn_name() -> opensmtpd::EventHandler {
|
fn #fn_name() -> opensmtpd::EventHandler {
|
||||||
opensmtpd::EventHandler::new(#attr.to_string(), |#fn_params| #fn_output #fn_body)
|
opensmtpd::EventHandler::new(#attr, |#fn_params| #fn_output #fn_body)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
output.into()
|
output.into()
|
||||||
|
|
|
@ -15,32 +15,29 @@ pub struct EventHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventHandler {
|
impl EventHandler {
|
||||||
fn get_events_from_string(event_str: &String) -> MatchEvent {
|
fn get_events_from_string(event_str: &str) -> MatchEvent {
|
||||||
let mut events = Vec::new();
|
let mut events = Vec::new();
|
||||||
for name in event_str.split(" , ") {
|
for name in event_str.split(" , ") {
|
||||||
match name {
|
match name {
|
||||||
"Any" | "All" => {
|
"Any" | "All" => {
|
||||||
return MatchEvent::All;
|
return MatchEvent::All;
|
||||||
}
|
}
|
||||||
_ => match Event::from_str(name) {
|
_ => if let Ok(e) = Event::from_str(name) {
|
||||||
Ok(e) => {
|
events.push(e);
|
||||||
events.push(e);
|
|
||||||
}
|
|
||||||
Err(_) => {}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MatchEvent::Evt(events)
|
MatchEvent::Evt(events)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(event_str: String, callback: (fn(&Entry) -> Response)) -> Self {
|
pub fn new(event_str: &str, callback: (fn(&Entry) -> Response)) -> Self {
|
||||||
EventHandler {
|
EventHandler {
|
||||||
event: EventHandler::get_events_from_string(&event_str),
|
event: EventHandler::get_events_from_string(event_str),
|
||||||
callback,
|
callback,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_callable(&self, event: Event) -> bool {
|
pub fn is_callable(&self, event: &Event) -> bool {
|
||||||
match &self.event {
|
match &self.event {
|
||||||
MatchEvent::All => true,
|
MatchEvent::All => true,
|
||||||
MatchEvent::Evt(v) => v.contains(&event),
|
MatchEvent::Evt(v) => v.contains(&event),
|
||||||
|
|
|
@ -37,7 +37,7 @@ struct SessionHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SessionHandler {
|
impl SessionHandler {
|
||||||
fn new(entry_rx: mpsc::Receiver<Entry>, handlers_rx: mpsc::Receiver<EventHandler>) -> Self {
|
fn new(entry_rx: mpsc::Receiver<Entry>, handlers_rx: &mpsc::Receiver<EventHandler>) -> Self {
|
||||||
debug!(
|
debug!(
|
||||||
"New thread for session {}",
|
"New thread for session {}",
|
||||||
thread::current().name().unwrap()
|
thread::current().name().unwrap()
|
||||||
|
@ -56,7 +56,7 @@ impl SessionHandler {
|
||||||
fn read_entries(&self) {
|
fn read_entries(&self) {
|
||||||
for e in self.entry_rx.iter() {
|
for e in self.entry_rx.iter() {
|
||||||
for h in self.event_handlers.iter() {
|
for h in self.event_handlers.iter() {
|
||||||
if h.is_callable(e.event.clone()) {
|
if h.is_callable(&e.event) {
|
||||||
h.call(&e);
|
h.call(&e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,6 +64,7 @@ impl SessionHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct SmtpIn {
|
pub struct SmtpIn {
|
||||||
sessions: HashMap<u64, (mpsc::Sender<Entry>, thread::JoinHandle<()>)>,
|
sessions: HashMap<u64, (mpsc::Sender<Entry>, thread::JoinHandle<()>)>,
|
||||||
event_handlers: Vec<EventHandler>,
|
event_handlers: Vec<EventHandler>,
|
||||||
|
@ -94,7 +95,7 @@ impl SmtpIn {
|
||||||
let (entry_tx, entry_rx) = mpsc::channel();
|
let (entry_tx, entry_rx) = mpsc::channel();
|
||||||
let name = entry.session_id.to_string();
|
let name = entry.session_id.to_string();
|
||||||
let handle = thread::Builder::new().name(name).spawn(move || {
|
let handle = thread::Builder::new().name(name).spawn(move || {
|
||||||
SessionHandler::new(entry_rx, handlers_rx).read_entries();
|
SessionHandler::new(entry_rx, &handlers_rx).read_entries();
|
||||||
})?;
|
})?;
|
||||||
for h in self.event_handlers.iter() {
|
for h in self.event_handlers.iter() {
|
||||||
handlers_tx.send(h.clone())?;
|
handlers_tx.send(h.clone())?;
|
||||||
|
@ -133,7 +134,7 @@ impl SmtpIn {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn event_handlers(&mut self, handlers: Vec<EventHandler>) -> &mut Self {
|
pub fn event_handlers(&mut self, handlers: Vec<EventHandler>) -> &mut Self {
|
||||||
self.event_handlers = handlers.clone();
|
self.event_handlers = handlers.to_owned();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue