52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
from docopt import Dict as DocOptDict
|
|
from irc3.plugins.command import command
|
|
from irc3.utils import IrcString
|
|
import irc3
|
|
|
|
from . import DatabasePlugin
|
|
|
|
|
|
@irc3.plugin
|
|
class Tell(DatabasePlugin):
|
|
requires = ['irc3.plugins.command',
|
|
'nxy.plugins.database']
|
|
|
|
def __init__(self, bot: irc3.IrcBot):
|
|
super().__init__(bot)
|
|
self.tell_queue = {}
|
|
# Restore tells from database
|
|
self.cur.execute('select from_user, to_user, message from tells')
|
|
for res in self.cur.fetchall():
|
|
user = res['to_user']
|
|
if user not in self.tell_queue:
|
|
self.tell_queue[user] = []
|
|
self.tell_queue[user].append(res)
|
|
|
|
@command
|
|
def tell(self, mask: IrcString, channel: IrcString, args: DocOptDict):
|
|
"""Saves a message for nick to forward on activity
|
|
|
|
%%tell <nick> <message>...
|
|
"""
|
|
nick = args['<nick>'].lower()
|
|
if nick not in self.tell_queue:
|
|
self.tell_queue[nick] = []
|
|
tell = [mask.nick.lower(), nick, ' '.join(args['<message>']).strip()]
|
|
self.tell_queue[nick].append(tell)
|
|
self.cur.execute('insert into tells (from_user, to_user, message)'
|
|
'values (?, ?, ?)', tell)
|
|
self.con.commit()
|
|
|
|
@irc3.event(r'(?i)^:(?P<mask>.*) PRIVMSG .* :.*')
|
|
def check(self, mask: str):
|
|
"""If activ user has tells, forward and delete them."""
|
|
nick = IrcString(mask).nick
|
|
if nick in self.tell_queue:
|
|
for tell in self.tell_queue[nick]:
|
|
self.bot.privmsg(nick, '[Tell] Message from {nick}: {message}'
|
|
.format(nick=tell[0], message=tell[2]))
|
|
del self.tell_queue[nick]
|
|
self.cur.execute('delete from tells where to_user = ?', [nick])
|
|
self.con.commit()
|