| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
$starttime = time(); |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
set_time_limit(0); |
|---|
| 7 |
|
|---|
| 8 |
require_once '../Doctrine/trunk/lib/Doctrine.php'; |
|---|
| 9 |
|
|---|
| 10 |
spl_autoload_register(array('Doctrine', 'autoload')); |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
class 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 72 |
|
|---|
| 73 |
// Joins channel |
|---|
| 74 |
public function join($channel) |
|---|
| 75 |
{ |
|---|
| 76 |
$this->execute('JOIN ' . $channel . "\r\n"); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
public function part($channel){ |
|---|
| 81 |
$this->execute('PART ' . $channel . "\r\n"); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
public function say($to, $msg){ |
|---|
| 86 |
$this->execute('PRIVMSG '. $to . ' :' . $msg . "\r\n"); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
public function setMode($user, $mode){ |
|---|
| 91 |
$this->execute('MODE ' . $this->channel . ' ' . $mode . ' ' . $user . "\r\n"); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
public function kick($user, $from, $reason = "") |
|---|
| 95 |
{ |
|---|
| 96 |
$this->execute('KICK ' . $from . ' ' . $user . ' :' . $reason . "\r\n"); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 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 |
|
|---|
| 107 |
|
|---|
| 108 |
while( ! feof($this->_socket)) { |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
$data = fgets($this->_socket, 4096); |
|---|
| 112 |
|
|---|
| 113 |
print $data . "<br>"; |
|---|
| 114 |
|
|---|
| 115 |
$ex = explode(' ', $data); |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
if ($ex[0] == 'PING') { |
|---|
| 119 |
$this->execute('PONG ' . $ex[1] . "\n"); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
// Say something in the channel |
|---|
| 124 |
$command = str_replace(array(chr(10), chr(13)), '', $ex[3]); |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 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 |
|
|---|
| 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(); |
|---|
| 155 |
|
|---|