Advertisement
Guest User

xat bot, basic source.

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