Escape replacement macros in preferences
[mudpy.git] / mudpy / command.py
index 14640da..1138aa8 100644 (file)
@@ -1,6 +1,6 @@
 """User command functions 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.
 
@@ -235,6 +235,44 @@ def move(actor, parameters):
         actor.send("You cannot go that way.")
 
 
+def preferences(actor, parameters):
+    """List, view and change actor preferences."""
+
+    # Escape replacement macros in preferences
+    parameters = mudpy.misc.escape_macros(parameters)
+
+    message = ""
+    arguments = parameters.split()
+    allowed_prefs = set()
+    user_config = actor.universe.contents.get("mudpy.user")
+    if user_config:
+        allowed_prefs.update(user_config.get("pref_allow", []))
+        if actor.owner.account.get("administrator"):
+            allowed_prefs.update(user_config.get("pref_admin", []))
+    if not arguments:
+        message += "These are your current preferences:"
+        for pref in allowed_prefs:
+            message += ("$(eol)   $(red)%s $(grn)%s$(nrm)"
+                        % (pref, actor.owner.account.get(pref)))
+    elif arguments[0] not in allowed_prefs:
+        message += (
+            'Preference "%s" does not exist. Try the `preferences` command by '
+            "itself for a list of valid preferences." % arguments[0])
+    elif len(arguments) == 1:
+        message += "%s" % actor.owner.account.get(arguments[0])
+    else:
+        pref = arguments[0]
+        value = " ".join(arguments[1:])
+        try:
+            actor.owner.account.set(pref, value)
+            message += 'Preference "%s" set to "%s".' % (pref, value)
+        except ValueError:
+            message = (
+                'Preference "%s" cannot be set to type "%s".' % (
+                    pref, type(value)))
+    actor.send(message)
+
+
 def quit(actor):
     """Leave the world and go back to the main menu."""
     if actor.owner:
@@ -341,7 +379,7 @@ def say(actor, parameters):
         actor.send("What do you want to say?")
 
 
-def set(actor, parameters):
+def c_set(actor, parameters):
     """Set a facet of an element."""
     if not parameters:
         message = "You must specify an element, a facet and a value."