2010-10-23 22:52:15 -05:00
|
|
|
# Karma - handle karma (++ and --) tracking
|
|
|
|
# Copyright (C) 2010 Brian S. Stephan
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from Module import Module
|
|
|
|
|
2010-10-24 09:37:43 -05:00
|
|
|
__author__ = "Mike Bloy <mike@bloy.org>"
|
|
|
|
__date__ = "$Oct 23, 2010 11:12:33 AM$"
|
2010-10-23 22:52:15 -05:00
|
|
|
|
|
|
|
class Karma(Module):
|
|
|
|
|
|
|
|
def __init__(self, config, server, modlist):
|
|
|
|
"""
|
|
|
|
Upon creation, determine the save file location
|
|
|
|
"""
|
|
|
|
|
|
|
|
Module.__init__(self, config, server, modlist)
|
|
|
|
|
2010-10-24 14:57:23 -05:00
|
|
|
pattern = "([a-zA-Z0-9_]+)"
|
|
|
|
karmapattern = '^' + pattern + '(\+\+|--)'
|
|
|
|
querypattern = '^!rank\s+' + pattern
|
2010-10-23 22:52:15 -05:00
|
|
|
|
|
|
|
self.karmare = re.compile(karmapattern)
|
|
|
|
self.queryre = re.compile(querypattern)
|
|
|
|
|
2010-10-24 13:05:16 -05:00
|
|
|
# need to init the database if karma tables don't already exist
|
|
|
|
version = self.db_module_registered(self.__class__.__name__)
|
|
|
|
if (version == None):
|
|
|
|
# have to create the database tables
|
|
|
|
conn = self.get_db()
|
|
|
|
try:
|
|
|
|
conn.execute('''
|
|
|
|
CREATE TABLE karma_log (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
key TEXT NOT NULL,
|
|
|
|
delta INTEGER NOT NULL,
|
|
|
|
who TEXT NOT NULL,
|
|
|
|
userhost TEXT NOT NULL,
|
|
|
|
karmatime TEXT DEFAULT CURRENT_TIMESTAMP
|
|
|
|
)''')
|
|
|
|
conn.execute('CREATE INDEX karma_log_key_ix ON karma_log (key)')
|
|
|
|
conn.execute('CREATE INDEX karma_log_who_ix ON karma_log (who)')
|
|
|
|
conn.execute('''
|
|
|
|
CREATE VIEW karma_values AS
|
|
|
|
SELECT key, SUM(delta) AS value
|
|
|
|
FROM karma_log
|
|
|
|
GROUP BY key''')
|
|
|
|
|
|
|
|
sql = 'INSERT INTO drbotzo_modules VALUES (?,?)'
|
|
|
|
conn.execute(sql, (self.__class__.__name__, 1))
|
|
|
|
conn.commit()
|
|
|
|
finally: conn.close()
|
|
|
|
|
2010-10-23 22:52:15 -05:00
|
|
|
def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked):
|
2010-10-24 09:36:15 -05:00
|
|
|
"""look for karma strings at the start of messages"""
|
2010-10-23 22:52:15 -05:00
|
|
|
|
|
|
|
if (self.karmare.match(what)):
|
2010-10-24 09:36:15 -05:00
|
|
|
self.handle_karma_change(connection, nick, userhost, replypath, what)
|
|
|
|
|
|
|
|
elif (self.queryre.match(what)):
|
|
|
|
self.handle_karma_query(connection, nick, userhost, replypath, what)
|
2010-10-23 22:52:15 -05:00
|
|
|
|
2010-10-24 09:36:15 -05:00
|
|
|
def handle_karma_change(self, connection, nick, userhost, replypath, what):
|
|
|
|
"""
|
|
|
|
handle the karma change and storage.
|
|
|
|
"""
|
|
|
|
match = self.karmare.match(what)
|
|
|
|
key = match.group(1)
|
|
|
|
value = match.group(2)
|
|
|
|
if (value == '++'):
|
|
|
|
value = 1;
|
|
|
|
else:
|
|
|
|
value = -1;
|
|
|
|
|
2010-10-24 13:05:16 -05:00
|
|
|
conn = self.get_db()
|
2010-10-24 09:36:15 -05:00
|
|
|
try:
|
2010-10-24 13:05:16 -05:00
|
|
|
sql = '''
|
|
|
|
INSERT INTO karma_log (key, delta, who, userhost)
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
'''
|
|
|
|
conn.execute(sql, (key, value, nick, userhost))
|
|
|
|
conn.commit()
|
|
|
|
finally: conn.close()
|
2010-10-24 09:36:15 -05:00
|
|
|
|
|
|
|
reply = "karma change for '" + key + "' (" + str(value) + ") by " + nick
|
|
|
|
self.reply(connection, replypath, reply)
|
|
|
|
|
|
|
|
def handle_karma_query(self, connection, nick, userhost, replypath, what):
|
|
|
|
match = self.queryre.match(what)
|
|
|
|
key = match.group(1)
|
|
|
|
|
2010-10-24 13:05:16 -05:00
|
|
|
conn = self.get_db()
|
|
|
|
reply = '{nick}: {key} has no karma'.format(nick=nick, key=key)
|
2010-10-24 09:36:15 -05:00
|
|
|
try:
|
2010-10-24 13:05:16 -05:00
|
|
|
query = '''
|
|
|
|
SELECT value
|
|
|
|
FROM karma_values
|
|
|
|
WHERE key = :key
|
|
|
|
'''
|
|
|
|
value = conn.execute(query, {'key': key}).fetchone()
|
|
|
|
if (value != None):
|
|
|
|
reply = '{nick}: {key} has {value[0]!s} points of karma'.format(nick=nick, key=key, value=value)
|
|
|
|
|
|
|
|
finally: conn.close()
|
2010-10-24 09:36:15 -05:00
|
|
|
|
|
|
|
self.reply(connection, replypath, reply)
|
2010-10-23 22:52:15 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print "Hello World"
|