root/branches/1.0/tools/Bot.php

Revision 3132, 4.2 KB (checked in by zYne, 14 months ago)

irc bot example

Line 
1<?php
2// Read Unix Time and put into a variable for uptime logging
3$starttime = time();
4 
5// Prevent PHP from stopping the script after 30 sec
6set_time_limit(0);
7
8require_once '../Doctrine/trunk/lib/Doctrine.php';
9
10spl_autoload_register(array('Doctrine', 'autoload'));
11
12
13class DBot
14{
15    protected $_options = array('server' => 'irc.freenode.net',
16                                'port' => 6667,
17                                'username' => 'Doctrine',
18                                'hostname' => 'phpdoctrine.net',
19                                'servername' => 'Doctrine',
20                                'realname' => 'Doctrine bot',
21                                'nick' => 'Doctrine',
22                                'channels' => array('#doctrine-test'));
23
24    protected $_socket;
25
26
27    public function connect()
28    {
29        // Open the socket to the IRC server
30        $this->_socket = fsockopen($this->_options['server'], $this->_options['port']);
31
32        unlink('log.txt');
33
34        sleep(1);
35       
36        Doctrine_Manager::connection('sqlite::memory:');
37
38
39        // Send auth info
40        $this->execute('USER ' . $this->_options['username'] . ' ' .
41                       $this->_options['hostname'] . ' ' .
42                       $this->_options['servername'] . ' :' .
43                       $this->_options['realname'] . "\n");
44
45        $this->execute('NICK ' . $this->_options['nick'] . "\n");
46
47        foreach ($this->_options['channels'] as $channel) {
48            $this->execute('JOIN ' . $channel . "\n");
49        }
50    }
51    public function execute($command)
52    {
53        fputs($this->_socket, $command);
54
55        $this->log('>>> ' . $command);
56    }
57    public function log($command)
58    {
59        $fp = fopen('log.txt', 'a+');
60
61        fwrite($fp, $command);
62       
63        fclose($fp);
64    }
65    public function disconnect()
66    {
67        $this->execute('QUIT' . "\n");
68
69        fclose($this->_socket);
70    }
71   // IRC Functions [BEGIN]
72   
73    // Joins channel
74    public function join($channel)
75    {
76        $this->execute('JOIN ' . $channel . "\r\n");
77    }
78
79    // Leaves the channel
80    public function part($channel){
81        $this->execute('PART ' . $channel . "\r\n");
82    }
83
84    // send message to channel/user
85    public function say($to, $msg){
86        $this->execute('PRIVMSG '. $to . ' :' . $msg . "\r\n");
87    }
88
89    // modes: +o, -o, +v, -v, etc.
90    public function setMode($user, $mode){
91        $this->execute('MODE ' . $this->channel . ' ' . $mode . ' ' . $user . "\r\n");
92    }
93    // kicks user from the channel
94    public function kick($user, $from, $reason = "")
95    {
96        $this->execute('KICK ' . $from . ' ' . $user . ' :' . $reason . "\r\n");
97    }
98    // changes the channel topic
99    public function topic($channel, $topic)
100    {
101        $this->execute('TOPIC ' . $channel . ' :' . $topic . "\r\n");
102    }
103    public function run()
104    {
105        $this->connect();
106        // Force an endless while
107
108        while( ! feof($this->_socket)) {
109
110            // Continue the rest of the script here
111            $data = fgets($this->_socket, 4096);
112
113            print $data . "<br>";
114            // Separate all data
115            $ex = explode(' ', $data);
116
117            // Send PONG back to the server
118            if ($ex[0] == 'PING') {
119                $this->execute('PONG ' . $ex[1] . "\n");
120            }
121            //$this->log($data);
122
123            // Say something in the channel
124            $command = str_replace(array(chr(10), chr(13)), '', $ex[3]);
125
126            // strip out ':'
127            $command = substr($command, 1);
128
129            array_shift($ex);
130            array_shift($ex);
131            $scope = array_shift($ex);
132            array_shift($ex);
133
134            $argsStr = implode(' ', $ex);
135
136
137            //$this->log($command . ' ' . $scope);
138
139            switch ($command) {
140                case '!shutdown':
141                    $this->disconnect();
142                    exit;
143                break;
144                case '!native-expr':
145                    $portableExpr = $ex[0];
146
147                break;
148            }
149        }
150
151    }
152}
153$bot = new Dbot();
154$bot->run();
Note: See TracBrowser for help on using the browser.