public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Leo Nunner <l.nunner@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api 8/8] feature: match groups: implement matching logic
Date: Fri,  7 Apr 2023 15:42:54 +0200	[thread overview]
Message-ID: <20230407134258.199691-9-l.nunner@proxmox.com> (raw)
In-Reply-To: <20230407134258.199691-1-l.nunner@proxmox.com>

Implements the logic behind match groups; A match group holds
several objects inside it, and only evalutes to true if ALL the
contained objects evaluate to true. Match groups are connected via
logical 'or' to all other objects inside the rule:

- Rule
    - Match Group
	- Object 1
	- Object 2
    - Object 3

This rule will match if either (Object 1 && Object 2), or if Object 3
evaluate to true.

Signed-off-by: Leo Nunner <l.nunner@proxmox.com>
---
RFC: This might be a bit weird for What objects: currently, it checks if
there are matches for each of the objects inside the match groups; if
one exists, *all* marks get pushed to the list. I'm not sure if this is
exactly what we want, but I can't really think of a better way here.

 src/PMG/RuleCache.pm | 50 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 7 deletions(-)

diff --git a/src/PMG/RuleCache.pm b/src/PMG/RuleCache.pm
index 9a531ef..eceffaf 100644
--- a/src/PMG/RuleCache.pm
+++ b/src/PMG/RuleCache.pm
@@ -253,11 +253,19 @@ sub from_match {
 	$ip = $1;
     }
 
+    # don't return true if there are no elements marked with and!
+    my $and = grep { $_->{and} } @$from;
     foreach my $obj (@$from) {
-	return 1 if ($obj->who_match($addr, $ip, $ldap) xor $obj->{negate});
+	my $match = ($obj->who_match($addr, $ip, $ldap) xor $obj->{negate});
+
+	if ($obj->{and}) {
+	    $and &&= $match;
+	} else {
+	    return 1 if $match;
+	}
     }
 
-    return 0;
+    return $and;
 }
 
 sub to_match {
@@ -267,11 +275,18 @@ sub to_match {
 
     return 1 if !defined ($to);
 
+    my $and = grep { $_->{and} } @$to;
     foreach my $obj (@$to) {
-	return 1 if ($obj->who_match($addr, undef, $ldap) xor $obj->{negate});
+	my $match = ($obj->who_match($addr, undef, $ldap) xor $obj->{negate});
+
+	if ($obj->{and}) {
+	    $and &&= $match;
+	} else {
+	    return 1 if $match;
+	}
     }
 
-    return 0;
+    return $and;
 }
 
 sub when_match {
@@ -281,11 +296,18 @@ sub when_match {
 
     return 1 if !defined ($when);
 
+    my $and = grep { $_->{and} } @$when;
     foreach my $obj (@$when) {
-	return 1 if ($obj->when_match($time) xor $obj->{negate});
+	my $match = ($obj->when_match($time) xor $obj->{negate});
+
+	if ($obj->{and}) {
+	    $and &&= $match;
+	} else {
+	    return 1 if $match;
+	}
     }
 
-    return 0;
+    return $and;
 }
 
 sub what_match {
@@ -311,14 +333,28 @@ sub what_match {
 
     my $marks;
 
+    # First, we loop through all objects which are not 'Spam Filter'
+    my $group_valid = 1;
+    my @group_matches = ();
     foreach my $obj (@$what) {
 	if (!$obj->can('what_match_targets')) {
 	    if (my $match = $obj->what_match($queue, $element, $msginfo, $dbh)) {
-		push @$marks, @$match;
+		if ($obj->{and}) {
+		    push @group_matches, $match;
+		} else {
+		    push @$marks, @$match;
+		}
+	    } elsif ($obj->{and}) {
+		$group_valid = 0;
 	    }
 	}
     }
 
+    # If the 'Match All' group matches, push everything into the list
+    if ($group_valid) {
+	push(@$marks, @group_matches);
+    }
+
     foreach my $target (@{$msginfo->{targets}}) {
 	$res->{$target}->{marks} = $marks;
 	$res->{marks} = $marks;
-- 
2.30.2





  parent reply	other threads:[~2023-04-07 13:43 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-07 13:42 [pmg-devel] [PATCH pmg-api/gui/docs, proxmox-widget-toolkit] Extend rule system Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 1/8] feature: negation: add field to database Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 2/8] feature: negation: parse negation value into objects Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 3/8] feature: negation: expand/implement API endpoints Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 4/8] feature: negation: implement matching logic Leo Nunner
2023-04-11  7:35   ` Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 5/8] feature: match groups: add field to database Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 6/8] feature: match groups: parse field into objects Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-api 7/8] feature: match groups: update API endpoints Leo Nunner
2023-04-07 13:42 ` Leo Nunner [this message]
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-gui 1/2] feature: negate objects inside rules Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-gui 2/2] feature: introduce logical 'and' for rules Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH pmg-docs] docs: document negation and match groups Leo Nunner
2023-04-07 13:42 ` [pmg-devel] [PATCH widget-toolkit] dark-mode: fix colour of default tree icons Leo Nunner
2023-04-11  9:52 ` [pmg-devel] [PATCH pmg-api/gui/docs, proxmox-widget-toolkit] Extend rule system Thomas Lamprecht
2023-04-11 11:04   ` Leo Nunner
2023-04-11 11:19     ` Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230407134258.199691-9-l.nunner@proxmox.com \
    --to=l.nunner@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal