#!/usr/bin/perl -w

use strict;
use warnings;
use Data::Dumper;

my %PINGEES;

my $USAGE = '
Usage:  /galtpingon <username> <message>
        /galtpingoff <username>
';

Xchat::register(
		my $NAME    = 'Ping Galt', 
		my $VERSION = '001', 
		'Hello Galt'
		);
Xchat::print("$NAME $VERSION is teh loaded");

Xchat::hook_command(
		    'galtpingon' 
		    ,\&pinggalt_cmd
		    ,{ help_text => 'Usage: /galtpingon <username> <message>' }
		    );
Xchat::hook_command(
		    'galtpingoff' 
		    ,\&pinggalt_cmd
		    ,{ help_text => 'Usage: /galtpingoff <username>' }
		    );
Xchat::hook_print( 
		   'JOIN',
		   \&lurk_function
		   );

sub lurk_function {

   my $data = shift;
   my ($user, $channel, $email) = @$data;

   if (defined $PINGEES{$user}) {
      my $message = "msg $user " . $PINGEES{$user};
      Xchat::command($message);
      Xchat::print("ping-galt ALERTED $user; alert taken off");
      $PINGEES{$user} = undef;
   }
   return Xchat::EAT_NONE;

}

sub pinggalt_cmd {

   my $data = shift;

   my $cmd = shift(@$data);
   my $user = shift(@$data) || "";

   unless ($user =~ /\S/) {
      Xchat::print( $USAGE );
      return Xchat::EAT_XCHAT;
   }

   my $message = "";
   while (defined $data->[0]) {
      $message .= " " . shift(@$data);
   }

   if ($cmd eq "galtpingon") {
      if ($message =~ /\S/) {
	 $PINGEES{$user} = $message;
	 Xchat::print( "$user shall ping ye" );
      }
      else {
	 Xchat::print( $USAGE );
	}
   }
   elsif ($cmd eq "galtpingoff") {
      if (defined $PINGEES{$user}) {
	 $PINGEES{$user} = undef;
	 Xchat::print( "$user will no longer ping ye" );
      }
      else {
	 Xchat::print( "Dolt!  That user isn't destined to ping ye yet!");
      }
   }

   return Xchat::EAT_XCHAT;
}
