print "${BGreen}Loading call.pl\n";
my %IPList = ();
my $ip;

sub match_ip {

  my $text = $_;
  my $ThisName = "";

  $text =~ s/(\xEA.|\xEA\n)//g;

  # This covers clan channel...

  if ($text =~ /You feel (\w*) say, \'.*?(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
    $ThisName = $1;
    $ip = "$2.$3.$4.$5";
    $IPList{lc($ThisName)} = $ip;
    print "\n${BGreen}Added ${BWhite}${ThisName} ${BGreen}to ip list.\n";
  }

  # This should cover many chanels...

  elsif ($text =~ /(\w*).*?,\s\'.*?(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
    $ThisName = $1;
    $ip = "$2.$3.$4.$5";
    $IPList{lc($ThisName)} = $ip;
    print "\n${BGreen}Added ${BWhite}${ThisName} ${BGreen}to ip list.\n";
  }

  # This covers imm...

  elsif ($text =~ /(\w*):: .*?(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
    $ThisName = $1;
    $ip = "$2.$3.$4.$5";
    $IPList{lc($ThisName)} = $ip;
    print "\n${BGreen}Added ${BWhite}${ThisName} ${BGreen}to ip list.\n";
  }
  
  # And this is just in case all else fails you can get the last one.
  
  elsif ($text =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/) {
    $ip = "$1.$2.$3.$4";
  }
}

sub cmd_cip {
  $ThisName = $_;
  $ThisName =~ s/\n//g;
  if (/^$/) {
    run("${commandCharacter}chat.call $ip");
  }
  else {
    if (defined $IPList{lc($ThisName)}) {
      run("${commandCharacter}chat.call $IPList{lc($ThisName)}");
    }
    else {
      print "No IP address has been stored for $ThisName.\n";
    }
  }
}

sub cmd_iplist {
  print "${BGreen}List of stored IP addresses:\n";
  print "-------------------------------------\n";
  foreach (keys %IPList) {
    printf ("${BCyan}%15s ${BRed}%s\n", $_, $IPList{$_});
  }
}

sub save_ips {
  local (*FH) = $_[0];
  foreach (keys %IPList) {
    print FH "ip ", $_, " $IPList{$_}\n";
  } 
}

sub load_ips {
  if (/(.*)\s+(.*)/) {
    $IPList{$1} = $2;
  }
}

load_add("ip", \&load_ips);
save_add(\&save_ips);
output_add(\&match_ip);





