summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xminiircd39
-rw-r--r--test.py2
2 files changed, 31 insertions, 10 deletions
diff --git a/miniircd b/miniircd
index b3d6a20..b2534a5 100755
--- a/miniircd
+++ b/miniircd
@@ -36,6 +36,22 @@ from optparse import OptionParser
VERSION = "1.1"
+PY3 = sys.version_info[0] >= 3
+
+if PY3:
+ def buffer_to_socket(msg):
+ return msg.encode('utf-8')
+
+ def socket_to_buffer(buf):
+ return buf.decode('utf-8')
+else:
+ def buffer_to_socket(msg):
+ return msg
+
+ def socket_to_buffer(buf):
+ return buf
+
+
def create_directory(path):
if not os.path.isdir(path):
os.makedirs(path)
@@ -86,7 +102,10 @@ class Channel(object):
if not (self._state_path and os.path.exists(self._state_path)):
return
data = {}
- exec(open(self._state_path), {}, data)
+
+ with open(self._state_path, "rb") as state_file:
+ exec(state_file.read(), {}, data)
+
self._topic = data.get("topic", "")
self._key = data.get("key")
@@ -163,8 +182,8 @@ class Client(object):
if len(x[1]) > 0 and x[1][0] == ":":
arguments = [x[1][1:]]
else:
- y = string.split(x[1], " :", 1)
- arguments = string.split(y[0])
+ y = x[1].split(" :", 1)
+ arguments = y[0].split()
if len(y) == 2:
arguments.append(y[1])
self.__handle_command(command, arguments)
@@ -306,8 +325,9 @@ class Client(object):
for channelname in arguments[0].split(","):
if server.has_channel(channelname):
channels.append(server.get_channel(channelname))
- channels.sort(key=lambda x: x.name)
- for channel in channels:
+
+ sorted_channels = sorted(channels, key=lambda x: x.name)
+ for channel in sorted_channels:
self.reply("322 %s %s %d :%s"
% (self.nickname, channel.name,
len(channel.members), channel.topic))
@@ -573,7 +593,7 @@ class Client(object):
data = ""
quitmsg = x
if data:
- self.__readbuffer += data
+ self.__readbuffer += socket_to_buffer(data)
self.__parse_read_buffer()
self.__timestamp = time.time()
self.__sent_ping = False
@@ -582,7 +602,7 @@ class Client(object):
def socket_writable_notification(self):
try:
- sent = self.socket.send(self.__writebuffer)
+ sent = self.socket.send(buffer_to_socket(self.__writebuffer))
self.server.print_debug(
"[%s:%d] <- %r" % (
self.host, self.port, self.__writebuffer[:sent]))
@@ -894,14 +914,15 @@ class Server(object):
client.check_aliveness()
last_aliveness_check = now
-_maketrans = str.maketrans if sys.version_info[0] == 3 else string.maketrans
+
+_maketrans = str.maketrans if PY3 else string.maketrans
_ircstring_translation = _maketrans(
string.ascii_lowercase.upper() + "[]\\^",
string.ascii_lowercase + "{}|~")
def irc_lower(s):
- return string.translate(s, _ircstring_translation)
+ return s.translate(_ircstring_translation)
def main(argv):
diff --git a/test.py b/test.py
index 1cca905..9c44787 100644
--- a/test.py
+++ b/test.py
@@ -248,7 +248,7 @@ class TestBasicStuff(ServerFixture):
# :FQDN 353 nick = #fisk :
base_len = len(socket.getfqdn()) + 66
- one_line = (512 - base_len) / 50
+ one_line = (512 - base_len) // 50
nick_list_one = []
for i in range(one_line):
long_nick = '%s%d' % (base_nick, i)