• Welcome to Valhalla Legends Archive.
 

Finished PHP Socket for Battle.net!

Started by Don Cullen, March 29, 2004, 02:36 AM

Previous topic - Next topic

AC_Drkan

Quote from: Forged on May 06, 2004, 07:55 AM
Yeah but then it wouldn't be a script on his site now woud it...
True true true
"The Arguments of Today Result in the Wars of Tomorrow" - Quote By Muah.
<@Logan> I spent a minute looking at my own code by accident.
<@Logan> I was thinking "What the hell is this guy doing?"

<kow`> "There are 10 types of people in the world... those who understand binary and those who don't."
<SpaceRain> That's only 2 types of people, kow.
<SpaceRain> STUPID


<[TN]FBMachine> i got kicked out of barnes and noble once for moving all the bibles into the fiction section

God i love Bash.org.

AC_Drkan

So is there any way to keep a socket open?

like if i wanted to run a bot off of a server would their be any way?
"The Arguments of Today Result in the Wars of Tomorrow" - Quote By Muah.
<@Logan> I spent a minute looking at my own code by accident.
<@Logan> I was thinking "What the hell is this guy doing?"

<kow`> "There are 10 types of people in the world... those who understand binary and those who don't."
<SpaceRain> That's only 2 types of people, kow.
<SpaceRain> STUPID


<[TN]FBMachine> i got kicked out of barnes and noble once for moving all the bibles into the fiction section

God i love Bash.org.

eurolan-StorM


<?

/*   
Project   AdminCraft
Module      -
File      /classes/bot.class.php
Aim      Class of the Bot to Battle.Net.
*/

require_once "C:\Program Files\EasyPHP1-7\php\pear\go-pear-bundle\PEAR.php" ;

define ('BNET_DISCONNECTED', 0) ;
define ('BNET_CONNECTED',    1) ;
define ('BNET_LOGGEDIN',     2) ;

class Bot {
   var $server ;
   var $port ;
   var $flood ;

   
   var $fp ;
   
   var $sbuff ;
   var $rbuff ;
   var $hbuff ;
   
   var $status ;
   
   var $historySize ;
   var $handlers ;
   
   function Bot (
      $server,
      $port,
      $flood = 5
      ) {
      
      $this->server = $server ;
      $this->port = $port ;
      $this->flood = $flood ;
      
      $this->sbuff = $this->rbuff = $this->hbuff = array() ;
      $this->status = BNET_DISCONNECTED ;
      $this->historySize = 30 ;
      
      $this->handlers = array() ;
   
   }

   function setHistory($size) {
      $this->historySize = $size ;
   }
   
   function connect() {
      $fp = fsockopen($this->server, $this->port, $errno, $errstr, 30);
      if(!$fp) {
         echo "$errstr, $errno" ;
      }
      socket_set_blocking($fp, FALSE) ;
      $this->fp = $fp ;
      $this->status = BNET_CONNECTED ;
   }
   
   function disconnect() {
      echo "Fermeture en cours...\n" ;
      fclose($this->fp) ;
      exit ();
      $this->status = BNET_DISCONNECTED ;
   }
   
   function login($login, $pass = false) {
      $l = chr(3).chr(4).$login."\r\n" ;
      if ($pass) $l .=  $pass."\r\n" ;
      $this->send($l) ;
      
      $GLOBALS['_loggedin'] = $this->status == BNET_LOGGEDIN ;
      while ($this->status == BNET_CONNECTED) {
         $this->register('NAME', '_login_handler') ;
         $this->idle() ;
         sleep(1) ;
         if ($GLOBALS['_loggedin']) $this->status = BNET_LOGGEDIN ;
      }
      if ($this->status == BNET_DISCONNECTED) {
         echo 'Connexion perdue au login' ;
      }
      $this->status = BNET_LOGGEDIN ;
   }

   
   function idle() {
      $this->_commit() ;
      $this->_read() ;
      while ($this->rQueueCount()) {
         $data = $this->get() ;
         if (trim($data)) {
            list($actionNo, $actionName, $text) = explode(' ', $data, 3) ;
            if (array_key_exists($actionName, $this->handlers)) {
               $func = $this->handlers[$actionName] ;
               $func($actionNo, $text) ;
            }
         }
      }
   }
   
   function tempo() {
      static $last = 0 ;
      $now = mktime() ;
      if ($now-$last > $this->flood) {
         $last = $now ;
         return true ;
      }
      
      return FALSE ;
   }
   
   function _commit() {
        if ($this->tempo() and $this->sQueueCount()) {
         $data = array_shift($this->sbuff) ;
         fputs($this->fp, $data) ;
         $this->_historize('> '.$data) ;
      }
   }
   
   function send($data) {
      array_push($this->sbuff, $data."\r\n") ;
      $this->idle();
      echo $this->getHistory('/ERROR|WHISPER/'); // Pour le débugage : echo $bot->getHistory('/ERROR|WHISPER|\>/') ;
      sleep(1);

   }
   
   function _read() {
      while (!feof($this->fp) and $data = fgets($this->fp, 512)) {
         array_push($this->rbuff, $data) ;
         $this->_historize('< '.$data) ;
      }
      if (feof($this->fp)) {
         echo 'Connexion perdue' ;
         $this->status = "BNET_DISCONNECTED" ;
      }
      
   }
   
   function sQueueCount() {
      return count($this->sbuff) ;
   }
   function rQueueCount() {
      return count($this->rbuff) ;
   }
   
   function get() {
      if (!$this->rQueueCount()) echo 'File vide' ;
      return array_shift($this->rbuff) ;
   }
   
   function _historize($data) {
      array_push($this->hbuff, $data) ;
      if (count($this->hbuff) > $this->historySize) $void = array_shift($this->hbuff) ;
   }
   
   function getHistory($filter = '/.*/') {
      $hist = '' ;
      while ($data = array_shift($this->hbuff)) {
              if (preg_match($filter, $data)) $hist .= $data ;
         }
      return $hist ;
   }
   
   function register($type, $function) {
      $this->handlers[$type] = $function ;
   }
   
   function getStatus() {
      return $this->status ;
   }
   
   function whisp($aka, $message) {
   $this->send("/w $aka $message") ;
   }
   
   function ann($aka, $message) {
   $this->send("/ann $aka $message") ;
   }
   
   function ping($server) {
   $server = $server;
      if (eregi('windows', $_SERVER["HTTP_USER_AGENT"])) {
      $arg = '-n 1';
      }
      else {
      $arg = '-c 1';
      }
   $list = exec('ping '.$server.' '.$arg);
   $ping = explode (' ',$list);
      foreach ($ping as $valeur) {
         if (eregi('ms', $valeur)) {
         static $i = 1;
         ++$i;      
            if ($i == 3) {
            echo "Votre ping est de $valeur sur le serveur\n";
            }
         }
      }
   }
}
   function _login_handler($no, $txt) {
   $GLOBALS['_loggedin'] = true ;
   }

?>

Don Cullen

mmmm that scripts superior to mine. 0.o
Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

eurolan-StorM

#19
I create other class with this to have a powerfull bot : many commands, database gestion (xml or mysql), tournament management.
Do you have an idea how to create a bot how can log himself  as warcraft client ?

Don Cullen

ummm if ur using the chat protocol for the script, then i'd say no way really- because when you login, you're set up as a chat user, meaning if you wanted to reterive ur own stats, ud have to specify what client.

However if you truly want to login as a warcraft client using PHP, that's a tad bit complicated because then it'd be a binary bot- it'd involve the use of hashes, check revision, packet handling, and et centra, which would be a toughie if done via PHP.

But then don't take my word for it, since I've not succeeded at coding a binary bot yet in VB. :-P
Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

eurolan-StorM

Yes it will be complicated to re write hash for php :-/

tA-Kane

It's cases like this which BNLS because extremely useful. It decreases coding time as well as execution time (being that PHP is a script, all of the hashing functions... CheckRevision in particular, will run very slowly).
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com