[pve-devel] [PATCH v3 proxmox-perl-rs 31/66] notify: implement context for getting default author/mailfrom

Lukas Wagner l.wagner at proxmox.com
Mon Jul 17 17:00:16 CEST 2023


Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 pve-rs/src/notify.rs | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/pve-rs/src/notify.rs b/pve-rs/src/notify.rs
index ea34bfe..04e902c 100644
--- a/pve-rs/src/notify.rs
+++ b/pve-rs/src/notify.rs
@@ -34,6 +34,14 @@ fn lookup_mail_address(content: &str, user: &str) -> Option<String> {
     }))
 }
 
+fn lookup_datacenter_config_key(content: &str, key: &str) -> Option<String> {
+    normalize_for_return(
+        content
+            .lines()
+            .find_map(|line| line.strip_prefix(&format!("{key}:"))),
+    )
+}
+
 struct PVEContext;
 
 impl Context for PVEContext {
@@ -41,11 +49,22 @@ impl Context for PVEContext {
         let content = attempt_file_read("/etc/pve/user.cfg");
         content.and_then(|content| lookup_mail_address(&content, user))
     }
+
+    fn default_sendmail_author(&self) -> String {
+        "Proxmox VE".into()
+    }
+
+    fn default_sendmail_from(&self) -> String {
+        let content = attempt_file_read("/etc/pve/datacenter.cfg");
+        content
+            .and_then(|content| lookup_datacenter_config_key(&content, "mail_from"))
+            .unwrap_or_else(|| String::from("root"))
+    }
 }
 
 #[cfg(test)]
 mod tests {
-    use crate::notify::lookup_mail_address;
+    use crate::notify::{lookup_datacenter_config_key, lookup_mail_address};
 
     const USER_CONFIG: &str = "
 user:root at pam:1:0:::root at example.com:::
@@ -65,6 +84,18 @@ user:no-mail at pve:1:0::::::
         );
         assert_eq!(lookup_mail_address(USER_CONFIG, "no-mail at pve"), None);
     }
+
+    const DC_CONFIG: &str = "
+email_from: user at example.com
+keyboard: en-us
+";
+    #[test]
+    fn test_parse_dc_config() {
+        assert_eq!(
+            lookup_datacenter_config_key(DC_CONFIG, "email_from"),
+            Some("user at example.com".to_string())
+        );
+    }
 }
 
 static CONTEXT: PVEContext = PVEContext;
-- 
2.39.2






More information about the pve-devel mailing list