" seen.py: written by sklnd in about two beers July 2009"

import time

from util import hook, timesince


def db_init(db):
    "check to see that our db has the the seen table and return a connection."
    db.execute("create table if not exists seen(name, time, quote, chan, "
               "primary key(name, chan))")
    db.commit()


@hook.singlethread
@hook.event('PRIVMSG', ignorebots=False)
def seeninput(paraml, input=None, db=None, bot=None):
    db_init(db)
    db.execute("insert or replace into seen(name, time, quote, chan)"
               "values(?,?,?,?)", (input.nick.lower(), time.time(), input.msg,
                                   input.chan))
    db.commit()


@hook.command
def seen(inp, nick='', chan='', db=None, input=None):
    ".seen <nick> -- Tell when a nickname was last in active in irc"

    inp = inp.lower()

    if input.conn.nick.lower() == inp:
        # user is looking for us, being a smartass
        return "You need to get your eyes checked."

    if inp == nick.lower():
        return "Have you looked in a mirror lately?"

    db_init(db)

    last_seen = db.execute("select name, time, quote from seen where"
                           " name = ? and chan = ?", (inp, chan)).fetchone()

    if last_seen:
        reltime = timesince.timesince(last_seen[1])
        if last_seen[0] != inp.lower():  # for glob matching
            inp = last_seen[0]
        if last_seen[2][0:1] == "\x01":
            return '%s was last seen %s ago: *%s %s*' % \
                (inp, reltime, inp, last_seen[2][8:-1])
        else:
            return '%s was last seen %s ago saying: %s' % \
                (inp, reltime, last_seen[2])
    else:
        return "I've never seen %s" % inp

@@ -63,16 +63,32 @@
 ort time
+%0Aimport unittest
 %0A%0Afrom u
@@ -1318,100 +1318,8 @@
 1%5D)%0A
-        if last_seen%5B0%5D != inp.lower():  # for glob matching%0A            inp = last_seen%5B0%5D%0A
     
@@ -1589,32 +1589,32 @@
 n%5B2%5D)%0A    else:%0A
-
         return %22
@@ -1639,8 +1639,1353 @@
 %22 %25 inp%0A
+%0A%0Aclass SeenTest(unittest.TestCase):%0A    class Mock(object):%0A        def __init__(self, **kwargs):%0A            self.__dict__.update(kwargs)%0A%0A    def setUp(self):%0A        import sqlite3%0A        self.db = sqlite3.connect(':memory:')%0A%0A    def seeninput(self, nick, msg, chan='#test'):%0A        seeninput(None, db=self.db,%0A                  input=self.Mock(nick=nick, msg=msg, chan=chan))%0A%0A    def seen(self, inp, nick='bob', chan='#test', bot_nick='skybot'):%0A        return seen(inp, nick=nick, chan=chan, db=self.db,%0A                    input=self.Mock(conn=self.Mock(nick=bot_nick)))%0A%0A    def test_missing(self):%0A        assert %22I've never seen nemo%22 in self.seen('NEMO')%0A%0A    def test_seen(self):%0A        self.seeninput('nemo', 'witty banter')%0A        assert 'nemo was last seen' in self.seen('nemo')%0A        assert 'witty banter' in self.seen('nemo')%0A%0A    def test_seen_missing_channel(self):%0A        self.seeninput('nemo', 'msg', chan='#secret')%0A        assert 'never seen' in self.seen('nemo')%0A%0A    def test_seen_ctcp(self):%0A        self.seeninput('nemo', '%5Cx01ACTION test lol%5Cx01')%0A        assert self.seen('nemo').endswith('ago: *nemo test lol*')%0A%0A    def test_snark_eyes(self):%0A        assert 'eyes checked' in self.seen('skybot', bot_nick='skybot')%0A%0A    def test_snark_mirror(self):%0A        assert 'mirror' in self.seen('bob', nick='bob')%0A

add test