2008-06-15 17:21:02 +00:00
|
|
|
"""
|
|
|
|
|
This is the command processing module. It is instanced once in the main
|
|
|
|
|
server module and the handle() function is hit every time a player sends
|
|
|
|
|
something.
|
|
|
|
|
"""
|
2007-04-30 14:21:48 +00:00
|
|
|
from traceback import format_exc
|
2007-05-09 15:28:12 +00:00
|
|
|
import time
|
2007-05-22 15:11:56 +00:00
|
|
|
|
2008-12-15 04:35:00 +00:00
|
|
|
from src.objects.models import Object
|
2007-05-22 15:11:56 +00:00
|
|
|
import defines_global
|
2007-05-11 15:23:27 +00:00
|
|
|
import cmdtable
|
2008-06-15 20:31:25 +00:00
|
|
|
import logger
|
|
|
|
|
import comsys
|
2006-12-03 00:40:19 +00:00
|
|
|
|
2006-11-28 21:51:36 +00:00
|
|
|
class UnknownCommand(Exception):
|
2008-06-17 00:38:59 +00:00
|
|
|
"""
|
|
|
|
|
Throw this when a user enters an an invalid command.
|
|
|
|
|
"""
|
2008-12-14 20:21:02 +00:00
|
|
|
pass
|
2008-12-15 01:10:26 +00:00
|
|
|
class ExitCommandHandler(Exception):
|
|
|
|
|
"""
|
|
|
|
|
Thrown when something happens and it's time to exit the command handler.
|
|
|
|
|
"""
|
|
|
|
|
pass
|
2008-12-14 20:21:02 +00:00
|
|
|
|
|
|
|
|
class Command(object):
|
|
|
|
|
# Reference to the master server object.
|
|
|
|
|
server = None
|
|
|
|
|
# The player session that the command originated from.
|
|
|
|
|
session = None
|
|
|
|
|
# The entire raw, un-parsed command.
|
|
|
|
|
raw_input = None
|
|
|
|
|
# Just the root command. IE: if input is "look dog", this is just "look".
|
|
|
|
|
command_string = None
|
|
|
|
|
# A list of switches in the form of strings.
|
|
|
|
|
command_switches = []
|
|
|
|
|
# The un-parsed argument provided. IE: if input is "look dog", this is "dog".
|
|
|
|
|
command_argument = None
|
2008-12-15 01:10:26 +00:00
|
|
|
# A reference to the command function looked up in a command table.
|
|
|
|
|
command_function = None
|
2008-12-14 20:21:02 +00:00
|
|
|
|
|
|
|
|
def parse_command_switches(self):
|
|
|
|
|
"""
|
|
|
|
|
Splits any switches out of a command_string into the command_switches
|
|
|
|
|
list, and yanks the switches out of the original command_string.
|
|
|
|
|
"""
|
|
|
|
|
splitted_command = self.command_string.split('/')
|
|
|
|
|
self.command_switches = splitted_command[1:]
|
|
|
|
|
self.command_string = splitted_command[0]
|
|
|
|
|
|
|
|
|
|
def parse_command(self):
|
|
|
|
|
"""
|
|
|
|
|
Breaks the command up into the main command string, a list of switches,
|
|
|
|
|
and a string containing the argument provided with the command. More
|
|
|
|
|
specific processing is left up to the individual command functions.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
"""
|
|
|
|
|
Break the command in half into command and argument. If the
|
|
|
|
|
command string can't be parsed, it has no argument and is
|
|
|
|
|
handled by the except ValueError block below.
|
|
|
|
|
"""
|
|
|
|
|
(self.command_string, self.command_argument) = self.raw_input.split(' ', 1)
|
|
|
|
|
self.command_argument = self.command_argument.strip()
|
2008-12-15 01:10:26 +00:00
|
|
|
self.command_string = self.command_string.strip()
|
|
|
|
|
"""
|
|
|
|
|
This is a really important behavior to note. If the user enters
|
|
|
|
|
anything other than a string with some character in it, the value
|
|
|
|
|
of the argument is None, not an empty string.
|
|
|
|
|
"""
|
|
|
|
|
if self.command_string == '':
|
|
|
|
|
self.command_string = None
|
2008-12-14 20:21:02 +00:00
|
|
|
if self.command_argument == '':
|
|
|
|
|
self.command_argument = None
|
|
|
|
|
except ValueError:
|
|
|
|
|
"""
|
|
|
|
|
No arguments. IE: look, who.
|
|
|
|
|
"""
|
|
|
|
|
self.command_string = self.raw_input
|
2009-01-13 01:42:39 +00:00
|
|
|
|
|
|
|
|
# Parse command_string for switches, regardless of what happens.
|
|
|
|
|
self.parse_command_switches()
|
2008-12-14 20:21:02 +00:00
|
|
|
|
|
|
|
|
def __init__(self, raw_input, server=None, session=None):
|
2008-12-15 01:10:26 +00:00
|
|
|
"""
|
|
|
|
|
Instantiates the Command object and does some preliminary parsing.
|
|
|
|
|
"""
|
2008-12-14 20:21:02 +00:00
|
|
|
self.server = server
|
|
|
|
|
self.raw_input = raw_input
|
|
|
|
|
self.session = session
|
2008-12-15 01:10:26 +00:00
|
|
|
# The work starts here.
|
2008-12-14 20:21:02 +00:00
|
|
|
self.parse_command()
|
|
|
|
|
|
|
|
|
|
def arg_has_target(self):
|
|
|
|
|
"""
|
|
|
|
|
Returns true if the argument looks to be target-style. IE:
|
|
|
|
|
page blah=hi
|
|
|
|
|
kick ball=north
|
|
|
|
|
"""
|
|
|
|
|
return "=" in self.command_argument
|
|
|
|
|
|
|
|
|
|
def get_arg_targets(self, delim=','):
|
|
|
|
|
"""
|
|
|
|
|
Returns a list of targets from the argument. These happen before
|
|
|
|
|
the '=' sign and may be separated by a delimiter.
|
|
|
|
|
"""
|
|
|
|
|
# Make sure we even have a target (= sign).
|
|
|
|
|
if not self.arg_has_target():
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
target = self.command_argument.split('=', 1)[0]
|
|
|
|
|
return [targ.strip() for targ in target.split(delim)]
|
|
|
|
|
|
|
|
|
|
def get_arg_target_value(self):
|
|
|
|
|
"""
|
|
|
|
|
In a case of something like: page bob=Hello there, the target is "bob",
|
|
|
|
|
while the value is "Hello there". This function returns the portion
|
|
|
|
|
of the command that takes place after the first equal sign.
|
|
|
|
|
"""
|
|
|
|
|
# Make sure we even have a target (= sign).
|
|
|
|
|
if not self.arg_has_target():
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
return self.command_argument.split('=', 1)[1]
|
2006-11-28 21:51:36 +00:00
|
|
|
|
2008-12-15 01:10:26 +00:00
|
|
|
def match_idle(command):
|
|
|
|
|
"""
|
|
|
|
|
Matches against the 'idle' command. It doesn't actually do anything, but it
|
|
|
|
|
lets the users get around badly configured NAT timeouts that would cause
|
|
|
|
|
them to drop if they don't send or receive something from the connection
|
|
|
|
|
for a while.
|
|
|
|
|
"""
|
|
|
|
|
if not command.command_string == 'idle':
|
|
|
|
|
# Anything other than an 'idle' command updates the public-facing idle
|
|
|
|
|
# time for the session.
|
|
|
|
|
command.session.count_command(silently=False)
|
|
|
|
|
else:
|
|
|
|
|
# User is hitting IDLE command. Don't update their publicly
|
|
|
|
|
# facing idle time, drop out of command handler immediately.
|
|
|
|
|
command.session.count_command(silently=True)
|
|
|
|
|
raise ExitCommandHandler
|
|
|
|
|
|
|
|
|
|
def match_exits(command):
|
2008-06-17 00:38:59 +00:00
|
|
|
"""
|
|
|
|
|
See if we can find an input match to exits.
|
|
|
|
|
"""
|
2008-12-15 01:10:26 +00:00
|
|
|
# If we're not logged in, don't check exits.
|
|
|
|
|
pobject = command.session.get_pobject()
|
2008-12-14 20:21:02 +00:00
|
|
|
exits = pobject.get_location().get_contents(filter_type=defines_global.OTYPE_EXIT)
|
2008-12-15 01:10:26 +00:00
|
|
|
exit_matches = Object.objects.list_search_object_namestr(exits,
|
|
|
|
|
command.command_string,
|
2008-12-14 20:21:02 +00:00
|
|
|
match_type="exact")
|
2008-12-15 01:10:26 +00:00
|
|
|
if exit_matches:
|
|
|
|
|
# Only interested in the first match.
|
|
|
|
|
targ_exit = exit_matches[0]
|
|
|
|
|
# An exit's home is its destination. If the exit has a None home value,
|
|
|
|
|
# it's not traversible.
|
|
|
|
|
if targ_exit.get_home():
|
|
|
|
|
# SCRIPT: See if the player can traverse the exit
|
|
|
|
|
if not targ_exit.scriptlink.default_lock({
|
|
|
|
|
"pobject": pobject
|
|
|
|
|
}):
|
|
|
|
|
command.session.msg("You can't traverse that exit.")
|
|
|
|
|
else:
|
|
|
|
|
pobject.move_to(targ_exit.get_home())
|
|
|
|
|
# Force the player to 'look' to see the description.
|
|
|
|
|
command.session.execute_cmd("look")
|
|
|
|
|
else:
|
|
|
|
|
command.session.msg("That exit leads to nowhere.")
|
|
|
|
|
# We found a match, kill the command handler.
|
|
|
|
|
raise ExitCommandHandler
|
|
|
|
|
|
|
|
|
|
def match_alias(command):
|
|
|
|
|
"""
|
|
|
|
|
Checks to see if the entered command matches an alias. If so, replaces
|
|
|
|
|
the command_string with the correct command.
|
|
|
|
|
|
|
|
|
|
We do a dictionary lookup. If the key (the player's command_string) doesn't
|
|
|
|
|
exist on the dict, just keep the command_string the same. If the key exists,
|
|
|
|
|
its value replaces the command_string. For example, sa -> say.
|
|
|
|
|
"""
|
|
|
|
|
command.command_string = command.server.cmd_alias_list.get(
|
|
|
|
|
command.command_string,
|
|
|
|
|
command.command_string)
|
|
|
|
|
|
|
|
|
|
def match_channel(command):
|
|
|
|
|
"""
|
|
|
|
|
Match against a comsys channel or comsys command. If the player is talking
|
|
|
|
|
over a channel, replace command_string with @cemit. If they're entering
|
|
|
|
|
a channel manipulation command, perform the operation and kill the things
|
|
|
|
|
immediately with a True value sent back to the command handler.
|
|
|
|
|
"""
|
|
|
|
|
if comsys.plr_has_channel(command.session, command.command_string,
|
|
|
|
|
alias_search=True, return_muted=True):
|
|
|
|
|
|
|
|
|
|
calias = command.command_string
|
|
|
|
|
cname = comsys.plr_cname_from_alias(command.session, calias)
|
|
|
|
|
|
|
|
|
|
if command.command_argument == "who":
|
|
|
|
|
comsys.msg_cwho(command.session, cname)
|
|
|
|
|
raise ExitCommandHandler
|
|
|
|
|
elif command.command_argument == "on":
|
|
|
|
|
comsys.plr_chan_on(command.session, calias)
|
|
|
|
|
raise ExitCommandHandler
|
|
|
|
|
elif command.command_argument == "off":
|
|
|
|
|
comsys.plr_chan_off(command.session, calias)
|
|
|
|
|
raise ExitCommandHandler
|
|
|
|
|
elif command.command_argument == "last":
|
|
|
|
|
comsys.msg_chan_hist(command.session, cname)
|
|
|
|
|
ExitCommandHandler
|
|
|
|
|
|
|
|
|
|
second_arg = "%s=%s" % (cname, command.command_argument)
|
|
|
|
|
command.command_string = "@cemit"
|
|
|
|
|
command.command_switches = ["sendername", "quiet"]
|
|
|
|
|
|
|
|
|
|
def command_table_lookup(command, command_table, eval_perms=True):
|
|
|
|
|
"""
|
|
|
|
|
Performs a command table lookup on the specified command table. Also
|
|
|
|
|
evaluates the permissions tuple.
|
|
|
|
|
"""
|
|
|
|
|
# Get the command's function reference (Or False)
|
|
|
|
|
cmdtuple = command_table.get_command_tuple(command.command_string)
|
|
|
|
|
if cmdtuple:
|
|
|
|
|
# If there is a permissions element to the entry, check perms.
|
|
|
|
|
if eval_perms and cmdtuple[1]:
|
|
|
|
|
if not command.session.get_pobject().user_has_perm_list(cmdtuple[1]):
|
|
|
|
|
command.session.msg(defines_global.NOPERMS_MSG)
|
|
|
|
|
raise ExitCommandHandler
|
|
|
|
|
# If flow reaches this point, user has perms and command is ready.
|
|
|
|
|
command.command_function = cmdtuple[0]
|
2006-12-25 06:04:06 +00:00
|
|
|
|
2008-12-14 20:21:02 +00:00
|
|
|
def handle(command):
|
2008-06-17 00:38:59 +00:00
|
|
|
"""
|
|
|
|
|
Use the spliced (list) uinput variable to retrieve the correct
|
|
|
|
|
command, or return an invalid command error.
|
basicobject.py
---------------
- Checks for NULL description on objects- if Null, it doesn't print the extra line any more.
- Made the checks for contents a little less ambiguous
cmdhandler.py
--------------
- Added new method 'parse_command' which takes a command string and tries to break it up based on common command parsing rules. Mostly complete, but could use some work on the edge cases. Check out the docstring on the function- I tried to make it fairly well documented.
- Changed the check for 'non-standard characters' to just return, rather than throw an Exception. Not sure if this causes any issues, but I noticed that when you hit enter without entering a command it would trigger this code. Now it just fails silently.
- The handle function now calls the parse_command function now and stores the results in parsed_input['parsed_command']. This then gets put into cdat['uinput'] at the end of handle() like before. The old data in parsed_input is still there, this is just a new field.
- Added cdat['raw_input'] to pass the full, untouched command string on. This is also stored in parsed_input['parsed_command']['raw_command'] so not sure fi this is necessary any longer, probably not.
cmdtable.py
------------
- Just cleaned it up a bit and straightened out the columns after changing 3-4 space indentation.
apps/objects/models.py
-----------------------
- set_description now sets the description attribute to 'None' (or Null in the db) when given a blank description. This is used for the change mentioned above in basicobject.py
- get_description now returns None if self.description is None
- used defines_global in the comparison methods like is_player
functions_db.py
----------------
- Changed import defines_global as defines_global to just 'import defines_global'- wasn't sure why this was this way, if I broke something (I didn't seem to) let me know.
- renamed player_search to player_name_search. Removed the use of local_and_global_search inside of it. local_and_global_search now calls it when it receives a search_string that starts with *.
- alias_search now only looks at attributes with attr_name == ALIAS. It used to just look at attr_value, which could match anything, it seemed.
- added 'dbref_search'
- local_and_global_search changes:
- Now uses dbref_search & player_search if the string starts with "#" or "*" respectively
- Changed when it uses dbref_search to whenever the search_string is a dbref. It used to check that it was a dbref, and that search_contents & search_location were set, but I *believe* in most MU*'s when you supply a dbref it never fails to find the object.
commands/unloggedin.py
-----------------------
- removed hardcoded object type #'s and started using defines_global instead
- when creating a new account, made sure that no object with an alias matching the player name requested exists. This is behavior from TinyMUSH, and I think most MUSHs follow this, but if not this is easy enough to change back.
commands/general.py
--------------------
- Rewrote cmd_page:
- New Features
- Page by dbref
- Page multiple people
- pose (:) and no space pose (;) pages
- When someone hits page without a target or data, it now will tell the player who they last paged, or say they haven't paged anyone if they don't have a LASTPAGED
- uses parse_command, made it a lot easier to work through the extra functionality added above
- When there are multiple words in a page target, it first tries to find a player that matches the entire string. If that fails, then it goes through each word, assuming each is a separate target, and works out paging them.
commands/objmanip.py
---------------------
- I started to muck with cmd_name & cmd_page, but decided to hold off for now. Largely, if everyone is cool with the idea that names & aliases should be totally unique, then we need to go ahead and re-write these. I'll do that if everyone is cool with it.
2008-06-13 18:15:54 +00:00
|
|
|
|
2008-06-17 00:38:59 +00:00
|
|
|
We're basically grabbing the player's command by tacking
|
|
|
|
|
their input on to 'cmd_' and looking it up in the GenCommands
|
|
|
|
|
class.
|
|
|
|
|
"""
|
2008-12-14 20:21:02 +00:00
|
|
|
session = command.session
|
|
|
|
|
server = command.server
|
2008-06-17 00:38:59 +00:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# TODO: Protect against non-standard characters.
|
2008-12-15 01:10:26 +00:00
|
|
|
if not command.command_string:
|
2008-12-14 20:21:02 +00:00
|
|
|
# Nothing sent in of value, ignore it.
|
2008-12-15 01:10:26 +00:00
|
|
|
raise ExitCommandHandler
|
basicobject.py
---------------
- Checks for NULL description on objects- if Null, it doesn't print the extra line any more.
- Made the checks for contents a little less ambiguous
cmdhandler.py
--------------
- Added new method 'parse_command' which takes a command string and tries to break it up based on common command parsing rules. Mostly complete, but could use some work on the edge cases. Check out the docstring on the function- I tried to make it fairly well documented.
- Changed the check for 'non-standard characters' to just return, rather than throw an Exception. Not sure if this causes any issues, but I noticed that when you hit enter without entering a command it would trigger this code. Now it just fails silently.
- The handle function now calls the parse_command function now and stores the results in parsed_input['parsed_command']. This then gets put into cdat['uinput'] at the end of handle() like before. The old data in parsed_input is still there, this is just a new field.
- Added cdat['raw_input'] to pass the full, untouched command string on. This is also stored in parsed_input['parsed_command']['raw_command'] so not sure fi this is necessary any longer, probably not.
cmdtable.py
------------
- Just cleaned it up a bit and straightened out the columns after changing 3-4 space indentation.
apps/objects/models.py
-----------------------
- set_description now sets the description attribute to 'None' (or Null in the db) when given a blank description. This is used for the change mentioned above in basicobject.py
- get_description now returns None if self.description is None
- used defines_global in the comparison methods like is_player
functions_db.py
----------------
- Changed import defines_global as defines_global to just 'import defines_global'- wasn't sure why this was this way, if I broke something (I didn't seem to) let me know.
- renamed player_search to player_name_search. Removed the use of local_and_global_search inside of it. local_and_global_search now calls it when it receives a search_string that starts with *.
- alias_search now only looks at attributes with attr_name == ALIAS. It used to just look at attr_value, which could match anything, it seemed.
- added 'dbref_search'
- local_and_global_search changes:
- Now uses dbref_search & player_search if the string starts with "#" or "*" respectively
- Changed when it uses dbref_search to whenever the search_string is a dbref. It used to check that it was a dbref, and that search_contents & search_location were set, but I *believe* in most MU*'s when you supply a dbref it never fails to find the object.
commands/unloggedin.py
-----------------------
- removed hardcoded object type #'s and started using defines_global instead
- when creating a new account, made sure that no object with an alias matching the player name requested exists. This is behavior from TinyMUSH, and I think most MUSHs follow this, but if not this is easy enough to change back.
commands/general.py
--------------------
- Rewrote cmd_page:
- New Features
- Page by dbref
- Page multiple people
- pose (:) and no space pose (;) pages
- When someone hits page without a target or data, it now will tell the player who they last paged, or say they haven't paged anyone if they don't have a LASTPAGED
- uses parse_command, made it a lot easier to work through the extra functionality added above
- When there are multiple words in a page target, it first tries to find a player that matches the entire string. If that fails, then it goes through each word, assuming each is a separate target, and works out paging them.
commands/objmanip.py
---------------------
- I started to muck with cmd_name & cmd_page, but decided to hold off for now. Largely, if everyone is cool with the idea that names & aliases should be totally unique, then we need to go ahead and re-write these. I'll do that if everyone is cool with it.
2008-06-13 18:15:54 +00:00
|
|
|
|
2008-06-17 00:38:59 +00:00
|
|
|
if session.logged_in:
|
2008-12-15 01:10:26 +00:00
|
|
|
# Match against the 'idle' command.
|
|
|
|
|
match_idle(command)
|
|
|
|
|
# See if this is an aliased command.
|
|
|
|
|
match_alias(command)
|
|
|
|
|
# Check if the user is using a channel command.
|
|
|
|
|
match_channel(command)
|
|
|
|
|
# See if the user is trying to traverse an exit.
|
|
|
|
|
match_exits(command)
|
|
|
|
|
# Retrieve the appropriate (if any) command function.
|
|
|
|
|
command_table_lookup(command, cmdtable.GLOBAL_CMD_TABLE)
|
2008-06-17 00:38:59 +00:00
|
|
|
else:
|
|
|
|
|
# Not logged in, look through the unlogged-in command table.
|
2008-12-15 01:10:26 +00:00
|
|
|
command_table_lookup(command, cmdtable.GLOBAL_UNCON_CMD_TABLE,
|
|
|
|
|
eval_perms=False)
|
2008-06-17 00:38:59 +00:00
|
|
|
|
2008-12-15 01:10:26 +00:00
|
|
|
"""
|
|
|
|
|
By this point, we assume that the user has entered a command and not
|
|
|
|
|
something like a channel or exit. Make sure that the command's
|
|
|
|
|
function reference is value and try to run it.
|
|
|
|
|
"""
|
|
|
|
|
if callable(command.command_function):
|
2008-06-17 00:38:59 +00:00
|
|
|
try:
|
2008-12-15 01:10:26 +00:00
|
|
|
# Move to the command function, passing the command object.
|
|
|
|
|
command.command_function(command)
|
2008-06-17 00:38:59 +00:00
|
|
|
except:
|
2008-12-15 01:10:26 +00:00
|
|
|
"""
|
|
|
|
|
This is a crude way of trapping command-related exceptions
|
|
|
|
|
and showing them to the user and server log. Once the
|
|
|
|
|
codebase stabilizes, we will probably want something more
|
|
|
|
|
useful or give them the option to hide exception values.
|
|
|
|
|
"""
|
|
|
|
|
session.msg("Untrapped error, please file a bug report:\n%s" %
|
|
|
|
|
(format_exc(),))
|
2008-06-17 00:38:59 +00:00
|
|
|
logger.log_errmsg("Untrapped error, evoker %s: %s" %
|
|
|
|
|
(session, format_exc()))
|
2008-12-15 01:10:26 +00:00
|
|
|
# Prevent things from falling through to UnknownCommand.
|
|
|
|
|
raise ExitCommandHandler
|
2009-01-15 02:39:11 +00:00
|
|
|
else:
|
|
|
|
|
# If we reach this point, we haven't matched anything.
|
|
|
|
|
raise UnknownCommand
|
2007-05-11 15:23:27 +00:00
|
|
|
|
2008-12-15 01:10:26 +00:00
|
|
|
except ExitCommandHandler:
|
|
|
|
|
# When this is thrown, just get out and do nothing. It doesn't mean
|
|
|
|
|
# something bad has happened.
|
|
|
|
|
pass
|
2008-06-17 00:38:59 +00:00
|
|
|
except UnknownCommand:
|
2008-12-15 01:10:26 +00:00
|
|
|
# Default fall-through. No valid command match.
|
2008-06-17 00:38:59 +00:00
|
|
|
session.msg("Huh? (Type \"help\" for help.)")
|
2007-05-11 15:23:27 +00:00
|
|
|
|