<?php


/**
 * BnetBot is a simple telnet based bot
 *
 * @author Nickolas Kost <[email protected]>
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.0.1
 */
class BnetBot {



 public $sock;

 public $errstr;
 public $errno;

 public $login;
 public $pass;

 public $channel;

 public $host;
 public $port;

 public $bufferStack;
 public $writeStack;
 public $bufferSize=256;

 public $_active;

 const NL="\\\\r\\\\n";

 function __construct($server,$port,$username,$pass,$channel) {
  $this->host=strval(gethostbyname(trim($server)));
  $this->port=intval($port);

  $this->login=trim($username);
  $this->pass=$pass;
    $this->channel=$channel;
 }

 function __destruct()
 {
  $this->send("/exit");
  $this->close();
 }

 function connect()
 {
  $this->open();
  $this->init();

 }

 function init()
 {

 }

  /**
   * Init Socket
   */
 function open()
 {
    $this->sock=@fsockopen&#40;$this->host, $this->port, $this->errno, $this->errstr,2&#41;;
   if(!$this->sock)
    {
      die("rn[BOT][ERROR] Socket Error: $this->errstr");
    }
    $this->_active=true;
    sleep(1);
    $this->send(self::NL);
    sleep(1);
    socket_set_timeout($this->sock,10,0);
    print "rn[BOT] INIT CONNECTION";
    $this->initConnection();

 }

 /*
  * Close Socket
  */
 function close()
 {
    fclose($this->sock);
    $this->_active=false;
 }

 /**
  * Send auth info into Server
  */
 function initConnection(){

  $login_flag=$pass_flag=$chan_flag=false;


  while($line=$this->read())
  {
   if(strstr($line,"Username:")&&!$login_flag)
   {
    //sleep(1);
    $login_flag=true;
    $this->send($this->login);
    $this->send(self::NL);
    continue;
   }
   elseif(strstr($line,"Password:")&&!$pass_flag)
   {
    //sleep(1);
   $pass_flag=true;
   $this->send($this->pass);
   $this->send(self::NL);
   $this->send(self::NL);
   continue;
   }
   elseif(strstr($line,"Login failed."))
   {
    $this->_active=false;
    $this->onDisconnect();
   }
   elseif(strstr($line,'['.$this->login.' is here]')&&!$chan_flag)
   {
   $chan_flag=true;
   $this->send("/channel $this->channel");
   $this->send(self::NL);
   //sleep(1);
   break;
   }

    }
    return $login_flag&&$pass_flag&&$chan_flag;
 }

 /**
  * Read from socket
  * @return string Buffer
  */
 function read()
 {
  $buffer=fread($this->sock,$this->bufferSize);
  if(!$buffer)
  {
    $this->_active=false;
    $this->onDisconnect();
  }

  $this->onRecieve(strval($buffer));
  return $buffer;
 }

 /**
  * Send Data to server(socket)
  * @param string $data
  */
 function send($data='')
 {
  $this->writeStack.= $data;
  $buffer_len = strlen($this->writeStack);
  $byte_written = fwrite($this->sock, $this->writeStack,256);

  if ($byte_written === false)
  {
   //print "rn[BOT][SEND]ERROR";
    $this->_active = false;
    $this->onDisconnect();
  }elseif($byte_written < $buffer_len)
  {
   //print "rn[BOT][SEND] $data";
    $this->writeStack = substr($this->writeStack, $byte_written);
    $this->write_len = $buffer_len - $byte_written;
  }
  else
  {
    $this->writeStack = '';
    $this->write_len = 0;
  }
 }

 /**
  * Send data as self::send() plus add's rn
  * @see self::send()
  * @param string $data
  */
 function sendLine($data=null){

  return $this->send($data.self::NL);
 }

 /**
  * Deletes a n symbol from end
  * @param string $line
  * @return string
  */
 function preParseLine($line)
 {
  $line=substr($line,0,strlen($line)-1);
  return $line;
 }

  /**
   * Convert char codes into symbols
   * @param string $str
   * @return string
   */
  function convert($str)
  {
    //Parsing Russian Letters
    $sympats = array(
    "/\\320\\271/","/\\320\\260/","/\\320\\261/","/\\320\\262/","/\\320\\263/","/\\320\\264/","/\\320\\265/","/\\320\\221/","/\\320\\266/",
    "/\\320\\267/","/\\320\\270/","/\\320\\271/","/\\320\\272/","/\\320\\273/","/\\320\\274/","/\\320\\275/","/\\320\\276/","/\\320\\277/",
    "/\\321\\200/","/\\321\\201/","/\\321\\202/","/\\321\\203/","/\\321\\204/","/\\321\\205/","/\\321\\206/","/\\321\\207/","/\\321\\210/",
    "/\\321\\211/","/\\321\\212/","/\\321\\213/","/\\321\\214/","/\\321\\215/","/\\321\\216/","/\\321\\217/",

    "/\\320\\220/","/\\320\\221/","/\\320\\222/","/\\320\\223/","/\\320\\224/","/\\320\\225/","/\\320\\201/","/\\320\\226/","/\\320\\227/",
    "/\\320\\230/","/\\320\\231/","/\\320\\232/","/\\320\\233/","/\\320\\234/","/\\320\\235/","/\\320\\236/","/\\320\\237/","/\\320\\240/",
    "/\\320\\241/","/\\320\\242/","/\\320\\243/","/\\320\\244/","/\\320\\245/","/\\320\\246/","/\\320\\247/","/\\320\\250/","/\\320\\251/",
    "/\\320\\252/","/\\320\\253/","/\\320\\254/","/\\320\\255/","/\\320\\256/","/\\320\\257/",
    );

    $alphabet = array(
    "й","a","б","в","г","д","е","ё","ж","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х","ц","ч","ш","щ","ъ","ы","ь","э","ю","я",
      "А","Б","В","Г","Д","Е","Ё","Ж","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","Ч","Ш","Щ","Ъ","Ы","Ь","Э","Ю","Я"
    );
    return preg_replace($sympats,$alphabet,$str);
  }

 /**
  * EVENT HANDLERS
  */

 function onDisconnect()
 {
  $this->_active=true;
  $this->__construct($this->host,$this->port,$this->login,$this->pass,$this->channel);
 }


 function onRecieve($packets)
 {
  print "rn$packets";

 }


 function onChannelPing($nickname)
 {
  $this->sendLine("/me $nickname тут");
 }

 function onEnterChannel($nickname)
 {
 }

 function onLeavesChannel($nickname)
 {
 }

}


$bot=new BnetBot('your server',6112,'telnetbot','yourpass','testchannel');
$bot->connect();


$patOnHere = "|^[(S+)s+iss+here]s+$|ixs";
$patOnJoin = "|^[(S+)s+enters]s+$|ixs";
$patOnLeave = "|^[(S+)s+leaves]s+$|ixs";
$patOnSay = "|^<(S+)>(.*?)$|ixs";
$patOnWhisper = "|^<froms(S+)>(.*?)$|ixs";

$onChannel=array();

while(1)
{
  $line=$bot->read();
  $line=$bot->preParseLine($line);//Delete rn

  $line=$bot->convert($line);//Convers codes into russian letters
  print $line;


  if(preg_match($patOnHere,$line,&$matOnHere))
  {
 // array('whole string','username who is here');
 $onChannel[]=$matOnHere[1];
  }

  //parse On Join Event
  if(preg_match($patOnJoin,$line, &$matOnJoin))
  {
 // array('whole string','username who join');
   $bot->sendLine("/me {$matOnJoin[1]}, enters");
   continue;
  }

  //parse On Leave Event
  if(preg_match($patOnLeave,$line, &$matOnLeave))
  {
 // array('whole string','username who leaves');
   $bot->sendLine("/me {$matOnLeave[1]}, leaves");
   continue;
  }

  //parse On Say Event
  if(preg_match($patOnSay,$line,&$matOnSay))
  {
 // array('whole string','username who say', 'what he say');
   $bot->sendLine("{$matOnSay[1]} say - {$matOnSay[2]}");
 sleep(0.5);
 if($matOnSay[2]==="who") $bot->sendLine(implode(" | ",$onChannel))." <<<"; // try to display all users in channel when anybody say "who"
   continue;
  }

  //parse On Whisper Event
  if(preg_match($patOnWhisper,$line,&$matOnWhisper))
  {
 // array('whole string','username who whisper', 'what he whisper');
   $bot->sendLine("{$matOnWhisper[1]} whisper - {$matOnWhisper[2]}");
   continue;
  }
}
$bot->close();
unset($bot);

Add a code snippet to your website: www.paste.org