Add support for user preferences
[mudpy.git] / mudpy / command.py
index 2eaf383..ebd9036 100644 (file)
@@ -235,6 +235,40 @@ def move(actor, parameters):
         actor.send("You cannot go that way.")
 
 
+def preferences(actor, parameters):
+    """List, view and change actor preferences."""
+    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: