From a7bdd631e374ec3a4609636f7e16833653fcfb1c Mon Sep 17 00:00:00 2001 From: James Vega Date: Fri, 10 Sep 2004 15:59:38 +0000 Subject: [PATCH] Add conf.supybot.plugins.Karma.allowUnaddressedKarma --- plugins/Karma.py | 43 +++++++++++++++++++++++++++++++++++++------ test/test_Karma.py | 20 +++++++++++++++++++- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/plugins/Karma.py b/plugins/Karma.py index 7538d1fda..fde16447e 100644 --- a/plugins/Karma.py +++ b/plugins/Karma.py @@ -34,6 +34,9 @@ Plugin for handling Karma stuff for a channel. """ __revision__ = "$Id$" +__contributors__ = { + supybot.authors.jamessan: ['allowUnaddressedKarma'], + } import os import sets @@ -42,6 +45,7 @@ from itertools import imap import supybot.conf as conf import supybot.utils as utils import supybot.plugins as plugins +import supybot.ircmsgs as ircmsgs import supybot.ircutils as ircutils import supybot.privmsgs as privmsgs import supybot.registry as registry @@ -70,6 +74,9 @@ conf.registerChannelValue(conf.supybot.plugins.Karma, 'mostDisplay', conf.registerChannelValue(conf.supybot.plugins.Karma, 'allowSelfRating', registry.Boolean(False, """Determines whether users can adjust the karma of their nick.""")) +conf.registerChannelValue(conf.supybot.plugins.Karma, 'allowUnaddressedKarma', + registry.Boolean(False, """Determines whether the bot will + increase/decrease karma without being addressed.""")) class SqliteKarmaDB(object): def _getDb(self, channel): @@ -200,10 +207,10 @@ def KarmaDB(): return SqliteKarmaDB() class Karma(callbacks.PrivmsgCommandAndRegexp): - addressedRegexps = ['increaseKarma', 'decreaseKarma'] + regexps = ['increaseKarma', 'decreaseKarma'] def __init__(self): self.db = KarmaDB() - callbacks.PrivmsgCommandAndRegexp.__init__(self) + super(Karma, self).__init__() def karma(self, irc, msg, args): """[] [ [ ...]] @@ -294,12 +301,34 @@ class Karma(callbacks.PrivmsgCommandAndRegexp): irc.replySuccess() clear = privmsgs.checkChannelCapability(clear, 'op') + def getName(self, nick, msg, match): + addressed = callbacks.addressed(nick, msg) + name = callbacks.addressed(nick, + ircmsgs.IrcMsg(prefix='', + args=(msg.args[0], match.group(1)), + msg=msg)) + if not name: + name = match.group(1) + if not addressed: + if not self.registryValue('allowUnaddressedKarma'): + return '' + if not msg.args[1].startswith(match.group(1)): + return '' + name = match.group(1) + elif addressed: + if not addressed.startswith(name): + return '' + name = name.strip('()') + return name + def increaseKarma(self, irc, msg, match): - r"^(\S+|\(.+\))\+\+\s*$" + r"(\S+|\(.+\))\+\+\s*$" channel = msg.args[0] if not ircutils.isChannel(channel): return - name = match.group(1).strip('()') + name = self.getName(irc.nick, msg, match) + if not name: + return if not self.registryValue('allowSelfRating', msg.args[0]): if ircutils.strEqual(name, msg.nick): return @@ -308,11 +337,13 @@ class Karma(callbacks.PrivmsgCommandAndRegexp): irc.replySuccess() def decreaseKarma(self, irc, msg, match): - r"^(\S+|\(.+\))--\s*$" + r"(\S+|\(.+\))--\s*$" channel = msg.args[0] if not ircutils.isChannel(channel): return - name = match.group(1).strip('()') + name = self.getName(irc.nick, msg, match) + if not name: + return if not self.registryValue('allowSelfRating', msg.args[0]): if ircutils.strEqual(name, msg.nick): return diff --git a/test/test_Karma.py b/test/test_Karma.py index e734e9d6b..725c2e38c 100644 --- a/test/test_Karma.py +++ b/test/test_Karma.py @@ -39,7 +39,7 @@ except ImportError: sqlite = None if sqlite is not None: - class KarmaTestCase(ChannelPluginTestCase, PluginDocumentation): + class KarmaTestCase(ChannelPluginTestCase): plugins = ('Karma',) def testKarma(self): self.assertError('karma') @@ -194,6 +194,24 @@ if sqlite is not None: self.assertNoResponse('(foo bar)++', 1) self.assertRegexp('karma "foo bar"', '1') + def testUnaddressedKarma(self): + karma = conf.supybot.plugins.Karma + resp = karma.response() + unaddressed = karma.allowUnaddressedKarma() + try: + karma.response.setValue(True) + karma.allowUnaddressedKarma.setValue(True) + for m in ('++', '--'): + self.assertRegexp('foo%s' % m, 'operation') + self.assertSnarfRegexp('foo%s' % m, 'operation') + self.assertNoResponse('foo bar%s' % m) + self.assertSnarfNoResponse('foo bar%s' % m) + self.assertRegexp('(foo bar)%s' % m, 'operation') + self.assertSnarfRegexp('(foo bar)%s' % m, 'operation') + finally: + karma.response.setValue(resp) + karma.allowUnaddressedKarma.setValue(unaddressed) + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: