-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpfdel.pl
144 lines (104 loc) · 3.87 KB
/
pfdel.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl
# Inspiried by a script I found at:
# http://www.ustrem.org/en/articles/postfix-queue-delete-en/
# and
# http://stage.seaglass.com/downloads/pfdel.pl
use strict;
use warnings;
use English qw( -no_match_vars );
use Carp;
use Getopt::Long;
use Pod::Usage;
# Initialize variables to be used later.
my @input_email = ();
my ( $man, $help, $verbose, $dryrun );
my $options_ok = GetOptions(
'email=s' => \@input_email,
'man' => \$man,
'help' => \$help,
'verbose' => \$verbose,
'dryrun' => \$dryrun,
) or pod2usage( -verbose => 1 );
pod2usage( -verbose => 2 ) if $man;
pod2usage( -verbose => 1 ) if $help;
# Change these paths if necessary.
my $postqueue = q{/usr/sbin/postqueue -p};
my $postsuper = q{/usr/sbin/postsuper};
open my $queue, qq{$postqueue |}
or croak qq{Can't get pipe to $postqueue: $OS_ERROR\n};
$INPUT_RECORD_SEPARATOR = q{}; # Paragraph mode
while ( my $entry = <$queue> ) {
$entry =~ s{\A-.*\n}{}; # remove header
# process each email address passed as input
for my $email_addr (@input_email) {
if ( $entry =~ / $email_addr$/m ) {
my ($qid) = split( /\s+/, $entry, 2 );
$qid =~ s{[\*\!]}{}; # remove * and ! from $qid
if ( $verbose or $dryrun ) {
print qq{$email_addr $qid\n};
print $entry;
print q{=} x 80 . qq{\n};
}
next unless ($qid);
# Execute postsuper -d with the queue id.
# postsuper provides feedback when it deletes
# messages. Let its output go through.
if ( not $dryrun ) {
if ( $EUID == 0 ) {
if ( system( $postsuper, "-d", $qid ) != 0 ) {
# If postsuper has a problem, bail.
croak qq{Error executing $postsuper: }
. ( $CHILD_ERROR / 256 ) . qq{\n};
}
}
else {
die qq{Must execute as root to delete queue files.\n};
}
}
}
}
}
close($queue);
__END__
# Documentation
=head1 NAME
pfdel.pl
=head1 DESCRIPTION
Perl script to delete messages from a postfix mail queue based on email address
in either the sender or recipient field.
=head1 ARGUMENTS
=over
=item --email I<EMAIL_ADDRESS>
This can be done multiple times, each additional address is added to an array.
=back
=head1 OPTIONS
=over
=item --verbose
Print the matching queue ID and email address followed by the raw queue output
for that entry.
=item --dryrun
Print the verbose output, but do not delete anything.
=item --help
Give a short help message.
=item --man
Give the documentation for this script.
=back
=head1 COPYRIGHT
The MIT License (MIT)
Copyright (c) 2013 Matt Okeson-Harlow
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
=cut
# vim: ai ts=4 sts=4 et sw=4 ft=perl