Imported from archive.
[mudpy.git] / lib / muff / muffuser.py
index 9a5169c..1d12be3 100644 (file)
@@ -6,7 +6,7 @@
 # user accounts are stored in ini-style files supported by ConfigParser
 import ConfigParser
 
-# test for existence of the account dir with os.listdir and os.mkdir to make it
+# os is used to test for existence of the account dir and, if necessary, make it
 import os
 
 # string.replace is used to perform substitutions for color codes and the like
@@ -45,7 +45,7 @@ class User:
                self.password_tries = 1
 
                # the current state of the user
-               self.state = "entering account name"
+               self.state = "entering_account_name"
 
                # flag to indicate whether a menu has been displayed
                self.menu_seen = False
@@ -65,6 +65,9 @@ class User:
                # flag to indicate the current echo status of the client
                self.echoing = True
 
+               # the active avatar
+               self.avatar = None
+
                # an object containing persistent account data
                self.record = ConfigParser.SafeConfigParser()
 
@@ -219,14 +222,14 @@ class User:
                        # the filename to which we'll write
                        filename = account_path + "/" + self.name.lower()
 
-                       # if the directory doesn't exist, create it
+                       # open the user account file for writing
                        try:
-                               if os.listdir(account_path): pass
-                       except:
-                               os.mkdir(account_path)
+                               record_file = file(filename, "w")
 
-                       # open the user account file for writing
-                       record_file = file(filename, "w")
+                       # if the directory doesn't exist, create it first
+                       except IOError:
+                               os.makedirs(account_path)
+                               record_file = file(filename, "w")
 
                        # dump the account data to it
                        self.record.write(record_file)
@@ -240,7 +243,17 @@ class User:
 
        def show_menu(self):
                """Send the user their current menu."""
-               self.send(muffmenu.get_menu(self))
+               if not self.menu_seen:
+                       self.menu_choices = muffmenu.get_menu_choices(self)
+                       self.send(muffmenu.get_menu(self.state, self.error, self.echoing, self.menu_choices), "")
+                       self.menu_seen = True
+                       self.error = False
+                       self.adjust_echoing()
+
+       def adjust_echoing(self):
+               """Adjust echoing to match state menu requirements."""
+               if self.echoing and not muffmenu.menu_echo_on(self.state): self.echoing = False
+               elif not self.echoing and muffmenu.menu_echo_on(self.state): self.echoing = True
 
        def remove(self):
                """Remove a user from the list of connected users."""
@@ -250,33 +263,33 @@ class User:
                """Send arbitrary text to a connected user."""
 
                # only when there is actual output
-               if output:
+               #if output:
 
-                       # start with a newline, append the message, then end
-                       # with the optional eol string passed to this function
-                       # and the ansi escape to return to normal text
-                       output = "\r\n" + output + eol + chr(27) + "[0m"
+               # start with a newline, append the message, then end
+               # with the optional eol string passed to this function
+               # and the ansi escape to return to normal text
+               output = "\r\n" + output + eol + chr(27) + "[0m"
 
-                       # find and replace macros in the output
-                       output = muffmisc.replace_macros(self, output)
+               # find and replace macros in the output
+               output = muffmisc.replace_macros(self, output)
 
-                       # wrap the text at 80 characters
-                       # TODO: prompt user for preferred wrap width
-                       output = muffmisc.wrap_ansi_text(output, 80)
+               # wrap the text at 80 characters
+               # TODO: prompt user for preferred wrap width
+               output = muffmisc.wrap_ansi_text(output, 80)
 
-                       # drop the formatted output into the output queue
-                       self.output_queue.append(output)
+               # drop the formatted output into the output queue
+               self.output_queue.append(output)
 
-                       # try to send the last item in the queue, remove it and
-                       # flag that menu display is not needed
-                       try:
-                               self.connection.send(self.output_queue[0])
-                               self.output_queue.remove(self.output_queue[0])
-                               self.menu_seen = False
+               # try to send the last item in the queue, remove it and
+               # flag that menu display is not needed
+               try:
+                       self.connection.send(self.output_queue[0])
+                       self.output_queue.remove(self.output_queue[0])
+                       self.menu_seen = False
 
-                       # but if we can't, that's okay too
-                       except:
-                               pass
+               # but if we can't, that's okay too
+               except:
+                       pass
 
        def pulse(self):
                """All the things to do to the user per increment."""
@@ -343,3 +356,31 @@ class User:
                                # put on the end of the queue
                                self.input_queue.append(line)
 
+       def new_avatar(self):
+               """Instantiate a new, unconfigured avatar for this user."""
+               try:
+                       counter = muffvars.variable_data.getint("counters", "next_actor")
+               except:
+                       muffmisc.log("get next_actor failed")
+                       counter = 1
+               while muffuniv.element_exists("actor:" + repr(counter)): counter += 1
+               muffvars.variable_data.set("counters", "next_actor", counter + 1)
+               self.avatar = muffuniv.Element("actor:" + repr(counter))
+               try:
+                       avatars = self.record.get("account", "avatars").split()
+               except:
+                       avatars = []
+               avatars.append(self.avatar.key)
+               self.record.set("account", "avatars", " ".join(avatars))
+
+       def list_avatar_names(self):
+               """A test function to list names of assigned avatars."""
+               try:
+                       avatars = self.record.get("account", "avatars").split()
+               except:
+                       avatars = []
+               avatar_names = []
+               for avatar in avatars:
+                       avatar_names.append(muffuniv.universe.contents[avatar].get("name"))
+               return avatar_names
+