Add conf.supybot.plugins.Karma.allowUnaddressedKarma

This commit is contained in:
James Vega 2004-09-10 15:59:38 +00:00
parent 4a6740fbca
commit a7bdd631e3
2 changed files with 56 additions and 7 deletions

View File

@ -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):
"""[<channel>] [<thing> [<thing> ...]]
@ -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

View File

@ -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: