User talk:Brian McNeil/enWN-NewsBot webpage

From Wikinews, the free news source you can write!
Jump to navigation Jump to search
<?php

/**
 * Developing PHP IRC Bot
 *                          *** RUNS IN A WEB PAGE FOR TESTING PURPOSES ***
 * PHP Version 5
 *
 * LICENSE: This source file is subject to Creative Commons Attribution 3.0
 * License available here: http://creativecommons.org/licenses/by/3.0/
 *
 * Derived from demo script by: Super3boy <admin@wildphp.com> last updated 2010-03-20
 *                              Retrieved from: http://wildphp.com
 *
 */

set_time_limit(0);  // Keep bot running indefinitely
ini_set('display_errors', 'on');  // Verbose error display

// In-testing definition, use a scratch channel.
$IRCconf = array(
    'server' => 'irc.freenode.net', 
    'port'   => 6667, 
    'channel' => '##wikinews-bot-test',
    'name'   => 'Bot - In - Training', 
    'nick'   => 'enWN-test-bot', 
    'pass'   => '', 
);

$db_IRC = array(
    'dB'     => 'example:3307',
    'dBuser' => 'MySQLusername',
    'dBpass' => 'dBpassword',
);


// Manage Bot in a Class.

class IRCBot {

    var $socket;  // to hold our TCP/IP connection
    var $ex = array();  // to hold all of the messages both server and client
    var $dBlink;  // to hold MySQL database 'handle'

    /*
        Construct item, opens the server connection, logs the bot in
        @param array

    */
    function __construct($IRCconf, $dB_IRC)

    {
        // Open socket to IRC server, log in.
        $this->socket = fsockopen($IRCconf['server'], $IRCconf['port']);
        $this->login($IRCconf);

        // Attach to the database for logging and queries
        $dBlink = mysql_connect($dB_IRC['dB'], $dB_IRC['user'], $dB_IRC['pass']);
        if (!$dBlink) {
            $this->send_data('QUIT', 'Bus Error. Passengers Dumped.');
            die('Could not connect: ' . mysql_error());
        }
        echo 'Connected successfully to database';

        // Run the main function loop
        $this->main($IRCconf);

        // Finished with the database
        mysql_close($dBlink);
    }

    function login($config)
    {
        $this->send_data('USER', $config['nick'].' robot.wikinewsie.org '.$config['nick'].' :'.$config['name']);
        $this->send_data('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, 512);  // Grab 'whatever' from the IRC server

        echo nl2br($data);  // Spit it out on a web page, with [cr][lf] pairs turned to line breaks

        flush();

        $this->ex = explode(' ', $data); // Split data into an array

        if ($this->ex[0] == 'PING')
        {
            $this->send_data( 'PONG', $this->ex[1]);  // PING? PONG!
        }

        $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);  // Strip [CR][LF] pairs

        switch( $command )  // Handle all commands within a switch
        {
            case ':!join':
                $this->join_channel($this->ex[4]);
                break;

            case ':!part':
                $this->send_data('PART '.$this->ex[4].' :', 'Exit, stage right.');
                break;

            case ':!say':
                $message = "";
                for($i=5; $i <= (count($this->ex)); $i++)
                {
                    $message .= $this->ex[$i]." ";
                }

                $this->send_data('PRIVMSG '.$this->ex[4].' :', $message);
                break;

            case ':!restart':
                echo "<meta http-equiv=\"refresh\" content=\"5\">";
                exit;

            case ':!shutdown':
                $this->send_data('QUIT', 'Bus Error. Passengers Dumped.');
                exit;
        }

        $this->main($config);
    }

    function send_data($cmd, $msg = null)  // Send to IRC server, debug output 
    {
        if($msg == null)
        {
            fputs($this->socket, $cmd."\r\n");
            echo '<strong>'.$cmd.'</strong><br />';
        } else {
            fputs($this->socket, $cmd.' '.$msg."\r\n");
            echo '<strong>'.$cmd.' '.$msg.'</strong><br />';
        }
    }

    function join_channel($channel)  //Join channel(s)
    {
        if(is_array($channel))
        {
            foreach($channel as $chan)
            {
                $this->send_data('JOIN', $chan);
            }

        } else {

            $this->send_data('JOIN', $channel);
        }
    }     
}

$bot = new IRCBot($IRCconf, £$dB_IRC);  // Start the bot


?>