Safely log unknown Telnet options and commands
[mudpy.git] / mudpy / telnet.py
index 0ce9aec..e483c13 100644 (file)
@@ -1,6 +1,6 @@
 """Telnet functions and constants for the mudpy engine."""
 
-# Copyright (c) 2004-2018 mudpy authors. Permission to use, copy,
+# Copyright (c) 2004-2019 mudpy authors. Permission to use, copy,
 # modify, and distribute this software is granted under terms
 # provided in the LICENSE file distributed with this software.
 
@@ -97,7 +97,20 @@ def telnet_proto(*arguments):
 
 def translate_action(*command):
     """Convert a Telnet command sequence into text suitable for logging."""
-    return "%s %s" % (command_names[command[0]], option_names[command[1]])
+    try:
+        command_name = command_names[command[0]]
+    except KeyError:
+        # This should never happen since we filter unknown commands from
+        # the input queue, but added here for completeness since logging
+        # should never crash the process
+        command_name = str(command[0])
+    try:
+        option_name = option_names[command[1]]
+    except KeyError:
+        # This can happen for any of the myriad of Telnet options missing
+        # from the option_names dict
+        option_name = str(command[1])
+    return "%s %s" % (command_name, option_name)
 
 
 def send_command(user, *command):