def set(self, facet, value):
"""Set values."""
+ if facet in ["loglevel"]:
+ value = int(value)
if not self.has_facet(facet) or not self.get(facet) == value:
if self.old_style:
if self.key not in self.origin.data:
def portals(self):
"""Map the portal directions for an area to neighbors."""
portals = {}
- if re.match("""^area:-?\d+,-?\d+,-?\d+$""", self.key):
+ if re.match(r"""^area:-?\d+,-?\d+,-?\d+$""", self.key):
coordinates = [(int(x))
for x in self.key.split(":")[1].split(",")]
offsets = dict(
inactive_avatars.append(self.contents[avatar])
except KeyError:
pending_loglines.append((
- "Missing avatar \"%s\", possible data corruption" %
+ 'Missing avatar "%s", possible data corruption' %
avatar, 6))
for user in self.userlist:
if user.avatar in inactive_avatars:
if self.state is not "authenticated":
log("User " + self.account.get("name") + " logged in.", 2)
self.authenticated = True
- if self.account.subkey in universe.contents["mudpy.limit"].get(
- "admins"):
+ if ("mudpy.limit" in universe.contents and self.account.subkey in
+ universe.contents["mudpy.limit"].get("admins")):
self.account.set("administrator", "True")
def show_menu(self):
try:
avatars.append(universe.contents[avatar].get("name"))
except KeyError:
- log("Missing avatar \"%s\", possible data corruption." %
+ log('Missing avatar "%s", possible data corruption.' %
avatar, 6)
return avatars
"""Log a message."""
# a couple references we need
- file_name = universe.contents["mudpy.log"].get("file")
- max_log_lines = universe.contents["mudpy.log"].get("lines")
- syslog_name = universe.contents["mudpy.log"].get("syslog")
+ if "mudpy.log" in universe.contents:
+ file_name = universe.contents["mudpy.log"].get("file", "")
+ max_log_lines = universe.contents["mudpy.log"].get("lines", 0)
+ syslog_name = universe.contents["mudpy.log"].get("syslog", "")
+ else:
+ file_name = ""
+ max_log_lines = 0
+ syslog_name = ""
timestamp = time.asctime()[4:19]
# turn the message into a list of nonempty lines
file_descriptor.close()
# send the timestamp and line to standard output
- if universe.contents["mudpy.log"].get("stdout"):
+ if ("mudpy.log" in universe.contents and
+ universe.contents["mudpy.log"].get("stdout")):
for line in lines:
print(timestamp + " " + line)
# get the next waiting line of input
input_data = user.input_queue.pop(0)
+ if "mudpy.limit" in universe.contents:
+ max_password_tries = universe.contents["mudpy.limit"].get(
+ "password_tries", 3)
+ else:
+ max_password_tries = 3
+
# does the hashed input equal the stored hash?
if mudpy.password.verify(input_data, user.account.get("passhash")):
user.state = "main_utility"
# if at first your hashes don't match, try, try again
- elif user.password_tries < universe.contents["mudpy.limit"].get(
- "password_tries") - 1:
+ elif user.password_tries < max_password_tries - 1:
user.password_tries += 1
user.error = "incorrect"
# get the next waiting line of input
input_data = user.input_queue.pop(0)
+ if "mudpy.limit" in universe.contents:
+ max_password_tries = universe.contents["mudpy.limit"].get(
+ "password_tries", 3)
+ else:
+ max_password_tries = 3
+
# make sure the password is strong--at least one upper, one lower and
# one digit, seven or more characters in length
if len(input_data) > 6 and len(
user.state = "verifying_new_password"
# the password was weak, try again if you haven't tried too many times
- elif user.password_tries < universe.contents["mudpy.limit"].get(
- "password_tries") - 1:
+ elif user.password_tries < max_password_tries - 1:
user.password_tries += 1
user.error = "weak"
# get the next waiting line of input
input_data = user.input_queue.pop(0)
+ if "mudpy.limit" in universe.contents:
+ max_password_tries = universe.contents["mudpy.limit"].get(
+ "password_tries", 3)
+ else:
+ max_password_tries = 3
+
# hash the input and match it to storage
if mudpy.password.verify(input_data, user.account.get("passhash")):
user.authenticate()
# go back to entering the new password as long as you haven't tried
# too many times
- elif user.password_tries < universe.contents["mudpy.limit"].get(
- "password_tries") - 1:
+ elif user.password_tries < max_password_tries - 1:
user.password_tries += 1
user.error = "differs"
user.state = "entering_new_password"
log(
"User " +
actor.owner.account.get("name") + " reloaded the world.",
- 8
+ 6
)
# set a flag to reload
else:
output += " $(grn)"
output += item + "$(nrm) - " + description + "$(eol)"
- output += ("$(eol)Enter \"help COMMAND\" for help on a command "
- "named \"COMMAND\".")
+ output += ('$(eol)Enter "help COMMAND" for help on a command '
+ 'named "COMMAND".')
# send the accumulated output to the user
actor.send(output)
# if the message is wrapped in quotes, remove them and leave contents
# intact
- if parameters.startswith("\"") and parameters.endswith("\""):
+ if parameters.startswith('"') and parameters.endswith('"'):
message = parameters[1:-1]
literal = True
# otherwise, get rid of stray quote marks on the ends of the message
else:
- message = parameters.strip("\"'`")
+ message = parameters.strip('''"'`''')
literal = False
# the user entered a message
if message:
# match the punctuation used, if any, to an action
- actions = universe.contents["mudpy.linguistic"].get(
- "actions"
- )
- default_punctuation = (
- universe.contents["mudpy.linguistic"].get(
- "default_punctuation"))
+ if "mudpy.linguistic" in universe.contents:
+ actions = universe.contents["mudpy.linguistic"].get("actions", {})
+ default_punctuation = (universe.contents["mudpy.linguistic"].get(
+ "default_punctuation", "."))
+ else:
+ actions = {}
+ default_punctuation = "."
action = ""
# reverse sort punctuation options so the longest match wins
message = message[0].lower() + message[1:]
# iterate over all words in message, replacing typos
- typos = universe.contents["mudpy.linguistic"].get(
- "typos"
- )
+ if "mudpy.linguistic" in universe.contents:
+ typos = universe.contents["mudpy.linguistic"].get("typos", {})
+ else:
+ typos = {}
words = message.split()
for index in range(len(words)):
word = words[index]
# tell the area
if message:
actor.echo_to_location(
- actor.get("name") + " " + action + "s, \"" + message + "\""
+ actor.get("name") + " " + action + 's, "' + message + '"'
)
- actor.send("You " + action + ", \"" + message + "\"")
+ actor.send("You " + action + ', "' + message + '"')
# there was no message
else:
if len(arguments) != 2:
message = "You must specify one category."
elif arguments[1] in universe.categories:
- message = ("These are the elements in the \"" + arguments[1]
- + "\" category:$(eol)")
+ message = ('These are the elements in the "' + arguments[1]
+ + '" category:$(eol)')
elements = [
(
universe.categories[arguments[1]][x].key
for element in elements:
message += "$(eol) $(grn)" + element + "$(nrm)"
else:
- message = "Category \"" + arguments[1] + "\" does not exist."
+ message = 'Category "' + arguments[1] + '" does not exist.'
elif arguments[0] == "file":
if len(arguments) != 2:
message = "You must specify one file."
elif arguments[1] in universe.files:
- message = ("These are the elements in the \"" + arguments[1]
- + "\" file:$(eol)")
+ message = ('These are the elements in the "' + arguments[1]
+ + '" file:$(eol)')
elements = universe.files[arguments[1]].data.keys()
elements.sort()
for element in elements:
message += "$(eol) $(grn)" + element + "$(nrm)"
else:
- message = "Category \"" + arguments[1] + "\" does not exist."
+ message = 'Category "' + arguments[1] + '" does not exist.'
elif arguments[0] == "element":
if len(arguments) != 2:
message = "You must specify one element."
elif arguments[1].strip(".") in universe.contents:
element = universe.contents[arguments[1].strip(".")]
- message = ("These are the properties of the \"" + arguments[1]
- + "\" element (in \"" + element.origin.filename
- + "\"):$(eol)")
+ message = ('These are the properties of the "' + arguments[1]
+ + '" element (in "' + element.origin.filename
+ + '"):$(eol)')
facets = element.facets()
for facet in sorted(facets):
if element.old_style:
message += ("$(eol) $(grn)%s: $(red)%s$(nrm)" %
(facet, str(facets[facet])))
else:
- message = "Element \"" + arguments[1] + "\" does not exist."
+ message = 'Element "' + arguments[1] + '" does not exist.'
elif arguments[0] == "result":
if len(arguments) < 2:
message = "You need to specify an expression."
"$(eol)$(bld)%s$(nrm)" % e)
elif arguments[0] == "log":
if len(arguments) == 4:
- if re.match("^\d+$", arguments[3]) and int(arguments[3]) >= 0:
+ if re.match(r"^\d+$", arguments[3]) and int(arguments[3]) >= 0:
stop = int(arguments[3])
else:
stop = -1
else:
stop = 0
if len(arguments) >= 3:
- if re.match("^\d+$", arguments[2]) and int(arguments[2]) > 0:
+ if re.match(r"^\d+$", arguments[2]) and int(arguments[2]) > 0:
start = int(arguments[2])
else:
start = -1
else:
start = 10
if len(arguments) >= 2:
- if (re.match("^\d+$", arguments[1])
+ if (re.match(r"^\d+$", arguments[1])
and 0 <= int(arguments[1]) <= 9):
level = int(arguments[1])
else:
message = ("When specified, level must be 0-9 (default 1), "
"start and stop must be >=1 (default 10 and 1).")
else:
- message = "I don't know what \"" + parameters + "\" is."
+ message = '''I don't know what "''' + parameters + '" is.'
actor.send(message)
if len(arguments) == 2:
element, filename = arguments
if element in universe.contents:
- message = "The \"" + element + "\" element already exists."
+ message = 'The "' + element + '" element already exists.'
else:
- message = ("You create \"" +
- element + "\" within the universe.")
+ message = ('You create "' +
+ element + '" within the universe.')
logline = actor.owner.account.get(
"name"
) + " created an element: " + element
logline += " in file " + filename
if filename not in universe.files:
message += (
- " Warning: \"" + filename + "\" is not yet "
+ ' Warning: "' + filename + '" is not yet '
"included in any other file and will not be read "
"on startup unless this is remedied.")
Element(element, universe, filename, old_style=True)
message = "You must specify an element to destroy."
else:
if parameters not in universe.contents:
- message = "The \"" + parameters + "\" element does not exist."
+ message = 'The "' + parameters + '" element does not exist.'
else:
universe.contents[parameters].destroy()
- message = ("You destroy \"" + parameters
- + "\" within the universe.")
+ message = ('You destroy "' + parameters
+ + '" within the universe.')
log(
actor.owner.account.get(
"name"
else:
arguments = parameters.split(" ", 2)
if len(arguments) == 1:
- message = ("What facet of element \"" + arguments[0]
- + "\" would you like to set?")
+ message = ('What facet of element "' + arguments[0]
+ + '" would you like to set?')
elif len(arguments) == 2:
- message = ("What value would you like to set for the \"" +
- arguments[1] + "\" facet of the \"" + arguments[0]
- + "\" element?")
+ message = ('What value would you like to set for the "' +
+ arguments[1] + '" facet of the "' + arguments[0]
+ + '" element?')
else:
element, facet, value = arguments
if element not in universe.contents:
- message = "The \"" + element + "\" element does not exist."
+ message = 'The "' + element + '" element does not exist.'
else:
- universe.contents[element].set(facet, value)
- message = ("You have successfully (re)set the \"" + facet
- + "\" facet of element \"" + element
- + "\". Try \"show element " +
- element + "\" for verification.")
+ try:
+ universe.contents[element].set(facet, value)
+ except ValueError:
+ message = ('Value "%s" of type "%s" cannot be coerced '
+ 'to the correct datatype for facet "%s".' %
+ (value, type(value), facet))
+ else:
+ message = ('You have successfully (re)set the "' + facet
+ + '" facet of element "' + element
+ + '". Try "show element ' +
+ element + '" for verification.')
actor.send(message)
else:
arguments = parameters.split(" ")
if len(arguments) == 1:
- message = ("What facet of element \"" + arguments[0]
- + "\" would you like to delete?")
+ message = ('What facet of element "' + arguments[0]
+ + '" would you like to delete?')
elif len(arguments) != 2:
message = "You may only specify an element and a facet."
else:
element, facet = arguments
if element not in universe.contents:
- message = "The \"" + element + "\" element does not exist."
+ message = 'The "' + element + '" element does not exist.'
elif facet not in universe.contents[element].facets():
- message = ("The \"" + element + "\" element has no \"" + facet
- + "\" facet.")
+ message = ('The "' + element + '" element has no "' + facet
+ + '" facet.')
else:
universe.contents[element].remove_facet(facet)
- message = ("You have successfully deleted the \"" + facet
- + "\" facet of element \"" + element
- + "\". Try \"show element " +
- element + "\" for verification.")
+ message = ('You have successfully deleted the "' + facet
+ + '" facet of element "' + element
+ + '". Try "show element ' +
+ element + '" for verification.')
actor.send(message)
# 90% of the time use a generic error
if random.randrange(10):
- message = "I'm not sure what \"" + input_data + "\" means..."
+ message = '''I'm not sure what "''' + input_data + '''" means...'''
# 10% of the time use the classic diku error
else:
"""Fork and disassociate from everything."""
# only if this is what we're configured to do
- if universe.contents["mudpy.process"].get("daemon"):
+ if "mudpy.process" in universe.contents and universe.contents[
+ "mudpy.process"].get("daemon"):
# log before we start forking around, so the terminal gets the message
log("Disassociating from the controlling terminal.")
"""Write a file containing the current process ID."""
pid = str(os.getpid())
log("Process ID: " + pid)
- file_name = universe.contents["mudpy.process"].get("pidfile")
+ if "mudpy.process" in universe.contents:
+ file_name = universe.contents["mudpy.process"].get("pidfile", "")
+ else:
+ file_name = ""
if file_name:
if not os.path.isabs(file_name):
file_name = os.path.join(universe.startdir, file_name)
def remove_pidfile(universe):
"""Remove the file containing the current process ID."""
- file_name = universe.contents["mudpy.process"].get("pidfile")
+ if "mudpy.process" in universe.contents:
+ file_name = universe.contents["mudpy.process"].get("pidfile", "")
+ else:
+ file_name = ""
if file_name:
if not os.path.isabs(file_name):
file_name = os.path.join(universe.startdir, file_name)