1 """Menu objects for the MUFF Engine"""
3 # Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
4 # Licensed per terms in the LICENSE file distributed with this software.
6 # muff uses menu data stored in ini-style files supported by ConfigParser
9 # hack to load all modules in the muff package
11 for module in muff.__all__:
12 exec("import " + module)
14 def get_menu(state, error=None, echoing=True, choices={}):
15 """Show the correct menu text to a user."""
17 # begin with a telnet echo command sequence if needed
18 message = get_echo_sequence(state, echoing)
20 # get the description or error text
21 message += get_menu_description(state, error)
23 # get menu choices for the current state
24 message += get_formatted_menu_choices(state, choices)
26 # try to get a prompt, if it was defined
27 message += get_menu_prompt(state)
29 # throw in the default choice, if it exists
30 message += get_formatted_default_menu_choice(state)
32 # display a message indicating if echo is off
33 message += get_echo_message(state)
35 # return the assembly of various strings defined above
38 def menu_echo_on(state):
39 """True if echo is on, false if it is off."""
40 return muffuniv.universe.categories["menu"][state].getboolean("echo", True)
42 def get_echo_sequence(state, echoing):
43 """Build the appropriate IAC ECHO sequence as needed."""
45 # if the user has echo on and the menu specifies it should be turned
46 # off, send: iac + will + echo + null
47 if echoing and not menu_echo_on(state): return chr(255) + chr(251) + chr(1) + chr(0)
49 # if echo is not set to off in the menu and the user curently has echo
50 # off, send: iac + wont + echo + null
51 elif not echoing and menu_echo_on(state): return chr(255) + chr(252) + chr(1) + chr(0)
53 # default is not to send an echo control sequence at all
56 def get_echo_message(state):
57 """Return a message indicating that echo is off."""
58 if menu_echo_on(state): return ""
59 else: return "(won't echo) "
61 def get_default_menu_choice(state):
62 """Return the default choice for a menu."""
63 return muffuniv.universe.categories["menu"][state].get("default")
65 def get_formatted_default_menu_choice(state):
66 """Default menu choice foratted for inclusion in a prompt string."""
67 default = get_default_menu_choice(state)
68 if default: return "[$(red)" + default + "$(nrm)] "
71 def get_menu_description(state, error):
72 """Get the description or error text."""
74 # an error condition was raised by the handler
77 # try to get an error message matching the condition
79 description = muffuniv.universe.categories["menu"][state].get("error_" + error)
80 if not description: description = "That is not a valid choice..."
81 description = "$(red)" + description + "$(nrm)"
83 # there was no error condition
86 # try to get a menu description for the current state
87 description = muffuniv.universe.categories["menu"][state].get("description")
89 # return the description or error message
90 if description: description += "$(eol)$(eol)"
93 def get_menu_prompt(state):
94 """Try to get a prompt, if it was defined."""
95 prompt = muffuniv.universe.categories["menu"][state].get("prompt")
96 if prompt: prompt += " "
99 def get_menu_choices(user):
100 """Return a dict of choice:meaning."""
102 for facet in muffuniv.universe.categories["menu"][user.state].facets():
103 if facet.startswith("choice_"):
104 choices[facet.split("_", 2)[1]] = muffuniv.universe.categories["menu"][user.state].get(facet)
105 elif facet.startswith("create_"):
106 choices[facet.split("_", 2)[1]] = eval(muffuniv.universe.categories["menu"][user.state].get(facet))
109 def get_formatted_menu_choices(state, choices):
110 """Returns a formatted string of menu choices."""
112 choice_keys = choices.keys()
114 for choice in choice_keys:
115 choice_output += " [$(red)" + choice + "$(nrm)] " + choices[choice] + "$(eol)"
116 if choice_output: choice_output += "$(eol)"
119 def get_menu_branches(state):
120 """Return a dict of choice:branch."""
122 for facet in muffuniv.universe.categories["menu"][state].facets():
123 if facet.startswith("branch_"):
124 branches[facet.split("_", 2)[1]] = muffuniv.universe.categories["menu"][state].get(facet)
127 def get_default_branch(state):
128 """Return the default branch."""
129 return muffuniv.universe.categories["menu"][state].get("branch")
131 def get_choice_branch(user, choice):
132 """Returns the new state matching the given choice."""
133 branches = get_menu_branches(user.state)
134 if not choice: choice = get_default_menu_choice(user.state)
135 if choice in branches.keys(): return branches[choice]
136 elif choice in user.menu_choices.keys(): return get_default_branch(user.state)
139 def get_menu_actions(state):
140 """Return a dict of choice:branch."""
142 for facet in muffuniv.universe.categories["menu"][state].facets():
143 if facet.startswith("action_"):
144 actions[facet.split("_", 2)[1]] = muffuniv.universe.categories["menu"][state].get(facet)
147 def get_default_action(state):
148 """Return the default action."""
149 return muffuniv.universe.categories["menu"][state].get("action")
151 def get_choice_action(user, choice):
152 """Run any indicated script for the given choice."""
153 actions = get_menu_actions(user.state)
154 if not choice: choice = get_default_menu_choice(user.state)
155 if choice in actions.keys(): return actions[choice]
156 elif choice in user.menu_choices.keys(): return get_default_action(user.state)