Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Simple PHP IRC Bot
- *
- * PHP Version 5
- *
- * LICENSE: This source file is subject to Creative Commons Attribution
- * 3.0 License that is available through the world-wide-web at the following URI:
- * http://creativecommons.org/licenses/by/3.0/. Basically you are free to adapt
- * and use this script commercially/non-commercially. My only requirement is that
- * you keep this header as an attribution to my work. Enjoy!
- *
- * @category Chat Room Scipt
- * @package Simple PHP IRC Bot
- * @author Super3boy <[email protected]>
- * @copyright 2010, The Nystic Network
- * @license http://creativecommons.org/licenses/by/3.0/
- * @link http://wildphp.com (Visit for updated versions and more free scripts!)
- * @version 1.0.0 (Last updated 03-20-2010)
- *
- */
- //So the bot doesnt stop.
- set_time_limit(0);
- ini_set('display_errors', 'on');
- //Sample connection data.
- include 'config.php';
- $conn = new mysqli($servername, $username, $password, $dbname);
- class IRCBot {
- //This is going to hold our TCP/IP connection
- var $socket;
- //This is going to hold all of the messages both server and client
- var $ex = array();
- /*
- Construct item, opens the server connection, logs the bot in
- @param array
- */
- function __construct($config)
- {
- //file_put_contents('log.txt', "Starting BOT.");
- //file_put_contents('joins.txt', "");
- $this->socket = fsockopen($config['server'], $config['port']);
- $this->login($config);
- $this->main($config);
- }
- /*
- Logs the bot in on the server
- @param array
- */
- function login($config)
- {
- $this->send_data('PASS', $config['pass']);
- $this->send_data('NICK', $config['nick']);
- $this->send_data('USER', $config['nick'].' '.$config['nick'].' '.$config['nick'].' '.$config['nick']);
- $this->join_channel($config['channel']);
- }
- /*
- This is the workhorse function, grabs the data from the server and displays on the browser
- */
- function main($config)
- {
- $data = fgets($this->socket, 256);
- echo nl2br($data);
- flush();
- //file_put_contents('log.txt', $data, FILE_APPEND);
- $this->ex = explode(' ', $data);
- if(@$this->ex[1] == "JOIN")
- {
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'How ya doing, '.$nick.'? Make sure to type !logged to log into the database');
- //file_put_contents('joins.txt', time().':'.$nick."\n", FILE_APPEND);
- }
- if($this->ex[0] == 'PING')
- {
- $this->send_data('PONG', $this->ex[1]); //Plays ping-pong with the server to stay connected.
- }
- $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
- switch($command) //List of commands the bot responds to from a user.
- {
- case ':!join':
- $this->join_channel($this->ex[4]);
- break;
- case ':!logged':
- include('config.php');
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- $sql = "INSERT INTO users (username, points, time) VALUES ('$nick', '50', '0')";
- if ($conn->query($sql) === TRUE) {
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'Added 50 tokens to '.$nick.'\'s bank!');
- }else{
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'You\re already in the database '.$nick.'!');
- }
- break;
- case ':!rawr':
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $this->send_data('PRIVMSG '.$config['channel'].' :', '* '.$nick.' rawrs with stength!*');
- break;
- case ':!check':
- // check if user is in database -> the give points -> else log the user
- include('config.php');
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $sql2 = "SELECT * FROM users WHERE username = '$nick'";
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- $result = $conn->query($sql2);
- if ($result->num_rows > 0) {
- while($row = mysqli_fetch_assoc($result)) {
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'You currently have '.$row['points'].' points');
- }
- }else{
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'Something went wrong, PM me and I will fix it!');
- }
- break;
- case ':!imback':
- // check if user is in database -> the give points -> else log the user
- include('config.php');
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $time = time();
- $time_input = $time+300;
- $sql2 = "SELECT * FROM users WHERE username = '$nick'";
- $sql3 = "UPDATE users SET points = points+10, time = '$time_input' WHERE username = '$nick'";
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- $result = $conn->query($sql2);
- if ($result->num_rows > 0) {
- while($row = mysqli_fetch_assoc($result)) {
- if($time<=$row['time']){
- $time_row = $row['time'];
- $time = time();
- $time_left = $time_row-$time;
- $time_print = floor($time_left/60);
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'Sorry '.$nick.' it\'s too soon for you to recieve viewer points. Try again in '.$time_print.' minutes.');
- }elseif($time>=$row['time']){
- $conn->query($sql3);
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'Added 10 points to '.$nick.'\'s bank!');
- }
- }
- }else{
- $blah;
- }
- break;
- case ':!items':
- // let's grab all the items and print it in an array!
- include('config.php');
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $sql2 = "SELECT * FROM items";
- $conn = mysqli_connect($servername, $username, $password, $dbname);
- $result = $conn->query($sql2);
- if ($result->num_rows > 0) {
- while($row = mysqli_fetch_assoc($result)) {
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'Item name: '.$row['item_name'].' - Cost: '.$row['item_worth'].' - Stock: '.$row['stock'].' type "!buy '.$row['id'].' "to purchase.');
- }
- }else{
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'Something went wrong, PM me and I will fix it!');
- }
- break;
- case ':!moo':
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $this->send_data('PRIVMSG '.$config['channel'].' :', '*'.$nick.' moos at everyone!*');
- break;
- case ':!moos':
- $id = $this->ex[4];
- $person = $this->ex[5];
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $this->send_data('PRIVMSG '.$config['channel'].' :', '* '.$nick.' moos at '.$id.'*');
- break;
- case ':!buy':
- $id = $this->ex[4];
- $email = $this->ex[5];
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $subject = 'Demo mail ';
- $message = 'We will set this up later.';
- mail($to, $subject, $message);
- $this->send_data('PRIVMSG '.$config['channel'].' :', 'PURCHASE @'.$nick.' - bought '.$id.' - Please check your email: '.$email.'!');
- break;
- case ':!modme':
- $id = $this->ex[4];
- $email = $this->ex[5];
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $this->send_data('PRIVMSG '.$config['channel'].' :', '/mod '.$id.' - modded test');
- break;
- case ':!unmodme':
- $id = $this->ex[4];
- $email = $this->ex[5];
- $parts = explode('!',$this->ex[0]);
- $nick = ltrim($parts[0], ':');
- $this->send_data('PRIVMSG '.$config['channel'].' :', '/unmod '.$id.' - unmodded test');
- break;
- case ':!say':
- $message = "";
- for($i=5; $i <= (count($this->ex)); $i++)
- {
- $message .= $this->ex[$i]." ";
- }
- $this->send_data('PRIVMSG '.$config['channel'].''.$this->ex[4].' :', $message);
- break;
- }
- $this->main($config);
- }
- function send_data($cmd, $msg = null) //displays stuff to the broswer and sends data to the server.
- {
- if($msg == null)
- {
- fputs($this->socket, $cmd."\r\n");
- echo $cmd;
- } else {
- fputs($this->socket, $cmd.' '.$msg."\r\n");
- echo $cmd.' '.$msg;
- }
- }
- function join_channel($channel) //Joins a channel, used in the join function.
- {
- if(is_array($channel))
- {
- foreach($channel as $chan)
- {
- $this->send_data('JOIN', $chan);
- }
- } else {
- $this->send_data('JOIN', $channel);
- }
- }
- }
- //Start the bot
- $bot = new IRCBot($config);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement