SedRegex: Avoid pickling IrcMsg objects

This commit is contained in:
Valentin Lorentz 2024-07-15 12:53:59 +02:00
parent 5e24dc9ed3
commit 7187f4882f

View File

@ -56,25 +56,30 @@ axe_spaces = utils.str.MultipleReplacer({'\n': '\\n', '\t': '\\t', '\r': '\\r'})
class SearchNotFoundError(Exception): class SearchNotFoundError(Exception):
pass pass
def _replacer_process(msg, target, pattern, replacement, count, def _replacer_process(source, target, pattern, replacement, count,
messages, historyLength, config, log): messages, historyLength, config, log):
for m in messages: (source_nick, source_args) = source
if m.command in ('PRIVMSG', 'NOTICE') and \ for (i, (nick, prefix, args)) in enumerate(messages):
ircutils.strEqual(m.args[0], msg.args[0]): if ircutils.strEqual(args[0], source_args[0]):
if target and m.nick != target: if target and nick != target:
continue continue
# Don't snarf ignored users' messages unless specifically # Don't snarf ignored users' messages unless specifically
# told to. # told to.
if ircdb.checkIgnored(m.prefix) and not target: if ircdb.checkIgnored(prefix) and not target:
continue continue
"""
# When running substitutions, ignore the "* nick" part of any actions. # When running substitutions, ignore the "* nick" part of any actions.
action = ircmsgs.isAction(m) action = ircmsgs.isAction(m)
if action: if action:
text = ircmsgs.unAction(m) text = ircmsgs.unAction(m)
else: else:
text = m.args[1] text = m.args[1]
"""
action = False
text = args[1]
"""
# Test messages sent before SedRegex was activated. Mark them all as seen # Test messages sent before SedRegex was activated. Mark them all as seen
# so we only need to do this check once per message. # so we only need to do this check once per message.
if not m.tagged(TAG_SEEN): if not m.tagged(TAG_SEEN):
@ -85,6 +90,7 @@ def _replacer_process(msg, target, pattern, replacement, count,
if config['ignoreRegex'] and m.tagged(TAG_IS_REGEX): if config['ignoreRegex'] and m.tagged(TAG_IS_REGEX):
log.debug("Skipping message %s because it is tagged as isRegex", m.args[1]) log.debug("Skipping message %s because it is tagged as isRegex", m.args[1])
continue continue
"""
try: try:
replace_result = pattern.search(text) replace_result = pattern.search(text)
@ -93,25 +99,25 @@ def _replacer_process(msg, target, pattern, replacement, count,
replacement = ircutils.bold(replacement) replacement = ircutils.bold(replacement)
subst = pattern.sub(replacement, text, count) subst = pattern.sub(replacement, text, count)
if action: # If the message was an ACTION, prepend the nick back. if action: # If the message was an ACTION, prepend the nick back.
subst = '* %s %s' % (m.nick, subst) subst = '* %s %s' % (nick, subst)
subst = axe_spaces(subst) subst = axe_spaces(subst)
if m.nick == msg.nick: if nick == source_nick:
fmt = config['format'] fmt = config['format']
env = {'replacement': subst} env = {'replacement': subst}
else: else:
fmt = config['format.other'] fmt = config['format.other']
env = {'otherNick': msg.nick, 'replacement': subst} env = {'otherNick': source_nick, 'replacement': subst}
return (m, fmt, env) return (i, fmt, env)
except Exception as e: except Exception as e:
log.warning(_("SedRegex error: %s"), e, exc_info=True) log.warning(_("SedRegex error: %s"), e, exc_info=True)
raise raise
log.debug(_("SedRegex: Search %r not found in the last %i messages of %s."), log.debug(_("SedRegex: Search %r not found in the last %i messages of %s."),
msg.args[1], historyLength, msg.args[0]) source_args[1], historyLength, source_args[0])
raise SearchNotFoundError() raise SearchNotFoundError()
class SedRegex(callbacks.PluginRegexp): class SedRegex(callbacks.PluginRegexp):
@ -233,7 +239,10 @@ class SedRegex(callbacks.PluginRegexp):
if not ircutils.isNick(str(target)): if not ircutils.isNick(str(target)):
return return
iterable = [m for m in iterable if m.tagged('receivedBy') == irc] messages = [m for m in iterable
if m.tagged('receivedBy') == irc
and m.command in ('PRIVMSG', 'NOTICE')]
iterable = [(m.nick, m.prefix, m.args) for m in messages]
regex_timeout = self.registryValue('processTimeout') regex_timeout = self.registryValue('processTimeout')
config = { config = {
@ -242,13 +251,13 @@ class SedRegex(callbacks.PluginRegexp):
'format.other') 'format.other')
} }
try: try:
(m, fmt, env) = process(_replacer_process, msg, (i, fmt, env) = process(_replacer_process, (msg.nick, msg.args),
target=target, pattern=pattern, replacement=replacement, target=target, pattern=pattern, replacement=replacement,
count=count, messages=iterable, count=count, messages=iterable,
historyLength=len(irc.state.history), historyLength=len(irc.state.history),
config=config, log=self.log, config=config, log=self.log,
timeout=regex_timeout, pn=self.name(), cn='replacer') timeout=regex_timeout, pn=self.name(), cn='replacer')
message = ircutils.standardSubstitute(irc, m, fmt, env) message = ircutils.standardSubstitute(irc, messages[i], fmt, env)
except ProcessTimeoutError: except ProcessTimeoutError:
irc.error(_("Search timed out.")) irc.error(_("Search timed out."))
except SearchNotFoundError: except SearchNotFoundError: