h/plugins/tell.py

106 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
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"
2014-01-14 21:12:37 +00:00
"(user_to, user_from, message, chan, time,"
"primary key(user_to, message))")
db.commit()
return db
def get_tells(db, user_to):
return db.execute("select user_from, message, time, chan from tell where"
2014-01-14 21:12:37 +00:00
" user_to=lower(?) order by time",
(user_to.lower(),)).fetchall()
2009-07-05 03:27:05 +00:00
@hook.singlethread
2010-03-11 23:34:54 +00:00
@hook.event('PRIVMSG')
def tellinput(paraml, input=None, db=None):
2010-02-02 04:42:34 +00:00
if 'showtells' in input.msg.lower():
return
2010-03-01 02:32:41 +00:00
db_init(db)
2009-07-08 17:28:15 +00:00
tells = get_tells(db, input.nick)
2009-07-05 15:03:24 +00:00
if tells:
user_from, message, time, chan = tells[0]
reltime = timesince.timesince(time)
2009-07-05 15:03:24 +00:00
2010-04-23 03:50:56 +00:00
reply = "%s said %s ago in %s: %s" % (user_from, reltime, chan,
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=?",
2014-01-14 21:12:37 +00:00
(input.nick, message))
2010-02-02 04:42:34 +00:00
db.commit()
input.pm(reply)
2010-03-01 02:32:41 +00:00
@hook.command(autohelp=False)
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-03-16 21:35:52 +00:00
tells = get_tells(db, nick)
2010-03-01 02:32:41 +00:00
if not tells:
pm("You have no pending tells.")
return
2009-07-05 03:27:05 +00:00
for tell in tells:
user_from, message, time, chan = tell
2010-04-23 03:50:56 +00:00
past = timesince.timesince(time)
pm("%s said %s ago in %s: %s" % (user_from, past, chan, message))
2010-03-01 02:32:41 +00:00
db.execute("delete from tell where user_to=lower(?)",
2014-01-14 21:12:37 +00:00
(nick,))
2010-02-02 04:42:34 +00:00
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
2015-01-12 07:31:09 +00:00
def tell(inp, nick='', chan='', db=None, conn=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
if 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 chan.lower() == user_from.lower():
chan = 'a pm'
2015-01-12 07:31:09 +00:00
if user_to in (user_from.lower(), conn.nick.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=?",
2014-01-14 21:12:37 +00:00
(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,"
2014-01-14 21:12:37 +00:00
"time) values(?,?,?,?,?)", (user_to, user_from, message,
chan, time.time()))
2010-02-02 04:42:34 +00:00
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."