Bot.php 4.2 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
<?php
// Read Unix Time and put into a variable for uptime logging
$starttime = time();
 
// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);

require_once '../Doctrine/trunk/lib/Doctrine.php';

spl_autoload_register(array('Doctrine', 'autoload'));


class DBot
{
    protected $_options = array('server' => 'irc.freenode.net',
                                'port' => 6667,
                                'username' => 'Doctrine',
                                'hostname' => 'phpdoctrine.net',
                                'servername' => 'Doctrine',
                                'realname' => 'Doctrine bot',
                                'nick' => 'Doctrine',
                                'channels' => array('#doctrine-test'));

    protected $_socket;


    public function connect()
    {
        // Open the socket to the IRC server
        $this->_socket = fsockopen($this->_options['server'], $this->_options['port']);

        unlink('log.txt');

        sleep(1);
        
        Doctrine_Manager::connection('sqlite::memory:');


        // Send auth info
        $this->execute('USER ' . $this->_options['username'] . ' ' .
                       $this->_options['hostname'] . ' ' .
                       $this->_options['servername'] . ' :' .
                       $this->_options['realname'] . "\n");

        $this->execute('NICK ' . $this->_options['nick'] . "\n");

        foreach ($this->_options['channels'] as $channel) {
            $this->execute('JOIN ' . $channel . "\n");
        }
    }
    public function execute($command)
    {
        fputs($this->_socket, $command);

        $this->log('>>> ' . $command);
    }
    public function log($command)
    {
        $fp = fopen('log.txt', 'a+');

        fwrite($fp, $command);
        
        fclose($fp);
    }
    public function disconnect()
    {
        $this->execute('QUIT' . "\n");

        fclose($this->_socket);
    }
   // IRC Functions [BEGIN] 
    
    // Joins channel
    public function join($channel)
    {
        $this->execute('JOIN ' . $channel . "\r\n");
    }

    // Leaves the channel
    public function part($channel){
        $this->execute('PART ' . $channel . "\r\n");
    }

    // send message to channel/user
    public function say($to, $msg){
        $this->execute('PRIVMSG '. $to . ' :' . $msg . "\r\n");
    }

    // modes: +o, -o, +v, -v, etc.
    public function setMode($user, $mode){
        $this->execute('MODE ' . $this->channel . ' ' . $mode . ' ' . $user . "\r\n");
    }
    // kicks user from the channel
    public function kick($user, $from, $reason = "")
    {
        $this->execute('KICK ' . $from . ' ' . $user . ' :' . $reason . "\r\n");
    }
    // changes the channel topic
    public function topic($channel, $topic)
    {
        $this->execute('TOPIC ' . $channel . ' :' . $topic . "\r\n");
    }
    public function run()
    {
    	$this->connect();
        // Force an endless while

        while( ! feof($this->_socket)) {

            // Continue the rest of the script here
            $data = fgets($this->_socket, 4096);

            print $data . "<br>";
            // Separate all data
            $ex = explode(' ', $data);

            // Send PONG back to the server
            if ($ex[0] == 'PING') {
                $this->execute('PONG ' . $ex[1] . "\n");
            }
            //$this->log($data);

            // Say something in the channel
            $command = str_replace(array(chr(10), chr(13)), '', $ex[3]);

            // strip out ':'
            $command = substr($command, 1);

            array_shift($ex);
            array_shift($ex);
            $scope = array_shift($ex);
            array_shift($ex);

            $argsStr = implode(' ', $ex);


            //$this->log($command . ' ' . $scope);

            switch ($command) {
                case '!shutdown':
                    $this->disconnect();
                    exit;
                break;
                case '!native-expr':
                    $portableExpr = $ex[0];

                break;
            }
        }

    }
}
$bot = new Dbot();
$bot->run();