| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | class Phergie_Plugin_Remind extends Phergie_Plugin_Abstract_Command |
|---|
| 10 | { |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | const PUBLIC_REMINDERS = 3; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | protected $needsDir = true; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | protected $db = null; |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | protected $keepListInMemory = true; |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | protected $msgStore = array(); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | public function onInit() |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | $this->db = new PDO('sqlite:' . $this->dir . 'remind.db'); |
|---|
| 53 | if (!$this->db) { |
|---|
| 54 | return; |
|---|
| 55 | } |
|---|
| 56 | $this->createTables(); |
|---|
| 57 | $this->populateMemory(); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | public static function checkDependencies(Phergie_Driver_Abstract $client, array $plugins) |
|---|
| 70 | { |
|---|
| 71 | $errors = array(); |
|---|
| 72 | |
|---|
| 73 | if (!extension_loaded('PDO')) { |
|---|
| 74 | $errors[] = 'PDO php extension is required'; |
|---|
| 75 | } |
|---|
| 76 | if (!extension_loaded('pdo_sqlite')) { |
|---|
| 77 | $errors[] = 'pdo_sqlite php extension is required'; |
|---|
| 78 | } |
|---|
| 79 | return empty($errors) ? true : $errors; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | protected function haveTable($name) |
|---|
| 89 | { |
|---|
| 90 | return (bool) $this->db->query( |
|---|
| 91 | 'SELECT COUNT(*) FROM sqlite_master WHERE name = ' . $this->db->quote($name) |
|---|
| 92 | )->fetchColumn(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | protected function createTables() |
|---|
| 101 | { |
|---|
| 102 | if (!$this->haveTable('remind')) { |
|---|
| 103 | $this->debug('Creating the database schema for: remind'); |
|---|
| 104 | $this->db->exec(' |
|---|
| 105 | CREATE TABLE |
|---|
| 106 | remind |
|---|
| 107 | ( |
|---|
| 108 | time INTEGER, |
|---|
| 109 | channel TEXT, |
|---|
| 110 | recipient TEXT, |
|---|
| 111 | sender TEXT, |
|---|
| 112 | message TEXT |
|---|
| 113 | ) |
|---|
| 114 | '); |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | protected function populateMemory() |
|---|
| 124 | { |
|---|
| 125 | if (!$this->keepListInMemory) { |
|---|
| 126 | return; |
|---|
| 127 | } |
|---|
| 128 | $storeCounter = 0; |
|---|
| 129 | foreach ($this->fetchMessages() as $msg) { |
|---|
| 130 | $this->msgStore[$msg['channel']][$msg['recipient']] = $msg['rowid']; |
|---|
| 131 | ++$storeCounter; |
|---|
| 132 | } |
|---|
| 133 | $this->debug("Found {$storeCounter} messages", true); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | protected function fetchMessages($channel = null, $recipient = null) |
|---|
| 144 | { |
|---|
| 145 | if ($channel) { |
|---|
| 146 | $qClause = 'WHERE channel = :channel AND recipient = :recipient'; |
|---|
| 147 | $params = array( |
|---|
| 148 | 'channel' => $channel, |
|---|
| 149 | 'recipient' => strtolower($recipient) |
|---|
| 150 | ); |
|---|
| 151 | } else { |
|---|
| 152 | $qClause = ''; |
|---|
| 153 | $params = array(); |
|---|
| 154 | } |
|---|
| 155 | $q = $this->db->prepare( |
|---|
| 156 | 'SELECT rowid, channel, sender, recipient, time, message |
|---|
| 157 | FROM remind ' . $qClause |
|---|
| 158 | ); |
|---|
| 159 | $q->execute($params); |
|---|
| 160 | return $q->fetchAll(); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | protected function deleteMessage($rowid, $channel, $nick) |
|---|
| 172 | { |
|---|
| 173 | $q = $this->db->prepare('DELETE FROM remind WHERE rowid = :rowid'); |
|---|
| 174 | $q->execute(array('rowid' => $rowid)); |
|---|
| 175 | |
|---|
| 176 | if ($this->keepListInMemory) { |
|---|
| 177 | if (isset($this->msgStore[$channel][$nick]) |
|---|
| 178 | && $this->msgStore[$channel][$nick] == $rowid) { |
|---|
| 179 | unset($this->msgStore[$channel][$nick]); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | protected function getMessage($rowid) |
|---|
| 190 | { |
|---|
| 191 | $q = $this->db->prepare(' |
|---|
| 192 | SELECT rowid, time, channel, recipient, sender, message |
|---|
| 193 | FROM remind |
|---|
| 194 | WHERE rowid = :rowid |
|---|
| 195 | '); |
|---|
| 196 | $q->execute(array('rowid' => $rowid)); |
|---|
| 197 | return $q->fetch(PDO::FETCH_ASSOC); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | protected function handleRemind($recipient, $message) |
|---|
| 208 | { |
|---|
| 209 | $source = $this->event->getSource(); |
|---|
| 210 | $nick = $this->event->getNick(); |
|---|
| 211 | if (!$this->event->isInChannel()) { |
|---|
| 212 | $this->doPrivmsg($source, 'Reminders must be requested in-channel.'); |
|---|
| 213 | return; |
|---|
| 214 | } |
|---|
| 215 | $q = $this->db->prepare(' |
|---|
| 216 | INSERT INTO remind |
|---|
| 217 | ( |
|---|
| 218 | time, |
|---|
| 219 | channel, |
|---|
| 220 | recipient, |
|---|
| 221 | sender, |
|---|
| 222 | message |
|---|
| 223 | ) |
|---|
| 224 | VALUES |
|---|
| 225 | ( |
|---|
| 226 | :time, |
|---|
| 227 | :channel, |
|---|
| 228 | :recipient, |
|---|
| 229 | :sender, |
|---|
| 230 | :message |
|---|
| 231 | ) |
|---|
| 232 | '); |
|---|
| 233 | try { |
|---|
| 234 | $q->execute(array( |
|---|
| 235 | 'time' => date(DATE_RFC822), |
|---|
| 236 | 'channel' => $source, |
|---|
| 237 | 'recipient' => strtolower($recipient), |
|---|
| 238 | 'sender' => strtolower($nick), |
|---|
| 239 | 'message' => $message |
|---|
| 240 | )); |
|---|
| 241 | } catch (PDOException $e) { } |
|---|
| 242 | |
|---|
| 243 | if ($rowid = $this->db->lastInsertId()) { |
|---|
| 244 | $this->doPrivmsg($source, "ok, $nick, message stored"); |
|---|
| 245 | } else { |
|---|
| 246 | $this->doPrivmsg($source, "$nick: bad things happened. Message not saved."); |
|---|
| 247 | return; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | if ($this->keepListInMemory) { |
|---|
| 251 | $this->msgStore[$source][$recipient] = $rowid; |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | protected function deliverReminders($channel, $nick) |
|---|
| 263 | { |
|---|
| 264 | if ($channel[0] != '#') { |
|---|
| 265 | |
|---|
| 266 | return; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | if ($this->keepListInMemory |
|---|
| 271 | && !isset($this->msgStore[$channel][strtolower($nick)])) { |
|---|
| 272 | return; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | $reminders = $this->fetchMessages($channel, $nick); |
|---|
| 277 | if (count($reminders) > self::PUBLIC_REMINDERS) { |
|---|
| 278 | $msgs = array_slice($reminders, 0, self::PUBLIC_REMINDERS); |
|---|
| 279 | $privmsgs = array_slice($reminders, self::PUBLIC_REMINDERS); |
|---|
| 280 | } else { |
|---|
| 281 | $msgs = $reminders; |
|---|
| 282 | $privmsgs = false; |
|---|
| 283 | } |
|---|
| 284 | foreach ($msgs as $msg) { |
|---|
| 285 | $this->doPrivmsg( |
|---|
| 286 | $channel, |
|---|
| 287 | "{$nick}: (from {$msg['sender']}, {$msg['time']}) " . |
|---|
| 288 | $msg['message'] |
|---|
| 289 | ); |
|---|
| 290 | $this->deleteMessage($msg['rowid'], $channel, $nick); |
|---|
| 291 | } |
|---|
| 292 | if ($privmsgs) { |
|---|
| 293 | foreach ($privmsgs as $msg) { |
|---|
| 294 | $this->doPrivmsg( |
|---|
| 295 | $nick, |
|---|
| 296 | "{$nick}: (from {$msg['sender']}, {$msg['time']}) " . |
|---|
| 297 | $msg['message'] |
|---|
| 298 | ); |
|---|
| 299 | $this->deleteMessage($msg['rowid'], $channel, $nick); |
|---|
| 300 | } |
|---|
| 301 | $this->doPrivmsg( |
|---|
| 302 | $channel, |
|---|
| 303 | "{$nick}: (" . count($privmsgs) . " more messages sent in private.)" |
|---|
| 304 | ); |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | public function onPrivmsg() |
|---|
| 318 | { |
|---|
| 319 | $this->deliverReminders($this->event->getSource(), $this->event->getNick()); |
|---|
| 320 | $this->processCommand($this->event->getArgument(1), false, true); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | public function onDoTell($recipient, $message) |
|---|
| 330 | { |
|---|
| 331 | $this->handleRemind($recipient, $message); |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | public function onDoAsk($recipient, $message) |
|---|
| 341 | { |
|---|
| 342 | $this->handleRemind($recipient, $message); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | public function onDoRemind($recipient, $message) |
|---|
| 352 | { |
|---|
| 353 | $this->handleRemind($recipient, $message); |
|---|
| 354 | } |
|---|
| 355 | } |
|---|