2004-01-20 08:15:37 +01:00
|
|
|
#!/usr/bin/env python
|
2003-11-04 09:42:11 +01:00
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2003-12-02 00:47:37 +01:00
|
|
|
Plugin for handling Karma stuff for a channel.
|
2003-11-04 09:42:11 +01:00
|
|
|
"""
|
|
|
|
|
2003-12-02 00:47:37 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-11-04 09:42:11 +01:00
|
|
|
import os
|
2003-11-21 13:31:51 +01:00
|
|
|
import sets
|
2003-11-15 05:37:04 +01:00
|
|
|
from itertools import imap
|
2003-11-04 09:42:11 +01:00
|
|
|
|
2003-12-16 14:06:21 +01:00
|
|
|
import conf
|
2003-11-04 09:42:11 +01:00
|
|
|
import utils
|
|
|
|
import plugins
|
|
|
|
import privmsgs
|
2004-01-27 16:28:00 +01:00
|
|
|
import registry
|
2003-11-04 09:42:11 +01:00
|
|
|
import callbacks
|
|
|
|
|
2003-12-04 00:48:00 +01:00
|
|
|
try:
|
|
|
|
import sqlite
|
|
|
|
except ImportError:
|
|
|
|
raise callbacks.Error, 'You need to have PySQLite installed to use this ' \
|
|
|
|
'plugin. Download it at <http://pysqlite.sf.net/>'
|
2003-11-04 09:42:11 +01:00
|
|
|
|
2004-01-27 16:28:00 +01:00
|
|
|
|
|
|
|
conf.registerPlugin('Karma')
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Karma, 'simpleOutput',
|
|
|
|
registry.Boolean(False, """Determines whether the bot will output shorter
|
|
|
|
versions of the karma output when requesting a single thing's karma."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Karma, 'karmaResponse',
|
|
|
|
registry.Boolean(False, """Determines whether the bot will reply with a
|
|
|
|
success message when something's karma is incrneased or decreased."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Karma, 'karmaRankingDisplay',
|
|
|
|
registry.Integer(3, """Determines how many highest/lowest karma things are
|
|
|
|
shown when karma is called with no arguments."""))
|
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Karma, 'karmaMostDisplay',
|
|
|
|
registry.Integer(25, """Determines how many karma things are shown when
|
|
|
|
the most command is called.'"""))
|
2003-11-04 09:42:11 +01:00
|
|
|
|
2004-01-30 23:12:27 +01:00
|
|
|
class Karma(callbacks.PrivmsgCommandAndRegexp, plugins.ChannelDBHandler):
|
|
|
|
addressedRegexps = ['increaseKarma', 'decreaseKarma']
|
2003-11-04 09:42:11 +01:00
|
|
|
def __init__(self):
|
|
|
|
callbacks.PrivmsgCommandAndRegexp.__init__(self)
|
2003-11-26 18:07:20 +01:00
|
|
|
plugins.ChannelDBHandler.__init__(self)
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
callbacks.PrivmsgCommandAndRegexp.die(self)
|
|
|
|
plugins.ChannelDBHandler.die(self)
|
2003-11-04 09:42:11 +01:00
|
|
|
|
|
|
|
def makeDb(self, filename):
|
|
|
|
if os.path.exists(filename):
|
|
|
|
db = sqlite.connect(filename)
|
|
|
|
else:
|
|
|
|
db = sqlite.connect(filename)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""CREATE TABLE karma (
|
|
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
name TEXT,
|
|
|
|
normalized TEXT UNIQUE ON CONFLICT IGNORE,
|
|
|
|
added INTEGER,
|
|
|
|
subtracted INTEGER
|
|
|
|
)""")
|
|
|
|
db.commit()
|
|
|
|
def p(s1, s2):
|
|
|
|
return int(ircutils.nickEqual(s1, s2))
|
|
|
|
db.create_function('nickeq', 2, p)
|
|
|
|
return db
|
|
|
|
|
|
|
|
def karma(self, irc, msg, args):
|
2003-11-21 13:31:51 +01:00
|
|
|
"""[<channel>] [<thing> [<thing> ...]]
|
2003-11-04 09:42:11 +01:00
|
|
|
|
2003-11-21 13:31:51 +01:00
|
|
|
Returns the karma of <text>. If <thing> is not given, returns the top
|
|
|
|
three and bottom three karmas. If one <thing> is given, returns the
|
|
|
|
details of its karma; if more than one <thing> is given, returns the
|
|
|
|
total karma of each of the the things. <channel> is only necessary if
|
|
|
|
the message isn't sent on the channel itself.
|
2003-11-04 09:42:11 +01:00
|
|
|
"""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
if len(args) == 1:
|
|
|
|
name = args[0]
|
|
|
|
normalized = name.lower()
|
|
|
|
cursor.execute("""SELECT added, subtracted
|
|
|
|
FROM karma
|
|
|
|
WHERE normalized=%s""", normalized)
|
|
|
|
if cursor.rowcount == 0:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('%s has no karma.' % name)
|
2003-11-04 09:42:11 +01:00
|
|
|
else:
|
2003-11-15 05:37:04 +01:00
|
|
|
(added, subtracted) = imap(int, cursor.fetchone())
|
2003-11-04 09:42:11 +01:00
|
|
|
total = added - subtracted
|
2004-01-27 16:28:00 +01:00
|
|
|
if self.registryValue('simpleOutput', channel):
|
2003-11-26 18:07:20 +01:00
|
|
|
s = '%s: %s' % (name, total)
|
|
|
|
else:
|
2003-12-12 16:41:33 +01:00
|
|
|
s = 'Karma for %r has been increased %s ' \
|
|
|
|
'and decreased %s for a total karma of %s.' % \
|
|
|
|
(name, utils.nItems('time', added),
|
|
|
|
utils.nItems('time', subtracted), total)
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-11-04 09:42:11 +01:00
|
|
|
elif len(args) > 1:
|
2003-11-21 13:31:51 +01:00
|
|
|
normalizedArgs = sets.Set(imap(str.lower, args))
|
|
|
|
criteria = ' OR '.join(['normalized=%s'] * len(normalizedArgs))
|
2003-11-04 09:42:11 +01:00
|
|
|
sql = """SELECT name, added-subtracted
|
|
|
|
FROM karma WHERE %s
|
|
|
|
ORDER BY added-subtracted DESC""" % criteria
|
|
|
|
cursor.execute(sql, *normalizedArgs)
|
|
|
|
if cursor.rowcount > 0:
|
2003-11-21 13:31:51 +01:00
|
|
|
L = []
|
|
|
|
for (n, t) in cursor.fetchall():
|
|
|
|
L.append('%s: %s' % (n, t))
|
|
|
|
normalizedArgs.remove(n.lower())
|
|
|
|
if normalizedArgs:
|
|
|
|
if len(normalizedArgs) == 1:
|
2003-11-21 14:02:26 +01:00
|
|
|
ss = '%s has no karma' % normalizedArgs.pop()
|
2003-11-21 13:31:51 +01:00
|
|
|
else:
|
|
|
|
LL = list(normalizedArgs)
|
|
|
|
LL.sort()
|
2003-11-21 14:02:26 +01:00
|
|
|
ss = '%s have no karma' % utils.commaAndify(LL)
|
|
|
|
s = '%s. %s.' % (utils.commaAndify(L), ss)
|
|
|
|
else:
|
|
|
|
s = utils.commaAndify(L) + '.'
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-11-04 09:42:11 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply('I didn\'t know the karma for any '
|
2003-11-04 09:42:11 +01:00
|
|
|
'of those things.')
|
2003-12-29 04:12:47 +01:00
|
|
|
else: # No name was given. Return the top/bottom N karmas.
|
2004-01-27 16:28:00 +01:00
|
|
|
limit = self.registryValue('karmaRankingDisplay', channel)
|
2003-11-04 09:42:11 +01:00
|
|
|
cursor.execute("""SELECT name, added-subtracted
|
|
|
|
FROM karma
|
|
|
|
ORDER BY added-subtracted DESC
|
2003-12-16 21:51:37 +01:00
|
|
|
LIMIT %s""", limit)
|
2003-12-29 04:12:47 +01:00
|
|
|
highest=['%r (%s)' % (t[0], int(t[1])) for t in cursor.fetchall()]
|
2003-11-04 09:42:11 +01:00
|
|
|
cursor.execute("""SELECT name, added-subtracted
|
|
|
|
FROM karma
|
|
|
|
ORDER BY added-subtracted ASC
|
2003-12-16 21:51:37 +01:00
|
|
|
LIMIT %s""", limit)
|
2003-12-29 04:12:47 +01:00
|
|
|
lowest=['%r (%s)' % (t[0], int(t[1])) for t in cursor.fetchall()]
|
2003-11-04 10:09:53 +01:00
|
|
|
if not (highest and lowest):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('I have no karma for this channel.')
|
2003-11-04 10:09:53 +01:00
|
|
|
else:
|
|
|
|
s = 'Highest karma: %s. Lowest karma: %s.' % \
|
|
|
|
(utils.commaAndify(highest), utils.commaAndify(lowest))
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(s)
|
2003-12-02 00:47:37 +01:00
|
|
|
|
|
|
|
_mostAbbrev = utils.abbrev(['increased', 'decreased', 'active'])
|
|
|
|
def most(self, irc, msg, args):
|
|
|
|
"""[<channel>] {increased,decreased,active}
|
|
|
|
|
|
|
|
Returns the most increased, the most decreased, or the most active
|
|
|
|
(the sum of increased and decreased) karma things. <channel> is only
|
|
|
|
necessary if the message isn't sent in the channel itself.
|
|
|
|
"""
|
|
|
|
channel = privmsgs.getChannel(msg, args)
|
|
|
|
kind = privmsgs.getArgs(args)
|
|
|
|
try:
|
|
|
|
kind = self._mostAbbrev[kind]
|
|
|
|
if kind == 'increased':
|
|
|
|
orderby = 'added'
|
|
|
|
elif kind == 'decreased':
|
|
|
|
orderby = 'subtracted'
|
|
|
|
elif kind == 'active':
|
|
|
|
orderby = 'added+subtracted'
|
2003-12-19 04:13:17 +01:00
|
|
|
sql = "SELECT name, %s FROM karma ORDER BY %s DESC LIMIT %s" % \
|
|
|
|
(orderby, orderby,
|
2004-01-27 16:28:00 +01:00
|
|
|
self.registryValue('karmaMostDisplay', channel))
|
2003-12-02 00:47:37 +01:00
|
|
|
db = self.getDb(channel)
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute(sql)
|
2003-12-19 04:13:17 +01:00
|
|
|
L = ['%s: %s' % (name, int(i)) for (name, i) in cursor.fetchall()]
|
2003-12-02 00:47:37 +01:00
|
|
|
if L:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.reply(utils.commaAndify(L))
|
2003-12-02 00:47:37 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('I have no karma for this channel.')
|
2003-12-02 00:47:37 +01:00
|
|
|
except KeyError:
|
|
|
|
raise callbacks.ArgumentError
|
|
|
|
|
2003-11-04 09:42:11 +01:00
|
|
|
def increaseKarma(self, irc, msg, match):
|
2003-11-14 16:09:30 +01:00
|
|
|
r"^(\S+)\+\+(|\s+)$"
|
2003-11-04 09:42:11 +01:00
|
|
|
name = match.group(1)
|
|
|
|
normalized = name.lower()
|
|
|
|
db = self.getDb(msg.args[0])
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""INSERT INTO karma VALUES (NULL, %s, %s, 0, 0)""",
|
|
|
|
name, normalized)
|
|
|
|
cursor.execute("""UPDATE karma
|
|
|
|
SET added=added+1
|
|
|
|
WHERE normalized=%s""", normalized)
|
2004-01-27 17:48:41 +01:00
|
|
|
if self.registryValue('karmaResponse', msg.args[0]):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-11-04 09:42:11 +01:00
|
|
|
|
|
|
|
def decreaseKarma(self, irc, msg, match):
|
2003-11-14 16:09:30 +01:00
|
|
|
r"^(\S+)--(|\s+)$"
|
2003-11-04 09:42:11 +01:00
|
|
|
name = match.group(1)
|
|
|
|
normalized = name.lower()
|
|
|
|
db = self.getDb(msg.args[0])
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute("""INSERT INTO karma VALUES (NULL, %s, %s, 0, 0)""",
|
|
|
|
name, normalized)
|
|
|
|
cursor.execute("""UPDATE karma
|
|
|
|
SET subtracted=subtracted+1
|
|
|
|
WHERE normalized=%s""", normalized)
|
2004-01-27 16:28:00 +01:00
|
|
|
if self.registryValue('karmaResponse', msg.args[0]):
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.replySuccess()
|
2003-11-04 09:42:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
Class = Karma
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|