#!/usr/bin/perl -w

IRC::register("fserv bot","0.1","","");
IRC::print "Loading fserv script\n";
IRC::add_command_handler("start", "fserv_start");
IRC::add_command_handler("continue","fserv_continue");
IRC::add_print_handler("DCC RECV Complete", "dcc_done");
IRC::add_message_handler("DCC", "got_dcc_data");

sub fserv_start
{
  IRC::command("dir");
  while($items[0]) {
     shift(@items);
  }
  return 1;
}

sub got_dcc_data
{
  $aline = shift(@_);
  @items = split ' ', $aline;
  $count = @items;
  if($count == 5){
     push @files, $items[3];
  }
  if($count == 6){
     if($items[3] eq "End"){
        IRC::print "End detected...\n";
        $filenum = @files;
        IRC::print "Received $filenum files...\n";
        if($filenum > 0)
        {
           $file = shift(@files);
           IRC::command("get $file");
        }
     }
  }
  return 1;
}

sub dcc_done
{
   $fcount = @files;
   if($fcount > 0)
   {
      $file = shift(@files);
      IRC::command("get $file");
   }
   else
   {
      IRC::print("done\n");
   }
   return 1;
}

sub fserv_continue
{
   $thecount = @files;
   if($thecount > 0)
   {
      $file = shift(@files);
      IRC::command("get $file");
   }
   return 1;
}