2017-05-18 00:01:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-06-30 17:33:26 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2017-06-30 12:04:50 +00:00
|
|
|
import irc3
|
2017-08-22 13:57:57 +00:00
|
|
|
from docopt import Dict
|
2017-05-18 00:01:44 +00:00
|
|
|
from irc3.plugins.command import command
|
|
|
|
from irc3.utils import IrcString
|
2017-06-30 13:03:01 +00:00
|
|
|
from psycopg2 import Error
|
2017-05-18 00:01:44 +00:00
|
|
|
|
2017-08-22 15:43:48 +00:00
|
|
|
from . import DatabasePlugin, Bot
|
2017-05-18 00:01:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Tell(DatabasePlugin):
|
2017-08-22 15:43:48 +00:00
|
|
|
def __init__(self, bot: Bot):
|
2017-05-18 00:01:44 +00:00
|
|
|
super().__init__(bot)
|
|
|
|
self.tell_queue = {}
|
2017-06-30 12:04:50 +00:00
|
|
|
|
2020-03-16 21:51:32 +00:00
|
|
|
with self.con.cursor() as cur:
|
|
|
|
cur.execute('''
|
|
|
|
SELECT
|
|
|
|
to_nick, from_nick, message, created_at
|
|
|
|
FROM
|
|
|
|
tells
|
|
|
|
''')
|
2017-06-30 12:04:50 +00:00
|
|
|
|
2020-03-16 21:51:32 +00:00
|
|
|
for res in cur.fetchall():
|
|
|
|
nick = res['to_nick'].lower()
|
2017-06-30 12:04:50 +00:00
|
|
|
|
2020-03-16 21:51:32 +00:00
|
|
|
if nick not in self.tell_queue:
|
|
|
|
self.tell_queue[nick] = []
|
|
|
|
self.tell_queue[nick].append(res[1:])
|
2017-05-18 00:01:44 +00:00
|
|
|
|
|
|
|
@command
|
2017-08-22 13:57:57 +00:00
|
|
|
def tell(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-05-18 00:01:44 +00:00
|
|
|
"""Saves a message for nick to forward on activity
|
|
|
|
|
|
|
|
%%tell <nick> <message>...
|
|
|
|
"""
|
2017-08-22 09:58:32 +00:00
|
|
|
nick = args['<nick>'].lower()
|
|
|
|
tell = [nick, mask.nick, ' '.join(args['<message>']).strip(), datetime.utcnow()]
|
2017-06-30 12:04:50 +00:00
|
|
|
|
2017-06-30 13:03:01 +00:00
|
|
|
try:
|
2020-03-16 21:51:32 +00:00
|
|
|
with self.con.cursor() as cur:
|
|
|
|
cur.execute('''
|
|
|
|
INSERT INTO
|
|
|
|
tells (to_nick, from_nick, message, created_at)
|
|
|
|
VALUES
|
|
|
|
(%s, %s, %s, %s)
|
|
|
|
''', tell)
|
2017-06-30 13:03:01 +00:00
|
|
|
self.con.commit()
|
2021-03-09 15:16:39 +00:00
|
|
|
if nick not in self.tell_queue:
|
|
|
|
self.tell_queue[nick] = []
|
|
|
|
self.tell_queue[nick].append(tell[1:])
|
2017-08-23 11:01:19 +00:00
|
|
|
self.bot.notice(mask.nick, "I will tell that to {} when I see them.".format(nick))
|
2017-08-22 09:58:32 +00:00
|
|
|
except Error as ex:
|
|
|
|
self.log.error(ex)
|
2017-06-30 13:03:01 +00:00
|
|
|
self.con.rollback()
|
2017-05-18 00:01:44 +00:00
|
|
|
|
2021-03-09 15:16:39 +00:00
|
|
|
self.bot.notice(mask.nick, "database error (is the user already awaiting such a message?)")
|
2017-08-23 10:49:17 +00:00
|
|
|
|
2017-05-29 12:40:58 +00:00
|
|
|
@irc3.event(r'(?i)^:(?P<mask>.*) PRIVMSG .* :.*')
|
2017-08-22 09:58:32 +00:00
|
|
|
def on_message(self, mask: str):
|
2017-06-30 12:04:50 +00:00
|
|
|
"""If activ nick has tells, forward and delete them."""
|
2017-08-22 09:58:32 +00:00
|
|
|
nick = IrcString(mask).nick.lower()
|
2017-06-30 12:04:50 +00:00
|
|
|
|
2017-08-22 09:58:32 +00:00
|
|
|
if nick in self.tell_queue:
|
2017-06-30 12:04:50 +00:00
|
|
|
# Forward all tells for nick
|
2017-08-22 09:58:32 +00:00
|
|
|
for tell in self.tell_queue[nick]:
|
2017-08-22 14:15:52 +00:00
|
|
|
self.bot.privmsg(nick, '\x02[Tell]\x02 Message from {nick} at {time}: {message}'.format(
|
2017-08-22 09:58:32 +00:00
|
|
|
nick=tell[0],
|
2020-05-01 23:20:37 +00:00
|
|
|
time=tell[2], # TODO: format time
|
|
|
|
message=tell[1],
|
2017-08-22 09:58:32 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
del self.tell_queue[nick]
|
2020-03-16 21:51:32 +00:00
|
|
|
with self.con.cursor() as cur:
|
|
|
|
cur.execute('''
|
|
|
|
DELETE FROM
|
|
|
|
tells
|
|
|
|
WHERE
|
|
|
|
to_nick = %s
|
|
|
|
''', [nick])
|
2017-08-22 09:58:32 +00:00
|
|
|
self.con.commit()
|