callbacks: Make _prepareReply return a namedtuple instead of a huge tuple.

Otherwise it may cause complex bugs in the future if we
mess up the order.
This commit is contained in:
Valentin Lorentz 2020-05-22 08:23:26 +02:00
parent 67612b5338
commit e531bc027e

View File

@ -41,6 +41,7 @@ import codecs
import getopt import getopt
import inspect import inspect
import warnings import warnings
import collections
from . import (conf, ircdb, irclib, ircmsgs, ircutils, log, registry, from . import (conf, ircdb, irclib, ircmsgs, ircutils, log, registry,
utils, world) utils, world)
@ -172,6 +173,10 @@ def reply(*args, **kwargs):
DeprecationWarning) DeprecationWarning)
return _makeReply(dynamic.irc, *args, **kwargs) return _makeReply(dynamic.irc, *args, **kwargs)
_prepared_reply = collections.namedtuple(
'_prepared_reply',
'replyMaker target to private overheadLength prefixLength')
def _prepareReply(irc, msg, def _prepareReply(irc, msg,
prefixNick=None, private=None, prefixNick=None, private=None,
notice=None, to=None, action=None, error=False, notice=None, to=None, action=None, error=False,
@ -281,11 +286,17 @@ def _prepareReply(irc, msg,
prefixLength = len(prefix) prefixLength = len(prefix)
return (replyMaker, target, to, private, overheadLength, prefixLength) return _prepared_reply(
replyMaker=replyMaker,
target=target,
to=to,
private=private,
overheadLength=overheadLength,
prefixLength=prefixLength
)
def _makeReply(irc, msg, s, **kwargs): def _makeReply(irc, msg, s, **kwargs):
(replyMaker, _, _, _, _, _) = _prepareReply(irc, msg, **kwargs) return _prepareReply(irc, msg, **kwargs).replyMaker(s)
return replyMaker(s)
def _splitReply(s, *, irc, target, replyMaker, overheadLength, prefixLength): def _splitReply(s, *, irc, target, replyMaker, overheadLength, prefixLength):
@ -747,21 +758,20 @@ class ReplyIrcProxy(RichReplyMethods):
else: else:
sendMsg = self.irc.queueMsg sendMsg = self.irc.queueMsg
(replyMaker, target, to, private, overheadLength, prefixLength) = \ preparedReply = _prepareReply(self, msg, **kwargs)
_prepareReply(self, msg, **kwargs)
s = ircutils.safeArgument(s) s = ircutils.safeArgument(s)
msgs = _splitReply( msgs = _splitReply(
s, s,
irc=self.irc, irc=self.irc,
target=target, target=preparedReply.target,
replyMaker=replyMaker, replyMaker=preparedReply.replyMaker,
overheadLength=overheadLength, overheadLength=preparedReply.overheadLength,
prefixLength=prefixLength, prefixLength=preparedReply.prefixLength,
) )
instant = conf.get(conf.supybot.reply.mores.instant, instant = conf.get(conf.supybot.reply.mores.instant,
channel=target, network=self.irc.network) channel=preparedReply.target, network=self.irc.network)
while instant > 1 and msgs: while instant > 1 and msgs:
instant -= 1 instant -= 1
response = msgs.pop() response = msgs.pop()
@ -775,16 +785,16 @@ class ReplyIrcProxy(RichReplyMethods):
return return
response = msgs.pop() response = msgs.pop()
prefix = msg.prefix prefix = msg.prefix
if to and ircutils.isNick(to): if preparedReply.to and ircutils.isNick(preparedReply.to):
try: try:
state = self.getRealIrc().state state = self.getRealIrc().state
prefix = state.nickToHostmask(to) prefix = state.nickToHostmask(preparedReply.to)
except KeyError: except KeyError:
pass # We'll leave it as it is. pass # We'll leave it as it is.
mask = prefix.split('!', 1)[1] mask = prefix.split('!', 1)[1]
self._mores[mask] = msgs self._mores[mask] = msgs
public = bool(self.msg.channel) public = bool(self.msg.channel)
private = private or not public private = preparedReply.private or not public
self._mores[msg.nick] = (private, msgs) self._mores[msg.nick] = (private, msgs)
sendMsg(response) sendMsg(response)
return response return response