#!/usr/bin/perl
use strict;

# this is a quick hack to count how many servers are behind
# the sitefinder-idn.verisign.com SMTP rejector.  -nick

# UPDATE!  This does not work anymore.  It just spews errors really fast.
# VeriSign switched out the SMTP server.
#
# However, the other version of this script for counting the httpd
# redirectors still works.  It can be found linked at:
# http://memedev.nicklevay.net/users/rattle/blogid3255361
# And also see http://memedev.nicklevay.net/users/rattle/blogid3255361

use Net::SMTP;

use vars qw( %map $tq );
$| = 1;
$SIG{INT} = \&got_sig_int;

while (1) {
	my $s = Net::SMTP->new('lkasjlkdsfj.net',
		Hello => 'some-dot.net',
		Timeout => 10,
		Debug => 0
	) || die $@;
	$tq++;
	my $host;
	if(my $banner = $s->banner) {
		if ($banner =~ m/^(.*) Snubby Mail Rejector/) {
			$host = $1;
		}
		if ($host =~ m/(.+)-(.+)/) {
			if (defined $map{$2}{$1}) {
				print ".";
			} else {
				$map{$2}{$1} = 0;
				print "+";
			}
			$map{$2}{$1}++;
		} else {
			print "\nSERVER NAME STRING NOT FOUND\n";
		}
	} else {
		print "\nSMTP ERROR\n";
	}
	$s->quit;
}

sub got_sig_int {
	print "\n\n";
	foreach my $loc (sort keys %map) {
		print "*** $loc\n";
		my $min = 9999;
		my ($max, $total);
		my $row = 1;
		foreach my $server (sort keys %{$map{$loc}}) {
			my $n = $server;
			$n =~ s/[a-z]*//ig;
			$min = $n if $n lt $min;
			$max = $n if $n gt $max;
			$total++;
			print "   \t$server: $map{$loc}{$server}";
			if ($row eq 4) {
				print "\n";
				$row = 0;
			}
			$row++;
		}
		print "\nserver range: $min <==> $max\n\n";
	}
	print "*** total queries: $tq\n";
	exit;
}
