opensmtpd-filter-dkimout/src/action.rs

45 lines
1.1 KiB
Rust
Raw Normal View History

2023-04-09 15:13:18 +02:00
use crate::config::Config;
use crate::entry::read_entry;
2023-04-09 17:21:17 +02:00
use crate::key::key_rotation;
2023-04-09 15:13:18 +02:00
use crate::message::Message;
use crate::stdin_reader::StdinReader;
2023-04-09 17:21:17 +02:00
use sqlx::SqlitePool;
2023-04-09 15:13:18 +02:00
use std::sync::Arc;
use tokio::sync::RwLock;
2023-04-09 23:31:16 +02:00
use tokio::time::sleep;
2023-04-09 15:13:18 +02:00
pub enum Action<'a> {
ReadLine(Arc<RwLock<StdinReader>>),
RotateKeys((&'a SqlitePool, &'a Config)),
2023-04-15 19:24:04 +02:00
SendMessage((&'a SqlitePool, &'a Config, Message)),
}
2023-04-09 15:13:18 +02:00
pub enum ActionResult {
EndOfStream,
2023-04-09 17:21:17 +02:00
KeyRotation,
2023-04-09 15:13:18 +02:00
MessageSent(String),
NewEntry(crate::entry::Entry),
NewEntryError(String),
}
pub async fn new_action(action: Action<'_>) -> ActionResult {
match action {
2023-04-09 17:54:07 +02:00
Action::ReadLine(reader_lock) => match read_entry(reader_lock).await {
Some(r) => match r {
Ok(entry) => ActionResult::NewEntry(entry),
2023-04-10 13:09:56 +02:00
Err(err) => ActionResult::NewEntryError(err.to_string()),
2023-04-09 17:54:07 +02:00
},
None => ActionResult::EndOfStream,
},
Action::RotateKeys((db, cnf)) => {
2023-04-09 23:31:16 +02:00
let duration = key_rotation(db, cnf).await;
sleep(duration).await;
ActionResult::KeyRotation
2023-04-09 17:21:17 +02:00
}
2023-04-15 19:24:04 +02:00
Action::SendMessage((db, cnf, msg)) => {
let msg_id = msg.sign_and_return(db, cnf).await;
2023-04-09 17:54:07 +02:00
ActionResult::MessageSent(msg_id)
}
2023-04-09 15:13:18 +02:00
}
}