From 3e8ae797272e2cce949614a781031b3d4f19bcd2 Mon Sep 17 00:00:00 2001 From: naskya Date: Mon, 1 Jul 2024 20:02:58 +0900 Subject: [PATCH] add email config --- fishctl/src/command/config/update/v1.rs | 20 +++++++++++++++++++ fishctl/src/config/server.rs | 26 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/fishctl/src/command/config/update/v1.rs b/fishctl/src/command/config/update/v1.rs index 47b5b4b..5ffaa12 100644 --- a/fishctl/src/command/config/update/v1.rs +++ b/fishctl/src/command/config/update/v1.rs @@ -372,6 +372,25 @@ fn create_new_server_config( } }; + let auto_email = match meta.email.as_ref() { + Some(address) => Some(server::Email { + enabled: meta.enable_email, + address: address.to_owned(), + host: meta + .smtp_host + .to_owned() + .ok_or(Error::InvalidConfig("SMTP host is not set"))?, + port: meta + .smtp_port + .ok_or(Error::InvalidConfig("SMTP port is not set"))? + .try_into() + .map_err(|_| Error::InvalidConfig("SMTP port is out of range"))?, + user: meta.smtp_user.to_owned(), + password: meta.smtp_pass.to_owned(), + }), + None => None, + }; + let http_proxy_bypass_hosts = match default_yml.get("proxyBypassHosts") { Some(hosts) => Some( hosts @@ -468,6 +487,7 @@ fn create_new_server_config( .try_into() .map_err(|_| Error::InvalidConfig("Invalid `port` number"))?, }, + auto_email, http_proxy, media_proxy, summaly_proxy, diff --git a/fishctl/src/config/server.rs b/fishctl/src/config/server.rs index a751796..4238460 100644 --- a/fishctl/src/config/server.rs +++ b/fishctl/src/config/server.rs @@ -39,6 +39,9 @@ pub struct Network { #[validate(nested)] pub listen: Listen, #[validate(nested)] + #[validate(custom(function = "validate_email_struct"))] + pub auto_email: Option, + #[validate(nested)] pub http_proxy: Option, #[validate(nested)] pub media_proxy: Option, @@ -54,6 +57,17 @@ pub struct Listen { pub port: u16, } +#[derive(Deserialize, Serialize, Validate, Debug, Clone)] +pub struct Email { + pub enabled: bool, + #[validate(email)] + pub address: String, + pub host: String, + pub port: u16, + pub user: Option, + pub password: Option, +} + #[derive(Deserialize, Serialize, Validate, Debug, Clone)] pub struct Proxy { pub enabled: bool, @@ -161,6 +175,18 @@ pub enum Captcha { ReCaptcha, } +fn validate_email_struct(value: &Email) -> Result<(), ValidationError> { + match (value.user.as_ref(), value.password.as_ref()) { + (Some(_), None) => Err(ValidationError::new( + "SMTP username is not set even though the password is set", + )), + (None, Some(_)) => Err(ValidationError::new( + "SMTP password is not set even though the username is set", + )), + _ => Ok(()), + } +} + fn validate_emoji(value: &str) -> Result<(), ValidationError> { let error = Err(ValidationError::new("not a Unicode emoji or :emoji_code:"));