<?php

include('../global.php');

class IRCBot
{
  var $OwnerName = 'Chronosův';
  var $Server = "game.zdechov.net";
  var $Port = 6667;
  var $Channel = "#zdechov";
  var $Nick = "History";
  var $JokeFileName = "jokes.txt";
  var $File;

  function __construct() 
  {
    $this->Jokes = file($this->JokeFileName);
    $this->File = fsockopen($this->Server, $this->Port, $ErrNumber, $ErrString, 5);
    $this->Connect($this->File, $this->Nick, $this->Channel, 0);
  }

  function Connect()
  {
    $this->Command('USER USER '.$this->Nick." # # :".$this->Nick);
    $this->Command('NICK '.$this->Nick);
    //sleep($JoinWaitTime);
    $this->Command('JOIN '.$this->Channel);
  }

  function Say($Message, $Recipient = '')
  {
    if($Recipient == '') $Recipient = $this->Channel;
    $this->Command(': PRIVMSG '.$Recipient.' :'.$Message);
  }

  function Command($Text)
  {
    fwrite($this->File, $Text."\n");
  }

  function Run()
  {
    global $Database;

    while(!fwrite($this->File, ''))
    {
      $this->Command('JOIN '.$this->Channel);

      $Line = fgets($this->File);
      echo($Line);

      $LineParts = explode(' ', $Line);
      if(count($LineParts) > 0)
      {
        if($LineParts[0] == 'PING')
        {
          $this->Command('PONG '.trim($LineParts[1])."\n");
          echo('-PONG '.trim($LineParts[1])."\n");
        }

        if(count($LineParts) > 1)
        {
          if((trim($LineParts[1]) == 'INVITE') && (substr(trim($LineParts[3]), 0, 2) == ':#'))
          {
            $CurrentChannel = substr(trim($LineParts[3]), 1);
            fwrite($this->File, 'JOIN '.$this->Channel."\n");
            $this->Say('Hello');
            echo('-JOINED '.$this->Channel."\n");
          }
        }
      }

      $Commands = array(substr($Line, 0, strpos($Line, ':') + 1));
      $Line = substr($Line, strlen($Commands[0]));
      $Commands[1] = substr($Line, 0, strpos($Line, ':') + 1);
      $Commands[2] = substr($Line, strlen($Commands[1]));      
      //print_r($Commands);

      // Log messages to database
      if(strpos($Line, 'PRIVMSG') !== false)
      {
        $Text = addslashes($Commands[2]);
        $LineParts2 = explode(' ', $Commands[1]);
        $LineParts3 = explode('@', $LineParts2[0]);
        $Host = addslashes($LineParts3[1]);
        $LineParts4 = explode('!', $LineParts3[0]);
        $Nick = addslashes($LineParts4[0]);
        $Database->insert('ChatHistory', array('Nick' => $Nick, 'Text' => $Text, 'Host' => $Host, 'Time' => 'NOW()', 'RoomName' => 'Všichni'));
      }

      
      explode(':', $Line);
      foreach($Commands as $Index => $Item)
        $Commands[$Index] = trim($Item);

      if(count($Commands) >= 2)
      {
        $Command = $Commands[2];

        // Jméno - Pošle vizitku
        if($Command == $this->Nick)
        {
          $this->Say('Ahoj lidi, ja jsem '.$this->OwnerName.' bot. Random#: '.rand(0, 10));
          echo('-VISITCARD'."\n");
        }

        // hhelp - vypise tuto napovedu
        if($Command == 'hhelp')
        {
          $this->Say('Ja jsem Harvester.');
          /*
          irc_say( $sfp, "Bot - Posle vizitku", $nick, $channel );
          irc_say( $sfp, "hhelp - vypise tuto napovedu", $nick, $channel );
          irc_say( $sfp, "hsay:Message - Posle zpravu", $nick, $channel );
          irc_say( $sfp, "hpsay:to:Message - Posle soukromou zpravu kanalu nebo osobe", $nick, $channel );
          irc_say( $sfp, "hdo:Command - Posle serveru prikaz", $nick, $channel );
          irc_say( $sfp, "hmove:Channel - Pripoji do kanalu / Zmeni aktivni kanal", $nick, $channel );
          irc_say( $sfp, "/invite Harvester #channel - Pozve a pripoji bota do kanalu", $nick, $channel );
          irc_say( $sfp, "hpart:Channel - Odpoji se z kanalu", $nick, $channel );
          irc_say( $sfp, "hjoke - Posle \"nahodny\" vtip", $nick, $channel );
          */
          echo("-HELPED"."\n");
        }

        // hsay:Message - Posle zpravu
        if($Command == 'hsay')
        {
          $this->Say($Commands[3]);
          echo("-SAID: ".$Commands[3]."\n");
        }

        // hpsay:to:Message - Posle soukromou zpravu kanalu nebo osobe
        if($Command == 'hpsay')
        {
          $this->Say($Commands[4], $Commands[3]);
          echo("-SAID: ".$Commands[4]." -- To: ".$Commands[3]."\n");
        }

        // hcol:to:Message - Posle kolizni zpravu kanalu nebo osobe
        if($Command == 'hcol')
        {
          $this->Say($Commands[4], $Commands[3]);
          echo("-COLIDED: ".$Commands[4]." To: ".$Commands[3]."\n");
        }

        // hdo:Command - Posle serveru prikaz
        if($Command == 'hdo')
        {
          $hdo = explode('hdo:', $Line);
          $this->Command(trim($hdo[1]));
          echo("-DONE: ".trim($hdo[1])."\n");
        }

        // hpart:Channel - Odpoji se z kanalu
        if($Command == 'hpart')
        {
          $hdo = explode('hpart:', $Line);
          if(trim($hdo[1]) != trim($this->Channel))
          {
            $this->Command('PART :'.trim($hdo[1])."\n");
            echo("-PARTED: ".trim($hdo[1])."\n");
          }
        }

        // hmove:Channel - Zmeni aktivni kanal
        if($Command == 'hmove')
        {
          $hdo = explode("hmove:", $Line);
          if(trim($hdo[1]) != trim($$this->Cannel))
          {
            $this->Channel = trim($hdo[1]);
            $this->Command('JOIN '.$this->Channel."\n");
            $this->Say('Hi, im here...');
            echo("-ACTIVATED: ".trim($hdo[1])."\n");
          }
        }

        // htime - udaje o casu
        if($Command == 'htime')
        {
          $Date = implode("-", getdate(time()));
          $this->Say(trim($Date));
          echo("-TIMED"."\n");
        }

        // hjoke - Posle nahodny vtip
        if($Command == 'hjoke') 
        {
          $Joke = ($this->Jokes[rand(0, (sizeof($this->Jokes) - 1))]);
          $this->Say(trim($Joke));
          echo("-JOKED"."\n");
        }
      }
    }
    echo("-Connection lost.\n");
  }

}

$IRCBot = new IRCBot();
$IRCBot->Run();

?>