Added privmsgs.name, privmsgs.channel, and privmsgs.checkChannelCapability. Converted the Topic plugin to use some of them.

This commit is contained in:
Jeremy Fincher 2003-09-17 22:38:03 +00:00
parent fd1aacbd64
commit d227111b0c
2 changed files with 94 additions and 64 deletions

View File

@ -68,54 +68,48 @@ class Topic(callbacks.Privmsg):
topicSeparator = ' || ' topicSeparator = ' || '
topicFormatter = '%s (%s)' topicFormatter = '%s (%s)'
topicUnformatter = re.compile('(.*) \((\S+)\)') topicUnformatter = re.compile('(.*) \((\S+)\)')
def addtopic(self, irc, msg, args): def addtopic(self, irc, msg, args, channel):
"""[<channel>] <topic> """[<channel>] <topic>
Adds <topic> to the topics for <channel>. <channel> is only necessary Adds <topic> to the topics for <channel>. <channel> is only necessary
if the message isn't sent in the channel itself. if the message isn't sent in the channel itself.
""" """
channel = privmsgs.getChannel(msg, args)
capability = ircdb.makeChannelCapability(channel, 'topic')
topic = privmsgs.getArgs(args) topic = privmsgs.getArgs(args)
if ircdb.checkCapability(msg.prefix, capability): if self.topicSeparator in topic:
if self.topicSeparator in topic: s = 'You can\'t have %s in your topic' % self.topicSeparator
s = 'You can\'t have %s in your topic' % self.topicSeparator irc.error(msg, s)
irc.error(msg, s) return
return currentTopic = irc.state.getTopic(channel)
currentTopic = irc.state.getTopic(channel) try:
try: name = ircdb.users.getUser(msg.prefix).name
name = ircdb.users.getUser(msg.prefix).name except KeyError:
except KeyError: 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.topicSeparator.join((currentTopic, formattedTopic))
formattedTopic))
else:
newTopic = formattedTopic
irc.queueMsg(ircmsgs.topic(channel, newTopic))
else: else:
irc.error(msg, conf.replyNoCapability % capability) newTopic = formattedTopic
irc.queueMsg(ircmsgs.topic(channel, newTopic))
addtopic = privmsgs.checkChannelCapability(addtopic, 'topic')
def shuffletopic(self, irc, msg, args): def shuffletopic(self, irc, msg, args, channel):
"""[<channel>] """[<channel>]
Shuffles the topics in <channel>. <channel> is only necessary if the Shuffles the topics in <channel>. <channel> is only necessary if the
message isn't sent in the channel itself. message isn't sent in the channel itself.
""" """
channel = privmsgs.getChannel(msg, args) newtopic = irc.state.getTopic(channel)
capability = ircdb.makeChannelCapability(channel, 'topic') topics = newtopic.split(self.topicSeparator)
if ircdb.checkCapability(msg.prefix, capability): random.shuffle(topics)
newtopic = irc.state.getTopic(channel) newtopic = self.topicSeparator.join(topics)
while newtopic == irc.state.getTopic(channel): while len(topics) > 1 and newtopic == irc.state.getTopic(channel):
topics = irc.state.getTopic(channel).split(self.topicSeparator) random.shuffle(topics)
random.shuffle(topics) newtopic = self.topicSeparator.join(topics)
newtopic = self.topicSeparator.join(topics) irc.queueMsg(ircmsgs.topic(channel, newtopic))
irc.queueMsg(ircmsgs.topic(channel, newtopic)) shuffletopic = privmsgs.checkChannelCapability(shuffletopic, 'topic')
else:
irc.error(msg, conf.replyNoCapability % capability)
def topic(self, irc, msg, args): def gettopic(self, irc, msg, args, channel):
"""[<channel>] <number> """[<channel>] <number>
Returns topic number <number> from <channel>. <number> is a zero-based Returns topic number <number> from <channel>. <number> is a zero-based
@ -128,14 +122,15 @@ 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 = irc.state.getTopic(msg.args[0]).split(self.topicSeparator) topics = irc.state.getTopic(channel).split(self.topicSeparator)
try: try:
irc.reply(msg, topics[i]) irc.reply(msg, topics[i])
except IndexError: except IndexError:
irc.error(msg, 'That\'s not a valid index.') irc.error(msg, 'That\'s not a valid index.')
return return
gettopic = privmsgs.channel(topicget)
def changetopic(self, irc, msg, args): def changetopic(self, irc, msg, args, channel):
"""[<channel>] <number> <regexp> """[<channel>] <number> <regexp>
Changes the topic number <number> on <channel> according to the regular Changes the topic number <number> on <channel> according to the regular
@ -144,7 +139,6 @@ class Topic(callbacks.Privmsg):
s/regexp/replacement/flags. <channel> is only necessary if the message s/regexp/replacement/flags. <channel> is only necessary if the message
isn't sent in the channel itself. isn't sent in the channel itself.
""" """
channel = privmsgs.getChannel(msg, args)
(number, regexp) = privmsgs.getArgs(args, needed=2) (number, regexp) = privmsgs.getArgs(args, needed=2)
try: try:
number = int(number) number = int(number)
@ -180,8 +174,9 @@ class Topic(callbacks.Privmsg):
topics.insert(number, newTopic) topics.insert(number, newTopic)
newTopic = self.topicSeparator.join(topics) newTopic = self.topicSeparator.join(topics)
irc.queueMsg(ircmsgs.topic(channel, newTopic)) irc.queueMsg(ircmsgs.topic(channel, newTopic))
changetopic = privmsgs.checkChannelCapability(changetopic, 'topic')
def removetopic(self, irc, msg, args): def removetopic(self, irc, msg, args, channel):
"""[<channel>] <number> """[<channel>] <number>
Removes topic <number> from the topic for <channel> Topics are Removes topic <number> from the topic for <channel> Topics are
@ -189,38 +184,34 @@ class Topic(callbacks.Privmsg):
to topics starting the from the end of the topic. <channel> is only to topics starting the from the end of the topic. <channel> is only
necessary if the message isn't sent in the channel itself. necessary if the message isn't sent in the channel itself.
""" """
channel = privmsgs.getChannel(msg, args)
capability = ircdb.makeChannelCapability(channel, 'topic')
try: try:
number = int(privmsgs.getArgs(args)) number = int(privmsgs.getArgs(args))
except ValueError: except ValueError:
irc.error(msg, 'The argument must be a number.') irc.error(msg, 'The argument must be a number.')
return return
if ircdb.checkCapability(msg.prefix, capability): topics = irc.state.getTopic(channel).split(self.topicSeparator)
topics = irc.state.getTopic(channel).split(self.topicSeparator) try:
try: topic = topics.pop(number)
topic = topics.pop(number) except IndexError:
except IndexError: irc.error(msg, 'That\'s not a valid topic number.')
irc.error(msg, 'That\'s not a valid topic number.') return
return ## debug.printf(topic)
## debug.printf(topic) match = self.topicUnformatter.match(topic)
match = self.topicUnformatter.match(topic) if match is None:
if match is None: name = ''
name = ''
else:
(topic, name) = match.groups()
try:
username = ircdb.users.getUser(msg.prefix).name
except KeyError:
username = msg.nick
if name and name != username and \
not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')):
irc.error(msg, 'You can only remove your own topics.')
return
newTopic = self.topicSeparator.join(topics)
irc.queueMsg(ircmsgs.topic(channel, newTopic))
else: else:
irc.error(msg, conf.replyNoCapability % capability) (topic, name) = match.groups()
try:
username = ircdb.users.getUser(msg.prefix).name
except KeyError:
username = msg.nick
if name and name != username and \
not ircdb.checkCapabilities(msg.prefix, ('op', 'admin')):
irc.error(msg, 'You can only remove your own topics.')
return
newTopic = self.topicSeparator.join(topics)
irc.queueMsg(ircmsgs.topic(channel, newTopic))
removetopic = privmsgs.checkChannelCapability(removetopic, 'topic')
Class = Topic Class = Topic

View File

@ -86,6 +86,19 @@ def checkCapability(f, capability):
newf.__doc__ = f.__doc__ newf.__doc__ = f.__doc__
return newf return newf
def checkChannelCapability(f, capability):
"""Makes sure a user has a certain channel capability before running f."""
def newf(self, irc, msg, args):
channel = getChannel(msg, args) # Make a copy, f might getChannel.
chancap = ircdb.makeChannelCapability(channel, capability)
if ircdb.checkCapability(msg.prefix, chancap):
ff = new.instancemethod(f, self, self.__class__)
ff(irc, msg, args, channel)
else:
irc.error(msg, conf.replyNoCapability % chancap)
newf.__doc__ = f.__doc__
return newf
def thread(f): def thread(f):
"""Makes sure a command spawns a thread when called.""" """Makes sure a command spawns a thread when called."""
def newf(self, irc, msg, args): def newf(self, irc, msg, args):
@ -95,6 +108,32 @@ def thread(f):
newf.__doc__ = f.__doc__ newf.__doc__ = f.__doc__
return newf return newf
def name(f):
"""Makes sure a name is available based on conf.requireRegistration."""
def newf(self, irc, msg, args):
try:
name = ircdb.users.getUser(msg.prefix).name
except KeyError:
if conf.requireRegistration:
irc.error(msg, conf.replyNotRegistered)
return
else:
name = msg.prefix
ff = new.instancemethod(f, self, self.__class__)
ff(irc, msg, args, name)
newf.__doc__ = f.__doc__
return newf
def channel(f):
"""Gives the command an extra channel arg as if it had called getChannel"""
def newf(self, irc, msg, args):
channel = getChannel(msg, args)
ff = new.instancemethod(f, self, self.__class__)
ff(irc, msg, args, channel)
newf.__doc__ = f.__doc__
return newf
class CapabilityCheckingPrivmsg(callbacks.Privmsg): class CapabilityCheckingPrivmsg(callbacks.Privmsg):
"""A small subclass of callbacks.Privmsg that checks self.capability """A small subclass of callbacks.Privmsg that checks self.capability
before allowing any command to be called. before allowing any command to be called.