Merge pull request #1004 from GLolol/karma/nicks-only

Karma: add an option to limit Karma to nicks
This commit is contained in:
Valentin Lorentz 2015-02-07 08:41:02 +01:00
commit fa7ef570c0
3 changed files with 46 additions and 17 deletions

View File

@ -66,5 +66,8 @@ conf.registerChannelValue(Karma, 'allowSelfRating',
conf.registerChannelValue(Karma, 'allowUnaddressedKarma',
registry.Boolean(True, _("""Determines whether the bot will
increase/decrease karma without being addressed.""")))
conf.registerChannelValue(Karma, 'onlyNicks',
registry.Boolean(False, _("""Determines whether the bot will
only increase/decrease karma for nicks in the current channel.""")))
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -246,25 +246,34 @@ class Karma(callbacks.Plugin):
def _doKarma(self, irc, msg, channel, thing):
inc = self.registryValue('incrementChars', channel)
dec = self.registryValue('decrementChars', channel)
if thing.endswith(tuple(inc + dec)):
for s in inc:
if thing.endswith(s):
thing = thing[:-len(s)]
if ircutils.strEqual(thing, msg.nick) and \
not self.registryValue('allowSelfRating', channel):
onlynicks = self.registryValue('onlyNicks', channel)
karma = ''
for s in inc:
if thing.endswith(s):
thing = thing[:-len(s)]
# Don't reply if the target isn't a nick
if onlynicks and thing.lower() not in map(ircutils.toLower,
irc.state.channels[channel].users):
return
if ircutils.strEqual(thing, msg.nick) and \
not self.registryValue('allowSelfRating', channel):
irc.error(_('You\'re not allowed to adjust your own karma.'))
return
self.db.increment(channel, self._normalizeThing(thing))
karma = self.db.get(channel, self._normalizeThing(thing))
for s in dec:
if thing.endswith(s):
thing = thing[:-len(s)]
if ircutils.strEqual(thing, msg.nick) and \
not self.registryValue('allowSelfRating', channel):
irc.error(_('You\'re not allowed to adjust your own karma.'))
return
self.db.decrement(channel, self._normalizeThing(thing))
karma = self.db.get(channel, self._normalizeThing(thing))
self.db.increment(channel, self._normalizeThing(thing))
karma = self.db.get(channel, self._normalizeThing(thing))
for s in dec:
if thing.endswith(s):
thing = thing[:-len(s)]
if onlynicks and thing.lower() not in map(ircutils.toLower,
irc.state.channels[channel].users):
return
if ircutils.strEqual(thing, msg.nick) and \
not self.registryValue('allowSelfRating', channel):
irc.error(_('You\'re not allowed to adjust your own karma.'))
return
self.db.decrement(channel, self._normalizeThing(thing))
karma = self.db.get(channel, self._normalizeThing(thing))
if karma:
self._respond(irc, channel, thing, karma[0]-karma[1])
def invalidCommand(self, irc, msg, tokens):

View File

@ -204,4 +204,21 @@ class KarmaTestCase(ChannelPluginTestCase):
karma.response.setValue(resp)
karma.allowUnaddressedKarma.setValue(unaddressed)
def testOnlyNicks(self):
# We use this to join a dummy user to test upon
msg = ircmsgs.join(self.channel, prefix='hello!foo@bar')
self.irc.feedMsg(msg)
karma = conf.supybot.plugins.Karma
resp = karma.response()
onlynicks = karma.onlyNicks()
try:
karma.onlynicks.setValue(True)
karma.response.setValue(True)
self.assertSnarfNoResponse('abcd++')
self.assertSnarfRegexp('hello--', 'is now')
self.assertSnarfNoResponse('abcd--')
self.assertSnarfRegexp('hello++', 'is now')
finally:
karma.onlynicks.setValue(onlynicks)
karma.response.setValue(resp)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: