add email config

This commit is contained in:
naskya 2024-07-01 20:02:58 +09:00
parent ca8e0cd02b
commit 3e8ae79727
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
2 changed files with 46 additions and 0 deletions

View file

@ -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,

View file

@ -39,6 +39,9 @@ pub struct Network {
#[validate(nested)]
pub listen: Listen,
#[validate(nested)]
#[validate(custom(function = "validate_email_struct"))]
pub auto_email: Option<Email>,
#[validate(nested)]
pub http_proxy: Option<Proxy>,
#[validate(nested)]
pub media_proxy: Option<Proxy>,
@ -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<String>,
pub password: Option<String>,
}
#[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:"));