Skip to content

Commit

Permalink
Factoids: Add supybot.plugins.Factoids.requireVoice. Closes GH-378.
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed May 17, 2013
1 parent ed62c9e commit 1090c46
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
4 changes: 4 additions & 0 deletions plugins/Factoids/config.py
Expand Up @@ -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 <key> as <value>. Users might
Expand Down
3 changes: 3 additions & 0 deletions plugins/Factoids/plugin.py
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions plugins/Factoids/test.py
Expand Up @@ -29,6 +29,7 @@
###

from supybot.test import *
import supybot.conf as conf

try:
import sqlite3
Expand Down Expand Up @@ -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/')
Expand Down
7 changes: 2 additions & 5 deletions src/commands.py
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/irclib.py
Expand Up @@ -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."
Expand Down

0 comments on commit 1090c46

Please sign in to comment.