From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 093391FF187 for ; Mon, 22 Sep 2025 10:58:38 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 47DAB15551; Mon, 22 Sep 2025 10:59:07 +0200 (CEST) Message-ID: <01437056-8b34-43af-a163-e059006b1889@proxmox.com> Date: Mon, 22 Sep 2025 10:58:25 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Beta To: Hannes Laimer , pmg-devel@lists.proxmox.com References: <20250919145049.20077-1-h.laimer@proxmox.com> <20250919145049.20077-2-h.laimer@proxmox.com> Content-Language: en-US From: Dominik Csapak In-Reply-To: <20250919145049.20077-2-h.laimer@proxmox.com> X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1758531501966 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.027 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: Re: [pmg-devel] [PATCH pmg-api 1/1] fix #3450: api: add 'filter' parameter to queue DELETE endpoint X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: pmg-devel-bounces@lists.proxmox.com Sender: "pmg-devel" one high level comment and a few comments inline i get what you want to achieve here and this is what stoiko wrote in the bugreport, but from an UX perspective this could be bad. Since the mails shown on the gui might not match what will be deleted (e.g. new mails came in, in the meantime) would it maybe make more sense to have an approach with giving a list of ids to the backend? (@stoiko ?) On 9/19/25 4:51 PM, Hannes Laimer wrote: > This allows to delete all mails in the queue which have a > sender/recipient that matches the passed filter. > > Signed-off-by: Hannes Laimer > --- > src/PMG/API2/Postfix.pm | 14 ++++++++++++-- > src/PMG/Postfix.pm | 40 ++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 52 insertions(+), 2 deletions(-) > > diff --git a/src/PMG/API2/Postfix.pm b/src/PMG/API2/Postfix.pm > index ba0689c..31f7449 100644 > --- a/src/PMG/API2/Postfix.pm > +++ b/src/PMG/API2/Postfix.pm > @@ -339,7 +339,7 @@ __PACKAGE__->register_method({ > name => 'delete_queue', > path => 'queue/{queue}', > method => 'DELETE', > - description => "Delete all mails in the queue.", > + description => "Delete all mails in the queue, or only those matching an optional filter.", > proxyto => 'node', > permissions => { check => ['admin'] }, > protected => 1, > @@ -348,13 +348,23 @@ __PACKAGE__->register_method({ > properties => { > node => get_standard_option('pve-node'), > queue => $queue_name_option, > + filter => { > + description => "Filter string (matches sender and recipients).", > + type => 'string', > + maxLength => 64, > + optional => 1, > + }, > }, > }, > returns => { type => 'null' }, > code => sub { > my ($param) = @_; > > - PMG::Postfix::delete_queue($param->{queue}); > + if (defined($param->{filter}) && $param->{filter} ne '') { > + PMG::Postfix::delete_queue_filtered($param->{queue}, $param->{filter}); > + } else { > + PMG::Postfix::delete_queue($param->{queue}); > + } > > return undef; > }, > diff --git a/src/PMG/Postfix.pm b/src/PMG/Postfix.pm > index 966130f..7c7869e 100644 > --- a/src/PMG/Postfix.pm > +++ b/src/PMG/Postfix.pm > @@ -221,6 +221,46 @@ sub delete_queue { > PVE::Tools::run_command($cmd); > } > > +# delete mails in a queue matching a sender/recipient filter > +sub delete_queue_filtered { > + my ($queue, $filter) = @_; > + > + die "no filter specified\n" if !defined($filter) || $filter eq ''; > + > + open(my $fh, '-|', '/usr/sbin/postqueue', '-j') || die "ERROR: unable to run postqueue - $!\n"; > + > + my %seen; > + my @queue_ids; > + my $line; > + while (defined($line = <$fh>)) { > + my $rec = decode_json($line); > + next if $rec->{queue_name} ne $queue; > + > + my $match = 0; > + my $sender = $rec->{sender} // ''; > + if ($sender =~ m/$filter/i) { if we accept any characters in the api, we must escape the regex string here, otherwise e.g. the filter '.' matches every thing with at least one character, not only those things with one dot. (same would actually go for the listing itself, alternatively we could document it as a 'regex' string? ) > + $match = 1; > + } else { > + my $recipients = $rec->{recipients} // []; > + foreach my $entry (@$recipients) { > + if (($entry->{address} // '') =~ m/$filter/i) { $match = 1; last; } same here > + } > + } > + > + next if !$match; > + > + my $qid = $rec->{queue_id}; > + next if !$qid || $seen{$qid}++; > + push @queue_ids, $qid; > + } > + > + return if !@queue_ids; > + > + my $input = join("\n", @queue_ids) . "\n"; > + my $cmd = ['/usr/sbin/postsuper', '-d', '-', $queue]; > + PVE::Tools::run_command($cmd, input => $input); > +} > + > sub discard_verify_cache { > unlink "/var/lib/postfix/verify_cache.db"; > _______________________________________________ pmg-devel mailing list pmg-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel