Imported from archive.
[mudpy.git] / lib / muff / muffmain.py
1 """Main loop 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 # string.strip is used to clean up leading/trailing whitespace in user input
7 import string
8
9 # time.sleep is used in the loop to save cpu and provide crude pulse timing
10 import time
11
12 # hack to load all modules in the muff package
13 import muff
14 for module in muff.__all__:
15         exec("import " + module)
16
17 def main():
18         """The main loop."""
19
20         # loop indefinitely while the world is not flagged for termination
21         while not muffvars.terminate_world:
22
23                 # open the listening socket if it hasn't been already
24                 if not muffvars.newsocket: muffsock.initialize_server_socket()
25
26                 # pause for a configurable amount of time (decimal seconds)
27                 time.sleep(muffconf.config_data.getfloat("general", "increment"))
28
29                 # the world was flagged for a reload of all code/data
30                 if muffvars.reload_modules:
31
32                         # reload the muff package
33                         reload(muff)
34
35                         # reload all modules listed in the muff package
36                         for module in muff.__all__:
37                                 exec("reload(muff." + module + ")")
38
39                         # reset the reload flag
40                         muffvars.reload_modules = False
41
42                 # assign a user if a new connection is waiting
43                 user = muffsock.check_for_connection(muffvars.newsocket)
44
45                 # there was a new connection
46                 if user:
47
48                         # welcome to the user list
49                         muffvars.userlist.append(user)
50
51                         # make a note of it
52                         # TODO: need to log this crap
53                         print len(muffvars.userlist),"connection(s)"
54
55                 # iterate over the connected users
56                 for each_user in muffvars.userlist:
57
58                         # show the user a menu as needed
59                         each_user.show_menu()
60
61                         # check for some input
62                         # TODO: make a separate function for this
63                         try:
64                                 input_data = each_user.connection.recv(1024)
65                         except:
66                                 input_data = ""
67                         # we got something
68                         if input_data:
69
70                                 # tack this on to any previous partial input
71                                 each_user.partial_input += input_data
72
73                                 # the held input ends in a newline
74                                 if each_user.partial_input[-1] == "\n":
75
76                                         # filter out non-printable characters
77                                         each_user.partial_input = filter(lambda x: x>=' ' and x<='~', each_user.partial_input)
78
79                                         # strip off leading/trailing whitespace
80                                         each_user.partial_input = string.strip(each_user.partial_input)
81
82                                         # move it to the end of the input queue
83                                         each_user.input_queue.append(each_user.partial_input)
84
85                                         # reset the held partial input
86                                         each_user.partial_input = ""
87
88                                         # pass the first item in the input
89                                         # queue to the main handler
90                                         muffcmds.handle_user_input(each_user, each_user.input_queue[0])
91
92                                         # remove the first item from the queue
93                                         each_user.input_queue.remove(each_user.input_queue[0])
94
95         # the loop has terminated, so tear down all sockets
96         # TODO: move the save from command_halt() to here
97         muffsock.destroy_all_sockets()
98
99         # log a final message
100         # TODO: need a logging function for this kind of stuff
101         print "Shutting down now."
102