Remove ban support from message, separate credits into another file

This commit is contained in:
Némunaire 2012-06-16 22:48:15 +02:00
parent 04788fbc9b
commit 5a81fa9822
2 changed files with 60 additions and 80 deletions

43
credits.py Normal file
View File

@ -0,0 +1,43 @@
# coding=utf-8
from datetime import datetime
from datetime import timedelta
import random
BANLIST = []
class Credits:
def __init__ (self, name):
self.name = name
self.credits = 5
self.randsec = timedelta(seconds=random.randint(0, 55))
self.lastmessage = datetime.now() + self.randsec
self.iask = True
def ask(self):
if self.name in BANLIST:
return False
now = datetime.now() + self.randsec
if self.lastmessage.minute == now.minute and (self.lastmessage.second == now.second or self.lastmessage.second == now.second - 1):
print("\033[1;36mAUTOBAN\033[0m %s: too low time between messages" % self.name)
#BANLIST.append(self.name)
self.credits -= self.credits / 2 #Une alternative
return False
self.iask = True
return self.credits > 0 or self.lastmessage.minute != now.minute
def speak(self):
if self.iask:
self.iask = False
now = datetime.now() + self.randsec
if self.lastmessage.minute != now.minute:
self.credits = min (15, self.credits + 5)
self.lastmessage = now
self.credits -= 1
return self.credits > -3
def to_string(self):
print ("%s: %d ; reset: %d" % (self.name, self.credits, self.randsec.seconds))

View File

@ -7,81 +7,22 @@ import sys
import string import string
import time import time
import imp import imp
import random
from xml.dom.minidom import parse
from xml.dom.minidom import parseString
from xml.dom.minidom import getDOMImplementation
BANLIST = [] from credits import Credits
import credits
CREDITS = {} CREDITS = {}
filename = "" filename = ""
def load(datas_path): def load(config_file):
global BANLIST, CREDITS, filename global CREDITS, filename
CREDITS = dict () CREDITS = dict ()
filename = config_file
credits.BANLIST = xmlparser.parse_file(filename)
BANLIST = list () def save():
filename = datas_path + "/general.xml" global filename
credits.BANLIST.save(filename)
sys.stdout.write ("Loading banlist ... ")
dom = parse(filename)
for item in dom.documentElement.getElementsByTagName("ban"):
BANLIST.append(item.getAttribute("name"))
print ("done (%d users banned)" % (len(BANLIST)))
def save_module():
global BANLIST, ALIAS, filename
sys.stdout.write ("Saving banlist ... ")
impl = getDOMImplementation()
newdoc = impl.createDocument(None, 'global', None)
top = newdoc.documentElement
for name in BANLIST:
item = parseString ('<ban name="%s" />' % (name)).documentElement
top.appendChild(item);
with open(filename, "w") as f:
newdoc.writexml (f)
print ("done")
class Credits:
def __init__ (self, name):
self.name = name
self.credits = 5
self.randsec = timedelta(seconds=random.randint(0, 55))
self.lastmessage = datetime.now() + self.randsec
self.iask = True
def ask(self):
if self.name in BANLIST:
return False
now = datetime.now() + self.randsec
if self.lastmessage.minute == now.minute and (self.lastmessage.second == now.second or self.lastmessage.second == now.second - 1):
print("AUTOBAN %s: too low time between messages" % self.name)
#BANLIST.append(self.name)
self.credits -= self.credits / 2 #Une alternative
return False
self.iask = True
return self.credits > 0 or self.lastmessage.minute != now.minute
def speak(self):
if self.iask:
self.iask = False
now = datetime.now() + self.randsec
if self.lastmessage.minute != now.minute:
self.credits = min (15, self.credits + 5)
self.lastmessage = now
self.credits -= 1
return self.credits > -3
def to_string(self):
print ("%s: %d ; reset: %d" % (self.name, self.credits, self.randsec.seconds))
class Message: class Message:
@ -193,6 +134,7 @@ class Message:
def parsemsg (self, mods): def parsemsg (self, mods):
#Treat all messages starting with 'nemubot:' as distinct commands #Treat all messages starting with 'nemubot:' as distinct commands
if self.content.find("%s:"%self.srv.nick) == 0: if self.content.find("%s:"%self.srv.nick) == 0:
self.content = self.content[len(self.srv.nick)+1:].strip()
messagel = self.content.lower() messagel = self.content.lower()
#Is it a simple response? #Is it a simple response?
@ -224,13 +166,8 @@ class Message:
#Try modules #Try modules
else: else:
for im in mods: for im in mods:
#try: if im.has_access(self) im.parseask(self):
if im.parseask(self): return
return
#except AttributeError:
#print ("Warning: in module `%s', no function parseask defined." % im.name)
#im.parseask = lambda x: False
#continue
#Owner commands #Owner commands
elif self.content[0] == '`' and self.sender == self.srv.owner: elif self.content[0] == '`' and self.sender == self.srv.owner:
@ -251,14 +188,14 @@ class Message:
elif self.cmd[0] == "ban": elif self.cmd[0] == "ban":
if len(self.cmd) > 1: if len(self.cmd) > 1:
BANLIST.append(self.cmd[1]) credits.BANLIST.append(self.cmd[1])
else: else:
print (BANLIST) print (credits.BANLIST)
elif self.cmd[0] == "banlist": elif self.cmd[0] == "banlist":
print (BANLIST) print (credits.BANLIST)
elif self.cmd[0] == "unban": elif self.cmd[0] == "unban":
if len(self.cmd) > 1: if len(self.cmd) > 1:
BANLIST.remove(self.cmd[1]) credits.BANLIST.remove(self.cmd[1])
elif self.cmd[0] == "credits": elif self.cmd[0] == "credits":
if len(self.cmd) > 1 and self.cmd[1] in CREDITS: if len(self.cmd) > 1 and self.cmd[1] in CREDITS: