Skip to content

Commit 51ba505

Browse files
committed
Try decoding in multiple codecs (from legacy)
1 parent de4f757 commit 51ba505

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

cloudbot/irc/client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
}
2525

2626

27+
def decode(bytestring):
28+
"""
29+
Tries to decode a bytestring using multiple encoding formats
30+
"""
31+
for codec in ('utf-8', 'iso-8859-1', 'shift_jis', 'cp1252'):
32+
try:
33+
return bytestring.decode(codec)
34+
except UnicodeDecodeError:
35+
continue
36+
return bytestring.decode('utf-8', errors='ignore')
37+
38+
2739
class IrcClient(Client):
2840
"""
2941
An implementation of Client for IRC.
@@ -275,7 +287,7 @@ def data_received(self, data):
275287

276288
while b"\r\n" in self._input_buffer:
277289
line_data, self._input_buffer = self._input_buffer.split(b"\r\n", 1)
278-
line = line_data.decode()
290+
line = decode(line_data)
279291

280292
# parse the line into a message
281293
if line.startswith(":"):

0 commit comments

Comments
 (0)