mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-12-06 14:52:41 +01:00
99 lines
2.8 KiB
Rust
99 lines
2.8 KiB
Rust
use lettre::{transport::smtp::authentication::Credentials, SmtpTransport};
|
|
use log::{debug, error, warn};
|
|
use once_cell::sync::Lazy;
|
|
use sequoia_net::wkd::Variant;
|
|
use sequoia_openpgp::policy::StandardPolicy;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::path::PathBuf;
|
|
use url::Url;
|
|
|
|
use crate::{log_err, utils::read_file};
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct Settings {
|
|
#[serde(with = "VariantDef")]
|
|
pub variant: Variant,
|
|
pub max_age: i64,
|
|
pub cleanup_interval: u64,
|
|
pub allowed_domains: Vec<String>,
|
|
pub port: u16,
|
|
pub bind_host: String,
|
|
pub external_url: Url,
|
|
pub mail_settings: MailSettings,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct MailSettings {
|
|
pub smtp_host: String,
|
|
pub smtp_username: String,
|
|
pub smtp_password: String,
|
|
pub smtp_port: u16,
|
|
pub smtp_tls: SMTPEncryption,
|
|
pub mail_from: String,
|
|
pub mail_subject: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
#[serde(remote = "Variant")]
|
|
pub enum VariantDef {
|
|
Advanced,
|
|
Direct,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub enum SMTPEncryption {
|
|
Tls,
|
|
Starttls,
|
|
}
|
|
|
|
fn get_settings() -> Settings {
|
|
debug!("Parsing settings...");
|
|
let content = match read_file(&PathBuf::from("config.toml")) {
|
|
Ok(content) => content,
|
|
Err(_) => {
|
|
error!("Unable to access settings file!");
|
|
panic!("Unable to access settings file!")
|
|
}
|
|
};
|
|
let settings = match log_err!(toml::from_str(&content), error) {
|
|
Ok(settings) => settings,
|
|
Err(_) => {
|
|
error!("Unable to parse settings from file!");
|
|
panic!("Unable to parse settings from file!")
|
|
}
|
|
};
|
|
settings
|
|
}
|
|
|
|
fn get_mailer() -> SmtpTransport {
|
|
debug!("Setting up smtp...");
|
|
let creds = Credentials::new(
|
|
SETTINGS.mail_settings.smtp_username.to_owned(),
|
|
SETTINGS.mail_settings.smtp_password.to_owned(),
|
|
);
|
|
let builder = match &SETTINGS.mail_settings.smtp_tls {
|
|
SMTPEncryption::Tls => SmtpTransport::relay(&SETTINGS.mail_settings.smtp_host),
|
|
SMTPEncryption::Starttls => {
|
|
SmtpTransport::starttls_relay(&SETTINGS.mail_settings.smtp_host)
|
|
}
|
|
};
|
|
let mailer = match builder {
|
|
Ok(builder) => builder,
|
|
Err(_) => {
|
|
error!("Unable to set up smtp");
|
|
panic!("Unable to set up smtp")
|
|
}
|
|
}
|
|
.credentials(creds)
|
|
.port(SETTINGS.mail_settings.smtp_port)
|
|
.build();
|
|
debug!("Testing smtp connection...");
|
|
let _ = log_err!(mailer.test_connection(), warn);
|
|
mailer
|
|
}
|
|
|
|
pub const ERROR_TEXT: &str = "An error occoured:";
|
|
pub const POLICY: &StandardPolicy = &StandardPolicy::new();
|
|
pub const ROOT_FOLDER: &str = "data";
|
|
pub static SETTINGS: Lazy<Settings> = Lazy::new(get_settings);
|
|
pub static MAILER: Lazy<SmtpTransport> = Lazy::new(get_mailer);
|