1 """Miscellaneous functions for the mudpy engine."""
3 # Copyright (c) 2004-2016 Jeremy Stanley <fungi@yuggoth.org>. Permission
4 # to use, copy, modify, and distribute this software is granted under
5 # terms provided in the LICENSE file distributed with this software.
24 """An element of the universe."""
26 def __init__(self, key, universe, filename=None, old_style=False):
27 """Set up a new element."""
29 # TODO(fungi): This can be removed after the transition is complete
30 self.old_style = old_style
32 # keep track of our key name
35 # keep track of what universe it's loading into
36 self.universe = universe
38 # clone attributes if this is replacing another element
39 if self.old_style and self.key in self.universe.contents:
40 old_element = self.universe.contents[self.key]
41 for attribute in vars(old_element).keys():
42 exec("self." + attribute + " = old_element." + attribute)
44 self.owner.avatar = self
46 # i guess this is a new element then
49 # set of facet keys from the universe
50 self.facethash = dict()
52 # not owned by a user by default (used for avatars)
55 # no contents in here by default
58 # parse out appropriate category and subkey names, add to list
59 if self.key.find(":") > 0:
60 self.category, self.subkey = self.key.split(":", 1)
62 self.category = "other"
63 self.subkey = self.key
64 if self.category not in self.universe.categories:
65 self.category = "other"
66 self.subkey = self.key
68 # get an appropriate filename for the origin
70 filename = self.universe.default_origins[self.category]
71 if not os.path.isabs(filename):
72 filename = os.path.abspath(filename)
74 # add the file if it doesn't exist yet
75 if filename not in self.universe.files:
76 mudpy.data.DataFile(filename, self.universe)
78 # record or reset a pointer to the origin file
79 self.origin = self.universe.files[filename]
81 # add a data section to the origin if necessary
82 if self.key not in self.origin.data:
83 self.origin.data[self.key] = {}
85 # add or replace this element in the universe
86 self.universe.contents[self.key] = self
87 self.universe.categories[self.category][self.subkey] = self
90 """Create a new element and replace this one."""
91 Element(self.key, self.universe, self.origin.filename,
92 old_style=self.old_style)
96 """Remove an element from the universe and destroy it."""
97 del(self.origin.data[self.key])
98 del self.universe.categories[self.category][self.subkey]
99 del self.universe.contents[self.key]
103 """Return a list of non-inherited facets for this element."""
106 return self.origin.data[self.key].keys()
107 except (AttributeError, KeyError):
110 return self.facethash
112 def has_facet(self, facet):
113 """Return whether the non-inherited facet exists."""
114 return facet in self.facets()
116 def remove_facet(self, facet):
117 """Remove a facet from the element."""
118 if self.has_facet(facet):
119 del(self.origin.data[self.key][facet])
120 self.origin.modified = True
123 """Return a list of the element's inheritance lineage."""
124 if self.has_facet("inherit"):
125 ancestry = self.get("inherit")
128 for parent in ancestry[:]:
129 ancestors = self.universe.contents[parent].ancestry()
130 for ancestor in ancestors:
131 if ancestor not in ancestry:
132 ancestry.append(ancestor)
137 def get(self, facet, default=None):
138 """Retrieve values."""
143 return self.origin.data[self.key][facet]
145 return self.origin.data[".".join((self.key, facet))]
146 except (KeyError, TypeError):
148 if self.has_facet("inherit"):
149 for ancestor in self.ancestry():
150 if self.universe.contents[ancestor].has_facet(facet):
151 return self.universe.contents[ancestor].get(facet)
155 def set(self, facet, value):
157 if facet in ["loglevel"]:
159 if not self.has_facet(facet) or not self.get(facet) == value:
161 if self.key not in self.origin.data:
162 self.origin.data[self.key] = {}
163 self.origin.data[self.key][facet] = value
165 node = ".".join((self.key, facet))
166 self.origin.data[node] = value
167 self.facethash[node] = self.origin.data[node]
168 self.origin.modified = True
170 def append(self, facet, value):
171 """Append value to a list."""
172 newlist = self.get(facet)
175 if type(newlist) is not list:
176 newlist = list(newlist)
177 newlist.append(value)
178 self.set(facet, newlist)
188 add_terminator=False,
191 """Convenience method to pass messages to an owner."""
204 def can_run(self, command):
205 """Check if the user can run this command object."""
207 # has to be in the commands category
208 if command not in self.universe.categories["command"].values():
211 # avatars of administrators can run any command
212 elif self.owner and self.owner.account.get("administrator"):
215 # everyone can run non-administrative commands
216 elif not command.get("administrative"):
219 # otherwise the command cannot be run by this actor
223 # pass back the result
226 def update_location(self):
227 """Make sure the location's contents contain this element."""
228 area = self.get("location")
229 if area in self.universe.contents:
230 self.universe.contents[area].contents[self.key] = self
232 def clean_contents(self):
233 """Make sure the element's contents aren't bogus."""
234 for element in self.contents.values():
235 if element.get("location") != self.key:
236 del self.contents[element.key]
238 def go_to(self, area):
239 """Relocate the element to a specific area."""
240 current = self.get("location")
241 if current and self.key in self.universe.contents[current].contents:
242 del universe.contents[current].contents[self.key]
243 if area in self.universe.contents:
244 self.set("location", area)
245 self.universe.contents[area].contents[self.key] = self
249 """Relocate the element to its default location."""
250 self.go_to(self.get("default_location"))
251 self.echo_to_location(
252 "You suddenly realize that " + self.get("name") + " is here."
255 def move_direction(self, direction):
256 """Relocate the element in a specified direction."""
257 motion = self.universe.contents["mudpy.movement.%s" % direction]
258 enter_term = motion.get("enter_term")
259 exit_term = motion.get("exit_term")
260 self.echo_to_location("%s exits %s." % (self.get("name"), exit_term))
261 self.send("You exit %s." % exit_term, add_prompt=False)
263 self.universe.contents[
264 self.get("location")].link_neighbor(direction)
266 self.echo_to_location("%s arrives from %s." % (
267 self.get("name"), enter_term))
269 def look_at(self, key):
270 """Show an element to another element."""
272 element = self.universe.contents[key]
274 name = element.get("name")
276 message += "$(cyn)" + name + "$(nrm)$(eol)"
277 description = element.get("description")
279 message += description + "$(eol)"
280 portal_list = list(element.portals().keys())
283 message += "$(cyn)[ Exits: " + ", ".join(
286 for element in self.universe.contents[
289 if element.get("is_actor") and element is not self:
290 message += "$(yel)" + element.get(
292 ) + " is here.$(nrm)$(eol)"
293 elif element is not self:
294 message += "$(grn)" + element.get(
300 """Map the portal directions for an area to neighbors."""
302 if re.match(r"""^area:-?\d+,-?\d+,-?\d+$""", self.key):
303 coordinates = [(int(x))
304 for x in self.key.split(":")[1].split(",")]
307 self.universe.contents["mudpy.movement.%s" % x].get("vector")
308 ) for x in self.universe.directions)
309 for portal in self.get("gridlinks"):
310 adjacent = map(lambda c, o: c + o,
311 coordinates, offsets[portal])
312 neighbor = "area:" + ",".join(
313 [(str(x)) for x in adjacent]
315 if neighbor in self.universe.contents:
316 portals[portal] = neighbor
317 for facet in self.facets():
318 if facet.startswith("link_"):
319 neighbor = self.get(facet)
320 if neighbor in self.universe.contents:
321 portal = facet.split("_")[1]
322 portals[portal] = neighbor
325 def link_neighbor(self, direction):
326 """Return the element linked in a given direction."""
327 portals = self.portals()
328 if direction in portals:
329 return portals[direction]
331 def echo_to_location(self, message):
332 """Show a message to other elements in the current location."""
333 for element in self.universe.contents[
336 if element is not self:
337 element.send(message)
344 def __init__(self, filename="", load=False):
345 """Initialize the universe."""
348 self.default_origins = {}
349 self.directions = set()
351 self.private_files = []
352 self.reload_flag = False
353 self.setup_loglines = []
354 self.startdir = os.getcwd()
355 self.terminate_flag = False
358 possible_filenames = [
360 "/usr/local/mudpy/etc/mudpy.yaml",
361 "/usr/local/etc/mudpy.yaml",
362 "/etc/mudpy/mudpy.yaml",
365 for filename in possible_filenames:
366 if os.access(filename, os.R_OK):
368 if not os.path.isabs(filename):
369 filename = os.path.join(self.startdir, filename)
370 self.filename = filename
372 # make sure to preserve any accumulated log entries during load
373 self.setup_loglines += self.load()
376 """Load universe data from persistent storage."""
378 # it's possible for this to enter before logging configuration is read
379 pending_loglines = []
381 # the files dict must exist and filename needs to be read-only
385 self.filename in self.files and self.files[
390 # clear out all read-only files
391 if hasattr(self, "files"):
392 for data_filename in list(self.files.keys()):
393 if not self.files[data_filename].is_writeable():
394 del self.files[data_filename]
396 # start loading from the initial file
397 mudpy.data.DataFile(self.filename, self)
399 # make a list of inactive avatars
400 inactive_avatars = []
401 for account in self.categories["account"].values():
402 for avatar in account.get("avatars"):
404 inactive_avatars.append(self.contents[avatar])
406 pending_loglines.append((
407 'Missing avatar "%s", possible data corruption' %
409 for user in self.userlist:
410 if user.avatar in inactive_avatars:
411 inactive_avatars.remove(user.avatar)
413 # go through all elements to clear out inactive avatar locations
414 for element in self.contents.values():
415 area = element.get("location")
416 if element in inactive_avatars and area:
417 if area in self.contents and element.key in self.contents[
420 del self.contents[area].contents[element.key]
421 element.set("default_location", area)
422 element.remove_facet("location")
424 # another pass to straighten out all the element contents
425 for element in self.contents.values():
426 element.update_location()
427 element.clean_contents()
428 return pending_loglines
431 """Create a new, empty Universe (the Big Bang)."""
432 new_universe = Universe()
433 for attribute in vars(self).keys():
434 exec("new_universe." + attribute + " = self." + attribute)
435 new_universe.reload_flag = False
440 """Save the universe to persistent storage."""
441 for key in self.files:
442 self.files[key].save()
444 def initialize_server_socket(self):
445 """Create and open the listening socket."""
447 # need to know the local address and port number for the listener
448 host = self.contents["mudpy.network"].get("host")
449 port = self.contents["mudpy.network"].get("port")
451 # if no host was specified, bind to all local addresses (preferring
459 # figure out if this is ipv4 or v6
460 family = socket.getaddrinfo(host, port)[0][0]
461 if family is socket.AF_INET6 and not socket.has_ipv6:
462 log("No support for IPv6 address %s (use IPv4 instead)." % host)
464 # create a new stream-type socket object
465 self.listening_socket = socket.socket(family, socket.SOCK_STREAM)
467 # set the socket options to allow existing open ones to be
468 # reused (fixes a bug where the server can't bind for a minute
469 # when restarting on linux systems)
470 self.listening_socket.setsockopt(
471 socket.SOL_SOCKET, socket.SO_REUSEADDR, 1
474 # bind the socket to to our desired server ipa and port
475 self.listening_socket.bind((host, port))
477 # disable blocking so we can proceed whether or not we can
479 self.listening_socket.setblocking(0)
481 # start listening on the socket
482 self.listening_socket.listen(1)
484 # note that we're now ready for user connections
486 "Listening for Telnet connections on: " +
487 host + ":" + str(port)
491 """Convenience method to get the elapsed time counter."""
492 return self.categories["internal"]["counters"].get("elapsed")
497 """This is a connected user."""
500 """Default values for the in-memory user variables."""
503 self.authenticated = False
506 self.connection = None
508 self.input_queue = []
509 self.last_address = ""
510 self.last_input = universe.get_time()
511 self.menu_choices = {}
512 self.menu_seen = False
513 self.negotiation_pause = 0
514 self.output_queue = []
515 self.partial_input = b""
516 self.password_tries = 0
517 self.state = "initial"
521 """Log, close the connection and remove."""
523 name = self.account.get("name")
527 message = "User " + name
529 message = "An unnamed user"
530 message += " logged out."
532 self.deactivate_avatar()
533 self.connection.close()
536 def check_idle(self):
537 """Warn or disconnect idle users as appropriate."""
538 idletime = universe.get_time() - self.last_input
539 linkdead_dict = universe.categories["internal"]["time"].get(
542 if self.state in linkdead_dict:
543 linkdead_state = self.state
545 linkdead_state = "default"
546 if idletime > linkdead_dict[linkdead_state]:
548 "$(eol)$(red)You've done nothing for far too long... goodbye!"
553 logline = "Disconnecting "
554 if self.account and self.account.get("name"):
555 logline += self.account.get("name")
557 logline += "an unknown user"
558 logline += (" after idling too long in the " + self.state
561 self.state = "disconnecting"
562 self.menu_seen = False
563 idle_dict = universe.categories["internal"]["time"].get("idle")
564 if self.state in idle_dict:
565 idle_state = self.state
567 idle_state = "default"
568 if idletime == idle_dict[idle_state]:
570 "$(eol)$(red)If you continue to be unproductive, "
571 + "you'll be shown the door...$(nrm)$(eol)"
575 """Save, load a new user and relocate the connection."""
577 # get out of the list
580 # create a new user object
583 # set everything equivalent
584 for attribute in vars(self).keys():
585 exec("new_user." + attribute + " = self." + attribute)
587 # the avatar needs a new owner
589 new_user.avatar.owner = new_user
592 universe.userlist.append(new_user)
594 # get rid of the old user object
597 def replace_old_connections(self):
598 """Disconnect active users with the same name."""
600 # the default return value
603 # iterate over each user in the list
604 for old_user in universe.userlist:
606 # the name is the same but it's not us
609 ) and old_user.account and old_user.account.get(
611 ) == self.account.get(
613 ) and old_user is not self:
617 "User " + self.account.get(
619 ) + " reconnected--closing old connection to "
620 + old_user.address + ".",
624 "$(eol)$(red)New connection from " + self.address
625 + ". Terminating old connection...$(nrm)$(eol)",
630 # close the old connection
631 old_user.connection.close()
633 # replace the old connection with this one
635 "$(eol)$(red)Taking over old connection from "
636 + old_user.address + ".$(nrm)"
638 old_user.connection = self.connection
639 old_user.last_address = old_user.address
640 old_user.address = self.address
642 # take this one out of the list and delete
648 # true if an old connection was replaced, false if not
651 def authenticate(self):
652 """Flag the user as authenticated and disconnect duplicates."""
653 if self.state is not "authenticated":
654 log("User " + self.account.get("name") + " logged in.", 2)
655 self.authenticated = True
656 if ("mudpy.limit" in universe.contents and self.account.subkey in
657 universe.contents["mudpy.limit"].get("admins")):
658 self.account.set("administrator", "True")
661 """Send the user their current menu."""
662 if not self.menu_seen:
663 self.menu_choices = get_menu_choices(self)
665 get_menu(self.state, self.error, self.menu_choices),
669 self.menu_seen = True
671 self.adjust_echoing()
673 def adjust_echoing(self):
674 """Adjust echoing to match state menu requirements."""
675 if mudpy.telnet.is_enabled(self, mudpy.telnet.TELOPT_ECHO,
677 if menu_echo_on(self.state):
678 mudpy.telnet.disable(self, mudpy.telnet.TELOPT_ECHO,
680 elif not menu_echo_on(self.state):
681 mudpy.telnet.enable(self, mudpy.telnet.TELOPT_ECHO,
685 """Remove a user from the list of connected users."""
686 universe.userlist.remove(self)
696 add_terminator=False,
699 """Send arbitrary text to a connected user."""
701 # unless raw mode is on, clean it up all nice and pretty
704 # strip extra $(eol) off if present
705 while output.startswith("$(eol)"):
707 while output.endswith("$(eol)"):
709 extra_lines = output.find("$(eol)$(eol)$(eol)")
710 while extra_lines > -1:
711 output = output[:extra_lines] + output[extra_lines + 6:]
712 extra_lines = output.find("$(eol)$(eol)$(eol)")
714 # start with a newline, append the message, then end
715 # with the optional eol string passed to this function
716 # and the ansi escape to return to normal text
717 if not just_prompt and prepend_padding:
718 if (not self.output_queue or not
719 self.output_queue[-1].endswith(b"\r\n")):
720 output = "$(eol)" + output
721 elif not self.output_queue[-1].endswith(
723 ) and not self.output_queue[-1].endswith(
726 output = "$(eol)" + output
727 output += eol + chr(27) + "[0m"
729 # tack on a prompt if active
730 if self.state == "active":
735 mode = self.avatar.get("mode")
737 output += "(" + mode + ") "
739 # find and replace macros in the output
740 output = replace_macros(self, output)
742 # wrap the text at the client's width (min 40, 0 disables)
744 if self.columns < 40:
748 output = wrap_ansi_text(output, wrap)
750 # if supported by the client, encode it utf-8
751 if mudpy.telnet.is_enabled(self, mudpy.telnet.TELOPT_BINARY,
753 encoded_output = output.encode("utf-8")
755 # otherwise just send ascii
757 encoded_output = output.encode("ascii", "replace")
759 # end with a terminator if requested
760 if add_prompt or add_terminator:
761 if mudpy.telnet.is_enabled(
762 self, mudpy.telnet.TELOPT_EOR, mudpy.telnet.US):
763 encoded_output += mudpy.telnet.telnet_proto(
764 mudpy.telnet.IAC, mudpy.telnet.EOR)
765 elif not mudpy.telnet.is_enabled(
766 self, mudpy.telnet.TELOPT_SGA, mudpy.telnet.US):
767 encoded_output += mudpy.telnet.telnet_proto(
768 mudpy.telnet.IAC, mudpy.telnet.GA)
770 # and tack it onto the queue
771 self.output_queue.append(encoded_output)
773 # if this is urgent, flush all pending output
777 # just dump raw bytes as requested
779 self.output_queue.append(output)
783 """All the things to do to the user per increment."""
785 # if the world is terminating, disconnect
786 if universe.terminate_flag:
787 self.state = "disconnecting"
788 self.menu_seen = False
790 # check for an idle connection and act appropriately
794 # if output is paused, decrement the counter
795 if self.state == "initial":
796 if self.negotiation_pause:
797 self.negotiation_pause -= 1
799 self.state = "entering_account_name"
801 # show the user a menu as needed
802 elif not self.state == "active":
805 # flush any pending output in the queue
808 # disconnect users with the appropriate state
809 if self.state == "disconnecting":
812 # check for input and add it to the queue
815 # there is input waiting in the queue
817 handle_user_input(self)
820 """Try to send the last item in the queue and remove it."""
821 if self.output_queue:
823 self.connection.send(self.output_queue[0])
824 except BrokenPipeError:
825 if self.account and self.account.get("name"):
826 account = self.account.get("name")
828 account = "an unknown user"
829 self.state = "disconnecting"
830 log("Broken pipe sending to %s." % account, 7)
831 del self.output_queue[0]
833 def enqueue_input(self):
834 """Process and enqueue any new input."""
836 # check for some input
838 raw_input = self.connection.recv(1024)
839 except (BlockingIOError, OSError):
845 # tack this on to any previous partial
846 self.partial_input += raw_input
848 # reply to and remove any IAC negotiation codes
849 mudpy.telnet.negotiate_telnet_options(self)
851 # separate multiple input lines
852 new_input_lines = self.partial_input.split(b"\n")
854 # if input doesn't end in a newline, replace the
855 # held partial input with the last line of it
856 if not self.partial_input.endswith(b"\n"):
857 self.partial_input = new_input_lines.pop()
859 # otherwise, chop off the extra null input and reset
860 # the held partial input
862 new_input_lines.pop()
863 self.partial_input = b""
865 # iterate over the remaining lines
866 for line in new_input_lines:
868 # strip off extra whitespace
871 # log non-printable characters remaining
872 if mudpy.telnet.is_enabled(self, mudpy.telnet.TELOPT_BINARY,
874 asciiline = bytes([x for x in line if 32 <= x <= 126])
875 if line != asciiline:
876 logline = "Non-ASCII characters from "
877 if self.account and self.account.get("name"):
878 logline += self.account.get("name") + ": "
880 logline += "unknown user: "
881 logline += repr(line)
886 line = line.decode("utf-8")
887 except UnicodeDecodeError:
888 logline = "Non-UTF-8 characters from "
889 if self.account and self.account.get("name"):
890 logline += self.account.get("name") + ": "
892 logline += "unknown user: "
893 logline += repr(line)
897 line = unicodedata.normalize("NFKC", line)
899 # put on the end of the queue
900 self.input_queue.append(line)
902 def new_avatar(self):
903 """Instantiate a new, unconfigured avatar for this user."""
905 while "avatar:" + self.account.get("name") + ":" + str(
907 ) in universe.categories["actor"].keys():
909 self.avatar = Element(
910 "actor:avatar:" + self.account.get("name") + ":" + str(
913 universe, old_style=True
915 self.avatar.append("inherit", "archetype:avatar")
916 self.account.append("avatars", self.avatar.key)
918 def delete_avatar(self, avatar):
919 """Remove an avatar from the world and from the user's list."""
920 if self.avatar is universe.contents[avatar]:
922 universe.contents[avatar].destroy()
923 avatars = self.account.get("avatars")
924 avatars.remove(avatar)
925 self.account.set("avatars", avatars)
927 def activate_avatar_by_index(self, index):
928 """Enter the world with a particular indexed avatar."""
929 self.avatar = universe.contents[
930 self.account.get("avatars")[index]]
931 self.avatar.owner = self
932 self.state = "active"
933 self.avatar.go_home()
935 def deactivate_avatar(self):
936 """Have the active avatar leave the world."""
938 current = self.avatar.get("location")
940 self.avatar.set("default_location", current)
941 self.avatar.echo_to_location(
942 "You suddenly wonder where " + self.avatar.get(
946 del universe.contents[current].contents[self.avatar.key]
947 self.avatar.remove_facet("location")
948 self.avatar.owner = None
952 """Destroy the user and associated avatars."""
953 for avatar in self.account.get("avatars"):
954 self.delete_avatar(avatar)
955 self.account.destroy()
957 def list_avatar_names(self):
958 """List names of assigned avatars."""
960 for avatar in self.account.get("avatars"):
962 avatars.append(universe.contents[avatar].get("name"))
964 log('Missing avatar "%s", possible data corruption.' %
969 def broadcast(message, add_prompt=True):
970 """Send a message to all connected users."""
971 for each_user in universe.userlist:
972 each_user.send("$(eol)" + message, add_prompt=add_prompt)
975 def log(message, level=0):
978 # a couple references we need
979 if "mudpy.log" in universe.contents:
980 file_name = universe.contents["mudpy.log"].get("file", "")
981 max_log_lines = universe.contents["mudpy.log"].get("lines", 0)
982 syslog_name = universe.contents["mudpy.log"].get("syslog", "")
987 timestamp = time.asctime()[4:19]
989 # turn the message into a list of nonempty lines
990 lines = [x for x in [(x.rstrip()) for x in message.split("\n")] if x != ""]
992 # send the timestamp and line to a file
994 if not os.path.isabs(file_name):
995 file_name = os.path.join(universe.startdir, file_name)
996 file_descriptor = codecs.open(file_name, "a", "utf-8")
998 file_descriptor.write(timestamp + " " + line + "\n")
999 file_descriptor.flush()
1000 file_descriptor.close()
1002 # send the timestamp and line to standard output
1003 if ("mudpy.log" in universe.contents and
1004 universe.contents["mudpy.log"].get("stdout")):
1006 print(timestamp + " " + line)
1008 # send the line to the system log
1011 syslog_name.encode("utf-8"),
1013 syslog.LOG_INFO | syslog.LOG_DAEMON
1019 # display to connected administrators
1020 for user in universe.userlist:
1021 if user.state == "active" and user.account.get(
1023 ) and user.account.get("loglevel", 0) <= level:
1024 # iterate over every line in the message
1028 "$(bld)$(red)" + timestamp + " "
1029 + line.replace("$(", "$_(") + "$(nrm)$(eol)")
1030 user.send(full_message, flush=True)
1032 # add to the recent log list
1034 while 0 < len(universe.loglines) >= max_log_lines:
1035 del universe.loglines[0]
1036 universe.loglines.append((level, timestamp + " " + line))
1039 def get_loglines(level, start, stop):
1040 """Return a specific range of loglines filtered by level."""
1042 # filter the log lines
1043 loglines = [x for x in universe.loglines if x[0] >= level]
1045 # we need these in several places
1046 total_count = str(len(universe.loglines))
1047 filtered_count = len(loglines)
1049 # don't proceed if there are no lines
1052 # can't start before the begining or at the end
1053 if start > filtered_count:
1054 start = filtered_count
1058 # can't stop before we start
1065 message = "There are " + str(total_count)
1066 message += " log lines in memory and " + str(filtered_count)
1067 message += " at or above level " + str(level) + "."
1068 message += " The matching lines from " + str(stop) + " to "
1069 message += str(start) + " are:$(eol)$(eol)"
1071 # add the text from the selected lines
1073 range_lines = loglines[-start:-(stop - 1)]
1075 range_lines = loglines[-start:]
1076 for line in range_lines:
1077 message += " (" + str(line[0]) + ") " + line[1].replace(
1081 # there were no lines
1083 message = "None of the " + str(total_count)
1084 message += " lines in memory matches your request."
1090 def glyph_columns(character):
1091 """Convenience function to return the column width of a glyph."""
1092 if unicodedata.east_asian_width(character) in "FW":
1098 def wrap_ansi_text(text, width):
1099 """Wrap text with arbitrary width while ignoring ANSI colors."""
1101 # the current position in the entire text string, including all
1102 # characters, printable or otherwise
1105 # the current text position relative to the begining of the line,
1106 # ignoring color escape sequences
1109 # the absolute position of the most recent whitespace character
1112 # whether the current character is part of a color escape sequence
1115 # normalize any potentially composited unicode before we count it
1116 text = unicodedata.normalize("NFKC", text)
1118 # iterate over each character from the begining of the text
1119 for each_character in text:
1121 # the current character is the escape character
1122 if each_character == "\x1b" and not escape:
1125 # the current character is within an escape sequence
1128 # the current character is m, which terminates the
1130 if each_character == "m":
1133 # the current character is a newline, so reset the relative
1134 # position (start a new line)
1135 elif each_character == "\n":
1137 last_whitespace = abs_pos
1139 # the current character meets the requested maximum line width,
1140 # so we need to backtrack and find a space at which to wrap;
1141 # special care is taken to avoid an off-by-one in case the
1142 # current character is a double-width glyph
1143 elif each_character != "\r" and (
1144 rel_pos >= width or (
1145 rel_pos >= width - 1 and glyph_columns(
1151 # it's always possible we landed on whitespace
1152 if unicodedata.category(each_character) in ("Cc", "Zs"):
1153 last_whitespace = abs_pos
1155 # insert an eol in place of the space
1156 text = text[:last_whitespace] + "\r\n" + text[last_whitespace + 1:]
1158 # increase the absolute position because an eol is two
1159 # characters but the space it replaced was only one
1162 # now we're at the begining of a new line, plus the
1163 # number of characters wrapped from the previous line
1165 for remaining_characters in text[last_whitespace:abs_pos]:
1166 rel_pos += glyph_columns(remaining_characters)
1168 # as long as the character is not a carriage return and the
1169 # other above conditions haven't been met, count it as a
1170 # printable character
1171 elif each_character != "\r":
1172 rel_pos += glyph_columns(each_character)
1173 if unicodedata.category(each_character) in ("Cc", "Zs"):
1174 last_whitespace = abs_pos
1176 # increase the absolute position for every character
1179 # return the newly-wrapped text
1183 def weighted_choice(data):
1184 """Takes a dict weighted by value and returns a random key."""
1186 # this will hold our expanded list of keys from the data
1189 # create the expanded list of keys
1190 for key in data.keys():
1191 for count in range(data[key]):
1192 expanded.append(key)
1194 # return one at random
1195 return random.choice(expanded)
1199 """Returns a random character name."""
1201 # the vowels and consonants needed to create romaji syllables
1230 # this dict will hold our weighted list of syllables
1233 # generate the list with an even weighting
1234 for consonant in consonants:
1235 for vowel in vowels:
1236 syllables[consonant + vowel] = 1
1238 # we'll build the name into this string
1241 # create a name of random length from the syllables
1242 for syllable in range(random.randrange(2, 6)):
1243 name += weighted_choice(syllables)
1245 # strip any leading quotemark, capitalize and return the name
1246 return name.strip("'").capitalize()
1249 def replace_macros(user, text, is_input=False):
1250 """Replaces macros in text output."""
1252 # third person pronouns
1254 "female": {"obj": "her", "pos": "hers", "sub": "she"},
1255 "male": {"obj": "him", "pos": "his", "sub": "he"},
1256 "neuter": {"obj": "it", "pos": "its", "sub": "it"}
1259 # a dict of replacement macros
1262 "bld": chr(27) + "[1m",
1263 "nrm": chr(27) + "[0m",
1264 "blk": chr(27) + "[30m",
1265 "blu": chr(27) + "[34m",
1266 "cyn": chr(27) + "[36m",
1267 "grn": chr(27) + "[32m",
1268 "mgt": chr(27) + "[35m",
1269 "red": chr(27) + "[31m",
1270 "yel": chr(27) + "[33m",
1273 # add dynamic macros where possible
1275 account_name = user.account.get("name")
1277 macros["account"] = account_name
1279 avatar_gender = user.avatar.get("gender")
1281 macros["tpop"] = pronouns[avatar_gender]["obj"]
1282 macros["tppp"] = pronouns[avatar_gender]["pos"]
1283 macros["tpsp"] = pronouns[avatar_gender]["sub"]
1288 # find and replace per the macros dict
1289 macro_start = text.find("$(")
1290 if macro_start == -1:
1292 macro_end = text.find(")", macro_start) + 1
1293 macro = text[macro_start + 2:macro_end - 1]
1294 if macro in macros.keys():
1295 replacement = macros[macro]
1297 # this is how we handle local file inclusion (dangerous!)
1298 elif macro.startswith("inc:"):
1299 incfile = mudpy.data.find_file(macro[4:], universe=universe)
1300 if os.path.exists(incfile):
1301 incfd = codecs.open(incfile, "r", "utf-8")
1304 if line.endswith("\n") and not line.endswith("\r\n"):
1305 line = line.replace("\n", "\r\n")
1307 # lose the trailing eol
1308 replacement = replacement[:-2]
1311 log("Couldn't read included " + incfile + " file.", 6)
1313 # if we get here, log and replace it with null
1317 log("Unexpected replacement macro " +
1318 macro + " encountered.", 6)
1320 # and now we act on the replacement
1321 text = text.replace("$(" + macro + ")", replacement)
1323 # replace the look-like-a-macro sequence
1324 text = text.replace("$_(", "$(")
1329 def escape_macros(value):
1330 """Escapes replacement macros in text."""
1331 if type(value) is str:
1332 return value.replace("$(", "$_(")
1337 def first_word(text, separator=" "):
1338 """Returns a tuple of the first word and the rest."""
1340 if text.find(separator) > 0:
1341 return text.split(separator, 1)
1349 """The things which should happen on each pulse, aside from reloads."""
1351 # open the listening socket if it hasn't been already
1352 if not hasattr(universe, "listening_socket"):
1353 universe.initialize_server_socket()
1355 # assign a user if a new connection is waiting
1356 user = check_for_connection(universe.listening_socket)
1358 universe.userlist.append(user)
1360 # iterate over the connected users
1361 for user in universe.userlist:
1364 # add an element for counters if it doesn't exist
1365 if "counters" not in universe.categories["internal"]:
1366 universe.categories["internal"]["counters"] = Element(
1367 "internal:counters", universe, old_style=True
1370 # update the log every now and then
1371 if not universe.categories["internal"]["counters"].get("mark"):
1372 log(str(len(universe.userlist)) + " connection(s)")
1373 universe.categories["internal"]["counters"].set(
1374 "mark", universe.categories["internal"]["time"].get(
1379 universe.categories["internal"]["counters"].set(
1380 "mark", universe.categories["internal"]["counters"].get(
1385 # periodically save everything
1386 if not universe.categories["internal"]["counters"].get("save"):
1388 universe.categories["internal"]["counters"].set(
1389 "save", universe.categories["internal"]["time"].get(
1394 universe.categories["internal"]["counters"].set(
1395 "save", universe.categories["internal"]["counters"].get(
1400 # pause for a configurable amount of time (decimal seconds)
1401 time.sleep(universe.categories["internal"]
1402 ["time"].get("increment"))
1404 # increase the elapsed increment counter
1405 universe.categories["internal"]["counters"].set(
1406 "elapsed", universe.categories["internal"]["counters"].get(
1413 """Reload all relevant objects."""
1414 for user in universe.userlist[:]:
1416 for element in universe.contents.values():
1417 if element.origin.is_writeable():
1422 def check_for_connection(listening_socket):
1423 """Check for a waiting connection and return a new user object."""
1425 # try to accept a new connection
1427 connection, address = listening_socket.accept()
1428 except BlockingIOError:
1431 # note that we got one
1432 log("Connection from " + address[0], 2)
1434 # disable blocking so we can proceed whether or not we can send/receive
1435 connection.setblocking(0)
1437 # create a new user object
1440 # associate this connection with it
1441 user.connection = connection
1443 # set the user's ipa from the connection's ipa
1444 user.address = address[0]
1446 # let the client know we WILL EOR (RFC 885)
1447 mudpy.telnet.enable(user, mudpy.telnet.TELOPT_EOR, mudpy.telnet.US)
1448 user.negotiation_pause = 2
1450 # return the new user object
1454 def get_menu(state, error=None, choices=None):
1455 """Show the correct menu text to a user."""
1457 # make sure we don't reuse a mutable sequence by default
1461 # get the description or error text
1462 message = get_menu_description(state, error)
1464 # get menu choices for the current state
1465 message += get_formatted_menu_choices(state, choices)
1467 # try to get a prompt, if it was defined
1468 message += get_menu_prompt(state)
1470 # throw in the default choice, if it exists
1471 message += get_formatted_default_menu_choice(state)
1473 # display a message indicating if echo is off
1474 message += get_echo_message(state)
1476 # return the assembly of various strings defined above
1480 def menu_echo_on(state):
1481 """True if echo is on, false if it is off."""
1482 return universe.categories["menu"][state].get("echo", True)
1485 def get_echo_message(state):
1486 """Return a message indicating that echo is off."""
1487 if menu_echo_on(state):
1490 return "(won't echo) "
1493 def get_default_menu_choice(state):
1494 """Return the default choice for a menu."""
1495 return universe.categories["menu"][state].get("default")
1498 def get_formatted_default_menu_choice(state):
1499 """Default menu choice foratted for inclusion in a prompt string."""
1500 default_choice = get_default_menu_choice(state)
1502 return "[$(red)" + default_choice + "$(nrm)] "
1507 def get_menu_description(state, error):
1508 """Get the description or error text."""
1510 # an error condition was raised by the handler
1513 # try to get an error message matching the condition
1515 description = universe.categories[
1516 "menu"][state].get("error_" + error)
1518 description = "That is not a valid choice..."
1519 description = "$(red)" + description + "$(nrm)"
1521 # there was no error condition
1524 # try to get a menu description for the current state
1525 description = universe.categories["menu"][state].get("description")
1527 # return the description or error message
1529 description += "$(eol)$(eol)"
1533 def get_menu_prompt(state):
1534 """Try to get a prompt, if it was defined."""
1535 prompt = universe.categories["menu"][state].get("prompt")
1541 def get_menu_choices(user):
1542 """Return a dict of choice:meaning."""
1543 menu = universe.categories["menu"][user.state]
1544 create_choices = menu.get("create")
1546 choices = eval(create_choices)
1552 for facet in menu.facets():
1553 if facet.startswith("demand_") and not eval(
1554 universe.categories["menu"][user.state].get(facet)
1556 ignores.append(facet.split("_", 2)[1])
1557 elif facet.startswith("create_"):
1558 creates[facet] = facet.split("_", 2)[1]
1559 elif facet.startswith("choice_"):
1560 options[facet] = facet.split("_", 2)[1]
1561 for facet in creates.keys():
1562 if not creates[facet] in ignores:
1563 choices[creates[facet]] = eval(menu.get(facet))
1564 for facet in options.keys():
1565 if not options[facet] in ignores:
1566 choices[options[facet]] = menu.get(facet)
1570 def get_formatted_menu_choices(state, choices):
1571 """Returns a formatted string of menu choices."""
1573 choice_keys = list(choices.keys())
1575 for choice in choice_keys:
1576 choice_output += " [$(red)" + choice + "$(nrm)] " + choices[
1580 choice_output += "$(eol)"
1581 return choice_output
1584 def get_menu_branches(state):
1585 """Return a dict of choice:branch."""
1587 for facet in universe.categories["menu"][state].facets():
1588 if facet.startswith("branch_"):
1590 facet.split("_", 2)[1]
1591 ] = universe.categories["menu"][state].get(facet)
1595 def get_default_branch(state):
1596 """Return the default branch."""
1597 return universe.categories["menu"][state].get("branch")
1600 def get_choice_branch(user, choice):
1601 """Returns the new state matching the given choice."""
1602 branches = get_menu_branches(user.state)
1603 if choice in branches.keys():
1604 return branches[choice]
1605 elif choice in user.menu_choices.keys():
1606 return get_default_branch(user.state)
1611 def get_menu_actions(state):
1612 """Return a dict of choice:branch."""
1614 for facet in universe.categories["menu"][state].facets():
1615 if facet.startswith("action_"):
1617 facet.split("_", 2)[1]
1618 ] = universe.categories["menu"][state].get(facet)
1622 def get_default_action(state):
1623 """Return the default action."""
1624 return universe.categories["menu"][state].get("action")
1627 def get_choice_action(user, choice):
1628 """Run any indicated script for the given choice."""
1629 actions = get_menu_actions(user.state)
1630 if choice in actions.keys():
1631 return actions[choice]
1632 elif choice in user.menu_choices.keys():
1633 return get_default_action(user.state)
1638 def handle_user_input(user):
1639 """The main handler, branches to a state-specific handler."""
1641 # if the user's client echo is off, send a blank line for aesthetics
1642 if mudpy.telnet.is_enabled(user, mudpy.telnet.TELOPT_ECHO,
1644 user.send("", add_prompt=False, prepend_padding=False)
1646 # check to make sure the state is expected, then call that handler
1647 if "handler_" + user.state in globals():
1648 exec("handler_" + user.state + "(user)")
1650 generic_menu_handler(user)
1652 # since we got input, flag that the menu/prompt needs to be redisplayed
1653 user.menu_seen = False
1655 # update the last_input timestamp while we're at it
1656 user.last_input = universe.get_time()
1659 def generic_menu_handler(user):
1660 """A generic menu choice handler."""
1662 # get a lower-case representation of the next line of input
1663 if user.input_queue:
1664 choice = user.input_queue.pop(0)
1666 choice = choice.lower()
1670 choice = get_default_menu_choice(user.state)
1671 if choice in user.menu_choices:
1672 exec(get_choice_action(user, choice))
1673 new_state = get_choice_branch(user, choice)
1675 user.state = new_state
1677 user.error = "default"
1680 def handler_entering_account_name(user):
1681 """Handle the login account name."""
1683 # get the next waiting line of input
1684 input_data = user.input_queue.pop(0)
1686 # did the user enter anything?
1689 # keep only the first word and convert to lower-case
1690 name = input_data.lower()
1692 # fail if there are non-alphanumeric characters
1693 if name != "".join(filter(
1694 lambda x: x >= "0" and x <= "9" or x >= "a" and x <= "z",
1696 user.error = "bad_name"
1698 # if that account exists, time to request a password
1699 elif name in universe.categories["account"]:
1700 user.account = universe.categories["account"][name]
1701 user.state = "checking_password"
1703 # otherwise, this could be a brand new user
1705 user.account = Element("account:" + name, universe, old_style=True)
1706 user.account.set("name", name)
1707 log("New user: " + name, 2)
1708 user.state = "checking_new_account_name"
1710 # if the user entered nothing for a name, then buhbye
1712 user.state = "disconnecting"
1715 def handler_checking_password(user):
1716 """Handle the login account password."""
1718 # get the next waiting line of input
1719 input_data = user.input_queue.pop(0)
1721 if "mudpy.limit" in universe.contents:
1722 max_password_tries = universe.contents["mudpy.limit"].get(
1723 "password_tries", 3)
1725 max_password_tries = 3
1727 # does the hashed input equal the stored hash?
1728 if mudpy.password.verify(input_data, user.account.get("passhash")):
1730 # if so, set the username and load from cold storage
1731 if not user.replace_old_connections():
1733 user.state = "main_utility"
1735 # if at first your hashes don't match, try, try again
1736 elif user.password_tries < max_password_tries - 1:
1737 user.password_tries += 1
1738 user.error = "incorrect"
1740 # we've exceeded the maximum number of password failures, so disconnect
1743 "$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)"
1745 user.state = "disconnecting"
1748 def handler_entering_new_password(user):
1749 """Handle a new password entry."""
1751 # get the next waiting line of input
1752 input_data = user.input_queue.pop(0)
1754 if "mudpy.limit" in universe.contents:
1755 max_password_tries = universe.contents["mudpy.limit"].get(
1756 "password_tries", 3)
1758 max_password_tries = 3
1760 # make sure the password is strong--at least one upper, one lower and
1761 # one digit, seven or more characters in length
1762 if len(input_data) > 6 and len(
1763 list(filter(lambda x: x >= "0" and x <= "9", input_data))
1765 list(filter(lambda x: x >= "A" and x <= "Z", input_data))
1767 list(filter(lambda x: x >= "a" and x <= "z", input_data))
1770 # hash and store it, then move on to verification
1771 user.account.set("passhash", mudpy.password.create(input_data))
1772 user.state = "verifying_new_password"
1774 # the password was weak, try again if you haven't tried too many times
1775 elif user.password_tries < max_password_tries - 1:
1776 user.password_tries += 1
1779 # too many tries, so adios
1782 "$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)"
1784 user.account.destroy()
1785 user.state = "disconnecting"
1788 def handler_verifying_new_password(user):
1789 """Handle the re-entered new password for verification."""
1791 # get the next waiting line of input
1792 input_data = user.input_queue.pop(0)
1794 if "mudpy.limit" in universe.contents:
1795 max_password_tries = universe.contents["mudpy.limit"].get(
1796 "password_tries", 3)
1798 max_password_tries = 3
1800 # hash the input and match it to storage
1801 if mudpy.password.verify(input_data, user.account.get("passhash")):
1804 # the hashes matched, so go active
1805 if not user.replace_old_connections():
1806 user.state = "main_utility"
1808 # go back to entering the new password as long as you haven't tried
1810 elif user.password_tries < max_password_tries - 1:
1811 user.password_tries += 1
1812 user.error = "differs"
1813 user.state = "entering_new_password"
1815 # otherwise, sayonara
1818 "$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)"
1820 user.account.destroy()
1821 user.state = "disconnecting"
1824 def handler_active(user):
1825 """Handle input for active users."""
1827 # get the next waiting line of input
1828 input_data = user.input_queue.pop(0)
1833 # split out the command and parameters
1835 mode = actor.get("mode")
1836 if mode and input_data.startswith("!"):
1837 command_name, parameters = first_word(input_data[1:])
1838 elif mode == "chat":
1839 command_name = "say"
1840 parameters = input_data
1842 command_name, parameters = first_word(input_data)
1844 # lowercase the command
1845 command_name = command_name.lower()
1847 # the command matches a command word for which we have data
1848 if command_name in universe.categories["command"]:
1849 command = universe.categories["command"][command_name]
1853 # if it's allowed, do it
1854 if actor.can_run(command):
1855 exec(command.get("action"))
1857 # otherwise, give an error
1859 command_error(actor, input_data)
1861 # if no input, just idle back with a prompt
1863 user.send("", just_prompt=True)
1866 def command_halt(actor, parameters):
1867 """Halt the world."""
1870 # see if there's a message or use a generic one
1872 message = "Halting: " + parameters
1874 message = "User " + actor.owner.account.get(
1876 ) + " halted the world."
1879 broadcast(message, add_prompt=False)
1882 # set a flag to terminate the world
1883 universe.terminate_flag = True
1886 def command_reload(actor):
1887 """Reload all code modules, configs and data."""
1890 # let the user know and log
1891 actor.send("Reloading all code modules, configs and data.")
1894 actor.owner.account.get("name") + " reloaded the world.",
1898 # set a flag to reload
1899 universe.reload_flag = True
1902 def command_quit(actor):
1903 """Leave the world and go back to the main menu."""
1905 actor.owner.state = "main_utility"
1906 actor.owner.deactivate_avatar()
1909 def command_help(actor, parameters):
1910 """List available commands and provide help for commands."""
1912 # did the user ask for help on a specific command word?
1913 if parameters and actor.owner:
1915 # is the command word one for which we have data?
1916 if parameters in universe.categories["command"]:
1917 command = universe.categories["command"][parameters]
1921 # only for allowed commands
1922 if actor.can_run(command):
1924 # add a description if provided
1925 description = command.get("description")
1927 description = "(no short description provided)"
1928 if command.get("administrative"):
1932 output += parameters + "$(nrm) - " + description + "$(eol)$(eol)"
1934 # add the help text if provided
1935 help_text = command.get("help")
1937 help_text = "No help is provided for this command."
1940 # list related commands
1941 see_also = command.get("see_also")
1943 really_see_also = ""
1944 for item in see_also:
1945 if item in universe.categories["command"]:
1946 command = universe.categories["command"][item]
1947 if actor.can_run(command):
1949 really_see_also += ", "
1950 if command.get("administrative"):
1951 really_see_also += "$(red)"
1953 really_see_also += "$(grn)"
1954 really_see_also += item + "$(nrm)"
1956 output += "$(eol)$(eol)See also: " + really_see_also
1958 # no data for the requested command word
1960 output = "That is not an available command."
1962 # no specific command word was indicated
1965 # give a sorted list of commands with descriptions if provided
1966 output = "These are the commands available to you:$(eol)$(eol)"
1967 sorted_commands = list(universe.categories["command"].keys())
1968 sorted_commands.sort()
1969 for item in sorted_commands:
1970 command = universe.categories["command"][item]
1971 if actor.can_run(command):
1972 description = command.get("description")
1974 description = "(no short description provided)"
1975 if command.get("administrative"):
1979 output += item + "$(nrm) - " + description + "$(eol)"
1980 output += ('$(eol)Enter "help COMMAND" for help on a command '
1983 # send the accumulated output to the user
1987 def command_move(actor, parameters):
1988 """Move the avatar in a given direction."""
1989 if parameters in universe.contents[actor.get("location")].portals():
1990 actor.move_direction(parameters)
1992 actor.send("You cannot go that way.")
1995 def command_look(actor, parameters):
1998 actor.send("You can't look at or in anything yet.")
2000 actor.look_at(actor.get("location"))
2003 def command_say(actor, parameters):
2004 """Speak to others in the same area."""
2006 # check for replacement macros and escape them
2007 parameters = escape_macros(parameters)
2009 # if the message is wrapped in quotes, remove them and leave contents
2011 if parameters.startswith('"') and parameters.endswith('"'):
2012 message = parameters[1:-1]
2015 # otherwise, get rid of stray quote marks on the ends of the message
2017 message = parameters.strip('''"'`''')
2020 # the user entered a message
2023 # match the punctuation used, if any, to an action
2024 if "mudpy.linguistic" in universe.contents:
2025 actions = universe.contents["mudpy.linguistic"].get("actions", {})
2026 default_punctuation = (universe.contents["mudpy.linguistic"].get(
2027 "default_punctuation", "."))
2030 default_punctuation = "."
2033 # reverse sort punctuation options so the longest match wins
2034 for mark in sorted(actions.keys(), reverse=True):
2035 if not literal and message.endswith(mark):
2036 action = actions[mark]
2039 # add punctuation if needed
2041 action = actions[default_punctuation]
2042 if message and not (
2043 literal or unicodedata.category(message[-1]) == "Po"
2045 message += default_punctuation
2047 # failsafe checks to avoid unwanted reformatting and null strings
2048 if message and not literal:
2050 # decapitalize the first letter to improve matching
2051 message = message[0].lower() + message[1:]
2053 # iterate over all words in message, replacing typos
2054 if "mudpy.linguistic" in universe.contents:
2055 typos = universe.contents["mudpy.linguistic"].get("typos", {})
2058 words = message.split()
2059 for index in range(len(words)):
2061 while unicodedata.category(word[0]) == "Po":
2063 while unicodedata.category(word[-1]) == "Po":
2065 if word in typos.keys():
2066 words[index] = words[index].replace(word, typos[word])
2067 message = " ".join(words)
2069 # capitalize the first letter
2070 message = message[0].upper() + message[1:]
2074 actor.echo_to_location(
2075 actor.get("name") + " " + action + 's, "' + message + '"'
2077 actor.send("You " + action + ', "' + message + '"')
2079 # there was no message
2081 actor.send("What do you want to say?")
2084 def command_chat(actor):
2085 """Toggle chat mode."""
2086 mode = actor.get("mode")
2088 actor.set("mode", "chat")
2089 actor.send("Entering chat mode (use $(grn)!chat$(nrm) to exit).")
2090 elif mode == "chat":
2091 actor.remove_facet("mode")
2092 actor.send("Exiting chat mode.")
2094 actor.send("Sorry, but you're already busy with something else!")
2097 def command_show(actor, parameters):
2098 """Show program data."""
2100 arguments = parameters.split()
2102 message = "What do you want to show?"
2103 elif arguments[0] == "time":
2104 message = universe.categories["internal"]["counters"].get(
2106 ) + " increments elapsed since the world was created."
2107 elif arguments[0] == "categories":
2108 message = "These are the element categories:$(eol)"
2109 categories = list(universe.categories.keys())
2111 for category in categories:
2112 message += "$(eol) $(grn)" + category + "$(nrm)"
2113 elif arguments[0] == "files":
2114 message = "These are the current files containing the universe:$(eol)"
2115 filenames = list(universe.files.keys())
2117 for filename in filenames:
2118 if universe.files[filename].is_writeable():
2122 message += ("$(eol) $(red)(" + status + ") $(grn)" + filename
2124 elif arguments[0] == "category":
2125 if len(arguments) != 2:
2126 message = "You must specify one category."
2127 elif arguments[1] in universe.categories:
2128 message = ('These are the elements in the "' + arguments[1]
2129 + '" category:$(eol)')
2132 universe.categories[arguments[1]][x].key
2133 ) for x in universe.categories[arguments[1]].keys()
2136 for element in elements:
2137 message += "$(eol) $(grn)" + element + "$(nrm)"
2139 message = 'Category "' + arguments[1] + '" does not exist.'
2140 elif arguments[0] == "file":
2141 if len(arguments) != 2:
2142 message = "You must specify one file."
2143 elif arguments[1] in universe.files:
2144 message = ('These are the elements in the "' + arguments[1]
2146 elements = universe.files[arguments[1]].data.keys()
2148 for element in elements:
2149 message += "$(eol) $(grn)" + element + "$(nrm)"
2151 message = 'Category "' + arguments[1] + '" does not exist.'
2152 elif arguments[0] == "element":
2153 if len(arguments) != 2:
2154 message = "You must specify one element."
2155 elif arguments[1].strip(".") in universe.contents:
2156 element = universe.contents[arguments[1].strip(".")]
2157 message = ('These are the properties of the "' + arguments[1]
2158 + '" element (in "' + element.origin.filename
2160 facets = element.facets()
2161 for facet in sorted(facets):
2162 if element.old_style:
2163 message += ("$(eol) $(grn)%s: $(red)%s$(nrm)" %
2164 (facet, escape_macros(element.get(facet))))
2166 message += ("$(eol) $(grn)%s: $(red)%s$(nrm)" %
2167 (facet, str(facets[facet])))
2169 message = 'Element "' + arguments[1] + '" does not exist.'
2170 elif arguments[0] == "result":
2171 if len(arguments) < 2:
2172 message = "You need to specify an expression."
2175 message = repr(eval(" ".join(arguments[1:])))
2176 except Exception as e:
2177 message = ("$(red)Your expression raised an exception...$(eol)"
2178 "$(eol)$(bld)%s$(nrm)" % e)
2179 elif arguments[0] == "log":
2180 if len(arguments) == 4:
2181 if re.match(r"^\d+$", arguments[3]) and int(arguments[3]) >= 0:
2182 stop = int(arguments[3])
2187 if len(arguments) >= 3:
2188 if re.match(r"^\d+$", arguments[2]) and int(arguments[2]) > 0:
2189 start = int(arguments[2])
2194 if len(arguments) >= 2:
2195 if (re.match(r"^\d+$", arguments[1])
2196 and 0 <= int(arguments[1]) <= 9):
2197 level = int(arguments[1])
2200 elif 0 <= actor.owner.account.get("loglevel", 0) <= 9:
2201 level = actor.owner.account.get("loglevel", 0)
2204 if level > -1 and start > -1 and stop > -1:
2205 message = get_loglines(level, start, stop)
2207 message = ("When specified, level must be 0-9 (default 1), "
2208 "start and stop must be >=1 (default 10 and 1).")
2210 message = '''I don't know what "''' + parameters + '" is.'
2214 def command_create(actor, parameters):
2215 """Create an element if it does not exist."""
2217 message = "You must at least specify an element to create."
2218 elif not actor.owner:
2221 arguments = parameters.split()
2222 if len(arguments) == 1:
2223 arguments.append("")
2224 if len(arguments) == 2:
2225 element, filename = arguments
2226 if element in universe.contents:
2227 message = 'The "' + element + '" element already exists.'
2229 message = ('You create "' +
2230 element + '" within the universe.')
2231 logline = actor.owner.account.get(
2233 ) + " created an element: " + element
2235 logline += " in file " + filename
2236 if filename not in universe.files:
2238 ' Warning: "' + filename + '" is not yet '
2239 "included in any other file and will not be read "
2240 "on startup unless this is remedied.")
2241 Element(element, universe, filename, old_style=True)
2243 elif len(arguments) > 2:
2244 message = "You can only specify an element and a filename."
2248 def command_destroy(actor, parameters):
2249 """Destroy an element if it exists."""
2252 message = "You must specify an element to destroy."
2254 if parameters not in universe.contents:
2255 message = 'The "' + parameters + '" element does not exist.'
2257 universe.contents[parameters].destroy()
2258 message = ('You destroy "' + parameters
2259 + '" within the universe.')
2261 actor.owner.account.get(
2263 ) + " destroyed an element: " + parameters,
2269 def command_set(actor, parameters):
2270 """Set a facet of an element."""
2272 message = "You must specify an element, a facet and a value."
2274 arguments = parameters.split(" ", 2)
2275 if len(arguments) == 1:
2276 message = ('What facet of element "' + arguments[0]
2277 + '" would you like to set?')
2278 elif len(arguments) == 2:
2279 message = ('What value would you like to set for the "' +
2280 arguments[1] + '" facet of the "' + arguments[0]
2283 element, facet, value = arguments
2284 if element not in universe.contents:
2285 message = 'The "' + element + '" element does not exist.'
2288 universe.contents[element].set(facet, value)
2290 message = ('Value "%s" of type "%s" cannot be coerced '
2291 'to the correct datatype for facet "%s".' %
2292 (value, type(value), facet))
2294 message = ('You have successfully (re)set the "' + facet
2295 + '" facet of element "' + element
2296 + '". Try "show element ' +
2297 element + '" for verification.')
2301 def command_delete(actor, parameters):
2302 """Delete a facet from an element."""
2304 message = "You must specify an element and a facet."
2306 arguments = parameters.split(" ")
2307 if len(arguments) == 1:
2308 message = ('What facet of element "' + arguments[0]
2309 + '" would you like to delete?')
2310 elif len(arguments) != 2:
2311 message = "You may only specify an element and a facet."
2313 element, facet = arguments
2314 if element not in universe.contents:
2315 message = 'The "' + element + '" element does not exist.'
2316 elif facet not in universe.contents[element].facets():
2317 message = ('The "' + element + '" element has no "' + facet
2320 universe.contents[element].remove_facet(facet)
2321 message = ('You have successfully deleted the "' + facet
2322 + '" facet of element "' + element
2323 + '". Try "show element ' +
2324 element + '" for verification.')
2328 def command_error(actor, input_data):
2329 """Generic error for an unrecognized command word."""
2331 # 90% of the time use a generic error
2332 if random.randrange(10):
2333 message = '''I'm not sure what "''' + input_data + '''" means...'''
2335 # 10% of the time use the classic diku error
2337 message = "Arglebargle, glop-glyf!?!"
2339 # send the error message
2343 def daemonize(universe):
2344 """Fork and disassociate from everything."""
2346 # only if this is what we're configured to do
2347 if "mudpy.process" in universe.contents and universe.contents[
2348 "mudpy.process"].get("daemon"):
2350 # log before we start forking around, so the terminal gets the message
2351 log("Disassociating from the controlling terminal.")
2353 # fork off and die, so we free up the controlling terminal
2357 # switch to a new process group
2360 # fork some more, this time to free us from the old process group
2364 # reset the working directory so we don't needlessly tie up mounts
2367 # clear the file creation mask so we can bend it to our will later
2370 # redirect stdin/stdout/stderr and close off their former descriptors
2371 for stdpipe in range(3):
2373 sys.stdin = codecs.open("/dev/null", "r", "utf-8")
2374 sys.__stdin__ = codecs.open("/dev/null", "r", "utf-8")
2375 sys.stdout = codecs.open("/dev/null", "w", "utf-8")
2376 sys.stderr = codecs.open("/dev/null", "w", "utf-8")
2377 sys.__stdout__ = codecs.open("/dev/null", "w", "utf-8")
2378 sys.__stderr__ = codecs.open("/dev/null", "w", "utf-8")
2381 def create_pidfile(universe):
2382 """Write a file containing the current process ID."""
2383 pid = str(os.getpid())
2384 log("Process ID: " + pid)
2385 if "mudpy.process" in universe.contents:
2386 file_name = universe.contents["mudpy.process"].get("pidfile", "")
2390 if not os.path.isabs(file_name):
2391 file_name = os.path.join(universe.startdir, file_name)
2392 file_descriptor = codecs.open(file_name, "w", "utf-8")
2393 file_descriptor.write(pid + "\n")
2394 file_descriptor.flush()
2395 file_descriptor.close()
2398 def remove_pidfile(universe):
2399 """Remove the file containing the current process ID."""
2400 if "mudpy.process" in universe.contents:
2401 file_name = universe.contents["mudpy.process"].get("pidfile", "")
2405 if not os.path.isabs(file_name):
2406 file_name = os.path.join(universe.startdir, file_name)
2407 if os.access(file_name, os.W_OK):
2408 os.remove(file_name)
2411 def excepthook(excepttype, value, tracebackdata):
2412 """Handle uncaught exceptions."""
2414 # assemble the list of errors into a single string
2416 traceback.format_exception(excepttype, value, tracebackdata)
2419 # try to log it, if possible
2422 except Exception as e:
2423 # try to write it to stderr, if possible
2424 sys.stderr.write(message + "\nException while logging...\n%s" % e)
2427 def sighook(what, where):
2428 """Handle external signals."""
2431 message = "Caught signal: "
2433 # for a hangup signal
2434 if what == signal.SIGHUP:
2435 message += "hangup (reloading)"
2436 universe.reload_flag = True
2438 # for a terminate signal
2439 elif what == signal.SIGTERM:
2440 message += "terminate (halting)"
2441 universe.terminate_flag = True
2443 # catchall for unexpected signals
2445 message += str(what) + " (unhandled)"
2451 def override_excepthook():
2452 """Redefine sys.excepthook with our own."""
2453 sys.excepthook = excepthook
2456 def assign_sighook():
2457 """Assign a customized handler for some signals."""
2458 signal.signal(signal.SIGHUP, sighook)
2459 signal.signal(signal.SIGTERM, sighook)
2463 """This contains functions to be performed when starting the engine."""
2465 # see if a configuration file was specified
2466 if len(sys.argv) > 1:
2467 conffile = sys.argv[1]
2473 universe = Universe(conffile, True)
2475 # report any loglines which accumulated during setup
2476 for logline in universe.setup_loglines:
2478 universe.setup_loglines = []
2480 # log an initial message
2481 log("Started mudpy with command line: " + " ".join(sys.argv))
2483 # fork and disassociate
2486 # override the default exception handler so we get logging first thing
2487 override_excepthook()
2489 # set up custom signal handlers
2493 create_pidfile(universe)
2495 # pass the initialized universe back
2500 """These are functions performed when shutting down the engine."""
2502 # the loop has terminated, so save persistent data
2505 # log a final message
2506 log("Shutting down now.")
2508 # get rid of the pidfile
2509 remove_pidfile(universe)