diff --git a/plugins/Topic.py b/plugins/Topic.py index b6e6c8e3b..799216088 100644 --- a/plugins/Topic.py +++ b/plugins/Topic.py @@ -139,7 +139,7 @@ class Topic(callbacks.Privmsg): else: return True - def _topicNumber(self, irc, n, topics=None): + def _topicNumber(self, irc, n, topics=None, normalize=False): try: n = int(n) if not n: @@ -148,6 +148,10 @@ class Topic(callbacks.Privmsg): n -= 1 if topics is not None: topics[n] + if normalize: + assert topics, 'Can\'t normalize without topics.' + if n < 0: + n += len(topics) return n except (ValueError, IndexError): irc.error('That\'s not a valid topic number.', Raise=True) @@ -380,6 +384,27 @@ class Topic(callbacks.Privmsg): irc.error('There are no more undos for %s.' % channel) undo = privmsgs.channel(undo) + def swap(self, irc, msg, args, channel): + """[] + + Swaps the order of the first topic number and the second topic number. + is only necessary if the message isn't sent in the channel + itself. + """ + self._canChangeTopic(irc, channel) + (first, second) = privmsgs.getArgs(args, required=2) + topics = self._splitTopic(irc.state.getTopic(channel), channel) + first = self._topicNumber(irc, first, topics, normalize=True) + second = self._topicNumber(irc, second, topics, normalize=True) + if first == second: + irc.error('I refuse to swap the same topic with itself.') + return + t = topics[first] + topics[first] = topics[second] + topics[second] = t + self._sendTopics(irc, channel, topics) + swap = privmsgs.channel(swap) + def default(self, irc, msg, args, channel): """[] diff --git a/test/test_Topic.py b/test/test_Topic.py index 7fa51d245..162f72bb5 100644 --- a/test/test_Topic.py +++ b/test/test_Topic.py @@ -130,6 +130,24 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation): self.assertResponse('topic undo', '') finally: conf.supybot.plugins.Topic.format.setValue(original) + + def testSwap(self): + try: + original = conf.supybot.plugins.Topic.format() + conf.supybot.plugins.Topic.format.setValue('$topic') + self.assertResponse('topic set ""', '') + self.assertResponse('topic add foo', 'foo') + self.assertResponse('topic add bar', 'foo || bar') + self.assertResponse('topic add baz', 'foo || bar || baz') + self.assertResponse('topic swap 1 2', 'bar || foo || baz') + self.assertResponse('topic swap 1 -1', 'baz || foo || bar') + self.assertError('topic swap -1 -1') + self.assertError('topic swap 2 -2') + self.assertError('topic swap 1 -3') + self.assertError('topic swap -2 2') + self.assertError('topic swap -3 1') + finally: + conf.supybot.plugins.Topic.format.setValue(original) def testDefault(self): self.assertError('topic default')