"""Menu objects for the MUFF Engine"""
-# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>
-# All rights reserved.
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
-#
-# - Redistributions of source code must retain the above copyright
-# notice, this list of conditions and the following disclaimer.
-# - Redistributions in binary form must reproduce the above
-# copyright notice, this list of conditions and the following
-# disclaimer in the documentation and/or other materials provided
-# with the distribution.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
-# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
+# 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
-import os
-import re
-import string
+# 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.config_data.get("general", "menu_path"):
- pass
+ 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 = []
-menu_path = muffconf.config_data.get("general", "menu_path")
-for each_file in os.listdir(menu_path):
+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_default(user):
- return menu_data.get(user.state, "default")
+def get_menu(state, error=None, echoing=True, choices={}):
+ """Show the correct menu text to a user."""
-def get_menu(user):
- if not user.menu_seen:
- if user.echoing:
- spacer = ""
- else:
- spacer = "$(eol)"
- try:
- if menu_data.get(user.state, "echo") == "off" and user.echoing:
- echo = chr(255) + chr(251) + chr(1) + chr(0)
- user.echoing = False
- else:
- echo = ""
- except:
- if not user.echoing:
- echo = chr(255) + chr(252) + chr(1) + chr(0)
- user.echoing = True
- else:
- echo = ""
- if user.error:
- try:
- description = "$(red)" + menu_data.get(user.state, "error_" + user.error) + "$(nrm)$(eol)$(eol)"
- except:
- description = "$(red)That is not a valid choice...$(nrm)$(eol)$(eol)"
- user.error = ""
- else:
- try:
- description = menu_data.get(user.state, "description") + "$(eol)$(eol)"
- except:
- description = ""
- try:
- choices = {}
- for option in menu_data.options(user.state):
- if re.match("choice_", option):
- choices[option.split("_")[1]] = menu_data.get(user.state, option)
- choice_keys = choices.keys()
- choice_keys.sort()
- choicestring = ""
- for choice in choice_keys:
- choicestring += " [$(red)" + choice + "$(nrm)] " + choices[choice] + "$(eol)"
- if choicestring:
- choicestring += "$(eol)"
- except:
- choicestring = ""
+ # 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:
- prompt = menu_data.get(user.state, "prompt") + " "
+ return "$(red)" + menu_data.get(state, "error_" + error) + "$(nrm)$(eol)$(eol)"
+
+ # otherwise, use a generic one
except:
- prompt = ""
+ 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:
- default = "[$(red)" + menu_data.get(user.state, "default") + "$(nrm)] "
+ return menu_data.get(state, "description") + "$(eol)$(eol)"
+
+ # otherwise, leave it blank
except:
- default = ""
- if user.echoing:
- echoing = ""
- else:
- echoing = "(won't echo) "
- user.send(echo + spacer + description + choicestring + prompt + default + echoing, "")
- user.menu_seen = True
+ 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 ""