#!/usr/local/bin/perl 
# Gopher-NNTP Gateway version 1.0
# Author: Daniel Schales (dan@engr.latech.edu)
# Major rewrite, socket support: Doug Schales (d1s8027@sc.tamu.edu)
#
# Set the 4 following variables for your setup. the 2 port variables
# are set to the standard, be sure to set gopherhost and nntphost to
# your respective hosts.
$SIG{'ALRM'} = 'stuck';
$gopherhost="aurora.engr.latech.edu";
$gopherport=70;
$nntpprt=119;
$nntphost="aurora.latech.edu";
$option=shift;
$item=shift;
$lookup=shift;
@INC=("/usr/local/lib/perl");
require 'sys/socket.ph';
# set an alarm 5 minutes from now, if it goes off we must be stuck
alarm(300);
open(LOG,">>/tmp/nntplog");
$date=`date`;chop($date);
print LOG $date," ",$option," ",$item," ",$lookup,"\n";
close(LOG);
$sockaddr = 'S n a4 x8';
($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $nntpport) = getservbyname('nntp', 'tcp');
($name, $aliases, $type, $len, $nntpaddr) = gethostbyname($nntphost);

$rsockaddr = pack($sockaddr, &AF_INET, $nntpport, $nntpaddr);

socket(NNTPSOCK, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
connect(NNTPSOCK, $rsockaddr) || die "connect: $!";

select(NNTPSOCK); $|= 1; select(stdout);

$_ = <NNTPSOCK>;


if($option eq "-g"){
    print NNTPSOCK "LIST\n";
    open(SORT,"|sort");
    $_ = <NNTPSOCK>;
    chop; chop;
    while($_ ne "."){
	if($_ =~ "^$item"){
	    ($group) = split;
	    print SORT "1$group\texec:-t $group:/bin/gonntp\t$gopherhost\t$gopherport\r\n";
	}
	$_ = <NNTPSOCK>;
	chop; chop;
    }
    close(SORT);
    print ".\r\n";
}if($option eq "-G"){
    print NNTPSOCK "LIST\n";
    open(SORT,"|sort");
    $_ = <NNTPSOCK>;
    chop; chop;
    while($_ ne "."){
	if($_ =~ "^$item"){
	    ($group) = split;
	    print SORT "1$group\texec:-T $group:/bin/gonntp\t$gopherhost\t$gopherport\r\n";
	}
	$_ = <NNTPSOCK>;
	chop; chop;
    }
    close(SORT);
    print ".\r\n";
}
elsif($option eq "-X") {
	($group,$thread) = split(/_/,$item);
	print NNTPSOCK "XINDEX $group \n";
	$_=<NNTPSOCK>;
	$grp = <NNTPSOCK>;
	$num = <NNTPSOCK>;
	for($cnt=0;$cnt <= $num;$cnt++){ $numart[$cnt] = 0;}
	$last = <NNTPSOCK>;
	$count=0;
	$_=<NNTPSOCK>;chop;chop;
	while ($_ ne ".") {
	$article[$count] = $_;
	$subject[$count] = <NNTPSOCK>;chop($subject[$count]);chop($subject[$count]);

	$author[$count] = <NNTPSOCK>;chop($author[$count]);chop($author[$count]);
	$name[$count] = <NNTPSOCK>;chop($name[$count]);chop($name[$count]);
for($i=1;$i<=4;$i++){	$skip = <NNTPSOCK>;}
	$count++;
	$_ = <NNTPSOCK>;
	chop;chop;
	
	}
for($cnt=0;$cnt < $count;$cnt++) {
	if($cnt eq $thread || substr($subject[$cnt],1) eq $thread) {
if (! index($author[$cnt],"%")) { 
$author[$cnt]=$author[substr($author[$cnt],1)];}
if (! index($name[$cnt],"%")) { 
$name[$cnt]=$name[substr($name[$cnt],1)];}


print "0$name[$cnt] $author[$cnt]\texec:-a ${group}_$article[$cnt]:/bin/gonntp\t$gopherhost\t$gopherport\r\n";
}
}
}

elsif($option eq "-T") {
	print NNTPSOCK "XINDEX $item \n";
	$_=<NNTPSOCK>;
	$group = <NNTPSOCK>;
	$num = <NNTPSOCK>;
	for($cnt=0;$cnt <= $num;$cnt++){ $numart[$cnt] = 0;}
	$last = <NNTPSOCK>;
	$count=0;
	$_=<NNTPSOCK>;chop;chop;
	while ($_ ne ".") {

	$article[$count] = $_;
	$subject[$count] = <NNTPSOCK>;chop($subject[$count]);chop($subject[$count]);
		if (! index($subject[$count],"%")) {
		$numart[substr($subject[$count],1)]++;}

	$author = <NNTPSOCK>;
	$name = <NNTPSOCK>;
	for($i=1;$i<=4;$i++){	$skip = <NNTPSOCK>;}
	$count++;
	$_ = <NNTPSOCK>;
	chop;chop;
	}
	
	for($cnt=0;$cnt < $count;$cnt++) {
	if(index($subject[$cnt],"%")) {
	
	if($numart[$cnt] eq "0") {
print "0$subject[$cnt]\texec:-a ${item}_$article[$cnt]:/bin/gonntp\t$gopherhost\t$gopherport\r\n";
}
else {
print "1$subject[$cnt]\texec:-X ${item}_$cnt:/bin/gonntp\t$gopherhost\t$gopherport\r\n";
}
}
}
}
elsif($option eq "-t") {
	print NNTPSOCK "XINDEX $item \n";
	$_=<NNTPSOCK>;
	$group = <NNTPSOCK>;
	$num = <NNTPSOCK>;
	$last = <NNTPSOCK>;
	$count = 0;
	$_=<NNTPSOCK>;chop;chop;
	while ($_ ne ".") {
	$article[$count] = $_;
	$subject[$count] = <NNTPSOCK>;chop($subject[$count]);chop($subject[$count]);

	$author = <NNTPSOCK>;
	$name = <NNTPSOCK>;
for($i=1;$i<=4;$i++){	$skip = <NNTPSOCK>;}
	$count++;
	$_ = <NNTPSOCK>;
	chop;chop;
	
	}

for($cnt=0;$cnt < $count;$cnt++) {
if (! index($subject[$cnt],"%")) { 
$subject[$cnt]=" Re:$subject[substr($subject[$cnt],1)]";}
print "0$subject[$cnt]\texec:-a ${item}_$article[$cnt]:/bin/gonntp\t$gopherhost\t$gopherport\r\n";
}
}
elsif($option eq "-l"){
     print NNTPSOCK "GROUP $item\n";
     $_ = <NNTPSOCK>;
     if($_ =~ "^211"){
         ($code, $count, $start, $end, $gname) = split;
         if($count ne "0"){
             print NNTPSOCK "ARTICLE $end\n";
             $body=0;
             $_ = <NNTPSOCK>;
             chop; chop;
             while($_ ne "."){
 	        if($body){
 	            print "$_\r\n";
 	        }
 	        elsif($_ =~ "^220 " || $_ =~ "^222 "){
 	            $body = 1;
 	        }
 	        $_ = <NNTPSOCK>;
 	        chop; chop;
             }
 	}
     }
 }
# rwp 20Aug92 Add ability to fetch last article.

elsif($option eq "-h" || $option eq "-b" || $option eq "-s"){
    print NNTPSOCK "GROUP $item\n";
    $_ = <NNTPSOCK>;
    if($_ =~ "^211"){
	($code, $count, $start, $end, $gname) = split;
	if($count ne "0"){
	    print NNTPSOCK "XHDR SUBJECT $start-$end\n";
	    <NNTPSOCK>;
	    $_ = <NNTPSOCK>;
	    chop; chop;
	    while($_ ne "."){
		($num,$desc) = split (/ /,$_,2);

		if ($option eq "-h" ) {
		print "0$desc\texec:-a ${item}_${num}:/bin/gonntp\t$gopherhost\t$gopherport\r\n";
}
elsif ($option eq "-b") {
print "0$desc\texec:-a ${item}_${num}_body:/bin/gonntp\t$gopherhost\t$gopherport\r\n";
}
elsif ($option eq "-s") {
$desc1="\L$desc\E";
$lookup1 ="\L$lookup\E";

if ($desc1 =~ $lookup1 ) {
print "0$desc\texec:-a ${item}_${num}:/bin/gonntp\t$gopherhost\t$gopherport\t\r\n";
}

}

		$_ = <NNTPSOCK>;
		chop; chop;
	    }
	}
    }
    print ".\r\n";
}
elsif($option eq "-a"){
    ($group, $num, $part) = split(/_/,$item);
    print NNTPSOCK "GROUP $group\n";
	    if($part eq "body") {
    print NNTPSOCK "BODY $num\n";
	}
    else {
    print NNTPSOCK "ARTICLE $num\n";
	}
	$body=0;
    $_ = <NNTPSOCK>;
    chop; chop;
    while($_ ne "."){
	if($body){
	    print "$_\r\n";
	}
	elsif($_ =~ "^220 " || $_ =~ "^222 "){
	    $body = 1;
	}
	$_ = <NNTPSOCK>;
	chop; chop;
    }
}

print NNTPSOCK "QUIT\n";
shutdown(NNTPSOCK, 2);

sub stuck {
open(LOG,">>/tmp/nntplog");
$date=`date`;chop($date);
print LOG $date," hung on ",$option," ",$item," ",$lookup,"\n";
close(LOG);

exit;
}





