Imported from archive.
authorJeremy Stanley <fungi@yuggoth.org>
Sat, 30 Jul 2005 17:04:00 +0000 (17:04 +0000)
committerJeremy Stanley <fungi@yuggoth.org>
Sat, 30 Jul 2005 17:04:00 +0000 (17:04 +0000)
* lib/muff/muffcmds.py (command_quit): Replaced with a disconnecting
state.
(handle_user_input): Reorganized the user state handlers.
(handler_entering_account_name): Fixed a race condition where empty
accounts would be created even if the user never got around to
entering a password before disconnecting.

* lib/muff/muffmisc.py (log): New function to abstract logging of
arbitrary information, for later flexibility.
(random_name, weighted_choice): Added a random name generator
function for later use.

lib/menus/miscellaneous [new file with mode: 0644]
lib/muff/muffcmds.py
lib/muff/muffmain.py
lib/muff/muffmisc.py
lib/muff/muffsock.py
lib/muff/muffuser.py

diff --git a/lib/menus/miscellaneous b/lib/menus/miscellaneous
new file mode 100644 (file)
index 0000000..6c3375b
--- /dev/null
@@ -0,0 +1,3 @@
+[disconnecting]
+description = Disconnecting...
+
index 555526a..033c74c 100644 (file)
@@ -47,38 +47,39 @@ command_data.read(command_files)
 # 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
@@ -89,15 +90,14 @@ def handler_entering_account_name(user, input):
                        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
@@ -114,26 +114,24 @@ def handler_checking_password(user, input):
                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":
@@ -147,15 +145,15 @@ def handler_checking_new_account_name(user, input):
        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
@@ -164,16 +162,15 @@ def handler_entering_new_password(user, input):
                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
@@ -187,14 +184,11 @@ def handler_verifying_new_password(user, input):
                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
@@ -203,10 +197,10 @@ def handler_active(user, input):
 
        # 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:
@@ -222,11 +216,10 @@ def handler_active(user, input):
        # 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."""
@@ -254,15 +247,7 @@ def command_reload(user, command="", parameters=""):
 
 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."""
@@ -337,10 +322,10 @@ def command_say(user, command="", parameters=""):
                        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
@@ -360,7 +345,7 @@ def command_error(user, command="", parameters=""):
        """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
index 9e6c0b4..56a9e57 100644 (file)
@@ -49,8 +49,7 @@ def main():
                        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:
@@ -58,45 +57,58 @@ def main():
                        # 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.")
 
index ba90981..d0765ef 100644 (file)
@@ -3,15 +3,32 @@
 # 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."""
@@ -82,3 +99,42 @@ def wrap_ansi_text(text, width):
        # 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()
+
index 67010b1..40fffa5 100644 (file)
@@ -21,8 +21,7 @@ def check_for_connection(newsocket):
                return None
 
        # note that we got one
-       # TODO: we should log this crap somewhere
-       print "Connection from", address
+       muffmisc.log("Connection from " + address[0])
 
        # disable blocking so we can proceed whether or not we can send/receive
        connection.setblocking(0)
@@ -60,8 +59,7 @@ def initialize_server_socket():
        newsocket.listen(1)
 
        # note that we're now ready for user connections
-       # TODO: we should log this crap somewhere
-       print "Waiting for connection(s)..."
+       muffmisc.log("Waiting for connection(s)...")
 
        # store this in a globally-accessible place
        muffvars.newsocket = newsocket
@@ -70,8 +68,7 @@ def destroy_all_sockets():
        """Go through all connected users and close their sockets."""
 
        # note that we're closing all sockets
-       # TODO: we should log this crap somewhere
-       print "Closing remaining connections..."
+       muffmisc.log("Closing remaining connections...")
 
        # iterate over each connected user and close their associated sockets
        for user in muffvars.userlist:
index 6881869..ebe0c32 100644 (file)
@@ -97,10 +97,12 @@ class User:
                try:
                        temporary_record.read(filename)
                        self.passhash = temporary_record.get("account", "passhash")
+                       return True
 
                # otherwise, the password hash is empty
                except:
                        self.passhash = ""
+                       return False
 
        def save(self):
                """Record account data to cold storage."""