#! /usr/bin/perl -w

# print postfix mailq, one message per line.
#
# $Id: cmailq.pl,v 1.5 2004/11/23 23:09:53 root Exp $

# Copyright (C) Craig Sanders <cas@taz.net.au> 2004
# 
# this program is licensed under the terms of the GNU General Public 
# License (GPL)
#
# the latest version can always be found at http://taz.net.au/postfix/scripts

use diagnostics;
use strict;

my $line = "";

foreach (`/usr/bin/mailq`) {

  # skip header, "reason", total and "is empty" lines.
  next if /-Queue ID-|^\s*\(|^--|^Mail queue is empty$/;
  chomp;

  if (/^\w|^\s*$/) {  # line is empty or begins with non-space

    # process current line;
    if ($line ne '') {
      # insert space before !,* status chars, a convenience feature to
      # make it easy to double-click the QUEUE-ID with a mouse or pipe
      # into "awk '{print $1}'" without having to get rid of the * or !
      $line =~ s/([!*])/ $1/;

      print "$line\n";
    } ;

    # begin new line
    $line = $_;  
    #print "^^^ '$line'\n";

  } else {
    # strip off leading and trailing spaces and append to current line
    s/^\s*|\s*$//;
    $line .= " $_";
    #print "+++ '$line'\n";
  };
} ;

