# this creates a list of commands mentioned in the data files
command_list = command_data.sections()
-def handle_user_input(user, input):
+def handle_user_input(user, input_data):
"""The main handler, branches to a state-specific handler."""
- # TODO: change this to use a dict
- if user.state == "active": handler_active(user, input)
- elif user.state == "entering account name": handler_entering_account_name(user, input)
- elif user.state == "checking password": handler_checking_password(user, input)
- elif user.state == "checking new account name": handler_checking_new_account_name(user, input)
- elif user.state == "entering new password": handler_entering_new_password(user, input)
- elif user.state == "verifying new password": handler_verifying_new_password(user, input)
+ # the pairings of user state and command to run
+ handler_dictionary = {
+ "active": handler_active,
+ "entering account name": handler_entering_account_name,
+ "checking password": handler_checking_password,
+ "checking new account name": handler_checking_new_account_name,
+ "entering new password": handler_entering_new_password,
+ "verifying new password": handler_verifying_new_password
+ }
+ # check to make sure the state is expected, then call that handler
+ if user.state in handler_dictionary.keys():
+ handler_dictionary[user.state](user, input_data)
# if there's input with an unknown user state, something is wrong
- else: handler_fallthrough(user, input)
+ else: handler_fallthrough(user, input_data)
# since we got input, flag that the menu/prompt needs to be redisplayed
user.menu_seen = False
-def handler_entering_account_name(user, input):
+def handler_entering_account_name(user, input_data):
"""Handle the login account name."""
# did the user enter anything?
- if input:
+ if input_data:
# keep only the first word and convert to lower-case
- user.proposed_name = string.split(input)[0].lower()
-
- # try to get a password hash for the proposed name
- user.get_passhash()
+ user.proposed_name = string.split(input_data)[0].lower()
# if we have a password hash, time to request a password
- # TODO: make get_passhash() return pass/fail and test that
- if user.passhash:
+ if user.get_passhash():
user.state = "checking password"
# otherwise, this could be a brand new user
user.state = "checking new account name"
# if the user entered nothing for a name, then buhbye
- # TODO: make a disconnect state instead of calling command_quit()
else:
- command_quit(user)
+ user.state = "disconnecting"
-def handler_checking_password(user, input):
+def handler_checking_password(user, input_data):
"""Handle the login account password."""
# does the hashed input equal the stored hash?
- if md5.new(user.proposed_name + input).hexdigest() == user.passhash:
+ if md5.new(user.proposed_name + input_data).hexdigest() == user.passhash:
# if so, set the username and load from cold storage
user.name = user.proposed_name
user.error = "incorrect"
# we've exceeded the maximum number of password failures, so disconnect
- # TODO: make a disconnect state instead of calling command_quit()
else:
user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
- command_quit(user)
+ user.state = "disconnecting"
-def handler_checking_new_account_name(user, input):
+def handler_checking_new_account_name(user, input_data):
"""Handle input for the new user menu."""
# if there's input, take the first character and lowercase it
- if input:
- choice = input.lower()[0]
+ if input_data:
+ choice = input_data.lower()[0]
# if there's no input, use the default
else:
choice = muffmenu.get_default(user)
# user selected to disconnect
- # TODO: make a disconnect state instead of calling command_quit()
if choice == "d":
- command_quit(user)
+ user.state == "disconnecting"
# go back to the login screen
elif choice == "g":
else:
user.error = "default"
-def handler_entering_new_password(user, input):
+def handler_entering_new_password(user, input_data):
"""Handle a new password entry."""
# make sure the password is strong--at least one upper, one lower and
# one digit, seven or more characters in length
- if len(input) > 6 and len(filter(lambda x: x>="0" and x<="9", input)) and len(filter(lambda x: x>="A" and x<="Z", input)) and len(filter(lambda x: x>="a" and x<="z", input)):
+ if len(input_data) > 6 and len(filter(lambda x: x>="0" and x<="9", input_data)) and len(filter(lambda x: x>="A" and x<="Z", input_data)) and len(filter(lambda x: x>="a" and x<="z", input_data)):
# hash and store it, then move on to verification
- user.passhash = md5.new(user.name + input).hexdigest()
+ user.passhash = md5.new(user.name + input_data).hexdigest()
user.state = "verifying new password"
# the password was weak, try again if you haven't tried too many times
user.error = "weak"
# too many tries, so adios
- # TODO: make a disconnect state instead of calling command_quit()
else:
user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
- command_quit(user)
+ user.state = "disconnecting"
-def handler_verifying_new_password(user, input):
+def handler_verifying_new_password(user, input_data):
"""Handle the re-entered new password for verification."""
# hash the input and match it to storage
- if md5.new(user.name + input).hexdigest() == user.passhash:
+ if md5.new(user.name + input_data).hexdigest() == user.passhash:
# the hashes matched, so go active
# TODO: branch to character creation and selection menus
user.state = "entering new password"
# otherwise, sayonara
- # TODO: make a disconnect state instead of calling command_quit()
else:
user.send("$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)")
- command_quit(user)
+ user.state = "disconnecting"
-# TODO: um, input is a reserved word. better replace it in all sources with
-# something else, like input_data
-def handler_active(user, input):
+def handler_active(user, input_data):
"""Handle input for active users."""
# users reaching this stage should be considered authenticated
# split out the command (first word) and parameters (everything else)
try:
- inputlist = string.split(input, None, 1)
+ inputlist = string.split(input_data, None, 1)
command = inputlist[0]
except:
- command = input
+ command = input_data
try:
parameters = inputlist[1]
except:
# no data matching the entered command word
elif command: command_error(user, command, parameters)
-# TODO: need a log function to handle conditions like this instead of print()
-def handler_fallthrough(user, input):
+def handler_fallthrough(user, input_data):
"""Input received in an unknown user state."""
- if input:
- print("User \"" + user + "\" entered \"" + input + "\" while in unknown state \"" + user.state + "\".")
+ if input_data:
+ muffmisc.log("User \"" + user + "\" entered \"" + input_data + "\" while in unknown state \"" + user.state + "\".")
def command_halt(user, command="", parameters=""):
"""Halt the world."""
def command_quit(user, command="", parameters=""):
"""Quit the world."""
-
- # save to cold storage
- user.save()
-
- # close the connection
- user.connection.close()
-
- # remove from the list
- user.remove()
+ user.state = "disconnecting"
def command_help(user, command="", parameters=""):
"""List available commands and provide help for commands."""
action = "ask"
# say because the message ended in a singular period
- # TODO: entering one period results in a double-period--oops!
else:
action = "say"
- message += "."
+ if message.endswith("."):
+ message += "."
# capitalize a list of words within the message
# TODO: move this list to the config
"""Generic error for an unrecognized command word."""
# 90% of the time use a generic error
- if random.random() > 0.1:
+ if random.randrange(10):
message = "I'm not sure what \"" + command
if parameters:
message += " " + parameters
muffvars.userlist.append(user)
# make a note of it
- # TODO: need to log this crap
- print len(muffvars.userlist),"connection(s)"
+ muffmisc.log(str(len(muffvars.userlist)) + " connection(s)")
# iterate over the connected users
for each_user in muffvars.userlist:
# show the user a menu as needed
each_user.show_menu()
- # check for some input
- # TODO: make a separate function for this
- try:
- input_data = each_user.connection.recv(1024)
- except:
- input_data = ""
- # we got something
- if input_data:
+ # disconnect users with the appropriate state
+ if each_user.state == "disconnecting":
- # tack this on to any previous partial input
- each_user.partial_input += input_data
+ # save to cold storage
+ each_user.save()
- # the held input ends in a newline
- if each_user.partial_input[-1] == "\n":
+ # close the connection
+ each_user.connection.close()
- # filter out non-printable characters
- each_user.partial_input = filter(lambda x: x>=' ' and x<='~', each_user.partial_input)
+ # remove from the list
+ each_user.remove()
- # strip off leading/trailing whitespace
- each_user.partial_input = string.strip(each_user.partial_input)
+ else:
- # move it to the end of the input queue
- each_user.input_queue.append(each_user.partial_input)
+ # check for some input
+ # TODO: make a separate function for this
+ try:
+ input_data = each_user.connection.recv(1024)
+ except:
+ input_data = ""
+ # we got something
+ if input_data:
- # reset the held partial input
- each_user.partial_input = ""
+ # tack this on to any previous partial
+ each_user.partial_input += input_data
- # pass the first item in the input
- # queue to the main handler
- muffcmds.handle_user_input(each_user, each_user.input_queue[0])
+ # the held input ends in a newline
+ if each_user.partial_input[-1] == "\n":
- # remove the first item from the queue
- each_user.input_queue.remove(each_user.input_queue[0])
+ # filter out non-printables
+ each_user.partial_input = filter(lambda x: x>=' ' and x<='~', each_user.partial_input)
+
+ # strip off extra whitespace
+ each_user.partial_input = string.strip(each_user.partial_input)
+
+ # put on the end of the queue
+ each_user.input_queue.append(each_user.partial_input)
+
+ # reset the held partial input
+ each_user.partial_input = ""
+
+ # pass first item in the input
+ # queue to the main handler
+ muffcmds.handle_user_input(each_user, each_user.input_queue[0])
+
+ # then remove it from the queue
+ each_user.input_queue.remove(each_user.input_queue[0])
# the loop has terminated, so tear down all sockets
# TODO: move the save from command_halt() to here
muffsock.destroy_all_sockets()
# log a final message
- # TODO: need a logging function for this kind of stuff
- print "Shutting down now."
+ muffmisc.log("Shutting down now.")
# Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
# Licensed per terms in the LICENSE file distributed with this software.
+# used by several functions for random calls
+import random
+
+# random_name uses string.strip
+import string
+
+# the log function uses time.asctime for creating timestamps
+import time
+
# hack to load all modules in the muff package
import muff
for module in muff.__all__:
exec("import " + module)
-def broadcast(output):
+def broadcast(message):
"""Send a message to all connected users."""
- for each_user in muffvars.userlist:
- each_user.send(output)
+ for each_user in muffvars.userlist: each_user.send(message)
+
+def log(message):
+ """Log a message."""
+
+ # the time in posix log timestamp format
+ timestamp = time.asctime()[4:19]
+
+ # send the timestamp and message to standard output
+ print(timestamp + " " + message)
def wrap_ansi_text(text, width):
"""Wrap text with arbitrary width while ignoring ANSI colors."""
# return the newly-wrapped text
return text
+def weighted_choice(data):
+ """Takes a dict weighted by value and returns a random key."""
+
+ # this will hold our expanded list of keys from the data
+ expanded = []
+
+ # create thee expanded list of keys
+ for key in data.keys():
+ for count in range(data[key]):
+ expanded.append(key)
+
+ # return one at random
+ return random.choice(expanded)
+
+def random_name():
+ """Returns a random character name."""
+
+ # the vowels and consonants needed to create romaji syllables
+ vowels = [ "a", "i", "u", "e", "o" ]
+ consonants = ["'", "k", "z", "s", "sh", "z", "j", "t", "ch", "ts", "d", "n", "h", "f", "m", "y", "r", "w" ]
+
+ # this dict will hold our weighted list of syllables
+ syllables = {}
+
+ # generate the list with an even weighting
+ for consonant in consonants:
+ for vowel in vowels:
+ syllables[consonant + vowel] = 1
+
+ # we'll build the name into this string
+ name = ""
+
+ # create a name of random length from the syllables
+ for syllable in range(random.randrange(2, 6)):
+ name += weighted_choice(syllables)
+
+ # strip any leading quotemark, capitalize and return the name
+ return string.strip(name, "'").capitalize()
+