From 1090c4623ff601cd1f09889c2111dd460c13ffb9 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Fri, 17 May 2013 17:47:43 +0200 Subject: [PATCH] Factoids: Add supybot.plugins.Factoids.requireVoice. Closes GH-378. --- plugins/Factoids/config.py | 4 ++++ plugins/Factoids/plugin.py | 3 +++ plugins/Factoids/test.py | 7 +++++++ src/commands.py | 7 ++----- src/irclib.py | 6 ++++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/plugins/Factoids/config.py b/plugins/Factoids/config.py index a673b2895..84ac73250 100644 --- a/plugins/Factoids/config.py +++ b/plugins/Factoids/config.py @@ -54,6 +54,10 @@ class FactoidFormat(registry.TemplatedString): conf.registerChannelValue(Factoids.web, 'channel', registry.Boolean(False, _("""Determines whether factoids can be displayed via the web server."""))) + +conf.registerChannelValue(Factoids, 'requireVoice', + registry.Boolean(False, _("""Only allows a user with voice or above on a + channel to use the command."""))) conf.registerChannelValue(Factoids, 'learnSeparator', registry.String('as', _("""Determines what separator must be used in the learn command. Defaults to 'as' -- learn as . Users might diff --git a/plugins/Factoids/plugin.py b/plugins/Factoids/plugin.py index d491d929d..9b477ad5c 100644 --- a/plugins/Factoids/plugin.py +++ b/plugins/Factoids/plugin.py @@ -270,6 +270,9 @@ def _getKeyAndFactId(self, channel, key, factoid): return (keyresults, factresults,) def learn(self, irc, msg, args, channel, key, factoid): + if self.registryValue('requireVoice', channel) and \ + not irc.state.channels[channel].isVoicePlus(msg.nick): + irc.error(_('You have to be at least voiced to teach factoids.')) # if neither key nor factoid exist, add them. # if key exists but factoid doesn't, add factoid, link it to existing key diff --git a/plugins/Factoids/test.py b/plugins/Factoids/test.py index e6c843d78..5a5e759f5 100644 --- a/plugins/Factoids/test.py +++ b/plugins/Factoids/test.py @@ -29,6 +29,7 @@ ### from supybot.test import * +import supybot.conf as conf try: import sqlite3 @@ -76,6 +77,12 @@ def testLearn(self): self.assertError('learn foo bar baz') # No 'as' self.assertError('learn foo bar') # No 'as' + with conf.supybot.plugins.Factoids.requireVoice.context(True): + self.assertError('learn jemfinch as my primary author') + self.irc.feedMsg(ircmsgs.mode(self.channel, + args=('+h', self.nick))) + self.assertNotError('learn jemfinch as my primary author') + def testChangeFactoid(self): self.assertNotError('learn foo as bar') self.assertNotError('change foo 1 s/bar/baz/') diff --git a/src/commands.py b/src/commands.py index cce5f5a00..f1f49ff03 100644 --- a/src/commands.py +++ b/src/commands.py @@ -330,9 +330,7 @@ def getHaveVoicePlus(irc, msg, args, state, action=_('do that')): getChannel(irc, msg, args, state) if state.channel not in irc.state.channels: state.error(_('I\'m not even in %s.') % state.channel, Raise=True) - if not irc.state.channels[state.channel].isOp(irc.nick) and \ - not irc.state.channels[state.channel].isHalfop(irc.nick) and \ - not irc.state.channels[state.channel].isVoice(irc.nick): + if not irc.state.channels[state.channel].isVoicePlus(irc.nick): # isOp includes owners and protected users state.error(_('I need to be at least voiced to %s.') % action, Raise=True) @@ -350,8 +348,7 @@ def getHaveHalfopPlus(irc, msg, args, state, action=_('do that')): getChannel(irc, msg, args, state) if state.channel not in irc.state.channels: state.error(_('I\'m not even in %s.') % state.channel, Raise=True) - if not irc.state.channels[state.channel].isOp(irc.nick) and \ - not irc.state.channels[state.channel].isHalfop(irc.nick): + if not irc.state.channels[state.channel].isHalfopPlus(irc.nick): # isOp includes owners and protected users state.error(_('I need to be at least halfopped to %s.') % action, Raise=True) diff --git a/src/irclib.py b/src/irclib.py index dc3865f3f..a8e1f1e78 100644 --- a/src/irclib.py +++ b/src/irclib.py @@ -245,10 +245,16 @@ def __init__(self): def isOp(self, nick): return nick in self.ops + def isOpPlus(self, nick): + return nick in self.ops def isVoice(self, nick): return nick in self.voices + def isVoicePlus(self, nick): + return nick in self.voices or nick in self.halfops or nick in self.ops def isHalfop(self, nick): return nick in self.halfops + def isHalfopPlus(self, nick): + return nick in self.halfops or nick in self.ops def addUser(self, user): "Adds a given user to the ChannelState. Power prefixes are handled."