Fix TypeError with IAC escaping
[mudpy.git] / mudpy / misc.py
index 2da1b40..154b555 100644 (file)
@@ -1,6 +1,6 @@
 """Miscellaneous functions for the mudpy engine."""
 
-# Copyright (c) 2004-2017 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.
 
@@ -129,16 +129,25 @@ class Element:
             # updated data from files
             raise PermissionError("Altering elements in read-only files is "
                                   "disallowed")
+        # Coerce some values to appropriate data types
+        # TODO(fungi) Move these to a separate validation mechanism
         if facet in ["loglevel"]:
             value = int(value)
         elif facet in ["administrator"]:
             value = bool(value)
-        if not self.has_facet(facet) or not self.get(facet) == value:
-            node = ".".join((self.key, facet))
+
+        # The canonical node for this facet within its origin
+        node = ".".join((self.key, facet))
+
+        if node not in self.origin.data or self.origin.data[node] != value:
+            # Be careful to only update the origin's contents when required,
+            # since that affects whether the backing file gets written
             self.origin.data[node] = value
-            self.facethash[facet] = self.origin.data[node]
             self.origin.modified = True
 
+        # Make sure this facet is included in the element's facets
+        self.facethash[facet] = self.origin.data[node]
+
     def append(self, facet, value):
         """Append value to a list."""
         newlist = self.get(facet)
@@ -825,13 +834,13 @@ class User:
         if self.output_queue:
             try:
                 self.connection.send(self.output_queue[0])
-            except BrokenPipeError:
+            except (BrokenPipeError, ConnectionResetError):
                 if self.account and self.account.get("name"):
                     account = self.account.get("name")
                 else:
                     account = "an unknown user"
                 self.state = "disconnecting"
-                log("Broken pipe sending to %s." % account, 7)
+                log("Disconnected while sending to %s." % account, 7)
             del self.output_queue[0]
 
     def enqueue_input(self):
@@ -889,7 +898,7 @@ class User:
                 try:
                     line = line.decode("utf-8")
                 except UnicodeDecodeError:
-                    logline = "Non-UTF-8 characters from "
+                    logline = "Non-UTF-8 sequence from "
                     if self.account and self.account.get("name"):
                         logline += self.account.get("name") + ": "
                     else: