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/plugins/tell.py

119 lines
3.4 KiB
Python
Raw 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
def db_init(db):
2013-09-04 12:30:04 +02:00
"""check to see that our db has the tell table and return a dbection."""
2011-11-20 10:23:31 +01:00
db.execute("create table if not exists tell"
2013-09-04 12:30:04 +02:00
"(user_to, user_from, message, chan, time,"
"primary key(user_to, message))")
2011-11-20 10:23:31 +01:00
db.commit()
return db
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(paraml, input=None, notice=None, db=None, bot=None, nick=None, conn=None):
2011-11-20 10:23:31 +01:00
if 'showtells' in input.msg.lower():
return
db_init(db)
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,
2013-09-04 12:30:04 +02: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):
2012-05-16 05:07:27 +02:00
"showtells -- View all pending tell messages (sent in a notice)."
2011-11-20 10:23:31 +01:00
db_init(db)
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):
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():
2011-11-20 10:23:31 +01:00
# user is looking for us, being a smartass
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()):
2011-11-20 10:23:31 +01:00
notice("I cant send a message to that user!")
return
db_init(db)
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!")