From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 1BEF71FF16F for ; Tue, 22 Jul 2025 14:02:16 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 727598742; Tue, 22 Jul 2025 14:03:20 +0200 (CEST) From: Lukas Wagner To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com Date: Tue, 22 Jul 2025 14:02:21 +0200 Message-ID: <20250722120305.326096-2-l.wagner@proxmox.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250722120305.326096-1-l.wagner@proxmox.com> References: <20250722120305.326096-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1753185787203 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.018 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [mail.rs, test.rs] Subject: [pve-devel] [PATCH proxmox 2/2] notify: sendmail: smtp: list email recipients in stable order X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-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 --- 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 { - Some("test@example.com".into()) + fn lookup_email_for_user(&self, user: &str) -> Option { + 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 { - 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 { + 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