Added selfstats toggle; Fixed karma to be case-insensitive but case-preserving.

This commit is contained in:
Jeremy Fincher 2003-11-02 18:59:06 +00:00
parent 4d77ffc92e
commit d816bc6a68
2 changed files with 46 additions and 27 deletions

View File

@ -30,10 +30,7 @@
### ###
""" """
Silently listens to every message received on a channel and keeps statistics Silently listens to every message received on a channel and keeps statistics concerning joins, parts, and various other commands in addition to tracking statistics about smileys, actions, characters, and words. """
concerning joins, parts, and various other commands in addition to tracking
statistics about smileys, actions, characters, and words.
"""
import plugins import plugins
@ -50,6 +47,7 @@ import debug
import utils import utils
import ircdb import ircdb
import ircmsgs import ircmsgs
import plugins
import privmsgs import privmsgs
import ircutils import ircutils
import callbacks import callbacks
@ -76,11 +74,15 @@ frowns = (':|', ':-/', ':-\\', ':\\', ':/', ':(', ':-(', ':\'(')
smileyre = re.compile('|'.join(map(re.escape, smileys))) smileyre = re.compile('|'.join(map(re.escape, smileys)))
frownre = re.compile('|'.join(map(re.escape, frowns))) frownre = re.compile('|'.join(map(re.escape, frowns)))
class ChannelDB(plugins.ChannelDBHandler, callbacks.PrivmsgCommandAndRegexp): class ChannelDB(plugins.ChannelDBHandler,
callbacks.PrivmsgCommandAndRegexp,
plugins.Toggleable):
addressedRegexps = sets.Set(['increaseKarma', 'decreaseKarma']) addressedRegexps = sets.Set(['increaseKarma', 'decreaseKarma'])
toggles = plugins.ToggleDictionary({'selfstats': True})
def __init__(self): def __init__(self):
plugins.ChannelDBHandler.__init__(self) plugins.ChannelDBHandler.__init__(self)
callbacks.PrivmsgCommandAndRegexp.__init__(self) callbacks.PrivmsgCommandAndRegexp.__init__(self)
plugins.Toggleable.__init__(self)
self.lastmsg = None self.lastmsg = None
self.laststate = None self.laststate = None
self.outFiltering = False self.outFiltering = False
@ -135,7 +137,8 @@ class ChannelDB(plugins.ChannelDBHandler, callbacks.PrivmsgCommandAndRegexp):
cursor.execute("""CREATE TABLE karma ( cursor.execute("""CREATE TABLE karma (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT UNIQUE ON CONFLICT IGNORE, name TEXT,
normalized TEXT UNIQUE ON CONFLICT IGNORE,
added INTEGER, added INTEGER,
subtracted INTEGER subtracted INTEGER
)""") )""")
@ -206,6 +209,7 @@ class ChannelDB(plugins.ChannelDBHandler, callbacks.PrivmsgCommandAndRegexp):
def outFilter(self, irc, msg): def outFilter(self, irc, msg):
if msg.command == 'PRIVMSG': if msg.command == 'PRIVMSG':
if ircutils.isChannel(msg.args[0]): if ircutils.isChannel(msg.args[0]):
if self.toggles.get('selfstats', msg.args[0]):
db = self.getDb(msg.args[0]) db = self.getDb(msg.args[0])
cursor = db.cursor() cursor = db.cursor()
try: try:
@ -359,28 +363,28 @@ class ChannelDB(plugins.ChannelDBHandler, callbacks.PrivmsgCommandAndRegexp):
db = self.getDb(channel) db = self.getDb(channel)
cursor = db.cursor() cursor = db.cursor()
if len(args) == 1: if len(args) == 1:
orig_name = args[0] name = args[0]
name = args[0].lower() normalized = name.lower()
cursor.execute("""SELECT added, subtracted cursor.execute("""SELECT added, subtracted
FROM karma FROM karma
WHERE name=%s""", name) WHERE normalized=%s""", normalized)
if cursor.rowcount == 0: if cursor.rowcount == 0:
irc.reply(msg, '%s has no karma.' % orig_name) irc.reply(msg, '%s has no karma.' % name)
else: else:
(added, subtracted) = map(int, cursor.fetchone()) (added, subtracted) = map(int, cursor.fetchone())
total = added - subtracted total = added - subtracted
s = 'Karma for %r has been increased %s %s ' \ s = 'Karma for %r has been increased %s %s ' \
'and decreased %s %s for a total karma of %s.' % \ 'and decreased %s %s for a total karma of %s.' % \
(orig_name, added, utils.pluralize(added, 'time'), (name, added, utils.pluralize(added, 'time'),
subtracted, utils.pluralize(subtracted, 'time'), total) subtracted, utils.pluralize(subtracted, 'time'), total)
irc.reply(msg, s) irc.reply(msg, s)
elif len(args) > 1: elif len(args) > 1:
lowered_args = map(str.lower, args) normalizedArgs = map(str.lower, args)
criteria = ' OR '.join(['name=%s'] * len(args)) criteria = ' OR '.join(['normalized=%s'] * len(args))
sql = """SELECT name, added-subtracted sql = """SELECT name, added-subtracted
FROM karma WHERE %s FROM karma WHERE %s
ORDER BY added-subtracted DESC""" % criteria ORDER BY added-subtracted DESC""" % criteria
cursor.execute(sql, *lowered_args) cursor.execute(sql, *normalizedArgs)
if cursor.rowcount > 0: if cursor.rowcount > 0:
s = utils.commaAndify(['%s: %s' % (n, t) s = utils.commaAndify(['%s: %s' % (n, t)
for (n,t) in cursor.fetchall()]) for (n,t) in cursor.fetchall()])
@ -405,21 +409,27 @@ class ChannelDB(plugins.ChannelDBHandler, callbacks.PrivmsgCommandAndRegexp):
def increaseKarma(self, irc, msg, match): def increaseKarma(self, irc, msg, match):
r"^(\S+)\+\+$" r"^(\S+)\+\+$"
name = match.group(1).lower() name = match.group(1)
normalized = name.lower()
db = self.getDb(msg.args[0]) db = self.getDb(msg.args[0])
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""INSERT INTO karma VALUES (NULL, %s, 0, 0)""", name) cursor.execute("""INSERT INTO karma VALUES (NULL, %s, %s, 0, 0)""",
cursor.execute("""UPDATE karma SET added=added+1 WHERE name=%s""",name) name, normalized)
cursor.execute("""UPDATE karma
SET added=added+1
WHERE normalized=%s""", normalized)
def decreaseKarma(self, irc, msg, match): def decreaseKarma(self, irc, msg, match):
r"^(\S+)--$" r"^(\S+)--$"
name = match.group(1).lower() name = match.group(1)
normalized = name.lower()
db = self.getDb(msg.args[0]) db = self.getDb(msg.args[0])
cursor = db.cursor() cursor = db.cursor()
cursor.execute("""INSERT INTO karma VALUES (NULL, %s, 0, 0)""", name) cursor.execute("""INSERT INTO karma VALUES (NULL, %s, %s, 0, 0)""",
name, normalized)
cursor.execute("""UPDATE karma cursor.execute("""UPDATE karma
SET subtracted=subtracted+1 SET subtracted=subtracted+1
WHERE name=%s""", name) WHERE normalized=%s""", normalized)
def stats(self, irc, msg, args): def stats(self, irc, msg, args):
"""[<channel>] [<name>] """[<channel>] [<name>]

View File

@ -60,6 +60,15 @@ if sqlite is not None:
self.assertNotError('channeldb stats') self.assertNotError('channeldb stats')
self.assertRegexp('channeldb stats', self.nick) self.assertRegexp('channeldb stats', self.nick)
def testSelfStats(self):
self.assertError('channeldb stats %s' % self.irc.nick)
self.assertNotError('channeldb stats %s' % self.irc.nick)
self.assertNotError('channeldb stats %s' % self.irc.nick)
m1 = self.getMsg('channeldb stats %s' % self.irc.nick)
self.assertNotError('channeldb toggle selfstats off')
m2 = self.getMsg('channeldb stats %s' % self.irc.nick)
self.assertEqual(m1.args[1], m2.args[1])
def testNoKeyErrorEscapeFromSeen(self): def testNoKeyErrorEscapeFromSeen(self):
self.assertRegexp('seen asldfkjasdlfkj', 'I have not seen') self.assertRegexp('seen asldfkjasdlfkj', 'I have not seen')
self.assertNotRegexp('seen asldfkjasdlfkj', 'KeyError') self.assertNotRegexp('seen asldfkjasdlfkj', 'KeyError')