irclib: Split _sendReply from NestedCommandsIrcProxy.reply

This function was getting uncomfortably big.
This commit is contained in:
Valentin Lorentz 2021-03-13 15:03:56 +01:00
parent 567618392f
commit 8a3efe4379

View File

@ -914,10 +914,6 @@ class NestedCommandsIrcProxy(ReplyIrcProxy):
assert not isinstance(s, ircmsgs.IrcMsg), \ assert not isinstance(s, ircmsgs.IrcMsg), \
'Old code alert: there is no longer a "msg" argument to reply.' 'Old code alert: there is no longer a "msg" argument to reply.'
self.repliedTo = True self.repliedTo = True
if sendImmediately:
sendMsg = self.irc.sendMsg
else:
sendMsg = self.irc.queueMsg
if msg is None: if msg is None:
msg = self.msg msg = self.msg
if prefixNick is not None: if prefixNick is not None:
@ -936,6 +932,46 @@ class NestedCommandsIrcProxy(ReplyIrcProxy):
if not isinstance(s, minisix.string_types): # avoid trying to str() unicode if not isinstance(s, minisix.string_types): # avoid trying to str() unicode
s = str(s) # Allow non-string esses. s = str(s) # Allow non-string esses.
if self.finalEvaled:
try:
self._sendReply(
s=s, target=target, msg=msg,
sendImmediately=sendImmediately, stripCtcp=stripCtcp)
finally:
self._resetReplyAttributes()
else:
if msg.ignored:
# Since the final reply string is constructed via
# ' '.join(self.args), the args index for ignored commands
# needs to be popped to avoid extra spaces in the final reply.
self.args.pop(self.counter)
msg.tag('ignored', False)
else:
self.args[self.counter] = s
self.evalArgs()
def _replyOverhead(self, target, msg):
"""Returns the number of bytes added to a PRIVMSG payload, either by
Limnoria itself or by the server.
Ignores tag bytes, as they are accounted for separatly."""
overhead = (
len(':')
+ len(self.irc.prefix)
+ len(' PRIVMSG ')
+ len(target)
+ len(' :')
+ len('\r\n')
)
if self.prefixNick:
overhead += len(msg.nick) + len(': ')
return overhead
def _sendReply(self, s, target, msg, sendImmediately, stripCtcp):
if sendImmediately:
sendMsg = self.irc.sendMsg
else:
sendMsg = self.irc.queueMsg
replyArgs = dict( replyArgs = dict(
to=self.to, to=self.to,
notice=self.notice, notice=self.notice,
@ -945,8 +981,6 @@ class NestedCommandsIrcProxy(ReplyIrcProxy):
stripCtcp=stripCtcp stripCtcp=stripCtcp
) )
if self.finalEvaled:
try:
if isinstance(self.irc, self.__class__): if isinstance(self.irc, self.__class__):
s = s[:conf.supybot.reply.maximumLength()] s = s[:conf.supybot.reply.maximumLength()]
return self.irc.reply(s, return self.irc.reply(s,
@ -963,15 +997,7 @@ class NestedCommandsIrcProxy(ReplyIrcProxy):
allowedLength = conf.get(conf.supybot.reply.mores.length, allowedLength = conf.get(conf.supybot.reply.mores.length,
channel=target, network=self.irc.network) channel=target, network=self.irc.network)
if not allowedLength: # 0 indicates this. if not allowedLength: # 0 indicates this.
allowedLength = (512 allowedLength = 512 - self._replyOverhead(target, msg)
- len(':') - len(self.irc.prefix)
- len(' PRIVMSG ')
- len(target)
- len(' :')
- len('\r\n')
)
if self.prefixNick:
allowedLength -= len(msg.nick) + len(': ')
maximumMores = conf.get(conf.supybot.reply.mores.maximum, maximumMores = conf.get(conf.supybot.reply.mores.maximum,
channel=target, network=self.irc.network) channel=target, network=self.irc.network)
maximumLength = allowedLength * maximumMores maximumLength = allowedLength * maximumMores
@ -1045,18 +1071,6 @@ class NestedCommandsIrcProxy(ReplyIrcProxy):
self._mores[msg.nick] = (private, msgs) self._mores[msg.nick] = (private, msgs)
sendMsg(response) sendMsg(response)
return response return response
finally:
self._resetReplyAttributes()
else:
if msg.ignored:
# Since the final reply string is constructed via
# ' '.join(self.args), the args index for ignored commands
# needs to be popped to avoid extra spaces in the final reply.
self.args.pop(self.counter)
msg.tag('ignored', False)
else:
self.args[self.counter] = s
self.evalArgs()
def noReply(self, msg=None): def noReply(self, msg=None):
if msg is None: if msg is None: