h/plugins/tell.py

108 lines
3.3 KiB
Python
Raw Normal View History

2009-07-05 03:27:05 +00:00
" tell.py: written by sklnd in July 2009"
import os
import time
import datetime
import sqlite3
from util import hook, timesince
2009-07-05 03:27:05 +00:00
dbname = "skydb"
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())
sqlite3.register_adapter(datetime.datetime, adapt_datetime)
2009-07-08 17:28:15 +00:00
2009-07-05 03:27:05 +00:00
@hook.command(hook=r'(.*)', 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()
command = "select count(name) from tell where name = ? 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 " \
"where name = ? and chan = ?"
2009-07-05 03:27:05 +00:00
tells = cursor.execute(command, (input.nick, input.chan)).fetchall()
for tell in tells:
2009-07-05 15:03:24 +00:00
reltime = timesince(datetime.datetime.fromtimestamp(tell[3]))
2009-07-08 17:28:15 +00:00
bot.reply('%(teller)s said %(reltime)s ago: %(quote)s' %
2009-07-05 15:03:24 +00:00
{'teller': tell[1], 'quote': tell[2], 'reltime': reltime})
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-07-05 03:27:05 +00:00
conn.commit()
conn.close()
@hook.command
def tell(bot, input):
2009-07-08 17:28:15 +00:00
".tell <nick> <message> - Relay <message> to <nick> the next time he talks"
2009-07-05 03:27:05 +00:00
if len(input.msg) < 6:
return tell.__doc__
query = input.msg[6:].strip().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)
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."
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,
datetime.datetime.now()))
2009-07-05 03:27:05 +00:00
conn.commit()
conn.close()
return "I'll let (him|her|it) know."
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-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 03:27:05 +00:00
conn = sqlite3.connect(db)
2009-07-08 17:28:15 +00:00
results = conn.execute("select count(*) from sqlite_master where name=?",
("tell", )).fetchone()
2009-07-05 03:27:05 +00:00
if results[0] == 0:
2009-07-08 17:28:15 +00:00
conn.execute("create table if not exists tell(id integer primary key "
"autoincrement, name varchar(30) not null, user_from "
"varchar(30) not null, quote varchar(250) not null, "
"chan varchar(32) not null, date datetime not null);")
2009-07-05 03:27:05 +00:00
conn.commit()
2009-07-08 17:28:15 +00:00
results = conn.execute("select count(*) from sqlite_master where name=?",
("tell_probation", )).fetchone()
2009-07-05 03:27:05 +00:00
if results[0] == 0:
conn.execute("create table if not exists "+ \
2009-07-08 17:28:15 +00:00
"tell_probation(name varchar(30), chan varchar(32),"
"primary key(name, chan));")
2009-07-05 03:27:05 +00:00
conn.commit()
return conn