Example #1
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()
    names = Dol()
    threadlist = threading.enumerate()
    for thread in threadlist:
        name = thread.getName()
        try: type = thread.type
        except AttributeError: type = name
        stats.upitem(type)
        try: names.add(type, thread.nowrunning)
        except AttributeError: pass
    if ievent.rest:
        result = []
        for item, n in names.items():
            if not n: continue
            res = "%s: " % item
            for name in n: res += "%s, " % name
            result.append(res[:-2])
        result.sort()
        ievent.reply("nowrunning: ", result, dot="<br>")
    result = []
    for item in stats.top(): result.append("%s = %s" % (item[0], item[1]))
    result.sort()
    ievent.reply(", ".join(result))
Example #2
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
Example #3
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()
Example #4
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()  
Example #5
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)
Example #6
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)
Example #7
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
Example #8
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)
        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))
Example #9
0
 def __init__(self):
     self.stats = StatDict()
     self.times = LazyDict()
     self.wait = LazyDict()
     self.warned = LazyDict()
Example #10
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 u in self.times:
            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