# -*- coding: utf-8 -*- import re import irc3 from irc3.utils import IrcString from . import DatabasePlugin class Useless(DatabasePlugin): requires = [] @irc3.event(r'^:(?P\S+) PRIVMSG (?P#\S+) :s/' r'(?P(?:[^/\\]|\\.)*)/' r'(?P(?:.*?))' r'(?:/ ?(?P.*))?$') def regex(self, mask: str, target: str, search: str, replace: str, nick: str = None): nick = (nick or IrcString(mask).nick).strip() if nick == self.bot.nick: return self.cur.execute(''' SELECT item FROM last_messages WHERE nick = lower(%s) AND channel = lower(%s) ''', [nick, target]) result = self.cur.fetchone() if result: old = result['item'] msg = old.replace(search, '\x02{}\x0F'.format(replace), 1) msg = re.sub(r'\x01ACTION (.*)\x01', r'/me \1', msg) if old != msg: self.bot.privmsg(target, '<{nick}> {msg}'.format(nick=nick, msg=msg)) @irc3.event(r'(?i)^:(?P\S+) PRIVMSG (?P#\S+) :(?P.*)$') def last_message(self, mask: str, target: str, msg: str): """Saves the last message of a user for each channel (for regex).""" mask = IrcString(mask) self.cur.execute(''' INSERT INTO last_messages (nick, host, channel, item) VALUES (lower(%s), %s, lower(%s), %s) ON CONFLICT (nick, channel) DO UPDATE SET host = excluded.host, item = excluded.item ''', [mask.nick, mask.host, target, msg]) self.con.commit()