"""Keeps track of karma counts.
@package ppbot

@syntax .karma <item>
"""
import re

from modules import *

class Karmamod(Module):

    def __init__(self, *args, **kwargs):
        """Constructor"""

        Module.__init__(self, kwargs=kwargs)

    def _register_events(self):
        self.add_command('karma', 'get_karma')
        self.add_event('pubmsg', 'parsekarma')

    @op
    def get_karma(self, event):
        karma = self.db.karma.find_one({'name': event['args'][0].lower(),
            'source': event['target']})
        try:
            result = karma['count']
        except KeyError, TypeError:
            result = 0

        self.msg(event['target'], '%s has %d karma.' % (event['args'][0], result))

    def parsekarma(self, event):
        inc_pattern = re.compile('([^ ]{2,})\+\+')
        m = inc_pattern.findall(event['message'])
        for term in m:
            self.change(event, term, 1)
        dec_pattern = re.compile('([^ ]{2,})--')
        m = dec_pattern.findall(event['message'])
        for term in m:
            self.change(event, term, -1)

    def change(self, event, name, value):
        """Change karma count."""

        self.db.karma.update({
            'name': name.lower(),
            'source': event['target']
        }, {
            '$inc': {
                'count': 1
            }
        }, True)

@@ -582,142 +582,130 @@
     
-except KeyError, TypeError:%0A            result = 0%0A%0A        self.msg(event%5B'target'%5D, '%25s has %25d karma.' %25 (event%5B'args'%5D%5B0%5D, result))
+    self.reply('%25s has %25d karma.' %25 (event%5B'args'%5D%5B0%5D, result))%0A        except KeyError, TypeError:%0A            result = 0
 %0A%0A  

Change to reply only if target has karma