diff --git a/bot.py b/bot.py index 12e6a13..539c33b 100644 --- a/bot.py +++ b/bot.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- ## Imports -import socket, string, re +import socket, string, re, time from config.irc_config import * from config.secure_config import * @@ -14,11 +14,16 @@ def main(): read_buffer = str() while 1: - read_buffer = read_buffer + actions.sock.recv(1024) - temp = string.split(read_buffer, "\n") - read_buffer = temp.pop() + try: + read_buffer = read_buffer + actions.get() + except RuntimeError: # socket broke + time.sleep(60) # sleep for sixty seconds + return # then exit our loop and restart the bot - for line in temp: + lines = string.split(read_buffer, "\n") + read_buffer = lines.pop() + + for line in lines: line = string.split(string.rstrip(line)) data = Data() diff --git a/irc/actions.py b/irc/actions.py index 2b3bddf..4bbf1d6 100644 --- a/irc/actions.py +++ b/irc/actions.py @@ -2,11 +2,20 @@ # Actions/commands to interface with IRC. +import string + class Actions: def __init__(self, sock): """actions/commands to interface with IRC""" self.sock = sock + def get(self, size = 4096): + """receive (get) data from the server""" + data = self.sock.recv(4096) + if not data: + raise RuntimeError("socket is dead") + return data + def send(self, msg): """send data to the server""" self.sock.send(msg + "\r\n")