#!/usr/pkg/bin/perl -w

# Written by Travis Whitton <whitton@atlantic.net>                                                              
# Released under the conditions of the GPL.                                                                     
# This was a *very* quick hack so please be gentle                                                              
# with flames and submit bug reports to whitton@atlantic.net.                                                   
# Thanks to: 
# Decklin Foster <fosterd@hartwick.edu> 
# for adding an exclude server feature and offering
# some perl tips.
# German Gutierrez <german@dd.com.ar> 
# removed dependence upon LWP.

use strict;
use Socket;

my $exclude = qr/WhateverYouWant|SomeServer/;
my $napsite = "www.napigator.com";
my $napport = 80;
my $getta = "GET /list.php\n\n";
my $nap_page = 'http://www.napigator.com/list.php';
my ($nick, $pass, $meta, $option, $string);
my ($host, $ip, $port, $network, $proto, $sin);

# Parse command line options. Ugly code...

for (my $i = 0; $ARGV[$i]; $i++) {
    if ($ARGV[$i] =~ /-h|--help/) {
        show_help();
    } elsif ($ARGV[$i] =~ /-t|--teknap/) {
        $option = '-t';
    } elsif ($ARGV[$i] =~ /-n|--nick/) {
        if ($ARGV[$i + 1] && $ARGV[$i + 1] !~ /^-/) {
            $nick = $ARGV[$i + 1] unless ($ARGV[$i + 1] =~ /^-/);
        } else {
            print "No nickname specified.\n";
            show_help();
        }
    } elsif ($ARGV[$i] =~ /-p|--pass/) {
        if ($ARGV[$i + 1] && $ARGV[$i + 1] !~ /^-/) {
            $pass = $ARGV[$i + 1] unless ($ARGV[$i + 1] =~ /^-/);
        } else {
            print "No password specified.\n";
            show_help();
        }
    } elsif ($ARGV[$i] =~ /-m|--meta/) {
        if ($ARGV[$i + 1] && $ARGV[$i + 1] !~ /^-/) {
            $meta = $ARGV[$i + 1];
        } else {
	    print "No meta specified.\n";
            show_help();
	}
    }
}

die("Can't set password without username") if (!$nick && $pass);
die("Nick, pass, and meta are only useful with TekNap format") if (!$option && ($nick || $pass || $meta));

# Setup the connection.
$proto = getprotobyname('tcp');
socket(F, PF_INET, SOCK_STREAM, $proto);
$sin = sockaddr_in($napport, inet_aton($napsite));
connect(F, $sin) || die("Damn! Couldn't connect to napigator");

# Send the request.
syswrite(F, $getta, length($getta));

# Get and print out the response.

while (<F>) {
    if (/^<tr>/) {
	s/\&nbsp;//g;
	s/<\/?(font|tr|a).*?>//g;
	s/<td.*?>//g;

	if ( (/td/) && (($host, $ip, $port, $network) = split /<\/td>/)) {
	    if ($option && $option eq '-t') {
		$string = "server +add $host:$port:";
		$string .= $nick if ($nick);
		$string .= ':';
		$string .= $pass if ($pass);
		$string .= ':';
		$string .= $meta if ($meta);
		print "$string\n";
	    } else {
		print "$ip:$port $host ($network)\n" unless $network =~ $exclude;
	    }
	}                                                                                                       
    }
}                                 

close(F);

sub show_help {
    print "USAGE: gnapfetch [OPTION]\n";
    print "  -h, --help        generate this list\n";
    print "  -t, --teknap      generate list for TekNap instead of gnapster\n";
    print "\nTekNap only options:\n";
    print "  -n [name], --nick[name]   use specified nickname in server list\n";
    print "  -p [pass], --pass[pass]   use specified password in server list\n";
    exit(0);
}
