summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Plugin/Lart.php
blob: d00cae0dd18c3beb7b7b8300397f78e3a606a1a9 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?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_Lart
 * @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_Lart
 */

/**
 * Accepts terms and corresponding definitions for storage to a local data
 * source and performs and returns the result of lookups for term definitions
 * as they are requested.
 *
 * @category Phergie
 * @package  Phergie_Plugin_Lart
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie_Plugin_Lart
 * @uses     Phergie_Plugin_Command pear.phergie.org
 * @uses     extension PDO
 * @uses     extension pdo_sqlite
 */
class Phergie_Plugin_Lart extends Phergie_Plugin_Abstract
{
    /**
     * PDO instance for the database
     *
     * @var PDO
     */
    protected $db;

    /**
     * Prepared statement for inserting a new definition
     *
     * @var PDOStatement
     */
    protected $save;

    /**
     * Prepared statement for deleting the definition for a given term
     *
     * @var PDOStatement
     */
    protected $delete;

    /**
     * Prepared statement for searching for a definition for which the term
     * matches as a regular expression against a given search string
     *
     * @var PDOStatement
     */
    protected $process;

    /**
     * Prepared statement for searching for a definition by its exact term
     *
     * @var PDOStatement
     */
    protected $select;

    /**
     * Checks for dependencies and initializes the database.
     *
     * @return void
     */
    public function onLoad()
    {
        if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
            $this->fail('PDO and pdo_sqlite extensions must be installed');
        }

        $this->plugins->getPlugin('Command');

        $dir = dirname(__FILE__) . '/' . $this->getName();
        $path = $dir . '/lart.db';
        $exists = file_exists($path);
        if (!$exists) {
            mkdir($dir);
        }

        try {
            $this->db = new PDO('sqlite:' . $path);
        } catch (PDO_Exception $e) {
            throw new Phergie_Plugin_Exception($e->getMessage());
        }

        $this->db->sqliteCreateFunction('preg_match', 'preg_match');

        if (!$exists) {
            $this->db->exec('
                CREATE TABLE lart (
                    name VARCHAR(255),
                    definition TEXT,
                    hostmask VARCHAR(50),
                    tstamp VARCHAR(19)
                )
            ');
            $this->db->exec('
                CREATE UNIQUE INDEX lart_name ON lart (name)
            ');
        }

        $this->save = $this->db->prepare('
            REPLACE INTO lart (name, definition, hostmask, tstamp)
            VALUES (:name, :definition, :hostmask, :tstamp)
        ');

        $this->process = $this->db->prepare('
            SELECT *
            FROM lart
            WHERE preg_match(name, :name)
        ');

        $this->select = $this->db->prepare('
            SELECT *
            FROM lart
            WHERE name = :name
        ');

        $this->delete = $this->db->prepare('
            DELETE FROM lart
            WHERE name = :name
        ');
    }

    /**
     * Retrieves the definition for a given term if it exists.
     *
     * @param string $term Term to search for
     *
     * @return mixed String containing the definition or FALSE if no definition
     *               exists
     */
    protected function getLart($term)
    {
        $this->process->execute(array(':name' => $term));
        $row = $this->process->fetchObject();
        if ($row === false) {
            return false;
        }
        preg_match($row->name, $term, $match);
        $definition = preg_replace(
            "/(?:\\\\|\\$)([0-9]+)/e",
            '$match[\1]',
            $row->definition
        );
        $event = $this->getEvent();
        $definition = str_replace(
            array('$source', '$nick'),
            array($event->getSource(), $event->getNick()),
            $definition
        );
        return $definition;
    }

    /**
     * Deletes a given definition.
     *
     * @param string $term Term for which the definition should be deleted
     *
     * @return boolean TRUE if the definition was found and deleted, FALSE
     *         otherwise
     */
    protected function deleteLart($term)
    {
        $this->delete->execute(array(':name' => $term));
        return ($this->delete->rowCount() > 0);
    }

    /**
     * Saves a given definition.
     *
     * @param string $term       Term to trigger a response containing the
     *        corresponding definition, may be a regular expression
     * @param string $definition Definition corresponding to the term
     *
     * @return boolean TRUE if the definition was saved successfully, FALSE
     *         otherwise
     */
    protected function saveLart($term, $definition)
    {
        $data = array(
            ':name' => $term,
            ':definition' => $definition,
            ':hostmask' => (string) $this->getEvent()->getHostmask(),
            ':tstamp' => time()
        );
        $this->save->execute($data);
        return ($this->save->rowCount() > 0);
    }

    /**
     * Returns information about a definition.
     *
     * @param string $term Term about which to return information
     *
     * @return void
     */
    public function onCommandLartinfo($term)
    {
        $this->select->execute(array(':name' => $term));
        $row = $this->select->fetchObject();
        $msg = $this->getEvent()->getNick() . ': ';
        if (!$row) {
            $msg .= 'Lart not found';
        } else {
            $msg .= 'Term: ' . $row->name
                . ', Definition: ' . $row->definition
                . ', User: ' . $row->hostmask
                . ', Added: ' . date('n/j/y g:i A', $row->tstamp);
        }
        $this->doNotice($this->getEvent()->getSource(), $msg);
    }

    /**
     * Creates a new definition.
     *
     * @param string $term       Term to add
     * @param string $definition Definition to add
     *
     * @return void
     */
    public function onCommandAddlart($term, $definition)
    {
        $result = $this->saveLart($term, $definition);
        if ($result) {
            $msg = 'Lart saved successfully';
        } else {
            $msg = 'Lart could not be saved';
        }
        $this->doNotice($this->getEvent()->getSource(), $msg);
    }

    /**
     * Removes an existing definition.
     *
     * @param string $term Term for which the definition should be removed
     *
     * @return void
     */
    public function onCommandDeletelart($term)
    {
        $source = $this->getEvent()->getSource();
        if ($this->deleteLart($term)) {
            $msg = 'Lart deleted successfully';
        } else {
            $msg = 'Lart not found';
        }
        $this->doNotice($source, $msg);
    }

    /**
     * Processes definition triggers in the text of the current event.
     *
     * @return void
     */
    protected function processLart()
    {
        $lart = $this->getLart($this->getEvent()->getText());
        if ($lart) {
            if (strpos($lart, '/me') === 0) {
                $lart = substr($lart, 4);
                $method = 'doAction';
            } else {
                $method = 'doPrivmsg';
            }
            $this->$method($this->getEvent()->getSource(), $lart);
        }
    }

    /**
     * Processes definition triggers in messages.
     *
     * @return void
     */
    public function onPrivmsg()
    {
        $this->processLart();
    }

    /**
     * Processes definition triggers in CTCP actions.
     *
     * @return void
     */
    public function onAction()
    {
        $this->processLart();
    }
}