From 7cc7b4fac93b53223dadcd6977d628fd3fa4708d Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Mon, 10 Sep 2018 22:39:45 +0200 Subject: [PATCH] Redesign the message truncation algorithm. --- src/callbacks.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index 3f66232c3..eb95c0bd0 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -905,11 +905,15 @@ class NestedCommandsIrcProxy(ReplyIrcProxy): allowedLength = conf.get(conf.supybot.reply.mores.length, target) if not allowedLength: # 0 indicates this. - allowedLength = 470 - len(self.irc.prefix) - allowedLength -= len(msg.nick) - # The '(XX more messages)' may have not the same - # length in the current locale - allowedLength -= len(_('(XX more messages)')) + allowedLength = (512 + - 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, target) maximumLength = allowedLength * maximumMores @@ -920,16 +924,6 @@ class NestedCommandsIrcProxy(ReplyIrcProxy): s_size = len(s.encode()) if minisix.PY3 else len(s) if s_size <= allowedLength or \ not conf.get(conf.supybot.reply.mores, target): - # In case we're truncating, we add 20 to allowedLength, - # because our allowedLength is shortened for the - # "(XX more messages)" trailer. - if minisix.PY3: - appended = _('(XX more messages)').encode() - s = s.encode()[:allowedLength-len(appended)] - s = s.decode('utf8', 'ignore') - else: - appended = _('(XX more messages)') - s = s[:allowedLength-len(appended)] # There's no need for action=self.action here because # action implies noLengthCheck, which has already been # handled. Let's stick an assert in here just in case. @@ -941,6 +935,9 @@ class NestedCommandsIrcProxy(ReplyIrcProxy): stripCtcp=stripCtcp) sendMsg(m) return m + # The '(XX more messages)' may have not the same + # length in the current locale + allowedLength -= len(_('(XX more messages)')) msgs = ircutils.wrap(s, allowedLength) msgs.reverse() instant = conf.get(conf.supybot.reply.mores.instant,target)