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
39 * Spell check dictionary handler
46 * Limit on the number of potential correct spellings returned
53 * Check for dependencies.
57 public function onLoad()
59 if (!extension_loaded('pspell')) {
60 $this->fail('pspell php extension is required');
63 if (!$this->getConfig('spellcheck.lang')) {
64 $this->fail('Setting spellcheck.lang must be filled-in');
67 $this->plugins->getPlugin('Command');
69 set_error_handler(array($this, 'loadDictionaryError'));
70 $this->pspell = pspell_new($this->getConfig('spellcheck.lang'));
71 restore_error_handler();
73 $this->limit = $this->getConfig('spellcheck.limit', 5);
77 * Intercepts and handles requests for spell checks.
79 * @param string $word the string to perform checks against
83 public function onCommandSpell($word)
85 $source = $this->event->getSource();
86 $target = $this->event->getNick();
88 $message = $target . ': The word "' . $word;
89 $message .= '" seems to be spelt correctly.';
90 if (!pspell_check($this->pspell, $word)) {
91 $suggestions = pspell_suggest($this->pspell, $word);
94 $message .= ': I could not find any suggestions for "' . $word . '".';
95 if (!empty($suggestions)) {
96 $suggestions = array_splice($suggestions, 0, $this->limit);
97 $message = $target . ': Suggestions for "';
98 $message .= $word . '": ' . implode(', ', $suggestions) . '.';
102 $this->doPrivmsg($source, $message);
106 * Handle any errors from loading dictionary
108 * @param integer $errno Error code
109 * @param string $errstr Error message
110 * @param string $errfile File that errored
111 * @param integer $errline Line where the error happened
115 protected function loadDictionaryError($errno, $errstr, $errfile, $errline)
117 $this->fail($errstr);