#!/usr/bin/perl -w
#
# Version 20140822
#
# strack
# John Simpson <jms1@jms1.net> 2005-11-26
# Originally "mtrack" John Simpson <jms1@jms1.net> 1998-05-01
#
# reads qmail-smtpd's log and gathers the lines pertaining to each connection,
# allowing easier tracking of messages which enter the machine.
#
# 2007-03-22 jms1 - adding support for jgreylist[___] and qmail-smtpd[___]
#   log line formats. qmail-smtpd[___] was introduced in version 6cd of the
#   combined patch. http://qmail.jms1.net/patches/combined-details.shtml
#
# 2014-09-22 izto - Soporte para logs estilo:
#                   Aug 19 21:38:16 sl4 qmail: 1408502296.250625 new msg 3939005
#   Nota que son 6 campos antes de la palabra "new". Originalmente el script
#   asumia 5 campos.
#
#
###############################################################################
#
# Copyright (C) 1998-2007 John Simpson.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or visit http://www.gnu.org/licenses/gpl.txt
#
###############################################################################

require 5.003 ;
use strict ;

my $do_debug = 0 ;
my $show_all = 0 ;

my ( %mtext , %dmesg , %denied ) ;

###############################################################################
#
# debug routine

sub debug
{
	$do_debug && ( print @_ ) ;
}

###############################################################################
###############################################################################
###############################################################################
#
# main processing

while ( my $line = <> )
{
	my $oline = $line ;
	chomp $line ;

	my @w = split ( /\s+/ , $line ) ;

	########################################
	# ignore timestamp

	if ( $line =~ /^[A-Z]/ )		# syslog timestamp
	{
		shift @w ;
		shift @w ;
		shift @w ;
		shift @w ;
		shift @w ;
		shift @w ;
	}
	elsif ( $line =~ /^[0-9]/ )		# tai64nlocal timestamp
	{
		shift @w ;
		shift @w ;
	}
	elsif ( $line =~ /^\@/ )		# raw multilog output
	{
		shift @w ;
	}

	debug "[$line]\n" ;

	if ( $w[0] eq "tcpserver:" )
	{
		if ( ( $w[1] eq "pid" ) || ( $w[1] eq "ok" ) )
		{
			$mtext{$w[2]} .= $oline ;
		}
		elsif ( $w[1] eq "deny" )
		{
			$mtext{$w[2]} .= $oline ;
			$denied{$w[2]} = 1 ;
		}
		elsif ( $w[1] eq "end" )
		{
			if ( $show_all || ! $denied{$w[2]} )
			{
				print $mtext{$w[2]} , $oline , "\n" ;
			}
			delete $mtext{$w[2]} ;
		}
	}
	elsif ( $w[0] eq "rblsmtpd:" )
	{
		my $zm = $w[3] ;
		$zm =~ s/\:// ;
		$mtext{$zm} .= $oline ;
		$denied{$zm} = 1 ;
	}
	elsif ( $w[0] =~ m|jgreylist\[(\d+)\]\:| )
	{
		my $zm = $1 ;
		$mtext{$zm} .= $oline ;
		if ( $w[2] ne "OK" )
		{
			$denied{$zm} = 1 ;
		}
	}
	elsif ( $w[0] =~ m|qmail\-smtpd\[(\d+)\]\:| )
	{
		my $zm = $1 ;
		$mtext{$zm} .= $oline ;
	}
}

print "-" x 79 , "\n\n" ;

map { print $mtext{$_} , "\n" } sort keys %mtext ;
