#!/usr/bin/perl

# autorealname script for xchat
# prints the real name of users on join
#
# Copyright (c) 2002 Jamie Wilkinson <jaq@spacepants.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# maximum number of whois queries before we decide to stop... probably
# people are riding in on the netsplit wave.
my $queue_full_size = 5;

# print out debugging messages or not
my $debugging = 0;

# NO USER MODIFIABLE PARTS AFTER THIS LINE

my $VERSION = "0.3";

my %queue;

sub join_handler {
  my $message = shift;
  $message =~ /:(.*)!(\S+) JOIN :(.*)/i;
  my $handle = $1;
  my $channel = $3;

  # don't display realname for ourself
  return if (IRC::get_info(1) eq $handle);

  if ($queue{$handle}) {
    IRC::print_with_channel("--- $handle already in the queue", $channel, IRC::get_info(3)) if $debugging;
  } elsif (scalar %queue > $queue_full_size) {
    IRC::print_with_channel("--- queue too big, not asking", $channel, IRC::get_indo(3)) if $debugging;
  } else {
    IRC::print_with_channel("--- sending whois for $handle", $channel, IRC::get_info(3)) if $debugging;
    $queue{$handle} = $channel;
    IRC::command("/whois $handle");
  }
}

sub whoisuser_handler {
  my ($server, $code, $to, $handle, $name, $host, $star, $realname) = split(/ +/, $_[0], 8);
  $realname =~ s/^://;
  if ($queue{$handle}) {
    IRC::print_with_channel("-\cC10-\cC11-\cC0 $handle\'s real name is $realname\n", $queue{$handle}, IRC::get_info(3));
    return 1;
  }
}

sub whoisextra_handler {
  my ($server, $code, $to, $handle, $msg) = split(/ +/, $_[0], 5);

  if ($queue{$handle}) {
    return 1;
  }
}

sub endofwhois_handler {
  my ($server, $code, $to, $handle, $msg) = split(/ +/, $_[0], 5);
  if ($queue{$handle}) {
    delete $queue{$handle};
    return 1;
  }
}

IRC::register("autorealname", $VERSION, "", "");
IRC::add_message_handler("JOIN", "join_handler");
IRC::add_message_handler("311", "whoisuser_handler");
IRC::add_message_handler("318", "endofwhois_handler");
# extra messages generated by a whois
IRC::add_message_handler("319", "whoisextra_handler");
IRC::add_message_handler("312", "whoisextra_handler");
IRC::add_message_handler("317", "whoisextra_handler");
IRC::add_message_handler("320", "whoisextra_handler");
IRC::add_message_handler("335", "whoisextra_handler");
IRC::add_message_handler("307", "whoisextra_handler");
IRC::print ":: autorealname $VERSION ::\n";




