h/plugins/tell.py

100 lines
2.9 KiB
Python
Raw Normal View History

2009-07-05 03:27:05 +00:00
" tell.py: written by sklnd in July 2009"
" 2010.01.25 - modified by Scaevolus"
2009-07-05 03:27:05 +00:00
import time
from util import hook, timesince
def get_tells(conn, user_to, chan):
return conn.execute("select user_from, message, time from tell where"
" user_to=lower(?) and chan=? order by time",
(user_to.lower(), chan)).fetchall()
2009-07-05 03:27:05 +00:00
@hook.command(hook=r'(.*)', prefix=False)
def tellinput(bot, input):
if 'showtells' in input.inp.lower():
return
conn = db_connect(bot, input.server)
2009-07-08 17:28:15 +00:00
tells = get_tells(conn, input.nick, input.chan)
2009-07-05 15:03:24 +00:00
if tells:
user_from, message, time = tells[0]
reltime = timesince.timesince(time)
2009-07-05 15:03:24 +00:00
reply = "%s said %s ago: %s" % (user_from, reltime, message)
if len(tells) > 1:
reply += " (+%d more, .showtells to view)" % (len(tells) - 1)
2009-07-08 17:28:15 +00:00
conn.execute("delete from tell where user_to=lower(?) and message=?",
(input.nick, message))
conn.commit()
return reply
@hook.command
def showtells(bot, input):
".showtells -- view all pending tell messages (sent in PM)."
conn = db_connect(bot, input.server)
2009-07-05 03:27:05 +00:00
tells = get_tells(conn, input.nick, input.chan)
if not tells:
input.pm("You have no pending tells.")
return
2009-07-05 03:27:05 +00:00
for tell in tells:
user_from, message, time = tell
reltime = timesince.timesince(time)
input.pm("%s said %s ago: %s" % (user_from, reltime, message))
conn.execute("delete from tell where user_to=lower(?) and chan=?",
(input.nick, input.chan))
conn.commit()
2009-07-05 03:27:05 +00:00
@hook.command
def tell(bot, input):
".tell <nick> <message> -- relay <message> to <nick> when <nick> is around"
2009-07-05 03:27:05 +00:00
query = input.inp.split(' ', 1)
2009-07-05 03:27:05 +00:00
if len(query) != 2 or not input.inp:
return tell.__doc__
2009-07-05 03:27:05 +00:00
user_to = query[0].lower()
message = query[1].strip()
user_from = input.nick
if user_to == user_from.lower():
2009-07-05 03:27:05 +00:00
return "No."
conn = db_connect(bot, input.server)
2009-07-05 03:27:05 +00:00
if conn.execute("select count() from tell where user_to=?",
(user_to,)).fetchone()[0] >= 5:
return "That person has too many things queued."
2009-07-08 17:28:15 +00:00
try:
conn.execute("insert into tell(user_to, user_from, message, chan,"
"time) values(?,?,?,?,?)", (user_to, user_from, message,
input.chan, time.time()))
2009-07-05 03:27:05 +00:00
conn.commit()
except conn.IntegrityError:
return "Message has already been queued."
2009-07-05 03:27:05 +00:00
return "I'll pass that along."
2009-07-05 03:27:05 +00:00
2009-07-08 17:28:15 +00:00
def db_connect(bot, server):
2009-11-21 00:57:00 +00:00
"check to see that our db has the tell table and return a connection."
conn = bot.get_db_connection(server)
2009-07-05 03:27:05 +00:00
conn.execute("create table if not exists tell"
"(user_to, user_from, message, chan, time,"
"primary key(user_to, message))")
2009-11-21 00:57:00 +00:00
conn.commit()
2009-07-05 03:27:05 +00:00
return conn