#!/usr/bin/perl -w
use strict;

###############################################################################
#####                                                                       ###
#####    BNC::Tools for [x]Chat by G. Nickels <g.nickels@nickels-it.de>     ###
#####                                                                       ###
###############################################################################



##### BEGIN CONFIG SECTION ####################################################

### activate / deactivate some of the scripts functions completely

my $use_who="false";                         # use who handling options
my $use_pingpong="false";                    # use ping/pong handling options

##### END CONFIG SECTION ######################################################



###############################################################################
#####                                         #################################
#####   !!! DON'T CHANGE ANYTHING BELOW !!!   #################################
#####                                         #################################
###############################################################################



##### global part #############################################################

# information about script name, author, etc.
my $scriptname='BNC::Tools';
my $scriptversion='0.1';
my $description='psyBNC handling tools';
my $year='2005';
my $author='Guido S. Nickels';
my $email='gsn@kernel-oops.de';

# script registration and onload output
my $providescommands=0;
IRC::register($scriptname,$scriptversion,$author,$description);
IRC::print("  \n");
IRC::print("Loading $scriptname $scriptversion\n");
IRC::print("  (C) $year $author <$email>\n");
IRC::print("  \n");
IRC::print("  :: Features enabled:          \n");
IRC::print("  ::                            \n");

# print enabled features

IRC::print("  :: pingpong - ping/pong handling\n") if($use_pingpong eq "true");

if($use_who eq "true"){
 $providescommands=1;
 IRC::print("  :: who      - who commands  \n");
}

# if some feature provides commands...

if($providescommands==1){
 IRC::print("  ::                            \n");
 IRC::print("  :: Commands:                  \n");
 IRC::print("  ::                            \n");
}

# print available commands

if($use_who eq "true"){
 IRC::print("  :: /rwho     </who options>\n");
}

IRC::print("  \n");


##### ping/pong handling part ##################################################

if($use_pingpong eq "true"){
 
 # ping/pong handler
 
 IRC::add_print_handler("Ping Reply","local_pp_pingpong_handler");

 sub local_pp_pingpong_handler {
  my $pp_server = IRC::get_info(3);
  my $pp_output = "@_";
  my @pp_line = split / /,$pp_output;
  if (@pp_line[0] eq $pp_server) {
   return 1;
  }
  else
  {
   return 0;
  }
 }
}

##### who info part ############################################################

if($use_who eq "true"){

 # who handler
 IRC::add_command_handler("rwho","local_who_rwho_handler");
 IRC::add_message_handler("352","local_who_who_handler");
 IRC::add_message_handler("315","local_who_who_handler");

 my $who_who_flag=0;

 sub local_who_rwho_handler {
  my $who_options=$_[0];
  
  # perform who on nick and set who info flag
  IRC::command("/who $who_options");
  
  # timeout
  IRC::add_timeout_handler(100,"local_who_flag_handler");
  $who_who_flag=1;
  return 1;
 }

 sub local_who_flag_handler { $who_who_flag=0; }
 
 sub local_who_who_handler {
 
  # only take action if the who is performed by our who_handler
  if ($who_who_flag==1) {
   return 0;
  }
  else {
   return 1;
  }
 }
}

