3nvisi0n

Xat Bot 3nvisi0n

Jun 15th, 2011 (edited)
17,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.19 KB | None | 0 0
  1. IM GAY
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. <?php
  28.  
  29. /**
  30.  * @author 3nvisi0n
  31.  * @copyright 2011
  32.  *
  33.  * This is just a basic version of my Xat Bot you can build upon to add the feature you want.
  34.  *
  35.  */
  36. set_time_limit(0);//No time out
  37. $bot = new BasicXatBot();
  38. $roomID = 88973610;//Don't know how to get this ID? goto xat.com/ROOM_YOU_WANT_BOT_IN  and View Source(Firefox: Ctrl+U, Internet Explorer: View>Source) Look for "flashvars="id=#########&..." the ID is the number you want
  39. $bot->connect("174.36.242.26","10024"); //Connect to XAT...this IP will change as necessary automatically depending on the room you want to join.
  40. $bot->join($roomID);
  41. while(true){
  42.     if($bot->read()=='DIED') {
  43.         $bot->connect("174.36.242.26","10024");
  44.         $bot->join($roomID);
  45.     }
  46. }
  47.  
  48.  
  49. class BasicXatBot {
  50.     private $soc;                   //Socket for bot
  51.     private $debug = false;          //Used to toggle debugging output.
  52.     private $packet;                //Stores information about the last recieved packet of each kind
  53.     private $userInfo;              //Stores User information
  54.    
  55.     //If you don't know where to get these you should probably not be trying to make your own bot.
  56.     //Go download WPE Pro and check out the packets XAT sends and learn about how XAT works.
  57.     //The UserID and K Value are the source or the 'old falsh' error(if you get it) make sure you get these values from a REGISTERED account
  58.    
  59.     private $userID = "351599155";  //The Bot's UserID
  60.     private $k = "3607438505";      //The Bot's K value
  61.    
  62.     //Bot Account Settings
  63.     private $name = "XBot";         //The display name the bot will use
  64.     private $avatar = -1;           //The Avatar value for the bot, this can be an id number or an image url.
  65.     private $homepage = "";         //The bot's homepage link.
  66.     private $roomID;                //This gets set in code, don't touch it here. just used ->join(ID)
  67.    
  68.    
  69.     /**
  70.      * This is where everything the bot does needs to happen, this is the only default function you should need to edit
  71.      * @param $event The event that just occured(and thus needs to be handled)
  72.      * @param $data Any data relating to this event.
  73.      */
  74.     function handleEvent($event,$data) {
  75.        $info = $this->getUserArray($data['id']);
  76.        switch($event) {
  77.             case 'userJoined':
  78.              /* $data['id']             Has the ID of the user who just joined
  79.                 $data['old']            Sometimes XAT sends packets that are not current(for example when you join a room, old ==true for all users who are already in the room when you join, but still this event is handled as thought they just joined */
  80.                
  81.                 //Do whever you want with users joining here...
  82.                 echo ((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) has just joined.\n";
  83.             break;
  84.             case 'userLeft':
  85.                 /* $data['id']     The ID of the user that just left. */                
  86.                 echo ((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) has just left.\n";
  87.             break;
  88.             case 'privateMessage':
  89.                 /* $data['id']      The ID of the user that just left.
  90.                    $data['message'] The message sent to you as a PM */
  91.                 echo "[PM] ".((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) -> $data[message]\n";
  92.                
  93.                 //Example of a private message command
  94.                 $command = explode(' ',$data['message'],2); //First parse the first word out see if it is a command...
  95.                 //[0] has first word [1] has everything else
  96.                 if($command[0]{0}=='!'){//I am use ! as the character to signify a command, so check if the first character is right.
  97.                     switch($command[0]) {
  98.                         case '!say':
  99.                         case '!speak':
  100.                         case '!talk':
  101.                             $this->sendPrivateMessage($command[1],$data['id']);
  102.                         break;
  103.                         case '!info':
  104.                             $this->sendPrivateMessage('I am a bot ^_^',$data['id']);
  105.                         break;
  106.                     }
  107.                 }
  108.             break;
  109.             case 'privateChat':
  110.                 /* $data['id']      The ID of the user that just left.
  111.                    $data['message'] The message sent to you as a PC */
  112.                 echo "[PC] ".((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) -> $data[message]\n";
  113.                
  114.                 //Example of a private chat command
  115.                 $command = explode(' ',$data['message'],2); //First parse the first word out see if it is a command...
  116.                 //[0] has first word [1] has everything else
  117.                 if($command[0]{0}=='!'){//I am use ! as the character to signify a command, so check if the first character is right.
  118.                     switch($command[0]) {
  119.                         case '!info':
  120.                             $this->sendPrivateChat('I am a bot ^_^',$data['id']);
  121.                         break;
  122.                     }
  123.                 }
  124.             break;
  125.             case 'message':
  126.                 /* $data['id']      The ID of the user
  127.                    $data['old']     See $data['old'] under userJoined
  128.                    $data['message'] The message sent to main chat */
  129.                 echo ((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])."($info[rank]) -> $data[message]\n";
  130.                
  131.                 //How to do main chat commands:
  132.                 if($data['old'])   return; //Old message            
  133.                 $command = explode(' ',trim($data['message']),2); //First parse the first word out see if it is a command...
  134.                 //[0] has first word [1] has everything else
  135.                 if($command[0]{0}=='!'){//I am use ! as the character to signify a command, so check if the first character is right.
  136.                     switch($command[0]) {
  137.                         case '!say':
  138.                         case '!speak':
  139.                         case '!talk':
  140.                             //Multiple cases lead to the same code being run...
  141.                             $this->sendMessage($command[1]);
  142.                         break;
  143.                         case '!slap':
  144.                         case '!slaps':
  145.                         case '!hit':
  146.                             $this->sendMessage(((trim($info['registeredName'])!='')?$info['registeredName']:$info['name'])." slaps $command[1] around a bit with a large trout.");
  147.                         break;
  148.                         case '!avatar'://steal an avatar via !avatar USER_ID
  149.                             $user = $this->getUserArray($command[1]);
  150.                             $this->sendMessage(str_replace('(','( ',$user['avatar']));
  151.                         break;
  152.                         case '!info':
  153.                             $this->sendMessage('I am a bot ^_^',$data['id']);
  154.                         break;
  155.                         case '!yt':
  156.                         case '!youtube':
  157.                         case '!y':
  158.                             if(trim($command[1])=='') $this->sendMessage("Usage: $command[0] SEARCH TERMS HERE");
  159.                             else {
  160.                                 //Yeah I know I don't really need urlencode() i could just replace spaces with + but this is a bit more secure.
  161.                                 $res = $this->get('http://www.youtube.com/results?search_query='.urlencode(preg_replace("/[^A-Za-z0-9 !.,:[]\s\s+]/",'',$command[1])));
  162.                                 for($i=0;$i<3;$i++) {
  163.                                     $msg=$this->getBetween($res, '<h3>', '</h3>');
  164.                                     $res = str_replace('<h3>'.$msg,'</h3>',$res);
  165.                                     $url = "http://www.youtube.com".$this->getBetween($msg,'href="', '"');
  166.                                     $title = @ereg_replace('[^A-Za-z0-9 -()[]<>{}&@]','',$this->getBetween($msg,'title="', '"'));
  167.                                     $this->sendMessage($title." - $url");
  168.                                     sleep(1);//Xat won't let us send more than 1 message per second, it ignores anything faster.
  169.                                 }  
  170.                             }
  171.                         break;
  172.                         case '!g':
  173.                         case '!google':
  174.                             $res = $this->get('http://www.google.com/search?hl=en&source=hp&biw=&bih=&q='.str_replace(' ','+',$command[1]).'&btnG=Google+Search',true);
  175.                             for($i=0;$i<3;$i++) {
  176.                                 $msg=$this->getBetween($res, '<h3 class="r"><a href="', '"');
  177.                                 $res = str_replace('<h3 class="r"><a href="'.$msg,'',$res);
  178.                                 $this->sendMessage($msg);
  179.                                 sleep(1);
  180.                             }
  181.                     }
  182.                 }
  183.             break;
  184.        
  185.        }        
  186.     }
  187.    
  188.    
  189.    
  190.     /* ****************************************************** */
  191.     /* *YOU SHOULD NOT NEED TO EDIT ANYTHING AFTER THIS LINE* */
  192.     /* ****************************************************** */
  193.     /**
  194.      * Connects to a given ip on the given port
  195.      * @param $ip The IP to connect to.
  196.      * @param $port The port on the IP to connect to.
  197.      */
  198.     function connect($ip, $port) {
  199.         if($this->soc!=null) socket_close($this->soc);
  200.         $this->soc = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
  201.         if(!$this->soc) die(socket_strerror(socket_last_error($this->soc)));
  202.         if(!socket_connect($this->soc,$ip,$port)) die("Could not connect.");  
  203.     }
  204.    
  205.     /**
  206.      * Writes the given message to the object socket($this->soc)
  207.      * @param $message The packet to send.
  208.      */
  209.     function send($message) {
  210.         if($this->debug)echo "->>\t$message\n";
  211.         socket_write($this->soc, $message."\0", strlen($message)+1);
  212.     }
  213.    
  214.    
  215.     /**
  216.      * Reads a message from the socket, will read until entire message has been recieved or connection closes.
  217.      * @param $parse Used for recursive calls to tell the function not to parse a partial packet
  218.      */
  219.     function read($parse=true) {
  220.         $res = rtrim(socket_read($this->soc, 4096));
  221.         if($this->debug)echo "<<-\t$res\n";
  222.         if(!$res) {
  223.             return "DIED"; //Used to gracefully handle a closed socket
  224.         }
  225.         if($res{strlen($res)-1}!='>') { $res.=$this->read(false);} //Recursive call for messages split over several packets.
  226.         if($parse)$this->parse($res);
  227.         return $res;
  228.     }
  229.    
  230.     /**
  231.      * Parses the recieved packets into their message and types.
  232.      * @param $packet The packet recieved.
  233.      */
  234.     function parse($packet) {
  235.         if(substr_count($packet,'>')>1) $packet = explode('/>',$packet);//If necessary split the packet into individual messages
  236.         foreach((Array)$packet as $p) {
  237.             $p = trim($p);
  238.             if(strlen($p)<5) return;//garbage data
  239.             $type = trim(strtolower(substr($p,1,strpos($p.' ',' '))));//packet type
  240.             $p = trim(str_replace("<$type",'',str_replace('/>','',$p)));//remove details so it is just the info to be parsed
  241.             parse_str(str_replace('"','',str_replace('" ','&',str_replace('="','=',str_replace('&','__38',$p)))),$this->packet[$type]);
  242.             foreach($this->packet[$type] as $k=>$v) {
  243.                 $this->packet[$type][$k] = str_replace('__38','&',$v); //htmlspecial chars are protected instead of being parsed
  244.             }
  245.             $this->handle($type,$p);
  246.         }
  247.     }
  248.    
  249.     /**
  250.      * This is the inital handler for the packets, parses them and sends them off to their respective function to be handled further.
  251.      * @param $type The character code indicating the type of data within the message.
  252.      * @param $message The data the message contains.
  253.      */
  254.     function handle($type,$msg) {
  255.         switch($type) {
  256.             case 'gp':
  257.                 if(isset($this->packet['gp']['x']))
  258.                     $this->send('<x i="'.$this->packet['gp']['x'].'" u="'.$this->userID.'" t="j" />'); //Handle groups
  259.             break;
  260.             case 'q'://XAT notice to change ip/port
  261.                 $this->connect($this->packet['q']['d'], $this->packet['q']['p']);
  262.                 $this->join($this->roomID);
  263.             break;  
  264.             case 'o':
  265.                 $this->packet['o']['u'] = $this->parseU(@$this->packet['u']['u']);
  266.                 $this->userInfo[$this->packet['o']['u']]['name'] = @$this->packet['o']['n'];
  267.                 $this->userInfo[$this->packet['o']['u']]['registeredName'] = ((isset($this->packet['o']['N']))?$this->packet['o']['N']:'');
  268.                 $this->userInfo[$this->packet['o']['u']]['avatar'] = @$this->packet['o']['a'];
  269.                 $this->userInfo[$this->packet['o']['u']]['homepage'] = @$this->packet['o']['h'];
  270.                 $this->userInfo[$this->packet['o']['u']]['rank'] = $this->f2rank(@$this->packet['o']['f']);
  271.             break;
  272.             case 'u':
  273.                 //Joined
  274.                 //Default Bot stuff regarding userInformation
  275.                 $this->packet['u']['u'] = $this->parseU(@$this->packet['u']['u']);
  276.                 $this->userInfo[$this->packet['u']['u']]['name'] = @$this->packet['u']['n'];
  277.                 $this->userInfo[$this->packet['u']['u']]['registeredName'] = ((isset($this->packet['u']['N']))?$this->packet['u']['N']:'');
  278.                 $this->userInfo[$this->packet['u']['u']]['avatar'] = @$this->packet['u']['a'];
  279.                 $this->userInfo[$this->packet['u']['u']]['homepage'] = @$this->packet['u']['h'];
  280.                 $this->userInfo[$this->packet['u']['u']]['rank'] = $this->f2rank(@$this->packet['u']['f']);
  281.                
  282.                 $event = 'userJoined';
  283.                 $data['id'] = $this->packet['u']['u'];
  284.                 $data['old'] = ($type=='o'||(isset($this->packet['u']['s']))?true:false);
  285.                 $this->handleEvent($event,$data);
  286.                
  287.             break;
  288.             case 'l':
  289.                 //User Left or was kicked, banned
  290.                 unset($this->userInfo[$this->packet['l']['u']]);
  291.                 $event = 'userLeft';
  292.                 $data['id'] = $this->packet['l']['u'];
  293.                 $this->handleEvent($event,$data);
  294.                
  295.             break;
  296.             case 'p':
  297.                 //Private message/chat recieved
  298.                 $event = ((isset($this->packet['p']['d']))?'privateChat':'privateMessage');
  299.                 $data['id'] =  $this->parseU(@$this->packet['p']['u']);
  300.                 $data['message'] = $this->packet['p']['t'];
  301.                 $this->handleEvent($event,$data);
  302.             break;
  303.             case 'm':
  304.                 //message to main chat.
  305.                 $event = 'message';
  306.                 $data['id'] = $this->parseU(@$this->packet['m']['u']);
  307.                 $data['message'] = $this->packet['m']['t'];
  308.                 $data['old'] = ((isset($this->packet['m']['s']))?true:false);
  309.                 $this->handleEvent($event,$data);
  310.             break;
  311.         }
  312.     }
  313.    
  314.     /**
  315.      * Joins a room.
  316.      * @param $roomID the numeric roomID to join.
  317.      */
  318.     function join($roomID) {
  319.         //Announce we are here:
  320.         $this->send('<y m="1" />');//Anounces our arrival to the server and gets some information to send back
  321.         $this->read(); //Auto parsed into $this->packet['y']
  322.         $this->send('<j2 q="1" y="'.$this->packet['y']['i'].'" k="'.$this->k.'" k3="0" z="12" p="0" c="'.$roomID.'" f="0" u="'.$this->userID.'" d0="0" n="'.$this->name.'" a="'.$this->avatar.'" h="'.$this->homepage.'" v="0" />');
  323.         $this->roomID = $roomID;
  324.     }
  325.    
  326.     /**
  327.      * Parses the u value from a packet to get just the id
  328.      * @param $id the id to be parsed.
  329.      */
  330.     function parseU($id) {
  331.         if(substr_count($id,'_')>=1) $id = substr($id,0,strpos($id,'_'));
  332.         return $id;
  333.     }
  334.    
  335.     /**
  336.      * Converts an f value to a string containing the corresponding rank...this if you don't understand bitwise operations is a little 'magical' deal with it.
  337.      * @param $f The f value to be parsed.
  338.      */
  339.     function f2rank($f) {
  340.         $f = $this->parseU($f);
  341.        
  342.         if($f==-1) return 'guest';
  343.         //Okay, 98% of you reading this on NewHax won't know what any of this means; if you do you are more adnvanced than I expected
  344.         //Not that this is advnaced stuff, but basiclly it is a bit-wise comparision(notice & instead of && it is checking if certain binary bits are set to 1
  345.         //The F value is essientially a bunch of flags where for example 00010000 == banned(the 0s can be 0 or 1, just as long as that one 1 is a one you are banned.
  346.         if((16 & $f)) return 'banned';
  347.         if((1 & $f)&&(2 & $f)) return 'member';
  348.         if((4 & $f)) return 'owner';
  349.         if((32 & $f)&&(1 & $f)&&!(2 & $f)) return 'main';
  350.         if(!(1 & $f)&&!(2 & $f)) return 'guest';
  351.         if((16 & $f)) return 'banned';
  352.         if((2 & $f)&&!(1 & $f)) return 'mod';
  353.     }
  354.    
  355.     /**
  356.      * Returns an assoc array of information regarding the user with the given id
  357.      * @param $id The user id you want information on.
  358.      */
  359.     function getUserArray($id) {
  360.         $id = $this->parseU($id);
  361.         if(isset($this->userInfo[$id])) {
  362.              return $this->userInfo[$id];
  363.         } else return false;
  364.     }
  365.    
  366.     /**
  367.      * Sends the given message to the main chat
  368.      * @param $message
  369.      */
  370.     function sendMessage($message) {
  371.         if(empty($message))return;
  372.         $this->send('<m t="'.$message.'" u="'.$this->userID.'" />');    
  373.     }
  374.    
  375.     /**
  376.      * Sends a PC to the given ID
  377.      * @param $message The message to send.
  378.      * @param $id The id to send the message to
  379.      */
  380.     function sendPrivateChat($message, $id) {
  381.         if(empty($message))return;
  382.         $this->send('<p u="'.$id.'" t="'.$message.'" s="2" d="'.$this->userID.'" />');    
  383.     }
  384.    
  385.     /**
  386.      * Sends a PM to the given ID
  387.      * @param $message The message to send.
  388.      * @param $id The id to send the message to
  389.      */
  390.     function sendPrivateMessage($message,$id) {
  391.         $id = $this->parseU($id);
  392.         if(empty($message))return;
  393.         $this->send('<p u="'.$id.'" t="'.$message.'" />');    
  394.     }
  395.    
  396.     /**
  397.      * Makes the given $id an owner, assuming the bot is main
  398.      * @param $id The id to promote
  399.      */
  400.     function mod($id) {
  401.         $this->send('<c u="'.$this->parseU($id).'" t="/M" />');
  402.     }
  403.    
  404.     /**
  405.      * Makes the given $id a mod, assuming the bot is owner
  406.      * @param $id The id to promote
  407.      */
  408.     function owner($id) {
  409.         $this->send('<c u="'.$this->parseU($id).'" t="/m" />');
  410.     }
  411.    
  412.     /**
  413.      * Makes the given $id a member, assuming the bot is mod
  414.      * @param $id The id to member
  415.      */
  416.     function member($id) {
  417.             $this->send('<c u="'.$this->parseU($id).'" t="/e" />');
  418.     }
  419.    
  420.     /**
  421.      * KIcks the given ID assuming the bot is a mod and $id is a member or less
  422.      * @param $id The id to kick
  423.      */
  424.     function kick($message, $id) {
  425.         $this->send('<c p="'.$message.'" u="'.$this->parseU($id).'" t="/k" />');  
  426.     }
  427.    
  428.     /**
  429.      * Bans the ID for a given time(0 is forever)
  430.      * @param $id The id to ban
  431.      */
  432.     function ban($message, $id, $time) {
  433.         if(empty($time)) $time = 3600;
  434.         $this->send('<c p="'.$message.'" u="'.$this->parseU($id).'" t="/g'.$time.'" />');
  435.     }
  436.    
  437.     /**
  438.      * Unbans the given ID
  439.      * @param $id The id to unban
  440.      */
  441.     function unban($id) {
  442.         $this->send('<c u="'.$this->parseU($id).'" t="/u" />');    
  443.     }
  444.    
  445.     /**
  446.      * Performs a basic HTTP GET to the given URL
  447.      * @param $url The url to retrieve, please include http:// and www if necessary
  448.      * @param $includeHeader Tells teh function wether or not to include the server header respond before the main content
  449.      */
  450.     function get($url, $includeHeader=false) {
  451.         $urlp = parse_url($url);
  452.         $fp = fsockopen($urlp['host'],80);
  453.         $path = explode('/',$url,4);
  454.         $path = ((count($path)>=4)?$path[3]:"");
  455.         $req = "GET /$path HTTP/1.1\r\n";
  456.         $req .= "Host: $urlp[host]\r\n";
  457.         $req .= "Connection: Close\r\n\r\n";
  458.         fputs($fp, $req);
  459.         $res = "";
  460.         while(!feof($fp)) $res .= fgets($fp, 4096);
  461.         fclose($fp);
  462.         if($includeHeader) return $res;
  463.         $res = explode("\r\n\r\n",$res,2);
  464.         return $res[1];
  465.     }
  466.     /**
  467.      * A utility function to get all text beween $start and $end
  468.      * @param $content The content from which we are grabbing data
  469.      * @param $start where to start grabbing from
  470.      * @param $end the end of the content to grab
  471.      */
  472.     function getBetween($content,$start,$end){
  473.         $r = explode($start, $content);
  474.         if (isset($r[1])){
  475.             $r = explode($end, $r[1]);
  476.             return $r[0];
  477.         }
  478.         return '';
  479.     }
  480.    
  481. }
  482.  
  483. ?>
Advertisement
Add Comment
Please, Sign In to add comment