+++ /dev/null
-"""Menu objects for the MUFF Engine"""
-
-# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
-# Licensed per terms in the LICENSE file distributed with this software.
-
-# muff uses menu data stored in ini-style files supported by ConfigParser
-import ConfigParser
-
-# hack to load all modules in the muff package
-import muff
-for module in muff.__all__:
- exec("import " + module)
-
-def get_menu(state, error=None, echoing=True, choices={}):
- """Show the correct menu text to a user."""
-
- # begin with a telnet echo command sequence if needed
- message = get_echo_sequence(state, echoing)
-
- # get the description or error text
- message += get_menu_description(state, error)
-
- # get menu choices for the current state
- message += get_formatted_menu_choices(state, choices)
-
- # try to get a prompt, if it was defined
- message += get_menu_prompt(state)
-
- # throw in the default choice, if it exists
- message += get_formatted_default_menu_choice(state)
-
- # display a message indicating if echo is off
- message += get_echo_message(state)
-
- # return the assembly of various strings defined above
- return message
-
-def menu_echo_on(state):
- """True if echo is on, false if it is off."""
- return muffuniv.universe.categories["menu"][state].getboolean("echo", True)
-
-def get_echo_sequence(state, echoing):
- """Build the appropriate IAC ECHO sequence as needed."""
-
- # if the user has echo on and the menu specifies it should be turned
- # off, send: iac + will + echo + null
- if echoing and not menu_echo_on(state): return chr(255) + chr(251) + chr(1) + chr(0)
-
- # if echo is not set to off in the menu and the user curently has echo
- # off, send: iac + wont + echo + null
- elif not echoing and menu_echo_on(state): return chr(255) + chr(252) + chr(1) + chr(0)
-
- # default is not to send an echo control sequence at all
- else: return ""
-
-def get_echo_message(state):
- """Return a message indicating that echo is off."""
- if menu_echo_on(state): return ""
- else: return "(won't echo) "
-
-def get_default_menu_choice(state):
- """Return the default choice for a menu."""
- return muffuniv.universe.categories["menu"][state].get("default")
-
-def get_formatted_default_menu_choice(state):
- """Default menu choice foratted for inclusion in a prompt string."""
- default = get_default_menu_choice(state)
- if default: return "[$(red)" + default + "$(nrm)] "
- else: return ""
-
-def get_menu_description(state, error):
- """Get the description or error text."""
-
- # an error condition was raised by the handler
- if error:
-
- # try to get an error message matching the condition
- # and current state
- description = muffuniv.universe.categories["menu"][state].get("error_" + error)
- if not description: description = "That is not a valid choice..."
- description = "$(red)" + description + "$(nrm)"
-
- # there was no error condition
- else:
-
- # try to get a menu description for the current state
- description = muffuniv.universe.categories["menu"][state].get("description")
-
- # return the description or error message
- if description: description += "$(eol)$(eol)"
- return description
-
-def get_menu_prompt(state):
- """Try to get a prompt, if it was defined."""
- prompt = muffuniv.universe.categories["menu"][state].get("prompt")
- if prompt: prompt += " "
- return prompt
-
-def get_menu_choices(user):
- """Return a dict of choice:meaning."""
- choices = {}
- for facet in muffuniv.universe.categories["menu"][user.state].facets():
- if facet.startswith("choice_"):
- choices[facet.split("_", 2)[1]] = muffuniv.universe.categories["menu"][user.state].get(facet)
- elif facet.startswith("create_"):
- choices[facet.split("_", 2)[1]] = eval(muffuniv.universe.categories["menu"][user.state].get(facet))
- return choices
-
-def get_formatted_menu_choices(state, choices):
- """Returns a formatted string of menu choices."""
- choice_output = ""
- choice_keys = choices.keys()
- choice_keys.sort()
- for choice in choice_keys:
- choice_output += " [$(red)" + choice + "$(nrm)] " + choices[choice] + "$(eol)"
- if choice_output: choice_output += "$(eol)"
- return choice_output
-
-def get_menu_branches(state):
- """Return a dict of choice:branch."""
- branches = {}
- for facet in muffuniv.universe.categories["menu"][state].facets():
- if facet.startswith("branch_"):
- branches[facet.split("_", 2)[1]] = muffuniv.universe.categories["menu"][state].get(facet)
- return branches
-
-def get_default_branch(state):
- """Return the default branch."""
- return muffuniv.universe.categories["menu"][state].get("branch")
-
-def get_choice_branch(user, choice):
- """Returns the new state matching the given choice."""
- branches = get_menu_branches(user.state)
- if not choice: choice = get_default_menu_choice(user.state)
- if choice in branches.keys(): return branches[choice]
- elif choice in user.menu_choices.keys(): return get_default_branch(user.state)
- else: return ""
-
-def get_menu_actions(state):
- """Return a dict of choice:branch."""
- actions = {}
- for facet in muffuniv.universe.categories["menu"][state].facets():
- if facet.startswith("action_"):
- actions[facet.split("_", 2)[1]] = muffuniv.universe.categories["menu"][state].get(facet)
- return actions
-
-def get_default_action(state):
- """Return the default action."""
- return muffuniv.universe.categories["menu"][state].get("action")
-
-def get_choice_action(user, choice):
- """Run any indicated script for the given choice."""
- actions = get_menu_actions(user.state)
- if not choice: choice = get_default_menu_choice(user.state)
- if choice in actions.keys(): return actions[choice]
- elif choice in user.menu_choices.keys(): return get_default_action(user.state)
- else: return ""
-