diff --git a/plugins/Topic/plugin.py b/plugins/Topic/plugin.py index 8cd027e2a..6276d2ef0 100644 --- a/plugins/Topic/plugin.py +++ b/plugins/Topic/plugin.py @@ -135,13 +135,18 @@ class Topic(callbacks.Privmsg): except (KeyError, IndexError): return None - def _sendTopics(self, irc, channel, topics, isDo=False): + def _sendTopics(self, irc, channel, topics, isDo=False, fit=False): topics = [s for s in topics if s and not s.isspace()] self.lastTopics[channel] = topics newTopic = self._joinTopic(channel, topics) try: maxLen = irc.state.supported['topiclen'] - if len(newTopic) > maxLen: + if fit: + while len(newTopic) > maxLen: + topics.pop(0) + self.lastTopics[channel] = topics + newTopic = self._joinTopic(channel, topics) + elif len(newTopic) > maxLen: if self.registryValue('recognizeTopiclen', channel): irc.error(format('That topic is too long for this server ' '(maximum length: %i; this topic: %i).', @@ -185,6 +190,19 @@ class Topic(callbacks.Privmsg): self._sendTopics(irc, channel, topics) add = wrap(add, ['canChangeTopic', rest('topic')]) + def fit(self, irc, msg, args, channel, topic): + """[] + + Adds to the topics for . If the topic is too long + for the server, topics will be popped until there is enough room. + is only necessary if the message isn't sent in the channel + itself. + """ + topics = self._splitTopic(irc.state.getTopic(channel), channel) + topics.append(topic) + self._sendTopics(irc, channel, topics, fit=True) + fit = wrap(fit, ['canChangeTopic', rest('topic')]) + def replace(self, irc, msg, args, channel, i, topic): """[] diff --git a/plugins/Topic/test.py b/plugins/Topic/test.py index 1dd889f09..e1a1b64b5 100644 --- a/plugins/Topic/test.py +++ b/plugins/Topic/test.py @@ -51,7 +51,7 @@ class TopicTestCase(ChannelPluginTestCase): self.assertRegexp('topic replace 2 lorem ipsum', 'oof.*lorem ipsum.*zab') self.assertRegexp('topic replace 2 rab', 'oof.*rab.*zab') - + def testGet(self): self.assertError('topic get 1') _ = self.getMsg('topic add foo') @@ -186,7 +186,7 @@ class TopicTestCase(ChannelPluginTestCase): self.assertError('topic swap -3 1') finally: conf.supybot.plugins.Topic.format.setValue(original) - + def testDefault(self): self.assertError('topic default') try: @@ -219,8 +219,20 @@ class TopicTestCase(ChannelPluginTestCase): self.assertResponse('topic separator ||', 'foo || bar || baz') finally: conf.supybot.plugins.Topic.format.setValue(original) - - + + def testFit(self): + original = conf.supybot.plugins.Topic.format() + try: + conf.supybot.plugins.Topic.format.setValue('$topic') + self.irc.state.supported['TOPICLEN'] = 20 + self.assertResponse('topic fit foo', 'foo') + self.assertResponse('topic fit bar', 'foo || bar') + self.assertResponse('topic fit baz', 'foo || bar || baz') + self.assertResponse('topic fit qux', 'bar || baz || qux') + finally: + conf.supybot.plugins.Topic.format.setValue(original) + self.irc.state.supported.pop('TOPICLEN', None) + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: