Changeset 2052 for trunk/modules/kobot
- Timestamp:
- 02/14/2008 11:10:34 PM (9 months ago)
- Location:
- trunk/modules/kobot
- Files:
-
- 2 modified
-
controllers/kobot.php (modified) (3 diffs)
-
libraries/Kobot.php (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/kobot/controllers/kobot.php
r2051 r2052 8 8 $bot = new Kobot('irc.freenode.net'); 9 9 10 // Enable debugging 11 $bog->log_level = 4; 12 10 13 // Add triggers 11 $bot->add_trigger(array($this, 'trigger_say'), '^tell (.+?) about (.+)$') 12 ->add_trigger(array($this, 'trigger_trac'), '^[r|#](\d+)$') 13 ->add_trigger(array($this, 'trigger_php'), '^([a-z_]+)$'); 14 15 // Enable debugging 16 $bog->log_level = 2; 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')); 17 18 18 19 // Login and join the default channel … … 24 25 // $bot->send('PRIVMSG #koboto :Go away, Shadowhand!'); 25 26 // $bot->quit('hahahaha'); 27 } 28 29 public function trigger_default(Kobot $bot, array $data, array $params) 30 { 31 if (function_exists($params[0])) 32 { 33 $bot->send('PRIVMSG '.$data['target'].' :'.$data['sender'].': http://php.net/'.$params[0]); 34 } 35 } 36 37 public function trigger_quit(Kobot $bot, array $data) 38 { 39 $bot->quit('goodnight, '.$data['sender']); 26 40 } 27 41 … … 36 50 } 37 51 52 public function trigger_trac(Kobot $bot, array $data, array $params) 53 { 54 switch ($params[1]) 55 { 56 case '#': 57 $bot->send('PRIVMSG '.$data['target'].' :Ticket #'.$params[2].' is http://trac.kohanaphp.com/ticket/'.$params[2]); 58 break; 59 case 'r': 60 $bot->send('PRIVMSG '.$data['target'].' :Revision r'.$params[2].' is http://trac.kohanaphp.com/changeset/'.$params[2]); 61 break; 62 } 63 } 64 38 65 } // End -
trunk/modules/kobot/libraries/Kobot.php
r2051 r2052 41 41 ); 42 42 43 // IRC socket, MOTD, and stats43 // IRC socket, username, MOTD, and stats 44 44 protected $socket; 45 protected $username; 45 46 protected $motd; 46 47 protected $stats = array … … 115 116 // Read the PART command 116 117 $this->set_response('PART', array($this, 'response_part')); 118 119 // Read the PRIVMSG command 120 $this->set_response('PRIVMSG', array($this, 'response_privmsg')); 121 122 // Reply to VERSION commands 123 $this->add_trigger('^VERSION$', array($this, 'trigger_version')); 117 124 } 118 125 else … … 126 133 public function login($username, $password = NULL, $realname = 'Kohana PHP Bot') 127 134 { 128 // Send the login commands 129 $this->send('USER '.$username.' * * :'.$realname); 135 // Cache the current username 136 $this->username = $username; 137 138 // Send the login commands, use 8 for the mask (invisible) 139 $this->send('USER '.$username.' 8 * :'.$realname); 130 140 $this->send('NICK '.$username); 131 141 … … 160 170 public function quit($message = '</Kirc> by Kohana Team') 161 171 { 162 // Quit, wait, and exit 163 $this->send('QUIT '.$message); 164 sleep(2); 165 exit; 172 // Send a quit message 173 $this->send('QUIT :'.$message); 166 174 } 167 175 … … 211 219 { 212 220 // Debug the response 213 $this->log( 2, 'NOT FOUND:'.$data['command'].' <<< '.trim($raw));221 $this->log(3, '<<< '.$data['command'].' <<< '.trim($raw)); 214 222 } 215 223 } … … 217 225 usleep(500000); 218 226 } 227 228 // Disconnect 229 $this->log(1, 'Disconnected'); 219 230 } 220 231 … … 295 306 } 296 307 297 protected function run()298 {299 // Current username and size of username300 $bot = $this->config['username'];301 $len = strlen($bot);302 303 // Parts of a publicly spoken message304 $parts = array('nickname', 'username', 'hostname', 'channel', 'message');305 306 while ( ! feof($this->socket))307 {308 while ($raw = fgets($this->socket, 1024))309 {310 // Remove extra whitespace311 $raw = trim($raw);312 313 if (substr($raw, 0, 4) === 'PING')314 {315 // Send a PONG response316 $this->send('PONG'.substr($raw, 4));317 break;318 }319 else320 {321 if (strpos($raw, 'PRIVMSG') !== FALSE322 AND strpos($raw, $bot) !== FALSE323 AND preg_match('/^:(.+?)!n=(.+?)@(.+?) PRIVMSG (#.+?) :(.+)$/', $raw, $data))324 {325 // Make an associative array of the data326 $data = array_combine($parts, array_slice($data, 1));327 328 if (substr($data['message'], 0, $len) === $bot)329 {330 // A command has been sent331 $command = ltrim(substr($data['message'], $len), ' :;,');332 333 if ($command === 'say hello')334 {335 ;336 }337 elseif (preg_match('/^r(\d+)$/', $command, $match))338 {339 // The URL for a revision number340 $url = 'http://trac.kohanaphp.com/changeset/'.$match[1];341 342 if ($this->url_status($url))343 {344 $this->send('PRIVMSG '.$data['channel'].' :Revision r'.$match[1].', '.$url);345 }346 }347 elseif (preg_match('/^#(\d+)$/', $command, $match))348 {349 // The URL for a ticket number350 $url = 'http://trac.kohanaphp.com/ticket/'.$match[1];351 352 if ($this->url_status($url))353 {354 $this->send('PRIVMSG '.$data['channel'].' :Ticket #'.$match[1].', '.$url);355 }356 }357 }358 }359 else360 {361 echo $raw."\n";362 }363 364 //365 // if (($offset = strpos($raw, ' PRIVMSG :'.$user)) !== FALSE AND )366 // {367 //368 // }369 // list ($host, $cmd, $msg) = explode(' ', $raw, 3);370 //371 // $host = trim($host);372 // $cmd = trim($cmd);373 // $msg = trim($msg);374 //375 // list ($chan, $msg) = explode(' ', $msg);376 // $msg = substr($msg, 1);377 //378 // print_r(array('host' => $host, 'cmd' => $cmd, 'chan' => $chan, 'msg' => $msg));379 380 // if (($offset = strpos($raw, ':', 1)) !== FALSE)381 // {382 // if (($offset = substr($raw, $offset, $size)) === $user)383 // {384 // // Process the command385 // $this->send('PRIVMSG '.$chan.' :saying hello?');386 // }387 // }388 }389 390 // Flush the console output391 flush();392 }393 }394 }395 396 308 public function log($level, $message) 397 309 { … … 420 332 } 421 333 422 public function add_trigger($callback, $pattern) 423 { 424 // TODO 334 public function add_trigger($pattern, $callback) 335 { 336 // Store the trigger and it's callback 337 $this->msg_triggers[$pattern] = $callback; 338 425 339 return $this; 426 340 } … … 533 447 } 534 448 449 public function response_privmsg($data) 450 { 451 if ($data['message'] === chr(1).'VERSION'.chr(1)) 452 { 453 // Send a CTCP VERSION response 454 $this->response_version($data); 455 } 456 elseif (substr($data['message'], 0, strlen($this->username)) === $this->username 457 AND $trigger = trim(substr($data['message'], strlen($this->username)), ' :')) 458 { 459 // Process triggers 460 foreach ($this->msg_triggers as $pattern => $func) 461 { 462 if (preg_match('/'.$pattern.'/', $trigger, $matches)) 463 { 464 // Execute the callback trigger: 465 // callback($this, $command_array, $captures); 466 call_user_func($func, $this, $data, $matches); 467 } 468 } 469 } 470 } 471 472 public function response_version($data) 473 { 474 // Send a CTCP VERSION response 475 $this->send('NOTICE '.$data['sender'].' :'.chr(1).'VERSION Kobot v'.KOHANA_VERSION.' Kohana Team'.chr(1)); 476 } 477 535 478 } // End Kobot
