mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-11 20:52:42 +01:00
Made the separator a configurable.
This commit is contained in:
parent
ee8be5abc1
commit
f9adc0d0c7
@ -44,15 +44,36 @@ import conf
|
|||||||
import utils
|
import utils
|
||||||
import ircdb
|
import ircdb
|
||||||
import ircmsgs
|
import ircmsgs
|
||||||
|
import plugins
|
||||||
import privmsgs
|
import privmsgs
|
||||||
import callbacks
|
import callbacks
|
||||||
|
|
||||||
class Topic(callbacks.Privmsg):
|
def ConfigurableTopicSeparator(s):
|
||||||
topicSeparator = ' || '
|
s = plugins.ConfigurableStrType(s)
|
||||||
|
if s.lstrip() == s:
|
||||||
|
s = ' ' + s
|
||||||
|
if s.rstrip() == s:
|
||||||
|
s += ' '
|
||||||
|
return s
|
||||||
|
|
||||||
|
class Topic(callbacks.Privmsg, plugins.Configurable):
|
||||||
topicFormatter = '%s (%s)'
|
topicFormatter = '%s (%s)'
|
||||||
topicUnformatter = re.compile('(.*) \((\S+)\)')
|
topicUnformatter = re.compile('(.*) \((\S+)\)')
|
||||||
def _splitTopic(self, topic):
|
configurables = plugins.ConfigurableDictionary(
|
||||||
return filter(None, topic.split(self.topicSeparator))
|
[('separator', plugins.ConfigurableStrType, ' || ',
|
||||||
|
"The separator between individual topics in the channel topic.")]
|
||||||
|
)
|
||||||
|
def __init__(self):
|
||||||
|
callbacks.Privmsg.__init__(self)
|
||||||
|
plugins.Configurable.__init__(self)
|
||||||
|
|
||||||
|
def _splitTopic(self, topic, channel):
|
||||||
|
separator = self.configurables.get('separator', channel)
|
||||||
|
return filter(None, topic.split(separator))
|
||||||
|
|
||||||
|
def _joinTopic(self, topics, channel):
|
||||||
|
separator = self.configurables.get('separator', channel)
|
||||||
|
return separator.join(topics)
|
||||||
|
|
||||||
def add(self, irc, msg, args, channel):
|
def add(self, irc, msg, args, channel):
|
||||||
"""[<channel>] <topic>
|
"""[<channel>] <topic>
|
||||||
@ -61,8 +82,9 @@ class Topic(callbacks.Privmsg):
|
|||||||
if the message isn't sent in the channel itself.
|
if the message isn't sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
topic = privmsgs.getArgs(args)
|
topic = privmsgs.getArgs(args)
|
||||||
if self.topicSeparator in topic:
|
separator = self.configurables.get('separator', channel)
|
||||||
s = 'You can\'t have %s in your topic' % self.topicSeparator
|
if separator in topic:
|
||||||
|
s = 'You can\'t have %s in your topic' % separator
|
||||||
irc.error(msg, s)
|
irc.error(msg, s)
|
||||||
return
|
return
|
||||||
currentTopic = irc.state.getTopic(channel)
|
currentTopic = irc.state.getTopic(channel)
|
||||||
@ -72,8 +94,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
name = msg.nick
|
name = msg.nick
|
||||||
formattedTopic = self.topicFormatter % (topic, name)
|
formattedTopic = self.topicFormatter % (topic, name)
|
||||||
if currentTopic:
|
if currentTopic:
|
||||||
newTopic = self.topicSeparator.join((currentTopic,
|
newTopic = self._joinTopic([currentTopic, formattedTopic], channel)
|
||||||
formattedTopic))
|
|
||||||
else:
|
else:
|
||||||
newTopic = formattedTopic
|
newTopic = formattedTopic
|
||||||
irc.queueMsg(ircmsgs.topic(channel, newTopic))
|
irc.queueMsg(ircmsgs.topic(channel, newTopic))
|
||||||
@ -86,19 +107,19 @@ class Topic(callbacks.Privmsg):
|
|||||||
message isn't sent in the channel itself.
|
message isn't sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
newtopic = irc.state.getTopic(channel)
|
newtopic = irc.state.getTopic(channel)
|
||||||
topics = self._splitTopic(irc.state.getTopic(channel))
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
if len(topics) == 0 or len(topics) == 1:
|
if len(topics) == 0 or len(topics) == 1:
|
||||||
irc.error(msg, 'I can\'t shuffle 1 or fewer topics.')
|
irc.error(msg, 'I can\'t shuffle 1 or fewer topics.')
|
||||||
return
|
return
|
||||||
elif len(topics) == 2:
|
elif len(topics) == 2:
|
||||||
topics.reverse()
|
topics.reverse()
|
||||||
newtopic = self.topicSeparator.join(topics)
|
newtopic = self._joinTopic(topics, channel)
|
||||||
else:
|
else:
|
||||||
random.shuffle(topics)
|
random.shuffle(topics)
|
||||||
newtopic = self.topicSeparator.join(topics)
|
newtopic = self._joinTopic(topics, channel)
|
||||||
while newtopic == irc.state.getTopic(channel):
|
while newtopic == irc.state.getTopic(channel):
|
||||||
random.shuffle(topics)
|
random.shuffle(topics)
|
||||||
newtopic = self.topicSeparator.join(topics)
|
newtopic = self._joinTopic(topics, channel)
|
||||||
irc.queueMsg(ircmsgs.topic(channel, newtopic))
|
irc.queueMsg(ircmsgs.topic(channel, newtopic))
|
||||||
shuffle = privmsgs.checkChannelCapability(shuffle, 'topic')
|
shuffle = privmsgs.checkChannelCapability(shuffle, 'topic')
|
||||||
|
|
||||||
@ -120,7 +141,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'The argument must be a valid integer.')
|
irc.error(msg, 'The argument must be a valid integer.')
|
||||||
return
|
return
|
||||||
topics = self._splitTopic(irc.state.getTopic(channel))
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
if topics:
|
if topics:
|
||||||
try:
|
try:
|
||||||
match = self.topicUnformatter.match(topics[number])
|
match = self.topicUnformatter.match(topics[number])
|
||||||
@ -162,7 +183,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
except re.error, e:
|
except re.error, e:
|
||||||
irc.error(msg, utils.exnToString(e))
|
irc.error(msg, utils.exnToString(e))
|
||||||
return
|
return
|
||||||
topics = self._splitTopic(irc.state.getTopic(channel))
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
if not topics:
|
if not topics:
|
||||||
irc.error(msg, 'There are no topics to change.')
|
irc.error(msg, 'There are no topics to change.')
|
||||||
return
|
return
|
||||||
@ -185,7 +206,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
if number < 0:
|
if number < 0:
|
||||||
number = len(topics)+1+number
|
number = len(topics)+1+number
|
||||||
topics.insert(number, newTopic)
|
topics.insert(number, newTopic)
|
||||||
newTopic = self.topicSeparator.join(topics)
|
newTopic = self._joinTopic(topics, channel)
|
||||||
irc.queueMsg(ircmsgs.topic(channel, newTopic))
|
irc.queueMsg(ircmsgs.topic(channel, newTopic))
|
||||||
change = privmsgs.checkChannelCapability(change, 'topic')
|
change = privmsgs.checkChannelCapability(change, 'topic')
|
||||||
|
|
||||||
@ -207,7 +228,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
irc.error(msg, 'The argument must be a number.')
|
irc.error(msg, 'The argument must be a number.')
|
||||||
return
|
return
|
||||||
topics = self._splitTopic(irc.state.getTopic(channel))
|
topics = self._splitTopic(irc.state.getTopic(channel), channel)
|
||||||
try:
|
try:
|
||||||
topic = topics.pop(number)
|
topic = topics.pop(number)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
@ -226,7 +247,7 @@ class Topic(callbacks.Privmsg):
|
|||||||
not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')):
|
not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')):
|
||||||
irc.error(msg, 'You can only remove your own topics.')
|
irc.error(msg, 'You can only remove your own topics.')
|
||||||
return
|
return
|
||||||
newTopic = self.topicSeparator.join(topics)
|
newTopic = self._joinTopic(topics, channel)
|
||||||
irc.queueMsg(ircmsgs.topic(channel, newTopic))
|
irc.queueMsg(ircmsgs.topic(channel, newTopic))
|
||||||
remove = privmsgs.checkChannelCapability(remove, 'topic')
|
remove = privmsgs.checkChannelCapability(remove, 'topic')
|
||||||
|
|
||||||
|
@ -80,6 +80,12 @@ class TopicTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
r'bar.*bazz.*biff')
|
r'bar.*bazz.*biff')
|
||||||
self.assertError('topic change 0 s/baz/biff/')
|
self.assertError('topic change 0 s/baz/biff/')
|
||||||
|
|
||||||
|
def testConfig(self):
|
||||||
|
self.assertNotError('topic config separator <==>')
|
||||||
|
_ = self.getMsg('topic add foo')
|
||||||
|
m = self.getMsg('topic add bar')
|
||||||
|
self.failUnless('<==>' in m.args[1])
|
||||||
|
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user