1 # -*- coding: utf-8 -*-
2 """Miscellaneous functions for the mudpy engine."""
4 # Copyright (c) 2004-2013 Jeremy Stanley <fungi@yuggoth.org>. Permission
5 # to use, copy, modify, and distribute this software is granted under
6 # terms provided in the LICENSE file distributed with this software.
11 """An element of the universe."""
13 def __init__(self, key, universe, filename=None):
14 """Set up a new element."""
18 # keep track of our key name
21 # keep track of what universe it's loading into
22 self.universe = universe
24 # clone attributes if this is replacing another element
25 if self.key in self.universe.contents:
26 old_element = self.universe.contents[self.key]
27 for attribute in vars(old_element).keys():
28 exec("self." + attribute + " = old_element." + attribute)
30 self.owner.avatar = self
32 # i guess this is a new element then
35 # not owned by a user by default (used for avatars)
38 # no contents in here by default
41 # parse out appropriate category and subkey names, add to list
42 if self.key.find(":") > 0:
43 self.category, self.subkey = self.key.split(":", 1)
45 self.category = "other"
46 self.subkey = self.key
47 if self.category not in self.universe.categories:
48 self.category = "other"
49 self.subkey = self.key
51 # get an appropriate filename for the origin
53 filename = self.universe.default_origins[self.category]
54 if not os.path.isabs(filename):
55 filename = os.path.abspath(filename)
57 # add the file if it doesn't exist yet
58 if filename not in self.universe.files:
59 mudpy.data.DataFile(filename, self.universe)
61 # record or reset a pointer to the origin file
62 self.origin = self.universe.files[filename]
64 # add a data section to the origin if necessary
65 if not self.origin.data.has_section(self.key):
66 self.origin.data.add_section(self.key)
68 # add or replace this element in the universe
69 self.universe.contents[self.key] = self
70 self.universe.categories[self.category][self.subkey] = self
73 """Create a new element and replace this one."""
74 new_element = Element(self.key, self.universe, self.origin.filename)
78 """Remove an element from the universe and destroy it."""
79 self.origin.data.remove_section(self.key)
80 del self.universe.categories[self.category][self.subkey]
81 del self.universe.contents[self.key]
85 """Return a list of non-inherited facets for this element."""
86 if self.key in self.origin.data.sections():
87 return self.origin.data.options(self.key)
91 def has_facet(self, facet):
92 """Return whether the non-inherited facet exists."""
93 return facet in self.facets()
95 def remove_facet(self, facet):
96 """Remove a facet from the element."""
97 if self.has_facet(facet):
98 self.origin.data.remove_option(self.key, facet)
99 self.origin.modified = True
102 """Return a list of the element's inheritance lineage."""
103 if self.has_facet("inherit"):
104 ancestry = self.getlist("inherit")
105 for parent in ancestry[:]:
106 ancestors = self.universe.contents[parent].ancestry()
107 for ancestor in ancestors:
108 if ancestor not in ancestry:
109 ancestry.append(ancestor)
114 def get(self, facet, default=None):
115 """Retrieve values."""
118 if self.origin.data.has_option(self.key, facet):
119 raw_data = self.origin.data.get(self.key, facet)
120 if raw_data.startswith("u\"") or raw_data.startswith("u'"):
121 raw_data = raw_data[1:]
122 raw_data.strip("\"'")
124 elif self.has_facet("inherit"):
125 for ancestor in self.ancestry():
126 if self.universe.contents[ancestor].has_facet(facet):
127 return self.universe.contents[ancestor].get(facet)
131 def getboolean(self, facet, default=None):
132 """Retrieve values as boolean type."""
135 if self.origin.data.has_option(self.key, facet):
136 return self.origin.data.getboolean(self.key, facet)
137 elif self.has_facet("inherit"):
138 for ancestor in self.ancestry():
139 if self.universe.contents[ancestor].has_facet(facet):
140 return self.universe.contents[ancestor].getboolean(facet)
144 def getint(self, facet, default=None):
145 """Return values as int type."""
148 if self.origin.data.has_option(self.key, facet):
149 return self.origin.data.getint(self.key, facet)
150 elif self.has_facet("inherit"):
151 for ancestor in self.ancestry():
152 if self.universe.contents[ancestor].has_facet(facet):
153 return self.universe.contents[ancestor].getint(facet)
157 def getfloat(self, facet, default=None):
158 """Return values as float type."""
161 if self.origin.data.has_option(self.key, facet):
162 return self.origin.data.getfloat(self.key, facet)
163 elif self.has_facet("inherit"):
164 for ancestor in self.ancestry():
165 if self.universe.contents[ancestor].has_facet(facet):
166 return self.universe.contents[ancestor].getfloat(facet)
170 def getlist(self, facet, default=None):
171 """Return values as list type."""
175 value = self.get(facet)
177 return mudpy.data.makelist(value)
181 def getdict(self, facet, default=None):
182 """Return values as dict type."""
186 value = self.get(facet)
188 return mudpy.data.makedict(value)
192 def set(self, facet, value):
194 if not self.has_facet(facet) or not self.get(facet) == value:
195 # TODO: remove this check after the switch to py3k
196 if repr(type(value)) == "<type 'unicode'>":
198 if not type(value) is str:
200 self.origin.data.set(self.key, facet, value)
201 self.origin.modified = True
203 def append(self, facet, value):
204 """Append value to a list."""
205 if not type(value) is str:
207 newlist = self.getlist(facet)
208 newlist.append(value)
209 self.set(facet, newlist)
211 def new_event(self, action, when=None):
212 """Create, attach and enqueue an event element."""
214 # if when isn't specified, that means now
216 when = self.universe.get_time()
218 # events are elements themselves
219 event = Element("event:" + self.key + ":" + counter)
229 add_terminator=False,
232 """Convenience method to pass messages to an owner."""
245 def can_run(self, command):
246 """Check if the user can run this command object."""
248 # has to be in the commands category
249 if command not in self.universe.categories["command"].values():
252 # avatars of administrators can run any command
253 elif self.owner and self.owner.account.getboolean("administrator"):
256 # everyone can run non-administrative commands
257 elif not command.getboolean("administrative"):
260 # otherwise the command cannot be run by this actor
264 # pass back the result
267 def update_location(self):
268 """Make sure the location's contents contain this element."""
269 location = self.get("location")
270 if location in self.universe.contents:
271 self.universe.contents[location].contents[self.key] = self
273 def clean_contents(self):
274 """Make sure the element's contents aren't bogus."""
275 for element in self.contents.values():
276 if element.get("location") != self.key:
277 del self.contents[element.key]
279 def go_to(self, location):
280 """Relocate the element to a specific location."""
281 current = self.get("location")
282 if current and self.key in self.universe.contents[current].contents:
283 del universe.contents[current].contents[self.key]
284 if location in self.universe.contents:
285 self.set("location", location)
286 self.universe.contents[location].contents[self.key] = self
287 self.look_at(location)
290 """Relocate the element to its default location."""
291 self.go_to(self.get("default_location"))
292 self.echo_to_location(
293 "You suddenly realize that " + self.get("name") + " is here."
296 def move_direction(self, direction):
297 """Relocate the element in a specified direction."""
298 self.echo_to_location(
301 ) + " exits " + self.universe.categories[
312 "You exit " + self.universe.categories[
324 self.universe.contents[
325 self.get("location")].link_neighbor(direction)
327 self.echo_to_location(
330 ) + " arrives from " + self.universe.categories[
341 def look_at(self, key):
342 """Show an element to another element."""
344 element = self.universe.contents[key]
346 name = element.get("name")
348 message += "$(cyn)" + name + "$(nrm)$(eol)"
349 description = element.get("description")
351 message += description + "$(eol)"
352 portal_list = list(element.portals().keys())
355 message += "$(cyn)[ Exits: " + ", ".join(
358 for element in self.universe.contents[
361 if element.getboolean("is_actor") and element is not self:
362 message += "$(yel)" + element.get(
364 ) + " is here.$(nrm)$(eol)"
365 elif element is not self:
366 message += "$(grn)" + element.get(
372 """Map the portal directions for a room to neighbors."""
375 if re.match("""^location:-?\d+,-?\d+,-?\d+$""", self.key):
376 coordinates = [(int(x))
377 for x in self.key.split(":")[1].split(",")]
378 directions = self.universe.categories["internal"]["directions"]
382 x, directions.getdict(x)["vector"]
383 ) for x in directions.facets()
386 for portal in self.getlist("gridlinks"):
387 adjacent = map(lambda c, o: c + o,
388 coordinates, offsets[portal])
389 neighbor = "location:" + ",".join(
390 [(str(x)) for x in adjacent]
392 if neighbor in self.universe.contents:
393 portals[portal] = neighbor
394 for facet in self.facets():
395 if facet.startswith("link_"):
396 neighbor = self.get(facet)
397 if neighbor in self.universe.contents:
398 portal = facet.split("_")[1]
399 portals[portal] = neighbor
402 def link_neighbor(self, direction):
403 """Return the element linked in a given direction."""
404 portals = self.portals()
405 if direction in portals:
406 return portals[direction]
408 def echo_to_location(self, message):
409 """Show a message to other elements in the current location."""
410 for element in self.universe.contents[
413 if element is not self:
414 element.send(message)
421 def __init__(self, filename="", load=False):
422 """Initialize the universe."""
427 self.default_origins = {}
429 self.pending_events_long = {}
430 self.pending_events_short = {}
431 self.private_files = []
432 self.reload_flag = False
433 self.startdir = os.getcwd()
434 self.terminate_flag = False
437 possible_filenames = [
443 "/usr/local/mudpy/mudpy.conf",
444 "/usr/local/mudpy/etc/mudpy.conf",
445 "/etc/mudpy/mudpy.conf",
448 for filename in possible_filenames:
449 if os.access(filename, os.R_OK):
451 if not os.path.isabs(filename):
452 filename = os.path.join(self.startdir, filename)
453 self.filename = filename
458 """Load universe data from persistent storage."""
461 # the files dict must exist and filename needs to be read-only
465 self.filename in self.files and self.files[
470 # clear out all read-only files
471 if hasattr(self, "files"):
472 for data_filename in self.files.keys():
473 if not self.files[data_filename].is_writeable():
474 del self.files[data_filename]
476 # start loading from the initial file
477 mudpy.data.DataFile(self.filename, self)
479 # make a list of inactive avatars
480 inactive_avatars = []
481 for account in self.categories["account"].values():
482 inactive_avatars += [
483 (self.contents[x]) for x in account.getlist("avatars")
485 for user in self.userlist:
486 if user.avatar in inactive_avatars:
487 inactive_avatars.remove(user.avatar)
489 # go through all elements to clear out inactive avatar locations
490 for element in self.contents.values():
491 location = element.get("location")
492 if element in inactive_avatars and location:
493 if location in self.contents and element.key in self.contents[
496 del self.contents[location].contents[element.key]
497 element.set("default_location", location)
498 element.remove_facet("location")
500 # another pass to straighten out all the element contents
501 for element in self.contents.values():
502 element.update_location()
503 element.clean_contents()
506 """Create a new, empty Universe (the Big Bang)."""
507 new_universe = Universe()
508 for attribute in vars(self).keys():
509 exec("new_universe." + attribute + " = self." + attribute)
510 new_universe.reload_flag = False
515 """Save the universe to persistent storage."""
516 for key in self.files:
517 self.files[key].save()
519 def initialize_server_socket(self):
520 """Create and open the listening socket."""
523 # need to know the local address and port number for the listener
524 host = self.categories["internal"]["network"].get("host")
525 port = self.categories["internal"]["network"].getint("port")
527 # if no host was specified, bind to all local addresses (preferring
535 # figure out if this is ipv4 or v6
536 family = socket.getaddrinfo(host, port)[0][0]
537 if family is socket.AF_INET6 and not socket.has_ipv6:
538 log("No support for IPv6 address %s (use IPv4 instead)." % host)
540 # create a new stream-type socket object
541 self.listening_socket = socket.socket(family, socket.SOCK_STREAM)
543 # set the socket options to allow existing open ones to be
544 # reused (fixes a bug where the server can't bind for a minute
545 # when restarting on linux systems)
546 self.listening_socket.setsockopt(
547 socket.SOL_SOCKET, socket.SO_REUSEADDR, 1
550 # bind the socket to to our desired server ipa and port
551 self.listening_socket.bind((host, port))
553 # disable blocking so we can proceed whether or not we can
555 self.listening_socket.setblocking(0)
557 # start listening on the socket
558 self.listening_socket.listen(1)
560 # note that we're now ready for user connections
562 "Listening for Telnet connections on: " +
563 host + ":" + str(port)
567 """Convenience method to get the elapsed time counter."""
568 return self.categories["internal"]["counters"].getint("elapsed")
573 """This is a connected user."""
576 """Default values for the in-memory user variables."""
580 self.authenticated = False
583 self.connection = None
585 self.input_queue = []
586 self.last_address = ""
587 self.last_input = universe.get_time()
588 self.menu_choices = {}
589 self.menu_seen = False
590 self.negotiation_pause = 0
591 self.output_queue = []
592 self.partial_input = b""
593 self.password_tries = 0
594 self.state = "initial"
598 """Log, close the connection and remove."""
600 name = self.account.get("name")
604 message = "User " + name
606 message = "An unnamed user"
607 message += " logged out."
609 self.deactivate_avatar()
610 self.connection.close()
613 def check_idle(self):
614 """Warn or disconnect idle users as appropriate."""
615 idletime = universe.get_time() - self.last_input
616 linkdead_dict = universe.categories["internal"]["time"].getdict(
619 if self.state in linkdead_dict:
620 linkdead_state = self.state
622 linkdead_state = "default"
623 if idletime > linkdead_dict[linkdead_state]:
625 "$(eol)$(red)You've done nothing for far too long... goodbye!"
630 logline = "Disconnecting "
631 if self.account and self.account.get("name"):
632 logline += self.account.get("name")
634 logline += "an unknown user"
635 logline += " after idling too long in the " + \
636 self.state + " state."
638 self.state = "disconnecting"
639 self.menu_seen = False
640 idle_dict = universe.categories["internal"]["time"].getdict("idle")
641 if self.state in idle_dict:
642 idle_state = self.state
644 idle_state = "default"
645 if idletime == idle_dict[idle_state]:
647 "$(eol)$(red)If you continue to be unproductive, "
648 + "you'll be shown the door...$(nrm)$(eol)"
652 """Save, load a new user and relocate the connection."""
654 # get out of the list
657 # create a new user object
660 # set everything equivalent
661 for attribute in vars(self).keys():
662 exec("new_user." + attribute + " = self." + attribute)
664 # the avatar needs a new owner
666 new_user.avatar.owner = new_user
669 universe.userlist.append(new_user)
671 # get rid of the old user object
674 def replace_old_connections(self):
675 """Disconnect active users with the same name."""
677 # the default return value
680 # iterate over each user in the list
681 for old_user in universe.userlist:
683 # the name is the same but it's not us
686 ) and old_user.account and old_user.account.get(
688 ) == self.account.get(
690 ) and old_user is not self:
694 "User " + self.account.get(
696 ) + " reconnected--closing old connection to "
697 + old_user.address + ".",
701 "$(eol)$(red)New connection from " + self.address
702 + ". Terminating old connection...$(nrm)$(eol)",
707 # close the old connection
708 old_user.connection.close()
710 # replace the old connection with this one
712 "$(eol)$(red)Taking over old connection from "
713 + old_user.address + ".$(nrm)"
715 old_user.connection = self.connection
716 old_user.last_address = old_user.address
717 old_user.address = self.address
719 # take this one out of the list and delete
725 # true if an old connection was replaced, false if not
728 def authenticate(self):
729 """Flag the user as authenticated and disconnect duplicates."""
730 if self.state is not "authenticated":
731 log("User " + self.account.get("name") + " logged in.", 2)
732 self.authenticated = True
733 if self.account.subkey in universe.categories[
740 self.account.set("administrator", "True")
743 """Send the user their current menu."""
744 if not self.menu_seen:
745 self.menu_choices = get_menu_choices(self)
747 get_menu(self.state, self.error, self.menu_choices),
751 self.menu_seen = True
753 self.adjust_echoing()
755 def adjust_echoing(self):
756 """Adjust echoing to match state menu requirements."""
758 if mudpy.telnet.is_enabled(self, mudpy.telnet.TELOPT_ECHO,
760 if menu_echo_on(self.state):
761 mudpy.telnet.disable(self, mudpy.telnet.TELOPT_ECHO,
763 elif not menu_echo_on(self.state):
764 mudpy.telnet.enable(self, mudpy.telnet.TELOPT_ECHO,
768 """Remove a user from the list of connected users."""
769 universe.userlist.remove(self)
779 add_terminator=False,
782 """Send arbitrary text to a connected user."""
785 # unless raw mode is on, clean it up all nice and pretty
788 # strip extra $(eol) off if present
789 while output.startswith("$(eol)"):
791 while output.endswith("$(eol)"):
793 extra_lines = output.find("$(eol)$(eol)$(eol)")
794 while extra_lines > -1:
795 output = output[:extra_lines] + output[extra_lines + 6:]
796 extra_lines = output.find("$(eol)$(eol)$(eol)")
798 # start with a newline, append the message, then end
799 # with the optional eol string passed to this function
800 # and the ansi escape to return to normal text
801 if not just_prompt and prepend_padding:
802 if not self.output_queue \
803 or not self.output_queue[-1].endswith("\r\n"):
804 output = "$(eol)" + output
805 elif not self.output_queue[-1].endswith(
807 ) and not self.output_queue[-1].endswith(
810 output = "$(eol)" + output
811 output += eol + chr(27) + "[0m"
813 # tack on a prompt if active
814 if self.state == "active":
819 mode = self.avatar.get("mode")
821 output += "(" + mode + ") "
823 # find and replace macros in the output
824 output = replace_macros(self, output)
826 # wrap the text at the client's width (min 40, 0 disables)
828 if self.columns < 40:
832 output = wrap_ansi_text(output, wrap)
834 # if supported by the client, encode it utf-8
835 if mudpy.telnet.is_enabled(self, mudpy.telnet.TELOPT_BINARY,
837 encoded_output = output.encode("utf-8")
839 # otherwise just send ascii
841 encoded_output = output.encode("ascii", "replace")
843 # end with a terminator if requested
844 if add_prompt or add_terminator:
845 if mudpy.telnet.is_enabled(
846 self, mudpy.telnet.TELOPT_EOR, mudpy.telnet.US):
847 encoded_output += mudpy.telnet.telnet_proto(
848 mudpy.telnet.IAC, mudpy.telnet.EOR)
849 elif not mudpy.telnet.is_enabled(
850 self, mudpy.telnet.TELOPT_SGA, mudpy.telnet.US):
851 encoded_output += mudpy.telnet.telnet_proto(
852 mudpy.telnet.IAC, mudpy.telnet.GA)
854 # and tack it onto the queue
855 self.output_queue.append(encoded_output)
857 # if this is urgent, flush all pending output
861 # just dump raw bytes as requested
863 self.output_queue.append(output)
867 """All the things to do to the user per increment."""
869 # if the world is terminating, disconnect
870 if universe.terminate_flag:
871 self.state = "disconnecting"
872 self.menu_seen = False
874 # check for an idle connection and act appropriately
878 # if output is paused, decrement the counter
879 if self.state == "initial":
880 if self.negotiation_pause:
881 self.negotiation_pause -= 1
883 self.state = "entering_account_name"
885 # show the user a menu as needed
886 elif not self.state == "active":
889 # flush any pending output in the queue
892 # disconnect users with the appropriate state
893 if self.state == "disconnecting":
896 # check for input and add it to the queue
899 # there is input waiting in the queue
901 handle_user_input(self)
904 """Try to send the last item in the queue and remove it."""
905 if self.output_queue:
907 self.connection.send(self.output_queue[0])
908 del self.output_queue[0]
910 if self.account and self.account.get("name"):
911 account = self.account.get("name")
913 account = "an unknown user"
914 log("Sending to %s raised an exception (broken pipe?)."
918 def enqueue_input(self):
919 """Process and enqueue any new input."""
923 # check for some input
925 raw_input = self.connection.recv(1024)
932 # tack this on to any previous partial
933 self.partial_input += raw_input
935 # reply to and remove any IAC negotiation codes
936 mudpy.telnet.negotiate_telnet_options(self)
938 # separate multiple input lines
939 new_input_lines = self.partial_input.split(b"\n")
941 # if input doesn't end in a newline, replace the
942 # held partial input with the last line of it
943 if not self.partial_input.endswith(b"\n"):
944 self.partial_input = new_input_lines.pop()
946 # otherwise, chop off the extra null input and reset
947 # the held partial input
949 new_input_lines.pop()
950 self.partial_input = b""
952 # iterate over the remaining lines
953 for line in new_input_lines:
955 # strip off extra whitespace
958 # log non-printable characters remaining
959 if mudpy.telnet.is_enabled(self, mudpy.telnet.TELOPT_BINARY,
961 asciiline = b"".join(
962 filter(lambda x: b" " <= x <= b"~", line))
963 if line != asciiline:
964 logline = "Non-ASCII characters from "
965 if self.account and self.account.get("name"):
966 logline += self.account.get("name") + ": "
968 logline += "unknown user: "
969 logline += repr(line)
974 line = line.decode("utf-8")
975 except UnicodeDecodeError:
976 logline = "Non-UTF-8 characters from "
977 if self.account and self.account.get("name"):
978 logline += self.account.get("name") + ": "
980 logline += "unknown user: "
981 logline += repr(line)
985 line = unicodedata.normalize("NFKC", line)
987 # put on the end of the queue
988 self.input_queue.append(line)
990 def new_avatar(self):
991 """Instantiate a new, unconfigured avatar for this user."""
993 while "avatar:" + self.account.get("name") + ":" + str(
995 ) in universe.categories["actor"].keys():
997 self.avatar = Element(
998 "actor:avatar:" + self.account.get("name") + ":" + str(
1003 self.avatar.append("inherit", "archetype:avatar")
1004 self.account.append("avatars", self.avatar.key)
1006 def delete_avatar(self, avatar):
1007 """Remove an avatar from the world and from the user's list."""
1008 if self.avatar is universe.contents[avatar]:
1010 universe.contents[avatar].destroy()
1011 avatars = self.account.getlist("avatars")
1012 avatars.remove(avatar)
1013 self.account.set("avatars", avatars)
1015 def activate_avatar_by_index(self, index):
1016 """Enter the world with a particular indexed avatar."""
1017 self.avatar = universe.contents[
1018 self.account.getlist("avatars")[index]]
1019 self.avatar.owner = self
1020 self.state = "active"
1021 self.avatar.go_home()
1023 def deactivate_avatar(self):
1024 """Have the active avatar leave the world."""
1026 current = self.avatar.get("location")
1028 self.avatar.set("default_location", current)
1029 self.avatar.echo_to_location(
1030 "You suddenly wonder where " + self.avatar.get(
1034 del universe.contents[current].contents[self.avatar.key]
1035 self.avatar.remove_facet("location")
1036 self.avatar.owner = None
1040 """Destroy the user and associated avatars."""
1041 for avatar in self.account.getlist("avatars"):
1042 self.delete_avatar(avatar)
1043 self.account.destroy()
1045 def list_avatar_names(self):
1046 """List names of assigned avatars."""
1048 universe.contents[avatar].get(
1050 ) for avatar in self.account.getlist("avatars")
1054 def broadcast(message, add_prompt=True):
1055 """Send a message to all connected users."""
1056 for each_user in universe.userlist:
1057 each_user.send("$(eol)" + message, add_prompt=add_prompt)
1060 def log(message, level=0):
1061 """Log a message."""
1067 # a couple references we need
1068 file_name = universe.categories["internal"]["logging"].get("file")
1069 max_log_lines = universe.categories["internal"]["logging"].getint(
1072 syslog_name = universe.categories["internal"]["logging"].get("syslog")
1073 timestamp = time.asctime()[4:19]
1075 # turn the message into a list of lines
1077 lambda x: x != "", [(x.rstrip()) for x in message.split("\n")]
1080 # send the timestamp and line to a file
1082 if not os.path.isabs(file_name):
1083 file_name = os.path.join(universe.startdir, file_name)
1084 file_descriptor = codecs.open(file_name, "a", "utf-8")
1086 file_descriptor.write(timestamp + " " + line + "\n")
1087 file_descriptor.flush()
1088 file_descriptor.close()
1090 # send the timestamp and line to standard output
1091 if universe.categories["internal"]["logging"].getboolean("stdout"):
1093 print(timestamp + " " + line)
1095 # send the line to the system log
1098 syslog_name.encode("utf-8"),
1100 syslog.LOG_INFO | syslog.LOG_DAEMON
1106 # display to connected administrators
1107 for user in universe.userlist:
1108 if user.state == "active" and user.account.getboolean(
1110 ) and user.account.getint("loglevel") <= level:
1111 # iterate over every line in the message
1115 "$(bld)$(red)" + timestamp + " "
1116 + line.replace("$(", "$_(") + "$(nrm)$(eol)")
1117 user.send(full_message, flush=True)
1119 # add to the recent log list
1121 while 0 < len(universe.loglines) >= max_log_lines:
1122 del universe.loglines[0]
1123 universe.loglines.append((level, timestamp + " " + line))
1126 def get_loglines(level, start, stop):
1127 """Return a specific range of loglines filtered by level."""
1129 # filter the log lines
1130 loglines = filter(lambda x: x[0] >= level, universe.loglines)
1132 # we need these in several places
1133 total_count = str(len(universe.loglines))
1134 filtered_count = len(loglines)
1136 # don't proceed if there are no lines
1139 # can't start before the begining or at the end
1140 if start > filtered_count:
1141 start = filtered_count
1145 # can't stop before we start
1152 message = "There are " + str(total_count)
1153 message += " log lines in memory and " + str(filtered_count)
1154 message += " at or above level " + str(level) + "."
1155 message += " The matching lines from " + str(stop) + " to "
1156 message += str(start) + " are:$(eol)$(eol)"
1158 # add the text from the selected lines
1160 range_lines = loglines[-start:-(stop - 1)]
1162 range_lines = loglines[-start:]
1163 for line in range_lines:
1164 message += " (" + str(line[0]) + ") " + line[1].replace(
1168 # there were no lines
1170 message = "None of the " + str(total_count)
1171 message += " lines in memory matches your request."
1177 def glyph_columns(character):
1178 """Convenience function to return the column width of a glyph."""
1180 if unicodedata.east_asian_width(character) in "FW":
1186 def wrap_ansi_text(text, width):
1187 """Wrap text with arbitrary width while ignoring ANSI colors."""
1190 # the current position in the entire text string, including all
1191 # characters, printable or otherwise
1194 # the current text position relative to the begining of the line,
1195 # ignoring color escape sequences
1198 # the absolute position of the most recent whitespace character
1201 # whether the current character is part of a color escape sequence
1204 # normalize any potentially composited unicode before we count it
1205 text = unicodedata.normalize("NFKC", text)
1207 # iterate over each character from the begining of the text
1208 for each_character in text:
1210 # the current character is the escape character
1211 if each_character == "\x1b" and not escape:
1214 # the current character is within an escape sequence
1217 # the current character is m, which terminates the
1219 if each_character == "m":
1222 # the current character is a newline, so reset the relative
1223 # position (start a new line)
1224 elif each_character == "\n":
1226 last_whitespace = abs_pos
1228 # the current character meets the requested maximum line width,
1229 # so we need to backtrack and find a space at which to wrap;
1230 # special care is taken to avoid an off-by-one in case the
1231 # current character is a double-width glyph
1232 elif each_character != "\r" and (
1233 rel_pos >= width or (
1234 rel_pos >= width - 1 and glyph_columns(
1240 # it's always possible we landed on whitespace
1241 if unicodedata.category(each_character) in ("Cc", "Zs"):
1242 last_whitespace = abs_pos
1244 # insert an eol in place of the space
1245 text = text[:last_whitespace] + \
1246 "\r\n" + text[last_whitespace + 1:]
1248 # increase the absolute position because an eol is two
1249 # characters but the space it replaced was only one
1252 # now we're at the begining of a new line, plus the
1253 # number of characters wrapped from the previous line
1255 for remaining_characters in text[last_whitespace:abs_pos]:
1256 rel_pos += glyph_columns(remaining_characters)
1258 # as long as the character is not a carriage return and the
1259 # other above conditions haven't been met, count it as a
1260 # printable character
1261 elif each_character != "\r":
1262 rel_pos += glyph_columns(each_character)
1263 if unicodedata.category(each_character) in ("Cc", "Zs"):
1264 last_whitespace = abs_pos
1266 # increase the absolute position for every character
1269 # return the newly-wrapped text
1273 def weighted_choice(data):
1274 """Takes a dict weighted by value and returns a random key."""
1277 # this will hold our expanded list of keys from the data
1280 # create the expanded list of keys
1281 for key in data.keys():
1282 for count in range(data[key]):
1283 expanded.append(key)
1285 # return one at random
1286 return random.choice(expanded)
1290 """Returns a random character name."""
1293 # the vowels and consonants needed to create romaji syllables
1322 # this dict will hold our weighted list of syllables
1325 # generate the list with an even weighting
1326 for consonant in consonants:
1327 for vowel in vowels:
1328 syllables[consonant + vowel] = 1
1330 # we'll build the name into this string
1333 # create a name of random length from the syllables
1334 for syllable in range(random.randrange(2, 6)):
1335 name += weighted_choice(syllables)
1337 # strip any leading quotemark, capitalize and return the name
1338 return name.strip("'").capitalize()
1341 def replace_macros(user, text, is_input=False):
1342 """Replaces macros in text output."""
1347 # third person pronouns
1349 "female": {"obj": "her", "pos": "hers", "sub": "she"},
1350 "male": {"obj": "him", "pos": "his", "sub": "he"},
1351 "neuter": {"obj": "it", "pos": "its", "sub": "it"}
1354 # a dict of replacement macros
1357 "bld": chr(27) + "[1m",
1358 "nrm": chr(27) + "[0m",
1359 "blk": chr(27) + "[30m",
1360 "blu": chr(27) + "[34m",
1361 "cyn": chr(27) + "[36m",
1362 "grn": chr(27) + "[32m",
1363 "mgt": chr(27) + "[35m",
1364 "red": chr(27) + "[31m",
1365 "yel": chr(27) + "[33m",
1368 # add dynamic macros where possible
1370 account_name = user.account.get("name")
1372 macros["account"] = account_name
1374 avatar_gender = user.avatar.get("gender")
1376 macros["tpop"] = pronouns[avatar_gender]["obj"]
1377 macros["tppp"] = pronouns[avatar_gender]["pos"]
1378 macros["tpsp"] = pronouns[avatar_gender]["sub"]
1383 # find and replace per the macros dict
1384 macro_start = text.find("$(")
1385 if macro_start == -1:
1387 macro_end = text.find(")", macro_start) + 1
1388 macro = text[macro_start + 2:macro_end - 1]
1389 if macro in macros.keys():
1390 replacement = macros[macro]
1392 # this is how we handle local file inclusion (dangerous!)
1393 elif macro.startswith("inc:"):
1394 incfile = mudpy.data.find_file(macro[4:], universe=universe)
1395 if os.path.exists(incfile):
1396 incfd = codecs.open(incfile, "r", "utf-8")
1399 if line.endswith("\n") and not line.endswith("\r\n"):
1400 line = line.replace("\n", "\r\n")
1402 # lose the trailing eol
1403 replacement = replacement[:-2]
1406 log("Couldn't read included " + incfile + " file.", 6)
1408 # if we get here, log and replace it with null
1412 log("Unexpected replacement macro " +
1413 macro + " encountered.", 6)
1415 # and now we act on the replacement
1416 text = text.replace("$(" + macro + ")", replacement)
1418 # replace the look-like-a-macro sequence
1419 text = text.replace("$_(", "$(")
1424 def escape_macros(text):
1425 """Escapes replacement macros in text."""
1426 return text.replace("$(", "$_(")
1429 def first_word(text, separator=" "):
1430 """Returns a tuple of the first word and the rest."""
1432 if text.find(separator) > 0:
1433 return text.split(separator, 1)
1441 """The things which should happen on each pulse, aside from reloads."""
1444 # open the listening socket if it hasn't been already
1445 if not hasattr(universe, "listening_socket"):
1446 universe.initialize_server_socket()
1448 # assign a user if a new connection is waiting
1449 user = check_for_connection(universe.listening_socket)
1451 universe.userlist.append(user)
1453 # iterate over the connected users
1454 for user in universe.userlist:
1457 # add an element for counters if it doesn't exist
1458 if "counters" not in universe.categories["internal"]:
1459 universe.categories["internal"]["counters"] = Element(
1460 "internal:counters", universe
1463 # update the log every now and then
1464 if not universe.categories["internal"]["counters"].getint("mark"):
1465 log(str(len(universe.userlist)) + " connection(s)")
1466 universe.categories["internal"]["counters"].set(
1467 "mark", universe.categories["internal"]["time"].getint(
1472 universe.categories["internal"]["counters"].set(
1473 "mark", universe.categories["internal"]["counters"].getint(
1478 # periodically save everything
1479 if not universe.categories["internal"]["counters"].getint("save"):
1481 universe.categories["internal"]["counters"].set(
1482 "save", universe.categories["internal"]["time"].getint(
1487 universe.categories["internal"]["counters"].set(
1488 "save", universe.categories["internal"]["counters"].getint(
1493 # pause for a configurable amount of time (decimal seconds)
1494 time.sleep(universe.categories["internal"]
1495 ["time"].getfloat("increment"))
1497 # increase the elapsed increment counter
1498 universe.categories["internal"]["counters"].set(
1499 "elapsed", universe.categories["internal"]["counters"].getint(
1506 """Reload all relevant objects."""
1507 for user in universe.userlist[:]:
1509 for element in universe.contents.values():
1510 if element.origin.is_writeable():
1515 def check_for_connection(listening_socket):
1516 """Check for a waiting connection and return a new user object."""
1519 # try to accept a new connection
1521 connection, address = listening_socket.accept()
1525 # note that we got one
1526 log("Connection from " + address[0], 2)
1528 # disable blocking so we can proceed whether or not we can send/receive
1529 connection.setblocking(0)
1531 # create a new user object
1534 # associate this connection with it
1535 user.connection = connection
1537 # set the user's ipa from the connection's ipa
1538 user.address = address[0]
1540 # let the client know we WILL EOR (RFC 885)
1541 mudpy.telnet.enable(user, mudpy.telnet.TELOPT_EOR, mudpy.telnet.US)
1542 user.negotiation_pause = 2
1544 # return the new user object
1548 def get_menu(state, error=None, choices=None):
1549 """Show the correct menu text to a user."""
1551 # make sure we don't reuse a mutable sequence by default
1555 # get the description or error text
1556 message = get_menu_description(state, error)
1558 # get menu choices for the current state
1559 message += get_formatted_menu_choices(state, choices)
1561 # try to get a prompt, if it was defined
1562 message += get_menu_prompt(state)
1564 # throw in the default choice, if it exists
1565 message += get_formatted_default_menu_choice(state)
1567 # display a message indicating if echo is off
1568 message += get_echo_message(state)
1570 # return the assembly of various strings defined above
1574 def menu_echo_on(state):
1575 """True if echo is on, false if it is off."""
1576 return universe.categories["menu"][state].getboolean("echo", True)
1579 def get_echo_message(state):
1580 """Return a message indicating that echo is off."""
1581 if menu_echo_on(state):
1584 return "(won't echo) "
1587 def get_default_menu_choice(state):
1588 """Return the default choice for a menu."""
1589 return universe.categories["menu"][state].get("default")
1592 def get_formatted_default_menu_choice(state):
1593 """Default menu choice foratted for inclusion in a prompt string."""
1594 default_choice = get_default_menu_choice(state)
1596 return "[$(red)" + default_choice + "$(nrm)] "
1601 def get_menu_description(state, error):
1602 """Get the description or error text."""
1604 # an error condition was raised by the handler
1607 # try to get an error message matching the condition
1609 description = universe.categories[
1610 "menu"][state].get("error_" + error)
1612 description = "That is not a valid choice..."
1613 description = "$(red)" + description + "$(nrm)"
1615 # there was no error condition
1618 # try to get a menu description for the current state
1619 description = universe.categories["menu"][state].get("description")
1621 # return the description or error message
1623 description += "$(eol)$(eol)"
1627 def get_menu_prompt(state):
1628 """Try to get a prompt, if it was defined."""
1629 prompt = universe.categories["menu"][state].get("prompt")
1635 def get_menu_choices(user):
1636 """Return a dict of choice:meaning."""
1637 menu = universe.categories["menu"][user.state]
1638 create_choices = menu.get("create")
1640 choices = eval(create_choices)
1646 for facet in menu.facets():
1647 if facet.startswith("demand_") and not eval(
1648 universe.categories["menu"][user.state].get(facet)
1650 ignores.append(facet.split("_", 2)[1])
1651 elif facet.startswith("create_"):
1652 creates[facet] = facet.split("_", 2)[1]
1653 elif facet.startswith("choice_"):
1654 options[facet] = facet.split("_", 2)[1]
1655 for facet in creates.keys():
1656 if not creates[facet] in ignores:
1657 choices[creates[facet]] = eval(menu.get(facet))
1658 for facet in options.keys():
1659 if not options[facet] in ignores:
1660 choices[options[facet]] = menu.get(facet)
1664 def get_formatted_menu_choices(state, choices):
1665 """Returns a formatted string of menu choices."""
1667 choice_keys = list(choices.keys())
1669 for choice in choice_keys:
1670 choice_output += " [$(red)" + choice + "$(nrm)] " + choices[
1674 choice_output += "$(eol)"
1675 return choice_output
1678 def get_menu_branches(state):
1679 """Return a dict of choice:branch."""
1681 for facet in universe.categories["menu"][state].facets():
1682 if facet.startswith("branch_"):
1684 facet.split("_", 2)[1]
1685 ] = universe.categories["menu"][state].get(facet)
1689 def get_default_branch(state):
1690 """Return the default branch."""
1691 return universe.categories["menu"][state].get("branch")
1694 def get_choice_branch(user, choice):
1695 """Returns the new state matching the given choice."""
1696 branches = get_menu_branches(user.state)
1697 if choice in branches.keys():
1698 return branches[choice]
1699 elif choice in user.menu_choices.keys():
1700 return get_default_branch(user.state)
1705 def get_menu_actions(state):
1706 """Return a dict of choice:branch."""
1708 for facet in universe.categories["menu"][state].facets():
1709 if facet.startswith("action_"):
1711 facet.split("_", 2)[1]
1712 ] = universe.categories["menu"][state].get(facet)
1716 def get_default_action(state):
1717 """Return the default action."""
1718 return universe.categories["menu"][state].get("action")
1721 def get_choice_action(user, choice):
1722 """Run any indicated script for the given choice."""
1723 actions = get_menu_actions(user.state)
1724 if choice in actions.keys():
1725 return actions[choice]
1726 elif choice in user.menu_choices.keys():
1727 return get_default_action(user.state)
1732 def handle_user_input(user):
1733 """The main handler, branches to a state-specific handler."""
1736 # if the user's client echo is off, send a blank line for aesthetics
1737 if mudpy.telnet.is_enabled(user, mudpy.telnet.TELOPT_ECHO,
1739 user.send("", add_prompt=False, prepend_padding=False)
1741 # check to make sure the state is expected, then call that handler
1742 if "handler_" + user.state in globals():
1743 exec("handler_" + user.state + "(user)")
1745 generic_menu_handler(user)
1747 # since we got input, flag that the menu/prompt needs to be redisplayed
1748 user.menu_seen = False
1750 # update the last_input timestamp while we're at it
1751 user.last_input = universe.get_time()
1754 def generic_menu_handler(user):
1755 """A generic menu choice handler."""
1757 # get a lower-case representation of the next line of input
1758 if user.input_queue:
1759 choice = user.input_queue.pop(0)
1761 choice = choice.lower()
1765 choice = get_default_menu_choice(user.state)
1766 if choice in user.menu_choices:
1767 exec(get_choice_action(user, choice))
1768 new_state = get_choice_branch(user, choice)
1770 user.state = new_state
1772 user.error = "default"
1775 def handler_entering_account_name(user):
1776 """Handle the login account name."""
1778 # get the next waiting line of input
1779 input_data = user.input_queue.pop(0)
1781 # did the user enter anything?
1784 # keep only the first word and convert to lower-case
1785 name = input_data.lower()
1787 # fail if there are non-alphanumeric characters
1788 if name != "".join(filter(
1789 lambda x: x >= "0" and x <= "9" or x >= "a" and x <= "z",
1791 user.error = "bad_name"
1793 # if that account exists, time to request a password
1794 elif name in universe.categories["account"]:
1795 user.account = universe.categories["account"][name]
1796 user.state = "checking_password"
1798 # otherwise, this could be a brand new user
1800 user.account = Element("account:" + name, universe)
1801 user.account.set("name", name)
1802 log("New user: " + name, 2)
1803 user.state = "checking_new_account_name"
1805 # if the user entered nothing for a name, then buhbye
1807 user.state = "disconnecting"
1810 def handler_checking_password(user):
1811 """Handle the login account password."""
1812 import mudpy.password
1814 # get the next waiting line of input
1815 input_data = user.input_queue.pop(0)
1817 # does the hashed input equal the stored hash?
1818 if mudpy.password.verify(input_data, user.account.get("passhash")):
1820 # if so, set the username and load from cold storage
1821 if not user.replace_old_connections():
1823 user.state = "main_utility"
1825 # if at first your hashes don't match, try, try again
1826 elif user.password_tries < universe.categories[
1833 user.password_tries += 1
1834 user.error = "incorrect"
1836 # we've exceeded the maximum number of password failures, so disconnect
1839 "$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)"
1841 user.state = "disconnecting"
1844 def handler_entering_new_password(user):
1845 """Handle a new password entry."""
1846 import mudpy.password
1848 # get the next waiting line of input
1849 input_data = user.input_queue.pop(0)
1851 # make sure the password is strong--at least one upper, one lower and
1852 # one digit, seven or more characters in length
1853 if len(input_data) > 6 and len(
1854 list(filter(lambda x: x >= "0" and x <= "9", input_data))
1856 list(filter(lambda x: x >= "A" and x <= "Z", input_data))
1858 list(filter(lambda x: x >= "a" and x <= "z", input_data))
1861 # hash and store it, then move on to verification
1862 user.account.set("passhash", mudpy.password.create(input_data))
1863 user.state = "verifying_new_password"
1865 # the password was weak, try again if you haven't tried too many times
1866 elif user.password_tries < universe.categories[
1873 user.password_tries += 1
1876 # too many tries, so adios
1879 "$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)"
1881 user.account.destroy()
1882 user.state = "disconnecting"
1885 def handler_verifying_new_password(user):
1886 """Handle the re-entered new password for verification."""
1887 import mudpy.password
1889 # get the next waiting line of input
1890 input_data = user.input_queue.pop(0)
1892 # hash the input and match it to storage
1893 if mudpy.password.verify(input_data, user.account.get("passhash")):
1896 # the hashes matched, so go active
1897 if not user.replace_old_connections():
1898 user.state = "main_utility"
1900 # go back to entering the new password as long as you haven't tried
1902 elif user.password_tries < universe.categories[
1909 user.password_tries += 1
1910 user.error = "differs"
1911 user.state = "entering_new_password"
1913 # otherwise, sayonara
1916 "$(eol)$(red)Too many failed password attempts...$(nrm)$(eol)"
1918 user.account.destroy()
1919 user.state = "disconnecting"
1922 def handler_active(user):
1923 """Handle input for active users."""
1925 # get the next waiting line of input
1926 input_data = user.input_queue.pop(0)
1931 # split out the command and parameters
1933 mode = actor.get("mode")
1934 if mode and input_data.startswith("!"):
1935 command_name, parameters = first_word(input_data[1:])
1936 elif mode == "chat":
1937 command_name = "say"
1938 parameters = input_data
1940 command_name, parameters = first_word(input_data)
1942 # lowercase the command
1943 command_name = command_name.lower()
1945 # the command matches a command word for which we have data
1946 if command_name in universe.categories["command"]:
1947 command = universe.categories["command"][command_name]
1951 # if it's allowed, do it
1952 if actor.can_run(command):
1953 exec(command.get("action"))
1955 # otherwise, give an error
1957 command_error(actor, input_data)
1959 # if no input, just idle back with a prompt
1961 user.send("", just_prompt=True)
1964 def command_halt(actor, parameters):
1965 """Halt the world."""
1968 # see if there's a message or use a generic one
1970 message = "Halting: " + parameters
1972 message = "User " + actor.owner.account.get(
1974 ) + " halted the world."
1977 broadcast(message, add_prompt=False)
1980 # set a flag to terminate the world
1981 universe.terminate_flag = True
1984 def command_reload(actor):
1985 """Reload all code modules, configs and data."""
1988 # let the user know and log
1989 actor.send("Reloading all code modules, configs and data.")
1992 actor.owner.account.get("name") + " reloaded the world.",
1996 # set a flag to reload
1997 universe.reload_flag = True
2000 def command_quit(actor):
2001 """Leave the world and go back to the main menu."""
2003 actor.owner.state = "main_utility"
2004 actor.owner.deactivate_avatar()
2007 def command_help(actor, parameters):
2008 """List available commands and provide help for commands."""
2010 # did the user ask for help on a specific command word?
2011 if parameters and actor.owner:
2013 # is the command word one for which we have data?
2014 if parameters in universe.categories["command"]:
2015 command = universe.categories["command"][parameters]
2019 # only for allowed commands
2020 if actor.can_run(command):
2022 # add a description if provided
2023 description = command.get("description")
2025 description = "(no short description provided)"
2026 if command.getboolean("administrative"):
2030 output += parameters + "$(nrm) - " + description + "$(eol)$(eol)"
2032 # add the help text if provided
2033 help_text = command.get("help")
2035 help_text = "No help is provided for this command."
2038 # list related commands
2039 see_also = command.getlist("see_also")
2041 really_see_also = ""
2042 for item in see_also:
2043 if item in universe.categories["command"]:
2044 command = universe.categories["command"][item]
2045 if actor.can_run(command):
2047 really_see_also += ", "
2048 if command.getboolean("administrative"):
2049 really_see_also += "$(red)"
2051 really_see_also += "$(grn)"
2052 really_see_also += item + "$(nrm)"
2054 output += "$(eol)$(eol)See also: " + really_see_also
2056 # no data for the requested command word
2058 output = "That is not an available command."
2060 # no specific command word was indicated
2063 # give a sorted list of commands with descriptions if provided
2064 output = "These are the commands available to you:$(eol)$(eol)"
2065 sorted_commands = list(universe.categories["command"].keys())
2066 sorted_commands.sort()
2067 for item in sorted_commands:
2068 command = universe.categories["command"][item]
2069 if actor.can_run(command):
2070 description = command.get("description")
2072 description = "(no short description provided)"
2073 if command.getboolean("administrative"):
2077 output += item + "$(nrm) - " + description + "$(eol)"
2078 output += "$(eol)Enter \"help COMMAND\" for help on a command " \
2079 + "named \"COMMAND\"."
2081 # send the accumulated output to the user
2085 def command_move(actor, parameters):
2086 """Move the avatar in a given direction."""
2087 if parameters in universe.contents[actor.get("location")].portals():
2088 actor.move_direction(parameters)
2090 actor.send("You cannot go that way.")
2093 def command_look(actor, parameters):
2096 actor.send("You can't look at or in anything yet.")
2098 actor.look_at(actor.get("location"))
2101 def command_say(actor, parameters):
2102 """Speak to others in the same room."""
2105 # check for replacement macros and escape them
2106 parameters = escape_macros(parameters)
2108 # if the message is wrapped in quotes, remove them and leave contents
2110 if parameters.startswith("\"") and parameters.endswith("\""):
2111 message = parameters[1:-1]
2114 # otherwise, get rid of stray quote marks on the ends of the message
2116 message = parameters.strip("\"'`")
2119 # the user entered a message
2122 # match the punctuation used, if any, to an action
2123 actions = universe.categories["internal"]["language"].getdict(
2126 default_punctuation = (
2127 universe.categories["internal"]["language"].get(
2128 "default_punctuation"))
2130 for mark in actions.keys():
2131 if not literal and message.endswith(mark):
2132 action = actions[mark]
2135 # add punctuation if needed
2137 action = actions[default_punctuation]
2138 if message and not (
2139 literal or unicodedata.category(message[-1]) == "Po"
2141 message += default_punctuation
2143 # failsafe checks to avoid unwanted reformatting and null strings
2144 if message and not literal:
2146 # decapitalize the first letter to improve matching
2147 message = message[0].lower() + message[1:]
2149 # iterate over all words in message, replacing typos
2150 typos = universe.categories["internal"]["language"].getdict(
2153 words = message.split()
2154 for index in range(len(words)):
2156 while unicodedata.category(word[0]) == "Po":
2158 while unicodedata.category(word[-1]) == "Po":
2160 if word in typos.keys():
2161 words[index] = words[index].replace(word, typos[word])
2162 message = " ".join(words)
2164 # capitalize the first letter
2165 message = message[0].upper() + message[1:]
2169 actor.echo_to_location(
2170 actor.get("name") + " " + action + "s, \"" + message + "\""
2172 actor.send("You " + action + ", \"" + message + "\"")
2174 # there was no message
2176 actor.send("What do you want to say?")
2179 def command_chat(actor):
2180 """Toggle chat mode."""
2181 mode = actor.get("mode")
2183 actor.set("mode", "chat")
2184 actor.send("Entering chat mode (use $(grn)!chat$(nrm) to exit).")
2185 elif mode == "chat":
2186 actor.remove_facet("mode")
2187 actor.send("Exiting chat mode.")
2189 actor.send("Sorry, but you're already busy with something else!")
2192 def command_show(actor, parameters):
2193 """Show program data."""
2196 arguments = parameters.split()
2198 message = "What do you want to show?"
2199 elif arguments[0] == "time":
2200 message = universe.categories["internal"]["counters"].get(
2202 ) + " increments elapsed since the world was created."
2203 elif arguments[0] == "categories":
2204 message = "These are the element categories:$(eol)"
2205 categories = list(universe.categories.keys())
2207 for category in categories:
2208 message += "$(eol) $(grn)" + category + "$(nrm)"
2209 elif arguments[0] == "files":
2210 message = "These are the current files containing the universe:$(eol)"
2211 filenames = list(universe.files.keys())
2213 for filename in filenames:
2214 if universe.files[filename].is_writeable():
2218 message += "$(eol) $(red)(" + status + ") $(grn)" + filename \
2220 elif arguments[0] == "category":
2221 if len(arguments) != 2:
2222 message = "You must specify one category."
2223 elif arguments[1] in universe.categories:
2224 message = "These are the elements in the \"" + arguments[1] \
2225 + "\" category:$(eol)"
2228 universe.categories[arguments[1]][x].key
2229 ) for x in universe.categories[arguments[1]].keys()
2232 for element in elements:
2233 message += "$(eol) $(grn)" + element + "$(nrm)"
2235 message = "Category \"" + arguments[1] + "\" does not exist."
2236 elif arguments[0] == "file":
2237 if len(arguments) != 2:
2238 message = "You must specify one file."
2239 elif arguments[1] in universe.files:
2240 message = "These are the elements in the \"" + arguments[1] \
2242 elements = universe.files[arguments[1]].data.sections()
2244 for element in elements:
2245 message += "$(eol) $(grn)" + element + "$(nrm)"
2247 message = "Category \"" + arguments[1] + "\" does not exist."
2248 elif arguments[0] == "element":
2249 if len(arguments) != 2:
2250 message = "You must specify one element."
2251 elif arguments[1] in universe.contents:
2252 element = universe.contents[arguments[1]]
2253 message = "These are the properties of the \"" + arguments[1] \
2255 "\" element (in \"" + \
2256 element.origin.filename + "\"):$(eol)"
2257 facets = element.facets()
2259 for facet in facets:
2260 message += "$(eol) $(grn)" + facet + ": $(red)" \
2261 + escape_macros(element.get(facet)) + "$(nrm)"
2263 message = "Element \"" + arguments[1] + "\" does not exist."
2264 elif arguments[0] == "result":
2265 if len(arguments) < 2:
2266 message = "You need to specify an expression."
2269 message = repr(eval(" ".join(arguments[1:])))
2271 message = "Your expression raised an exception!"
2272 elif arguments[0] == "log":
2273 if len(arguments) == 4:
2274 if re.match("^\d+$", arguments[3]) and int(arguments[3]) >= 0:
2275 stop = int(arguments[3])
2280 if len(arguments) >= 3:
2281 if re.match("^\d+$", arguments[2]) and int(arguments[2]) > 0:
2282 start = int(arguments[2])
2287 if len(arguments) >= 2:
2288 if (re.match("^\d+$", arguments[1])
2289 and 0 <= int(arguments[1]) <= 9):
2290 level = int(arguments[1])
2293 elif 0 <= actor.owner.account.getint("loglevel") <= 9:
2294 level = actor.owner.account.getint("loglevel")
2297 if level > -1 and start > -1 and stop > -1:
2298 message = get_loglines(level, start, stop)
2300 message = "When specified, level must be 0-9 (default 1), " \
2301 + "start and stop must be >=1 (default 10 and 1)."
2303 message = "I don't know what \"" + parameters + "\" is."
2307 def command_create(actor, parameters):
2308 """Create an element if it does not exist."""
2310 message = "You must at least specify an element to create."
2311 elif not actor.owner:
2314 arguments = parameters.split()
2315 if len(arguments) == 1:
2316 arguments.append("")
2317 if len(arguments) == 2:
2318 element, filename = arguments
2319 if element in universe.contents:
2320 message = "The \"" + element + "\" element already exists."
2322 message = "You create \"" + \
2323 element + "\" within the universe."
2324 logline = actor.owner.account.get(
2326 ) + " created an element: " + element
2328 logline += " in file " + filename
2329 if filename not in universe.files:
2330 message += " Warning: \"" + filename \
2331 + "\" is not yet included in any other file and will " \
2333 "not be read on startup unless this is remedied."
2334 Element(element, universe, filename)
2336 elif len(arguments) > 2:
2337 message = "You can only specify an element and a filename."
2341 def command_destroy(actor, parameters):
2342 """Destroy an element if it exists."""
2345 message = "You must specify an element to destroy."
2347 if parameters not in universe.contents:
2348 message = "The \"" + parameters + \
2349 "\" element does not exist."
2351 universe.contents[parameters].destroy()
2352 message = "You destroy \"" + parameters \
2353 + "\" within the universe."
2355 actor.owner.account.get(
2357 ) + " destroyed an element: " + parameters,
2363 def command_set(actor, parameters):
2364 """Set a facet of an element."""
2366 message = "You must specify an element, a facet and a value."
2368 arguments = parameters.split(" ", 2)
2369 if len(arguments) == 1:
2370 message = "What facet of element \"" + arguments[0] \
2371 + "\" would you like to set?"
2372 elif len(arguments) == 2:
2373 message = "What value would you like to set for the \"" \
2374 + arguments[1] + "\" facet of the \"" + arguments[0] \
2377 element, facet, value = arguments
2378 if element not in universe.contents:
2379 message = "The \"" + element + "\" element does not exist."
2381 universe.contents[element].set(facet, value)
2382 message = "You have successfully (re)set the \"" + facet \
2383 + "\" facet of element \"" + element \
2384 + "\". Try \"show element " + \
2385 element + "\" for verification."
2389 def command_delete(actor, parameters):
2390 """Delete a facet from an element."""
2392 message = "You must specify an element and a facet."
2394 arguments = parameters.split(" ")
2395 if len(arguments) == 1:
2396 message = "What facet of element \"" + arguments[0] \
2397 + "\" would you like to delete?"
2398 elif len(arguments) != 2:
2399 message = "You may only specify an element and a facet."
2401 element, facet = arguments
2402 if element not in universe.contents:
2403 message = "The \"" + element + "\" element does not exist."
2404 elif facet not in universe.contents[element].facets():
2405 message = "The \"" + element + "\" element has no \"" + facet \
2408 universe.contents[element].remove_facet(facet)
2409 message = "You have successfully deleted the \"" + facet \
2410 + "\" facet of element \"" + element \
2411 + "\". Try \"show element " + \
2412 element + "\" for verification."
2416 def command_error(actor, input_data):
2417 """Generic error for an unrecognized command word."""
2420 # 90% of the time use a generic error
2421 if random.randrange(10):
2422 message = "I'm not sure what \"" + input_data + "\" means..."
2424 # 10% of the time use the classic diku error
2426 message = "Arglebargle, glop-glyf!?!"
2428 # send the error message
2432 def daemonize(universe):
2433 """Fork and disassociate from everything."""
2441 # only if this is what we're configured to do
2442 if universe.contents["internal:process"].getboolean("daemon"):
2444 # if possible, we want to rename the process to the same as the script
2445 new_argv = b"\x00".join(x.encode("utf-8") for x in sys.argv) + b"\x00"
2446 short_argv0 = os.path.basename(sys.argv[0]).encode("utf-8") + b"\x00"
2448 # attempt the linux way first
2450 argv_array = ctypes.POINTER(ctypes.c_char_p)
2451 ctypes.pythonapi.Py_GetArgcArgv.argtypes = (
2452 ctypes.POINTER(ctypes.c_int),
2453 ctypes.POINTER(argv_array)
2456 ctypes.pythonapi.Py_GetArgcArgv(
2458 ctypes.pointer(argc)
2460 old_argv0_size = len(argc.contents.value)
2461 ctypes.memset(argc.contents, 0, len(new_argv) + old_argv0_size)
2462 ctypes.memmove(argc.contents, new_argv, len(new_argv))
2463 ctypes.CDLL(ctypes.util.find_library("c")).prctl(
2473 # since that failed, maybe it's bsd?
2476 # much simpler, since bsd has a libc function call for this
2477 ctypes.CDLL(ctypes.util.find_library("c")).setproctitle(
2483 # that didn't work either, so just log that we couldn't
2484 log("Failed to rename the interpreter process (cosmetic).")
2486 # log before we start forking around, so the terminal gets the message
2487 log("Disassociating from the controlling terminal.")
2489 # fork off and die, so we free up the controlling terminal
2493 # switch to a new process group
2496 # fork some more, this time to free us from the old process group
2500 # reset the working directory so we don't needlessly tie up mounts
2503 # clear the file creation mask so we can bend it to our will later
2506 # redirect stdin/stdout/stderr and close off their former descriptors
2507 for stdpipe in range(3):
2509 sys.stdin = codecs.open("/dev/null", "r", "utf-8")
2510 sys.__stdin__ = codecs.open("/dev/null", "r", "utf-8")
2511 sys.stdout = codecs.open("/dev/null", "w", "utf-8")
2512 sys.stderr = codecs.open("/dev/null", "w", "utf-8")
2513 sys.__stdout__ = codecs.open("/dev/null", "w", "utf-8")
2514 sys.__stderr__ = codecs.open("/dev/null", "w", "utf-8")
2517 def create_pidfile(universe):
2518 """Write a file containing the current process ID."""
2522 pid = str(os.getpid())
2523 log("Process ID: " + pid)
2524 file_name = universe.contents["internal:process"].get("pidfile")
2526 if not os.path.isabs(file_name):
2527 file_name = os.path.join(universe.startdir, file_name)
2528 file_descriptor = codecs.open(file_name, "w", "utf-8")
2529 file_descriptor.write(pid + "\n")
2530 file_descriptor.flush()
2531 file_descriptor.close()
2534 def remove_pidfile(universe):
2535 """Remove the file containing the current process ID."""
2538 file_name = universe.contents["internal:process"].get("pidfile")
2540 if not os.path.isabs(file_name):
2541 file_name = os.path.join(universe.startdir, file_name)
2542 if os.access(file_name, os.W_OK):
2543 os.remove(file_name)
2546 def excepthook(excepttype, value, tracebackdata):
2547 """Handle uncaught exceptions."""
2550 # assemble the list of errors into a single string
2552 traceback.format_exception(excepttype, value, tracebackdata)
2555 # try to log it, if possible
2561 # try to write it to stderr, if possible
2563 sys.stderr.write(message)
2568 def sighook(what, where):
2569 """Handle external signals."""
2573 message = "Caught signal: "
2575 # for a hangup signal
2576 if what == signal.SIGHUP:
2577 message += "hangup (reloading)"
2578 universe.reload_flag = True
2580 # for a terminate signal
2581 elif what == signal.SIGTERM:
2582 message += "terminate (halting)"
2583 universe.terminate_flag = True
2585 # catchall for unexpected signals
2587 message += str(what) + " (unhandled)"
2593 def override_excepthook():
2594 """Redefine sys.excepthook with our own."""
2596 sys.excepthook = excepthook
2599 def assign_sighook():
2600 """Assign a customized handler for some signals."""
2602 signal.signal(signal.SIGHUP, sighook)
2603 signal.signal(signal.SIGTERM, sighook)
2607 """This contains functions to be performed when starting the engine."""
2610 # see if a configuration file was specified
2611 if len(sys.argv) > 1:
2612 conffile = sys.argv[1]
2618 universe = Universe(conffile, True)
2620 # log an initial message
2621 log("Started mudpy with command line: " + " ".join(sys.argv))
2623 # fork and disassociate
2626 # override the default exception handler so we get logging first thing
2627 override_excepthook()
2629 # set up custom signal handlers
2633 create_pidfile(universe)
2635 # pass the initialized universe back
2640 """This contains functions to be performed when shutting down the
2643 # the loop has terminated, so save persistent data
2646 # log a final message
2647 log("Shutting down now.")
2649 # get rid of the pidfile
2650 remove_pidfile(universe)