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