diff --git a/IRCBot.py b/IRCBot.py index f514b9e2..3509effa 100644 --- a/IRCBot.py +++ b/IRCBot.py @@ -14,6 +14,7 @@ class Bot(object): self.events = EventManager.EventHook(self) self.timers = [] self.events.on("timer").on("reconnect").hook(self.reconnect) + self.events.on("boot").on("done").hook(self.setup_timers) def add_server(self, id, hostname, port, password, ipv4, tls, nickname, username, realname, connect=False): @@ -36,9 +37,24 @@ class Bot(object): for server in self.servers.values(): self.connect(server) - def add_timer(self, event_name, delay, **kwargs): - timer = Timer.Timer(self, event_name, delay, **kwargs) - timer.set_started_time() + def setup_timers(self, event): + for setting, value in self.find_settings("timer-%"): + id = setting.split("timer-", 1)[1] + self.add_timer(value["event-name"], value["delay"], value[ + "next-due"], id, **value["kwargs"]) + def timer_setting(self, timer): + self.set_setting("timer-%s" % timer.id, { + "event-name": timer.event_name, "delay": timer.delay, + "next-due": timer.next_due, "kwargs": timer.kwargs}) + def timer_setting_remove(self, timer): + self.timers.remove(timer) + self.del_setting("timer-%s" % timer.id) + def add_timer(self, event_name, delay, next_due=None, id=None, **kwargs): + timer = Timer.Timer(self, event_name, delay, next_due, **kwargs) + if id: + timer.id = id + else: + self.timer_setting(timer) self.timers.append(timer) def next_timer(self): next = None @@ -50,10 +66,11 @@ 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) + self.timer_setting_remove(timer) + else: + self.timer_setting(timer) def register_read(self, server): self.poll.modify(server.fileno(), select.EPOLLIN) diff --git a/IRCServer.py b/IRCServer.py index 388843aa..9c4bc6de 100644 --- a/IRCServer.py +++ b/IRCServer.py @@ -41,6 +41,7 @@ class Server(object): context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) self.socket = context.wrap_socket(self.socket) self.cached_fileno = self.socket.fileno() + self.bot.events.on("timer").on("rejoin").hook(self.try_rejoin) def __repr__(self): return "%s:%s%s" % (self.target_hostname, "+" if self.tls else "", self.port) @@ -177,11 +178,12 @@ class Server(object): self.send("PING :%s" % nonce) def send_pong(self, nonce="hello"): self.send("PONG :%s" % nonce) - def try_rejoin(self, timer, channel_name, key): - if channel_name in self.attempted_join: - self.send_join(channel_name, key) + def try_rejoin(self, event): + if event["server_id"] == self.id and event["channel_name" + ] in self.attempted_join: + self.send_join(event["channel_name"], event["key"]) def send_join(self, channel_name, key=None): - self.attempted_join[channel_name.lower()] = None + self.attempted_join[channel_name.lower()] = key self.send("JOIN %s%s" % (channel_name, "" if key == None else " %s" % key)) def send_part(self, channel_name, reason=None): diff --git a/Timer.py b/Timer.py index 32392628..789e7e37 100644 --- a/Timer.py +++ b/Timer.py @@ -1,22 +1,27 @@ -import time +import time, uuid class Timer(object): - def __init__(self, bot, event_name, delay, **kwargs): + def __init__(self, bot, event_name, delay, next_due=None, **kwargs): + self.id = uuid.uuid4().hex self.bot = bot self.event_name = event_name self.delay = delay + if next_due: + self.next_due = next_due + else: + self.set_next_due() self.kwargs = kwargs self._done = False self.call_count = 0 - def set_started_time(self): - self.started_time = time.time() + def set_next_due(self): + self.next_due = time.time()+self.delay def due(self): return self.time_left() <= 0 def time_left(self): - return (self.started_time+self.delay)-time.time() + return self.next_due-time.time() def call(self): self._done = True @@ -26,7 +31,7 @@ class Timer(object): def redo(self): self._done = False - self.set_started_time() + self.set_next_due() def done(self): return self._done diff --git a/modules/in.py b/modules/in.py index 81b94111..4f4aef51 100644 --- a/modules/in.py +++ b/modules/in.py @@ -10,40 +10,8 @@ class Module(object): bot.events.on("received").on("command").on("in").hook( self.in_command, min_args=2, help="Set a reminder", usage="