Imported from archive.
[mudpy.git] / muff / muffmenu.py
1 """Menu objects for the MUFF Engine"""
2
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.
5
6 # muff uses menu data stored in ini-style files supported by ConfigParser
7 import ConfigParser
8
9 # hack to load all modules in the muff package
10 import muff
11 for module in muff.__all__:
12         exec("import " + module)
13
14 def get_menu(state, error=None, echoing=True, choices={}):
15         """Show the correct menu text to a user."""
16
17         # begin with a telnet echo command sequence if needed
18         message = get_echo_sequence(state, echoing)
19
20         # get the description or error text
21         message += get_menu_description(state, error)
22
23         # get menu choices for the current state
24         message += get_formatted_menu_choices(state, choices)
25
26         # try to get a prompt, if it was defined
27         message += get_menu_prompt(state)
28
29         # throw in the default choice, if it exists
30         message += get_formatted_default_menu_choice(state)
31
32         # display a message indicating if echo is off
33         message += get_echo_message(state)
34
35         # return the assembly of various strings defined above
36         return message
37
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)
41
42 def get_echo_sequence(state, echoing):
43         """Build the appropriate IAC ECHO sequence as needed."""
44
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)
48
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)
52
53         # default is not to send an echo control sequence at all
54         else: return ""
55
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) "
60
61 def get_default_menu_choice(state):
62         """Return the default choice for a menu."""
63         return muffuniv.universe.categories["menu"][state].get("default")
64
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)] "
69         else: return ""
70
71 def get_menu_description(state, error):
72         """Get the description or error text."""
73
74         # an error condition was raised by the handler
75         if error:
76
77                 # try to get an error message matching the condition
78                 # and current state
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)"
82
83         # there was no error condition
84         else:
85
86                 # try to get a menu description for the current state
87                 description = muffuniv.universe.categories["menu"][state].get("description")
88
89         # return the description or error message
90         if description: description += "$(eol)$(eol)"
91         return description
92
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 += " "
97         return prompt
98
99 def get_menu_choices(user):
100         """Return a dict of choice:meaning."""
101         choices = {}
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))
107         return choices
108
109 def get_formatted_menu_choices(state, choices):
110         """Returns a formatted string of menu choices."""
111         choice_output = ""
112         choice_keys = choices.keys()
113         choice_keys.sort()
114         for choice in choice_keys:
115                 choice_output += "   [$(red)" + choice + "$(nrm)]  " + choices[choice] + "$(eol)"
116         if choice_output: choice_output += "$(eol)"
117         return choice_output
118
119 def get_menu_branches(state):
120         """Return a dict of choice:branch."""
121         branches = {}
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)
125         return branches
126
127 def get_default_branch(state):
128         """Return the default branch."""
129         return muffuniv.universe.categories["menu"][state].get("branch")
130
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)
137         else: return ""
138
139 def get_menu_actions(state):
140         """Return a dict of choice:branch."""
141         actions = {}
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)
145         return actions
146
147 def get_default_action(state):
148         """Return the default action."""
149         return muffuniv.universe.categories["menu"][state].get("action")
150
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)
157         else: return ""
158