#!/usr/local/bin/perl
#
# access2drbl converts sendmail-style access file to drbl zone
#
# Usage: access2drbl [header] < access > drbl
#
# Optional header just prepends the output. You should
# place your SOA, NS and other common records there.
# If you place $time$ somewhere in the header file, it will be replaced
# with the current time in seconds since epoch so you can use it as 
# your SOA serial number. 


my ($header) = $ARGV[0] if @ARGV >= 0;

if ( $header ) {
	open (HEADER, $header) || die "Can't open $header: $!\n";
	while (<HEADER>) { s/\$time\$/time()/ge; print; }
	close (HEADER);
}


while (<STDIN>) {
	chomp;
	next unless $_;
	my ($key, $res) = split (/\s+/, $_, 2);
	next if $key =~ /[^0-9\.]/o;
	next if $res =~ /^(ok|relay)/oi;
	my $ip = join ('.', reverse (split(/\./, $key)));
	print "$ip\tIN\tA\t127.0.0.2\n";
	print "$ip\tIN\tTXT\t\"$res\"\n";
	if ( ($ip =~ tr/\./\./) < 3 ) {
		print "*.$ip\tIN\tA\t127.0.0.2\n";
		print "*.$ip\tIN\tTXT\t\"$res\"\n";
	}
}
