From 030ce5e6d46e35d0f41da2b52db163b1b0d0fe32 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Thu, 1 Feb 2018 22:37:24 +0100 Subject: [PATCH] Fix previous commit. --- plugins/Games/test.py | 2 +- src/callbacks.py | 2 +- src/ircutils.py | 3 ++- src/utils/str.py | 5 ++++- test/test_ircutils.py | 20 ++++++++++++++++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/plugins/Games/test.py b/plugins/Games/test.py index aa971b033..49da3ee7a 100644 --- a/plugins/Games/test.py +++ b/plugins/Games/test.py @@ -36,7 +36,7 @@ class GamesTestCase(ChannelPluginTestCase): self.irc.feedMsg(ircmsgs.op(self.channel, self.irc.nick)) sawKick = False for i in range(100): - m = self.getMsg('roulette', frm='someoneElse') + m = self.getMsg('roulette', frm='someoneElse!foo@bar') if m.command == 'PRIVMSG': self.failUnless(self._nonKickRe.search(m.args[1]), 'Got a msg without bang|click|spin: %r' % m) diff --git a/src/callbacks.py b/src/callbacks.py index a021f13ec..e9d011169 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -926,7 +926,7 @@ class NestedCommandsIrcProxy(ReplyIrcProxy): maximumLength, len(s)) s = s[:maximumLength] s_size = len(s.encode()) if minisix.PY3 else len(s) - if s_size > allowedLength or \ + 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 diff --git a/src/ircutils.py b/src/ircutils.py index ccda3392d..975453dcc 100644 --- a/src/ircutils.py +++ b/src/ircutils.py @@ -598,7 +598,8 @@ class FormatParser(object): def wrap(s, length, break_on_hyphens = False, break_long_words = False): processed = [] - chunks = utils.str.byteTextWrap(s, length) + chunks = utils.str.byteTextWrap(s, length, + break_long_words=break_long_words) context = None for chunk in chunks: if context is not None: diff --git a/src/utils/str.py b/src/utils/str.py index b2670cb18..2c990e352 100644 --- a/src/utils/str.py +++ b/src/utils/str.py @@ -306,7 +306,7 @@ def perlVariableSubstitute(vars, text): return '$' + unbraced return _perlVarSubstituteRe.sub(replacer, text) -def byteTextWrap(text, size): +def byteTextWrap(text, size, break_on_hyphens=False, break_long_words=True): """Similar to textwrap.wrap(), but considers the size of strings (in bytes) instead of their length (in characters).""" words = textwrap.TextWrapper()._split_chunks(text) @@ -316,6 +316,9 @@ def byteTextWrap(text, size): lines = [b''] while words: word = words.pop(-1) + if len(word) > size: + words.append(word[size:]) + word = word[0:size] if len(lines[-1]) + len(word) <= size: lines[-1] += word else: diff --git a/test/test_ircutils.py b/test/test_ircutils.py index f7e573ab7..ed90fc8ad 100644 --- a/test/test_ircutils.py +++ b/test/test_ircutils.py @@ -195,6 +195,26 @@ class FunctionsTestCase(SupyTestCase): self.assertTrue(max(map(pred, r)) <= 100) self.assertEqual(''.join(r), s) + s = ('foobarbazqux ' * 100)[0:-1] + + r = ircutils.wrap(s, 10) + self.assertTrue(max(map(pred, r)) <= 10) + self.assertEqual(''.join(r), s) + + r = ircutils.wrap(s, 100) + self.assertTrue(max(map(pred, r)) <= 100) + self.assertEqual(''.join(r), s) + + s = ('foobarbazqux' * 100)[0:-1] + + r = ircutils.wrap(s, 10) + self.assertTrue(max(map(pred, r)) <= 10) + self.assertEqual(''.join(r), s) + + r = ircutils.wrap(s, 100) + self.assertTrue(max(map(pred, r)) <= 100) + self.assertEqual(''.join(r), s) + def testSafeArgument(self): s = 'I have been running for 9 seconds'