summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Plugin/Censor.php
blob: 99c69d873452248604f5caf15a7a133f8cc63950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
 * Phergie
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie
 * @package   Phergie_Plugin_Censor
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie_Plugin_Censor
 */

/**
 * Facilitates censoring of event content or discardment of events
 * containing potentially offensive phrases depending on the value of the
 * configuration setting censor.mode ('off', 'censor', 'discard'). Also
 * provides access to a web service for detecting censored words so that
 * other plugins may optionally integrate and adjust behavior accordingly to
 * prevent discardment of events.
 *
 * @category Phergie
 * @package  Phergie_Plugin_Censor
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie_Plugin_Censor
 * @uses     extension soap
 */
class Phergie_Plugin_Censor extends Phergie_Plugin_Abstract
{
    /**
     * SOAP client to interact with the CDYNE Profanity Filter API
     *
     * @var SoapClient
     */
    protected $soap;

    /**
     * Checks for dependencies.
     *
     * @return void
     */
    public function onLoad()
    {
        if (!extension_loaded('soap')) {
            $this->fail('The PHP soap extension is required');
        }

        if (!in_array($this->config['censor.mode'], array('censor', 'discard'))) {
            $this->plugins->removePlugin($this);
        }
    }

    /**
     * Returns a "clean" version of a given string.
     *
     * @param string $string String to clean
     *
     * @return string Cleaned string
     */
    public function cleanString($string)
    {
        if (empty($this->soap)) {
            $this->soap = new SoapClient('http://ws.cdyne.com/ProfanityWS/Profanity.asmx?wsdl');
        }
        $params = array('Text' => $string);
        $attempts = 0;
        while ($attempts < 3) {
            try {
                $response = $this->soap->SimpleProfanityFilter($params);
                break;
            } catch (SoapFault $e) {
                $attempts++;
                sleep(1);
            }
        }
        if ($attempts == 3) {
            return $string;
        }
        return $response->SimpleProfanityFilterResult->CleanText;
    }

    /**
     * Processes events before they are dispatched and either censors their
     * content or discards them if they contain potentially offensive
     * content.
     *
     * @return void
     */
    public function preDispatch()
    {
        $events = $this->events->getEvents();

        foreach ($events as $event) {
            switch ($event->getType()) {
                case Phergie_Event_Request::TYPE_PRIVMSG:
                case Phergie_Event_Request::TYPE_ACTION:
                case Phergie_Event_Request::TYPE_NOTICE:
                    $text = $event->getArgument(1);
                    $clean = $this->cleanString($text);
                    if ($text != $clean) {
                        if ($this->config['censor.mode'] == 'censor') {
                            $event->setArgument(1, $clean);
                        } else {
                            $this->events->removeEvent($event);
                        }
                    }
                    break;
            }
        }
    }
}