# botop.pl: bot checker, Version_0.1:[final] -- 6kiodine. (!6000 bytes).
# botop.pl: by: _v9[v9@ice.org] -- for use in X-Chat(built on 1.2.1).
# botop.pl: type "/load /pathto/botop.pl" in X-Chat or put it in ~/.xchat to auto-load on start.

# botop.pl: base vars. (1)
$hd=$ENV{"HOME"};
$pmt="\02***\02";
$lsoffset=4;
$oschar="0";
$ufunc=0;
$opterm="OP";
$botsendto=">>$hd/.xchat/botlist";
$botwriteto=">$hd/.xchat/botlist";
$botgetfrom="$hd/.xchat/botlist";
# botop.pl: subroutines. (2)
sub out{IRC::print "@_\n";}
sub bout{&out("$pmt botop\02.\02pl: @_");}
sub rargs{
 my $i=0;my $out;
 if (@_){$i=@_[0];}
 while (@args[$i]){$out="$out @args[$i]";$i++;}
 return substr($out,1,length($out));
}
sub totb{
 my $i=0;
 open(READ, $botgetfrom);
 my @read=<READ>;
 close(READ);
 while(@read[$i]){$i++;}
 return $i/2;
}
sub getb{
 my $i=-2;
 if (@_[1] eq "host"){$i++;}
 open(READ, $botgetfrom);
 my @read=<READ>;
 close(READ);
 if (@_[1] ne "host"){
  my @m=split(/ /, $read[$i+$_[0]*2]);
  if (@_[1] eq "passwd") {return @m[1];}
  else {return @m[0];}
 }
 else {return $read[$i+$_[0]*2];}
}
sub botcheck{
 my $m=IRC::get_info(3);
 if (!$m) {&bout("no connection to botop channel(s).");$ufunc=0;return 1;}
 else {
  my @args=split(/\+/, @_[0]);
  if (!@args[1]) {
   if ($ufunc) {&bout("no host reply, aborting function: $ufunc.");}
   else {&out("\02userhost\02: no userhost reply.");}
   $ufunc=0;return 1;
  }
  if ($ufunc eq "add") {
   open(READ, $botsendto) || &bout("error opening $botsendto.");
   print READ "$ufunc_user $ufunc_passwd\n@args[1]\n";
   close(READ);
   &bout("added bot $ufunc_user(@args[1]) to your botlist.");
  }
  elsif ($ufunc eq "op") {
   if (@args[1] eq $ufunc_host) {
    IRC::send_raw "PRIVMSG $ufunc_user :$opterm $ufunc_pass $ufunc_chan\n";
    &bout("hostmask matched stored hostmask, sending passwd.");
   }
   else {&bout("hostmask did not matched stored hostmask, aborting.");}
   $ufunc=0;return 1;
  }
  if ($ufunc){$ufunc=0;return 1;}
  else {
   my @m=split(/ /, $_[0]);my $m=substr(@m[3],1,length(@m[3]));
   &out("\02userhost\02: $m");return 1;
  }
 }
}
sub bothandler{
 @args=split(/ /, shift); #@args is the equivalent of @ARGV.
 $ufunc_chan=@args[2];chomp $ufunc_chan;
 if (! -d "$hd/.xchat"){
  my $success=mkdir "$hd/.xchat",022;
  if ($success){&bout("~/.xchat did not exist, created successfully.");}
  else {&bout("error in creation of ~/.xchat, halting.");return 1;}
 }
 if (@args[0] eq "add"){
  if (!@args[2]){&bout("/botop \02add\02 <\02nickname\02> <\02password\02>");return 1;}
  my $m=IRC::get_info(3);
  if (!$m) {&bout("no connection to add a bot.");return 1;}
  if ($ufunc) {&bout("function already in progress: $ufunc.");return 1;}
  my $i=1;
  while ($i <= &totb()){
   if (&getb($i, "nick") eq "@args[1]") {&bout("bot is already on your list, delete it first.");return 1;}
   $i++;
  }
  $ufunc="add";
  $ufunc_user=@args[1];
  $ufunc_passwd=&rargs(2);
  IRC::command "/userhost $ufunc_user";
  &bout("attempting to get @args[1]\'s user information to add to your list.");
 }
 elsif (@args[0] eq "del"){
  if (!@args[1] || int(@args[1]) != @args[1]){&bout("/botop \02del\02 [\02#\02]");return 1;}
  if (@args[1] > &totb() || @args[1] < 1){&bout("invalid bot number selected.");return 1;}
  my $i=0;my $j=0;
  open(READ, $botgetfrom) || &bout("error opening $botgetfrom.");
  my @read=<READ>;
  close(READ);
  open(SEND, $botwriteto) || &bout("error opening $botwriteto.");
  while (@read[$i]){
   if (@args[1]*2-2 == $i || $j eq "1"){
    if ($j eq "1"){$j--;}
    else {my @m=split(/ /, @read[$i]);$m=@m[0];chomp $m;$j++;}
   }
   else {print SEND "@read[$i]";}
   $i++;
  }
  close(SEND);
  &bout("deleted bot number: @args[1]. ($m)");
 }
 elsif (@args[0] eq "list"){
  my $i=1;my $j;
  &bout("listing bot(s).");
  while ($i <= &totb()){
   my $m=&getb($i, "nick");chomp $m;
   my $n=&getb($i, "host");chomp $n;
   my $num=$lsoffset-length($i);my $nu=0;my $j;
   while ($num > $nu){$j="$oschar$j";$nu++;}
   &out("$pmt (\02$j$i\02)\02 $m(\02$n\02)");
   $i++;
  }
  if ($i eq "1"){&bout("no bots found.");return 1;}
  &bout("completed bot list.");
 }
 elsif (@args[0] eq "op"){
  if (!@args[1] || int(@args[1]) != @args[1]){&bout("/botop \02op\02 [\02#\02] <\02#channel\02>");return 1;}
  if (@args[1] > &totb() || @args[1] < 1){&bout("invalid bot number selected.");return 1;}
  if ($ufunc) {&bout("function already in progress: $ufunc.");return 1;}
  $ufunc="op";
  $ufunc_host=&getb(@args[1], "host");chomp $ufunc_host;
  $ufunc_pass=&getb(@args[1], "passwd");chomp $ufunc_pass;
  $ufunc_user=&getb(@args[1], "nick");chomp $ufunc_user;
  if ($ufunc_chan) {if (substr($ufunc_chan,0,1) ne "#") {$ufunc_chan="#$ufunc_chan";}}
  IRC::command "/userhost $ufunc_user";
  &bout("attempting to check $ufunc_user\'s user information to send passwd.");
 }
 elsif (@args[0] eq "clear"){
  if (! -f "$hd/.xchat/botlist"){&bout("no botlist file to clear.");}
  else {
   my $success=unlink "$hd/.xchat/botlist";
   if ($success){&bout("cleared botlist file successfully.");}
   else {&bout("botlist file was cleared un-successfully.");}
  }
 }
 elsif (@args[0] eq "help"){
  &bout("-- \02syntax help for /botop.\02 --");
  &bout("(\02add    \02)\02 add a channel to your botlist.");
  &bout("(\02del    \02)\02 delete a stored botlist numeric value.");
  &bout("(\02list   \02)\02 list current channel(s) in storage.");
  &bout("(\02op     \02)\02 check hostmask, and send op request if a valid bot.");
  &bout("(\02clear  \02)\02 clear bot(s) in storage.");
  &bout("(\02#      \02)\02 equivalent of /botop op [#] <#channel>.");
 }
 elsif (&totb() >= @args[0] && @args[0] > 0) {IRC::command "/botop op @args[0] @args[1]";}
 else {&bout("/botop [\02add\02|\02del\02|\02list\02|\02op\02|\02clear\02|\02#\02|\02help\02]");}
 return 1;
}
# botop.pl: init. (3)
IRC::add_command_handler("botop", "bothandler");
IRC::add_message_handler("302", "botcheck");
&bout("by _v9[v9\@ice.org], type \"\02/botop help\02\" for syntax help.");
# botop.pl: eof. (4)
