9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_TerryChay
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_TerryChay
23 * Handles requests for checking spelling of specified words and returning
24 * either confirmation of correctly spelled words or potential correct
25 * spellings for misspelled words.
28 * @package Phergie_Plugin_SpellCheck
29 * @author Phergie Development Team <team@phergie.org>
30 * @license http://phergie.org/license New BSD License
31 * @link http://pear.phergie.org/package/Phergie_Plugin_TerryChay
32 * @uses Phergie_Plugin_Command pear.phergie.org
33 * @uses extension pspell
35 class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
38 * Spell check dictionary handler
45 * Limit on the number of potential correct spellings returned
52 * Check for dependencies.
56 public function onLoad()
58 if (!extension_loaded('pspell')) {
59 $this->fail('pspell php extension is required');
62 if (!$this->getConfig('spellcheck.lang')) {
63 $this->fail('Setting spellcheck.lang must be filled-in');
66 $this->plugins->getPlugin('Command');
68 set_error_handler(array($this, 'loadDictionaryError'));
69 $this->pspell = pspell_new($this->getConfig('spellcheck.lang'));
70 restore_error_handler();
72 $this->limit = $this->getConfig('spellcheck.limit', 5);
76 * Intercepts and handles requests for spell checks.
78 * @param string $word the string to perform checks against
82 public function onCommandSpell($word)
84 $source = $this->event->getSource();
85 $target = $this->event->getNick();
87 $message = $target . ': The word "' . $word;
88 $message .= '" seems to be spelled correctly.';
89 if (!pspell_check($this->pspell, $word)) {
90 $suggestions = pspell_suggest($this->pspell, $word);
93 $message .= ': I could not find any suggestions for "' . $word . '".';
94 if (!empty($suggestions)) {
95 $suggestions = array_splice($suggestions, 0, $this->limit);
96 $message = $target . ': Suggestions for "';
97 $message .= $word . '": ' . implode(', ', $suggestions) . '.';
101 $this->doPrivmsg($source, $message);
105 * Handle any errors from loading dictionary
107 * @param integer $errno Error code
108 * @param string $errstr Error message
109 * @param string $errfile File that errored
110 * @param integer $errline Line where the error happened
114 protected function loadDictionaryError($errno, $errstr, $errfile, $errline)
116 $this->fail($errstr);