#/usr/local/bin/perl

#----------------------------------------------------------------------
# variables you can change:

#$webster_server  = "webster.lcs.mit.edu";
#$webster_port    = 103;

$webster_server = "nakamichi.micro.umn.edu";
$webster_port   = 2627;

@webster_acl=(
#     ipaddress  access + = allow, - = deny    
     '^134.84\.      +',
     '^128.101\.     +',
     '.*         -'
  );

#----------------------------------------------------------------------

sub webster_main {
 local($_ )= @_;

 &Gsorry if /^sorry$/;

  if (&check_access() eq '-') {
      &Greply("0Sorry! No access off of campus!\t$Ggw sorry\t$Ghost\t$Gport");
      &Greply("."); 
      exit; 
  }

 if (/^$/) {
  &Greply("7Lookup word in dictionary\t$Ggw default SPELL\t$Ghost\t$Gport");
  &Greply("7Lookup word in dictionary (phonetic match)\t$Ggw default PSPELL\t$Ghost\t$Gport");
  &Greply("7Search text of entire dictionary\t$Ggw dictionary-full SPELL\t$Ghost\t$Gport");
  &Greply("7Show words that start with\t$Ggw default ENDINGS\t$Ghost\t$Gport");
  &Greply("7Thesaurus\t$Ggw thesaurus SPELL\t$Ghost\t$Gport");
  &Greply(".");
  exit(0);
 }

 ($index,$cmd,$query) = /^(\S+)\s+(\S+)\s+(.+)$/;

  &GopenServer($webster_server,$webster_port);

  &Gsend("INDEX $index") if ($index ne 'default');

  if ($cmd eq "PSPELL") {                     # phonetic lookup
       &Gsend("SPELL $query ?");
  } else {
       &Gsend("$cmd $query");
  }

$_ = &Grecv;

  if (/^SPELLING 0/ || /^WILD 0/) {
        &Greply(".");
   } elsif (/^SPELLING 1/) {
      &Greply("0$query\t$Ggw $index DEFINE $query\t$Ghost\t$Gport"); 
      &Greply(".");
   } elsif (/^SPELLING$/ || /^MATCHS$/ || /^WILD/) {
        $/ = "\200";
        $buf = <GSERVER>;
        $buf =~ s/[\r\200]//g;
        foreach (split(/\n/,$buf)) { /\d+\s+(\S+)/; $words{$1}=""; }
        foreach (sort keys %words) { 
               &Greply("0$_\t$Ggw $index DEFINE $_\t$Ghost\t$Gport"); 
        }
        &Greply(".");
   } elsif (/^DEFINITION/) {
        $/ = "\200";
        $buf = <GSERVER>;
        $buf =~ s/[\r\200]//g;
        print $buf;
   } else {
        &Greply(".");
   }
  &Gsend("QUIT");
  &GcloseServer;
  exit;
}

sub check_access {

   return 1 if (-t STDIN);
   $sockaddr = 'S n a4 x8';
   $mysockaddr = getpeername(STDIN);
   ($ramily,$rport,$raddr) = unpack($sockaddr,$mysockaddr);
   ($a,$b,$c,$d) = unpack('C4',$raddr);
   $ipaddress = "$a.$b.$c.$d";

   foreach (@webster_acl) {
      ($ipacl,$access)=split;
      return $access if  ($ipaddress =~ /$ipacl/);
   }
   return '-'; #default is to restrict access
}

1; # for require
