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 output = quote! {
|
||||
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()
|
||||
|
|
|
@ -15,32 +15,29 @@ pub struct 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();
|
||||
for name in event_str.split(" , ") {
|
||||
match name {
|
||||
"Any" | "All" => {
|
||||
return MatchEvent::All;
|
||||
}
|
||||
_ => match Event::from_str(name) {
|
||||
Ok(e) => {
|
||||
_ => if let Ok(e) = Event::from_str(name) {
|
||||
events.push(e);
|
||||
}
|
||||
Err(_) => {}
|
||||
},
|
||||
}
|
||||
}
|
||||
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 {
|
||||
event: EventHandler::get_events_from_string(&event_str),
|
||||
event: EventHandler::get_events_from_string(event_str),
|
||||
callback,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_callable(&self, event: Event) -> bool {
|
||||
pub fn is_callable(&self, event: &Event) -> bool {
|
||||
match &self.event {
|
||||
MatchEvent::All => true,
|
||||
MatchEvent::Evt(v) => v.contains(&event),
|
||||
|
|
|
@ -37,7 +37,7 @@ struct 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!(
|
||||
"New thread for session {}",
|
||||
thread::current().name().unwrap()
|
||||
|
@ -56,7 +56,7 @@ impl SessionHandler {
|
|||
fn read_entries(&self) {
|
||||
for e in self.entry_rx.iter() {
|
||||
for h in self.event_handlers.iter() {
|
||||
if h.is_callable(e.event.clone()) {
|
||||
if h.is_callable(&e.event) {
|
||||
h.call(&e);
|
||||
}
|
||||
}
|
||||
|
@ -64,6 +64,7 @@ impl SessionHandler {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SmtpIn {
|
||||
sessions: HashMap<u64, (mpsc::Sender<Entry>, thread::JoinHandle<()>)>,
|
||||
event_handlers: Vec<EventHandler>,
|
||||
|
@ -94,7 +95,7 @@ impl SmtpIn {
|
|||
let (entry_tx, entry_rx) = mpsc::channel();
|
||||
let name = entry.session_id.to_string();
|
||||
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() {
|
||||
handlers_tx.send(h.clone())?;
|
||||
|
@ -133,7 +134,7 @@ impl SmtpIn {
|
|||
}
|
||||
|
||||
pub fn event_handlers(&mut self, handlers: Vec<EventHandler>) -> &mut Self {
|
||||
self.event_handlers = handlers.clone();
|
||||
self.event_handlers = handlers.to_owned();
|
||||
self
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue