From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <l.wagner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 7DC1B9C0CC
 for <pve-devel@lists.proxmox.com>; Tue, 21 Nov 2023 16:53:37 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 63964CFCC
 for <pve-devel@lists.proxmox.com>; Tue, 21 Nov 2023 16:53:07 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Tue, 21 Nov 2023 16:53:06 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 2F3D141D2B
 for <pve-devel@lists.proxmox.com>; Tue, 21 Nov 2023 16:53:06 +0100 (CET)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 21 Nov 2023 16:53:03 +0100
Message-Id: <20231121155303.284443-1-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.008 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
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pve-devel] [PATCH proxmox-mail-forward] do not forward on PBS
 systems if co-installed with PVE
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 21 Nov 2023 15:53:37 -0000

With the newly built-in targets/matchers, we should not add
a target/matcher manually any more. In fact, this broke mail
forwarding on PBS because 'default-matcher' already existed as a
built-in and could thus not be created.
We now simply do an early return.

Also initialize notify-context before instantiating the config,
since that already requires the context to be set.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 src/main.rs | 38 ++++++++++++++------------------------
 1 file changed, 14 insertions(+), 24 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index e56bc1e..4662ffa 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -25,8 +25,6 @@ use anyhow::Error;
 
 use proxmox_notify::context::pbs::PBS_CONTEXT;
 use proxmox_notify::context::pve::PVE_CONTEXT;
-use proxmox_notify::endpoints::sendmail::SendmailConfig;
-use proxmox_notify::matcher::MatcherConfig;
 use proxmox_notify::Config;
 use proxmox_sys::fs;
 
@@ -74,48 +72,40 @@ fn forward_common(mail: &[u8], config: &Config) -> Result<(), Error> {
 
 /// Forward a mail to PVE's notification system
 fn forward_for_pve(mail: &[u8]) -> Result<(), Error> {
+    proxmox_notify::context::set_context(&PVE_CONTEXT);
     let config = attempt_file_read(PVE_PUB_NOTIFICATION_CFG_FILENAME).unwrap_or_default();
     let priv_config = attempt_file_read(PVE_PRIV_NOTIFICATION_CFG_FILENAME).unwrap_or_default();
 
     let config = Config::new(&config, &priv_config)?;
 
-    proxmox_notify::context::set_context(&PVE_CONTEXT);
     forward_common(mail, &config)
 }
 
 /// Forward a mail to PBS's notification system
 fn forward_for_pbs(mail: &[u8], has_pve: bool) -> Result<(), Error> {
+    proxmox_notify::context::set_context(&PBS_CONTEXT);
+
     let config = if Path::new(PBS_PUB_NOTIFICATION_CFG_FILENAME).exists() {
         let config = attempt_file_read(PBS_PUB_NOTIFICATION_CFG_FILENAME).unwrap_or_default();
         let priv_config = attempt_file_read(PBS_PRIV_NOTIFICATION_CFG_FILENAME).unwrap_or_default();
 
         Config::new(&config, &priv_config)?
     } else {
-        // TODO: This can be removed once PBS has full notification integration
-        let mut config = Config::new("", "")?;
-        if !has_pve {
-            proxmox_notify::api::sendmail::add_endpoint(
-                &mut config,
-                &SendmailConfig {
-                    name: "default-target".to_string(),
-                    mailto_user: Some(vec!["root@pam".to_string()]),
-                    ..Default::default()
-                },
-            )?;
-
-            proxmox_notify::api::matcher::add_matcher(
-                &mut config,
-                &MatcherConfig {
-                    name: "default-matcher".to_string(),
-                    target: Some(vec!["default-target".to_string()]),
-                    ..Default::default()
-                },
-            )?;
+        // Instantiate empty config.
+        // Note: This will contain the default built-in targets/matchers.
+        let config = Config::new("", "")?;
+        if has_pve {
+            // Skip forwarding if we are co-installed with PVE AND
+            // we do not have our own notifications.cfg file yet
+            // --> We assume that PVE has a sane matcher configured that
+            // forwards the mail properly
+            // TODO: This can be removed once PBS has full notification integration
+
+            return Ok(());
         }
         config
     };
 
-    proxmox_notify::context::set_context(&PBS_CONTEXT);
     forward_common(mail, &config)?;
 
     Ok(())
-- 
2.39.2