Changeset 2055 for trunk/modules/kobot/libraries/Kobot.php
- Timestamp:
- 02/15/2008 08:53:29 AM (11 months ago)
- Files:
-
- 1 modified
-
trunk/modules/kobot/libraries/Kobot.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/kobot/libraries/Kobot.php
r2052 r2055 18 18 public $log_level = 1; 19 19 20 // Command responses 20 // Command responses and timers 21 21 protected $responses = array(); 22 protected $timers = array(); 22 23 23 24 // Responses to drop by default … … 119 120 // Read the PRIVMSG command 120 121 $this->set_response('PRIVMSG', array($this, 'response_privmsg')); 121 122 // Reply to VERSION commands123 $this->add_trigger('^VERSION$', array($this, 'trigger_version'));124 122 } 125 123 else … … 202 200 while ( ! feof($this->socket)) 203 201 { 202 // Start a new read loop 203 $loop_time = microtime(TRUE); 204 204 205 // Read the raw server stream, up to 512 characters 205 206 while ($raw = fgets($this->socket, 512)) … … 222 223 } 223 224 } 224 // One half-second is high enough interactivity 225 usleep(500000); 225 226 // Check the timers 227 $this->check_timers(); 228 229 // Detemine the amount of time spent in this loop, in microseconds 230 $loop_time = (microtime(TRUE) - $loop_time) * 1000000; 231 232 // Every loop should take one-half second 233 ($loop_time < 500000) and usleep(500000 - $loop_time); 226 234 } 227 235 … … 332 340 } 333 341 334 public function add_trigger($pattern, $callback) 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) 335 382 { 336 383 // Store the trigger and it's callback … … 349 396 350 397 return $this; 398 } 399 400 protected function callback_hash($callback) 401 { 402 $hash = NULL; 403 if (is_array($callback)) 404 { 405 if (is_string($callback[0])) 406 { 407 // Static method callback 408 $hash = sha1($callback[0].'::'.$callback[1]); 409 } 410 else 411 { 412 // Object method callback 413 $hash = sha1(get_class($callback[0]).'->'.$callback[1]); 414 } 415 } 416 else 417 { 418 // Hash the name 419 $hash = sha1($callback); 420 } 421 422 return $hash; 351 423 } 352 424 … … 423 495 $this->channels[$data['target']][] = $data['sender']; 424 496 497 // This prevents the userlist key from growing too large, causing a buffer overflow 498 $this->channels[$data['target']] = array_values($this->channels[$data['target']]); 499 425 500 // Debug the join 426 501 $this->log(2, '> '.$data['sender'].' ('.$data['target'].')');
