Support clients using CR+NUL to signal EOL
[mudpy.git] / mudpy / telnet.py
index 9feac03..ed99faf 100644 (file)
@@ -1,6 +1,6 @@
 """Telnet functions and constants for the mudpy engine."""
 
-# Copyright (c) 2004-2015 Jeremy Stanley <fungi@yuggoth.org>. Permission
+# Copyright (c) 2004-2018 Jeremy Stanley <fungi@yuggoth.org>. Permission
 # to use, copy, modify, and distribute this software is granted under
 # terms provided in the LICENSE file distributed with this software.
 
@@ -125,9 +125,11 @@ def negotiate_telnet_options(user):
         # the byte following the IAC is our command
         command = text[position+1]
 
-        # replace a double (literal) IAC if there's aLF later
+        # replace a double (literal) IAC if there's a CR+NUL or CR+LF later
         if command is IAC:
-            if text.find("\n", position) > 0:
+            if (
+                    text.find(b"\r\0", position) > 0 or
+                    text.find(b"\r\n", position) > 0):
                 position += 1
                 text = text[:position] + text[position + 1:]
             else:
@@ -179,10 +181,10 @@ def negotiate_telnet_options(user):
 
         # subnegotiation options
         elif len_text > position + 4 and command is SB:
-            telopt = ord(text[position + 2])
+            telopt = text[position + 2]
             if telopt is TELOPT_NAWS:
                 user.columns = (
-                    ord(text[position + 3]) * 256 + ord(text[position + 4]))
+                    text[position + 3] * 256 + text[position + 4])
             end_subnegotiation = text.find(telnet_proto(IAC, SE), position)
             if end_subnegotiation > 0:
                 text = text[:position] + text[end_subnegotiation + 2:]