From ca07e6ce8998cdb01b338e03cc6280aa4a01a169 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Sun, 16 Mar 2014 03:10:55 -0500 Subject: [PATCH] [Logger] Add seen functionality --- Logger/plugin.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ Logger/storage.py | 25 +++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/Logger/plugin.py b/Logger/plugin.py index ad56922..57f35c4 100644 --- a/Logger/plugin.py +++ b/Logger/plugin.py @@ -30,6 +30,63 @@ class Logger(callbacks.Plugin): self.db = LogDB(filename) world.flushers.append(self.flush) + def _seen(self, irc, msg, channel, nick, findAny): + if nick == irc.nick: + irc.reply("Of course I've seen myself!") + return + bufid = self.db.getBuffer(irc.network, channel)["id"] + if findAny: + entry = self.db.getLast(bufid, nick) + else: + entry = self.db.getLastMessage(bufid, nick) + if not entry: + irc.reply("I haven't seen %s in %s." % (nick, channel)) + return + t = time.time() + rpl = "I saw %s in %s %s ago" % ( + nick, + channel, + utils.timeElapsed(t - entry["timestamp"]) + ) + tp = entry["type"] + if tp == MessageType.privmsg or\ + tp == MessageType.notice or\ + tp == MessageType.action: + rpl += " saying \"%s\"" % (entry["message"],) + elif tp == MessageType.join: + rpl += " joining." + elif tp == MessageType.part: + rpl += " parting (%s)" % (entry["message"],) + elif tp == MessageType.quit: + rpl += " quiting (%s)" % (entry["message"],) + elif tp == MessageType.kick: + rpl += " kicking %s (%s)" % ( + entry["message"].split(' ')[0], + entry["message"].split(' ')[1:]) + elif tp == MessageType.nick: + rpl += " changing nick to %s" % (entry["message"],) + elif tp == MessageType.mode: + rpl += " setting mode %s" % (entry["message"],) + elif tp == MessageType.topic: + rpl += " setting the topic to \"%s\"" % (entry["message"],) + irc.reply(rpl) + + def seen(self, irc, msg, args, channel, nick): + """[channel] + + Finds the last time a nick was seen speaking and what they said. + """ + self._seen(irc, msg, channel, nick, False) + seen = wrap(seen, ["channel", "nick"]) + + def seenany(self, irc, msg, args, channel, nick): + """[channel] + + Finds the last time a nick was seen and what they were doing. + """ + self._seen(irc, msg, channel, nick, True) + seenany = wrap(seenany, ["channel", "nick"]) + def __call__(self, irc, msg): try: self.__parent.__call__(irc, msg) diff --git a/Logger/storage.py b/Logger/storage.py index 80e63e4..55de835 100644 --- a/Logger/storage.py +++ b/Logger/storage.py @@ -171,3 +171,28 @@ class LogDB: ORDER BY id ASC """).fetchall() + def getLast(self, bufid, nick): + with self.lock: + return self.cur.execute(""" + SELECT * FROM log + INNER JOIN sender ON sender.id=log.senderid + WHERE + log.bufferid = ? AND + sender.nick = ? + ORDER BY id DESC + LIMIT 1 + """, (bufid, nick)).fetchone() + + def getLastMessage(self, bufid, nick): + with self.lock: + return self.cur.execute(""" + SELECT * FROM log + INNER JOIN sender ON sender.id=log.senderid + WHERE + log.bufferid = ? AND + log.type BETWEEN 0 AND 2 AND + sender.nick = ? + ORDER BY id DESC + LIMIT 1 + """, (bufid, nick)).fetchone() +