lock, unlock, restore.

This commit is contained in:
Jeremy Fincher 2004-04-27 11:04:36 +00:00
parent 4db5d0e536
commit 41a1c9f4d1
2 changed files with 85 additions and 35 deletions

View File

@ -1,3 +1,13 @@
* Added Topic.lock and Topic.unlock, for locking and unlocking
the topic.
* Added Topic.restore, for restoring the topic to the last-sent
topic. Useful for when people change your carefully crafted
topic through means other than the bot.
* Changed supybot.brackets so you can now provide the empty
string, which means you cannot do nesting with brackets.
* Added Anonymous, a plugin for anonymously saying things to a * Added Anonymous, a plugin for anonymously saying things to a
channel. channel.

View File

@ -45,6 +45,7 @@ import utils
import ircdb import ircdb
import ircmsgs import ircmsgs
import plugins import plugins
import ircutils
import privmsgs import privmsgs
import registry import registry
import callbacks import callbacks
@ -57,21 +58,38 @@ conf.registerChannelValue(conf.supybot.plugins.Topic, 'separator',
class Topic(callbacks.Privmsg): class Topic(callbacks.Privmsg):
topicFormatter = '%s (%s)' topicFormatter = '%s (%s)'
topicUnformatter = re.compile('(.*) \((\S+)\)') topicUnformatter = re.compile('(.*) \((\S+)\)')
def __init__(self):
callbacks.Privmsg.__init__(self)
self.lastTopics = ircutils.IrcDict()
def _splitTopic(self, topic, channel): def _splitTopic(self, topic, channel):
separator = self.registryValue('separator', channel) separator = self.registryValue('separator', channel)
return filter(None, topic.split(separator)) return filter(None, topic.split(separator))
def _joinTopic(self, topics, channel): def _joinTopic(self, channel, topics):
separator = self.registryValue('separator', channel) separator = self.registryValue('separator', channel)
return separator.join(topics) return separator.join(topics)
def _unformatTopic(self, topic, channel): def _unformatTopic(self, channel, topic):
m = self.topicUnformatter.match(topic) m = self.topicUnformatter.match(topic)
if m: if m:
return (m.group(1), m.group(2)) return (m.group(1), m.group(2))
else: else:
return (topic, '') return (topic, '')
def _formatTopic(self, channel, msg, topic):
try:
name = ircdb.users.getUser(msg.prefix).name
except KeyError:
name = msg.nick
return self.topicFormatter % (topic, name)
def _sendTopic(self, irc, channel, topics):
topics = [s for s in topics if s and not s.isspace()]
self.lastTopics[channel] = topics
newTopic = self._joinTopic(channel, topics)
irc.queueMsg(ircmsgs.topic(channel, newTopic))
def add(self, irc, msg, args, channel): def add(self, irc, msg, args, channel):
"""[<channel>] <topic> """[<channel>] <topic>
@ -85,17 +103,10 @@ class Topic(callbacks.Privmsg):
irc.error(s) irc.error(s)
return return
currentTopic = irc.state.getTopic(channel) currentTopic = irc.state.getTopic(channel)
try: formattedTopic = self._formatTopic(channel, msg, topic)
name = ircdb.users.getUser(msg.prefix).name # Empties are removed by _sendTopic.
except KeyError: self._sendTopic(irc, channel, [currentTopic, formattedTopic])
name = msg.nick add = privmsgs.channel(add)
formattedTopic = self.topicFormatter % (topic, name)
if currentTopic:
newTopic = self._joinTopic([currentTopic, formattedTopic], channel)
else:
newTopic = formattedTopic
irc.queueMsg(ircmsgs.topic(channel, newTopic))
add = privmsgs.checkChannelCapability(add, 'topic')
def shuffle(self, irc, msg, args, channel): def shuffle(self, irc, msg, args, channel):
"""[<channel>] """[<channel>]
@ -110,15 +121,12 @@ class Topic(callbacks.Privmsg):
return return
elif len(topics) == 2: elif len(topics) == 2:
topics.reverse() topics.reverse()
newtopic = self._joinTopic(topics, channel)
else: else:
original = topics[:]
while topics == original:
random.shuffle(topics) random.shuffle(topics)
newtopic = self._joinTopic(topics, channel) self._sendTopic(irc, channel, topics)
while newtopic == irc.state.getTopic(channel): shuffle = privmsgs.channel(shuffle)
random.shuffle(topics)
newtopic = self._joinTopic(topics, channel)
irc.queueMsg(ircmsgs.topic(channel, newtopic))
shuffle = privmsgs.checkChannelCapability(shuffle, 'topic')
def reorder(self, irc, msg, args, channel): def reorder(self, irc, msg, args, channel):
"""[<channel>] <number> [<number> ...] """[<channel>] <number> [<number> ...]
@ -156,13 +164,12 @@ class Topic(callbacks.Privmsg):
return return
try: try:
newtopics = [topics[i] for i in order] newtopics = [topics[i] for i in order]
newtopic = self._joinTopic(newtopics, channel) self._sendTopic(irc, channel, newtopics)
irc.queueMsg(ircmsgs.topic(channel, newtopic))
except IndexError: except IndexError:
irc.error('An invalid topic number was specified.') irc.error('An invalid topic number was specified.')
else: else:
irc.error('There are no topics to reorder.') irc.error('There are no topics to reorder.')
reorder = privmsgs.checkChannelCapability(reorder, 'topic') reorder = privmsgs.channel(reorder)
def list(self, irc, msg, args, channel): def list(self, irc, msg, args, channel):
"""[<channel>] <number> """[<channel>] <number>
@ -174,7 +181,7 @@ class Topic(callbacks.Privmsg):
topics = self._splitTopic(irc.state.getTopic(channel), channel) topics = self._splitTopic(irc.state.getTopic(channel), channel)
L = [] L = []
for (i, t) in enumerate(topics): for (i, t) in enumerate(topics):
(t, _) = self._unformatTopic(t, channel) (t, _) = self._unformatTopic(channel, t)
L.append('%s: %s' % (i+1, utils.ellipsisify(t, 30))) L.append('%s: %s' % (i+1, utils.ellipsisify(t, 30)))
s = utils.commaAndify(L) s = utils.commaAndify(L)
irc.reply(s) irc.reply(s)
@ -201,7 +208,7 @@ class Topic(callbacks.Privmsg):
topics = self._splitTopic(irc.state.getTopic(channel), channel) topics = self._splitTopic(irc.state.getTopic(channel), channel)
if topics: if topics:
try: try:
irc.reply(self._unformatTopic(topics[number], channel)[0]) irc.reply(self._unformatTopic(channel, topics[number])[0])
except IndexError: except IndexError:
irc.error('That\'s not a valid topic.') irc.error('That\'s not a valid topic.')
else: else:
@ -241,13 +248,11 @@ class Topic(callbacks.Privmsg):
irc.error('There are no topics to change.') irc.error('There are no topics to change.')
return return
topic = topics.pop(number) topic = topics.pop(number)
(topic, name) = self._unformatTopic(topic, channel) (topic, name) = self._unformatTopic(channel, topic)
try: try:
senderName = ircdb.users.getUser(msg.prefix).name senderName = ircdb.users.getUser(msg.prefix).name
except KeyError: except KeyError:
senderName = msg.nick senderName = msg.nick
# irc.errorNoUser()
# return
if name and name != senderName and \ if name and name != senderName and \
not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')): not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')):
irc.error('You can only modify your own topics.') irc.error('You can only modify your own topics.')
@ -256,9 +261,8 @@ 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._joinTopic(topics, channel) self._sendTopic(irc, channel, topics)
irc.queueMsg(ircmsgs.topic(channel, newTopic)) change = privmsgs.channel(change)
change = privmsgs.checkChannelCapability(change, 'topic')
def remove(self, irc, msg, args, channel): def remove(self, irc, msg, args, channel):
"""[<channel>] <number> """[<channel>] <number>
@ -284,7 +288,7 @@ class Topic(callbacks.Privmsg):
except IndexError: except IndexError:
irc.error('That\'s not a valid topic number.') irc.error('That\'s not a valid topic number.')
return return
(topic, name) = self._unformatTopic(topic, channel) (topic, name) = self._unformatTopic(channel, topic)
try: try:
username = ircdb.users.getUser(msg.prefix).name username = ircdb.users.getUser(msg.prefix).name
except KeyError: except KeyError:
@ -293,10 +297,46 @@ class Topic(callbacks.Privmsg):
not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')): not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')):
irc.error('You can only remove your own topics.') irc.error('You can only remove your own topics.')
return return
newTopic = self._joinTopic(topics, channel) self._sendTopic(irc, channel, topics)
irc.queueMsg(ircmsgs.topic(channel, newTopic)) remove = privmsgs.channel(remove)
remove = privmsgs.checkChannelCapability(remove, 'topic')
def lock(self, irc, msg, args, channel):
"""[<channel>]
Locks the topic (sets the mode +t) in <channel>. <channel> is only
necessary if the message isn't sent in the channel itself.
"""
if irc.nick in irc.state.channels[channel].ops:
irc.queueMsg(ircmsgs.mode(channel, '+t'))
else:
irc.error('How can I unlock the topic, I\'m not opped!')
lock = privmsgs.channel(lock)
def unlock(self, irc, msg, args, channel):
"""[<channel>]
Locks the topic (sets the mode +t) in <channel>. <channel> is only
necessary if the message isn't sent in the channel itself.
"""
if irc.nick in irc.state.channels[channel].ops:
irc.queueMsg(ircmsgs.mode(channel, '-t'))
else:
irc.error('How can I unlock the topic, I\'m not opped!')
unlock = privmsgs.channel(unlock)
def restore(self, irc, msg, args, channel):
"""[<channel>]
Restores the topic to the last topic set by the bot. <channel> is only
necessary if the message isn't sent in the channel itself.
"""
try:
topics = self.lastTopics[channel]
except KeyError:
irc.error('I haven\'t yet set the topic in %s.' % channel)
return
self._sendTopic(irc, channel, topics)
restore = privmsgs.channel(restore)
Class = Topic Class = Topic