#!/usr/bin/perl -w
init_word();
print "What is your name? ";
$name = <STDIN>;
chomp $name;
if ($name =~ /^aaron\b/i)
{
   print "Hello, Aaron. How good of you to join me.\n";
}
else
{
   print "Hello $name.\n";
   print "What is the secret word? ";
   $guess = <stdin>;
   chomp $guess;
   while (!good_word($name, $guess))
   {
      print "Wrong try again. What is the secret word? ";
      $guess = <stdin>;
      chomp $guess;
   }
}
sub good_word
{
   my($somename, $someguess) = @_;
   $somename =~ s/\W.*//;
   $somename =~ tr/A-Z/a-z/;
   if($somename eq "aaron") {
      return 1;
   } elsif ( ($words{$somename} || "groucho") eq $someguess) {
      return 1;
   } else {
      open MAIL, "|mail user\@localhost";
      print MAIL "bad news: $somename guessed $someguess\n";
      close MAIL;
      return 0;
   }
}  
sub init_word
{
   open (WORDLIST, "wordlist") || die "Can't open wordlist : $!";
   if(-M WORDLIST >= 7.0){
      die "Sorry, the wordlist is older than seven days.";
   }
   while(defined ($name = <WORDLIST>)){
      chomp($name);
      $word = <WORDLIST>;
      chomp($word);
      $words{$name} = $word;
      }
   close(WORDLIST) || die "Couldn't close wordlist : $!";
}