Changeset 248
- Timestamp:
- 04/27/08 01:45:15 (7 months ago)
- Files:
-
- trunk/Phergie/Plugin/Convert.php (modified) (1 diff)
- trunk/Phergie/Plugin/Drink.php (modified) (17 diffs)
- trunk/Phergie/Plugin/Spellcheck.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Phergie/Plugin/Convert.php
r217 r248 55 55 $text = $result->item(0)->nodeValue; 56 56 57 $this->doPrivmsg($this->event->getSource(), $target . ': ' . $text); 57 if (empty($text)) { 58 $this->doNotice($target, 'Computation error, nothing was returned, perhaps division by zero?'); 59 } else { 60 $this->doPrivmsg($this->event->getSource(), $target . ': ' . $text); 61 } 58 62 } 59 63 } trunk/Phergie/Plugin/Drink.php
r232 r248 100 100 $names = array(); 101 101 foreach($matches[1] as $key => $name) { 102 $name = $this->decodeTranslit($name); 102 103 if ($this->hasBadChars($name) || strpos($name, '(discontinued)') !== false) { 103 104 continue; … … 131 132 preg_match_all('/db\\/drink\\/[0-9]+">([^<]+)/', $contents, $matches); 132 133 foreach($matches[1] as $name) { 134 $name = $this->decodeTranslit($name); 133 135 if ($this->hasBadChars($name)) { 134 136 continue; … … 161 163 $names = array(); 162 164 foreach($matches[2] as $name) { 165 $name = $this->decodeTranslit($name); 166 if ($this->hasBadChars($name)) { 167 continue; 168 } 163 169 $name = html_entity_decode(trim(preg_replace('/ \\([^)]+\\)| - .*$/', '', $name))); 164 170 if (!preg_match('/(?:^|\s+)(?:' . implode('|', $filter) . ')(?:\s+|$)/i', $name)) { … … 177 183 $this->debug('Parsing data for: Tea'); 178 184 foreach($names as $key => $value) { 179 $names[$key] = ucwords(rtrim($value)); 185 $value = $this->decodeTranslit($value); 186 if ($this->hasBadChars($value)) { 187 continue; 188 } 189 $names[$key] = ucwords(trim($value)); 180 190 } 181 191 $this->populateTable('tea', $names); … … 216 226 { 217 227 $table = $this->db->query('SELECT COUNT(*) FROM sqlite_master WHERE name = ' . $this->db->quote($name))->fetchColumn(); 218 219 228 if (!$table) { 220 229 return true; … … 262 271 263 272 /** 273 * Returns a random record value from a given search pattern. 274 * 275 * @string $table Name of the table 276 * @return string Value of the name column for the selected record 277 */ 278 private function getSearchRecord($table, $search) 279 { 280 $search = sqlite_escape_string(str_replace(array('\\', '%'), array('\\\\', '\\%'), $search)); 281 return $this->db->query('SELECT name FROM ' . $table . ' WHERE name LIKE \'%' . $search . '%\' ESCAPE "\\" ORDER BY RANDOM() LIMIT 1')->fetchColumn(); 282 } 283 284 /** 264 285 * Returns whether or not a given value has characters that may not be 265 286 * displayed correctly. … … 283 304 $target = rtrim(trim($target), '.?!'); 284 305 285 switch ( $target) {306 switch (trim(strtolower($target))) { 286 307 case 'me': 287 308 $target = $this->event->getNick(); … … 289 310 290 311 case 'you': 312 case 'your self': 313 case 'yourself': 291 314 case $this->getIni('nick'): 292 315 $gender = $this->getIni('gender'); … … 307 330 * 308 331 * @param string $type Type of drink 309 * @param string $ target Target to receive the drink310 * @return void 311 */ 312 protected function throwDrink($type, $target)332 * @param string $message Drink reuest message 333 * @return void 334 */ 335 protected function handleDrink($type, $message) 313 336 { 314 337 if (!$this->db) { … … 316 339 } 317 340 341 $message = preg_replace('/\s+/', ' ', trim($message)); 342 preg_match('/^(.+?)(?:\s+an?(?:\s+(?:cuppa|cup\s+of))?\s+(.+?))?(\s+(?:for|because)\s+.+)?$/', $message, $m); 343 list(, $target, $drink, $action) = array_pad($m, 4, null); 344 345 $drink = trim($drink); 346 if ($type == 'tea' && strtolower(substr($drink, -3)) == 'tea') { 347 $drink = trim(substr($drink, 0, -3)); 348 } 349 350 $action = trim($action); 318 351 $target = $this->resolveTarget($target); 319 $drink = $this->getRandomRecord($type); 352 if (!empty($drink)) { 353 $drink = implode(' ', array_map('ucfirst', explode(' ', $drink))); 354 if ($search = $this->getSearchRecord($type, $drink)) { 355 $drink = $search; 356 } 357 } else { 358 $drink = $this->getRandomRecord($type); 359 } 320 360 321 361 if ($drink) { … … 325 365 $text .= 'n'; 326 366 } 327 $text .= ' ' . $drink ;367 $text .= ' ' . $drink . ($action ? ' ' . $action : ''); 328 368 } else { 329 369 // One must be gentle with tea 330 $text = 'pours ' . $target . ' a cup of ' . $drink . ' tea' ;370 $text = 'pours ' . $target . ' a cup of ' . $drink . ' tea' . ($action ? ' ' . $action : ''); 331 371 } 332 372 $this->doAction($this->event->getSource(), $text . '.'); … … 340 380 * @return void 341 381 */ 342 public function onDoBeer($ target)343 { 344 $this-> throwDrink('beer', $target);382 public function onDoBeer($message) 383 { 384 $this->handleDrink('beer', $message); 345 385 } 346 386 … … 351 391 * @return void 352 392 */ 353 public function onDoBooze($ target)354 { 355 $this-> throwDrink('beer', $target);393 public function onDoBooze($message) 394 { 395 $this->handleDrink('beer', $message); 356 396 } 357 397 … … 362 402 * @return void 363 403 */ 364 public function onDoCocktail($ target)365 { 366 $this-> throwDrink('cocktail', $target);404 public function onDoCocktail($message) 405 { 406 $this->handleDrink('cocktail', $message); 367 407 } 368 408 … … 373 413 * @return void 374 414 */ 375 public function onDoCoke($ target)376 { 377 $this-> throwDrink('coke', $target);415 public function onDoCoke($message) 416 { 417 $this->handleDrink('coke', $message); 378 418 } 379 419 … … 384 424 * @return void 385 425 */ 386 public function onDoSoda($target) 387 { 388 $this->throwDrink('coke', $target); 426 public function onDoSoda($message) 427 { 428 $this->handleDrink('coke', $message); 429 } 430 431 /** 432 * Handles tea requests. 433 * 434 * @param string $target Target for the request 435 * @return void 436 */ 437 public function onDoTea($message) 438 { 439 $this->handleDrink('tea', $message); 389 440 } 390 441 … … 401 452 $this->doAction($this->event->getSource(), 'lays ' . $target . ' out flat.'); 402 453 } 403 404 /**405 * Handles tea requests.406 *407 * @param string $target Target for the request408 * @return void409 */410 public function onDoTea($target)411 {412 $this->throwDrink('tea', $target);413 }414 454 } trunk/Phergie/Plugin/Spellcheck.php
r237 r248 8 8 class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract_Base 9 9 { 10 /** 11 * Indicates that a local directory is required for this plugin 12 * 13 * @var bool 14 */ 15 protected $needsDir = true; 16 10 17 /** 11 18 * Spell check dictionary handler … … 30 37 public function onInit() 31 38 { 32 $this->pspell = pspell_new($this->getPluginIni('lang')); 39 $config = pspell_config_create($this->getPluginIni('lang')); 40 pspell_config_personal($config, $this->dir . 'custom.pws'); 41 pspell_config_repl($config, $this->dir . 'custom.repl'); 42 $this->pspell = pspell_new_config($config); 43 33 44 $this->limit = $this->getPluginIni('limit'); 34 45 if (!$this->limit) { … … 71 82 $exp = '(?:(?:' . $bot . '\s*[:,>]?\s+(?:' . $prefix . ')?)|(?:' . $prefix . '))'; 72 83 84 // Do spell checking of the given word 73 85 if (preg_match('#(?:^' . $exp . 'spell(?:check)?\s+(\S+)|(\S+)\s*\(sp\??\))#i', $message, $m)) { 74 86 $word = (!empty($m[1]) ? $m[1] : $m[2]); … … 84 96 $this->doPrivmsg($source, $target . ': The word ' . $word . ' seems to be spelled correctly.'); 85 97 } 98 // Check to see if if someone is trying to add a word or an replacement to the custom dictionary. 99 } elseif (preg_match('#^' . $exp . 'add(word|repl(?:ace(?:ment)?)?)\s+(\S+)(?:\s+(\S+))?#i', $message, $m)) { 100 if ($this->fromAdmin()) { 101 $m = array_pad($m, 4, null); 102 $addWord = (substr(strtolower($m[1]), 0, 4) == 'word'); 103 $correct = ($addWord ? $m[2] : $m[3]); 104 $mispelled = ($addWord ? $m[3] : $m[2]); 105 106 // Check to see if the correct word is empty or not, its required in both cases 107 if (empty($correct)) { 108 return; 109 } 110 111 // pSpell doesn't like hyphenated words so check for them 112 if (strpos($correct, '-') !== false || strpos($mispelled, '-') !== false) { 113 $this->doNotice($target, 'You can not add hyphenated words to the dictionary.'); 114 return; 115 } 116 117 // Check to see if the given word is in the dictionary already 118 if (!pspell_check($this->pspell, $m[2])) { 119 if ($addWord) { 120 if (pspell_add_to_personal($this->pspell, $correct)) { 121 pspell_save_wordlist($this->pspell); 122 $this->doNotice($target, 'Added the word "' . $correct . '" to the personal dictionary.'); 123 } else { 124 $this->doNotice($target, 'Could not add the word "' . $correct . '" to the personal dictionary.'); 125 } 126 } else { 127 // Check to see if both the mispelled and correct word start with the same letter 128 if (substr(strtolower($mispelled), 0, 1) != substr(strtolower($correct), 0, 1)) { 129 $this->doNotice($target, 'Both the correct and mispelled word of the replacement need to start with the same letter.'); 130 } 131 132 if (pspell_store_replacement($this->pspell, $mispelled, $correct)) { 133 pspell_save_wordlist($this->pspell); 134 $this->doNotice($target, 'Added the replacement "' . $correct . '" for the word "' . $mispelled . '".'); 135 } else { 136 $this->doNotice($target, 'Could not add the replacement "' . $correct . '" for the word "' . $mispelled . '".'); 137 } 138 } 139 } else { 140 $this->doNotice($target, 'The word "' . $m[2] . '" seems to be in the dictionary already.'); 141 } 142 } else { 143 $this->doNotice($target, 'You do not have permission to add words to the dictionary.'); 144 } 86 145 } 87 146 }