Advertisement
Guest User

Untitled

a guest
Jan 13th, 2015
3,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.18 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Simple PHP IRC Bot
  5.  *
  6.  * PHP Version 5
  7.  *
  8.  * LICENSE: This source file is subject to Creative Commons Attribution
  9.  * 3.0 License that is available through the world-wide-web at the following URI:
  10.  * http://creativecommons.org/licenses/by/3.0/.  Basically you are free to adapt
  11.  * and use this script commercially/non-commercially. My only requirement is that
  12.  * you keep this header as an attribution to my work. Enjoy!
  13.  *
  14.  * @category   Chat Room Scipt
  15.  * @package    Simple PHP IRC Bot
  16.  * @author     Super3boy <[email protected]>
  17.  * @copyright  2010, The Nystic Network
  18.  * @license    http://creativecommons.org/licenses/by/3.0/
  19.  * @link       http://wildphp.com (Visit for updated versions and more free scripts!)
  20.  * @version    1.0.0 (Last updated 03-20-2010)
  21.  *
  22.  */
  23.  
  24. //So the bot doesnt stop.
  25. set_time_limit(0);
  26. ini_set('display_errors', 'on');
  27.  
  28. //Sample connection data.
  29. include 'config.php';
  30.  
  31. $conn = new mysqli($servername, $username, $password, $dbname);
  32.                                  
  33. class IRCBot {
  34.  
  35.         //This is going to hold our TCP/IP connection
  36.         var $socket;
  37.  
  38.         //This is going to hold all of the messages both server and client
  39.         var $ex = array();
  40.  
  41.         /*
  42.        
  43.          Construct item, opens the server connection, logs the bot in
  44.          @param array
  45.  
  46.         */
  47.  
  48.         function __construct($config)
  49.  
  50.         {
  51.  
  52.                 //file_put_contents('log.txt', "Starting BOT.");
  53.                 //file_put_contents('joins.txt', "");
  54.  
  55.                 $this->socket = fsockopen($config['server'], $config['port']);
  56.                 $this->login($config);
  57.                 $this->main($config);
  58.         }
  59.  
  60.  
  61.  
  62.         /*
  63.  
  64.          Logs the bot in on the server
  65.          @param array
  66.  
  67.         */
  68.  
  69.         function login($config)
  70.         {
  71.                 $this->send_data('PASS', $config['pass']);
  72.                 $this->send_data('NICK', $config['nick']);
  73.                 $this->send_data('USER', $config['nick'].' '.$config['nick'].' '.$config['nick'].' '.$config['nick']);
  74.                 $this->join_channel($config['channel']);
  75.         }
  76.  
  77.  
  78.  
  79.         /*
  80.  
  81.          This is the workhorse function, grabs the data from the server and displays on the browser
  82.  
  83.         */
  84.  
  85.         function main($config)
  86.         {            
  87.                 $data = fgets($this->socket, 256);
  88.                
  89.                 echo nl2br($data);
  90.                
  91.                 flush();
  92.  
  93.                 //file_put_contents('log.txt', $data, FILE_APPEND);
  94.  
  95.                 $this->ex = explode(' ', $data);
  96.                
  97.  
  98.                 if(@$this->ex[1] == "JOIN")
  99.                 {
  100.                     $parts = explode('!',$this->ex[0]);
  101.                     $nick = ltrim($parts[0], ':');
  102.                     $this->send_data('PRIVMSG '.$config['channel'].' :', 'How ya doing, '.$nick.'?  Make sure to type !logged to log into the database');
  103.                     //file_put_contents('joins.txt', time().':'.$nick."\n", FILE_APPEND);
  104.                 }
  105.  
  106.  
  107.                 if($this->ex[0] == 'PING')
  108.                 {
  109.                         $this->send_data('PONG', $this->ex[1]); //Plays ping-pong with the server to stay connected.
  110.                 }
  111.                
  112.                 $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
  113.                 switch($command) //List of commands the bot responds to from a user.
  114.                 {    
  115.  
  116.                         case ':!join':     
  117.                                 $this->join_channel($this->ex[4]);
  118.                                 break;                    
  119.                         case ':!logged':
  120.                                 include('config.php');
  121.                                 $parts = explode('!',$this->ex[0]);
  122.                                 $nick = ltrim($parts[0], ':');
  123.                                 $conn = mysqli_connect($servername, $username, $password, $dbname);
  124.                                 $sql = "INSERT INTO users (username, points, time) VALUES ('$nick', '50', '0')";
  125.                                 if ($conn->query($sql) === TRUE) {
  126.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', 'Added 50 tokens to '.$nick.'\'s bank!');
  127.                                 }else{
  128.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', 'You\re already in the database '.$nick.'!');
  129.                                 }
  130.                                 break;  
  131.                         case ':!rawr':
  132.                                 $parts = explode('!',$this->ex[0]);
  133.                                 $nick = ltrim($parts[0], ':'); 
  134.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', '* '.$nick.' rawrs with stength!*');
  135.                                 break;  
  136.                                      
  137.                         case ':!check':
  138.                                 // check if user is in database -> the give points -> else log the user
  139.                                 include('config.php');
  140.                                 $parts = explode('!',$this->ex[0]);
  141.                                 $nick = ltrim($parts[0], ':');
  142.                                 $sql2 = "SELECT * FROM users WHERE username = '$nick'";
  143.                                 $conn = mysqli_connect($servername, $username, $password, $dbname);
  144.                                 $result = $conn->query($sql2);
  145.                                 if ($result->num_rows > 0) {
  146.                                     while($row = mysqli_fetch_assoc($result)) {
  147.                                     $this->send_data('PRIVMSG '.$config['channel'].' :', 'You currently have '.$row['points'].' points');
  148.                                         }
  149.                                 }else{
  150.                                     $this->send_data('PRIVMSG '.$config['channel'].' :', 'Something went wrong, PM me and I will fix it!');
  151.                                 }
  152.                                 break;  
  153.  
  154.                                      
  155.                         case ':!imback':
  156.                                 // check if user is in database -> the give points -> else log the user
  157.                                 include('config.php');
  158.                                 $parts = explode('!',$this->ex[0]);
  159.                                 $nick = ltrim($parts[0], ':');
  160.                                 $time = time();
  161.                                 $time_input = $time+300;
  162.                                 $sql2 = "SELECT * FROM users WHERE username = '$nick'";
  163.                                 $sql3 = "UPDATE users SET points = points+10, time = '$time_input' WHERE username = '$nick'";                                
  164.                                 $conn = mysqli_connect($servername, $username, $password, $dbname);
  165.                                 $result = $conn->query($sql2);
  166.                                 if ($result->num_rows > 0) {
  167.                                     while($row = mysqli_fetch_assoc($result)) {
  168.                                         if($time<=$row['time']){
  169.                                             $time_row = $row['time'];
  170.                                             $time = time();
  171.                                             $time_left = $time_row-$time;
  172.                                             $time_print = floor($time_left/60);
  173.                                             $this->send_data('PRIVMSG '.$config['channel'].' :', 'Sorry '.$nick.' it\'s too soon for you to recieve viewer points. Try again in '.$time_print.' minutes.');                                            
  174.                                             }elseif($time>=$row['time']){
  175.                                             $conn->query($sql3);                                            
  176.                                             $this->send_data('PRIVMSG '.$config['channel'].' :', 'Added 10 points to '.$nick.'\'s bank!');
  177.                                         }
  178.                                     }
  179.                                 }else{
  180.                                     $blah;
  181.                                 }                                                              
  182.                                 break;
  183.  
  184.                         case ':!items':
  185.                                 // let's grab all the items and print it in an array!
  186.                                 include('config.php');
  187.                                 $parts = explode('!',$this->ex[0]);
  188.                                 $nick = ltrim($parts[0], ':');
  189.                                 $sql2 = "SELECT * FROM items";
  190.                                 $conn = mysqli_connect($servername, $username, $password, $dbname);
  191.                                 $result = $conn->query($sql2);
  192.                                 if ($result->num_rows > 0) {
  193.                                     while($row = mysqli_fetch_assoc($result)) {
  194.                                         $this->send_data('PRIVMSG '.$config['channel'].' :', 'Item name: '.$row['item_name'].' - Cost: '.$row['item_worth'].' - Stock: '.$row['stock'].' type "!buy '.$row['id'].'  "to purchase.');
  195.                                         }
  196.                                 }else{
  197.                                     $this->send_data('PRIVMSG '.$config['channel'].' :', 'Something went wrong, PM me and I will fix it!');
  198.                                 }
  199.                                 break;
  200.                                      
  201.                         case ':!moo':
  202.                                 $parts = explode('!',$this->ex[0]);
  203.                                 $nick = ltrim($parts[0], ':'); 
  204.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', '*'.$nick.' moos at everyone!*');
  205.                                 break;  
  206.  
  207.                         case ':!moos':
  208.                                 $id = $this->ex[4];
  209.                                 $person = $this->ex[5];
  210.                                 $parts = explode('!',$this->ex[0]);
  211.                                 $nick = ltrim($parts[0], ':');  
  212.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', '* '.$nick.' moos at '.$id.'*');
  213.                                 break;
  214.  
  215.                         case ':!buy':
  216.                                 $id = $this->ex[4];
  217.                                 $email = $this->ex[5];
  218.                                 $parts = explode('!',$this->ex[0]);
  219.                                 $nick = ltrim($parts[0], ':');
  220.                                 $to  = '[email protected]';
  221.                                 $subject = 'Demo mail ';  
  222.                                 $message = 'We will set this up later.';  
  223.                                 mail($to, $subject, $message);
  224.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', 'PURCHASE @'.$nick.' -  bought '.$id.' - Please check your email: '.$email.'!');
  225.                                 break;
  226.  
  227.                         case ':!modme':
  228.                                 $id = $this->ex[4];
  229.                                 $email = $this->ex[5];
  230.                                 $parts = explode('!',$this->ex[0]);
  231.                                 $nick = ltrim($parts[0], ':');  
  232.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', '/mod '.$id.' - modded test');
  233.                                 break;  
  234.                                                                                            
  235.                         case ':!unmodme':
  236.                                 $id = $this->ex[4];
  237.                                 $email = $this->ex[5];
  238.                                 $parts = explode('!',$this->ex[0]);
  239.                                 $nick = ltrim($parts[0], ':');  
  240.                                 $this->send_data('PRIVMSG '.$config['channel'].' :', '/unmod '.$id.' - unmodded test');
  241.                                 break;  
  242.                                                                          
  243.                         case ':!say':
  244.                                 $message = "";
  245.                                 for($i=5; $i <= (count($this->ex)); $i++)
  246.                                 {
  247.                                         $message .= $this->ex[$i]." ";
  248.                                 }
  249.                                
  250.                                 $this->send_data('PRIVMSG '.$config['channel'].''.$this->ex[4].' :', $message);
  251.                                 break;                         
  252.                 }
  253.  
  254.                 $this->main($config);
  255.         }
  256.  
  257.  
  258.  
  259.         function send_data($cmd, $msg = null) //displays stuff to the broswer and sends data to the server.
  260.         {
  261.                 if($msg == null)
  262.                 {
  263.                         fputs($this->socket, $cmd."\r\n");
  264.                         echo $cmd;
  265.                 } else {
  266.  
  267.                         fputs($this->socket, $cmd.' '.$msg."\r\n");
  268.                         echo $cmd.' '.$msg;
  269.                 }
  270.  
  271.         }
  272.  
  273.  
  274.  
  275.         function join_channel($channel) //Joins a channel, used in the join function.
  276.         {
  277.  
  278.                 if(is_array($channel))
  279.                 {
  280.                         foreach($channel as $chan)
  281.                         {
  282.                                 $this->send_data('JOIN', $chan);
  283.                         }
  284.  
  285.                 } else {
  286.                         $this->send_data('JOIN', $channel);
  287.                 }
  288.         }    
  289. }
  290.  
  291. //Start the bot
  292. $bot = new IRCBot($config);
  293. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement