Beispiel #1
0
def makestats(objs, target, skip=[]):
    stats = StatDict()
    if not objs: return stats
    ignore = []
    res = []
    count = {}
    for t in target:
        for u in objs:
            if u in ignore: continue
            if u and u.data and u.data.txt:
                cont = False
                for s in skip:
                    if s in u.data.url: cont = True ; break
                if cont: continue
                if t in u.data.txt:
                    res.append(u)
                else:
                    ignore.append(u)
                    try: res.remove(u)
                    except ValueError: pass 
    for item in res:
        c = 0
        if not item in ignore:
            for t in target: c += item.data.txt.count(t)
            stats.upitem(item.data.url, c)
    return stats
Beispiel #2
0
def handle_karmawhodown(bot, event):
    k = event.rest.lower()
    item = KarmaItem(event.channel.lower() + "-" + k)
    sd = StatDict(item.data.whodown)
    res = []
    for i in sd.down():
        res.append("%s: %s" % i)
    if res: event.reply("downers of %s are: " % k, res)
    else: event.reply("nobody downed %s yet" % k)
Beispiel #3
0
def handle_karmawhoup(bot, event):
    """ arguments: <item> - show who increased the karma of an item. """
    k = event.rest.lower()
    item = KarmaItem(event.channel.lower() + "-" + k)
    sd = StatDict(item.data.whoup)
    res = []
    for i in sd.top():
        res.append("%s: %s" % i)
    if res: event.reply("uppers of %s are: " % k, res)
    else: event.reply("nobody upped %s yet" % k)
Beispiel #4
0
 def whatup(self, nick):
     """ show what items are upped by nick """
     global db
     if not db: logging.error("plugin isnt initialised yet") ; return []
     nick = nick.lower()
     statdict = StatDict()
     result = db.execute(""" SELECT item FROM whokarma WHERE nick = %s AND updown = 'up' """, nick)
     if not result: return []
     for i in result: statdict.upitem(i[0])
     return statdict.top()
Beispiel #5
0
 def scan(self, name):
     """ scan a rss url for tokens. """
     keys = []
     items = self.fetchdata(name)
     for item in items:
         for key in item:
             if key in allowedtokens: keys.append(key)            
     statdict = StatDict()
     for key in keys: statdict.upitem(key)
     return statdict.top()  
Beispiel #6
0
 def quotebad(self, limit=10):
     """ show lowest 10 of negative karma items """
     global db
     if not db: logging.error("plugin isnt initialised yet") ; return []
     statdict = StatDict()
     result = db.execute(""" SELECT item, value FROM karma """)
     if not result: return []
     for i in result:
         if not i[0].startswith('quote '): continue
         statdict.upitem(i[0], value=i[1])
     return statdict.down(limit=limit)
Beispiel #7
0
def handle_whokarmadown(bot, ievent):
    """ karma-whodown <item> .. show who decreased a karma item """
    if not ievent.rest: ievent.missing('<item>') ; return
    item = ievent.rest
    result = karma.getwhodown(item)
    statdict = StatDict()
    if result:
        for i in result: statdict.upitem(i)
        res = []
        for i in statdict.top(): res.append("%s=%s" % i)
        ievent.reply("whokarmadown of %s: " % item, res)
    else: ievent.reply('no whokarmadown data available for %s' % item)
Beispiel #8
0
 def input(self, html):
     self.scantime = time.time()
     words = striphtml(html)
     words = words.replace("\n", "").split()
     stats = StatDict()
     for w in words:
         stats.upitem(w)
     self.data.url = self.url.url
     self.data.words = stats
     self.save()
     logging.warn("%s words found for %s" % (len(stats), self.url.url))
     return stats
Beispiel #9
0
class FloodControl(object):

    def __init__(self):
        self.stats = StatDict()
        self.times = LazyDict()
        self.wait = LazyDict()
        self.warned = LazyDict()

    def reset(self, userhost):
        try: del self.times[userhost]
        except KeyError: pass
        try: del self.stats[userhost]
        except KeyError: pass
        try: del self.wait[userhost]
        except KeyError: pass
        try: del self.warned[userhost]
        except KeyError: pass

    def check(self, userhost, timetomonitor=60, threshold=10, wait=120, floodrate=1):
        u = userhost
        t = time.time()
        w = wait
        if self.times.has_key(u):
            if t - self.times[u] > w: self.reset(u) ; return False
            if (t - self.times[u] < timetomonitor): self.stats.upitem(u)
            if (self.stats.get(u) >  threshold) or (t - self.times[u] < floodrate): self.wait[userhost] = wait ; return True
        else: self.times[u] = t ; return False
        if self.stats.get(u) <= threshold: return False
        return True

    def checkevent(self, event, dobind=True):
        if not event.iscommand: return False
        if getmainconfig().floodallow: return False
        if dobind: event.bind()
        if not event.user: got = False
        else: got = True
        t = got and event.user.data.floodtime or 60
        if t < 60: t = 60
        threshold = got and event.user.data.floodthreshold or 20
        if threshold < 20: threshold = 20
        wait = got and event.user.data.floodwait or 120
        if wait < 120: wait = 120
        floodrate = got and event.user.data.floodrate or 0.1
        if floodrate < 0.1: floodrate = 0.1
        if not self.check(event.userhost, t, threshold, wait, floodrate): return False 
        if event.user and "OPER" in event.user.data.perms: return False
        logging.warn("floodcontrol block on %s" % event.userhost)
        if event.userhost not in self.warned:
            logging.warn("floodcontrol block on %s" % event.userhost)
            event.reply("floodcontrol enabled (%s seconds)" % wait)
        self.warned[event.userhost] = time.time()
        return True
Beispiel #10
0
def handle_karmagood(bot, event):
    """ arguments: none - show top karma items of a channel. """
    collection = PlugPersistCollection()
    stats = StatDict()
    objs = collection.objects(event.channel.lower())
    for name, obj in objs.iteritems():
       if not obj.data: logging.warn("%s is empty" % name) ; continue
       item = stripname(name).split("-")[-1]
       stats.upitem(item, obj.data.count)
    res = []
    for item in stats.top():
        res.append("%s - %s" % item)
    event.reply("top karma items of %s: " % event.channel, res)
Beispiel #11
0
def handle_karmawhoup(bot, event):
    """ arguments: <item> - show who increased the karma of an item. """
    k = event.rest.lower()
    item = KarmaItem(event.channel.lower() + "-" + k)
    sd = StatDict(item.data.whoup)
    #    res = []
    #    for i in sorted(sd.top(),key=itemgetter(1), reverse=True):
    #        res.append("%s: %s" % i)
    res = ""
    for item in sorted(sd.top(), key=itemgetter(1), reverse=True):
        res += (str(item[0]) + ':' + str(item[1]) + ' ')
    if res: event.reply("uppers of " + k + " are: " + res)
    else: event.reply("nobody upped " + k + " yet")
Beispiel #12
0
 def quotebad(self, limit=10):
     """ show lowest 10 of negative karma items """
     global db
     if not db:
         logging.error("plugin isnt initialised yet")
         return []
     statdict = StatDict()
     result = db.execute(""" SELECT item, value FROM karma """)
     if not result: return []
     for i in result:
         if not i[0].startswith('quote '): continue
         statdict.upitem(i[0], value=i[1])
     return statdict.down(limit=limit)
Beispiel #13
0
def handle_threads(bot, ievent):
    """ no arguments - show running threads. """
    try: import threading
    except ImportError:
         ievent.reply("threading is not enabled.")
         return
    stats = StatDict()
    threadlist = threading.enumerate()
    for thread in threadlist: stats.upitem(thread.getName())
    result = []
    for item in stats.top(): result.append("%s = %s" % (item[0], item[1]))
    result.sort()
    ievent.reply("threads running: ", result)
Beispiel #14
0
def handle_karmawhoup(bot, event):
    """ arguments: <item> - show who increased the karma of an item. """
    k = event.rest.lower()
    item = KarmaItem(event.channel.lower() + "-" + k)
    sd = StatDict(item.data.whoup)
#    res = []
#    for i in sorted(sd.top(),key=itemgetter(1), reverse=True):
#        res.append("%s: %s" % i)
    res = ""
    for item in sorted(sd.top(),key=itemgetter(1), reverse=True):
        res += (str(item[0])+':'+str(item[1])+' ')
    if res: event.reply("uppers of "+k+" are: "+ res)
    else: event.reply("nobody upped "+k+" yet")
Beispiel #15
0
 def whatup(self, nick):
     """ show what items are upped by nick """
     global db
     if not db:
         logging.error("plugin isnt initialised yet")
         return []
     nick = nick.lower()
     statdict = StatDict()
     result = db.execute(
         """ SELECT item FROM whokarma WHERE nick = %s AND updown = 'up' """,
         nick)
     if not result: return []
     for i in result:
         statdict.upitem(i[0])
     return statdict.top()
Beispiel #16
0
def handle_threads(bot, ievent):
    """ no arguments - show running threads. """
    try:
        import threading
    except ImportError:
        ievent.reply("threading is not enabled.")
        return
    stats = StatDict()
    threadlist = threading.enumerate()
    for thread in threadlist:
        stats.upitem(thread.getName())
    result = []
    for item in stats.top():
        result.append("%s = %s" % (item[0], item[1]))
    result.sort()
    ievent.reply("threads running: ", result)
Beispiel #17
0
def handle_karmagood(bot, event):
    """ arguments: none - show top karma items of a channel. """
    collection = PlugPersistCollection()
#    event.reply('Processing karma list.')
    stats = StatDict()
    objs = collection.objects()
    for name, obj in objs.iteritems():
       if not obj.data: logging.warn("%s is empty" % name) ; continue
       item = stripname(name).split("-",2)[-1]
       if stripname(name).split("-",2)[1] == event.channel.lower()[1:]:
           stats.upitem(item, obj.data.count)
    ktop = stats.top(limit=20)
#    event.reply(str(ktop))
    res = ""
    for item in ktop:
        res += (str(item[0])+':'+str(item[1])+' ')
    event.reply('Top 20 Karma for '+event.channel+': '+ res)
Beispiel #18
0
def handle_whokarmadown(bot, ievent):
    """ karma-whodown <item> .. show who decreased a karma item """
    if not ievent.rest:
        ievent.missing('<item>')
        return
    item = ievent.rest
    result = karma.getwhodown(item)
    statdict = StatDict()
    if result:
        for i in result:
            statdict.upitem(i)
        res = []
        for i in statdict.top():
            res.append("%s=%s" % i)
        ievent.reply("whokarmadown of %s: " % item, res)
    else:
        ievent.reply('no whokarmadown data available for %s' % item)
Beispiel #19
0
def handle_karmagood(bot, event):
    """ arguments: none - show top karma items of a channel. """
    collection = PlugPersistCollection()
    #    event.reply('Processing karma list.')
    stats = StatDict()
    objs = collection.objects()
    for name, obj in objs.iteritems():
        if not obj.data:
            logging.warn("%s is empty" % name)
            continue
        item = stripname(name).split("-", 2)[-1]
        if stripname(name).split("-", 2)[1] == event.channel.lower()[1:]:
            stats.upitem(item, obj.data.count)
    ktop = stats.top(limit=20)
    #    event.reply(str(ktop))
    res = ""
    for item in ktop:
        res += (str(item[0]) + ':' + str(item[1]) + ' ')
    event.reply('Top 20 Karma for ' + event.channel + ': ' + res)
Beispiel #20
0
def handle_stats(bot, event):
    totalstats = StatDict()
    counter = 0
    for modname in sys.modules:
        if modname.startswith("jsb"):
            plugname = modname.split(".")[-1]
            if event.args and plugname not in event.args: continue
            try:
                modstats = getattr(sys.modules[modname], "stats")
            except AttributeError:
                continue
            totalstats += modstats
            counter += 1
    event.reply("stats results from %s modules: " % counter, totalstats)
Beispiel #21
0
from jsb.lib.errors import URLNotEnabled
from jsb.utils.statdict import StatDict
from jsb.utils.locking import lockdec

## basic imports

import Queue
import time
import thread
import random
import logging
import sys

## defines

stats = StatDict()

## locks

startlock = thread.allocate_lock()
startlocked = lockdec(startlock)

## Runner class


class Runner(RunnerLoop):
    """
        a runner is a thread with a queue on which jobs can be pushed. 
        jobs scheduled should not take too long since only one job can 
        be executed in a Runner at the same time.
Beispiel #22
0
 def __init__(self):
     self.stats = StatDict()
     self.times = LazyDict()
     self.wait = LazyDict()
     self.warned = LazyDict()
Beispiel #23
0
class FloodControl(object):
    def __init__(self):
        self.stats = StatDict()
        self.times = LazyDict()
        self.wait = LazyDict()
        self.warned = LazyDict()

    def reset(self, userhost):
        try:
            del self.times[userhost]
        except KeyError:
            pass
        try:
            del self.stats[userhost]
        except KeyError:
            pass
        try:
            del self.wait[userhost]
        except KeyError:
            pass
        try:
            del self.warned[userhost]
        except KeyError:
            pass

    def check(self,
              userhost,
              timetomonitor=60,
              threshold=10,
              wait=120,
              floodrate=1):
        u = userhost
        t = time.time()
        w = wait
        if self.times.has_key(u):
            if t - self.times[u] > w:
                self.reset(u)
                return False
            if (t - self.times[u] < timetomonitor): self.stats.upitem(u)
            if (self.stats.get(u) > threshold) or (t - self.times[u] <
                                                   floodrate):
                self.wait[userhost] = wait
                return True
        else:
            self.times[u] = t
            return False
        if self.stats.get(u) <= threshold: return False
        return True

    def checkevent(self, event, dobind=True):
        if not event.iscommand: return False
        if getmainconfig().floodallow: return False
        if dobind: event.bind()
        if not event.user: got = False
        else: got = True
        t = got and event.user.data.floodtime or 60
        if t < 60: t = 60
        threshold = got and event.user.data.floodthreshold or 20
        if threshold < 20: threshold = 20
        wait = got and event.user.data.floodwait or 120
        if wait < 120: wait = 120
        floodrate = got and event.user.data.floodrate or 0.1
        if floodrate < 0.1: floodrate = 0.1
        if not self.check(event.userhost, t, threshold, wait, floodrate):
            return False
        if event.user and "OPER" in event.user.data.perms: return False
        logging.warn("floodcontrol block on %s" % event.userhost)
        if event.userhost not in self.warned:
            logging.warn("floodcontrol block on %s" % event.userhost)
            event.reply("floodcontrol enabled (%s seconds)" % wait)
        self.warned[event.userhost] = time.time()
        return True
Beispiel #24
0
 def __init__(self):
     self.stats = StatDict()
     self.times = LazyDict()
     self.wait = LazyDict()
     self.warned = LazyDict()
Beispiel #25
0
 def stats(self):
     stats = StatDict(self.data.words)
     return stats
Beispiel #26
0
def handle_chatlogstats(bot, event):
    """ no arguments - create log stats of the channel, possible options: --chan <channel> """
    what = event.rest.strip()
    chatlogdir = getdatadir() + os.sep + "chatlogs"
    if event.options and event.options.channel: chan = event.options.channel
    else: chan = event.channel
    logs = os.listdir(chatlogdir)
    if not logs: event.reply("no logs available for %s" % chan) ; return
    now = time.time()
    if what: timetarget = strtotime2(what) ; what = striptime(what)
    else: timetarget = 0 ; what = None
    event.reply("creating stats for channel %s (%s)" % (chan, time.ctime(timetarget)))
    userstats = StatDict()
    wordstats = StatDict()
    stop = False
    for f in logs[::-1]:
        filename = stripname(f)
        channel = stripname(chan[1:])
        if not channel in filename: continue
        for line in open(chatlogdir + os.sep + filename, 'r'):
            splitted = line.strip().split()
            if len(splitted) < 2: continue
            who = "unknown"
            for i in splitted:
               if i.startswith("<"): who = i[1:-1]
            if what and who != what: continue
            timestr = "%s %s" % (splitted[0], splitted[1])
            logtime = strtotime2(timestr)
            if logtime:
                if logtime > timetarget: userstats.upitem(who)
                else: continue
            else: userstats.upitem(who)
            for word in splitted[4:]: wordstats.upitem(word)
    if what: result = wordstats.top()
    else: result = userstats.top()
    if result:
        res = ["%s: %s" % item for item in result]
        event.reply("stat results for %s: " % (what or chan), res)
    else: event.reply("no result found for %s" % (what or chan))