]> jfr.im git - erebus.git/blobdiff - bot.py
add compatibility with Python3. add README.
[erebus.git] / bot.py
diff --git a/bot.py b/bot.py
index 16979727e6194891ce0b02b024415ef8b2c687b7..0e6e9263d7b072ae372156346349c4f21195b092 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
 
 
@@ -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
@@ -456,22 +460,19 @@ class BotConnection(object):
        def send(self, line):
                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