Assembla home | Assembla project page
 

Changeset 248

Show
Ignore:
Timestamp:
04/27/08 01:45:15 (7 months ago)
Author:
Slynderdale
Message:

Various additions and fixes.
Convert: Handles invalid conversions now, if google returns nothing, show an error instead of an empty message
Drink: Allow people to specify what type of drink to server otherwise select at RandomError?
Spellcheck: Added addword and addreplacement so ops and amins can add words and replacements to a custom dictionary

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Plugin/Convert.php

    r217 r248  
    5555        $text = $result->item(0)->nodeValue; 
    5656 
    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        } 
    5862    } 
    5963} 
  • trunk/Phergie/Plugin/Drink.php

    r232 r248  
    100100                    $names = array(); 
    101101                    foreach($matches[1] as $key => $name) { 
     102                        $name = $this->decodeTranslit($name); 
    102103                        if ($this->hasBadChars($name) || strpos($name, '(discontinued)') !== false) { 
    103104                            continue; 
     
    131132                    preg_match_all('/db\\/drink\\/[0-9]+">([^<]+)/', $contents, $matches); 
    132133                    foreach($matches[1] as $name) { 
     134                        $name = $this->decodeTranslit($name); 
    133135                        if ($this->hasBadChars($name)) { 
    134136                            continue; 
     
    161163                    $names = array(); 
    162164                    foreach($matches[2] as $name) { 
     165                        $name = $this->decodeTranslit($name); 
     166                        if ($this->hasBadChars($name)) { 
     167                            continue; 
     168                        } 
    163169                        $name = html_entity_decode(trim(preg_replace('/ \\([^)]+\\)| - .*$/', '', $name))); 
    164170                        if (!preg_match('/(?:^|\s+)(?:' . implode('|', $filter) . ')(?:\s+|$)/i', $name)) { 
     
    177183                    $this->debug('Parsing data for: Tea'); 
    178184                    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)); 
    180190                    } 
    181191                    $this->populateTable('tea', $names); 
     
    216226    { 
    217227        $table = $this->db->query('SELECT COUNT(*) FROM sqlite_master WHERE name = ' . $this->db->quote($name))->fetchColumn(); 
    218  
    219228        if (!$table) { 
    220229            return true; 
     
    262271 
    263272    /** 
     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    /** 
    264285     * Returns whether or not a given value has characters that may not be 
    265286     * displayed correctly. 
     
    283304        $target = rtrim(trim($target), '.?!'); 
    284305 
    285         switch ($target) { 
     306        switch (trim(strtolower($target))) { 
    286307            case 'me': 
    287308                $target = $this->event->getNick(); 
     
    289310 
    290311            case 'you': 
     312            case 'your self': 
     313            case 'yourself': 
    291314            case $this->getIni('nick'): 
    292315                $gender = $this->getIni('gender'); 
     
    307330     * 
    308331     * @param string $type Type of drink 
    309      * @param string $target Target to receive the drink 
    310      * @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
    313336    { 
    314337        if (!$this->db) { 
     
    316339        } 
    317340 
     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); 
    318351        $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        } 
    320360 
    321361        if ($drink) { 
     
    325365                    $text .= 'n'; 
    326366                } 
    327                 $text .= ' ' . $drink
     367                $text .= ' ' . $drink . ($action ? ' ' . $action : '')
    328368            } else { 
    329369                // 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 : '')
    331371            } 
    332372            $this->doAction($this->event->getSource(), $text . '.'); 
     
    340380     * @return void 
    341381     */ 
    342     public function onDoBeer($target
    343     { 
    344         $this->throwDrink('beer', $target); 
     382    public function onDoBeer($message
     383    { 
     384        $this->handleDrink('beer', $message); 
    345385    } 
    346386 
     
    351391     * @return void 
    352392     */ 
    353     public function onDoBooze($target
    354     { 
    355         $this->throwDrink('beer', $target); 
     393    public function onDoBooze($message
     394    { 
     395        $this->handleDrink('beer', $message); 
    356396    } 
    357397 
     
    362402     * @return void 
    363403     */ 
    364     public function onDoCocktail($target
    365     { 
    366         $this->throwDrink('cocktail', $target); 
     404    public function onDoCocktail($message
     405    { 
     406        $this->handleDrink('cocktail', $message); 
    367407    } 
    368408 
     
    373413     * @return void 
    374414     */ 
    375     public function onDoCoke($target
    376     { 
    377         $this->throwDrink('coke', $target); 
     415    public function onDoCoke($message
     416    { 
     417        $this->handleDrink('coke', $message); 
    378418    } 
    379419 
     
    384424     * @return void 
    385425     */ 
    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); 
    389440    } 
    390441 
     
    401452        $this->doAction($this->event->getSource(), 'lays ' . $target . ' out flat.'); 
    402453    } 
    403  
    404     /** 
    405      * Handles tea requests. 
    406      * 
    407      * @param string $target Target for the request 
    408      * @return void 
    409      */ 
    410     public function onDoTea($target) 
    411     { 
    412         $this->throwDrink('tea', $target); 
    413     } 
    414454} 
  • trunk/Phergie/Plugin/Spellcheck.php

    r237 r248  
    88class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract_Base 
    99{ 
     10    /** 
     11     * Indicates that a local directory is required for this plugin 
     12     * 
     13     * @var bool 
     14     */ 
     15    protected $needsDir = true; 
     16 
    1017    /** 
    1118     * Spell check dictionary handler 
     
    3037    public function onInit() 
    3138    { 
    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 
    3344        $this->limit = $this->getPluginIni('limit'); 
    3445        if (!$this->limit) { 
     
    7182        $exp = '(?:(?:' . $bot . '\s*[:,>]?\s+(?:' . $prefix . ')?)|(?:' . $prefix . '))'; 
    7283 
     84        // Do spell checking of the given word 
    7385        if (preg_match('#(?:^' . $exp . 'spell(?:check)?\s+(\S+)|(\S+)\s*\(sp\??\))#i', $message, $m)) { 
    7486            $word = (!empty($m[1]) ? $m[1] : $m[2]); 
     
    8496                $this->doPrivmsg($source, $target . ': The word ' . $word . ' seems to be spelled correctly.'); 
    8597            } 
     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            } 
    86145        } 
    87146    }