- if output:
- output = "$(eol)" + output + eol
- output = string.replace(output, "$(eol)", "\r\n")
- output = string.replace(output, "$(div)", "\r\n\r\n")
- output = string.replace(output, "$(bld)", chr(27)+"[1m")
- output = string.replace(output, "$(nrm)", chr(27)+"[0m")
- output = string.replace(output, "$(blk)", chr(27)+"[30m")
- output = string.replace(output, "$(grn)", chr(27)+"[32m")
- output = string.replace(output, "$(red)", chr(27)+"[31m")
- output = string.replace(output, "$(account)", self.name)
- output = muffmisc.wrap_ansi_text(output, 80)
- self.output_queue.append(output)
- try:
- self.connection.send(self.output_queue[0])
- self.output_queue.remove(self.output_queue[0])
- self.menu_seen = False
- except:
- pass
+ """Send arbitrary text to a connected user."""
+
+ # only when there is actual 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"
+
+ # 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)
+
+ # 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
+
+ # but if we can't, that's okay too
+ except:
+ pass
+
+ def pulse(self):
+ """All the things to do to the user per increment."""
+
+ # if the world is terminating, disconnect
+ if muffvars.terminate_world:
+ self.state = "disconnecting"
+ self.menu_seen = False
+
+ # show the user a menu as needed
+ self.show_menu()
+
+ # disconnect users with the appropriate state
+ if self.state == "disconnecting":
+ self.quit()
+
+ # the user is unique and not flagged to disconnect
+ else:
+
+ # check for input and add it to the queue
+ self.enqueue_input()
+
+ # there is input waiting in the queue
+ if self.input_queue: muffcmds.handle_user_input(self)
+
+ def enqueue_input(self):
+ """Process and enqueue any new input."""
+
+ # check for some input
+ try:
+ input_data = self.connection.recv(1024)
+ except:
+ input_data = ""
+
+ # we got something
+ if input_data:
+
+ # tack this on to any previous partial
+ self.partial_input += input_data
+
+ # separate multiple input lines
+ new_input_lines = self.partial_input.split("\n")
+
+ # if input doesn't end in a newline, replace the
+ # held partial input with the last line of it
+ if not self.partial_input.endswith("\n"):
+ self.partial_input = new_input_lines.pop()
+
+ # otherwise, chop off the extra null input and reset
+ # the held partial input
+ else:
+ new_input_lines.pop()
+ self.partial_input = ""
+
+ # iterate over the remaining lines
+ for line in new_input_lines:
+
+ # filter out non-printables
+ line = filter(lambda x: x>=' ' and x<='~', line)
+
+ # strip off extra whitespace
+ line = line.strip()
+
+ # 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