]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/SpellCheck.php
Bot responds to channel commands via PM
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / SpellCheck.php
1 <?php
2 /**
3  * Phergie 
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie 
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
20  */
21
22 /**
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.
26  *
27  * @category Phergie 
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
34  */
35 class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
36 {
37
38     /**
39      * Spell check dictionary handler
40      *
41      * @var resource
42      */
43     protected $pspell;
44
45     /**
46      * Limit on the number of potential correct spellings returned
47      *
48      * @var int
49      */
50     protected $limit;
51
52     /**
53      * Check for dependencies.
54      *
55      * @return void
56      */
57     public function onLoad()
58     {
59         if (!extension_loaded('pspell')) {
60             $this->fail('pspell php extension is required');
61         }
62
63         if (!$this->getConfig('spellcheck.lang')) {
64             $this->fail('Setting spellcheck.lang must be filled-in');
65         }
66
67         $this->plugins->getPlugin('Command');
68        
69         set_error_handler(array($this, 'loadDictionaryError'));
70         $this->pspell = pspell_new($this->getConfig('spellcheck.lang'));
71         restore_error_handler();
72
73         $this->limit = $this->getConfig('spellcheck.limit', 5);
74     }
75
76     /**
77      * Intercepts and handles requests for spell checks.
78      *
79      * @param string $word the string to perform checks against
80      *
81      * @return void
82      */
83     public function onCommandSpell($word)
84     {
85         $source = $this->event->getSource();
86         $target = $this->event->getNick();
87
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);
92            
93             $message  = $target; 
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) . '.';
99             }
100         }
101          
102         $this->doPrivmsg($source, $message);
103     }
104
105     /**
106      * Handle any errors from loading dictionary
107      *
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
112      *
113      * @return void
114      */
115     protected function loadDictionaryError($errno, $errstr, $errfile, $errline)
116     {
117         $this->fail($errstr);
118     }
119
120 }