Imported from archive.
[mudpy.git] / lib / muff / muffmain.py
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.")