Changeset 2055
- Timestamp:
- 02/15/2008 08:53:29 AM (11 months ago)
- Location:
- trunk/modules/kobot
- Files:
-
- 3 modified
-
controllers/kobot.php (modified) (2 diffs)
-
i18n/en_US/kobot.php (modified) (1 diff)
-
libraries/Kobot.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/kobot/controllers/kobot.php
r2052 r2055 12 12 13 13 // Add triggers 14 $bot->add_trigger('^goodnight, bot$', array($this, 'trigger_quit')) 15 ->add_trigger('^tell (.+?) about (.+)$', array($this, 'trigger_say')) 16 ->add_trigger('^([r|#])(\d+)$', array($this, 'trigger_trac')) 17 ->add_trigger('^[a-z_]+$', array($this, 'trigger_default')); 14 $bot->set_trigger('^goodnight, bot$', array($this, 'trigger_quit')) 15 ->set_trigger('^tell (.+?) about (.+)$', array($this, 'trigger_say')) 16 ->set_trigger('^([r|#])(\d+)$', array($this, 'trigger_trac')) 17 ->set_trigger('^[a-z_]+$', array($this, 'trigger_default')); 18 19 // Add timers 20 $bot->set_timer(5, array($this, 'say_hi')); 18 21 19 22 // Login and join the default channel 20 23 $bot->login('koboto'); 21 24 $bot->join('#koboto'); 25 $bot->read(); 26 } 22 27 23 $bot->read(); 28 public function say_hi(Kobot $bot) 29 { 30 // Say hello! 31 $bot->log(1, 'Just saying a timed hello!'); 24 32 25 // $bot->send('PRIVMSG #koboto :Go away, Shadowhand!');26 // $bot->quit('hahahaha');33 // Only execute the timer once 34 $bot->remove_timer(array($this, __FUNCTION__)); 27 35 } 28 36 … … 55 63 { 56 64 case '#': 57 $bot->send('PRIVMSG '.$data['target'].' :Ticket #'.$params[2].' ishttp://trac.kohanaphp.com/ticket/'.$params[2]);65 $bot->send('PRIVMSG '.$data['target'].' :Ticket '.$params[2].', http://trac.kohanaphp.com/ticket/'.$params[2]); 58 66 break; 59 67 case 'r': 60 $bot->send('PRIVMSG '.$data['target'].' :Revision r'.$params[2].' ishttp://trac.kohanaphp.com/changeset/'.$params[2]);68 $bot->send('PRIVMSG '.$data['target'].' :Revision '.$params[2].', http://trac.kohanaphp.com/changeset/'.$params[2]); 61 69 break; 62 70 } -
trunk/modules/kobot/i18n/en_US/kobot.php
r2051 r2055 5 5 'command_line_only' => 'Kobot can only be run in command line mode: <tt>php index.php "kobot"</tt>', 6 6 'invalid_callback' => 'Invalid response for %s. See <a href="http://php.net/callback#language.types.callback">php.net/callback</a>.', 7 'invalid_timer' => 'All timers must be valid callbacks. See <a href="http://php.net/callback#language.types.callback">php.net/callback</a>.', 7 8 ); -
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'].')');
