#!/usr/bin/perl -w
use strict;
my %patterns=();
my %reason=();
my ($line,$net,$revnet);

while(defined($line=<>)) {
	chomp $line;
	die $line unless($line =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)(?:[:=](.+))?$/s);
	if(defined($net)) {
		die "netblock changed from $net. to $1.$2.$3." unless("$1.$2.$3" eq $net);
	} else {
		$net="$1.$2.$3";
		$revnet="$3.$2.$1";
	}
	my ($d,$name)=($4,$5);
	$name="" unless(defined($name));
	$reason{$d}="ends in digit" if($name =~ /\d$/s);
	$reason{$d}="contains last octet in name" if($name =~ /\b0*$d\b/s or $name =~ /^$d/s);
	$name =~ s/[^-.0-9A-Za-z]/_/gs;
#	1 while($name =~ s/^([^.]*?|[#.]+)\d+/$1#/s);
	1 while($name =~ s/\d+(.*\..*\.)/#$1/s);
	$patterns{$name}=[] unless($patterns{$name});
	push @{$patterns{$name}},$d;
}
foreach my $pattern (keys %patterns) {
	if("" eq $pattern or 7<=@{$patterns{$pattern}} or not $pattern =~ /\./) {
		my $reason=@{$patterns{$pattern}}." entries match $pattern";
		$reason=$pattern unless($pattern =~ /\./);
		$reason="no reverse DNS entry" unless(length($reason));
		map {$reason{$_}=$reason} @{$patterns{$pattern}};
	}
}
my $numbad=scalar(keys %reason);
if(200<$numbad) {
	print "+*.$revnet.netblock.pedantic.org:127.0.0.2::\n";
	for(my $d=0;$d<256;$d++) {
		next if(exists $reason{$d});
		print "'$d.$revnet.netblock.pedantic.org:Excluded::\n";
	}
} elsif(50<$numbad) {
	for(my $d=0;$d<256;$d++) {
		next unless(exists $reason{$d});
		print "+$d.$revnet.netblock.pedantic.org:127.0.0.2::\n";
		print "'$d.$revnet.netblock.pedantic.org:$reason{$d}::\n" if($reason{$d});
	}
}
0;
