#!/usr/bin/env python ################################################## # formats the output of Postfix' mailq command # # http://www.postconf.com/docs/mq # # $Id: mq,v 1.64 2012/04/10 02:09:33 marquis Exp $ ################################################## #### defaults #### termwidth = 80 # default linelength w/o terminfo longlines = False # default no linewrapping html = True # default text output format debug = False ############### import commands, string, sys, os, re def printusage(): print(" USAGE: " + sys.argv[0] + " [ -h | -html | -txt | -l= | -l ]") sys.exit() #### parse command line if (len(sys.argv) > 1): if (sys.argv[1] == "-html"): html = True elif (sys.argv[1] == "-txt"): html = False elif (sys.argv[1].startswith('-l=')): try: termwidth = int(sys.argv[1][3:]) longlines = True except ValueError: print " ERROR: \"" + sys.argv[1][3:] + "\" is not an integer" printusage() elif (sys.argv[1] == "-l"): longlines = True else: printusage() #### determine terminal width to avoid linewrap if not html and not longlines: if os.path.exists('/usr/bin/stty'): sttyA = os.popen('/usr/bin/stty -a 2>/dev/null', 'r') elif os.path.exists('/bin/stty'): sttyA = os.popen('/bin/stty -a 2>/dev/null', 'r') else: sttyA = os.popen('stty -a 2>/dev/null', 'r') sttySize = sttyA.readline() if sttySize: for x in string.split(sttySize, ';'): sttyWidth = string.split(x) if len(sttyWidth) == 2 and sttyWidth[1] == 'columns': termwidth = int( sttyWidth[0] ) sttyA.close() termwidth -= 1 # fix x11 linewrap bug in some termtypes if debug: print(' debug = True, html = ' + str(html) + ', longlines = ' + str(longlines) + ', termwidth = ' + str(termwidth)) #### check for multiple instances, /etc/postfix, /etc/postfix1, ... mailconfigs = [] for config in os.listdir('/etc'): # in case of multi-queue server, FreeBSD requires "INST_BASE=YES" if config.startswith('postfix') and os.path.exists('/etc/' + config + '/main.cf'): mailconfigs.append( config ) if debug: print(' debug = True, mailconfigs = ' + str(mailconfigs)) #### parse mailq output into an array with one row per message queue = [] for config in mailconfigs: os.environ["MAIL_CONFIG"]=('/etc/' + config) qline = ["", "", "", ""] for line in commands.getoutput('mailq').splitlines(): if re.match( '^[-|postqueue:|Mail]', line ): # discard in-queue wrapper continue elif re.match( '^[A-Z0-9]', line ): #### queueid/.../sender qline[0] = line[:line.rindex( " " )].strip() qline[1] = line[line.rindex( " " ):].strip() elif line.count( ')' ) > 0: #### status qline[2] = line.strip() elif re.match( '^\s', line ): #### recipient/s qline[3] = ( qline[3] + " " + line.lstrip() ).strip() elif not line: #### end of record if html: if '!' in qline[0]: # held openTag = '' elif '*' in qline[0]: # in delivery openTag = '' else: openTag = '' closeTag = '' queue.append( openTag + qline[0] + closeTag + openTag + qline[1] + closeTag + openTag + qline[3] + closeTag + openTag + qline[2] + closeTag + "\n" ) else: queue.append( qline[0] + " " + qline[1] + " " + qline[3] + " " + qline[2] + "\n" ) qline = ["", "", "", ""] else: #### should never get here print( " ERROR: unknown input: \"" + line + "\"") #### output already #### lineNum = 0 if not html: if ( queue ): header='# --Queue ID-- -Size- ---Arrival Time---- ---Sender/Recipient(s)/(Status)---' print( header[:termwidth] ) for line in ' '.join( queue ).splitlines(): lineNum += 1 if not longlines: print( str( lineNum ) + " " + line.strip() )[:termwidth] else: print( str( lineNum ) + " " + line.strip() ) lineNum += 1 else: header=('Postfix mailq
\n' +
      '' +
      '' +
      '' +
      '' +
      '')
  print('Content-type: text/html\n\n' + header)
  if ( queue ):
    for line in ''.join( queue ).splitlines():
      lineNum += 1
      print( '' + line.strip() + "" )
  print( '
#  --Queue ID-- -Size- ---Arrival Time----  ---Sender---  ---Recipient(s)---  --(Status)--
' + str(lineNum) + ' 
' )