Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL);
- set_time_limit(0);
- date_default_timezone_set('America/Chicago');
- $bot = new XatBot();
- while (true) {
- try {
- if ($bot->read() == 'DIED') {
- $bot->disconnect();
- $bot->join();
- }
- } catch (Exception $e) {
- die($e);
- }
- }
- class XatBot
- {
- public $BotRegname = 'The bot\'s regname';
- public $BotID = 'The bot\'s id';
- public $BotPassword = '$The password you get from login';
- public $roomID = 'The room id';
- public $soc;
- public $packet;
- public $users;
- public $done = false;
- public function loadSettings()
- {
- $this->botInfo = array(
- 'name' => 'Bot',
- 'avatar' => 1,
- 'home' => '',
- );
- }
- public function disconnect()
- {
- if (isset($this->soc) && $this->soc == true) {
- socket_close($this->soc);
- }
- $this->soc = false;
- }
- public $ip = 'fwdelb02-53956973.us-east-1.elb.amazonaws.com';
- public function connect($ip = 'xat.com', $port = 10008)
- {
- if (empty($this->BotRegname) || empty($this->BotPassword)) {
- die("You must enter your bot's username and password.\n");
- }
- $error = false;
- do {
- try {
- $this->soc = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- socket_set_option($this->soc, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1000, 'usec' => 0));
- if (!$this->soc) {
- die(socket_strerror(socket_last_error($this->soc)));
- }
- if (!socket_connect($this->soc, $this->ip, $port)) {
- echo "Connecting to ['".$this->ip."':'".$port."'] -> Failed to connect, reconnecting.\n";
- $this->connect($this->ip, $port);
- $this->reconnected = true;
- }
- echo "Connecting to ['".$this->ip."':'".$port."'] -> Connected\n";
- } catch (Exception $e) {
- $error = true;
- }
- } while ($error);
- }
- public function login($user, $pass)
- {
- $p = fsockopen($this->ip, 10000, $e, $e, 1);
- stream_set_timeout($p, 2);
- $packet = '<y r="8" v="0" u="'.$this->BotID.'" />';
- fwrite($p, $packet.chr(0));
- $x = trim(fread($p, 1024));
- echo "[SEND] $packet\n[RECV] $x\n";
- $packet = '<v p="'.$pass.'" n="'.$user.'" />';
- fwrite($p, $packet.chr(0));
- $x = trim(fread($p, 1024));
- echo "[SEND] $packet\n";
- $this->parse($x);
- $this->loginInfo = $this->packet['v'];
- if (count($this->loginInfo) <= 4) {
- return false;
- }
- return $this->loginInfo;
- }
- public function parseU($id)
- {
- if (strpos($id, '_') > -1) {
- $e = explode('_', $id);
- }
- $u = (strpos($id, '_') > -1) ? $e[0] : $id;
- return $u;
- }
- public function join($room = false)
- {
- if ($this->soc == false) {
- $this->connect();
- $this->loggedIn = $this->login($this->BotRegname, $this->BotPassword);
- }
- $this->loadSettings();
- unset($this->packet['y']);
- $this->send('<y r="'.$this->roomID.'" m="1" v="0" u="'.$this->BotID.'" />');
- while (!@is_array(@$this->packet['y'])) {
- $this->read();
- }
- if ($this->loggedIn != false) {
- $i = $this->loginInfo;
- $this->userID = $i['i'];
- $j2 = '';
- $j2 .= 'cb="'.time().'" ';
- $j2 .= 'q="1" ';
- $j2 .= 'y="'.$this->packet['y']['i'].'" ';
- $j2 .= 'k="'.$i['k1'].'" ';
- $j2 .= 'k3="'.$i['k3'].'" ';
- if (isset($i['d1'])) {
- $j2 .= 'd1="'.$i['d1'].'" ';
- } else {
- $i['di'] = '';
- }
- $j2 .= 'p="0" ';
- $j2 .= 'c="'.$this->roomID.'" ';
- $j2 .= 'r="130007525" ';
- $j2 .= 'f="'.'2" ';
- $j2 .= 'u="'.$i['i'].'" ';
- for ($c = 0;$c < 15;++$c) {
- if ($c == 1) {
- continue;
- } elseif (isset($i['d'.$c])) {
- $j2 .= 'd'.$c.'="'.$i['d'.$c].'" ';
- }
- }
- if (isset($i['sn'])) {
- $j2 .= 'sn="'.$i['sn'].'" ';
- }
- if (isset($i['dx'])) {
- $j2 .= 'dx="'.$i['dx'].'" ';
- } else {
- $i['dx'] = '';
- }
- $j2 .= 'dt="'.$i['dt'].'" ';
- $j2 .= 'N="'.$i['n'].'" ';
- $this->botInfo['name'] = str_replace('"', '\'\'', $this->botInfo['name']);
- $this->botInfo['avatar'] = str_replace('"', '\'\'', $this->botInfo['avatar']);
- $j2 .= 'n="'.$this->botInfo['name'].'" ';
- $j2 .= 'a="'.$this->botInfo['avatar'].'" ';
- $j2 .= 'h="'.$this->botInfo['home'].'" ';
- $j2 .= 'v="0" ';
- $j2 = trim($j2);
- $j2 = '<j2 '.$j2.' />';
- $this->send($j2);
- } else {
- die('Could not login.');
- }
- }
- public function handle($type, $msg)
- {
- switch ($type) {
- case 'u':
- $this->users[$this->parseU($msg['u'])] = $msg;
- break;
- case 'q':
- $this->connect($this->packet['q']['d'], $this->packet['q']['p']);
- $this->join();
- break;
- case 'done':
- $this->done = true;
- if (isset($this->reconnected) && $this->reconnected == true) {
- echo 'Recovered';
- $this->reconnected = false;
- }
- break;
- case 'm':
- case 'p':
- if (isset($msg['i']) && $msg['i'] > 0) {
- break;
- }
- if (!$this->done) {
- break;
- }
- $mess = $message = explode(' ', $msg['t']);
- unset($mess[0]);
- $mess = implode(' ', $mess);
- if (isset($messsage[0][0]) && $message[0][0] != '!') {
- break;
- }
- switch (substr($message[0], 1)) {
- case 'say':
- $this->sendMessage($mess);
- break;
- }
- break;
- case 'z':
- if (substr($msg['t'], 0, 2) == '/l') {
- $this->send('<z d="'.$this->parseU($msg['u']).'" u="'.$this->BotID.'" t="/a_NF" />');
- }
- break;
- }
- }
- public function read($parse = true, $handle = true)
- {
- $res = '';
- try {
- $res = @rtrim(socket_read($this->soc, 2048));
- $res = str_replace('', '', $res);
- } catch (Exception $e) {
- return 'DIED';
- }
- if (!$res) {
- return 'DIED';
- }
- if ($res[strlen($res) - 1] != '>') {
- $res .= $this->read(true);
- }
- if ($parse) {
- $this->parse($res, $handle);
- }
- return $res;
- }
- public function send($data)
- {
- if ($data[strlen($data) - 1] != chr(0)) {
- $data .= chr(0);
- }
- echo '[SEND] '.$data."\n";
- return socket_write($this->soc, $data, strlen($data));
- }
- public function parse($packet, $handle = true)
- {
- $packet = explode('/>', $packet);
- $this->packers = array();
- foreach ((Array) $packet as $p) {
- $p = trim($p);
- $p = str_replace('', '', $p);
- if (!empty($p)) {
- $last3 = substr(str_replace(chr(0), '', $p), (strlen($p) - 3));
- if ($last3 != ' />') {
- $p .= ' />';
- }
- echo '[RECV] '.html_entity_decode($p)."\n";
- $this->packers[] = $p;
- }
- if (strlen($p) < 5) {
- return;
- }
- if ($p{0} != '<') {
- $this->packers[count($this->packers) - 2] = $this->packers[count($this->packers) - 2].' ';
- $x = strrev($this->packers[count($this->packers) - 2]);
- if ($x{0} == '"') {
- $p = trim($this->packers[count($this->packers) - 2]).''.$p.' ';
- } else {
- $p = trim($this->packers[count($this->packers) - 2]).''.$p;
- }
- }
- $p = str_replace('/>', '', $p);
- $p = $p.'/>';
- $pack = $this->parse2($p);
- $type = $pack['type'];
- foreach ($pack as $k => $v) {
- $pack[$k] = $v;
- }
- $this->packet[$type] = $pack;
- if ($handle) {
- $this->handle($type, $pack);
- } else {
- return $pack;
- }
- }
- return $pack;
- }
- public function xml($xml)
- {
- $array = json_decode(json_encode($xml), true);
- foreach ((Array) @array_slice($array, 0) as $key => $value) {
- if (is_array(@$value)) {
- $array[$key] = $this->xml($value);
- }
- }
- return $array;
- }
- public function parse2($p)
- {
- global $base;
- $p1 = $p;
- $p = trim($p, '<');
- $p1 = trim($p, '<');
- $p1 = '<'.$p1;
- $p = explode('/>', $p);
- $p = $p[0];
- $x = explode(' ', $p);
- $type = $x[0];
- $packet['type'] = $type;
- $xml = @simplexml_load_string($p1);
- $x = $this->xml($xml);
- $attributes = @$x['@attributes'];
- foreach ((Array) $attributes as $type => $var) {
- $packet[$type] = $var;
- }
- return $packet;
- }
- public function sendMessage($msg)
- {
- if (empty($msg)) {
- return false;
- }
- $this->send('<m t="'.$msg.'" u="'.$this->loginInfo['i'].'" />');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement