56a9e57360d9a3262b30af626f80160b88502123
[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                         muffmisc.log(str(len(muffvars.userlist)) + " connection(s)")
53
54                 # iterate over the connected users
55                 for each_user in muffvars.userlist:
56
57                         # show the user a menu as needed
58                         each_user.show_menu()
59
60                         # disconnect users with the appropriate state
61                         if each_user.state == "disconnecting":
62
63                                 # save to cold storage
64                                 each_user.save()
65
66                                 # close the connection
67                                 each_user.connection.close()
68
69                                 # remove from the list
70                                 each_user.remove()
71
72                         else:
73
74                                 # check for some input
75                                 # TODO: make a separate function for this
76                                 try:
77                                         input_data = each_user.connection.recv(1024)
78                                 except:
79                                         input_data = ""
80                                 # we got something
81                                 if input_data:
82
83                                         # tack this on to any previous partial
84                                         each_user.partial_input += input_data
85
86                                         # the held input ends in a newline
87                                         if each_user.partial_input[-1] == "\n":
88
89                                                 # filter out non-printables
90                                                 each_user.partial_input = filter(lambda x: x>=' ' and x<='~', each_user.partial_input)
91
92                                                 # strip off extra whitespace
93                                                 each_user.partial_input = string.strip(each_user.partial_input)
94
95                                                 # put on the end of the queue
96                                                 each_user.input_queue.append(each_user.partial_input)
97
98                                                 # reset the held partial input
99                                                 each_user.partial_input = ""
100
101                                                 # pass first item in the input
102                                                 # queue to the main handler
103                                                 muffcmds.handle_user_input(each_user, each_user.input_queue[0])
104
105                                                 # then remove it from the queue
106                                                 each_user.input_queue.remove(each_user.input_queue[0])
107
108         # the loop has terminated, so tear down all sockets
109         # TODO: move the save from command_halt() to here
110         muffsock.destroy_all_sockets()
111
112         # log a final message
113         muffmisc.log("Shutting down now.")
114