Changeset 61
- Timestamp:
- 02/17/08 17:24:15 (9 months ago)
- Files:
-
- trunk/Phergie/Driver/Abstract.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Dns.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Drink.php (modified) (6 diffs)
- trunk/Phergie/Plugin/Math.php (modified) (3 diffs)
- trunk/Phergie/Plugin/Nickserv.php (modified) (7 diffs)
- trunk/Phergie/Plugin/Pong.php (modified) (2 diffs)
- trunk/Phergie/Plugin/Url.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Phergie/Driver/Abstract.php
r60 r61 18 18 * @var array 19 19 */ 20 protected $config ;20 protected $config = array(); 21 21 22 22 /** … … 29 29 { 30 30 $name = strtolower($name); 31 if (!isset ($this->config[$name])) {31 if (!isset ($this->config[$name])) { 32 32 return null; 33 33 } trunk/Phergie/Plugin/Dns.php
r59 r61 2 2 3 3 /** 4 * @see Phergie_ Event_Handler4 * @see Phergie_Plugin_Abstract_Command 5 5 */ 6 require_once 'Phergie/ Event/Handler.php';6 require_once 'Phergie/Plugin/Abstract/Command.php'; 7 7 8 8 /** … … 11 11 * responds with a message containing the lookup result. 12 12 */ 13 class Phergie_Plugin_Dns extends Phergie_ Event_Handler13 class Phergie_Plugin_Dns extends Phergie_Plugin_Abstract_Command 14 14 { 15 15 /** 16 * Responds to DNS or reverse DNS lookup requests16 * Processes a DNS or reverse DNS lookup request. 17 17 * 18 * @param string $arg Host or IP address to look up 18 19 * @return void 19 20 */ 20 p ublic function onPrivmsg()21 protected function processRequest($arg) 21 22 { 23 if (preg_match('/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/', $arg)) { 24 $resolved = gethostbyaddr(long2ip(ip2long($arg))); 25 } elseif (preg_match('/^(?:[a-z0-9]+\.)+[a-z]{2,6}$/', $arg)) { 26 $resolved = gethostbyname($arg); 27 } 28 22 29 $source = $this->event->getSource(); 23 $text = $this->event->getArgument(1); 24 25 // Check if we have [rev]dns IP-address and if the IP is valid 26 if (preg_match('#^(?:rev)?dns ((?:[0-9]{1,3}\.){3}[0-9]{1,3})$#i', $text, $m) && ip2long($m[1]) !== false) { 27 $this->doPrivmsg($source, $m[1] . ' resolved as ' . gethostbyaddr(long2ip(ip2long($m[1])))); 28 // Check if we have [rev]dns host 29 } elseif (preg_match('#^(?:rev)?dns ((?:[a-z0-9]+\.)+[a-z]{2,6})$#i', $text, $m)) { 30 if(($ip = gethostbyname($m[1])) !== $m[1]) { 31 $this->doPrivmsg($source, $m[1] . ' resolved as '.$ip); 32 } else { 33 $this->doPrivmsg($source, $m[1] . ' can not be resolved'); 34 } 30 if (!isset ($resolved)) { 31 $this->doPrivmsg($source, $arg . ' cannot be resolved.'); 32 } else { 33 $this->doPrivmsg($source, $arg . ' resolved to ' . $resolved); 35 34 } 36 35 } 36 37 /** 38 * Forwards DNS lookup requests onto a central handler. 39 * 40 * @param string $host Host to look up 41 * @return void 42 */ 43 public function onDoDns($host) 44 { 45 $this->processRequest($host); 46 } 47 48 /** 49 * Forwards reverse DNS lookup requests onto a central handler. 50 * 51 * @param string $ip IP address to look up 52 * @return void 53 */ 54 public function onDoRevdns($ip) 55 { 56 $this->processRequest($ip); 57 } 37 58 } trunk/Phergie/Plugin/Drink.php
r60 r61 234 234 235 235 /** 236 * Returns a random record value from a given table 236 * Returns a random record value from a given table. 237 237 * 238 238 * @string $table Name of the table … … 248 248 /** 249 249 * Returns whether or not a given value has characters that may not be 250 * displayed correctly 250 * displayed correctly. 251 251 * 252 252 * @param string $name Value to check … … 255 255 { 256 256 return (max(array_map('ord', str_split($name))) > 126); 257 } 258 259 /** 260 * Resolves a target to the appropriate nick or pronoun and returns the 261 * result. 262 * 263 * @param string $target Original specified target 264 * @return string Resolved target 265 */ 266 protected function resolveTarget($target) 267 { 268 $target = trim($target); 269 270 switch ($target) { 271 case 'me': 272 $target = $this->event->getNick(); 273 break; 274 case 'you': 275 case $this->getIni('nick'): 276 $gender = $this->getIni('gender'); 277 if (!$gender || $gender == 'F') { 278 $target = 'herself'; 279 } else { 280 $target = 'himself'; 281 } 282 break; 283 } 284 285 return $target; 257 286 } 258 287 … … 271 300 } 272 301 273 if (preg_match('/^me(\s|$)/', $target)) { 274 $target = preg_replace('/^me/', $this->event->getNick(), $target); 275 } else if ($target == $this->getIni('nick')) { 276 $gender = $this->getIni('gender'); 277 if (!$gender || $gender == 'F') { 278 $target = 'herself'; 279 } else { 280 $target = 'himself'; 281 } 282 } 283 302 $target = $this->resolveTarget($target); 284 303 $drink = $this->getRandomRecord($type); 285 304 … … 300 319 301 320 /** 302 * Handles beer requests 321 * Handles beer requests. 322 * 323 * @param string $target Target for the request 324 * @return void 325 */ 326 public function onDoBeer($target) 327 { 328 $this->throwDrink('beer', $target); 329 } 330 331 /** 332 * Handles cocktail requests. 333 * 334 * @param string $target Target for the request 335 * @return void 336 */ 337 public function onDoCocktail($target) 338 { 339 $this->throwDrink('cocktail', $target); 340 } 341 342 /** 343 * Handles coke requests. 303 344 * 304 345 * @param string $target Source of the request 305 346 * @return void 306 347 */ 307 public function onDoBeer($target)308 {309 $this->throwDrink('beer', $target);310 }311 312 /**313 * Handles cocktail requests314 *315 * @param string $target Source of the request316 * @return void317 */318 public function onDoCocktail($target)319 {320 $this->throwDrink('cocktail', $target);321 }322 323 /**324 * Handles coke requests325 *326 * @param string $target Source of the request327 * @return void328 */329 348 public function onDoCoke($target) 330 349 { … … 333 352 334 353 /** 335 * Handles tea requests 336 * 337 * @param string $target Source of the request 354 * Provides a soda alias for coke requests. 355 * 356 * @param string $target Target for the request 357 * @return void 358 */ 359 public function onDoSoda($target) 360 { 361 $this->throwDrink('coke', $target); 362 } 363 364 /** 365 * Handles pop requests. 366 * 367 * @param string $target Target for the request 368 * @return void 369 */ 370 public function onDoPop($target) 371 { 372 $target = $this->resolveTarget($target); 373 374 $this->doAction($this->event->getSource(), 'lays ' . $target . ' out flat.'); 375 } 376 377 /** 378 * Handles tea requests. 379 * 380 * @param string $target Target for the request 338 381 * @return void 339 382 */ trunk/Phergie/Plugin/Math.php
r60 r61 2 2 3 3 /** 4 * @see Phergie_ Event_Handler4 * @see Phergie_Plugin_Abstract_Command 5 5 */ 6 require_once 'Phergie/ Event/Handler.php';6 require_once 'Phergie/Plugin/Abstract/Command.php'; 7 7 8 8 /** … … 11 11 * result. 12 12 */ 13 class Phergie_Plugin_Math extends Phergie_ Event_Handler13 class Phergie_Plugin_Math extends Phergie_Plugin_Abstract_Command 14 14 { 15 15 /** … … 40 40 41 41 /** 42 * Parses a simple arithmetic expression, evaluates it, and returns the 43 * result to the sender. 42 * Processes a request to perform a calculations. 43 * 44 * @param string $expr Expression to evaluate 45 * @return void 46 */ 47 protected function processRequest($expr) 48 { 49 // Replace constants 50 $equation = str_ireplace( 51 array('pi', 'M_PI()', 'chucknorris', 'inf', ' e '), 52 array('M_PI', 'M_PI', 1e10000, 'INF', ' M_E '), 53 $expr 54 ); 55 $equationSrc = $equation; 56 57 // Parse equation 58 $out = ''; 59 $ptr = 1; 60 $allowcomma = 0; 61 while (strlen($equation) > 0) { 62 $substr = substr($equation, 0, $ptr); 63 // Allowed string 64 if (array_search($substr, $this->allowed) !== false) { 65 $out .= $substr; 66 $equation = substr($equation, $ptr); 67 $ptr = 0; 68 // Allowed func 69 } elseif (array_search($substr, $this->funcs) !== false) { 70 $out .= $substr; 71 $equation = substr($equation, $ptr); 72 $ptr = 0; 73 $allowcomma++; 74 if ($allowcomma === 1) { 75 $this->allowed[] = ','; 76 } 77 // Opening parenthesis 78 } elseif ($substr === '(') { 79 if ($allowcomma > 0) { 80 $allowcomma++; 81 } 82 $out .= $substr; 83 $equation = substr($equation, $ptr); 84 $ptr = 0; 85 // Closing parenthesis 86 } elseif ($substr === ')') { 87 if ($allowcomma > 0) { 88 $allowcomma--; 89 if($allowcomma === 0) { 90 array_pop($this->allowed); 91 } 92 } 93 94 $out .= $substr; 95 $equation = substr($equation, $ptr); 96 $ptr = 0; 97 // Parse error if we've consumed the entire equation without finding anything valid 98 } elseif ($ptr >= strlen($equation)) { 99 $this->doPrivmsg($source, 'Syntax error at "' . $substr . '" in equation "' . $equationSrc . '"'); 100 return; 101 } else { 102 $ptr++; 103 } 104 } 105 $res = @eval('return ' . $out . ';'); 106 $source = $this->event->getSource(); 107 if($res === false) { 108 $this->doPrivmsg($source, 'Computation error, division by zero?'); 109 } else { 110 $this->doPrivmsg($source, $res); 111 } 112 } 113 114 /** 115 * Forwards math commands onto a central handler. 44 116 * 45 117 * @return void 46 118 */ 47 public function on Privmsg()119 public function onDoMath($expr) 48 120 { 49 $source = $this->event->getSource(); 50 $message = $this->event->getArgument(1); 51 if (preg_match('/^(?:math|calc) /', $message)) { 52 // Get equation 53 $equation = substr($message, 5); 54 // Replace constants 55 $equation = str_ireplace( 56 array('pi', 'M_PI()', 'chucknorris', 'inf', ' e '), 57 array('M_PI', 'M_PI', 1e10000, 'INF', ' M_E '), 58 $equation 59 ); 60 $equationSrc = $equation; 121 $this->processRequest($expr); 122 } 61 123 62 // Parse equation 63 $out = ''; 64 $ptr = 1; 65 $allowcomma = 0; 66 while (strlen($equation) > 0) { 67 $substr = substr($equation, 0, $ptr); 68 // Allowed string 69 if (array_search($substr, $this->allowed) !== false) { 70 $out .= $substr; 71 $equation = substr($equation, $ptr); 72 $ptr = 0; 73 // Allowed func 74 } elseif (array_search($substr, $this->funcs) !== false) { 75 $out .= $substr; 76 $equation = substr($equation, $ptr); 77 $ptr = 0; 78 $allowcomma++; 79 if ($allowcomma === 1) { 80 $this->allowed[] = ','; 81 } 82 // Opening parenthesis 83 } elseif ($substr === '(') { 84 if ($allowcomma > 0) { 85 $allowcomma++; 86 } 87 $out .= $substr; 88 $equation = substr($equation, $ptr); 89 $ptr = 0; 90 // Closing parenthesis 91 } elseif ($substr === ')') { 92 if ($allowcomma > 0) { 93 $allowcomma--; 94 if($allowcomma === 0) { 95 array_pop($this->allowed); 96 } 97 } 98 99 $out .= $substr; 100 $equation = substr($equation, $ptr); 101 $ptr = 0; 102 // Parse error if we've consumed the entire equation without finding anything valid 103 } elseif ($ptr >= strlen($equation)) { 104 $this->doPrivmsg($source, 'Syntax error at "' . $substr . '" in equation "' . $equationSrc . '"'); 105 return; 106 } else { 107 $ptr++; 108 } 109 } 110 $res = @eval('return ' . $out . ';'); 111 if($res === false) { 112 $this->doPrivmsg($source, 'Computation error, division by zero?'); 113 } else { 114 $this->doPrivmsg($source, $res); 115 } 116 } 124 /** 125 * Forwards calc commands onto a central handler. 126 * 127 * @return void 128 */ 129 public function onDoCalc($expr) 130 { 131 $this->processRequest($expr); 117 132 } 118 133 } trunk/Phergie/Plugin/Nickserv.php
r60 r61 2 2 3 3 /** 4 * @see Phergie_ Event_Handler4 * @see Phergie_Plugin_Abstract_AdminCommand 5 5 */ 6 require_once 'Phergie/ Event/Handler.php';6 require_once 'Phergie/Plugin/Abstract/Command.php'; 7 7 8 8 /** … … 13 13 * with NickServ for the nick used by the bot. 14 14 */ 15 class Phergie_Plugin_Nickserv extends Phergie_ Event_Handler15 class Phergie_Plugin_Nickserv extends Phergie_Plugin_Abstract_Command 16 16 { 17 17 /** … … 19 19 * 20 20 * @var int 21 */ 22 protected $index; 23 24 /** 25 * Primary nick for the bot 26 * 27 * @var string 21 28 */ 22 29 protected $nick; … … 29 36 public function init() 30 37 { 31 $this->nick = -1; 38 $this->nick = $this->getIni('nick'); 39 $this->index = -1; 32 40 } 33 41 34 42 /** 35 43 * Checks for a notice from NickServ and responds accordingly if it is an 36 * authentication request. 44 * authentication request or a notice that a ghost connection has been 45 * killed. 37 46 * 38 * @todo Add a check to reclaim the bot's nick if it is already in use39 47 * @return void 40 48 */ 41 49 public function onNotice() 42 50 { 43 if ($this->event->getNick() == 'NickServ' 44 && $this->event->getArgument(1) == 'This nickname is owned by someone else') { 45 $password = $this->getIni('password'); 46 if (! empty($password)) { 47 $this->doPrivmsg('NickServ', 'IDENTIFY ' . $password); 51 if ($this->event->getNick() == 'NickServ') { 52 $message = $this->event->getArgument(1); 53 if ($message == 'This nickname is owned by someone else') { 54 $password = $this->getIni('password'); 55 if (! empty($password)) { 56 $this->doPrivmsg('NickServ', 'IDENTIFY ' . $password); 57 } 58 } elseif (preg_match('/^.*' . $this->nick . '.* has been killed', $message)) { 59 $this->doNick($this->nick); 60 $this->setIni('nick', $this->nick); 48 61 } 49 62 } … … 51 64 52 65 /** 53 * Intercepts a "nickname in use" response in cases where the bot may not 54 * have disconnected properly and a ghost connection still has its nick in 55 * reserve when it tries to reconnect. 66 * Switches to alternate nicks as needed when nick collisions occur. 56 67 * 57 68 * @return void … … 60 71 { 61 72 if ($this->event->getCode() == Phergie_Event_Response::ERR_NICKNAMEINUSE) { 62 $this-> nick++;63 $altnick = $this->getIni('altnick' . $this-> nick);73 $this->index++; 74 $altnick = $this->getIni('altnick' . $this->index); 64 75 if ($altnick) { 65 76 $this->doNick($altnick); … … 70 81 71 82 /** 72 * Handles communications with NickServ to terminate ghost connections and 73 * recover the bot's nick. 83 * Provides a command to terminate ghost connections. 74 84 * 75 85 * @return void 76 86 */ 77 /*public function onPrivmsg()87 public function onDoGhostbust() 78 88 { 79 89 $password = $this->getIni('password'); 80 if ($this->event->getNick() == 'NickServ' 81 && ! empty ($password)) { 82 $msg = $this->event->getArgument(1); 83 if (preg_match('/The nickname .* has been recovered/', $msg)) { 84 $this->doPrivmsg( 85 'NickServ', 86 'RELEASE ' . $this->getIni('nick') . ' ' . $password 87 ); 88 } elseif (preg_match('/The nickname .* has been released/', $msg)) { 89 $this->doNick($this->getIni('nick')); 90 } 90 91 if (!empty ($password) && $this->index != -1) { 92 $this->doPrivmsg( 93 'NickServ', 94 'GHOST ' . $this->nick . ' ' . $password 95 ); 91 96 } 92 } */97 } 93 98 } trunk/Phergie/Plugin/Pong.php
r59 r61 13 13 { 14 14 /** 15 * Hostname for the bot 16 * 17 * @var string 18 */ 19 protected $hostname; 20 21 /** 22 * Stores the hostname for the bot to use in PONG responses. 23 * 24 * @return void 25 */ 26 public function init() 27 { 28 $this->hostname = gethostbyaddr(file_get_contents('http://whatismyip.com/automation/n09230945.asp')); 29 } 30 31 /** 15 32 * Responds to PING requests from the server to indicate that the client 16 33 * connection is still active. … … 20 37 public function onPing() 21 38 { 22 $this->doPong($this-> event->getArgument(0));39 $this->doPong($this->hostname); 23 40 } 24 41 } trunk/Phergie/Plugin/Url.php
r60 r61 110 110 $out = strtr($out, 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ', 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYPYaaaaaaaceeeeiiiidnoooooouuuuypy'); 111 111 $out = preg_replace('{[^a-z0-9&|"#\'\{\}()§^!°\[\]$*¨µ£%´`~=+:/;.,?><\\ _-]}i', '', $out); 112 $out = trim($out); 112 113 if($trim > 0) { 113 114 $out = substr($out, 0, $trim) . (strlen($out) > $trim ? '...' : '');