public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pmg-devel] [PATCH pmg-api] rulesystem: matchfield: match all headers not only the first
@ 2022-05-17  9:39 Stoiko Ivanov
  2022-05-17 10:05 ` Dominik Csapak
  0 siblings, 1 reply; 2+ messages in thread
From: Stoiko Ivanov @ 2022-05-17  9:39 UTC (permalink / raw)
  To: pmg-devel

currently the match field uses $entity->head->get in scalar context,
which only returns the first matching header (see [0])

switch over to using get_all in list context and iterating over all
headers makes it possible to match subsequent headers.

while it is uncommon in general - the Received headers are usually not
restricted to one - reported in our community forum:
https://forum.proxmox.com/threads/.109629/

[0] https://metacpan.org/pod/MIME::Head#Getting-field-contents
Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
best viewed with `git show -w`
tested with the sample from the forum for the multiple received headers
case and a simple subject match for the single header case.

 src/PMG/RuleDB/MatchField.pm | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/PMG/RuleDB/MatchField.pm b/src/PMG/RuleDB/MatchField.pm
index b1da4fa..171cb3d 100644
--- a/src/PMG/RuleDB/MatchField.pm
+++ b/src/PMG/RuleDB/MatchField.pm
@@ -102,13 +102,15 @@ sub parse_entity {
     if (my $id = $entity->head->mime_attr ('x-proxmox-tmp-aid')) {
 	chomp $id;
 
-	if (defined(my $value = $entity->head->get($self->{field}))) {
-	    chomp $value;
+	if (my @values = $entity->head->get_all($self->{field})) {
+	    for my $value (@values) {
+		chomp $value;
 
-	    my $decvalue = MIME::Words::decode_mimewords($value);
+		my $decvalue = MIME::Words::decode_mimewords($value);
 
-	    if ($decvalue =~ m|$self->{field_value}|i) {
-		push @$res, $id;
+		if ($decvalue =~ m|$self->{field_value}|i) {
+		    push @$res, $id;
+		}
 	    }
 	}
     }
-- 
2.30.2





^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [pmg-devel] [PATCH pmg-api] rulesystem: matchfield: match all headers not only the first
  2022-05-17  9:39 [pmg-devel] [PATCH pmg-api] rulesystem: matchfield: match all headers not only the first Stoiko Ivanov
@ 2022-05-17 10:05 ` Dominik Csapak
  0 siblings, 0 replies; 2+ messages in thread
From: Dominik Csapak @ 2022-05-17 10:05 UTC (permalink / raw)
  To: Stoiko Ivanov, pmg-devel

On 5/17/22 11:39, Stoiko Ivanov wrote:
> currently the match field uses $entity->head->get in scalar context,
> which only returns the first matching header (see [0])
> 
> switch over to using get_all in list context and iterating over all
> headers makes it possible to match subsequent headers.
> 
> while it is uncommon in general - the Received headers are usually not
> restricted to one - reported in our community forum:
> https://forum.proxmox.com/threads/.109629/
> 
> [0] https://metacpan.org/pod/MIME::Head#Getting-field-contents
> Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
> ---
> best viewed with `git show -w`
> tested with the sample from the forum for the multiple received headers
> case and a simple subject match for the single header case.
> 
>   src/PMG/RuleDB/MatchField.pm | 12 +++++++-----
>   1 file changed, 7 insertions(+), 5 deletions(-)
> 
> diff --git a/src/PMG/RuleDB/MatchField.pm b/src/PMG/RuleDB/MatchField.pm
> index b1da4fa..171cb3d 100644
> --- a/src/PMG/RuleDB/MatchField.pm
> +++ b/src/PMG/RuleDB/MatchField.pm
> @@ -102,13 +102,15 @@ sub parse_entity {
>       if (my $id = $entity->head->mime_attr ('x-proxmox-tmp-aid')) {
>   	chomp $id;
>   
> -	if (defined(my $value = $entity->head->get($self->{field}))) {
> -	    chomp $value;
> +	if (my @values = $entity->head->get_all($self->{field})) {
> +	    for my $value (@values) {
> +		chomp $value;
>   
> -	    my $decvalue = MIME::Words::decode_mimewords($value);
> +		my $decvalue = MIME::Words::decode_mimewords($value);
>   
> -	    if ($decvalue =~ m|$self->{field_value}|i) {
> -		push @$res, $id;
> +		if ($decvalue =~ m|$self->{field_value}|i) {
> +		    push @$res, $id;
> +		}
>   	    }
>   	}
>       }

the docs say that get_all returns always a list, so we could do:

for my $value ($entity->head->get_all($self->{field})) {
...
}

directly, no?
would save us a level of indentation




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-17 10:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-17  9:39 [pmg-devel] [PATCH pmg-api] rulesystem: matchfield: match all headers not only the first Stoiko Ivanov
2022-05-17 10:05 ` Dominik Csapak

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