| 131 | | public function login($username, $password = NULL, $realname = 'Kohana PHP Bot') |
| 132 | | { |
| 133 | | // Cache the current username |
| 134 | | $this->username = $username; |
| 135 | | |
| 136 | | // Send the login commands, use 8 for the mask (invisible) |
| 137 | | $this->send('USER '.$username.' 8 * :'.$realname); |
| 138 | | $this->send('NICK '.$username); |
| 139 | | |
| 140 | | // Update the last ping |
| 141 | | $this->stats['last_ping'] = microtime(TRUE); |
| 142 | | } |
| 143 | | |
| 144 | | public function join($channel) |
| 145 | | { |
| 146 | | if (empty($this->channels[$channel])) |
| 147 | | { |
| 148 | | // Set the channel as joined |
| 149 | | $this->channels[$channel] = array(); |
| 150 | | |
| 151 | | // Join the channel |
| 152 | | $this->send('JOIN '.$channel); |
| 153 | | } |
| 154 | | } |
| 155 | | |
| 156 | | public function part($channel) |
| 157 | | { |
| 158 | | if ( ! empty($this->channels[$channel])) |
| 159 | | { |
| 160 | | // Leave the channel |
| 161 | | $this->send('PART '.$channel); |
| 162 | | |
| 163 | | // Remove the channel |
| 164 | | unset($this->channels[$channel]); |
| 165 | | } |
| 166 | | } |
| 167 | | |
| 168 | | public function quit($message = '</Kirc> by Kohana Team') |
| 169 | | { |
| 170 | | // Send a quit message |
| 171 | | $this->send('QUIT :'.$message); |
| 172 | | } |
| | 131 | public function log($level, $message) |
| | 132 | { |
| | 133 | if ($level >= $this->log_level) |
| | 134 | { |
| | 135 | // Display the message with a timestamp, flush the output |
| | 136 | echo date('Y-m-d g:i:s').' --- '.$message."\n"; flush(); |
| | 137 | } |
| | 138 | } |
| | 139 | |
| | 140 | /** |
| | 141 | * Handler setters for responses, triggers, and timers. |
| | 142 | * - A response executes when an IRC command is received. |
| | 143 | * - A timer executes every N.N seconds. |
| | 144 | * - A trigger executes when the bot is spoken to. |
| | 145 | */ |
| | 146 | |
| | 147 | public function set_response($command, $callback) |
| | 148 | { |
| | 149 | if ( ! is_callable($callback)) |
| | 150 | throw new Kohana_Exception('kobot.invalid_callback', $command); |
| | 151 | |
| | 152 | // Set the response callback |
| | 153 | $this->responses[$command] = $callback; |
| | 154 | |
| | 155 | return $this; |
| | 156 | } |
| | 157 | |
| | 158 | public function remove_response($command) |
| | 159 | { |
| | 160 | // Remove the response |
| | 161 | unset($this->responses[$command]); |
| | 162 | |
| | 163 | return $this; |
| | 164 | } |
| | 165 | |
| | 166 | public function set_timer($interval, $callback) |
| | 167 | { |
| | 168 | if ( ! is_callable($callback)) |
| | 169 | throw new Kohana_Exception('kobot.invalid_timer'); |
| | 170 | |
| | 171 | // Add the timer to the timers, forcing the callback to be unique |
| | 172 | $this->timers[$this->callback_hash($callback)] = array |
| | 173 | ( |
| | 174 | 'callback' => $callback, |
| | 175 | 'interval' => $interval, |
| | 176 | 'timeout' => microtime(TRUE) + $interval, |
| | 177 | ); |
| | 178 | |
| | 179 | return $this; |
| | 180 | } |
| | 181 | |
| | 182 | public function remove_timer($callback) |
| | 183 | { |
| | 184 | // Remove the timer |
| | 185 | unset($this->timers[$this->callback_hash($callback)]); |
| | 186 | |
| | 187 | return $this; |
| | 188 | } |
| | 189 | |
| | 190 | public function set_trigger($pattern, $callback) |
| | 191 | { |
| | 192 | // Store the trigger and it's callback |
| | 193 | $this->msg_triggers[$pattern] = $callback; |
| | 194 | |
| | 195 | return $this; |
| | 196 | } |
| | 197 | |
| | 198 | public function remove_trigger($pattern) |
| | 199 | { |
| | 200 | // Remove the trigger |
| | 201 | unset($this->msg_triggers[$pattern]); |
| | 202 | |
| | 203 | return $this; |
| | 204 | } |
| | 205 | |
| | 206 | /** |
| | 207 | * Server stream reading and writing. This is where the magic happens! |
| | 208 | */ |
| 316 | | public function log($level, $message) |
| 317 | | { |
| 318 | | if ($level >= $this->log_level) |
| 319 | | { |
| 320 | | // Display the message with a timestamp, flush the output |
| 321 | | echo date('Y-m-d g:i:s').' --- '.$message."\n"; flush(); |
| 322 | | } |
| 323 | | } |
| 324 | | |
| | 352 | protected function check_timers() |
| | 353 | { |
| | 354 | foreach ($this->timers as $key => $data) |
| | 355 | { |
| | 356 | if (microtime(TRUE) >= $data['timeout']) |
| | 357 | { |
| | 358 | // Run the callback, passing the bot as the only parameter |
| | 359 | call_user_func($data['callback'], $this); |
| | 360 | |
| | 361 | // Restart the timer, if it was not removed |
| | 362 | isset($this->timers[$key]) and $this->timers[$key]['timeout'] = microtime(TRUE) + $data['interval']; |
| | 363 | } |
| | 364 | } |
| | 365 | } |
| | 366 | |
| | 367 | /** |
| | 368 | * Error and exception handler. Logs errors to the console rather than |
| | 369 | * displaying them as HTML with Kohana. |
| | 370 | */ |
| 342 | | protected function check_timers() |
| 343 | | { |
| 344 | | foreach ($this->timers as $key => $data) |
| 345 | | { |
| 346 | | if (microtime(TRUE) >= $data['timeout']) |
| 347 | | { |
| 348 | | // Run the callback, passing the bot as the only parameter |
| 349 | | call_user_func($data['callback'], $this); |
| 350 | | |
| 351 | | // Restart the timer, if it was not removed |
| 352 | | isset($this->timers[$key]) and $this->timers[$key]['timeout'] = microtime(TRUE) + $data['interval']; |
| 353 | | } |
| 354 | | } |
| 355 | | } |
| 356 | | |
| 357 | | public function set_timer($interval, $callback) |
| 358 | | { |
| 359 | | if ( ! is_callable($callback)) |
| 360 | | throw new Kohana_Exception('kobot.invalid_timer'); |
| 361 | | |
| 362 | | // Add the timer to the timers, forcing the callback to be unique |
| 363 | | $this->timers[$this->callback_hash($callback)] = array |
| 364 | | ( |
| 365 | | 'callback' => $callback, |
| 366 | | 'interval' => $interval, |
| 367 | | 'timeout' => microtime(TRUE) + $interval, |
| 368 | | ); |
| 369 | | |
| 370 | | return $this; |
| 371 | | } |
| 372 | | |
| 373 | | public function remove_timer($callback) |
| 374 | | { |
| 375 | | // Remove the timer |
| 376 | | unset($this->timers[$this->callback_hash($callback)]); |
| 377 | | |
| 378 | | return $this; |
| 379 | | } |
| 380 | | |
| 381 | | public function set_trigger($pattern, $callback) |
| 382 | | { |
| 383 | | // Store the trigger and it's callback |
| 384 | | $this->msg_triggers[$pattern] = $callback; |
| 385 | | |
| 386 | | return $this; |
| 387 | | } |
| 388 | | |
| 389 | | public function set_response($command, $callback) |
| 390 | | { |
| 391 | | if ( ! is_callable($callback)) |
| 392 | | throw new Kohana_Exception('kobot.invalid_callback', $command); |
| 393 | | |
| 394 | | // Set the response callback |
| 395 | | $this->responses[$command] = $callback; |
| 396 | | |
| 397 | | return $this; |
| 398 | | } |
| 399 | | |
| | 388 | /** |
| | 389 | * Generates a unique key for a callback. |
| | 390 | */ |
| 426 | | * Kobot default responses. You can overload these in your own extension class, |
| 427 | | * or attach your own event handlers |
| | 417 | * Standard IRC commands. |
| | 418 | */ |
| | 419 | |
| | 420 | public function login($username, $password = NULL, $realname = 'Kohana PHP Bot') |
| | 421 | { |
| | 422 | // Cache the current username |
| | 423 | $this->username = $username; |
| | 424 | |
| | 425 | // Send the login commands, use 8 for the mask (invisible) |
| | 426 | $this->send('USER '.$username.' 8 * :'.$realname); |
| | 427 | $this->send('NICK '.$username); |
| | 428 | |
| | 429 | // Update the last ping |
| | 430 | $this->stats['last_ping'] = microtime(TRUE); |
| | 431 | } |
| | 432 | |
| | 433 | public function join($channel) |
| | 434 | { |
| | 435 | if (empty($this->channels[$channel])) |
| | 436 | { |
| | 437 | // Set the channel as joined |
| | 438 | $this->channels[$channel] = array(); |
| | 439 | |
| | 440 | // Join the channel |
| | 441 | $this->send('JOIN '.$channel); |
| | 442 | } |
| | 443 | } |
| | 444 | |
| | 445 | public function part($channel) |
| | 446 | { |
| | 447 | if ( ! empty($this->channels[$channel])) |
| | 448 | { |
| | 449 | // Leave the channel |
| | 450 | $this->send('PART '.$channel); |
| | 451 | |
| | 452 | // Remove the channel |
| | 453 | unset($this->channels[$channel]); |
| | 454 | } |
| | 455 | } |
| | 456 | |
| | 457 | public function quit($message = '</Kirc> by Kohana Team') |
| | 458 | { |
| | 459 | // Send a quit message |
| | 460 | $this->send('QUIT :'.$message); |
| | 461 | } |
| | 462 | |
| | 463 | /** |
| | 464 | * Default response handlers. You can overload these in your own extension |
| | 465 | * class, or attach your own event handlers |