Convert the events module to v3.2

This commit is contained in:
Némunaire 2012-08-16 04:34:22 +02:00
parent 603baa8581
commit e7f9d329cc
3 changed files with 117 additions and 140 deletions

View File

@ -1,2 +1,17 @@
<?xml version="1.0" ?> <?xml version="1.0" ?>
<nemubotmodule name="events" /> <nemubotmodule name="events">
<message type="cmd" name="start" call="start_countdown" />
<message type="cmd" name="end" call="end_countdown" />
<message type="cmd" name="forceend" call="end_countdown" />
<message type="cmd" name="eventlist" call="liste" />
<message type="cmd" name="eventslist" call="liste" />
<message type="cmd" name="eventliste" call="liste" />
<message type="cmd" name="eventsliste" call="liste" />
<message type="cmd" name="we" call="cmd_we" />
<message type="cmd" name="week-end" call="cmd_we" />
<message type="cmd" name="weekend" call="cmd_we" />
<message type="cmd" name="new-year" call="cmd_newyear" />
<message type="cmd" name="newyear" call="cmd_newyear" />
<message type="cmd" name="ny" call="cmd_newyear" />
<message type="cmd" name="start" call="start_countdown" />
</nemubotmodule>

View File

@ -1,48 +0,0 @@
# coding=utf-8
from datetime import datetime
import threading
newStrendEvt = threading.Event()
class Manager(threading.Thread):
def __init__(self, datas, srvs):
self.stop = False
self.DATAS = datas
self.SRVS = srvs
threading.Thread.__init__(self)
def alertEnd(self, evt):
global newStrendEvt
#Send the message on each matched servers
for server in self.SRVS.keys():
if not evt.hasAttribute("server") or server == evt["server"]:
if evt["channel"] == self.SRVS[server].nick:
self.SRVS[server].send_msg_usr(evt["proprio"], "%s: %s arrivé à échéance." % (evt["proprio"], evt["name"]))
else:
self.SRVS[server].send_msg(evt["channel"], "%s: %s arrivé à échéance." % (evt["proprio"], evt["name"]))
self.DATAS.delChild(self.DATAS.index[evt["name"]])
save()
newStrendEvt.set()
def run(self):
global newStrendEvt
while not self.stop:
newStrendEvt.clear()
closer = None
#Gets the closer event
for evt in self.DATAS.index.keys():
if self.DATAS.index[evt].hasAttribute("end") and (closer is None or self.DATAS.index[evt].getDate("end") < closer.getDate("end")) and self.DATAS.index[evt].getDate("end") > datetime.now():
closer = self.DATAS.index[evt]
if closer is not None and closer.hasAttribute("end"):
#print ("Closer: %s à %s"%(closer.name, closer["end"]))
timeleft = (closer.getDate("end") - datetime.now()).seconds
timer = threading.Timer(timeleft, self.alertEnd, (closer,))
timer.start()
#print ("Start timer (%ds)"%timeleft)
newStrendEvt.wait()
if closer is not None and closer.hasAttribute("end") and closer.getDate("end") > datetime.now():
timer.cancel()
self.threadManager = None

View File

@ -8,11 +8,11 @@ from datetime import datetime
import time import time
import threading import threading
nemubotversion = 3.0 nemubotversion = 3.2
from module_state import ModuleState from event import ModuleEvent
from hooks import Hook
from . import Manager from xmlparser.node import ModuleState
def help_tiny (): def help_tiny ():
"""Line inserted in the response to the command !help""" """Line inserted in the response to the command !help"""
@ -21,109 +21,119 @@ def help_tiny ():
def help_full (): def help_full ():
return "This module store a lot of events: ny, we, vacs, " + (", ".join(DATAS.index.keys())) + "\n!eventslist: gets list of timer\n!start /something/: launch a timer" return "This module store a lot of events: ny, we, vacs, " + (", ".join(DATAS.index.keys())) + "\n!eventslist: gets list of timer\n!start /something/: launch a timer"
CONTEXT = None
threadManager = None def load(context):
global DATAS, CONTEXT
CONTEXT = context
#Define the index
DATAS.setIndex("name")
def load(): for evt in DATAS.index.keys():
global DATAS, SRVS, threadManager if DATAS.index[evt].hasAttribute("end"):
#Define the index event = ModuleEvent(call=fini, call_data=dict(strend=DATAS.index[evt]))
DATAS.setIndex("name") event.end = DATAS.index[evt].getDate("end")
#Load the manager context.add_event(event)
Manager.save = save context.hooks.add_hook(context.hooks.cmd_hook,
threadManager = Manager.Manager(DATAS, SRVS) Hook(other, DATAS.index[evt]["name"]))
threadManager.start()
def reload(): def fini(strend):
imp.reload(Manager) for server in CONTEXT.servers.keys():
if not strend.hasAttribute("server") or server == strend["server"]:
if strend["channel"] == CONTEXT.servers[server].nick:
CONTEXT.servers[server].send_msg_usr(strend["proprio"], "%s: %s arrivé à échéance." % (strend["proprio"], strend["name"]))
else:
CONTEXT.servers[server].send_msg(strend["channel"], "%s: %s arrivé à échéance." % (strend["proprio"], strend["name"]))
DATAS.delChild(DATAS.index[strend["name"]])
save()
def close(): def cmd_we(data, msg):
global threadManager
if threadManager is not None:
threadManager.stop = True
Manager.newStrendEvt.set()
def parseanswer(msg):
global DATAS
if msg.cmd[0] == "we" or msg.cmd[0] == "week-end" or msg.cmd[0] == "weekend":
ndate = datetime.today() + timedelta(5 - datetime.today().weekday()) ndate = datetime.today() + timedelta(5 - datetime.today().weekday())
ndate = datetime(ndate.year, ndate.month, ndate.day, 0, 0, 1) ndate = datetime(ndate.year, ndate.month, ndate.day, 0, 0, 1)
msg.send_chn ( msg.send_chn (
msg.countdown_format (ndate, msg.countdown_format (ndate,
"Il reste %s avant le week-end, courage ;)", "Il reste %s avant le week-end, courage ;)",
"Youhou, on est en week-end depuis %s.")) "Youhou, on est en week-end depuis %s."))
return True
elif msg.cmd[0] == "new-year" or msg.cmd[0] == "newyear" or msg.cmd[0] == "ny":
msg.send_chn (
msg.countdown_format (datetime(datetime.today().year + 1, 1, 1, 0, 0, 1),
"Il reste %s avant la nouvelle année.",
"Nous faisons déjà la fête depuis %s !"))
return True
elif msg.cmd[0] == "vacances" or msg.cmd[0] == "vacs" or msg.cmd[0] == "holiday" or msg.cmd[0] == "holidays":
msg.send_chn (
msg.countdown_format (datetime(2012, 7, 30, 18, 0, 1),
"Il reste %s avant les vacances :)",
"Profitons, c'est les vacances depuis %s."))
return True
elif msg.cmd[0] == "start" and len(msg.cmd) > 1:
if msg.cmd[1] not in DATAS:
strnd = ModuleState("strend")
strnd["server"] = msg.srv.id
strnd["channel"] = msg.channel
strnd["proprio"] = msg.nick
strnd["start"] = datetime.now()
strnd["name"] = msg.cmd[1]
DATAS.addChild(strnd)
if len(msg.cmd) > 2: def cmd_newyear(data, msg):
result = re.match("([0-9]+)([smhdjSMHDJ])?", msg.cmd[2]) msg.send_chn (
if result is not None: msg.countdown_format (datetime(datetime.today().year + 1, 1, 1, 0, 0, 1),
try: "Il reste %s avant la nouvelle année.",
if result.group(2) is not None and (result.group(2) == "m" or result.group(2) == "M"): "Nous faisons déjà la fête depuis %s !"))
strnd["end"] = datetime.now() + timedelta(minutes=int(result.group(1)))
elif result.group(2) is not None and (result.group(2) == "h" or result.group(2) == "H"): def cmd_vacances(data, msg):
strnd["end"] = datetime.now() + timedelta(hours=int(result.group(1))) msg.send_chn (
elif result.group(2) is not None and (result.group(2) == "d" or result.group(2) == "D" or result.group(2) == "j" or result.group(2) == "J"): msg.countdown_format (datetime(2012, 7, 30, 18, 0, 1),
strnd["end"] = datetime.now() + timedelta(days=int(result.group(1))) "Il reste %s avant les vacances :)",
"Profitons, c'est les vacances depuis %s."))
def start_countdown(data, msg):
if msg.cmd[1] not in DATAS:
strnd = ModuleState("strend")
strnd["server"] = msg.srv.id
strnd["channel"] = msg.channel
strnd["proprio"] = msg.nick
strnd["start"] = datetime.now()
strnd["name"] = msg.cmd[1]
DATAS.addChild(strnd)
evt = ModuleEvent(call=fini, call_data=dict(strend=strnd))
if len(msg.cmd) > 2:
result = re.match("([0-9]+)([smhdjSMHDJ])?", msg.cmd[2])
if result is not None:
try:
if result.group(2) is not None and (result.group(2) == "m" or result.group(2) == "M"):
strnd["end"] = datetime.now() + timedelta(minutes=int(result.group(1)))
elif result.group(2) is not None and (result.group(2) == "h" or result.group(2) == "H"):
strnd["end"] = datetime.now() + timedelta(hours=int(result.group(1)))
elif result.group(2) is not None and (result.group(2) == "d" or result.group(2) == "D" or result.group(2) == "j" or result.group(2) == "J"):
strnd["end"] = datetime.now() + timedelta(days=int(result.group(1)))
else:
strnd["end"] = datetime.now() + timedelta(seconds=int(result.group(1)))
evt.end = strnd.getDate("end")
CONTEXT.add_event(evt)
msg.send_snd ("%s commencé le %s et se terminera le %s."% (msg.cmd[1], datetime.now(), strnd.getDate("end")))
except:
msg.send_snd ("Impossible de définir la fin de %s."% (msg.cmd[1]))
msg.send_snd ("%s commencé le %s."% (msg.cmd[1], datetime.now()))
else: else:
strnd["end"] = datetime.now() + timedelta(seconds=int(result.group(1))) msg.send_snd ("%s commencé le %s"% (msg.cmd[1], datetime.now()))
Manager.newStrendEvt.set()
msg.send_snd ("%s commencé le %s et se terminera le %s."% (msg.cmd[1], datetime.now(), strnd.getDate("end"))) context.hooks.add_hook(context.hooks.cmd_hook,
except: Hook(other, DATAS.index[evt]["name"]))
msg.send_snd ("Impossible de définir la fin de %s."% (msg.cmd[1]))
msg.send_snd ("%s commencé le %s."% (msg.cmd[1], datetime.now()))
else:
msg.send_snd ("%s commencé le %s"% (msg.cmd[1], datetime.now()))
save()
else:
msg.send_snd ("%s existe déjà."% (msg.cmd[1]))
return True
elif (msg.cmd[0] == "end" or msg.cmd[0] == "forceend") and len(msg.cmd) > 1:
if msg.cmd[1] in DATAS.index:
msg.send_chn ("%s a duré %s." % (msg.cmd[1], msg.just_countdown(datetime.now () - DATAS.index[msg.cmd[1]].getDate("start"))))
if DATAS.index[msg.cmd[1]]["proprio"] == msg.nick or (msg.cmd[0] == "forceend" and msg.nick == msg.srv.owner):
DATAS.delChild(DATAS.index[msg.cmd[1]])
Manager.newStrendEvt.set()
save() save()
else:
msg.send_snd ("Vous ne pouvez pas terminer le compteur %s, créé par %s."% (msg.cmd[1], DATAS.index[msg.cmd[1]]["proprio"]))
else: else:
msg.send_snd ("%s n'est pas un compteur connu."% (msg.cmd[1])) msg.send_snd ("%s existe déjà."% (msg.cmd[1]))
return True return True
elif msg.cmd[0] == "eventslist" or msg.cmd[0] == "eventlist" or msg.cmd[0] == "eventsliste" or msg.cmd[0] == "eventliste":
def end_countdown(data, msg):
if msg.cmd[1] in DATAS.index:
msg.send_chn ("%s a duré %s." % (msg.cmd[1], msg.just_countdown(datetime.now () - DATAS.index[msg.cmd[1]].getDate("start"))))
if DATAS.index[msg.cmd[1]]["proprio"] == msg.nick or (msg.cmd[0] == "forceend" and msg.nick == msg.srv.owner):
DATAS.delChild(DATAS.index[msg.cmd[1]])
Manager.newStrendEvt.set()
save()
else:
msg.send_snd ("Vous ne pouvez pas terminer le compteur %s, créé par %s."% (msg.cmd[1], DATAS.index[msg.cmd[1]]["proprio"]))
else:
msg.send_snd ("%s n'est pas un compteur connu."% (msg.cmd[1]))
return True
def liste(data, msg):
msg.send_snd ("Compteurs connus : %s." % ", ".join(DATAS.index.keys())) msg.send_snd ("Compteurs connus : %s." % ", ".join(DATAS.index.keys()))
elif msg.cmd[0] in DATAS.index:
def other(data, msg):
if DATAS.index[msg.cmd[0]].name == "strend": if DATAS.index[msg.cmd[0]].name == "strend":
if DATAS.index[msg.cmd[0]].hasAttribute("end"): if DATAS.index[msg.cmd[0]].hasAttribute("end"):
msg.send_chn ("%s commencé il y a %s et se terminera dans %s." % (msg.cmd[0], msg.just_countdown(datetime.now() - DATAS.index[msg.cmd[0]].getDate("start")), msg.just_countdown(DATAS.index[msg.cmd[0]].getDate("end") - datetime.now()))) msg.send_chn ("%s commencé il y a %s et se terminera dans %s." % (msg.cmd[0], msg.just_countdown(datetime.now() - DATAS.index[msg.cmd[0]].getDate("start")), msg.just_countdown(DATAS.index[msg.cmd[0]].getDate("end") - datetime.now())))
else: else:
msg.send_chn ("%s commencé il y a %s." % (msg.cmd[0], msg.just_countdown(datetime.now () - DATAS.index[msg.cmd[0]].getDate("start")))) msg.send_chn ("%s commencé il y a %s." % (msg.cmd[0], msg.just_countdown(datetime.now () - DATAS.index[msg.cmd[0]].getDate("start"))))
else: else:
msg.send_chn (msg.countdown_format (DATAS.index[msg.cmd[0]].getDate("start"), DATAS.index[msg.cmd[0]]["msg_before"], DATAS.index[msg.cmd[0]]["msg_after"])) msg.send_chn (msg.countdown_format (DATAS.index[msg.cmd[0]].getDate("start"), DATAS.index[msg.cmd[0]]["msg_before"], DATAS.index[msg.cmd[0]]["msg_after"]))
save() save()
return True return True
else:
return False
def parseask(msg): def parseask(msg):