entirely reworked the timer system to work with the event system, will eventually persist all timers to database until complete in a central way.
This commit is contained in:
parent
ce4f0f68b0
commit
9eff1b8931
4 changed files with 29 additions and 24 deletions
16
IRCBot.py
16
IRCBot.py
|
@ -13,6 +13,7 @@ class Bot(object):
|
|||
self.modules = ModuleManager.ModuleManager(self)
|
||||
self.events = EventManager.EventHook(self)
|
||||
self.timers = []
|
||||
self.events.on("timer").on("reconnect").hook(self.reconnect)
|
||||
|
||||
def add_server(self, id, hostname, port, password, ipv4, tls,
|
||||
nickname, username, realname, connect=False):
|
||||
|
@ -35,8 +36,8 @@ class Bot(object):
|
|||
for server in self.servers.values():
|
||||
self.connect(server)
|
||||
|
||||
def add_timer(self, function, delay, *args, **kwargs):
|
||||
timer = Timer.Timer(function, delay, *args, **kwargs)
|
||||
def add_timer(self, event_name, delay, **kwargs):
|
||||
timer = Timer.Timer(self, event_name, delay, **kwargs)
|
||||
timer.set_started_time()
|
||||
self.timers.append(timer)
|
||||
def next_timer(self):
|
||||
|
@ -49,6 +50,7 @@ class Bot(object):
|
|||
def call_timers(self):
|
||||
for timer in self.timers[:]:
|
||||
if timer.due():
|
||||
print(timer.event_name)
|
||||
timer.call()
|
||||
if timer.done():
|
||||
self.timers.remove(timer)
|
||||
|
@ -69,14 +71,16 @@ class Bot(object):
|
|||
self.poll.unregister(server.fileno())
|
||||
del self.servers[server.fileno()]
|
||||
|
||||
def reconnect(self, timer, server):
|
||||
def reconnect(self, event):
|
||||
server = event["server"]
|
||||
IRCServer.Server.__init__(server, server.id, server.target_hostname,
|
||||
server.port, server.password, server.ipv4, server.tls,
|
||||
server.nickname, server.username, server.realname, self)
|
||||
server.original_nickname, server.original_username,
|
||||
server.original_realname, self)
|
||||
if self.connect(server):
|
||||
self.servers[server.fileno()] = server
|
||||
else:
|
||||
timer.redo()
|
||||
event["timer"].redo()
|
||||
|
||||
def set_setting(self, setting, value):
|
||||
self.database.set_bot_setting(setting, value)
|
||||
|
@ -120,7 +124,7 @@ class Bot(object):
|
|||
self.disconnect(server)
|
||||
|
||||
reconnect_delay = self.config.get("reconnect-delay", 10)
|
||||
self.add_timer(self.reconnect, reconnect_delay, server)
|
||||
self.add_timer("reconnect", reconnect_delay, server=server)
|
||||
|
||||
print("disconnected from %s, reconnecting in %d seconds" % (
|
||||
str(server), reconnect_delay))
|
||||
|
|
10
IRCServer.py
10
IRCServer.py
|
@ -12,9 +12,9 @@ class Server(object):
|
|||
self.tls = tls
|
||||
self.password = password
|
||||
self.ipv4 = ipv4
|
||||
self.nickname = nickname
|
||||
self.username = username or nickname
|
||||
self.realname = realname or nickname
|
||||
self.original_nickname = nickname
|
||||
self.original_username = username or nickname
|
||||
self.original_realname = realname or nickname
|
||||
self.write_buffer = b""
|
||||
self.read_buffer = b""
|
||||
self.users = {}
|
||||
|
@ -53,8 +53,8 @@ class Server(object):
|
|||
self.socket.connect((self.target_hostname, self.port))
|
||||
if self.password:
|
||||
self.send_pass(self.password)
|
||||
self.send_user(self.username, self.realname)
|
||||
self.send_nick(self.nickname)
|
||||
self.send_user(self.original_username, self.original_realname)
|
||||
self.send_nick(self.original_nickname)
|
||||
self.connected = True
|
||||
def disconnect(self):
|
||||
self.connected = False
|
||||
|
|
9
Timer.py
9
Timer.py
|
@ -1,11 +1,11 @@
|
|||
import time
|
||||
|
||||
class Timer(object):
|
||||
def __init__(self, function, delay, *args, **kwargs):
|
||||
self.function = function
|
||||
def __init__(self, bot, event_name, delay, **kwargs):
|
||||
self.bot = bot
|
||||
self.event_name = event_name
|
||||
self.delay = delay
|
||||
self.kwargs = kwargs
|
||||
self.args = args
|
||||
self._done = False
|
||||
self.call_count = 0
|
||||
|
||||
|
@ -21,7 +21,8 @@ class Timer(object):
|
|||
def call(self):
|
||||
self._done = True
|
||||
self.call_count +=1
|
||||
self.function(self, *self.args, **self.kwargs)
|
||||
self.bot.events.on("timer").on(self.event_name).call(
|
||||
timer=self, **self.kwargs)
|
||||
|
||||
def redo(self):
|
||||
self._done = False
|
||||
|
|
|
@ -12,6 +12,7 @@ class Module(object):
|
|||
help="Set a reminder", usage="<time> <message>")
|
||||
bot.events.on("received").on("numeric").on("001").hook(
|
||||
self.on_connect)
|
||||
bot.events.on("timer").on("in").hook(self.timer_due)
|
||||
|
||||
def on_connect(self, event):
|
||||
self.load_reminders(event["server"])
|
||||
|
@ -36,7 +37,7 @@ class Module(object):
|
|||
for target, due_time, server_id, nickname, message in user_reminders[1]:
|
||||
time_left = due_time-time.time()
|
||||
if time_left > 0:
|
||||
self.bot.add_timer(self.timer_due, time_left, target=target,
|
||||
self.bot.add_timer("in", time_left, target=target,
|
||||
due_time=due_time, server_id=server_id, nickname=nickname,
|
||||
message=message)
|
||||
else:
|
||||
|
@ -56,7 +57,7 @@ class Module(object):
|
|||
event["server"].id, event["user"].nickname, message])
|
||||
event["server"].set_setting(setting, reminders)
|
||||
|
||||
self.bot.add_timer(self.timer_due, seconds,
|
||||
self.bot.add_timer("in", seconds,
|
||||
target=event["target"].name, due_time=due_time,
|
||||
server_id=event["server"].id, nickname=event["user"].nickname,
|
||||
message=message)
|
||||
|
@ -69,13 +70,12 @@ class Module(object):
|
|||
event["stderr"].write(
|
||||
"Please provided a valid time above 0 seconds")
|
||||
|
||||
def timer_due(self, timer, **kwargs):
|
||||
def timer_due(self, event):
|
||||
for server in self.bot.servers.values():
|
||||
if kwargs["server_id"] == server.id:
|
||||
server.send_message(kwargs["target"],
|
||||
if event["server_id"] == server.id:
|
||||
server.send_message(event["target"],
|
||||
"%s, this is your reminder: %s" % (
|
||||
kwargs["nickname"], kwargs["message"]))
|
||||
event["nickname"], event["message"]))
|
||||
break
|
||||
setting = "in-%s" % kwargs["nickname"]
|
||||
self.remove_timer(kwargs["target"], kwargs["due_time"],
|
||||
kwargs["server_id"], kwargs["nickname"], kwargs["message"])
|
||||
self.remove_timer(event["target"], event["due_time"],
|
||||
event["server_id"], event["nickname"], event["message"])
|
||||
|
|
Loading…
Reference in a new issue