all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox 1/2] notify: warn if a user referred to by 'mailto-user' does not have an email address
@ 2025-07-22 12:02 ` Lukas Wagner
  0 siblings, 0 replies; 6+ messages in thread
From: Lukas Wagner @ 2025-07-22 12:02 UTC (permalink / raw)
  To: pve-devel, pbs-devel

Showing warnings for any email address that could not be looked up seems
appropriate. If the final recipient list is completely empty (as in,
attempting to send an email with no recipients), an error is still
raised.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-notify/src/endpoints/common/mail.rs | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/proxmox-notify/src/endpoints/common/mail.rs b/proxmox-notify/src/endpoints/common/mail.rs
index b1c48236..7a2de59f 100644
--- a/proxmox-notify/src/endpoints/common/mail.rs
+++ b/proxmox-notify/src/endpoints/common/mail.rs
@@ -10,8 +10,14 @@ pub(crate) fn get_recipients(email_addrs: &[String], users: &[String]) -> HashSe
     }
 
     for user in users {
-        if let Some(addr) = context::context().lookup_email_for_user(user) {
-            recipients.insert(addr);
+        match context::context().lookup_email_for_user(user) {
+            Some(address) => {
+                recipients.insert(address);
+            }
+            None => tracing::warn!(
+                "'{user}' does not have a configured email address in the user configuration - \
+                not sending an email to this user"
+            ),
         }
     }
     recipients
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH proxmox 1/2] notify: warn if a user referred to by 'mailto-user' does not have an email address
@ 2025-07-22 12:02 ` Lukas Wagner
  0 siblings, 0 replies; 6+ messages in thread
From: Lukas Wagner @ 2025-07-22 12:02 UTC (permalink / raw)
  To: pve-devel, pbs-devel

Showing warnings for any email address that could not be looked up seems
appropriate. If the final recipient list is completely empty (as in,
attempting to send an email with no recipients), an error is still
raised.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-notify/src/endpoints/common/mail.rs | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/proxmox-notify/src/endpoints/common/mail.rs b/proxmox-notify/src/endpoints/common/mail.rs
index b1c48236..7a2de59f 100644
--- a/proxmox-notify/src/endpoints/common/mail.rs
+++ b/proxmox-notify/src/endpoints/common/mail.rs
@@ -10,8 +10,14 @@ pub(crate) fn get_recipients(email_addrs: &[String], users: &[String]) -> HashSe
     }
 
     for user in users {
-        if let Some(addr) = context::context().lookup_email_for_user(user) {
-            recipients.insert(addr);
+        match context::context().lookup_email_for_user(user) {
+            Some(address) => {
+                recipients.insert(address);
+            }
+            None => tracing::warn!(
+                "'{user}' does not have a configured email address in the user configuration - \
+                not sending an email to this user"
+            ),
         }
     }
     recipients
-- 
2.47.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pbs-devel] [PATCH proxmox 2/2] notify: sendmail: smtp: list email recipients in stable order
  2025-07-22 12:02 ` [pve-devel] " Lukas Wagner
@ 2025-07-22 12:02   ` Lukas Wagner
  -1 siblings, 0 replies; 6+ messages in thread
From: Lukas Wagner @ 2025-07-22 12:02 UTC (permalink / raw)
  To: pve-devel, pbs-devel

The get_recipients function, which is used by the smtp and sendmail
target, is used to look up user's email addresses from the user
configuration while avoiding duplicate recipients at the same time.

Due to the use of a HashSet to avoid duplicate addresses, the order of
the email addresses was not stable. Only a minor cosmetic detail, but
using the same order as defined in the target config is definitely
nicer, which is achieved by using a plain old Vec instead, adding
.contains(...) checks as needed.

Also add some basic rust doc and test while at it.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-notify/src/context/test.rs          | 10 ++++-
 proxmox-notify/src/endpoints/common/mail.rs | 49 ++++++++++++++++++---
 2 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/proxmox-notify/src/context/test.rs b/proxmox-notify/src/context/test.rs
index 13ef6eae..2c236b4c 100644
--- a/proxmox-notify/src/context/test.rs
+++ b/proxmox-notify/src/context/test.rs
@@ -6,8 +6,14 @@ use crate::Error;
 pub struct TestContext;
 
 impl Context for TestContext {
-    fn lookup_email_for_user(&self, _user: &str) -> Option<String> {
-        Some("test@example.com".into())
+    fn lookup_email_for_user(&self, user: &str) -> Option<String> {
+        user.split_once('@').and_then(|(user, realm)| {
+            if realm == "pve" {
+                Some(format!("{user}@example.com"))
+            } else {
+                None
+            }
+        })
     }
 
     fn default_sendmail_author(&self) -> String {
diff --git a/proxmox-notify/src/endpoints/common/mail.rs b/proxmox-notify/src/endpoints/common/mail.rs
index 7a2de59f..074712cf 100644
--- a/proxmox-notify/src/endpoints/common/mail.rs
+++ b/proxmox-notify/src/endpoints/common/mail.rs
@@ -1,18 +1,25 @@
-use std::collections::HashSet;
-
 use crate::context;
 
-pub(crate) fn get_recipients(email_addrs: &[String], users: &[String]) -> HashSet<String> {
-    let mut recipients = HashSet::new();
+/// Get list of email recipients from a list of email addresses and users.
+///
+/// Any user passed in the user list will be looked up in the user configuration to
+/// obtain this user's email address. The list of returned email addresses does
+/// not contain any duplicates.
+pub(crate) fn get_recipients(email_addrs: &[String], users: &[String]) -> Vec<String> {
+    let mut recipients = Vec::new();
 
     for addr in email_addrs {
-        recipients.insert(addr.clone());
+        if !recipients.contains(addr) {
+            recipients.push(addr.clone());
+        }
     }
 
     for user in users {
         match context::context().lookup_email_for_user(user) {
             Some(address) => {
-                recipients.insert(address);
+                if !recipients.contains(&address) {
+                    recipients.push(address);
+                }
             }
             None => tracing::warn!(
                 "'{user}' does not have a configured email address in the user configuration - \
@@ -22,3 +29,33 @@ pub(crate) fn get_recipients(email_addrs: &[String], users: &[String]) -> HashSe
     }
     recipients
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_get_recipients() {
+        let emails = Vec::from(
+            [
+                "test1@example.com",
+                "test2@example.com",
+                "test2@example.com",
+            ]
+            .map(Into::into),
+        );
+        let users =
+            Vec::from(["user1@pve", "user2@pve", "user2@pve", "user3@invalid"].map(Into::into));
+
+        let expected = [
+            "test1@example.com",
+            "test2@example.com",
+            "user1@example.com",
+            "user2@example.com",
+        ];
+
+        let addrs = get_recipients(&emails, &users);
+
+        assert_eq!(addrs, expected);
+    }
+}
-- 
2.47.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] [PATCH proxmox 2/2] notify: sendmail: smtp: list email recipients in stable order
@ 2025-07-22 12:02   ` Lukas Wagner
  0 siblings, 0 replies; 6+ messages in thread
From: Lukas Wagner @ 2025-07-22 12:02 UTC (permalink / raw)
  To: pve-devel, pbs-devel

The get_recipients function, which is used by the smtp and sendmail
target, is used to look up user's email addresses from the user
configuration while avoiding duplicate recipients at the same time.

Due to the use of a HashSet to avoid duplicate addresses, the order of
the email addresses was not stable. Only a minor cosmetic detail, but
using the same order as defined in the target config is definitely
nicer, which is achieved by using a plain old Vec instead, adding
.contains(...) checks as needed.

Also add some basic rust doc and test while at it.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-notify/src/context/test.rs          | 10 ++++-
 proxmox-notify/src/endpoints/common/mail.rs | 49 ++++++++++++++++++---
 2 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/proxmox-notify/src/context/test.rs b/proxmox-notify/src/context/test.rs
index 13ef6eae..2c236b4c 100644
--- a/proxmox-notify/src/context/test.rs
+++ b/proxmox-notify/src/context/test.rs
@@ -6,8 +6,14 @@ use crate::Error;
 pub struct TestContext;
 
 impl Context for TestContext {
-    fn lookup_email_for_user(&self, _user: &str) -> Option<String> {
-        Some("test@example.com".into())
+    fn lookup_email_for_user(&self, user: &str) -> Option<String> {
+        user.split_once('@').and_then(|(user, realm)| {
+            if realm == "pve" {
+                Some(format!("{user}@example.com"))
+            } else {
+                None
+            }
+        })
     }
 
     fn default_sendmail_author(&self) -> String {
diff --git a/proxmox-notify/src/endpoints/common/mail.rs b/proxmox-notify/src/endpoints/common/mail.rs
index 7a2de59f..074712cf 100644
--- a/proxmox-notify/src/endpoints/common/mail.rs
+++ b/proxmox-notify/src/endpoints/common/mail.rs
@@ -1,18 +1,25 @@
-use std::collections::HashSet;
-
 use crate::context;
 
-pub(crate) fn get_recipients(email_addrs: &[String], users: &[String]) -> HashSet<String> {
-    let mut recipients = HashSet::new();
+/// Get list of email recipients from a list of email addresses and users.
+///
+/// Any user passed in the user list will be looked up in the user configuration to
+/// obtain this user's email address. The list of returned email addresses does
+/// not contain any duplicates.
+pub(crate) fn get_recipients(email_addrs: &[String], users: &[String]) -> Vec<String> {
+    let mut recipients = Vec::new();
 
     for addr in email_addrs {
-        recipients.insert(addr.clone());
+        if !recipients.contains(addr) {
+            recipients.push(addr.clone());
+        }
     }
 
     for user in users {
         match context::context().lookup_email_for_user(user) {
             Some(address) => {
-                recipients.insert(address);
+                if !recipients.contains(&address) {
+                    recipients.push(address);
+                }
             }
             None => tracing::warn!(
                 "'{user}' does not have a configured email address in the user configuration - \
@@ -22,3 +29,33 @@ pub(crate) fn get_recipients(email_addrs: &[String], users: &[String]) -> HashSe
     }
     recipients
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_get_recipients() {
+        let emails = Vec::from(
+            [
+                "test1@example.com",
+                "test2@example.com",
+                "test2@example.com",
+            ]
+            .map(Into::into),
+        );
+        let users =
+            Vec::from(["user1@pve", "user2@pve", "user2@pve", "user3@invalid"].map(Into::into));
+
+        let expected = [
+            "test1@example.com",
+            "test2@example.com",
+            "user1@example.com",
+            "user2@example.com",
+        ];
+
+        let addrs = get_recipients(&emails, &users);
+
+        assert_eq!(addrs, expected);
+    }
+}
-- 
2.47.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pbs-devel] applied: [pve-devel] [PATCH proxmox 1/2] notify: warn if a user referred to by 'mailto-user' does not have an email address
  2025-07-22 12:02 ` [pve-devel] " Lukas Wagner
@ 2025-07-23 18:54   ` Thomas Lamprecht
  -1 siblings, 0 replies; 6+ messages in thread
From: Thomas Lamprecht @ 2025-07-23 18:54 UTC (permalink / raw)
  To: pve-devel, pbs-devel, Lukas Wagner

On Tue, 22 Jul 2025 14:02:20 +0200, Lukas Wagner wrote:
> Showing warnings for any email address that could not be looked up seems
> appropriate. If the final recipient list is completely empty (as in,
> attempting to send an email with no recipients), an error is still
> raised.
> 
> 

Applied, thanks!

[1/2] notify: warn if a user referred to by 'mailto-user' does not have an email address
      commit: ab08733f74eab0b5dd7dd4d95f01c8aa607c5077
[2/2] notify: sendmail: smtp: list email recipients in stable order
      commit: 862dba538e4fe1c29c8332ea8a2f9ad843925199


_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [pve-devel] applied: [PATCH proxmox 1/2] notify: warn if a user referred to by 'mailto-user' does not have an email address
@ 2025-07-23 18:54   ` Thomas Lamprecht
  0 siblings, 0 replies; 6+ messages in thread
From: Thomas Lamprecht @ 2025-07-23 18:54 UTC (permalink / raw)
  To: pve-devel, pbs-devel, Lukas Wagner

On Tue, 22 Jul 2025 14:02:20 +0200, Lukas Wagner wrote:
> Showing warnings for any email address that could not be looked up seems
> appropriate. If the final recipient list is completely empty (as in,
> attempting to send an email with no recipients), an error is still
> raised.
> 
> 

Applied, thanks!

[1/2] notify: warn if a user referred to by 'mailto-user' does not have an email address
      commit: ab08733f74eab0b5dd7dd4d95f01c8aa607c5077
[2/2] notify: sendmail: smtp: list email recipients in stable order
      commit: 862dba538e4fe1c29c8332ea8a2f9ad843925199


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-07-23 18:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-07-22 12:02 [pbs-devel] [PATCH proxmox 1/2] notify: warn if a user referred to by 'mailto-user' does not have an email address Lukas Wagner
2025-07-22 12:02 ` [pve-devel] " Lukas Wagner
2025-07-22 12:02 ` [pbs-devel] [PATCH proxmox 2/2] notify: sendmail: smtp: list email recipients in stable order Lukas Wagner
2025-07-22 12:02   ` [pve-devel] " Lukas Wagner
2025-07-23 18:54 ` [pbs-devel] applied: [pve-devel] [PATCH proxmox 1/2] notify: warn if a user referred to by 'mailto-user' does not have an email address Thomas Lamprecht
2025-07-23 18:54   ` [pve-devel] applied: " Thomas Lamprecht

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal