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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
<?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_Remind
* @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_Remind
*/
/**
* Parses and logs messages that should be relayed to other users the next time
* the recipient is active on the same channel.
*
* @category Phergie
* @package Phergie_Plugin_Remind
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_Remind
* @uses Phergie_Plugin_Command pear.phergie.org
* @uses Phergie_Plugin_Time pear.phergie.org
*/
class Phergie_Plugin_Remind extends Phergie_Plugin_Abstract
{
/**
* Number of reminders to show in public.
*/
protected $publicReminders = 3;
/**
* PDO resource for a SQLite database containing the reminders.
*
* @var resource
*/
protected $db;
/**
* Flag that indicates whether or not to use an in-memory reminder list.
*
* @var bool
*/
protected $keepListInMemory = true;
/**
* In-memory store for pending reminders.
*/
protected $msgStorage = array();
/**
* Check for dependencies.
*
* @return void
*/
public function onLoad()
{
$plugins = $this->getPluginHandler();
$plugins->getPlugin('Command');
$plugins->getPlugin('Time');
}
/**
* Creates the database if it does not already exist.
*
* @return void
*/
public function onConnect()
{
$dir = dirname(__FILE__) . '/' . $this->getName();
$path = $dir . '/reminder.db';
if (!file_exists($dir)) {
mkdir($dir);
}
if (isset($this->config['remind.use_memory'])) {
$this->keepListInMemory = (bool) $this->config['remind.use_memory'];
}
if (isset($this->config['remind.public_reminders'])) {
$this->publicReminders = (int) $this->config['remind.public_reminders'];
$this->publicReminders = max($this->publicReminders, 0);
}
try {
$this->db = new PDO('sqlite:' . $path);
$this->createTables();
} catch (PDO_Exception $e) {
throw new Phergie_Plugin_Exception($e->getMessage());
}
}
/**
* Intercepts a message and processes any contained recognized commands.
*
* @return void
*/
public function onPrivmsg()
{
$source = $this->getEvent()->getSource();
$nick = $this->getEvent()->getNick();
$this->deliverReminders($source, $nick);
}
/**
* Handle reminder requests
*
* @param string $recipient recipient of the message
* @param string $message message to tell the recipient
*
* @return void
* @see handleRemind()
*/
public function onCommandTell($recipient, $message)
{
$this->handleRemind($recipient, $message);
}
/**
* Handle reminder requests
*
* @param string $recipient recipient of the message
* @param string $message message to tell the recipient
*
* @return void
* @see handleRemind()
*/
public function onCommandAsk($recipient, $message)
{
$this->handleRemind($recipient, $message);
}
/**
* Handle reminder requests
*
* @param string $recipient recipient of the message
* @param string $message message to tell the recipient
*
* @return void
* @see handleRemind()
*/
public function onCommandRemind($recipient, $message)
{
$this->handleRemind($recipient, $message);
}
/**
* Handles the tell/remind command (stores the message)
*
* @param string $recipient name of the recipient
* @param string $message message to store
*
* @return void
*/
protected function handleRemind($recipient, $message)
{
$source = $this->getEvent()->getSource();
$nick = $this->getEvent()->getNick();
if (!$this->getEvent()->isInChannel()) {
$this->doPrivmsg($source, 'Reminders must be requested in-channel.');
return;
}
$q = $this->db->prepare(
'INSERT INTO remind
(
time,
channel,
recipient,
sender,
message
)
VALUES
(
:time,
:channel,
:recipient,
:sender,
:message
)'
);
try {
$q->execute(
array(
'time' => date(DATE_RFC822),
'channel' => $source,
'recipient' => strtolower($recipient),
'sender' => strtolower($nick),
'message' => $message
)
);
} catch (PDOException $e) {
}
if ($rowid = $this->db->lastInsertId()) {
$this->doPrivmsg($source, 'ok, ' . $nick . ', message stored');
} else {
$this->doPrivmsg(
$source,
$nick . ': bad things happened. Message not saved.'
);
return;
}
if ($this->keepListInMemory) {
$this->msgStorage[$source][strtolower($recipient)] = $rowid;
}
}
/**
* Determines if the user has pending reminders, and if so, delivers them.
*
* @param string $channel channel to check
* @param string $nick nick to check
*
* @return void
*/
protected function deliverReminders($channel, $nick)
{
if ($channel[0] != '#') {
// private message, not a channel, so don't check
return;
}
// short circuit if there's no message in memory (if allowed)
if ($this->keepListInMemory
&& !isset($this->msgStorage[$channel][strtolower($nick)])
) {
return;
}
// fetch and deliver messages
$reminders = $this->fetchMessages($channel, $nick);
if (count($reminders) > $this->publicReminders) {
$msgs = array_slice($reminders, 0, $this->publicReminders);
$privmsgs = array_slice($reminders, $this->publicReminders);
} else {
$msgs = $reminders;
$privmsgs = false;
}
foreach ($msgs as $msg) {
$ts = $this->plugins->time->getCountdown($msg['time']);
$formatted = sprintf(
'%s: (from %s, %s ago) %s',
$nick, $msg['sender'], $ts, $msg['message']
);
$this->doPrivmsg($channel, $formatted);
$this->deleteMessage($msg['rowid'], $channel, $nick);
}
if ($privmsgs) {
foreach ($privmsgs as $msg) {
$ts = $this->plugins->time->getCountdown($msg['time']);
$formatted = sprintf(
'from %s, %s ago: %s',
$msg['sender'], $ts, $msg['message']
);
$this->doPrivmsg($nick, $formatted);
$this->deleteMessage($msg['rowid'], $channel, $nick);
}
$formatted = sprintf(
'%s: (%d more messages sent in private.)',
$nick, count($privmsgs)
);
$this->doPrivmsg($channel, $formatted);
}
}
/**
* Get pending messages (for a specific channel/recipient)
*
* @param string $channel channel on which to check for pending messages
* @param string $recipient user for which to check pending messages
*
* @return array of records
*/
protected function fetchMessages($channel = null, $recipient = null)
{
if ($channel) {
$qClause = 'WHERE channel = :channel AND recipient LIKE :recipient';
$params = compact('channel', 'recipient');
} else {
$qClause = '';
$params = array();
}
$q = $this->db->prepare(
'SELECT rowid, channel, sender, recipient, time, message
FROM remind ' . $qClause
);
$q->execute($params);
return $q->fetchAll();
}
/**
* Deletes a delivered message
*
* @param int $rowid ID of the message to delete
* @param string $channel message's channel
* @param string $nick message's recipient
*
* @return void
*/
protected function deleteMessage($rowid, $channel, $nick)
{
$nick = strtolower($nick);
$q = $this->db->prepare('DELETE FROM remind WHERE rowid = :rowid');
$q->execute(array('rowid' => $rowid));
if ($this->keepListInMemory) {
if (isset($this->msgStorage[$channel][$nick])
&& $this->msgStorage[$channel][$nick] == $rowid
) {
unset($this->msgStorage[$channel][$nick]);
}
}
}
/**
* Determines if a table exists
*
* @param string $name Table name
*
* @return bool
*/
protected function haveTable($name)
{
$sql = 'SELECT COUNT(*) FROM sqlite_master WHERE name = '
. $this->db->quote($name);
return (bool) $this->db->query($sql)->fetchColumn();
}
/**
* Creates the database table(s) (if they don't exist)
*
* @return void
*/
protected function createTables()
{
if (!$this->haveTable('remind')) {
$this->db->exec(
'CREATE TABLE
remind
(
time INTEGER,
channel TEXT,
recipient TEXT,
sender TEXT,
message TEXT
)'
);
}
}
/**
* Populates the in-memory cache of pending reminders
*
* @return void
*/
protected function populateMemory()
{
if (!$this->keepListInMemory) {
return;
}
foreach ($this->fetchMessages() as $msg) {
$this->msgStorage[$msg['channel']][$msg['recipient']] = $msg['rowid'];
}
}
}
|