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 4/8] feature: negation: implement matching logic
Date: Fri,  7 Apr 2023 15:42:50 +0200	[thread overview]
Message-ID: <20230407134258.199691-5-l.nunner@proxmox.com> (raw)
In-Reply-To: <20230407134258.199691-1-l.nunner@proxmox.com>

Straightforward for most objects, where the matching result gets XOR'd
with the specific negation setting. For 'what' objects, more changes
were necessary: the different types of what matches all needed to be
expanded inside their respective parse_entity functions. For some of
these, the result might be _strange_ (which is more due to the concept
of inverting what objects itself), but seems to work as intended.

Signed-off-by: Leo Nunner <l.nunner@proxmox.com>
---
 src/PMG/RuleCache.pm                   | 8 ++++----
 src/PMG/RuleDB/ArchiveFilter.pm        | 6 ++++--
 src/PMG/RuleDB/ContentTypeFilter.pm    | 6 ++++--
 src/PMG/RuleDB/MatchArchiveFilename.pm | 4 ++--
 src/PMG/RuleDB/MatchField.pm           | 2 +-
 src/PMG/RuleDB/MatchFilename.pm        | 2 +-
 6 files changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/PMG/RuleCache.pm b/src/PMG/RuleCache.pm
index c5a57f6..04e35da 100644
--- a/src/PMG/RuleCache.pm
+++ b/src/PMG/RuleCache.pm
@@ -252,7 +252,7 @@ sub from_match {
     }
 
     foreach my $obj (@$from) {
-	return 1 if $obj->who_match($addr, $ip, $ldap);
+	return 1 if ($obj->who_match($addr, $ip, $ldap) xor $obj->{negate});
     }
 
     return 0;
@@ -266,7 +266,7 @@ sub to_match {
     return 1 if !defined ($to);
 
     foreach my $obj (@$to) {
-	return 1 if $obj->who_match($addr, undef, $ldap);
+	return 1 if ($obj->who_match($addr, undef, $ldap) xor $obj->{negate});
     }
 
     return 0;
@@ -280,7 +280,7 @@ sub when_match {
     return 1 if !defined ($when);
 
     foreach my $obj (@$when) {
-	return 1 if $obj->when_match($time);
+	return 1 if ($obj->when_match($time) xor $obj->{negate});
     }
 
     return 0;
@@ -325,7 +325,7 @@ sub what_match {
     foreach my $obj (@$what) {
 	if ($obj->can ("what_match_targets")) {
 	    my $target_info;
-	    if ($target_info = $obj->what_match_targets($queue, $element, $msginfo, $dbh)) {
+	    if (($target_info = $obj->what_match_targets($queue, $element, $msginfo, $dbh)) xor $obj->{negate}) {
 		foreach my $k (keys %$target_info) {
 		    my $cmarks = $target_info->{$k}->{marks}; # make a copy
 		    $res->{$k} = $target_info->{$k};
diff --git a/src/PMG/RuleDB/ArchiveFilter.pm b/src/PMG/RuleDB/ArchiveFilter.pm
index 6d91556..61f6c50 100755
--- a/src/PMG/RuleDB/ArchiveFilter.pm
+++ b/src/PMG/RuleDB/ArchiveFilter.pm
@@ -60,10 +60,12 @@ sub parse_entity {
 	my $glob_ct = $entity->{PMX_glob_ct};
 
 	if ($header_ct && $header_ct =~ m|$self->{field_value}|) {
-	    push @$res, $id;
+	    push @$res, $id if !$self->{negate};
 	} elsif ($magic_ct && $magic_ct =~ m|$self->{field_value}|) {
-	    push @$res, $id;
+	    push @$res, $id if !$self->{negate};
 	} elsif ($glob_ct && $glob_ct =~ m|$self->{field_value}|) {
+	    push @$res, $id if !$self->{negate};
+	} elsif ($self->{negate}) {
 	    push @$res, $id;
 	} else {
 	    # match inside archives 
diff --git a/src/PMG/RuleDB/ContentTypeFilter.pm b/src/PMG/RuleDB/ContentTypeFilter.pm
index 76fc1ce..eb35292 100755
--- a/src/PMG/RuleDB/ContentTypeFilter.pm
+++ b/src/PMG/RuleDB/ContentTypeFilter.pm
@@ -72,10 +72,12 @@ sub parse_entity {
 	my $glob_ct = $entity->{PMX_glob_ct};
 
 	if ($header_ct && $header_ct =~ m|$self->{field_value}|) {
-	    push @$res, $id;
+	    push @$res, $id if !$self->{negate};
 	} elsif ($magic_ct && $magic_ct =~ m|$self->{field_value}|) {
-	    push @$res, $id;
+	    push @$res, $id if !$self->{negate};
 	} elsif ($glob_ct && $glob_ct =~ m|$self->{field_value}|) {
+	    push @$res, $id if !$self->{negate};
+	} elsif ($self->{negate}) {
 	    push @$res, $id;
 	}
     }
diff --git a/src/PMG/RuleDB/MatchArchiveFilename.pm b/src/PMG/RuleDB/MatchArchiveFilename.pm
index 2ef3543..8abd592 100644
--- a/src/PMG/RuleDB/MatchArchiveFilename.pm
+++ b/src/PMG/RuleDB/MatchArchiveFilename.pm
@@ -29,12 +29,12 @@ sub parse_entity {
 	chomp $id;
 
 	my $fn = PMG::Utils::extract_filename($entity->head);
-	if (defined($fn) && $fn =~ m|^$self->{fname}$|i) {
+	if (defined($fn) && ($fn =~ m|^$self->{fname}$|i xor $self->{negate})) {
 	    push @$res, $id;
 	} elsif (my $filenames = $entity->{PMX_filenames}) {
 	    # Match inside archives
 	    for my $fn (keys %$filenames) {
-		if ($fn =~ m|^$self->{fname}$|i) {
+		if ($fn =~ m|^$self->{fname}$|i xor $self->{negate}) {
 		    push @$res, $id;
 		    last;
 		}
diff --git a/src/PMG/RuleDB/MatchField.pm b/src/PMG/RuleDB/MatchField.pm
index 2b56058..8f89297 100644
--- a/src/PMG/RuleDB/MatchField.pm
+++ b/src/PMG/RuleDB/MatchField.pm
@@ -111,7 +111,7 @@ sub parse_entity {
 	    my $decvalue = PMG::Utils::decode_rfc1522($value);
 	    $decvalue = PMG::Utils::try_decode_utf8($decvalue);
 
-	    if ($decvalue =~ m|$self->{field_value}|i) {
+	    if (($decvalue =~ m|$self->{field_value}|i) xor $self->{negate}) {
 		push @$res, $id;
 	    }
 	}
diff --git a/src/PMG/RuleDB/MatchFilename.pm b/src/PMG/RuleDB/MatchFilename.pm
index c9cdbe0..0665efc 100644
--- a/src/PMG/RuleDB/MatchFilename.pm
+++ b/src/PMG/RuleDB/MatchFilename.pm
@@ -91,7 +91,7 @@ sub parse_entity {
 	chomp $id;
 
 	if (my $value = PMG::Utils::extract_filename($entity->head)) {
-	    if ($value =~ m|^$self->{fname}$|i) {
+	    if (($value =~ m|^$self->{fname}$|i) xor $self->{negate}) {
 		push @$res, $id;
 	    }
 	}
-- 
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 ` Leo Nunner [this message]
2023-04-11  7:35   ` [pmg-devel] [PATCH pmg-api 4/8] feature: negation: implement matching logic 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 ` [pmg-devel] [PATCH pmg-api 8/8] feature: match groups: implement matching logic Leo Nunner
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-5-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