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-05-18 00:01:44 +00:00
|
|
|
from docopt import Dict as DocOptDict
|
|
|
|
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
|
|
|
|
|
|
|
from . import DatabasePlugin
|
|
|
|
|
|
|
|
|
|
|
|
@irc3.plugin
|
|
|
|
class Tell(DatabasePlugin):
|
|
|
|
requires = ['irc3.plugins.command',
|
2017-07-07 00:11:20 +00:00
|
|
|
'bot.plugins.storage']
|
2017-05-18 00:01:44 +00:00
|
|
|
|
2017-05-29 12:40:58 +00:00
|
|
|
def __init__(self, bot: irc3.IrcBot):
|
2017-05-18 00:01:44 +00:00
|
|
|
super().__init__(bot)
|
|
|
|
self.tell_queue = {}
|
2017-06-30 12:04:50 +00:00
|
|
|
|
|
|
|
# Fetch tells from database
|
|
|
|
self.cur.execute('''
|
|
|
|
select
|
2017-06-30 17:33:26 +00:00
|
|
|
to_nick, from_nick, message, created_at
|
2017-06-30 12:04:50 +00:00
|
|
|
from
|
|
|
|
tells
|
|
|
|
''')
|
|
|
|
|
|
|
|
# Add tells to queue
|
2017-05-18 00:01:44 +00:00
|
|
|
for res in self.cur.fetchall():
|
2017-06-30 17:33:26 +00:00
|
|
|
nick = res['to_nick'].lower()
|
2017-06-30 12:04:50 +00:00
|
|
|
|
|
|
|
# Create list in queue and add tell
|
|
|
|
if nick not in self.tell_queue:
|
|
|
|
self.tell_queue[nick] = []
|
2017-06-30 17:33:26 +00:00
|
|
|
self.tell_queue[nick].append(res[1:])
|
2017-05-18 00:01:44 +00:00
|
|
|
|
|
|
|
@command
|
2017-06-30 12:04:50 +00:00
|
|
|
def tell(self, mask: IrcString, target: IrcString, args: DocOptDict):
|
2017-05-18 00:01:44 +00:00
|
|
|
"""Saves a message for nick to forward on activity
|
|
|
|
|
|
|
|
%%tell <nick> <message>...
|
|
|
|
"""
|
2017-06-30 17:33:26 +00:00
|
|
|
nick = args['<nick>']
|
|
|
|
nick_lower = nick.lower()
|
|
|
|
tell = [nick, mask.nick, ' '.join(args['<message>']).strip(),
|
|
|
|
datetime.now()]
|
2017-06-30 12:04:50 +00:00
|
|
|
|
|
|
|
# Create list in queue and add tell
|
2017-06-30 17:33:26 +00:00
|
|
|
if nick_lower not in self.tell_queue:
|
|
|
|
self.tell_queue[nick_lower] = []
|
|
|
|
self.tell_queue[nick_lower].append(tell[1:])
|
2017-06-30 12:04:50 +00:00
|
|
|
|
2017-06-30 13:03:01 +00:00
|
|
|
try:
|
|
|
|
# Insert tell into database
|
|
|
|
self.cur.execute('''
|
|
|
|
insert into
|
2017-06-30 17:33:26 +00:00
|
|
|
tells (to_nick, from_nick, message, created_at)
|
2017-06-30 13:03:01 +00:00
|
|
|
values
|
2017-06-30 17:33:26 +00:00
|
|
|
(%s, %s, %s, %s)
|
2017-06-30 13:03:01 +00:00
|
|
|
''', tell)
|
|
|
|
self.con.commit()
|
|
|
|
except Error:
|
|
|
|
# Rollback transaction on error
|
|
|
|
self.con.rollback()
|
2017-05-18 00:01:44 +00:00
|
|
|
|
2017-05-29 12:40:58 +00:00
|
|
|
@irc3.event(r'(?i)^:(?P<mask>.*) PRIVMSG .* :.*')
|
2017-05-18 00:01:44 +00:00
|
|
|
def check(self, mask: str):
|
2017-06-30 12:04:50 +00:00
|
|
|
"""If activ nick has tells, forward and delete them."""
|
2017-05-18 00:01:44 +00:00
|
|
|
nick = IrcString(mask).nick
|
2017-06-30 17:33:26 +00:00
|
|
|
nick_lower = nick.lower()
|
2017-06-30 12:04:50 +00:00
|
|
|
|
2017-06-30 17:33:26 +00:00
|
|
|
if nick_lower in self.tell_queue:
|
2017-06-30 12:04:50 +00:00
|
|
|
# Forward all tells for nick
|
2017-06-30 17:33:26 +00:00
|
|
|
for tell in self.tell_queue[nick_lower]:
|
|
|
|
# TODO: format time
|
|
|
|
self.bot.privmsg(nick, '[Tell] Message from {nick} at {time}: '
|
|
|
|
'{message}'.format(nick=tell[0],
|
|
|
|
time=tell[2],
|
|
|
|
message=tell[1]))
|
2017-06-30 12:04:50 +00:00
|
|
|
|
|
|
|
# Remove nick from queue
|
2017-06-30 17:33:26 +00:00
|
|
|
del self.tell_queue[nick_lower]
|
2017-06-30 12:04:50 +00:00
|
|
|
|
2017-06-30 13:03:01 +00:00
|
|
|
try:
|
|
|
|
# Remove tells from database
|
|
|
|
self.cur.execute('''
|
|
|
|
delete from
|
|
|
|
tells
|
|
|
|
where
|
2017-06-30 17:33:26 +00:00
|
|
|
lower(to_nick) = lower(%s)
|
2017-06-30 13:03:01 +00:00
|
|
|
''', [nick])
|
|
|
|
self.con.commit()
|
2017-06-30 17:33:26 +00:00
|
|
|
except Error as ex:
|
|
|
|
print(ex)
|
2017-06-30 13:03:01 +00:00
|
|
|
# Rollback transaction on error
|
|
|
|
self.con.rollback()
|