all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api 1/1] fix #3450: api: add 'filter' parameter to queue DELETE endpoint
Date: Fri, 19 Sep 2025 16:50:48 +0200	[thread overview]
Message-ID: <20250919145049.20077-2-h.laimer@proxmox.com> (raw)
In-Reply-To: <20250919145049.20077-1-h.laimer@proxmox.com>

This allows to delete all mails in the queue which have a
sender/recipient that matches the passed filter.

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
 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) {
+            $match = 1;
+        } else {
+            my $recipients = $rec->{recipients} // [];
+            foreach my $entry (@$recipients) {
+                if (($entry->{address} // '') =~ m/$filter/i) { $match = 1; last; }
+            }
+        }
+
+        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";
 
-- 
2.47.3



_______________________________________________
pmg-devel mailing list
pmg-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pmg-devel


  reply	other threads:[~2025-09-19 14:51 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-19 14:50 [pmg-devel] [PATCH pmg-api/pmg-gui 0/2] add support to delete multiple queue entries at once Hannes Laimer
2025-09-19 14:50 ` Hannes Laimer [this message]
2025-09-22  8:58   ` [pmg-devel] [PATCH pmg-api 1/1] fix #3450: api: add 'filter' parameter to queue DELETE endpoint Dominik Csapak
2025-09-22  9:18     ` Thomas Lamprecht
2025-09-22 10:09       ` Stoiko Ivanov
2025-09-22 10:15         ` Hannes Laimer
2025-09-22  9:19   ` Thomas Lamprecht
2025-09-19 14:50 ` [pmg-devel] [PATCH pmg-gui 1/1] fix #3450: ui: add button to delete all queue entries matching a filter Hannes Laimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250919145049.20077-2-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal