From d30d984f057b5c37201247d1527371da0bd5f994 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Thu, 5 Jul 2012 14:43:15 -0400 Subject: [PATCH] Some refactoring. --- earwigbot/irc/connection.py | 12 +++++++++++- earwigbot/irc/frontend.py | 5 ----- earwigbot/irc/watcher.py | 6 ------ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/earwigbot/irc/connection.py b/earwigbot/irc/connection.py index 75d6399..740292f 100644 --- a/earwigbot/irc/connection.py +++ b/earwigbot/irc/connection.py @@ -89,6 +89,12 @@ class IRCConnection(object): else: self._send("QUIT") + def _process_defaults(self, line): + """Default process hooks for lines received on IRC.""" + self._last_recv = time() + if line[0] == "PING": # If we are pinged, pong back + self.pong(line[1]) + @property def host(self): """The hostname of the IRC server, like ``"irc.freenode.net"``.""" @@ -175,10 +181,11 @@ class IRCConnection(object): self._is_running = False break - self._last_recv = time() lines = read_buffer.split("\n") read_buffer = lines.pop() for line in lines: + line = line.strip().split() + self._process_defaults(line) self._process_message(line) if self.is_stopped(): break @@ -190,9 +197,12 @@ class IRCConnection(object): now = time() if now - self._last_recv > 60: if self._last_ping < self._last_recv: + log = "Last message was received over 60 seconds ago. Pinging." + self.logger.debug(log) self.ping(self.host) self._last_ping = now elif now - self._last_ping > 60: + self.logger.debug("No ping response in 60 seconds. Stopping.") self.stop() def stop(self, msg=None): diff --git a/earwigbot/irc/frontend.py b/earwigbot/irc/frontend.py index a301c18..cda2670 100644 --- a/earwigbot/irc/frontend.py +++ b/earwigbot/irc/frontend.py @@ -49,8 +49,6 @@ class Frontend(IRCConnection): def _process_message(self, line): """Process a single message from IRC.""" - line = line.strip().split() - if line[1] == "JOIN": data = Data(self.bot, self.nick, line, msgtype="JOIN") self.bot.commands.call("join", data) @@ -63,9 +61,6 @@ class Frontend(IRCConnection): self.bot.commands.call("msg_public", data) self.bot.commands.call("msg", data) - elif line[0] == "PING": # If we are pinged, pong back - self.pong(line[1]) - elif line[1] == "376": # On successful connection to the server # If we're supposed to auth to NickServ, do that: try: diff --git a/earwigbot/irc/watcher.py b/earwigbot/irc/watcher.py index 8303457..d333e52 100644 --- a/earwigbot/irc/watcher.py +++ b/earwigbot/irc/watcher.py @@ -50,8 +50,6 @@ class Watcher(IRCConnection): def _process_message(self, line): """Process a single message from IRC.""" - line = line.strip().split() - if line[1] == "PRIVMSG": chan = line[2] @@ -65,10 +63,6 @@ class Watcher(IRCConnection): rc.parse() # Parse a message into pagenames, usernames, etc. self._process_rc_event(rc) - # If we are pinged, pong back: - elif line[0] == "PING": - self.pong(line[1]) - # When we've finished starting up, join all watcher channels: elif line[1] == "376": for chan in self.bot.config.irc["watcher"]["channels"]: