h/plugins/tell.py

105 lines
2.8 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
2010-03-01 02:32:41 +00:00
2010-02-02 04:42:34 +00:00
def get_tells(db, user_to, chan):
return db.execute("select user_from, message, time from tell where"
2010-03-01 02:32:41 +00:00
" user_to=lower(?) and chan=? order by time",
(user_to.lower(), chan)).fetchall()
2009-07-05 03:27:05 +00:00
2010-02-02 04:42:34 +00:00
@hook.tee
def tellinput(bot, input):
2010-02-02 04:42:34 +00:00
if input.command != 'PRIVMSG':
return
if 'showtells' in input.msg.lower():
return
2010-03-01 02:32:41 +00:00
2010-02-02 04:42:34 +00:00
db = bot.get_db_connection(input.server)
db = db_init(db)
2009-07-08 17:28:15 +00:00
2010-02-02 04:42:34 +00:00
tells = get_tells(db, 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
2010-02-02 04:42:34 +00:00
db.execute("delete from tell where user_to=lower(?) and message=?",
(input.nick, message))
2010-02-02 04:42:34 +00:00
db.commit()
input.reply(reply)
2010-03-01 02:32:41 +00:00
@hook.command
2010-02-02 04:42:34 +00:00
def showtells(inp, nick='', chan='', pm=None, db=None):
".showtells -- view all pending tell messages (sent in PM)."
2010-03-01 02:32:41 +00:00
2010-02-02 04:42:34 +00:00
db_init(db)
2009-07-05 03:27:05 +00:00
2010-02-02 04:42:34 +00:00
tells = get_tells(db, nick, chan)
2010-03-01 02:32:41 +00:00
if not tells:
2010-02-02 04:42:34 +00:00
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)
2010-02-02 04:42:34 +00:00
pm("%s said %s ago: %s" % (user_from, reltime, message))
2010-03-01 02:32:41 +00:00
2010-02-02 04:42:34 +00:00
db.execute("delete from tell where user_to=lower(?) and chan=?",
(nick, chan))
db.commit()
2009-07-05 03:27:05 +00:00
2010-03-01 02:32:41 +00:00
2009-07-05 03:27:05 +00:00
@hook.command
2010-02-02 04:42:34 +00:00
def tell(inp, nick='', chan='', db=None):
".tell <nick> <message> -- relay <message> to <nick> when <nick> is around"
2009-07-05 03:27:05 +00:00
2010-02-02 04:42:34 +00:00
query = inp.split(' ', 1)
2009-07-05 03:27:05 +00:00
2010-02-02 04:42:34 +00:00
if not inp or len(query) != 2:
return tell.__doc__
2009-07-05 03:27:05 +00:00
user_to = query[0].lower()
message = query[1].strip()
2010-02-02 04:42:34 +00:00
user_from = nick
2010-03-01 02:32:41 +00:00
if user_to == user_from.lower():
2009-07-05 03:27:05 +00:00
return "No."
2010-02-02 04:42:34 +00:00
db_init(db)
2009-07-05 03:27:05 +00:00
2010-02-02 04:42:34 +00:00
if db.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:
2010-02-02 04:42:34 +00:00
db.execute("insert into tell(user_to, user_from, message, chan,"
"time) values(?,?,?,?,?)", (user_to, user_from, message,
2010-02-02 04:42:34 +00:00
chan, time.time()))
db.commit()
except db.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
2010-02-02 04:42:34 +00:00
def db_init(db):
"check to see that our db has the tell table and return a dbection."
db.execute("create table if not exists tell"
"(user_to, user_from, message, chan, time,"
"primary key(user_to, message))")
2010-02-02 04:42:34 +00:00
db.commit()
2010-03-01 02:32:41 +00:00
2010-02-02 04:42:34 +00:00
return db