Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.61 KB | None | 0 0
  1. <?php
  2.  
  3. //So the bot doesnt stop.
  4.  
  5. set_time_limit(0);
  6.  
  7. ini_set('display_errors', 'on');
  8.  
  9.  
  10.     //Example connection stuff.
  11.  
  12.     $config = array(
  13.         'server' => 'irc.cit2.net',
  14.         'port' => 6667,
  15.         'nick' => 'Naija',
  16.         'name' => 'Naija',
  17.         'pass' => 'YOURPAASSWORDHERE',
  18.     );
  19.      
  20.  
  21. class IRCBot {
  22.  
  23.     //This is going to hold our TCP/IP connection
  24.  
  25.     var $socket;
  26.  
  27.  
  28.  
  29.     //This is going to hold all of the messages both server and client
  30.  
  31.     var $ex = array();
  32.  
  33.  
  34.  
  35.     /*
  36.  
  37.      Construct item, opens the server connection, logs the bot in
  38.  
  39.  
  40.  
  41.      @param array
  42.  
  43.     */
  44.  
  45.     function __construct($config)
  46.  
  47.     {
  48.  
  49.         $this->socket = fsockopen($config['server'], $config['port']);
  50.  
  51.         $this->login($config);
  52.  
  53.         $this->main();
  54.  
  55.         $this->send_data('JOIN', '#aayush');
  56.  
  57.     }
  58.  
  59.  
  60.  
  61.     /*
  62.  
  63.      Logs the bot in on the server
  64.  
  65.  
  66.  
  67.      @param array
  68.  
  69.     */
  70.  
  71.     function login($config)
  72.  
  73.     {
  74.  
  75.         $this->send_data('USER', $config['nick'].' acidavengers.co.uk '.$config['nick'].' :'.$config['name']);
  76.  
  77.         $this->send_data('NICK', $config['nick']);
  78.  
  79.     }
  80.  
  81.  
  82.  
  83.     /*
  84.  
  85.      This is the workhorse function, grabs the data from the server and displays on the browser
  86.  
  87.     */
  88.  
  89.     function main()
  90.  
  91.     {
  92.  
  93.         $data = fgets($this->socket, 128);
  94.  
  95.         echo nl2br($data);
  96.  
  97.         flush();
  98.  
  99.         $this->ex = explode(' ', $data);
  100.  
  101.  
  102.  
  103.         if($this->ex[0] == 'PING')
  104.  
  105.         {
  106.  
  107.             $this->send_data('PONG', $this->ex[1]); //Plays ping-pong with the server to stay connected.
  108.  
  109.         }
  110.  
  111.  
  112.  
  113.         $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
  114.  
  115.  
  116.  
  117.         switch($command) //List of commands the bot responds to from a user.
  118.  
  119.         {
  120.  
  121.             case ':!join':
  122.  
  123.                 $this->join_channel($this->ex[4]);
  124.  
  125.                 break;
  126.  
  127.  
  128.  
  129.             case ':!quit':
  130.  
  131.                 $this->send_data('QUIT', 'acidavengers.co.uk made Bot');
  132.  
  133.                 break;
  134.  
  135.  
  136.  
  137.             case ':!op':
  138.  
  139.                 $this->op_user();
  140.  
  141.                 break;
  142.  
  143.  
  144.  
  145.             case ':!deop':
  146.  
  147.                 $this->op_user('','', false);
  148.  
  149.                 break;
  150.  
  151.  
  152.  
  153.             case ':!protect':
  154.  
  155.                 $this->protect_user();
  156.  
  157.                 break;
  158.  
  159.         }
  160.  
  161.  
  162.  
  163.         $this->main();
  164.  
  165.     }
  166.  
  167.  
  168.  
  169.     function send_data($cmd, $msg = null) //displays stuff to the broswer and sends data to the server.
  170.  
  171.     {
  172.  
  173.         if($msg == null)
  174.  
  175.         {
  176.  
  177.             fputs($this->socket, $cmd."\r\n");
  178.  
  179.             echo '<strong>'.$cmd.'</strong><br />';
  180.  
  181.         } else {
  182.  
  183.             fputs($this->socket, $cmd.' '.$msg."\r\n");
  184.  
  185.             echo '<strong>'.$cmd.' '.$msg.'</strong><br />';
  186.  
  187.         }
  188.  
  189.     }
  190.  
  191.  
  192.  
  193.     function join_channel($channel) //Joins a channel, used in the join function.
  194.  
  195.     {
  196.  
  197.         if(is_array($channel))
  198.  
  199.         {
  200.  
  201.             foreach($channel as $chan)
  202.  
  203.             {
  204.  
  205.                 $this->send_data('JOIN', $chan);
  206.  
  207.             }
  208.  
  209.         } else {
  210.  
  211.             $this->send_data('JOIN', $channel);
  212.  
  213.         }
  214.  
  215.     }
  216.  
  217.  
  218.  
  219.     function protect_user($user = '')
  220.  
  221.     {
  222.  
  223.         if($user == '')
  224.  
  225.         {
  226.  
  227. if(php_version() >= '5.3.0')
  228.             {
  229.                 $user = strstr($this->ex[0], '!', true);
  230.             } else {
  231.                 $length = strstr($this->ex[0], '!');
  232.                 $user   = substr($this->ex[0], 0, $length);
  233.             }
  234.             }
  235.  
  236.         }
  237.  
  238.  
  239.  
  240.         $this->send_data('MODE', $this->ex[2] . ' +a ' . $user);
  241.  
  242.     }
  243.  
  244.            
  245.  
  246.     function op_user($channel = '', $user = '', $op = true)
  247.  
  248.     {
  249.  
  250.         if($channel == '' || $user == '')
  251.  
  252.         {
  253.  
  254.             if($channel == '')
  255.  
  256.             {
  257.  
  258.                 $channel = $this->ex[2];
  259.  
  260.             }
  261.  
  262.  
  263.  
  264.             if($user == '')
  265.  
  266.             {
  267.  
  268. if(php_version() >= '5.3.0')
  269.             {
  270.                 $user = strstr($this->ex[0], '!', true);
  271.             } else {
  272.                 $length = strstr($this->ex[0], '!');
  273.                 $user   = substr($this->ex[0], 0, $length);
  274.             }
  275.             }
  276.  
  277.         }
  278.  
  279.  
  280.  
  281.         if($op)
  282.  
  283.         {
  284.  
  285.             $this->send_data('MODE', $channel . ' +o ' . $user);
  286.  
  287.         } else {
  288.  
  289.             $this->send_data('MODE', $channel . ' -o ' . $user);
  290.  
  291.         }
  292.  
  293.     }
  294.  
  295. }
  296.  
  297.  
  298.     $bot = new IRCBot($config);
  299.  
  300. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement