]> jfr.im git - erebus.git/blobdiff - bot.py
config - fix getboolean
[erebus.git] / bot.py
diff --git a/bot.py b/bot.py
index d5d397153821e7d3d8f4ce4c84cf476b3ec300a6..ed45293998fcb0efde7e83a55602ddf5beacaacc 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -8,9 +8,13 @@ from collections import deque
 
 MAXLEN = 400 # arbitrary max length of a command generated by Bot.msg functions
 
-class MyTimer(threading._Timer):
+if sys.version_info.major < 3:
+       timerbase = threading._Timer
+else:
+       timerbase = threading.Timer
+class MyTimer(timerbase):
        def __init__(self, *args, **kwargs):
-               threading._Timer.__init__(self, *args, **kwargs)
+               timerbase.__init__(self, *args, **kwargs)
                self.daemon = True
 
 
@@ -181,7 +185,7 @@ class Bot(object):
                                self.msg(nick, "I tried, but you're not authed!")
        def _got433(self, pieces):
                if not self.conn.registered(): #we're trying to connect
-                       newnick = "%s%d" % (self.nick, random.randint(111,999))
+                       newnick = "%s%d" % (self.nick, random.randint(111, 999))
                        self.conn.send("NICK %s" % (newnick))
                        self.nick = newnick
        def _gotjoin(self, pieces):
@@ -424,7 +428,7 @@ class Bot(object):
 class BotConnection(object):
        def __init__(self, parent, bind, server, port):
                self.parent = parent
-               self.buffer = ''
+               self.buffer = bytearray(8192)
                self.socket = None
 
                self.bind = bind
@@ -454,24 +458,21 @@ class BotConnection(object):
                return self.state == 2
 
        def send(self, line):
-               if self.parent.cfg.getboolean('debug', 'io'):
+               if self.parent.parent.cfg.getboolean('debug', 'io'):
                        self.parent.log('O', line)
-#              print "%09.3f %s [O] %s" % (time.time() % 100000, self.parent.nick, line)
                self.bytessent += len(line)
                self._write(line)
 
        def _write(self, line):
-               self.socket.sendall(line+"\r\n")
+               self.socket.sendall(line.encode('utf-8', 'backslashreplace')+b"\r\n")
 
        def read(self):
                self.buffer += self.socket.recv(8192)
                lines = []
 
-               while "\r\n" in self.buffer:
-                       pieces = self.buffer.split("\r\n", 1)
-#                      self.parent.log('I', pieces[0]) # replaced by statement in Bot.parse()
-#                      print "%09.3f %s [I] %s" % (time.time() % 100000, self.parent.nick, pieces[0])
-                       lines.append(pieces[0])
+               while b"\r\n" in self.buffer:
+                       pieces = self.buffer.split(b"\r\n", 1)
+                       lines.append(pieces[0].decode('utf-8', 'backslashreplace'))
                        self.buffer = pieces[1]
 
                return lines