added utf8 support, limit messages length to prevent unexpected disconnects

This commit is contained in:
Antoine Pietri 2013-01-31 20:42:47 +05:00 committed by Ammar Askar
parent 4988ab6ae1
commit 7e993237e3
3 changed files with 10 additions and 11 deletions

View File

@ -46,7 +46,7 @@ def readByteArray(FileObject, length):
def readString(FileObject):
length = readShort(FileObject) * 2
return FileObject.read(length).decode("utf-16be")
return unicode(FileObject.read(length), "utf-16be")
def sendBoolean(socket, value):
@ -88,10 +88,9 @@ def sendDouble(socket, value):
def sendString(socket, value):
if (type(value) is not types.StringType):
value = str(value)
socket.send(struct.pack('>h', value.__len__()))
socket.send(value.encode('utf-16be'))
value = unicode(value).encode('utf-16be')
socket.send(struct.pack('>h', len(value) / 2))
socket.send(value)
def readEntityMetadata(FileObject):
@ -145,4 +144,4 @@ def readSlotData(FileObject):
}
return {'BlockID': -1,
'ItemCount': 0
}
}

View File

@ -5,6 +5,7 @@ import traceback
import threading
import hashlib
import string
import unicodedata
import Utils
import sys
from networking import PacketSenderManager
@ -194,12 +195,10 @@ class PacketListener(threading.Thread):
print "Logged in \o/ Received an entity id of " + str(packet['EntityID'])
elif (response == "\x03"):
packet = PacketListenerManager.handle03(self.FileObject)
# Add "\x1b" because it is essential for ANSI escapes emitted by translate_escapes
if not self.connection.options.disableAnsiColours:
filtered_string = filter(lambda x: x in string.printable + "\x1b",
Utils.translate_escapes(packet['Message']))
filtered_string = Utils.translate_escapes(packet['Message'])
else:
filtered_string = filter(lambda x: x in string.printable, packet['Message'])
filtered_string = packet['Message']
print filtered_string
elif (response == "\x04"):

View File

@ -79,7 +79,8 @@ if __name__ == "__main__":
try:
chat_input = raw_input()
if (connection.isConnected):
PacketSenderManager.send03(connection.grabSocket(), chat_input)
PacketSenderManager.send03(connection.grabSocket(),
chat_input.decode('utf-8')[:100])
else:
pass
except KeyboardInterrupt, e: