From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <l.nunner@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 5CFEFC0C6
 for <pmg-devel@lists.proxmox.com>; Thu, 14 Sep 2023 11:53:24 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 4B4BE36B9F
 for <pmg-devel@lists.proxmox.com>; Thu, 14 Sep 2023 11:52:53 +0200 (CEST)
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 <pmg-devel@lists.proxmox.com>; Thu, 14 Sep 2023 11:52:51 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 04278472BE
 for <pmg-devel@lists.proxmox.com>; Thu, 14 Sep 2023 11:52:51 +0200 (CEST)
From: Leo Nunner <l.nunner@proxmox.com>
To: pmg-devel@lists.proxmox.com
Date: Thu, 14 Sep 2023 11:52:25 +0200
Message-Id: <20230914095234.115469-5-l.nunner@proxmox.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20230914095234.115469-1-l.nunner@proxmox.com>
References: <20230914095234.115469-1-l.nunner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.095 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
Subject: [pmg-devel] [PATCH WIP api 04/11] negation: implement matching logic
X-BeenThere: pmg-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Mail Gateway development discussion
 <pmg-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pmg-devel/>
List-Post: <mailto:pmg-devel@lists.proxmox.com>
List-Help: <mailto:pmg-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel>, 
 <mailto:pmg-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 14 Sep 2023 09:53:24 -0000

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 100644
--- 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 100644
--- 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 177a283..0748d77 100644
--- a/src/PMG/RuleDB/MatchField.pm
+++ b/src/PMG/RuleDB/MatchField.pm
@@ -118,7 +118,7 @@ sub parse_entity {
 	    $decvalue = PMG::Utils::try_decode_utf8($decvalue);
 
 	    eval {
-		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.39.2