diff --git a/conduwuit-example.toml b/conduwuit-example.toml index a980efaf3..9a478fa0c 100644 --- a/conduwuit-example.toml +++ b/conduwuit-example.toml @@ -2174,7 +2174,11 @@ #connection_uri = # Same as `connection_uri`, except loaded from a file. If `connection_uri` -# is defined, it is prioritized and `connection_uri_file` is ignored +# is defined, it is prioritized and `connection_uri_file` is ignored. +# +# The file will be read once, when Continuwuity starts. It is not +# currently reread when the server configuration is reloaded. If the file +# cannot be read, Continuwuity will fail to start. # #connection_uri_file = diff --git a/src/core/config/check.rs b/src/core/config/check.rs index 4b053403a..866d330cf 100644 --- a/src/core/config/check.rs +++ b/src/core/config/check.rs @@ -1,4 +1,4 @@ -use std::{env::consts::OS, path::PathBuf}; +use std::env::consts::OS; use either::Either; use figment::Figment; @@ -264,14 +264,14 @@ pub fn check(config: &Config) -> Result { } if config.smtp.as_ref().is_some_and(|f| { - f.connection_uri.is_none() && f.connection_uri_file.is_none() - || (f.connection_uri == Some(String::new()) - && f.connection_uri_file == Some(PathBuf::new())) + f.connection_uri.as_ref().is_none_or(String::is_empty) + && f.connection_uri_file + .as_ref() + .is_none_or(|f| f.as_os_str().is_empty()) }) { return Err!(Config( "connection_uri", - "If smtp is enabled, either connection_uri or connection_uri_file has to be \ - defined." + "If smtp is enabled, either connection_uri or connection_uri_file has to be defined." )); } diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index ea6aeb5e8..13358d59b 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -2592,7 +2592,11 @@ pub struct SmtpConfig { pub connection_uri: Option, /// Same as `connection_uri`, except loaded from a file. If `connection_uri` - /// is defined, it is prioritized and `connection_uri_file` is ignored + /// is defined, it is prioritized and `connection_uri_file` is ignored. + /// + /// The file will be read once, when Continuwuity starts. It is not + /// currently reread when the server configuration is reloaded. If the file + /// cannot be read, Continuwuity will fail to start. pub connection_uri_file: Option, /// The outgoing address which will be used for sending emails. diff --git a/src/service/mailer/mod.rs b/src/service/mailer/mod.rs index 9126769c1..cefecfdcb 100644 --- a/src/service/mailer/mod.rs +++ b/src/service/mailer/mod.rs @@ -20,28 +20,27 @@ pub struct Service { #[async_trait::async_trait] impl crate::Service for Service { fn build(args: Args<'_>) -> Result> { - let transport = - args.server - .config - .smtp - .as_ref() - .map(|config| -> Result<_> { - let connection_uri: String = - if let Some(uri) = &config.connection_uri { - uri.clone() - } else { - std::fs::read_to_string( - config.connection_uri_file.as_ref().unwrap() // Safe to unwrap here assuming the config checks properly run and throw errors - ) - .map_err(|err| err!("Failed to read SMTP connection URI file: {err}"))? - }; - let transport = Transport::from_url(&connection_uri) - .map_err(|err| err!("Failed to set up SMTP transport: {err}"))? - .build(); + let transport = args + .server + .config + .smtp + .as_ref() + .map(|config| -> Result<_> { + let connection_uri = match (&config.connection_uri, &config.connection_uri_file) { + | (Some(uri), _) => uri.clone(), + | (None, Some(path)) => std::fs::read_to_string(path) + .map_err(|err| err!("Failed to read SMTP connection URI file: {err}"))?, + | (None, None) => unreachable!( + "Config validation should guarantee one SMTP connection source." + ), + }; + let transport = Transport::from_url(&connection_uri) + .map_err(|err| err!("Failed to set up SMTP transport: {err}"))? + .build(); - Ok((config.sender.clone(), transport)) - }) - .transpose()?; + Ok((config.sender.clone(), transport)) + }) + .transpose()?; Ok(Arc::new(Self { transport })) }