Added Topic.fit.

This commit is contained in:
James Vega 2005-02-04 05:04:40 +00:00
parent 6b75be6e60
commit 14837bc544
2 changed files with 36 additions and 6 deletions

View File

@ -135,13 +135,18 @@ class Topic(callbacks.Privmsg):
except (KeyError, IndexError): except (KeyError, IndexError):
return None 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()] topics = [s for s in topics if s and not s.isspace()]
self.lastTopics[channel] = topics self.lastTopics[channel] = topics
newTopic = self._joinTopic(channel, topics) newTopic = self._joinTopic(channel, topics)
try: try:
maxLen = irc.state.supported['topiclen'] 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): if self.registryValue('recognizeTopiclen', channel):
irc.error(format('That topic is too long for this server ' irc.error(format('That topic is too long for this server '
'(maximum length: %i; this topic: %i).', '(maximum length: %i; this topic: %i).',
@ -185,6 +190,19 @@ class Topic(callbacks.Privmsg):
self._sendTopics(irc, channel, topics) self._sendTopics(irc, channel, topics)
add = wrap(add, ['canChangeTopic', rest('topic')]) add = wrap(add, ['canChangeTopic', rest('topic')])
def fit(self, irc, msg, args, channel, topic):
"""[<channel>] <topic>
Adds <topic> to the topics for <channel>. If the topic is too long
for the server, topics will be popped until there is enough room.
<channel> 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): def replace(self, irc, msg, args, channel, i, topic):
"""[<channel>] <number> <topic> """[<channel>] <number> <topic>

View File

@ -220,6 +220,18 @@ class TopicTestCase(ChannelPluginTestCase):
finally: finally:
conf.supybot.plugins.Topic.format.setValue(original) 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: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: