added utf8 support, limit messages length to prevent unexpected disconnects

This commit is contained in:
Antoine Pietri authored and Ammar Askar committed 2013-01-31 20:44:43 +05:00
1 parent 4988ab6ae1
commit 7e993237e3
3 files changed
+10 -11

No files matched your search

+5 -6
View File
@@ -46,7 +46,7 @@ def readByteArray(FileObject, length):
def readString(FileObject): def readString(FileObject):
length = readShort(FileObject) * 2 length = readShort(FileObject) * 2
return FileObject.read(length).decode("utf-16be") return unicode(FileObject.read(length), "utf-16be")
def sendBoolean(socket, value): def sendBoolean(socket, value):
@@ -88,10 +88,9 @@ def sendDouble(socket, value):
def sendString(socket, value): def sendString(socket, value):
if (type(value) is not types.StringType): value = unicode(value).encode('utf-16be')
value = str(value) socket.send(struct.pack('>h', len(value) / 2))
socket.send(struct.pack('>h', value.__len__())) socket.send(value)
socket.send(value.encode('utf-16be'))
def readEntityMetadata(FileObject): def readEntityMetadata(FileObject):
@@ -145,4 +144,4 @@ def readSlotData(FileObject):
} }
return {'BlockID': -1, return {'BlockID': -1,
'ItemCount': 0 'ItemCount': 0
} }
+3 -4
View File
@@ -5,6 +5,7 @@ import traceback
import threading import threading
import hashlib import hashlib
import string import string
import unicodedata
import Utils import Utils
import sys import sys
from networking import PacketSenderManager from networking import PacketSenderManager
@@ -194,12 +195,10 @@ class PacketListener(threading.Thread):
print "Logged in \o/ Received an entity id of " + str(packet['EntityID']) print "Logged in \o/ Received an entity id of " + str(packet['EntityID'])
elif (response == "\x03"): elif (response == "\x03"):
packet = PacketListenerManager.handle03(self.FileObject) packet = PacketListenerManager.handle03(self.FileObject)
# Add "\x1b" because it is essential for ANSI escapes emitted by translate_escapes
if not self.connection.options.disableAnsiColours: if not self.connection.options.disableAnsiColours:
filtered_string = filter(lambda x: x in string.printable + "\x1b", filtered_string = Utils.translate_escapes(packet['Message'])
Utils.translate_escapes(packet['Message']))
else: else:
filtered_string = filter(lambda x: x in string.printable, packet['Message']) filtered_string = packet['Message']
print filtered_string print filtered_string
elif (response == "\x04"): elif (response == "\x04"):
+2 -1
View File
@@ -79,7 +79,8 @@ if __name__ == "__main__":
try: try:
chat_input = raw_input() chat_input = raw_input()
if (connection.isConnected): if (connection.isConnected):
PacketSenderManager.send03(connection.grabSocket(), chat_input) PacketSenderManager.send03(connection.grabSocket(),
chat_input.decode('utf-8')[:100])
else: else:
pass pass
except KeyboardInterrupt, e: except KeyboardInterrupt, e: