/usr/share/perl5/Postfix/Parse/Mailq.pm is in libpostfix-parse-mailq-perl 1.004-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | use strict;
use warnings;
package Postfix::Parse::Mailq;
# ABSTRACT: parse the output of the postfix mailq command
$Postfix::Parse::Mailq::VERSION = '1.004';
use Mixin::Linewise::Readers -readers;
# =head1 SYNOPSIS
#
# use Postfix::Parse::Mailq;
#
# my $mailq_output = `mailq`;
# my $entries = Postfix::Parse::Mailq->read_string($mailq_output);
#
# my $bytes = 0;
# for my $entry (@$entries) {
# next unless grep { /\@aol.com$/ } @{ $entry->{remaining_rcpts} };
# $bytes += $entry->{size};
# }
#
# print "$bytes bytes remain to send to AOL destinations\n";
#
# =head1 WARNING
#
# This code is really rough and the interface will change. Entries will be
# objects. There will be some more methods. Still, the basics are likely to
# keep working, or keep pretty close to what you see here now.
#
# =method read_file
#
# =method read_handle
#
# =method read_string
#
# my $entries = Postfix::Parse::Mailq->read_string($string, \%arg);
#
# This methods read the output of postfix's F<mailq> from a file (by name), a
# filehandle, or a string, respectively. They return an arrayref of hashrefs,
# each hashref representing one entry in the queue as reported by F<mailq>.
#
# Valid arguments are:
#
# spool - a hashref of { queue_id -> spool_name } pairs
# if given, this will be used to attempt to indicate in which
# spool messages currently are; it is not entirely reliable (race!)
#
# =cut
sub read_handle {
my ($self, $handle, $arg) = @_;
$arg ||= {};
$arg->{spool} ||= {};
my $first = $handle->getline;
chomp $first;
return [] if $first eq 'Mail queue is empty';
Carp::confess("first line did not appear to be first line of mailq output")
unless $first =~ m{\A-+Queue ID-+};
my @current;
my @entries;
LINE: while (my $line = $handle->getline) {
if ($line eq "\n") {
my $entry = $self->parse_block(\@current);
$entry->{spool} = $arg->{spool}{ $entry->{queue_id} } if $arg->{spool};
push @entries, $entry;
@current = ();
next LINE;
}
push @current, $line;
}
if (@current and $current[0] !~ /^-- \d+ .?bytes/i) {
my $entry = $self->parse_block(\@current);
$entry->{spool} = $arg->{spool}{ $entry->{queue_id} } if $arg->{spool};
push @entries, $entry;
}
return \@entries;
}
# =method parse_block
#
# my $entry = Mailq->parse_block(\@lines);
#
# Given all the lines in a single entry's block of lines in mailq output, this
# returns data about the entry.
#
# =cut
my %STATUS_FOR = (
'!' => 'held',
'*' => 'active',
);
sub parse_block {
my ($self, $block) = @_;
chomp @$block;
my $first = shift @$block;
my $error = defined $block->[0] && $block->[0] =~ /\A\S/
? (shift @$block)
: undef;
my @dest = map { s/^\s+//; $_; } @$block;
my ($qid, $status_chr, $size, $date, $sender) = $first =~ m/
\A
([A-F0-9]+|[0-9B-Zb-z]+)
([*!])?
\s+
(\d+)
\s+
(.{19})
\s+
(\S.+)
\z
/x;
my $status = $status_chr ? ($STATUS_FOR{$status_chr} || 'unknown') : 'queued';
return {
queue_id => $qid,
status => $status,
size => $size,
date => $date,
sender => $sender,
error_string => $error,
remaining_rcpts => \@dest,
}
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Postfix::Parse::Mailq - parse the output of the postfix mailq command
=head1 VERSION
version 1.004
=head1 SYNOPSIS
use Postfix::Parse::Mailq;
my $mailq_output = `mailq`;
my $entries = Postfix::Parse::Mailq->read_string($mailq_output);
my $bytes = 0;
for my $entry (@$entries) {
next unless grep { /\@aol.com$/ } @{ $entry->{remaining_rcpts} };
$bytes += $entry->{size};
}
print "$bytes bytes remain to send to AOL destinations\n";
=head1 METHODS
=head2 read_file
=head2 read_handle
=head2 read_string
my $entries = Postfix::Parse::Mailq->read_string($string, \%arg);
This methods read the output of postfix's F<mailq> from a file (by name), a
filehandle, or a string, respectively. They return an arrayref of hashrefs,
each hashref representing one entry in the queue as reported by F<mailq>.
Valid arguments are:
spool - a hashref of { queue_id -> spool_name } pairs
if given, this will be used to attempt to indicate in which
spool messages currently are; it is not entirely reliable (race!)
=head2 parse_block
my $entry = Mailq->parse_block(\@lines);
Given all the lines in a single entry's block of lines in mailq output, this
returns data about the entry.
=head1 WARNING
This code is really rough and the interface will change. Entries will be
objects. There will be some more methods. Still, the basics are likely to
keep working, or keep pretty close to what you see here now.
=head1 AUTHOR
Ricardo SIGNES <rjbs@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Ricardo SIGNES.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|