Advertisement
austinh115

[PHP] XatBot [OLD, might still work? lmao idk]

Mar 11th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.05 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4. set_time_limit(0);
  5. date_default_timezone_set('America/Chicago');
  6.  
  7. $bot = new XatBot();
  8. while (true) {
  9.     try {
  10.         if ($bot->read() == 'DIED') {
  11.             $bot->disconnect();
  12.             $bot->join();
  13.         }
  14.     } catch (Exception $e) {
  15.         die($e);
  16.     }
  17. }
  18. class XatBot
  19. {
  20.     public $BotRegname = 'The bot\'s regname';
  21.     public $BotID = 'The bot\'s id';
  22.     public $BotPassword = '$The password you get from login';
  23.     public $roomID = 'The room id';
  24.     public $soc;
  25.     public $packet;
  26.     public $users;
  27.     public $done = false;
  28.  
  29.     public function loadSettings()
  30.     {
  31.         $this->botInfo = array(
  32.             'name' => 'Bot',
  33.             'avatar' => 1,
  34.             'home' => '',
  35.         );
  36.     }
  37.     public function disconnect()
  38.     {
  39.         if (isset($this->soc) && $this->soc == true) {
  40.             socket_close($this->soc);
  41.         }
  42.         $this->soc = false;
  43.     }
  44.     public $ip = 'fwdelb02-53956973.us-east-1.elb.amazonaws.com';
  45.     public function connect($ip = 'xat.com', $port = 10008)
  46.     {
  47.         if (empty($this->BotRegname) || empty($this->BotPassword)) {
  48.             die("You must enter your bot's username and password.\n");
  49.         }
  50.         $error = false;
  51.         do {
  52.             try {
  53.                 $this->soc = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  54.                 socket_set_option($this->soc, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1000, 'usec' => 0));
  55.                 if (!$this->soc) {
  56.                     die(socket_strerror(socket_last_error($this->soc)));
  57.                 }
  58.                 if (!socket_connect($this->soc, $this->ip, $port)) {
  59.                     echo "Connecting to ['".$this->ip."':'".$port."'] -> Failed to connect, reconnecting.\n";
  60.                     $this->connect($this->ip, $port);
  61.                     $this->reconnected = true;
  62.                 }
  63.                 echo "Connecting to ['".$this->ip."':'".$port."'] -> Connected\n";
  64.             } catch (Exception $e) {
  65.                 $error = true;
  66.             }
  67.         } while ($error);
  68.     }
  69.     public function login($user, $pass)
  70.     {
  71.         $p = fsockopen($this->ip, 10000, $e, $e, 1);
  72.         stream_set_timeout($p, 2);
  73.         $packet = '<y r="8" v="0" u="'.$this->BotID.'" />';
  74.         fwrite($p, $packet.chr(0));
  75.         $x = trim(fread($p, 1024));
  76.         echo "[SEND] $packet\n[RECV] $x\n";
  77.         $packet = '<v p="'.$pass.'" n="'.$user.'" />';
  78.         fwrite($p, $packet.chr(0));
  79.         $x = trim(fread($p, 1024));
  80.         echo "[SEND] $packet\n";
  81.         $this->parse($x);
  82.         $this->loginInfo = $this->packet['v'];
  83.         if (count($this->loginInfo) <= 4) {
  84.             return false;
  85.         }
  86.  
  87.         return $this->loginInfo;
  88.     }
  89.     public function parseU($id)
  90.     {
  91.         if (strpos($id, '_') > -1) {
  92.             $e = explode('_', $id);
  93.         }
  94.         $u = (strpos($id, '_') > -1) ? $e[0] : $id;
  95.  
  96.         return $u;
  97.     }
  98.     public function join($room = false)
  99.     {
  100.         if ($this->soc == false) {
  101.             $this->connect();
  102.             $this->loggedIn = $this->login($this->BotRegname, $this->BotPassword);
  103.         }
  104.         $this->loadSettings();
  105.         unset($this->packet['y']);
  106.         $this->send('<y r="'.$this->roomID.'" m="1" v="0" u="'.$this->BotID.'" />');
  107.         while (!@is_array(@$this->packet['y'])) {
  108.             $this->read();
  109.         }
  110.         if ($this->loggedIn != false) {
  111.             $i = $this->loginInfo;
  112.             $this->userID = $i['i'];
  113.             $j2 = '';
  114.             $j2 .= 'cb="'.time().'" ';
  115.             $j2 .= 'q="1" ';
  116.             $j2 .= 'y="'.$this->packet['y']['i'].'" ';
  117.             $j2 .= 'k="'.$i['k1'].'" ';
  118.             $j2 .= 'k3="'.$i['k3'].'" ';
  119.             if (isset($i['d1'])) {
  120.                 $j2 .= 'd1="'.$i['d1'].'" ';
  121.             } else {
  122.                 $i['di'] = '';
  123.             }
  124.             $j2 .= 'p="0" ';
  125.             $j2 .= 'c="'.$this->roomID.'" ';
  126.             $j2 .= 'r="130007525" ';
  127.             $j2 .= 'f="'.'2" ';
  128.             $j2 .= 'u="'.$i['i'].'" ';
  129.             for ($c = 0;$c < 15;++$c) {
  130.                 if ($c == 1) {
  131.                     continue;
  132.                 } elseif (isset($i['d'.$c])) {
  133.                     $j2 .= 'd'.$c.'="'.$i['d'.$c].'" ';
  134.                 }
  135.             }
  136.             if (isset($i['sn'])) {
  137.                 $j2 .= 'sn="'.$i['sn'].'" ';
  138.             }
  139.             if (isset($i['dx'])) {
  140.                 $j2 .= 'dx="'.$i['dx'].'" ';
  141.             } else {
  142.                 $i['dx'] = '';
  143.             }
  144.             $j2 .= 'dt="'.$i['dt'].'" ';
  145.             $j2 .= 'N="'.$i['n'].'" ';
  146.             $this->botInfo['name'] = str_replace('"', '\'\'', $this->botInfo['name']);
  147.             $this->botInfo['avatar'] = str_replace('"', '\'\'', $this->botInfo['avatar']);
  148.             $j2 .= 'n="'.$this->botInfo['name'].'" ';
  149.             $j2 .= 'a="'.$this->botInfo['avatar'].'" ';
  150.             $j2 .= 'h="'.$this->botInfo['home'].'" ';
  151.             $j2 .= 'v="0" ';
  152.             $j2 = trim($j2);
  153.             $j2 = '<j2 '.$j2.' />';
  154.             $this->send($j2);
  155.         } else {
  156.             die('Could not login.');
  157.         }
  158.     }
  159.     public function handle($type, $msg)
  160.     {
  161.         switch ($type) {
  162.             case 'u':
  163.                 $this->users[$this->parseU($msg['u'])] = $msg;
  164.             break;
  165.             case 'q':
  166.                 $this->connect($this->packet['q']['d'], $this->packet['q']['p']);
  167.                 $this->join();
  168.             break;
  169.             case 'done':
  170.                 $this->done = true;
  171.                 if (isset($this->reconnected) && $this->reconnected == true) {
  172.                     echo 'Recovered';
  173.                     $this->reconnected = false;
  174.                 }
  175.             break;
  176.             case 'm':
  177.             case 'p':
  178.                 if (isset($msg['i']) && $msg['i'] > 0) {
  179.                     break;
  180.                 }
  181.                 if (!$this->done) {
  182.                     break;
  183.                 }
  184.                 $mess = $message = explode(' ', $msg['t']);
  185.                 unset($mess[0]);
  186.                 $mess = implode(' ', $mess);
  187.                 if (isset($messsage[0][0]) && $message[0][0] != '!') {
  188.                     break;
  189.                 }
  190.                 switch (substr($message[0], 1)) {
  191.                     case 'say':
  192.                         $this->sendMessage($mess);
  193.                     break;
  194.                 }
  195.             break;
  196.             case 'z':
  197.                 if (substr($msg['t'], 0, 2) == '/l') {
  198.                     $this->send('<z d="'.$this->parseU($msg['u']).'" u="'.$this->BotID.'" t="/a_NF" />');
  199.                 }
  200.                 break;
  201.  
  202.         }
  203.     }
  204.     public function read($parse = true, $handle = true)
  205.     {
  206.         $res = '';
  207.         try {
  208.             $res = @rtrim(socket_read($this->soc, 2048));
  209.             $res = str_replace('', '', $res);
  210.         } catch (Exception $e) {
  211.             return 'DIED';
  212.         }
  213.         if (!$res) {
  214.             return 'DIED';
  215.         }
  216.         if ($res[strlen($res) - 1] != '>') {
  217.             $res .= $this->read(true);
  218.         }
  219.         if ($parse) {
  220.             $this->parse($res, $handle);
  221.         }
  222.  
  223.         return $res;
  224.     }
  225.     public function send($data)
  226.     {
  227.         if ($data[strlen($data) - 1] != chr(0)) {
  228.             $data .= chr(0);
  229.         }
  230.         echo '[SEND] '.$data."\n";
  231.  
  232.         return socket_write($this->soc, $data, strlen($data));
  233.     }
  234.     public function parse($packet, $handle = true)
  235.     {
  236.         $packet = explode('/>', $packet);
  237.         $this->packers = array();
  238.         foreach ((Array) $packet as $p) {
  239.             $p = trim($p);
  240.             $p = str_replace('', '', $p);
  241.             if (!empty($p)) {
  242.                 $last3 = substr(str_replace(chr(0), '', $p), (strlen($p) - 3));
  243.                 if ($last3 != ' />') {
  244.                     $p .= ' />';
  245.                 }
  246.                 echo '[RECV] '.html_entity_decode($p)."\n";
  247.                 $this->packers[] = $p;
  248.             }
  249.             if (strlen($p) < 5) {
  250.                 return;
  251.             }
  252.             if ($p{0} != '<') {
  253.                 $this->packers[count($this->packers) - 2] = $this->packers[count($this->packers) - 2].' ';
  254.                 $x = strrev($this->packers[count($this->packers) - 2]);
  255.                 if ($x{0} == '"') {
  256.                     $p = trim($this->packers[count($this->packers) - 2]).''.$p.' ';
  257.                 } else {
  258.                     $p = trim($this->packers[count($this->packers) - 2]).''.$p;
  259.                 }
  260.             }
  261.             $p = str_replace('/>', '', $p);
  262.             $p = $p.'/>';
  263.             $pack = $this->parse2($p);
  264.             $type = $pack['type'];
  265.             foreach ($pack as $k => $v) {
  266.                 $pack[$k] = $v;
  267.             }
  268.             $this->packet[$type] = $pack;
  269.             if ($handle) {
  270.                 $this->handle($type, $pack);
  271.             } else {
  272.                 return $pack;
  273.             }
  274.         }
  275.  
  276.         return $pack;
  277.     }
  278.  
  279.     public function xml($xml)
  280.     {
  281.         $array = json_decode(json_encode($xml), true);
  282.         foreach ((Array) @array_slice($array, 0) as $key => $value) {
  283.             if (is_array(@$value)) {
  284.                 $array[$key] = $this->xml($value);
  285.             }
  286.         }
  287.  
  288.         return $array;
  289.     }
  290.     public function parse2($p)
  291.     {
  292.         global $base;
  293.         $p1 = $p;
  294.         $p = trim($p, '<');
  295.         $p1 = trim($p, '<');
  296.         $p1 = '<'.$p1;
  297.         $p = explode('/>', $p);
  298.         $p = $p[0];
  299.         $x = explode(' ', $p);
  300.         $type = $x[0];
  301.         $packet['type'] = $type;
  302.         $xml = @simplexml_load_string($p1);
  303.         $x = $this->xml($xml);
  304.         $attributes = @$x['@attributes'];
  305.         foreach ((Array) $attributes as $type => $var) {
  306.             $packet[$type] = $var;
  307.         }
  308.  
  309.         return $packet;
  310.     }
  311.     public function sendMessage($msg)
  312.     {
  313.         if (empty($msg)) {
  314.             return false;
  315.         }
  316.         $this->send('<m t="'.$msg.'" u="'.$this->loginInfo['i'].'" />');
  317.     }
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement