root/trunk/Phergie/Plugin/Spellcheck.php @ 113

Revision 113, 2.5 KB (checked in by tobias382, 5 years ago)

* Fixed a bug in the Autojoin plugin where the channel list was not processed correctly when using a space as a delimiter (thanks Slynderdale)
* Modified the Spellcheck plugin to not require a space between the word being checked and the spell check request trigger (thanks Slynderdale)

Line 
1<?php
2
3/**
4* @see Phergie_Plugin_Abstract_Base
5*/
6require_once 'Phergie/Plugin/Abstract/Base.php';
7
8/**
9* Handles requests for checking spelling of specified words and returning
10* either confirmation of correctly spelled words or potential correct
11* spellings for misspelled words.
12*/
13class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract_Base
14{
15    /**
16    * Spell check dictionary handler
17    *
18    * @var resource
19    */
20    protected $pspell;
21   
22    /**
23    * Limit on the number of potential correct spellings returned
24    *
25    * @var int
26    */
27    protected $limit;
28
29    /**
30    * Obtains configuration settings and initializes the spell check
31    * dictionary handler.
32    *
33    * @return void
34    */
35    public function init()
36    {
37        $this->pspell = pspell_new($this->getPluginIni('lang'));
38        $this->limit = $this->getPluginIni('limit');
39        if (!$this->limit) {
40            $this->limit = 5;
41        }
42    }
43   
44    /**
45    * Returns whether or not the plugin's dependencies are met.
46    *
47    * @param Phergie_Driver_Abstract $client Client instance
48    * @param array $plugins List of short names for plugins that the
49    *                       bootstrap file intends to instantiate
50    * @see Phergie_Plugin_Abstract_Base::checkDependencies()
51    * @return bool TRUE if dependencies are met, FALSE otherwise
52    */
53    static public function checkDependencies(Phergie_Driver_Abstract $client, array $plugins)
54    {
55        if (!extension_loaded('pspell')
56            || !$client->getIni('spellcheck.lang')) {
57            return false;
58        }
59       
60        return true;
61    }
62   
63    /**
64    * Intercepts and handles requests for spell checks.
65    *
66    * @return void
67    */
68    public function onPrivmsg()
69    {
70        $source = $this->event->getSource();
71        $message = $this->event->getArgument(1);
72       
73        if (preg_match('#(\S+)\s*\(sp\??\)#i', $message, $m)) {
74            $word = $m[1];
75            if (!pspell_check($this->pspell, $word)) {
76                $suggestions = pspell_suggest($this->pspell, $word);
77                if (empty($suggestions)) {
78                    $this->doPrivmsg($source, 'I could not find any suggestions for '.$word);
79                } else {
80                    $suggestions = array_splice($suggestions, 0, $this->limit);
81                    $this->doPrivmsg($source, 'Suggestions for \''.$word.'\': '.implode(', ', $suggestions));
82                }
83            } else {
84                $this->doPrivmsg($source, 'The word '.$word.' seems to be spelled correctly.');
85            }
86        }
87    }
88}
Note: See TracBrowser for help on using the browser.