#! /usr/bin/perl

# tail -f /var/log/apache/access.log | ./isDL
 
# Variables

$CONFIG = "/home/users/michoux/.isDL";
$MAILTO = "michoux";

open(IN, "tail -f /var/log/apache/access.log|");

while(<IN>)
{
	s/(.*) - - \[(.*)\] \"GET (.*)\" ([0-9]+) [0-9\-]+ \".*\" \"(.*)\" [0-9]+ (.*)/$1µ$2µ$3µ$4µ$5µ$6/;
	($host, $date, $url, $code, $navig, $vhost) = split("µ");
	open (CONF, "<$CONFIG") || die "Fichier de config de merde";
	if ($code != 404)
	{
		while(<CONF>)
		{
			chomp;
	#		push @nospam, $_;
	#	}
	#	foreach(@nospam)
	#	{
			$match = $_;
			$_ = $url;
			# Si $_ contient $match alors OK comme ED
#			print "<$match> et _ $_ _\n";
			if(/$match/)
			{
			open (SENDMAIL, "|/usr/lib/sendmail -odd -t") or die "Cannot run sendmail: $!\n";
			print SENDMAIL <<EOFMAIL;
From: Vigilant Automate <ik\@nospam.org>
To: michoux  
Subject: $url isDL
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

$url a ete downloade par $host le $date 
sous $navig sur $vhost.

-- 
Cordialement, Ta machine preferee.
EOFMAIL
			close SENDMAIL;

			}
		}
		close (CONF);
	}
}
close(IN);

# Lecture du fichier de config

#while(<>)
#{
#	s/(.*) - - \[(.*)\] \"GET (.*)\" ([0-9]+) [0-9\-]+ \".*\" \"(.*)\" [0-9]+ (.*)/$1µ$2µ$3µ$4µ$5µ$6/;
#	($host, $date, $url, $code, $navig, $vhost) = split("µ");
#	print "$host\n";
#}
