From a64c83ce59a787c2c077fdae8ba97571edf196df Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sat, 23 Oct 2010 22:52:15 -0500 Subject: [PATCH] working karma module --- .gitignore | 1 + dr.botzo.cfg.example | 4 ++ modules/Karma.py | 113 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100755 modules/Karma.py diff --git a/.gitignore b/.gitignore index f2ea467..6754abf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +karma* *.facts *.pyc *.swp diff --git a/dr.botzo.cfg.example b/dr.botzo.cfg.example index 5731f97..7bf599d 100644 --- a/dr.botzo.cfg.example +++ b/dr.botzo.cfg.example @@ -10,3 +10,7 @@ module_list = IrcAdmin [IrcAdmin] autojoin = #bss + +[Karma] +meta.pubmsg_needs_bot_prefix = false +karmafile = karma \ No newline at end of file diff --git a/modules/Karma.py b/modules/Karma.py new file mode 100755 index 0000000..d0e4e5f --- /dev/null +++ b/modules/Karma.py @@ -0,0 +1,113 @@ +# 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 . + +from ConfigParser import NoOptionError + +import re +import shelve + +from Module import Module + +__author__="Mike Bloy " +__date__ ="$Oct 23, 2010 11:12:33 AM$" + +class Karma(Module): + + def __init__(self, config, server, modlist): + """ + Upon creation, determine the save file location + """ + + Module.__init__(self, config, server, modlist) + + filename = self.config.get(self.__class__.__name__, 'karmafile') + self.karmafile=filename + "_karma.dat" + self.trendfile=filename + "_trends.dat" + + pattern = "^([a-zA-Z0-9_]+)" + + karmapattern = pattern + '(\+\+|--)' + querypattern = pattern + '\?' + + self.karmare = re.compile(karmapattern) + self.queryre = re.compile(querypattern) + + def do(self, connection, event, nick, userhost, replypath, what, admin_unlocked): + """look for karma strings.""" + + if (self.karmare.match(what)): + + match = self.karmare.match(what) + key = match.group(1) + value = match.group(2) + if (value == '++'): + value = 1; + else: + value = -1; + + # do karma recording + karma = shelve.open(self.karmafile) + try: + oldvalue = 0; + if karma.has_key(key): + oldvalue = karma[key] + newvalue = oldvalue + value; + karma[key] = newvalue; + finally: + karma.close() + + trend = shelve.open(self.trendfile) + try: + nickpos = nick + "_pos" + nickneg = nick + "_neg" + trend_pos = 0; + trend_neg = 0; + if trend.has_key(nickpos): + trend_pos = trend[nickpos] + if trend.has_key(nickneg): + trend_neg = trend[nickneg] + if value > 0: + trend_pos = trend_pos + 1 + else: + trend_neg = trend_neg + 1 + trend[nickpos] = trend_pos; + trend[nickneg] = trend_neg; + finally: + trend.close(); + + reply = "karma change for '" + key + "' (" + str(value) + ") by " + nick + self.reply(connection, replypath, reply) + return + + if (self.queryre.match(what)): + # tell about karma + match = self.queryre.match(what) + key = match.group(1) + + karma = shelve.open(self.karmafile, "r") + reply = key + ' has no karma' + try: + if karma.has_key(key): + value = karma[key] + reply = key + ' has ' + str(value) + ' points of karma' + finally: + karma.close() + + self.reply(connection, replypath, reply) + return + +if __name__ == "__main__": + print "Hello World"