Imported from archive.
[mudpy.git] / mudpy.py
index d1c434f..8b91054 100644 (file)
--- a/mudpy.py
+++ b/mudpy.py
@@ -1,15 +1,16 @@
 """Core objects for the mudpy engine."""
 
-# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
+# Copyright (c) 2006 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
 # Licensed per terms in the LICENSE file distributed with this software.
 
 # import some things we need
 from ConfigParser import RawConfigParser
 from md5 import new as new_md5
-from os import R_OK, access, chmod, makedirs, stat
+from os import _exit, R_OK, W_OK, access, chmod, close, fork, getpid, makedirs, remove, setsid, stat
 from os.path import abspath, dirname, exists, isabs, join as path_join
 from random import choice, randrange
 from re import match
+from signal import SIGHUP, SIGTERM, signal
 from socket import AF_INET, SO_REUSEADDR, SOCK_STREAM, SOL_SOCKET, socket
 from stat import S_IMODE, ST_MODE
 from sys import stderr
@@ -36,54 +37,96 @@ def excepthook(excepttype, value, traceback):
 import sys
 sys.excepthook = excepthook
 
+def sighook(what, where):
+       """Handle external signals."""
+
+       # a generic message
+       message = "Caught signal: "
+
+       # for a hangup signal
+       if what == SIGHUP:
+               message += "hangup (reloading)"
+               universe.reload_modules = True
+
+       # for a terminate signal
+       elif what == SIGTERM:
+               message += "terminate (halting)"
+               universe.terminate_world = True
+
+       # catchall for unexpected signals
+       else: message += str(what) + " (unhandled)"
+
+       # log what happened
+       log(message, 8)
+
+# assign the sgnal handlers
+signal(SIGHUP, sighook)
+signal(SIGTERM, sighook)
+
 class Element:
        """An element of the universe."""
        def __init__(self, key, universe, filename=None):
                """Set up a new element."""
 
-               # not owned by a user by default (used for avatars)
-               self.owner = None
-
-               # no contents in here by default
-               self.contents = {}
-
-               # an event queue for the element
-               self.events = {}
-
                # keep track of our key name
                self.key = key
 
-               # parse out appropriate category and subkey names, add to list
-               if self.key.find(":") > 0:
-                       self.category, self.subkey = self.key.split(":", 1)
+               # keep track of what universe it's loading` into
+               self.universe = universe
+
+               # clone attributes if this is replacing another element
+               if self.key in self.universe.contents:
+                       old_element = self.universe.contents[self.key]
+                       for attribute in vars(old_element).keys():
+                               exec("self." + attribute + " = old_element." + attribute)
+                       if self.owner: self.owner.avatar = self
+
+               # i guess this is a new element then
                else:
-                       self.category = "other"
-                       self.subkey = self.key
-               if not self.category in universe.categories: self.category = "other"
-               universe.categories[self.category][self.subkey] = self
 
-               # get an appropriate filename for the origin
-               if not filename: filename = universe.default_origins[self.category]
-               if not isabs(filename): filename = abspath(filename)
+                       # not owned by a user by default (used for avatars)
+                       self.owner = None
+
+                       # no contents in here by default
+                       self.contents = {}
+
+                       # parse out appropriate category and subkey names, add to list
+                       if self.key.find(":") > 0:
+                               self.category, self.subkey = self.key.split(":", 1)
+                       else:
+                               self.category = "other"
+                               self.subkey = self.key
+                       if not self.category in self.universe.categories:
+                               self.category = "other"
+                               self.subkey = self.key
 
-               # add the file if it doesn't exist yet
-               if not filename in universe.files: DataFile(filename, universe)
+                       # get an appropriate filename for the origin
+                       if not filename: filename = self.universe.default_origins[self.category]
+                       if not isabs(filename): filename = abspath(filename)
 
-               # record a pointer to the origin file
-               self.origin = universe.files[filename]
+                       # add the file if it doesn't exist yet
+                       if not filename in self.universe.files: DataFile(filename, self.universe)
+
+               # record or reset a pointer to the origin file
+               self.origin = self.universe.files[filename]
 
                # add a data section to the origin if necessary
                if not self.origin.data.has_section(self.key):
                        self.origin.data.add_section(self.key)
 
-               # add this element to the universe contents
-               universe.contents[self.key] = self
+               # add or replace this element in the universe
+               self.universe.contents[self.key] = self
+               self.universe.categories[self.category][self.subkey] = self
 
+       def reload(self):
+               """Create a new element and replace this one."""
+               new_element = Element(self.key, self.universe, self.origin.filename)
+               del(self)
        def destroy(self):
                """Remove an element from the universe and destroy it."""
                self.origin.data.remove_section(self.key)
-               del universe.categories[self.category][self.subkey]
-               del universe.contents[self.key]
+               del self.universe.categories[self.category][self.subkey]
+               del self.universe.contents[self.key]
                del self
        def facets(self):
                """Return a list of non-inherited facets for this element."""
@@ -103,7 +146,7 @@ class Element:
                if self.has_facet("inherit"):
                        ancestry = self.getlist("inherit")
                        for parent in ancestry[:]:
-                               ancestors = universe.contents[parent].ancestry()
+                               ancestors = self.universe.contents[parent].ancestry()
                                for ancestor in ancestors:
                                        if ancestor not in ancestry: ancestry.append(ancestor)
                        return ancestry
@@ -115,8 +158,8 @@ class Element:
                        return self.origin.data.get(self.key, facet)
                elif self.has_facet("inherit"):
                        for ancestor in self.ancestry():
-                               if universe.contents[ancestor].has_facet(facet):
-                                       return universe.contents[ancestor].get(facet)
+                               if self.universe.contents[ancestor].has_facet(facet):
+                                       return self.universe.contents[ancestor].get(facet)
                else: return default
        def getboolean(self, facet, default=None):
                """Retrieve values as boolean type."""
@@ -125,8 +168,8 @@ class Element:
                        return self.origin.data.getboolean(self.key, facet)
                elif self.has_facet("inherit"):
                        for ancestor in self.ancestry():
-                               if universe.contents[ancestor].has_facet(facet):
-                                       return universe.contents[ancestor].getboolean(facet)
+                               if self.universe.contents[ancestor].has_facet(facet):
+                                       return self.universe.contents[ancestor].getboolean(facet)
                else: return default
        def getint(self, facet, default=None):
                """Return values as int/long type."""
@@ -135,8 +178,8 @@ class Element:
                        return self.origin.data.getint(self.key, facet)
                elif self.has_facet("inherit"):
                        for ancestor in self.ancestry():
-                               if universe.contents[ancestor].has_facet(facet):
-                                       return universe.contents[ancestor].getint(facet)
+                               if self.universe.contents[ancestor].has_facet(facet):
+                                       return self.universe.contents[ancestor].getint(facet)
                else: return default
        def getfloat(self, facet, default=None):
                """Return values as float type."""
@@ -145,8 +188,8 @@ class Element:
                        return self.origin.data.getfloat(self.key, facet)
                elif self.has_facet("inherit"):
                        for ancestor in self.ancestry():
-                               if universe.contents[ancestor].has_facet(facet):
-                                       return universe.contents[ancestor].getfloat(facet)
+                               if self.universe.contents[ancestor].has_facet(facet):
+                                       return self.universe.contents[ancestor].getfloat(facet)
                else: return default
        def getlist(self, facet, default=None):
                """Return values as list type."""
@@ -179,7 +222,7 @@ class Element:
                """Create, attach and enqueue an event element."""
 
                # if when isn't specified, that means now
-               if not when: when = universe.get_time()
+               if not when: when = self.universe.get_time()
 
                # events are elements themselves
                event = Element("event:" + self.key + ":" + counter)
@@ -192,7 +235,7 @@ class Element:
                """Check if the user can run this command object."""
 
                # has to be in the commands category
-               if command not in universe.categories["command"].values(): result = False
+               if command not in self.universe.categories["command"].values(): result = False
 
                # avatars of administrators can run any command
                elif self.owner and self.owner.account.getboolean("administrator"): result = True
@@ -206,13 +249,23 @@ class Element:
                # pass back the result
                return result
 
+       def update_location(self):
+               """Make sure the location's contents contain this element."""
+               location = self.get("location")
+               if location in self.universe.contents:
+                       self.universe.contents[location].contents[self.key] = self
+       def clean_contents(self):
+               """Make sure the element's contents aren't bogus."""
+               for element in self.contents.values():
+                       if element.get("location") != self.key:
+                               del self.contents[element.key]
        def go_to(self, location):
                """Relocate the element to a specific location."""
                current = self.get("location")
-               if current and self.key in universe.contents[current].contents:
+               if current and self.key in self.universe.contents[current].contents:
                        del universe.contents[current].contents[self.key]
-               if location in universe.contents: self.set("location", location)
-               universe.contents[location].contents[self.key] = self
+               if location in self.universe.contents: self.set("location", location)
+               self.universe.contents[location].contents[self.key] = self
                self.look_at(location)
        def go_home(self):
                """Relocate the element to its default location."""
@@ -220,14 +273,14 @@ class Element:
                self.echo_to_location("You suddenly realize that " + self.get("name") + " is here.")
        def move_direction(self, direction):
                """Relocate the element in a specified direction."""
-               self.echo_to_location(self.get("name") + " exits " + universe.categories["internal"]["directions"].getdict(direction)["exit"] + ".")
-               self.send("You exit " + universe.categories["internal"]["directions"].getdict(direction)["exit"] + ".")
-               self.go_to(universe.contents[self.get("location")].link_neighbor(direction))
-               self.echo_to_location(self.get("name") + " arrives from " + universe.categories["internal"]["directions"].getdict(direction)["enter"] + ".")
+               self.echo_to_location(self.get("name") + " exits " + self.universe.categories["internal"]["directions"].getdict(direction)["exit"] + ".")
+               self.send("You exit " + self.universe.categories["internal"]["directions"].getdict(direction)["exit"] + ".")
+               self.go_to(self.universe.contents[self.get("location")].link_neighbor(direction))
+               self.echo_to_location(self.get("name") + " arrives from " + self.universe.categories["internal"]["directions"].getdict(direction)["enter"] + ".")
        def look_at(self, key):
                """Show an element to another element."""
                if self.owner:
-                       element = universe.contents[key]
+                       element = self.universe.contents[key]
                        message = ""
                        name = element.get("name")
                        if name: message += "$(cyn)" + name + "$(nrm)$(eol)"
@@ -237,25 +290,27 @@ class Element:
                        if portal_list:
                                portal_list.sort()
                                message += "$(cyn)[ Exits: " + ", ".join(portal_list) + " ]$(nrm)$(eol)"
-                       for element in universe.contents[self.get("location")].contents.values():
+                       for element in self.universe.contents[self.get("location")].contents.values():
                                if element.getboolean("is_actor") and element is not self:
                                        message += "$(yel)" + element.get("name") + " is here.$(nrm)$(eol)"
+                               elif element is not self:
+                                       message += "$(grn)" + element.get("impression") + "$(nrm)$(eol)"
                        self.send(message)
        def portals(self):
                """Map the portal directions for a room to neighbors."""
                portals = {}
                if match("""^location:-?\d+,-?\d+,-?\d+$""", self.key):
                        coordinates = [(int(x)) for x in self.key.split(":")[1].split(",")]
-                       directions = universe.categories["internal"]["directions"]
+                       directions = self.universe.categories["internal"]["directions"]
                        offsets = dict([(x, directions.getdict(x)["vector"]) for x in directions.facets()])
                        for portal in self.getlist("gridlinks"):
                                adjacent = map(lambda c,o: c+o, coordinates, offsets[portal])
                                neighbor = "location:" + ",".join([(str(x)) for x in adjacent])
-                               if neighbor in universe.contents: portals[portal] = neighbor
+                               if neighbor in self.universe.contents: portals[portal] = neighbor
                for facet in self.facets():
                        if facet.startswith("link_"):
                                neighbor = self.get(facet)
-                               if neighbor in universe.contents:
+                               if neighbor in self.universe.contents:
                                        portal = facet.split("_")[1]
                                        portals[portal] = neighbor
                return portals
@@ -265,17 +320,22 @@ class Element:
                if direction in portals: return portals[direction]
        def echo_to_location(self, message):
                """Show a message to other elements in the current location."""
-               for element in universe.contents[self.get("location")].contents.values():
+               for element in self.universe.contents[self.get("location")].contents.values():
                        if element is not self: element.send(message)
 
 class DataFile:
        """A file containing universe elements."""
        def __init__(self, filename, universe):
+               self.filename = filename
+               self.universe = universe
+               self.load()
+       def load(self):
+               """Read a file and create elements accordingly."""
                self.modified = False
                self.data = RawConfigParser()
-               if access(filename, R_OK): self.data.read(filename)
-               self.filename = filename
-               universe.files[filename] = self
+               if access(self.filename, R_OK): self.data.read(self.filename)
+               if not hasattr(self.universe, "files"): self.universe.files = {}
+               self.universe.files[self.filename] = self
                if self.data.has_option("__control__", "include_files"):
                        includes = makelist(self.data.get("__control__", "include_files"))
                else: includes = []
@@ -283,26 +343,24 @@ class DataFile:
                        origins = makedict(self.data.get("__control__", "default_files"))
                        for key in origins.keys():
                                if not key in includes: includes.append(key)
-                               universe.default_origins[key] = origins[key]
-                               if not key in universe.categories:
-                                       universe.categories[key] = {}
+                               self.universe.default_origins[key] = origins[key]
+                               if not key in self.universe.categories:
+                                       self.universe.categories[key] = {}
                if self.data.has_option("__control__", "private_files"):
                        for item in makelist(self.data.get("__control__", "private_files")):
                                if not item in includes: includes.append(item)
-                               if not item in universe.private_files:
+                               if not item in self.universe.private_files:
                                        if not isabs(item):
-                                               item = path_join(dirname(filename), item)
-                                       universe.private_files.append(item)
+                                               item = path_join(dirname(self.filename), item)
+                                       self.universe.private_files.append(item)
                for section in self.data.sections():
                        if section != "__control__":
-                               Element(section, universe, filename)
+                               Element(section, self.universe, self.filename)
                for include_file in includes:
                        if not isabs(include_file):
-                               include_file = path_join(dirname(filename), include_file)
-                       DataFile(include_file, universe)
-       def is_writeable(self):
-               """Returns True if the __control__ read_only is False."""
-               return not self.data.has_option("__control__", "read_only") or not self.data.getboolean("__control__", "read_only")
+                               include_file = path_join(dirname(self.filename), include_file)
+                       if include_file not in self.universe.files or not self.universe.files[include_file].is_writeable():
+                               DataFile(include_file, self.universe)
        def save(self):
                """Write the data, if necessary."""
 
@@ -317,7 +375,7 @@ class DataFile:
                        file_descriptor = file(self.filename, "w")
 
                        # if it's marked private, chmod it appropriately
-                       if self.filename in universe.private_files and oct(S_IMODE(stat(self.filename)[ST_MODE])) != 0600:
+                       if self.filename in self.universe.private_files and oct(S_IMODE(stat(self.filename)[ST_MODE])) != 0600:
                                chmod(self.filename, 0600)
 
                        # write it back sorted, instead of using ConfigParser
@@ -337,17 +395,20 @@ class DataFile:
 
                        # unset the modified flag
                        self.modified = False
+       def is_writeable(self):
+               """Returns True if the __control__ read_only is False."""
+               return not self.data.has_option("__control__", "read_only") or not self.data.getboolean("__control__", "read_only")
 
 class Universe:
        """The universe."""
+
        def __init__(self, filename=""):
                """Initialize the universe."""
                self.categories = {}
                self.contents = {}
                self.default_origins = {}
-               self.files = {}
                self.private_files = []
-               self.loglines = {}
+               self.loglines = []
                self.pending_events_long = {}
                self.pending_events_short = {}
                self.userlist = []
@@ -369,7 +430,46 @@ class Universe:
                                if access(filename, R_OK): break
                if not isabs(filename):
                        filename = abspath(filename)
-               DataFile(filename, self)
+               self.filename = filename
+               self.load()
+
+       def load(self):
+               """Load universe data from persistent storage."""
+
+               # the files dict must exist and filename needs to be read-only
+               if not hasattr(self, "files") or not ( self.filename in self.files and self.files[self.filename].is_writeable() ):
+
+                       # clear out all read-only files
+                       if hasattr(self, "files"):
+                               for data_filename in self.files.keys():
+                                       if not self.files[data_filename].is_writeable():
+                                               del self.files[data_filename]
+
+                       # start loading from the initial file
+                       DataFile(self.filename, self)
+
+               # make a list of inactive avatars
+               inactive_avatars = []
+               for account in self.categories["account"].values():
+                       inactive_avatars += [ (self.contents[x]) for x in account.getlist("avatars") ]
+               for user in self.userlist:
+                       if user.avatar in inactive_avatars:
+                               inactive_avatars.remove(user.avatar)
+
+               # go through all elements to clear out inactive avatar locations
+               for element in self.contents.values():
+                       location = element.get("location")
+                       if element in inactive_avatars and location:
+                               if location in self.contents and element.key in self.contents[location].contents:
+                                       del self.contents[location].contents[element.key]
+                               element.set("default_location", location)
+                               element.remove_facet("location")
+
+               # another pass to straighten out all the element contents
+               for element in self.contents.values():
+                       element.update_location()
+                       element.clean_contents()
+
        def save(self):
                """Save the universe to persistent storage."""
                for key in self.files: self.files[key].save()
@@ -386,7 +486,9 @@ class Universe:
                self.listening_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
 
                # bind the socket to to our desired server ipa and port
-               self.listening_socket.bind((self.categories["internal"]["network"].get("host"), self.categories["internal"]["network"].getint("port")))
+               host = self.categories["internal"]["network"].get("host")
+               port = self.categories["internal"]["network"].getint("port")
+               self.listening_socket.bind((host, port))
 
                # disable blocking so we can proceed whether or not we can
                # send/receive
@@ -396,7 +498,8 @@ class Universe:
                self.listening_socket.listen(1)
 
                # note that we're now ready for user connections
-               log("Waiting for connection(s)...")
+               if not host: host = "0.0.0.0"
+               log("Listening for Telnet connections on: " + host + ":" + str(port))
 
        def get_time(self):
                """Convenience method to get the elapsed time counter."""
@@ -407,23 +510,24 @@ class User:
 
        def __init__(self):
                """Default values for the in-memory user variables."""
+               self.account = None
                self.address = ""
-               self.last_address = ""
-               self.connection = None
                self.authenticated = False
-               self.password_tries = 0
-               self.state = "initial"
-               self.menu_seen = False
+               self.avatar = None
+               self.connection = None
+               self.echoing = True
                self.error = ""
                self.input_queue = []
+               self.last_address = ""
+               self.menu_choices = {}
+               self.menu_seen = False
+               self.negotiation_pause = 0
                self.output_queue = []
                self.partial_input = ""
-               self.echoing = True
+               self.password_tries = 0
                self.received_newline = True
+               self.state = "initial"
                self.terminator = IAC+GA
-               self.negotiation_pause = 0
-               self.avatar = None
-               self.account = None
 
        def quit(self):
                """Log, close the connection and remove."""
@@ -446,28 +550,13 @@ class User:
                # create a new user object
                new_user = User()
 
-               # set everything else equivalent
-               for attribute in [
-                       "address",
-                       "last_address",
-                       "connection",
-                       "authenticated",
-                       "password_tries",
-                       "state",
-                       "menu_seen",
-                       "error",
-                       "input_queue",
-                       "output_queue",
-                       "partial_input",
-                       "echoing",
-                       "received_newline",
-                       "terminator",
-                       "negotiation_pause",
-                       "avatar",
-                       "account"
-                       ]:
+               # set everything equivalent
+               for attribute in vars(self).keys():
                        exec("new_user." + attribute + " = self." + attribute)
 
+               # the avatar needs a new owner
+               if new_user.avatar: new_user.avatar.owner = new_user
+
                # add it to the list
                universe.userlist.append(new_user)
 
@@ -498,7 +587,10 @@ class User:
                                old_user.connection = self.connection
                                old_user.last_address = old_user.address
                                old_user.address = self.address
-                               old_user.echoing = self.echoing
+
+                               # may need to tell the new connection to echo
+                               if old_user.echoing:
+                                       old_user.send(get_echo_sequence(old_user.state, self.echoing), raw=True)
 
                                # take this one out of the list and delete
                                self.remove()
@@ -785,6 +877,22 @@ class User:
                """List names of assigned avatars."""
                return [ universe.contents[avatar].get("name") for avatar in self.account.getlist("avatars") ]
 
+def create_pidfile(universe):
+       """Write a file containing the current process ID."""
+       pid = str(getpid())
+       log("Process ID: " + pid)
+       file_name = universe.contents["internal:process"].get("pidfile")
+       if file_name:
+               file_descriptor = file(file_name, 'w')
+               file_descriptor.write(pid + "\n")
+               file_descriptor.flush()
+               file_descriptor.close()
+
+def remove_pidfile(universe):
+       """Remove the file containing the current process ID."""
+       file_name = universe.contents["internal:process"].get("pidfile")
+       if file_name and access(file_name, W_OK): remove(file_name)
+
 def makelist(value):
        """Turn string into list type."""
        if value[0] + value[-1] == "[]": return eval(value)
@@ -843,38 +951,44 @@ def log(message, level=0):
                while 0 < len(universe.loglines) >= max_log_lines: del universe.loglines[0]
                universe.loglines.append((level, timestamp + " " + line))
 
-def get_loglines(level, start, stop=0):
+def get_loglines(level, start, stop):
        """Return a specific range of loglines filtered by level."""
 
-       # begin with a blank message
-       message = ""
-
        # filter the log lines
-       loglines = filter(lambda x,y: x>=level, universe.loglines)
+       loglines = filter(lambda x: x[0]>=level, universe.loglines)
 
-       # we need this in several places
-       count = len(loglines)
+       # we need these in several places
+       total_count = str(len(universe.loglines))
+       filtered_count = len(loglines)
 
        # don't proceed if there are no lines
-       if count:
+       if filtered_count:
 
                # can't start before the begining or at the end
-               if start > count: start = count
+               if start > filtered_count: start = filtered_count
                if start < 1: start = 1
 
                # can't stop before we start
-               if stop >= start: stop = start - 1
+               if stop > start: stop = start
+               elif stop < 1: stop = 1
 
                # some preamble
-               message += "There are " + str(len(universe.loglist))
-               message += " log lines in memory and " + str(count)
+               message = "There are " + str(total_count)
+               message += " log lines in memory and " + str(filtered_count)
                message += " at or above level " + str(level) + "."
-               message += " The lines from " + str(stop)
-               message += " to " + str(start) + " are:$(eol)$(eol)"
+               message += " The lines from " + str(stop) + " to " + str(start)
+               message += " are:$(eol)$(eol)"
 
                # add the text from the selected lines
-               for line in loglines[-start:-stop]:
-                       message += "   " + line[1] + "$(eol)"
+               if stop > 1: range_lines = loglines[-start:-(stop-1)]
+               else: range_lines = loglines[-start:]
+               for line in range_lines:
+                       message += "   (" + str(line[0]) + ") " + line[1] + "$(eol)"
+
+       # there were no lines
+       else:
+               message = "None of the " + str(total_count)
+               message += " lines in memory matches your request."
 
        # pass it back
        return message
@@ -1082,12 +1196,15 @@ def on_pulse():
        # pause for a configurable amount of time (decimal seconds)
        sleep(universe.categories["internal"]["time"].getfloat("increment"))
 
-       # increment the elapsed increment counter
+       # increase the elapsed increment counter
        universe.categories["internal"]["counters"].set("elapsed", universe.categories["internal"]["counters"].getint("elapsed") + 1)
 
 def reload_data():
-       """Reload data into new persistent objects."""
+       """Reload all relevant objects."""
        for user in universe.userlist[:]: user.reload()
+       for element in universe.contents.values():
+               if element.origin.is_writeable(): element.reload()
+       universe.load()
 
 def check_for_connection(listening_socket):
        """Check for a waiting connection and return a new user object."""
@@ -1445,10 +1562,11 @@ def handler_active(user):
                else: command = None
 
                # if it's allowed, do it
-               if user.avatar.can_run(command): exec(command.get("action"))
+               actor = user.avatar
+               if actor.can_run(command): exec(command.get("action"))
 
                # otherwise, give an error
-               elif command_name: command_error(user.avatar, input_data)
+               elif command_name: command_error(actor, input_data)
 
        # if no input, just idle back with a prompt
        else: user.send("", just_prompt=True)
@@ -1482,8 +1600,8 @@ def command_reload(actor):
 def command_quit(actor):
        """Leave the world and go back to the main menu."""
        if actor.owner:
-               actor.owner.deactivate_avatar()
                actor.owner.state = "main_utility"
+               actor.owner.deactivate_avatar()
 
 def command_help(actor, parameters):
        """List available commands and provide help for commands."""
@@ -1561,7 +1679,8 @@ def command_say(actor, parameters):
 
                # get rid of quote marks on the ends of the message and
                # capitalize the first letter
-               message = parameters.strip("\"'`").capitalize()
+               message = parameters.strip("\"'`")
+               message = message[0].capitalize() + message[1:]
 
                # a dictionary of punctuation:action pairs
                actions = {}
@@ -1599,55 +1718,78 @@ def command_say(actor, parameters):
 def command_show(actor, parameters):
        """Show program data."""
        message = ""
-       if parameters.find(" ") < 1:
-               if parameters == "time":
-                       message = universe.categories["internal"]["counters"].get("elapsed") + " increments elapsed since the world was created."
-               elif parameters == "categories":
-                       message = "These are the element categories:$(eol)"
-                       categories = universe.categories.keys()
-                       categories.sort()
-                       for category in categories: message += "$(eol)   $(grn)" + category + "$(nrm)"
-               elif parameters == "files":
-                       message = "These are the current files containing the universe:$(eol)"
-                       filenames = universe.files.keys()
-                       filenames.sort()
-                       for filename in filenames: message += "$(eol)   $(grn)" + filename + "$(nrm)"
-               else: message = ""
-       else:
-               arguments = parameters.split()
-               if arguments[0] == "category":
-                       if arguments[1] in universe.categories:
-                               message = "These are the elements in the \"" + arguments[1] + "\" category:$(eol)"
-                               elements = universe.categories[arguments[1]].keys()
-                               elements.sort()
-                               for element in elements:
-                                       message += "$(eol)   $(grn)" + universe.categories[arguments[1]][element].key + "$(nrm)"
-               elif arguments[0] == "element":
-                       if arguments[1] in universe.contents:
-                               message = "These are the properties of the \"" + arguments[1] + "\" element:$(eol)"
-                               element = universe.contents[arguments[1]]
-                               facets = element.facets()
-                               facets.sort()
-                               for facet in facets:
-                                       message += "$(eol)   $(grn)" + facet + ": $(red)" + escape_macros(element.get(facet)) + "$(nrm)"
-               elif arguments[0] == "result":
-                       if len(arguments) > 1:
-                               try:
-                                       message = repr(eval(" ".join(arguments[1:])))
-                               except:
-                                       message = "Your expression raised an exception!"
-               elif arguments[0] == "log":
-                       if match("^\d+$", arguments[1]) and int(arguments[1]) > 0:
-                               linecount = int(arguments[1])
-                               if linecount > len(universe.loglines): linecount = len(universe.loglist)
-                               message = "There are " + str(len(universe.loglist)) + " log lines in memory."
-                               message += " The most recent " + str(linecount) + " lines are:$(eol)$(eol)"
-                               for line in universe.loglist[-linecount:]:
-                                       message += "   " + line + "$(eol)"
-                       else: message = "\"" + arguments[1] + "\" is not a positive integer greater than 0."
-       if not message:
-               if parameters: message = "I don't know what \"" + parameters + "\" is."
-               else: message = "What do you want to show?"
+       arguments = parameters.split()
+       if not parameters: message = "What do you want to show?"
+       elif arguments[0] == "time":
+               message = universe.categories["internal"]["counters"].get("elapsed") + " increments elapsed since the world was created."
+       elif arguments[0] == "categories":
+               message = "These are the element categories:$(eol)"
+               categories = universe.categories.keys()
+               categories.sort()
+               for category in categories: message += "$(eol)   $(grn)" + category + "$(nrm)"
+       elif arguments[0] == "files":
+               message = "These are the current files containing the universe:$(eol)"
+               filenames = universe.files.keys()
+               filenames.sort()
+               for filename in filenames:
+                       if universe.files[filename].is_writeable(): status = "rw"
+                       else: status = "ro"
+                       message += "$(eol)   $(red)(" + status + ") $(grn)" + filename + "$(nrm)"
+       elif arguments[0] == "category":
+               if len(arguments) != 2: message = "You must specify one category."
+               elif arguments[1] in universe.categories:
+                       message = "These are the elements in the \"" + arguments[1] + "\" category:$(eol)"
+                       elements = [(universe.categories[arguments[1]][x].key) for x in universe.categories[arguments[1]].keys()]
+                       elements.sort()
+                       for element in elements:
+                               message += "$(eol)   $(grn)" + element + "$(nrm)"
+               else: message = "Category \"" + arguments[1] + "\" does not exist."
+       elif arguments[0] == "file":
+               if len(arguments) != 2: message = "You must specify one file."
+               elif arguments[1] in universe.files:
+                       message = "These are the elements in the \"" + arguments[1] + "\" file:$(eol)"
+                       elements = universe.files[arguments[1]].data.sections()
+                       elements.sort()
+                       for element in elements:
+                               message += "$(eol)   $(grn)" + element + "$(nrm)"
+               else: message = "Category \"" + arguments[1] + "\" does not exist."
+       elif arguments[0] == "element":
+               if len(arguments) != 2: message = "You must specify one element."
+               elif arguments[1] in universe.contents:
+                       element = universe.contents[arguments[1]]
+                       message = "These are the properties of the \"" + arguments[1] + "\" element (in \"" + element.origin.filename + "\"):$(eol)"
+                       facets = element.facets()
+                       facets.sort()
+                       for facet in facets:
+                               message += "$(eol)   $(grn)" + facet + ": $(red)" + escape_macros(element.get(facet)) + "$(nrm)"
+               else: message = "Element \"" + arguments[1] + "\" does not exist."
+       elif arguments[0] == "result":
+               if len(arguments) < 2: message = "You need to specify an expression."
+               else:
+                       try:
+                               message = repr(eval(" ".join(arguments[1:])))
+                       except:
+                               message = "Your expression raised an exception!"
+       elif arguments[0] == "log":
+               if len(arguments) == 4:
+                       if match("^\d+$", arguments[3]) and int(arguments[3]) >= 0:
+                               stop = int(arguments[3])
+                       else: stop = -1
+               else: stop = 0
+               if len(arguments) >= 3:
+                       if match("^\d+$", arguments[2]) and int(arguments[2]) > 0:
+                               start = int(arguments[2])
+                       else: start = -1
+               else: start = 10
+               if len(arguments) >= 2:
+                       if match("^\d+$", arguments[1]) and 0 <= int(arguments[1]) <= 9:
+                               level = int(arguments[1])
+                       else: level = -1
+               else: level = 1
+               if level > -1 and start > -1 and stop > -1:
+                       message = get_loglines(level, start, stop)
+               else: message = "When specified, level must be 0-9 (default 1), start and stop must be >=1 (default 10 and 1)."
+       else: message = "I don't know what \"" + parameters + "\" is."
        actor.send(message)
 
 def command_create(actor, parameters):
@@ -1729,6 +1871,17 @@ def command_error(actor, input_data):
        # send the error message
        actor.send(message)
 
+def daemonize():
+       """Fork and disassociate from everything."""
+       if universe.contents["internal:process"].getboolean("daemon"):
+               import sys
+               log("Disassociating from the controlling terminal.")
+               if fork(): _exit(0)
+               setsid()
+               for stdpipe in range(3): close(stdpipe)
+               sys.stdin = sys.__stdin__ = file("/dev/null", "r")
+               sys.stdout = sys.stderr = sys.__stdout__ = sys.__stderr__ = file("/dev/null", "w")
+
 # if there is no universe, create an empty one
 if not "universe" in locals(): universe = Universe()