#!/usr/bin/perl
#
# $Id: postfix,v 1.1 2002/02/07 18:31:12 syatskevich Exp $
#
#   postfix     mail_log
#
#       :
#     1)  (   )  
#     2) 
#     3) 
#     4)   
#
#     
#
#  " "/" "  
# 1, 2, 3
#

use DB_File;
use DBI;

$user   = $ENV{"USER"};
$dbase  = $ENV{"DBASE"};
$passwd = $ENV{"PASSWD"};

#  
if (($user eq "") || ($dbase eq "") || ($passwd eq ""))
{
	die "  \n";
}

$dbdir  = "/var/log/loganalyzer/db";

($current_month, $current_year) = (localtime) [4, 5];

$current_month++;
$current_year += 1900;
$last_year     = $current_year - 1;

sub parseLogLine
{
	if (($month, $day, $message_id, $cmd) =
	    (/^(\w+)\s+(\d+)\s+\d+:\d+:\d+\s+\w+\s+postfix\/\w+\[\d+\]:\s+([\da-fA-F]+):\s+(.*)/i))
	{
		#    syslog (    postfix)  
		# ALTLinux (  ,   )       ,
		#            
		# ,          
		# .
		#
		#            
		#   ,       
		$date = ($month > $current_month) ? "$last_year/$month/$day" : "$current_year/$month/$day";

		if (($sender, $size, $nrcpt) = ($cmd =~ /^from=<(.+)>, size=(\d+), nrcpt=(\d+)/i))
		{
			return ("QUEUE", $message_id, $date, lc ($sender), $nrcpt, $size);
		}
		elsif (($reciever) = ($cmd =~ /^to=<(.+)>.*status=sent/i))
		{
			return ("SENT", $message_id, $date, lc ($reciever), 0, 0);
		}
	}

	return ("SKIP_LINE", "", "", "", 0, 0);
}

tie (%hdatedb  , "DB_File", "$dbdir/date.db"  , O_CREAT|O_RDWR, 0600) || die "Can't open/create date.db hash";
tie (%hsenderdb, "DB_File", "$dbdir/sender.db", O_CREAT|O_RDWR, 0600) || die "Can't open/create sender.db hash";
tie (%hnrcptdb , "DB_File", "$dbdir/nrcpt.db" , O_CREAT|O_RDWR, 0600) || die "Can't open/create nrcpt.db hash";
tie (%hsizedb  , "DB_File", "$dbdir/size.db"  , O_CREAT|O_RDWR, 0600) || die "Can't open/create size.db hash";

%hdate   = %hdatedb;
%hsender = %hsenderdb;
%hnrcpt  = %hnrcptdb;
%hsize   = %hsizedb;

$dbh = DBI->connect ("dbi:Pg:dbname=$dbase", $user, $passwd, {AutoCommit => 0});

$sth = $dbh->prepare ("INSERT INTO mail_log VALUES (?, ?, ?, ?, 1)");

while (<STDIN>)
{
	($state, $message_id, $date, $sender_reciever, $nrcpt, $size) = parseLogLine ($_);

	if ($state eq "QUEUE")
	{
		#  postfix        
		#        . ,  
		#  ,         
		# $message_id
		if ((! exists $hdate{$message_id}) && ($nrcpt > 0) && ($size > 0))
		{
			$hdate{$message_id}   = $date;
			$hsender{$message_id} = $sender_reciever;
			$hnrcpt{$message_id}  = $nrcpt;
			$hsize{$message_id}   = $size;
		}
	}

	elsif ($state eq "SENT")
	{
		if (exists $hdate{$message_id})
		{
			$sth->execute ($date, $hsender{$message_id}, $sender_reciever, $hsize{$message_id});
			if ($sth->state)
			{
				untie (%hdatedb);
				untie (%hsenderdb);
				untie (%hnrcptdb);
				untie (%hsizedb);

				$dbh->rollback;
				$dbh->disconnect;

				die "  ,    \n";
			}

			if (--$hnrcpt{$message_id} <= 0)
			{
				delete $hdate{$message_id};
				delete $hsender{$message_id};
				delete $hnrcpt{$message_id};
				delete $hsize{$message_id};
			}
		}
	}
}

%hdatedb   = %hdate;
%hsenderdb = %hsender;
%hnrcptdb  = %hnrcpt;
%hsizedb   = %hsize;

untie (%hdatedb);
untie (%hsenderdb);
untie (%hnrcptdb);
untie (%hsizedb);

$dbh->commit;
$dbh->disconnect;
