2009-07-05 00:52:17 +00:00
|
|
|
" seen.py: written by sklnd in about two beers July 2009"
|
|
|
|
|
|
|
|
import os
|
2009-07-08 17:28:15 +00:00
|
|
|
import time
|
2009-07-08 17:57:02 +00:00
|
|
|
from datetime import datetime
|
2009-07-08 17:28:15 +00:00
|
|
|
import sqlite3
|
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
|
|
|
|
2009-07-08 17:49:45 +00:00
|
|
|
dbname = "skybot.db"
|
2009-07-05 00:52:17 +00:00
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
|
2009-07-05 00:52:17 +00:00
|
|
|
def adapt_datetime(ts):
|
|
|
|
return time.mktime(ts.timetuple())
|
|
|
|
|
2009-07-08 17:48:22 +00:00
|
|
|
sqlite3.register_adapter(datetime, adapt_datetime)
|
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(hook=r'(.*)', prefix=False, ignorebots=False)
|
2009-07-08 17:28:15 +00:00
|
|
|
def seeninput(bot, input):
|
2009-07-05 00:52:17 +00:00
|
|
|
dbpath = os.path.join(bot.persist_dir, dbname)
|
|
|
|
|
|
|
|
conn = dbconnect(dbpath)
|
|
|
|
cursor = conn.cursor()
|
2009-07-08 17:28:15 +00:00
|
|
|
cursor.execute("insert or replace into seen(name, date, quote, chan)"
|
2009-07-08 17:48:22 +00:00
|
|
|
"values(?,?,?,?)", (input.nick, datetime.now(),
|
2009-07-08 17:28:15 +00:00
|
|
|
input.msg, input.chan))
|
2009-07-05 00:52:17 +00:00
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
|
2009-07-05 00:52:17 +00:00
|
|
|
@hook.command
|
|
|
|
def seen(bot, input):
|
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
|
|
|
|
|
|
|
if len(input.msg) < 6:
|
|
|
|
return seen.__doc__
|
|
|
|
|
2009-07-05 03:46:11 +00:00
|
|
|
query = input.msg[6:].strip()
|
2009-07-05 00:52:17 +00:00
|
|
|
|
|
|
|
if query == input.nick:
|
|
|
|
return "Have you looked in a mirror lately?"
|
|
|
|
|
|
|
|
dbpath = os.path.join(bot.persist_dir, dbname)
|
|
|
|
conn = dbconnect(dbpath)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
|
|
|
command = "select date, quote from seen where name = ? and chan = ?"
|
|
|
|
cursor.execute(command, (query, input.chan))
|
|
|
|
results = cursor.fetchone()
|
|
|
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
if(results != None):
|
2009-07-08 17:48:22 +00:00
|
|
|
reltime = timesince.timesince(datetime.fromtimestamp(results[0]))
|
2009-07-10 07:18:02 +00:00
|
|
|
return '%s was last seen %s ago saying: %s' % \
|
2009-07-08 17:28:15 +00:00
|
|
|
(query, reltime, results[1])
|
2009-07-05 00:52:17 +00:00
|
|
|
else:
|
2009-07-08 17:28:15 +00:00
|
|
|
return "I've never seen %s" % query
|
2009-07-05 00:52:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dbconnect(db):
|
2009-07-08 17:28:15 +00:00
|
|
|
"check to see that our db has the the seen table and return a connection."
|
2009-07-05 00:52:17 +00:00
|
|
|
conn = sqlite3.connect(db)
|
2009-07-08 17:28:15 +00:00
|
|
|
results = conn.execute("select count(*) from sqlite_master where name=?",
|
|
|
|
("seen", )).fetchone()
|
2009-07-05 00:52:17 +00:00
|
|
|
|
|
|
|
if(results[0] == 0):
|
2009-07-08 17:28:15 +00:00
|
|
|
conn.execute("create table if not exists "
|
|
|
|
"seen(name varchar(30) not null, date datetime not null, "
|
|
|
|
"quote varchar(250) not null, chan varchar(32) not null, "
|
2009-07-05 00:52:17 +00:00
|
|
|
"primary key(name, chan));")
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
return conn
|