2009-07-05 03:27:05 +00:00
|
|
|
" tell.py: written by sklnd in July 2009"
|
|
|
|
|
|
|
|
import os
|
2009-07-08 17:04:30 +00:00
|
|
|
import time
|
2009-07-08 17:48:22 +00:00
|
|
|
from datetime import datetime
|
2009-07-08 17:04:30 +00:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
from util import hook, timesince
|
|
|
|
|
2009-07-05 03:27:05 +00:00
|
|
|
|
2009-07-08 17:49:45 +00:00
|
|
|
dbname = "skybot.db"
|
2009-07-05 03:27:05 +00:00
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
|
2009-07-05 15:03:24 +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 15:03:24 +00:00
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
|
2010-01-30 19:09:34 +00:00
|
|
|
@hook.command(hook=r'^(?!\.showtells)(.*)', prefix=False, ignorebots=True)
|
2009-07-08 17:28:15 +00:00
|
|
|
def tellinput(bot, input):
|
2009-07-05 03:27:05 +00:00
|
|
|
dbpath = os.path.join(bot.persist_dir, dbname)
|
|
|
|
conn = dbconnect(dbpath)
|
|
|
|
|
|
|
|
cursor = conn.cursor()
|
2009-11-21 00:57:00 +00:00
|
|
|
command = "select count(name) from tell where name LIKE ? and chan = ?"
|
2009-07-08 17:28:15 +00:00
|
|
|
results = cursor.execute(command, (input.nick, input.chan)).fetchone()
|
2009-07-05 03:27:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
if results[0] > 0:
|
2009-07-08 17:28:15 +00:00
|
|
|
command = "select id, user_from, quote, date from tell " \
|
2009-11-21 00:57:00 +00:00
|
|
|
"where name LIKE ? and chan = ? limit 1"
|
2009-10-18 22:39:37 +00:00
|
|
|
tell = cursor.execute(command, (input.nick, input.chan)).fetchall()[0]
|
2009-11-18 00:38:48 +00:00
|
|
|
more = results[0] - 1
|
2009-10-18 22:39:37 +00:00
|
|
|
reltime = timesince.timesince(datetime.fromtimestamp(tell[3]))
|
|
|
|
|
2009-11-17 23:08:45 +00:00
|
|
|
reply = "%(teller)s said %(reltime)s ago: %(quote)s" % \
|
2009-10-18 22:39:37 +00:00
|
|
|
{"teller": tell[1], "quote": tell[2], "reltime": reltime}
|
|
|
|
if more:
|
2010-01-30 19:09:34 +00:00
|
|
|
reply += " (+%(more)d more, to view use .showtells)" % {"more": more}
|
2009-10-18 22:39:37 +00:00
|
|
|
|
2009-11-19 01:42:37 +00:00
|
|
|
input.reply(reply)
|
2009-10-18 22:39:37 +00:00
|
|
|
command = "delete from tell where id = ?"
|
|
|
|
cursor.execute(command, (tell[0], ))
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
@hook.command
|
|
|
|
def showtells(bot, input):
|
2009-11-19 03:20:59 +00:00
|
|
|
".showtells -- view all pending tell messages (sent in PM)."
|
2009-10-18 22:39:37 +00:00
|
|
|
|
|
|
|
dbpath = os.path.join(bot.persist_dir, dbname)
|
|
|
|
conn = dbconnect(dbpath)
|
2009-07-05 03:27:05 +00:00
|
|
|
|
2009-10-18 22:39:37 +00:00
|
|
|
cursor = conn.cursor()
|
2009-11-21 00:57:00 +00:00
|
|
|
command = "SELECT id, user_from, quote, date FROM tell " \
|
|
|
|
"WHERE name LIKE ? and chan = ?"
|
2009-10-18 22:39:37 +00:00
|
|
|
tells = cursor.execute(command, (input.nick, input.chan)).fetchall()
|
|
|
|
|
|
|
|
if(len(tells) > 0):
|
2009-07-05 03:27:05 +00:00
|
|
|
for tell in tells:
|
2009-07-08 17:48:22 +00:00
|
|
|
reltime = timesince.timesince(datetime.fromtimestamp(tell[3]))
|
2010-01-30 19:09:34 +00:00
|
|
|
input.pm('%(teller)s said %(time)s ago: %(quote)s' % \
|
|
|
|
{'teller': tell[1], 'quote': tell[2], 'time': reltime})
|
2009-10-18 22:39:37 +00:00
|
|
|
|
2009-07-05 03:27:05 +00:00
|
|
|
command = "delete from tell where id = ?"
|
2009-07-08 17:28:15 +00:00
|
|
|
cursor.execute(command, (tell[0], ))
|
2009-10-18 22:39:37 +00:00
|
|
|
|
2009-07-05 03:27:05 +00:00
|
|
|
conn.commit()
|
2009-10-18 22:39:37 +00:00
|
|
|
else:
|
2009-11-19 03:20:59 +00:00
|
|
|
input.pm("You have no pending tells.")
|
2009-10-18 22:39:37 +00:00
|
|
|
|
2009-07-05 03:27:05 +00:00
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command
|
|
|
|
def tell(bot, input):
|
2009-11-19 03:20:59 +00:00
|
|
|
".tell <nick> <message> -- relay <message> to <nick> when <nick> is around"
|
2009-07-05 03:27:05 +00:00
|
|
|
|
|
|
|
if len(input.msg) < 6:
|
|
|
|
return tell.__doc__
|
|
|
|
|
2010-01-17 23:07:08 +00:00
|
|
|
query = input.inp.partition(" ")
|
2009-07-05 03:27:05 +00:00
|
|
|
|
|
|
|
if query[0] == input.nick:
|
|
|
|
return "No."
|
|
|
|
|
|
|
|
|
|
|
|
if query[2] != "":
|
|
|
|
dbpath = os.path.join(bot.persist_dir, dbname)
|
|
|
|
conn = dbconnect(dbpath)
|
|
|
|
|
2009-07-05 15:42:07 +00:00
|
|
|
command = "select count(*) from tell_probation where name=? and chan=?"
|
2009-07-08 17:28:15 +00:00
|
|
|
if conn.execute(command, (input.nick, input.chan)).fetchone()[0] > 0:
|
2009-07-05 03:27:05 +00:00
|
|
|
return "No."
|
|
|
|
|
2009-07-05 15:40:04 +00:00
|
|
|
command = "select count(*) from tell where name=? and user_from=?"
|
|
|
|
if conn.execute(command, (query[0], input.nick)).fetchone()[0] >= 3:
|
|
|
|
return "You've told that person too many things."
|
|
|
|
|
2009-07-05 03:27:05 +00:00
|
|
|
cursor = conn.cursor()
|
2009-07-08 17:28:15 +00:00
|
|
|
command = "insert into tell(name, user_from, quote, chan, date) " \
|
|
|
|
"values(?,?,?,?,?)"
|
|
|
|
cursor.execute(command, (query[0], input.nick, query[2], input.chan,
|
2009-07-08 17:48:22 +00:00
|
|
|
datetime.now()))
|
2009-07-08 17:28:15 +00:00
|
|
|
|
2009-07-05 03:27:05 +00:00
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
2009-07-08 17:48:22 +00:00
|
|
|
return "I'll pass that along."
|
2009-07-05 03:27:05 +00:00
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
else:
|
2009-07-05 03:27:05 +00:00
|
|
|
return tell.__doc__
|
|
|
|
|
2009-07-08 17:28:15 +00:00
|
|
|
|
2009-07-05 03:27:05 +00:00
|
|
|
def dbconnect(db):
|
2009-11-21 00:57:00 +00:00
|
|
|
"check to see that our db has the tell table and return a connection."
|
2009-07-05 03:27:05 +00:00
|
|
|
conn = sqlite3.connect(db)
|
|
|
|
|
2009-11-21 00:58:17 +00:00
|
|
|
conn.execute("CREATE TABLE IF NOT EXISTS tell(id integer primary key "
|
2009-11-21 00:57:00 +00:00
|
|
|
"autoincrement, name text not null, user_from text not null,"
|
|
|
|
"quote text not null, chan text not null, "
|
|
|
|
"date datetime not null);")
|
2009-07-05 03:27:05 +00:00
|
|
|
|
2009-11-21 00:57:00 +00:00
|
|
|
conn.execute("CREATE TABLE IF NOT EXISTS "
|
|
|
|
"tell_probation(name text, chan text,"
|
|
|
|
"primary key(name, chan));")
|
2009-07-05 03:27:05 +00:00
|
|
|
|
2009-11-21 00:57:00 +00:00
|
|
|
conn.commit()
|
2009-07-05 03:27:05 +00:00
|
|
|
|
|
|
|
return conn
|