+++ /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)
-
-# see if the menupath can be retrieved from muffconf
-try:
- if muffconf.get("files", "menus"): pass
-
-# otherwise, reload muffconf
-except AttributeError:
- reload(muffconf)
-
-# now we can safely nab the menu path setting and build a list of data files
-menu_path = muffconf.get("files", "menus")
-menu_files_index = ConfigParser.SafeConfigParser()
-menu_files_index.read(menu_path + "/index")
-menu_files = []
-for each_file in menu_files_index.get("index", "files").split():
- menu_files.append(menu_path + "/" + each_file)
-
-# read the menu files
-menu_data = ConfigParser.SafeConfigParser()
-menu_data.read(menu_files)
-
-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."""
- try:
- return menu_data.getboolean(state, "echo")
- except:
- return 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."""
- try:
- return menu_data.get(state, "default")
- except:
- return ""
-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
- try:
- return "$(red)" + menu_data.get(state, "error_" + error) + "$(nrm)$(eol)$(eol)"
-
- # otherwise, use a generic one
- except:
- return "$(red)That is not a valid choice...$(nrm)$(eol)$(eol)"
-
- # there was no error condition
- else:
-
- # try to get a menu description for the current state
- try:
- return menu_data.get(state, "description") + "$(eol)$(eol)"
-
- # otherwise, leave it blank
- except:
- return ""
-
-def get_menu_prompt(state):
- """Try to get a prompt, if it was defined."""
- try:
- return menu_data.get(state, "prompt") + " "
- except:
- return ""
-
-def get_menu_choices(user):
- """Return a dict of choice:meaning."""
- choices = {}
- for option in menu_data.options(user.state):
- if option.startswith("choice_"):
- choices[option.split("_", 2)[1]] = menu_data.get(user.state, option)
- elif option.startswith("create_"):
- choices[option.split("_", 2)[1]] = eval(menu_data.get(user.state, option))
- 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 = {}
- try:
- for option in menu_data.options(state):
- if option.startswith("branch_"):
- branches[option.split("_", 2)[1]] = menu_data.get(state, option)
- except:
- pass
- return branches
-
-def get_default_branch(state):
- """Return the default branch."""
- try:
- return menu_data.get(state, "branch")
- except:
- return ""
-
-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 = {}
- try:
- for option in menu_data.options(state):
- if option.startswith("action_"):
- actions[option.split("_", 2)[1]] = menu_data.get(state, option)
- except:
- pass
- return actions
-
-def get_default_action(state):
- """Return the default action."""
- try:
- return menu_data.get(state, "action")
- except:
- return ""
-
-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 ""
-