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 DDADB99AAE
 for <pve-devel@lists.proxmox.com>; Tue, 14 Nov 2023 14:00:10 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id AABF61FCFC
 for <pve-devel@lists.proxmox.com>; Tue, 14 Nov 2023 14:00:10 +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, 14 Nov 2023 14:00:09 +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 D053B4298C
 for <pve-devel@lists.proxmox.com>; Tue, 14 Nov 2023 14:00:08 +0100 (CET)
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 14 Nov 2023 13:59:16 +0100
Message-Id: <20231114130000.565122-9-l.wagner@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20231114130000.565122-1-l.wagner@proxmox.com>
References: <20231114130000.565122-1-l.wagner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.012 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 v2 proxmox 08/52] notify: let a matcher always
 match if it has no matching directives
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, 14 Nov 2023 13:00:10 -0000

This should be a bit more intuitive to users than the current
behavior, which is 'always match' for mode==all and 'never match' for
mode==any. The current behavior originates in the neutral element of
the underlying logical operation (and, or).

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 proxmox-notify/src/matcher.rs | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/proxmox-notify/src/matcher.rs b/proxmox-notify/src/matcher.rs
index e299fd0..553ca87 100644
--- a/proxmox-notify/src/matcher.rs
+++ b/proxmox-notify/src/matcher.rs
@@ -265,17 +265,22 @@ impl MatcherConfig {
         let mode = self.mode.unwrap_or_default();
 
         let mut is_match = mode.neutral_element();
+        // If there are no matching directives, the matcher will always match
+        let mut no_matchers = true;
 
         if let Some(severity_matchers) = self.match_severity.as_deref() {
+            no_matchers = false;
             is_match = mode.apply(
                 is_match,
                 self.check_matches(notification, severity_matchers)?,
             );
         }
         if let Some(field_matchers) = self.match_field.as_deref() {
+            no_matchers = false;
             is_match = mode.apply(is_match, self.check_matches(notification, field_matchers)?);
         }
         if let Some(calendar_matchers) = self.match_calendar.as_deref() {
+            no_matchers = false;
             is_match = mode.apply(
                 is_match,
                 self.check_matches(notification, calendar_matchers)?,
@@ -284,7 +289,7 @@ impl MatcherConfig {
 
         let invert_match = self.invert_match.unwrap_or_default();
 
-        Ok(if is_match != invert_match {
+        Ok(if is_match != invert_match || no_matchers {
             Some(self.target.as_deref().unwrap_or_default())
         } else {
             None
@@ -455,4 +460,25 @@ mod tests {
         let matcher: SeverityMatcher = "info,notice,warning,error".parse().unwrap();
         assert!(matcher.matches(&notification).unwrap());
     }
+
+    #[test]
+    fn test_empty_matcher_matches_always() {
+        let notification = Notification::new_templated(
+            Severity::Notice,
+            "test",
+            "test",
+            Value::Null,
+            Default::default(),
+        );
+
+        for mode in [MatchModeOperator::All, MatchModeOperator::Any] {
+            let config = MatcherConfig {
+                name: "matcher".to_string(),
+                mode: Some(mode),
+                ..Default::default()
+            };
+
+            assert!(config.matches(&notification).unwrap().is_some())
+        }
+    }
 }
-- 
2.39.2