This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/disabled_stuff/tell.py

122 lines
3.5 KiB
Python
Raw Permalink Normal View History

2013-09-04 12:30:04 +02:00
""" tell.py: written by sklnd in July 2009
2010.01.25 - modified by Scaevolus"""
2011-11-20 10:23:31 +01:00
import time
import re
from util import hook, timesince
db_ready = []
2011-11-20 10:23:31 +01:00
def db_init(db, conn):
"""Check that our db has the tell table, create it if not."""
global db_ready
if not conn.name in db_ready:
db.execute("create table if not exists tell"
"(user_to, user_from, message, chan, time,"
"primary key(user_to, message))")
db.commit()
db_ready.append(conn.name)
2011-11-20 10:23:31 +01:00
def get_tells(db, user_to):
return db.execute("select user_from, message, time, chan from tell where"
2013-09-04 12:30:04 +02:00
" user_to=lower(?) order by time",
(user_to.lower(),)).fetchall()
2011-11-20 10:23:31 +01:00
@hook.singlethread
@hook.event('PRIVMSG')
def tellinput(inp, input=None, notice=None, db=None, nick=None, conn=None):
2011-11-20 10:23:31 +01:00
if 'showtells' in input.msg.lower():
return
db_init(db, conn)
2011-11-20 10:23:31 +01:00
tells = get_tells(db, nick)
2011-11-20 10:23:31 +01:00
if tells:
user_from, message, time, chan = tells[0]
reltime = timesince.timesince(time)
reply = "{} sent you a message {} ago from {}: {}".format(user_from, reltime, chan,
2014-02-14 05:03:08 +01:00
message)
2011-11-20 10:23:31 +01:00
if len(tells) > 1:
reply += " (+{} more, {}showtells to view)".format(len(tells) - 1, conn.conf["command_prefix"])
2011-11-20 10:23:31 +01:00
db.execute("delete from tell where user_to=lower(?) and message=?",
2013-09-04 12:30:04 +02:00
(nick, message))
2011-11-20 10:23:31 +01:00
db.commit()
2012-03-31 18:22:06 +02:00
notice(reply)
2011-11-20 10:23:31 +01:00
@hook.command(autohelp=False)
def showtells(inp, nick='', chan='', notice=None, db=None, conn=None):
2014-02-13 12:47:01 +01:00
"""showtells -- View all pending tell messages (sent in a notice)."""
2011-11-20 10:23:31 +01:00
db_init(db, conn)
2011-11-20 10:23:31 +01:00
tells = get_tells(db, nick)
if not tells:
notice("You have no pending tells.")
return
for tell in tells:
user_from, message, time, chan = tell
past = timesince.timesince(time)
notice("{} sent you a message {} ago from {}: {}".format(user_from, past, chan, message))
2011-11-20 10:23:31 +01:00
db.execute("delete from tell where user_to=lower(?)",
2013-09-04 12:30:04 +02:00
(nick,))
2011-11-20 10:23:31 +01:00
db.commit()
@hook.command
def tell(inp, nick='', chan='', db=None, input=None, notice=None, conn=None):
2013-09-04 12:30:04 +02:00
"""tell <nick> <message> -- Relay <message> to <nick> when <nick> is around."""
2011-11-20 10:23:31 +01:00
query = inp.split(' ', 1)
if len(query) != 2:
2012-02-02 14:05:11 +01:00
notice(tell.__doc__)
2011-12-30 10:10:13 +01:00
return
2011-11-20 10:23:31 +01:00
user_to = query[0].lower()
message = query[1].strip()
user_from = nick
if chan.lower() == user_from.lower():
chan = 'a pm'
if user_to == user_from.lower():
2012-03-31 17:57:51 +02:00
notice("Have you looked in a mirror lately?")
2011-11-20 10:23:31 +01:00
return
2012-02-03 04:41:07 +01:00
if user_to.lower() == input.conn.nick.lower():
2014-02-14 04:49:41 +01:00
# user is looking for us, being a smart-ass
notice("Thanks for the message, {}!".format(user_from))
2011-11-20 10:23:31 +01:00
return
if not re.match("^[A-Za-z0-9_|.\-\]\[]*$", user_to.lower()):
notice("I can't send a message to that user!")
2011-11-20 10:23:31 +01:00
return
db_init(db, conn)
2011-11-20 10:23:31 +01:00
if db.execute("select count() from tell where user_to=?",
2013-09-04 12:30:04 +02:00
(user_to,)).fetchone()[0] >= 10:
2012-05-16 21:36:17 +02:00
notice("That person has too many messages queued.")
2011-11-20 10:23:31 +01:00
return
try:
db.execute("insert into tell(user_to, user_from, message, chan,"
2013-09-04 12:30:04 +02:00
"time) values(?,?,?,?,?)", (user_to, user_from, message,
chan, time.time()))
2011-11-20 10:23:31 +01:00
db.commit()
except db.IntegrityError:
notice("Message has already been queued.")
return
2012-02-03 04:41:07 +01:00
notice("Your message has been sent!")