From 37d66093cc5eaa1804be0f6c76a0fbe6c5152e75 Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Sun, 14 Dec 2008 01:49:37 +0000 Subject: [PATCH] Addition of a re-usable CommandTable class. We still have two global command tables that are now instances of this class. Game developers will use methods on CommandTable to add their own commands without modifying the base server code. This is also in preparation of allowing commands to be present on objects via their script parents. --- src/cmdhandler.py | 15 ++-- src/cmdtable.py | 169 +++++++++++++++++++++++++++------------------- src/logger.py | 8 +++ 3 files changed, 116 insertions(+), 76 deletions(-) diff --git a/src/cmdhandler.py b/src/cmdhandler.py index 953be9c0b8..c0a8eccf1c 100755 --- a/src/cmdhandler.py +++ b/src/cmdhandler.py @@ -35,15 +35,16 @@ def parse_command(command_string): look look - I'm not married to either of these terms, but I couldn't think of anything better. If you can, lets change it :) + I'm not married to either of these terms, but I couldn't think of anything + better. If you can, lets change it :) The only cases that I haven't handled is if someone enters something like: @pemit /= - - Ends up considering both targets as one with a space between them, and the switch as a switch. + - Ends up considering both targets as one with a space between them, + and the switch as a switch. @pemit / = - - Ends up considering the first target a target, and the second target as part of the switch. - - + - Ends up considering the first target a target, and the second + target as part of the switch. """ # Each of the bits of data starts off as None, except for the raw, original # command @@ -199,7 +200,7 @@ def handle(cdat): parsed_input['root_cmd'] = '@cemit' # Get the command's function reference (Or False) - cmdtuple = cmdtable.return_cmdtuple(parsed_input['root_cmd']) + cmdtuple = cmdtable.GLOBAL_CMD_TABLE.get_command_tuple(parsed_input['root_cmd']) if cmdtuple: # If there is a permissions element to the entry, check perms. if cmdtuple[1]: @@ -211,7 +212,7 @@ def handle(cdat): else: # Not logged in, look through the unlogged-in command table. - cmdtuple = cmdtable.return_cmdtuple(parsed_input['root_cmd'], unlogged_cmd=True) + cmdtuple = cmdtable.GLOBAL_UNCON_CMD_TABLE.get_command_tuple(parsed_input['root_cmd']) if cmdtuple: cmd = cmdtuple[0] diff --git a/src/cmdtable.py b/src/cmdtable.py index ef923e980d..b3df75922f 100644 --- a/src/cmdtable.py +++ b/src/cmdtable.py @@ -15,75 +15,106 @@ import commands.comsys import commands.unloggedin import commands.info import commands.objmanip +import logger -# -- Unlogged-in Command Table -- -# Command Name Command Function Privilege Tuple -uncon_ctable = { - "connect": (commands.unloggedin.cmd_connect, None), - "create": (commands.unloggedin.cmd_create, None), - "quit": (commands.unloggedin.cmd_quit, None), -} - - -# -- Command Table -- -# Command Name Command Function Privilege Tuple -ctable = { - "addcom": (commands.comsys.cmd_addcom, None), - "comlist": (commands.comsys.cmd_comlist, None), - "delcom": (commands.comsys.cmd_delcom, None), - "drop": (commands.general.cmd_drop, None), - "examine": (commands.general.cmd_examine, None), - "get": (commands.general.cmd_get, None), - "help": (commands.general.cmd_help, None), - "idle": (commands.general.cmd_idle, None), - "inventory": (commands.general.cmd_inventory, None), - "look": (commands.general.cmd_look, None), - "page": (commands.general.cmd_page, None), - "pose": (commands.general.cmd_pose, None), - "quit": (commands.general.cmd_quit, None), - "say": (commands.general.cmd_say, None), - "time": (commands.info.cmd_time, None), - "uptime": (commands.info.cmd_uptime, None), - "version": (commands.info.cmd_version, None), - "who": (commands.general.cmd_who, None), - "@alias": (commands.objmanip.cmd_alias, None), - "@boot": (commands.privileged.cmd_boot, ("genperms.manage_players")), - "@ccreate": (commands.comsys.cmd_ccreate, ("objects.add_commchannel")), - "@cdestroy": (commands.comsys.cmd_cdestroy, ("objects.delete_commchannel")), - "@cemit": (commands.comsys.cmd_cemit, None), - "@clist": (commands.comsys.cmd_clist, None), - "@create": (commands.objmanip.cmd_create, ("genperms.builder")), - "@describe": (commands.objmanip.cmd_description, None), - "@destroy": (commands.objmanip.cmd_destroy, ("genperms.builder")), - "@dig": (commands.objmanip.cmd_dig, ("genperms.builder")), - "@emit": (commands.general.cmd_emit, ("genperms.announce")), -# "@pemit": (commands.general.cmd_pemit, None), - "@find": (commands.objmanip.cmd_find, ("genperms.builder")), - "@link": (commands.objmanip.cmd_link, ("genperms.builder")), - "@list": (commands.info.cmd_list, ("genperms.process_control")), - "@name": (commands.objmanip.cmd_name, None), - "@nextfree": (commands.objmanip.cmd_nextfree, ("genperms.builder")), - "@newpassword": (commands.privileged.cmd_newpassword, ("genperms.manage_players")), - "@open": (commands.objmanip.cmd_open, ("genperms.builder")), - "@password": (commands.general.cmd_password, None), - "@ps": (commands.info.cmd_ps, ("genperms.process_control")), - "@reload": (commands.privileged.cmd_reload, ("genperms.process_control")), - "@set": (commands.objmanip.cmd_set, None), - "@shutdown": (commands.privileged.cmd_shutdown, ("genperms.process_control")), - "@stats": (commands.info.cmd_stats, None), - "@teleport": (commands.objmanip.cmd_teleport, ("genperms.builder")), - "@unlink": (commands.objmanip.cmd_unlink, ("genperms.builder")), - "@wall": (commands.general.cmd_wall, ("genperms.announce")), - "@wipe": (commands.objmanip.cmd_wipe, None), -} - -def return_cmdtuple(func_name, unlogged_cmd=False): +class CommandTable(object): """ - Returns a reference to the command's tuple. If there are no matches, - returns false. + Stores command tables and performs lookups. """ - if not unlogged_cmd: - cfunc = ctable.get(func_name, False) - else: - cfunc = uncon_ctable.get(func_name, False) - return cfunc + ctable = {} + + def add_command(self, command_string, function, priv_tuple=None): + """ + Adds a command to the command table. + + command_string: (string) Command string (IE: WHO, QUIT, look). + function: (reference) The command's function. + priv_tuple: (tuple) String tuple of permissions required for command. + """ + self.ctable[command_string] = (function, priv_tuple) + + def get_command_tuple(self, func_name): + """ + Returns a reference to the command's tuple. If there are no matches, + returns false. + """ + return self.ctable.get(func_name, False) + +""" +Global command table for logged in users. +""" +GLOBAL_CMD_TABLE = CommandTable() +GLOBAL_CMD_TABLE.add_command("addcom", commands.comsys.cmd_addcom), +GLOBAL_CMD_TABLE.add_command("comlist", commands.comsys.cmd_comlist), +GLOBAL_CMD_TABLE.add_command("delcom", commands.comsys.cmd_delcom), +GLOBAL_CMD_TABLE.add_command("drop", commands.general.cmd_drop), +GLOBAL_CMD_TABLE.add_command("examine", commands.general.cmd_examine), +GLOBAL_CMD_TABLE.add_command("get", commands.general.cmd_get), +GLOBAL_CMD_TABLE.add_command("help", commands.general.cmd_help), +GLOBAL_CMD_TABLE.add_command("idle", commands.general.cmd_idle), +GLOBAL_CMD_TABLE.add_command("inventory", commands.general.cmd_inventory), +GLOBAL_CMD_TABLE.add_command("look", commands.general.cmd_look), +GLOBAL_CMD_TABLE.add_command("page", commands.general.cmd_page), +GLOBAL_CMD_TABLE.add_command("pose", commands.general.cmd_pose), +GLOBAL_CMD_TABLE.add_command("quit", commands.general.cmd_quit), +GLOBAL_CMD_TABLE.add_command("say", commands.general.cmd_say), +GLOBAL_CMD_TABLE.add_command("time", commands.info.cmd_time), +GLOBAL_CMD_TABLE.add_command("uptime", commands.info.cmd_uptime), +GLOBAL_CMD_TABLE.add_command("version", commands.info.cmd_version), +GLOBAL_CMD_TABLE.add_command("who", commands.general.cmd_who), +GLOBAL_CMD_TABLE.add_command("@alias", commands.objmanip.cmd_alias), +GLOBAL_CMD_TABLE.add_command("@boot", commands.privileged.cmd_boot, + priv_tuple=("genperms.manage_players")), +GLOBAL_CMD_TABLE.add_command("@ccreate", commands.comsys.cmd_ccreate, + priv_tuple=("objects.add_commchannel")), +GLOBAL_CMD_TABLE.add_command("@cdestroy", commands.comsys.cmd_cdestroy, + priv_tuple=("objects.delete_commchannel")), +GLOBAL_CMD_TABLE.add_command("@cemit", commands.comsys.cmd_cemit), +GLOBAL_CMD_TABLE.add_command("@clist", commands.comsys.cmd_clist), +GLOBAL_CMD_TABLE.add_command("@create", commands.objmanip.cmd_create, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@describe", commands.objmanip.cmd_description), +GLOBAL_CMD_TABLE.add_command("@destroy", commands.objmanip.cmd_destroy, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@dig", commands.objmanip.cmd_dig, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@emit", commands.general.cmd_emit, + priv_tuple=("genperms.announce")), +#GLOBAL_CMD_TABLE.add_command("@pemit", commands.general.cmd_pemit, None), +GLOBAL_CMD_TABLE.add_command("@find", commands.objmanip.cmd_find, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@link", commands.objmanip.cmd_link, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@list", commands.info.cmd_list, + priv_tuple=("genperms.process_control")), +GLOBAL_CMD_TABLE.add_command("@name", commands.objmanip.cmd_name), +GLOBAL_CMD_TABLE.add_command("@nextfree", commands.objmanip.cmd_nextfree, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@newpassword", commands.privileged.cmd_newpassword, + priv_tuple=("genperms.manage_players")), +GLOBAL_CMD_TABLE.add_command("@open", commands.objmanip.cmd_open, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@password", commands.general.cmd_password), +GLOBAL_CMD_TABLE.add_command("@ps", commands.info.cmd_ps, + priv_tuple=("genperms.process_control")), +GLOBAL_CMD_TABLE.add_command("@reload", commands.privileged.cmd_reload, + priv_tuple=("genperms.process_control")), +GLOBAL_CMD_TABLE.add_command("@set", commands.objmanip.cmd_set), +GLOBAL_CMD_TABLE.add_command("@shutdown", commands.privileged.cmd_shutdown, + priv_tuple=("genperms.process_control")), +GLOBAL_CMD_TABLE.add_command("@stats", commands.info.cmd_stats), +GLOBAL_CMD_TABLE.add_command("@teleport", commands.objmanip.cmd_teleport, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@unlink", commands.objmanip.cmd_unlink, + priv_tuple=("genperms.builder")), +GLOBAL_CMD_TABLE.add_command("@wall", commands.general.cmd_wall, + priv_tuple=("genperms.announce")), +GLOBAL_CMD_TABLE.add_command("@wipe", commands.objmanip.cmd_wipe), + +""" +Global unconnected command table, for unauthenticated users. +""" +GLOBAL_UNCON_CMD_TABLE = CommandTable() +GLOBAL_UNCON_CMD_TABLE.add_command("connect", commands.unloggedin.cmd_connect) +GLOBAL_UNCON_CMD_TABLE.add_command("create", commands.unloggedin.cmd_create) +GLOBAL_UNCON_CMD_TABLE.add_command("quit", commands.unloggedin.cmd_quit) diff --git a/src/logger.py b/src/logger.py index 788165b443..a15b9492a2 100644 --- a/src/logger.py +++ b/src/logger.py @@ -15,6 +15,14 @@ def log_errmsg(errormsg): """ log.err('ERROR: %s' % (errormsg,)) +def log_warnmsg(warnmsg): + """ + Prints/logs any warnings that aren't critical but should be noted. + + warnmsg: (string) The message to be logged. + """ + log.msg('WARNING: %s' % (warnmsg,)) + def log_infomsg(infomsg): """ Prints any generic debugging/informative info that should appear in the log.