#!/usr/bin/perl

#################################
#  EDIT THIS CONFIGURATION FIRST
#
#  $current_log - this variable is files of your qmail-send log. 
#
#   $old_log - if you have to managed qmail-send log to a file in format /path/to/filename.month.day.year.log
#    such as "/qmailsendlog/qmail-send.apr.5.2004.log" , this script can be tracking old log automatically.
#		However , it's just an optional feature, leave it if you dont really need.

$current_log='/var/log/qmail/*';
$old_log='/qmailstat/qmail-send';
#################################

## find today 

$today = time();

($sec,$min,$hour,$mday,$mon,$myear,$wday,$yday,$isdst) = localtime($today);
$today_year = $myear + 1900;
$today_day = $mday;

$today_month = (qw(jan feb mar apr may jun jul aug sep oct nov dec))[$mon];
$today_mon = $mon + 1;

if (@ARGV!=6&&@ARGV!=3) {
	print "usage : qmail-track.pl (<sender>|all) <receiver> [<day> <month> <year>] (<hr>|all)\n";
	print "\n";
	exit(0);
}

print "Please wait....\n\n";

# READ parameter

if (@ARGV==6) {
$sender = $ARGV[0];
$receiver = $ARGV[1];
$day   = $ARGV[2];
$month   = $ARGV[3];
$year  = $ARGV[4];
$hour = $ARGV[5];
}

if (@ARGV==3) {
$sender = $ARGV[0];
$receiver = $ARGV[1];
$hour = $ARGV[2];

$day   = $today_day;
$month   = $today_month;
$year  = $today_year;
}

# Prepare Time to search

if ($hour=='00') {$widetime=' 00:| 01:';}
if ($hour=='01') {$widetime=' 00:| 01:| 02:';}
if ($hour=='02') {$widetime=' 01:| 02:| 03:';}
if ($hour=='03') {$widetime=' 02:| 03:| 04:';}
if ($hour=='04') {$widetime=' 03:| 04:| 05:';}

if ($hour=='05') {$widetime=' 04:| 05:| 06:';}
if ($hour=='06') {$widetime=' 05:| 06:| 07:';}
if ($hour=='07') {$widetime=' 06:| 07:| 08:';}
if ($hour=='08') {$widetime=' 07:| 08:| 09:';}
if ($hour=='09') {$widetime=' 08:| 09:| 10:';}

if ($hour=='10') {$widetime=' 09:| 10:| 11:';}
if ($hour=='11') {$widetime=' 10:| 11:| 12:';}
if ($hour=='12') {$widetime=' 11:| 12:| 13:';}
if ($hour=='13') {$widetime=' 12:| 13:| 14:';}
if ($hour=='14') {$widetime=' 13:| 14:| 15:';}

if ($hour=='15') {$widetime=' 14:| 15:| 16:';}
if ($hour=='16') {$widetime=' 15:| 16:| 17:';}
if ($hour=='17') {$widetime=' 16:| 17:| 18:';}
if ($hour=='18') {$widetime=' 17:| 18:| 19:';}
if ($hour=='19') {$widetime=' 18:| 19:| 20:';}

if ($hour=='20') {$widetime=' 19:| 20:| 21:';}
if ($hour=='21') {$widetime=' 20:| 21:| 22:';}
if ($hour=='22') {$widetime=' 21:| 22:| 23:';}
if ($hour=='23') {$widetime=' 22:| 23:';}

if ($hour=='all') {$widetime=' 00:| 01:| 02:| 03:| 04:| 05:| 06:| 07:| 08:| 09:| 10:| 11:| 12:| 13:| 14:| 15:| 16:| 17:| 18:| 19:| 20:| 21:| 22:| 23:|';}


## calculate file locatetion to search
if ($year==$today_year&&$day==$today_day&&$month==$today_month) {
	$file=$current_log;
} else
{
	$file="$old_log.$month.$day.$year.log";
}

$command="cat $file | tai64nlocal | grep -E '$widetime' | grep -E 'remote $receiver|local $receiver'";
#print "COMMAND: $command\n";
$res=`$command`;
#print "RESULT ----------------------\n";

@result = split(/\n/, $res);
$nresult=@result;
print "Found $nresult message to $receiver (ALL sender)\n";
for ($i=0;$i<$nresult ;$i++) {
	@column=split(/ /, $result[$i]);
	$delivery_id=$column[4];
	chop($delivery_id);
	$msg_id=$column[6];


	######## FINDING MSG ID AND CHECK SENDER #############
	$command="cat $file | tai64nlocal | grep -E '$widetime' | grep 'msg $msg_id'";
	$res_msg=`$command`;
	@result_msg = split(/\n/, $res_msg);
	$nresult_msg=@result_msg;
	$index=-1;
	for ($j=0;$j<$nresult_msg ;$j++) {
		if ($result_msg[$j] eq $result[$i]) {
			$index=$j;
		}
	}

	## FIND GROUP OF THIS MSG ##
	$begin=$index;
	$info=$index;
	$end=$index;
	@msg_col=split(/ /,$result_msg[$index]);

	## FIND begin of msg
	while ($msg_col[2] ne 'new') {
		$begin--;
		@msg_col=split(/ /,$result_msg[$begin]);
	}

	## FIND info of msg ( SENDER )
	while ($msg_col[2] ne 'info') {
		$info--;
		@msg_col=split(/ /,$result_msg[$info]);
	}

	## FIND end of msg
	while ($msg_col[2] ne 'end') {
		$end++;
		@msg_col=split(/ /,$result_msg[$end]);
	}

	### Check sender
	@msg_col=split(/ /,$result_msg[$info]);
	$match_sender="<$sender>";
	if ($match_sender eq $msg_col[8] || $sender=='all') {
		$true_sender	= 1;
		print "Check sender of Message No. $i ...$msg_col[8]... Match\n";
	} else {
		$true_sender	= 0;
		print "Check sender of Message No. $i ...$msg_col[8]... Not Match\n";
	}
	
	if ($true_sender	== 1) {
		$command="cat $file | tai64nlocal | grep -E '$widetime' | grep 'delivery $delivery_id'";
		$res_delivery=`$command`;
		@result_delivery = split(/\n/, $res_delivery);
		$nresult_delivery=@result_delivery;
		$index_delivery=-1;
		for ($m=0;$m<$nresult_delivery ;$m++) {
			if ($result_delivery[$m] eq $result[$i]) {
				$index_delivery=$m;
			}
		}

		### RESULT OF DEVIVERY --- SUCCESS , DEFERRAL or FAILURE is HERE!! 
		$final_result=$result_delivery[$index_delivery+1];

	## Display Group of msg
		print "-------------[Detail of Delivery no.: $i]-------------\n";
		for ($k=$begin;$k<=$end;$k++) {
			print $result_msg[$k]."\n";
			if ($k==$index) {
				print $final_result."\n";
			}
		}
		print "------------------------------------------------------\n";
	}
}






