| @@ -89,6 +89,12 @@ class IRCConnection(object): | |||||
| else: | else: | ||||
| self._send("QUIT") | 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 | @property | ||||
| def host(self): | def host(self): | ||||
| """The hostname of the IRC server, like ``"irc.freenode.net"``.""" | """The hostname of the IRC server, like ``"irc.freenode.net"``.""" | ||||
| @@ -175,10 +181,11 @@ class IRCConnection(object): | |||||
| self._is_running = False | self._is_running = False | ||||
| break | break | ||||
| self._last_recv = time() | |||||
| lines = read_buffer.split("\n") | lines = read_buffer.split("\n") | ||||
| read_buffer = lines.pop() | read_buffer = lines.pop() | ||||
| for line in lines: | for line in lines: | ||||
| line = line.strip().split() | |||||
| self._process_defaults(line) | |||||
| self._process_message(line) | self._process_message(line) | ||||
| if self.is_stopped(): | if self.is_stopped(): | ||||
| break | break | ||||
| @@ -190,9 +197,12 @@ class IRCConnection(object): | |||||
| now = time() | now = time() | ||||
| if now - self._last_recv > 60: | if now - self._last_recv > 60: | ||||
| if self._last_ping < self._last_recv: | 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.ping(self.host) | ||||
| self._last_ping = now | self._last_ping = now | ||||
| elif now - self._last_ping > 60: | elif now - self._last_ping > 60: | ||||
| self.logger.debug("No ping response in 60 seconds. Stopping.") | |||||
| self.stop() | self.stop() | ||||
| def stop(self, msg=None): | def stop(self, msg=None): | ||||
| @@ -49,8 +49,6 @@ class Frontend(IRCConnection): | |||||
| def _process_message(self, line): | def _process_message(self, line): | ||||
| """Process a single message from IRC.""" | """Process a single message from IRC.""" | ||||
| line = line.strip().split() | |||||
| if line[1] == "JOIN": | if line[1] == "JOIN": | ||||
| data = Data(self.bot, self.nick, line, msgtype="JOIN") | data = Data(self.bot, self.nick, line, msgtype="JOIN") | ||||
| self.bot.commands.call("join", data) | 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_public", data) | ||||
| self.bot.commands.call("msg", 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 | elif line[1] == "376": # On successful connection to the server | ||||
| # If we're supposed to auth to NickServ, do that: | # If we're supposed to auth to NickServ, do that: | ||||
| try: | try: | ||||
| @@ -50,8 +50,6 @@ class Watcher(IRCConnection): | |||||
| def _process_message(self, line): | def _process_message(self, line): | ||||
| """Process a single message from IRC.""" | """Process a single message from IRC.""" | ||||
| line = line.strip().split() | |||||
| if line[1] == "PRIVMSG": | if line[1] == "PRIVMSG": | ||||
| chan = line[2] | chan = line[2] | ||||
| @@ -65,10 +63,6 @@ class Watcher(IRCConnection): | |||||
| rc.parse() # Parse a message into pagenames, usernames, etc. | rc.parse() # Parse a message into pagenames, usernames, etc. | ||||
| self._process_rc_event(rc) | 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: | # When we've finished starting up, join all watcher channels: | ||||
| elif line[1] == "376": | elif line[1] == "376": | ||||
| for chan in self.bot.config.irc["watcher"]["channels"]: | for chan in self.bot.config.irc["watcher"]["channels"]: | ||||