nxy/nxy/plugins/tell.py

54 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
from docopt import Dict as DocOptDict
from irc3.plugins.command import command
from irc3.utils import IrcString
from irc3 import IrcBot, event
import irc3
from . import DatabasePlugin
@irc3.plugin
class Tell(DatabasePlugin):
requires = ['irc3.plugins.command',
'nxy.plugins.database']
def __init__(self, bot: 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)
# noinspection PyUnusedLocal
@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()
@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()