From 40310c4d5ad29b4ec6c88b018623f49a0c2c4f65 Mon Sep 17 00:00:00 2001 From: GLolol Date: Sun, 11 Jan 2015 19:47:20 -0800 Subject: [PATCH 1/4] Karma: only track karma for nicks --- plugins/Karma/plugin.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plugins/Karma/plugin.py b/plugins/Karma/plugin.py index 2b8671530..3ce2bc7f4 100644 --- a/plugins/Karma/plugin.py +++ b/plugins/Karma/plugin.py @@ -250,6 +250,10 @@ class Karma(callbacks.Plugin): for s in inc: if thing.endswith(s): thing = thing[:-len(s)] + # Don't reply if the target isn't a nick + if 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.')) @@ -259,6 +263,9 @@ class Karma(callbacks.Plugin): for s in dec: if thing.endswith(s): thing = thing[:-len(s)] + if 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.')) From 472921389897757edeeb1027dd3cead728228a74 Mon Sep 17 00:00:00 2001 From: GLolol Date: Mon, 12 Jan 2015 18:08:44 -0800 Subject: [PATCH 2/4] Karma: refactor _doKarma handling --- plugins/Karma/plugin.py | 49 +++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/plugins/Karma/plugin.py b/plugins/Karma/plugin.py index 3ce2bc7f4..59275410d 100644 --- a/plugins/Karma/plugin.py +++ b/plugins/Karma/plugin.py @@ -246,32 +246,33 @@ 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)] - # Don't reply if the target isn't a nick - if 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): + karma = '' + for s in inc: + if thing.endswith(s): + thing = thing[:-len(s)] + # Don't reply if the target isn't a nick + if 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 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)) + 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 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): From bc7430cce49296a888a18ad9dd4782ecc64cd3e8 Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 14 Jan 2015 21:26:50 -0500 Subject: [PATCH 3/4] Karma: make onlyNicks a config option Conflicts: plugins/Karma/plugin.py --- plugins/Karma/config.py | 3 +++ plugins/Karma/plugin.py | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/plugins/Karma/config.py b/plugins/Karma/config.py index 376348b20..fcc62db1f 100644 --- a/plugins/Karma/config.py +++ b/plugins/Karma/config.py @@ -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: diff --git a/plugins/Karma/plugin.py b/plugins/Karma/plugin.py index 59275410d..c29bedabb 100644 --- a/plugins/Karma/plugin.py +++ b/plugins/Karma/plugin.py @@ -246,12 +246,13 @@ class Karma(callbacks.Plugin): def _doKarma(self, irc, msg, channel, thing): inc = self.registryValue('incrementChars', channel) dec = self.registryValue('decrementChars', 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 thing.lower() not in map(ircutils.toLower, + if onlynicks and thing.lower() not in map(ircutils.toLower, irc.state.channels[channel].users): return if ircutils.strEqual(thing, msg.nick) and \ @@ -263,7 +264,7 @@ class Karma(callbacks.Plugin): for s in dec: if thing.endswith(s): thing = thing[:-len(s)] - if thing.lower() not in map(ircutils.toLower, + if onlynicks and thing.lower() not in map(ircutils.toLower, irc.state.channels[channel].users): return if ircutils.strEqual(thing, msg.nick) and \ From 981ec12baa410fb373b755fac1273e812cd744de Mon Sep 17 00:00:00 2001 From: James Lu Date: Fri, 16 Jan 2015 19:44:29 -0500 Subject: [PATCH 4/4] Karma: add tests for onlyNicks --- plugins/Karma/test.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/plugins/Karma/test.py b/plugins/Karma/test.py index f615646d5..b5e4e5edc 100644 --- a/plugins/Karma/test.py +++ b/plugins/Karma/test.py @@ -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: