From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.ivanov@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 B56A79391B
 for <pmg-devel@lists.proxmox.com>; Tue, 20 Feb 2024 14:09:46 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 9EDD373DE
 for <pmg-devel@lists.proxmox.com>; Tue, 20 Feb 2024 14:09:46 +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 <pmg-devel@lists.proxmox.com>; Tue, 20 Feb 2024 14:09:45 +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 A7B3843F84
 for <pmg-devel@lists.proxmox.com>; Tue, 20 Feb 2024 14:09:45 +0100 (CET)
Date: Tue, 20 Feb 2024 14:09:44 +0100
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: Dominik Csapak <d.csapak@proxmox.com>
Cc: pmg-devel@lists.proxmox.com
Message-ID: <20240220140944.5d927a46@rosa.proxmox.com>
In-Reply-To: <20240209125440.2572239-9-d.csapak@proxmox.com>
References: <20240209125440.2572239-1-d.csapak@proxmox.com>
 <20240209125440.2572239-9-d.csapak@proxmox.com>
X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-pc-linux-gnu)
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.085 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: Re: [pmg-devel] [PATCH pmg-api 08/12] RuleCache: implement
 and/invert for when/from/to
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: Tue, 20 Feb 2024 13:09:46 -0000

LGTM (from a quick glance, and after thinking a bit too long why
match_list_with_mode needs to get called recursively)

Reviewed-by: Stoiko Ivanov <s.ivanov@proxmox.com>

On Fri,  9 Feb 2024 13:54:32 +0100
Dominik Csapak <d.csapak@proxmox.com> wrote:

> by introducing 'match_list_with_mode' that gets called for each group
> and then on the result of each group match result.
> 
> This does not work for 'what' matches since they are not a simple
> yes/no match (they include the parts) so this will be done seperately.
> 
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
>  src/PMG/RuleCache.pm | 65 +++++++++++++++++++++++++++++---------------
>  1 file changed, 43 insertions(+), 22 deletions(-)
> 
> diff --git a/src/PMG/RuleCache.pm b/src/PMG/RuleCache.pm
> index 0b62aeb..7d08107 100644
> --- a/src/PMG/RuleCache.pm
> +++ b/src/PMG/RuleCache.pm
> @@ -273,13 +273,14 @@ sub from_match {
>  	$ip = $1;
>      }
>  
> -    for my $group ($from->{groups}->@*) {
> -	for my $obj ($group->{objects}->@*) {
> -	    return 1 if $obj->who_match($addr, $ip, $ldap);
> -	}
> -    }
> -
> -    return 0;
> +    return match_list_with_mode($from->{groups}, $from->{and}, $from->{invert}, sub {
> +	my ($group) = @_;
> +	my $list = $group->{objects};
> +	return match_list_with_mode($list, $group->{and}, $group->{invert}, sub {
> +	    my ($obj) = @_;
> +	    return $obj->who_match($addr, $ip, $ldap);
> +	});
> +    });
>  }
>  
>  sub to_match {
> @@ -289,14 +290,14 @@ sub to_match {
>  
>      return 1 if scalar($to->{groups}->@*) == 0;
>  
> -    for my $group ($to->{groups}->@*) {
> -	for my $obj ($group->{objects}->@*) {
> -	    return 1 if $obj->who_match($addr, undef, $ldap);
> -	}
> -    }
> -
> -
> -    return 0;
> +    return match_list_with_mode($to->{groups}, $to->{and}, $to->{invert}, sub {
> +	my ($group) = @_;
> +	my $list = $group->{objects};
> +	return match_list_with_mode($list, $group->{and}, $group->{invert}, sub {
> +	    my ($obj) = @_;
> +	    return $obj->who_match($addr, undef, $ldap);
> +	});
> +    });
>  }
>  
>  sub when_match {
> @@ -306,13 +307,14 @@ sub when_match {
>  
>      return 1 if scalar($when->{groups}->@*) == 0;
>  
> -    for my $group ($when->{groups}->@*) {
> -	for my $obj ($group->{objects}->@*) {
> -	    return 1 if $obj->when_match($time);
> -	}
> -    }
> -
> -    return 0;
> +    return match_list_with_mode($when->{groups}, $when->{and}, $when->{invert}, sub {
> +	my ($group) = @_;
> +	my $list = $group->{objects};
> +	return match_list_with_mode($list, $group->{and}, $group->{invert}, sub {
> +	    my ($obj) = @_;
> +	    return $obj->when_match($time);
> +	});
> +    });
>  }
>  
>  sub what_match {
> @@ -357,4 +359,23 @@ sub what_match {
>      return $res;
>  }
>  
> +# calls sub with each element of $list, and and/ors/inverts the result
> +sub match_list_with_mode($$$$) {
> +    my ($list, $and, $invert, $sub) = @_;
> +
> +    $and //= 0;
> +    $invert //= 0;
> +
> +    for my $el ($list->@*) {
> +	my $res = $sub->($el);
> +	if (!$and) {
> +	    return !$invert if $res;
> +	} else {
> +	    return $invert if !$res;
> +	}
> +    }
> +
> +    return $and != $invert;
> +}
> +
>  1;