[pmg-devel] [PATCH pmg-api v3 06/10] RuleDB/Remove: improve attachment detection for 'remove all'

Dominik Csapak d.csapak at proxmox.com
Thu Oct 10 11:21:57 CEST 2019


this patch improves the attachment detection in such a way that
instead of leaving the first part if it has a content-type of
text/* and removing all other parts we now leave the 'real' message, meaning:

* the first text/plain or text/html part if no multipart
* the first text/plain or text/html part if multipart/mixed
* all text/plain and text/html parts of the first multipart/alternative
  (if we did not already see the message)

this way the 'real' message including the text/html alternative
(or the only text/html part if no text/plain was sent) is
left in the message that is being sent to the receiver

also add missing '$rulename' parameter to the recursive call to
'delete_marked_parts'

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes from v2:
* mention the parameter addition in commit message
 src/PMG/RuleDB/Remove.pm | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/PMG/RuleDB/Remove.pm b/src/PMG/RuleDB/Remove.pm
index e610532..ee6e37b 100644
--- a/src/PMG/RuleDB/Remove.pm
+++ b/src/PMG/RuleDB/Remove.pm
@@ -115,6 +115,7 @@ sub delete_marked_parts {
 
     my $nparts = [];
 
+    my $ctype = $entity->head->mime_type;
     my $pn = $entity->parts;
     for (my $i = 0; $i < $pn; $i++) {
 	my $part = $entity->parts($i);
@@ -126,8 +127,26 @@ sub delete_marked_parts {
 
 	    if ($self->{all}) {
 		my $ctype_part = $part->head->mime_type;
-		if (!($i == 0 && $ctype_part =~ m|text/.*|i)) {
+		if ($self->{message_seen}) {
 		    $found = 1;
+		} else {
+		    if ($ctype =~ m|multipart/alternative|i) {
+			if ($ctype_part !~ m{text/(?:plain|html)}i) {
+			    $found = 1 ;
+			}
+
+			if ($i == ($pn-1)) {
+			    # we have not seen the message and it is the
+			    # end of the first multipart/alternative, mark as message seen
+			    $self->{message_seen} = 1;
+			}
+		    } else {
+			if ($ctype_part =~ m{text/(?:plain|html)}i) {
+			    $self->{message_seen} = 1;
+			} elsif ($ctype_part !~ m|multipart/|i) {
+			    $found = 1 ;
+			}
+		    }
 		}
 	    } else {
 		foreach my $m (@$marks) {
@@ -159,7 +178,7 @@ sub delete_marked_parts {
 		    $queue->{logid}, $on, $rulename);
 
 	} else {
-	    $self->delete_marked_parts($queue, $part, $html, $rtype, $marks);
+	    $self->delete_marked_parts($queue, $part, $html, $rtype, $marks, $rulename);
 	    push (@$nparts, $part);
 	}
     }
@@ -202,7 +221,9 @@ sub execute {
 	    $entity->head->delete('x-proxmox-tmp-aid');
 	}
 
+	$self->{message_seen} = 0;
 	$self->delete_marked_parts($queue, $entity, $html, $rtype, $marks, $rulename);
+	delete $self->{message_seen};
 
 	if ($msginfo->{testmode}) {
 	    $entity->head->mime_attr('Content-type.boundary' => '------=_TEST123456') if $entity->is_multipart;
-- 
2.20.1




More information about the pmg-devel mailing list