2009-07-05 00:52:17 +00:00
|
|
|
" seen.py: written by sklnd in about two beers July 2009"
|
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
import time
|
2015-01-21 07:42:23 +00:00
|
|
|
import unittest
|
2009-07-05 00:52:17 +00:00
|
|
|
|
2009-07-08 17:04:30 +00:00
|
|
|
from util import hook, timesince
|
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
|
2010-03-13 14:24:19 +00:00
|
|
|
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, "
|
2014-01-14 21:12:37 +00:00
|
|
|
"primary key(name, chan))")
|
2010-03-13 14:24:19 +00:00
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
@hook.singlethread
|
2010-08-11 08:31:40 +00:00
|
|
|
@hook.event('PRIVMSG', ignorebots=False)
|
2010-03-13 14:24:19 +00:00
|
|
|
def seeninput(paraml, input=None, db=None, bot=None):
|
2010-02-02 04:42:34 +00:00
|
|
|
db_init(db)
|
|
|
|
db.execute("insert or replace into seen(name, time, quote, chan)"
|
2014-01-14 21:12:37 +00:00
|
|
|
"values(?,?,?,?)", (input.nick.lower(), time.time(), input.msg,
|
|
|
|
input.chan))
|
2010-02-02 04:42:34 +00:00
|
|
|
db.commit()
|
2009-07-05 00:52:17 +00:00
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
|
2009-07-05 00:52:17 +00:00
|
|
|
@hook.command
|
2010-09-20 11:04:45 +00:00
|
|
|
def seen(inp, nick='', chan='', db=None, input=None):
|
2009-11-19 03:20:59 +00:00
|
|
|
".seen <nick> -- Tell when a nickname was last in active in irc"
|
2009-07-05 00:52:17 +00:00
|
|
|
|
2013-09-20 20:12:07 +00:00
|
|
|
inp = inp.lower()
|
|
|
|
|
|
|
|
if input.conn.nick.lower() == inp:
|
2010-11-12 05:18:32 +00:00
|
|
|
# user is looking for us, being a smartass
|
2010-09-20 11:04:45 +00:00
|
|
|
return "You need to get your eyes checked."
|
|
|
|
|
2013-09-20 20:12:07 +00:00
|
|
|
if inp == nick.lower():
|
2009-07-05 00:52:17 +00:00
|
|
|
return "Have you looked in a mirror lately?"
|
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
db_init(db)
|
2009-07-05 00:52:17 +00:00
|
|
|
|
2013-09-20 20:12:07 +00:00
|
|
|
last_seen = db.execute("select name, time, quote from seen where"
|
|
|
|
" name = ? and chan = ?", (inp, chan)).fetchone()
|
2009-07-05 00:52:17 +00:00
|
|
|
|
2010-02-02 04:42:34 +00:00
|
|
|
if last_seen:
|
|
|
|
reltime = timesince.timesince(last_seen[1])
|
2014-01-14 21:12:37 +00:00
|
|
|
if last_seen[2][0:1] == "\x01":
|
2013-02-24 06:13:12 +00:00
|
|
|
return '%s was last seen %s ago: *%s %s*' % \
|
2014-01-14 21:12:37 +00:00
|
|
|
(inp, reltime, inp, last_seen[2][8:-1])
|
2013-02-24 06:13:12 +00:00
|
|
|
else:
|
|
|
|
return '%s was last seen %s ago saying: %s' % \
|
2014-01-14 21:12:37 +00:00
|
|
|
(inp, reltime, last_seen[2])
|
2009-07-05 00:52:17 +00:00
|
|
|
else:
|
2010-02-02 04:42:34 +00:00
|
|
|
return "I've never seen %s" % inp
|
2015-01-21 07:42:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SeenTest(unittest.TestCase):
|
|
|
|
class Mock(object):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.__dict__.update(kwargs)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
import sqlite3
|
|
|
|
self.db = sqlite3.connect(':memory:')
|
|
|
|
|
|
|
|
def seeninput(self, nick, msg, chan='#test'):
|
|
|
|
seeninput(None, db=self.db,
|
|
|
|
input=self.Mock(nick=nick, msg=msg, chan=chan))
|
|
|
|
|
|
|
|
def seen(self, inp, nick='bob', chan='#test', bot_nick='skybot'):
|
|
|
|
return seen(inp, nick=nick, chan=chan, db=self.db,
|
|
|
|
input=self.Mock(conn=self.Mock(nick=bot_nick)))
|
|
|
|
|
|
|
|
def test_missing(self):
|
|
|
|
assert "I've never seen nemo" in self.seen('NEMO')
|
|
|
|
|
|
|
|
def test_seen(self):
|
|
|
|
self.seeninput('nemo', 'witty banter')
|
|
|
|
assert 'nemo was last seen' in self.seen('nemo')
|
|
|
|
assert 'witty banter' in self.seen('nemo')
|
|
|
|
|
|
|
|
def test_seen_missing_channel(self):
|
|
|
|
self.seeninput('nemo', 'msg', chan='#secret')
|
|
|
|
assert 'never seen' in self.seen('nemo')
|
|
|
|
|
|
|
|
def test_seen_ctcp(self):
|
|
|
|
self.seeninput('nemo', '\x01ACTION test lol\x01')
|
|
|
|
assert self.seen('nemo').endswith('ago: *nemo test lol*')
|
|
|
|
|
|
|
|
def test_snark_eyes(self):
|
|
|
|
assert 'eyes checked' in self.seen('skybot', bot_nick='skybot')
|
|
|
|
|
|
|
|
def test_snark_mirror(self):
|
|
|
|
assert 'mirror' in self.seen('bob', nick='bob')
|