From c76cc82dc26a1472ab88aa483bb53a7e5545a356 Mon Sep 17 00:00:00 2001 From: Toddler Howard <47868518+RaveMaster073@users.noreply.github.com> Date: Tue, 4 Feb 2020 16:28:19 -0800 Subject: [PATCH] Update all commands to include Constants.assert_command() lines (#39) * Update all commands to include Constants.assert_command() lines. * Update all commands to include Constants.assert_command() lines and fix some docstrings. * Fix incorrectly fixed line that was accidentally removed. * Fix /handicap. * Fix /ungimp. * Fix segment_version in tsuserver.py. * Delete needless line in /passage_clear. * Fix errors found through test.py. Co-authored-by: Chrezm <42015761+Chrezm@users.noreply.github.com> --- server/commands.py | 419 +++++++++++++++----------------------------- server/tsuserver.py | 4 +- 2 files changed, 143 insertions(+), 280 deletions(-) diff --git a/server/commands.py b/server/commands.py index 73a24adc..ea544bad 100644 --- a/server/commands.py +++ b/server/commands.py @@ -125,12 +125,12 @@ def ooc_cmd_area_kick(client: ClientManager.Client, arg: str): /area_kick 3 Class Trial Room,\ 2 :: Kicks the player whose client ID is 1 to Class Trial Room, 2 (note the ,\). """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True, parameters='&1-2', split_spaces=True) + arg = arg.split(' ') + if not client.is_mod and not client.is_cm and client.area.lobby_area: raise ClientError('You must be authorized to kick clients in lobby areas.') - arg = arg.split(' ') if len(arg) == 1: area = client.server.area_manager.get_area_by_id(client.server.default_area) else: @@ -188,8 +188,7 @@ def ooc_cmd_area_list(client: ClientManager.Client, arg: str): /area_list :: Reset the area list to its original value. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # lists which areas are locked before the reload old_locked_areas = [area.name for area in client.server.area_manager.areas if area.is_locked] @@ -246,10 +245,7 @@ def ooc_cmd_area_lists(client: ClientManager.Client, arg: str): /area_lists :: Return all available area lists """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_mod=True, parameters='=0') try: with Constants.fopen('config/area_lists.yaml', 'r') as f: @@ -277,8 +273,7 @@ def ooc_cmd_autopass(client: ClientManager.Client, arg: str): /autopass """ - if len(arg) != 0: - raise ArgumentError("This command has no arguments.") + Constants.assert_command(client, arg, parameters='=0') client.autopass = not client.autopass status = {False: 'off', True: 'on'} @@ -413,7 +408,9 @@ def ooc_cmd_bg(client: ClientManager.Client, arg: str): /bg Principal's Room :: Changes background to Principal's Room """ - if len(arg) == 0: + try: + Constants.assert_command(client, arg, parameters='>0') + except ArgumentError: raise ArgumentError('You must specify a name. Use /bg .') if not client.is_mod and client.area.bg_lock: raise AreaError("This area's background is locked.") @@ -438,10 +435,7 @@ def ooc_cmd_bglock(client: ClientManager.Client, arg: str): /bglock """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_mod=True, parameters='=0') client.area.bg_lock = not client.area.bg_lock client.area.broadcast_ooc('A mod has set the background lock to {}.' @@ -480,9 +474,8 @@ def ooc_cmd_bilock(client: ClientManager.Client, arg: str): and Class Trial Room, 2 (note the ,\ in the command. """ + Constants.assert_command(client, arg, parameters='&1-2', split_commas=True) areas = arg.split(', ') - if len(areas) > 2 or arg == '': - raise ArgumentError('This command takes one or two arguments.') if len(areas) == 2 and not client.is_staff(): raise ClientError('You must be authorized to use the two-parameter version of this command.') @@ -569,8 +562,7 @@ def ooc_cmd_blockdj(client: ClientManager.Client, arg: str): /blockdj 1234567890 :: Revokes DJ permissions to all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_officer=True) # Block DJ permissions to matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -817,8 +809,7 @@ def ooc_cmd_bloodtrail_set(client: ClientManager.Client, arg: str): while there being no blood in the current area. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if not arg: areas_to_link = [client.area] @@ -931,10 +922,7 @@ def ooc_cmd_can_iniswap(client: ClientManager.Client, arg: str): /can_iniswap """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command takes no arguments.') + Constants.assert_command(client, arg, is_mod=True, parameters='=0') status = {True: 'now', False: 'no longer'} client.area.iniswap_allowed = not client.area.iniswap_allowed @@ -952,7 +940,7 @@ def ooc_cmd_can_passagelock(client: ClientManager.Client, arg: str): /bilock, or implicitly in /unilock if ability is turned off (but can be used explicitly). SYNTAX - /toggle_passagelock + /can_passagelock PARAMETERS None @@ -963,10 +951,7 @@ def ooc_cmd_can_passagelock(client: ClientManager.Client, arg: str): /can_passagelock :: Non-staff members can now use /bilock and /unilock again. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') client.area.change_reachability_allowed = not client.area.change_reachability_allowed status = {True: 'enabled', False: 'disabled'} @@ -989,10 +974,7 @@ def ooc_cmd_can_rollp(client: ClientManager.Client, arg: str): /can_rollp """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') client.area.rollp_allowed = not client.area.rollp_allowed status = {False: 'disabled', True: 'enabled'} @@ -1017,10 +999,7 @@ def ooc_cmd_can_rpgetarea(client: ClientManager.Client, arg: str): /can_rpgetarea """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') client.area.rp_getarea_allowed = not client.area.rp_getarea_allowed status = {False: 'disabled', True: 'enabled'} @@ -1045,10 +1024,7 @@ def ooc_cmd_can_rpgetareas(client: ClientManager.Client, arg: str): /can_rpgetareas """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') client.area.rp_getareas_allowed = not client.area.rp_getareas_allowed status = {False: 'disabled', True: 'enabled'} @@ -1110,9 +1086,9 @@ def ooc_cmd_char_restrict(client: ClientManager.Client, arg: str): /char_restrict Phantom_HD :: Unrestrict the use of Phantom_HD. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) == 0: + try: + Constants.assert_command(client, arg, is_staff=True, parameters='>0') + except ArgumentError: raise ArgumentError('This command takes one character name.') if arg not in client.server.char_list: @@ -1166,8 +1142,7 @@ def ooc_cmd_chars_restricted(client: ClientManager.Client, arg: str): /chars_restricted :: Returns 'Phantom_HD' """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') info = '== Characters restricted in area {} =='.format(client.area.name) # If no characters restricted, print a manual message. @@ -1194,8 +1169,7 @@ def ooc_cmd_cleardoc(client: ClientManager.Client, arg: str): /cleardoc """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') client.area.broadcast_ooc('{} cleared the doc link.'.format(client.displayname)) logger.log_server('[{}][{}]Cleared document. Old link: {}' @@ -1216,11 +1190,8 @@ def ooc_cmd_cleargm(client: ClientManager.Client, arg: str): /cleargm """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') - + Constants.assert_command(client, arg, is_officer=True, parameters='=0') + for area in client.server.area_manager.areas: for c in [x for x in area.clients if x.is_gm]: c.is_gm = False @@ -1272,15 +1243,11 @@ def ooc_cmd_clock(client: ClientManager.Client, arg: str): /clock 0 5 10 19 :: Start a 10-second hour clock spanning areas 0 through 5, with starting hour 7 p.m. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - - # Perform input validation first - try: - pre_area_1, pre_area_2, pre_hour_length, pre_hour_start = arg.split(' ') - except ValueError: - raise ClientError('This command takes four arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=4', split_spaces=True) + # Inputs already validated, move on + pre_area_1, pre_area_2, pre_hour_length, pre_hour_start = arg.split(' ') + areas = Constants.parse_two_area_names(client, [pre_area_1, pre_area_2], check_valid_range=True) area_1, area_2 = areas[0].id, areas[1].id @@ -1336,8 +1303,7 @@ def ooc_cmd_clock_cancel(client: ClientManager.Client, arg: str): /clock_cancel 0 :: Cancels the day cycle established by the player whose client ID is 0. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if len(arg) == 0: arg = str(client.id) @@ -1368,8 +1334,7 @@ def ooc_cmd_clock_pause(client: ClientManager.Client, arg: str): /clock_pause 0 :: Pauses the day cycle established by the player whose client ID is 0. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if len(arg) == 0: arg = str(client.id) @@ -1407,8 +1372,7 @@ def ooc_cmd_clock_unpause(client: ClientManager.Client, arg: str): /clock_unpause 0 :: Unpauses the day cycle established by the player whose client ID is 0. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if len(arg) == 0: arg = str(client.id) @@ -1526,8 +1490,7 @@ def ooc_cmd_currentmusic(client: ClientManager.Client, arg: str): /currentmusic """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') if client.area.current_music == '': raise ClientError('There is no music currently playing.') @@ -1584,10 +1547,7 @@ def ooc_cmd_defaultarea(client: ClientManager.Client, arg: str): /defaultarea 1 :: Set area 1 to be the default area. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) == 0: - raise ClientError('This command takes one argument.') + Constants.assert_command(client, arg, is_mod=True, parameters='=1') try: client.server.area_manager.get_area_by_id(int(arg)) @@ -1597,7 +1557,7 @@ def ooc_cmd_defaultarea(client: ClientManager.Client, arg: str): raise ClientError('ID {} does not correspond to a valid area ID.'.format(arg)) client.server.default_area = int(arg) - client.send_ooc('Set default area to {}'.format(arg)) + client.send_ooc('Set default area to {}.'.format(arg)) def ooc_cmd_dicelog(client: ClientManager.Client, arg: str): """ (STAFF ONLY) @@ -1691,8 +1651,7 @@ def ooc_cmd_disemconsonant(client: ClientManager.Client, arg: str): /disemconsonant 1234567890 :: Disemconsonants all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # Disemconsonant matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -1721,8 +1680,7 @@ def ooc_cmd_disemvowel(client: ClientManager.Client, arg: str): /disemvowel 1234567890 :: Disemwvowels all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # Disemvowel matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -1849,8 +1807,7 @@ def ooc_cmd_follow(client: ClientManager.Client, arg: str): /follow 1 :: Starts following the player whose client ID is 1 """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if client.party: raise PartyError('You cannot follow someone while in a party.') @@ -1966,8 +1923,7 @@ def ooc_cmd_getareas(client: ClientManager.Client, arg: str): /getareas """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') if not client.is_staff() and client.is_blind: raise ClientError('You are blind, so you cannot see anything.') @@ -1994,8 +1950,7 @@ def ooc_cmd_gimp(client: ClientManager.Client, arg: str): /gimp 1234567890 :: Gimps all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # Gimp matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -2117,10 +2072,7 @@ def ooc_cmd_gmlock(client: ClientManager.Client, arg: str): /gmlock :: Sets the current area as accessible only to staff members. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') if not client.area.locking_allowed: raise ClientError('Area locking is disabled in this area.') @@ -2244,16 +2196,8 @@ def ooc_cmd_handicap(client: ClientManager.Client, arg: str): which will not send notifications once the timer expires. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - + Constants.assert_command(client, arg, is_staff=True, parameters='&2-4', split_spaces=True) args = arg.split(' ') - if len(args) < 2: - raise ClientError('This command variation takes at least two parameters ' - '(target and length).') - if len(args) >= 5: - raise ClientError('This command variation takes at most four parameters ' - '(target, length, name, announce_if_over).') # Obtain targets targets = Constants.parse_id_or_ipid(client, args[0]) @@ -2374,12 +2318,9 @@ def ooc_cmd_iclock(client: ClientManager.Client, arg: str): /iclock """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') if not (client.is_mod or client.is_cm) and (client.is_gm and not client.area.gm_iclock_allowed): raise ClientError('GMs are not authorized to change IC locks in this area.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') client.area.ic_lock = not client.area.ic_lock status = {True: 'locked', False: 'unlocked'} @@ -2506,8 +2447,7 @@ def ooc_cmd_kick(client: ClientManager.Client, arg: str): /kick 1234567890 :: Kick all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_officer=True) # Kick matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -2535,8 +2475,7 @@ def ooc_cmd_kickself(client: ClientManager.Client, arg: str): /kickself """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') for target in client.get_multiclients(): if target != client: @@ -2565,8 +2504,7 @@ def ooc_cmd_knock(client: ClientManager.Client, arg: str): /knock Courtroom, 2 :: Knock the door to area "Courtroom, 2" """ - if len(arg) == 0: - raise ArgumentError('This command takes one argument.') + Constants.assert_command(client, arg, parameters='=1') # Get area by either name or ID try: @@ -2638,10 +2576,7 @@ def ooc_cmd_lasterror(client: ClientManager.Client, arg: str): Note: yes, this is an error message that was created while testing this command. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_mod=True, parameters='=0') if not client.server.last_error: raise ClientError('No error messages have been raised and not been caught since server bootup.') @@ -2669,7 +2604,9 @@ def ooc_cmd_lights(client: ClientManager.Client, arg: str): /lights on :: Turns on lights """ - if len(arg) == 0: + try: + Constants.assert_command(client, arg, parameters='>0') + except ArgumentError: raise ArgumentError('You must specify either on or off.') if arg not in ['off', 'on']: raise ClientError('Expected on or off.') @@ -2726,8 +2663,7 @@ def ooc_cmd_lock(client: ClientManager.Client, arg: str): /lock """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') if not client.area.locking_allowed: raise ClientError('Area locking is disabled in this area.') @@ -2873,8 +2809,7 @@ def ooc_cmd_look(client: ClientManager.Client, arg: str): /look :: Returns "Literally a courtroom" """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') if not client.is_staff() and client.is_blind: raise ClientError('You are blind, so you cannot see anything.') if not client.is_staff() and not client.area.lights: @@ -2899,8 +2834,7 @@ def ooc_cmd_look_clean(client: ClientManager.Client, arg: str): /look_clean 3, Class Trial Room,\ 2 :: Restores the default area descriptions in area 3 and Class Trial Room, 2 (note the ,\) """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if len(arg) == 0: areas_to_clean = [client.area] @@ -2950,10 +2884,7 @@ def ooc_cmd_look_list(client: ClientManager.Client, arg: str): *(0) Basement: Not a courtroom """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') info = '== Areas in this server with custom descriptions ==' # Get all areas with changed descriptions @@ -2987,8 +2918,7 @@ def ooc_cmd_look_set(client: ClientManager.Client, arg: str): /look_set :: Sets the area description in area 0 to be the default "A courtroom". """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if len(arg) == 0: client.area.description = client.area.default_description @@ -3027,8 +2957,7 @@ def ooc_cmd_make_gm(client: ClientManager.Client, arg: str): /make_gm 3 :: Makes the client with ID 3 a GM """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_officer=True) target = Constants.parse_id(client, arg) if target.is_gm: @@ -3053,8 +2982,7 @@ def ooc_cmd_minimap(client: ClientManager.Client, arg: str): /minimap """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') info = '== Areas reachable from {} =='.format(client.area.name) try: @@ -3094,13 +3022,10 @@ def ooc_cmd_modlock(client: ClientManager.Client, arg: str): /modlock :: Sets the current area as accessible only to staff members. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_mod=True, parameters='=0') if not client.area.locking_allowed: - raise ClientError('Area locking is disabled in this area') + raise ClientError('Area locking is disabled in this area.') if client.area.is_modlocked: raise ClientError('Area is already mod-locked.') @@ -3218,8 +3143,7 @@ def ooc_cmd_music_lists(client: ClientManager.Client, arg: str): /music_lists :: Return all available music lists """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') try: with Constants.fopen('config/music_lists.yaml', 'r') as f: @@ -3253,8 +3177,7 @@ def ooc_cmd_mute(client: ClientManager.Client, arg: str): /mute 1234567890 :: Mutes all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_officer=True) # Mute matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -3298,9 +3221,9 @@ def ooc_cmd_ooc_mute(client: ClientManager.Client, arg: str): /ooc_mute Aba :: Mutes from OOC the player whose username is Aba """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') - if len(arg) == 0: + try: + Constants.assert_command(client, arg, is_officer=True, parameters='>0') + except ArgumentError: raise ArgumentError('You must specify a target. Use /ooc_mute .') targets = client.server.client_manager.get_targets(client, TargetType.OOC_NAME, arg, False) @@ -3328,10 +3251,10 @@ def ooc_cmd_ooc_unmute(client: ClientManager.Client, arg: str): /ooc_unmute Aba :: Unmutes from OOC the player whose username is Aba """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') - if len(arg) == 0: - raise ArgumentError('You must specify a target. Use /ooc_mute .') + try: + Constants.assert_command(client, arg, is_officer=True, parameters='>0') + except ArgumentError: + raise ArgumentError('You must specify a target. Use /ooc_unmute .') targets = client.server.client_manager.get_targets(client, TargetType.OOC_NAME, arg, False) if not targets: @@ -3734,8 +3657,6 @@ def ooc_cmd_passage_clear(client: ClientManager.Client, arg: str): /passage_clear /passage_clear , - /passage_restore , - PARAMETERS : Start of area range (inclusive) : End of area range (inclusive) @@ -3746,11 +3667,8 @@ def ooc_cmd_passage_clear(client: ClientManager.Client, arg: str): /passage_clear 16, 116 :: Restores the passages starting in areas 16 through 116. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True, parameters='<3', split_commas=True) areas = arg.split(', ') - if len(areas) > 2: - raise ClientError('This command takes at most two arguments.') areas = Constants.parse_two_area_names(client, areas) @@ -3785,12 +3703,9 @@ def ooc_cmd_passage_restore(client: ClientManager.Client, arg: str): /passage_restore 16, 116 :: Restores the passages starting in areas 16 through 116. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True, parameters='<3', split_commas=True) areas = arg.split(', ') - if len(areas) > 2: - raise ClientError('This command takes at most two arguments.') - + areas = Constants.parse_two_area_names(client, areas) for i in range(areas[0].id, areas[1].id+1): @@ -3804,6 +3719,7 @@ def ooc_cmd_passage_restore(client: ClientManager.Client, arg: str): client.send_ooc('Passages in areas {} through {} have been restored to their original ' 'state.'.format(areas[0].name, areas[1].name)) + def ooc_cmd_ping(client: ClientManager.Client, arg: str): """ Returns "Pong" and nothing else. Useful to check if the player has lost connection. @@ -3867,12 +3783,14 @@ def ooc_cmd_pm(client: ClientManager.Client, arg: str): /pm Santa_HD Sad :: Sends that message to the user whose character name is Santa_HD. """ + try: + Constants.assert_command(client, arg, parameters='>1') + except ArgumentError: + raise ArgumentError('Not enough arguments. Use /pm . Target should be ' + 'ID, char-name, edited-to character, custom showname or OOC-name.') if client.pm_mute: raise ClientError('You have muted all PM conversations.') args = arg.split() - if len(args) < 2: - raise ArgumentError('Not enough arguments. Use /pm . Target should be ' - 'ID, char-name, edited-to character, custom showname or OOC-name.') cm = client.server.client_manager target, recipient, msg = cm.get_target_public(client, arg) @@ -4033,11 +3951,8 @@ def ooc_cmd_refresh(client: ClientManager.Client, arg: str): /refresh """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) > 0: - raise ArgumentError('This command has no arguments.') - + Constants.assert_command(client, arg, is_mod=True, parameters='=0') + client.server.reload() client.send_ooc('You have reloaded the server.') @@ -4055,8 +3970,7 @@ def ooc_cmd_reload(client: ClientManager.Client, arg: str): /reload """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') client.reload_character() client.send_ooc('Character reloaded.') @@ -4078,10 +3992,7 @@ def ooc_cmd_reload_commands(client: ClientManager.Client, arg: str): /reload_commands """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) > 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_mod=True, parameters='=0') outcome = client.server.reload_commands() if outcome: @@ -4112,8 +4023,7 @@ def ooc_cmd_remove_h(client: ClientManager.Client, arg: str): /remove_h 1234567890 :: Has all messages sent by all clients opened by the user whose IPID is 1234567890 have their H's removed. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # Removes H's to matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -4144,8 +4054,7 @@ def ooc_cmd_reveal(client: ClientManager.Client, arg: str): /reveal 1234567890 :: Set all clients opened by the user whose IPID is 1234567890 to no longer be sneaking. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) # Unsneak matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -4292,12 +4201,12 @@ def ooc_cmd_rpmode(client: ClientManager.Client, arg: str): /rpmode off :: Turns off RP mode """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + try: + Constants.assert_command(client, arg, is_staff=True, parameters='=1') + except ArgumentError: + raise ArgumentError('You must specify either on or off.') if not client.server.config['rp_mode_enabled']: raise ClientError("RP mode is disabled in this server.") - if len(arg) == 0: - raise ArgumentError('You must specify either on or off.') if arg == 'on': client.server.rp_mode = True @@ -4333,10 +4242,12 @@ def ooc_cmd_scream(client: ClientManager.Client, arg: str): /scream Hello World :: Sends Hello World to users in scream reachable areas+staff. """ + try: + Constants.assert_command(client, arg, parameters='>0') + except ArgumentError: + raise ArgumentError('You cannot send an empty message.') if client.muted_global: raise ClientError('You have the global chat muted.') - if not arg: - raise ArgumentError("You cannot send an empty message.") if not client.is_gagged: client.send_ooc('You screamed "{}".'.format(arg)) @@ -4390,10 +4301,7 @@ def ooc_cmd_scream_range(client: ClientManager.Client, arg: str): /scream_range :: Obtain the current area's scream range, for example {'Basement'} """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') info = '== Areas in scream range of area {} =='.format(client.area.name) # If no areas in scream range, print a manual message. @@ -4427,10 +4335,7 @@ def ooc_cmd_scream_set(client: ClientManager.Client, arg: str): /scream_set 2 :: Removes "Class Trial Room, 2" from the scream range of Basement. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) == 0: - raise ArgumentError('This command takes one area name.') + Constants.assert_command(client, arg, is_staff=True, parameters='=1') # This should just return a list with one area intended_area = Constants.parse_area_names(client, arg.split(', ')) @@ -4486,8 +4391,7 @@ def ooc_cmd_scream_set_range(client: ClientManager.Client, arg: str): /scream_set_range :: Sets Basement's scream range to no areas. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if len(arg) == 0: client.area.scream_range = set() @@ -4530,8 +4434,7 @@ def ooc_cmd_shoutlog(client: ClientManager.Client, arg: str): *Sat Jun 29 13:16:41 2019 | [1] Phantom (1234567890) used shout 3 with the message: u wrong m9 """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) if len(arg) == 0: arg = client.area.name @@ -4683,19 +4586,16 @@ def ooc_cmd_showname_freeze(client: ClientManager.Client, arg: str): /showname_freeze :: Unfreezes all shownames. Everyone can use /showname again. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_mod=True, parameters='=0') client.server.showname_freeze = not client.server.showname_freeze status = {False: 'unfrozen', True: 'frozen'} client.send_ooc('You have {} all shownames.'.format(status[client.server.showname_freeze])) client.send_ooc_others('A mod has {} all shownames.' - .format(status[client.server.showname_freeze]), is_mod=False) + .format(status[client.server.showname_freeze]), is_staff=False) client.send_ooc_others('{} has {} all shownames.' - .format(client.name, status[client.server.showname_freeze]), is_mod=True) + .format(client.name, status[client.server.showname_freeze]), is_staff=True) logger.log_server('{} has {} all shownames.' .format(client.name, status[client.server.showname_freeze]), client) @@ -4730,8 +4630,7 @@ def ooc_cmd_showname_history(client: ClientManager.Client, arg: str): *Sat Jun 1 18:54:46 2019 | Was cleared """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # Obtain matching targets's showname history for c in Constants.parse_id_or_ipid(client, arg): @@ -4752,18 +4651,15 @@ def ooc_cmd_showname_nuke(client: ClientManager.Client, arg: str): /showname_nuke :: Clears all shownames """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_mod=True, parameters='=0') for c in client.server.client_manager.clients: if not c.is_staff() and c.showname != '': c.change_showname('') client.send_ooc('You have nuked all shownames.') - client.send_ooc_others('A mod has nuked all shownames.', is_mod=False) - client.send_ooc_others('{} has nuked all shownames.'.format(client.name), is_mod=True) + client.send_ooc_others('A mod has nuked all shownames.', is_staff=False) + client.send_ooc_others('{} has nuked all shownames.'.format(client.name), is_staff=True) logger.log_server('{} has nuked all shownames.'.format(client.name), client) def ooc_cmd_showname_set(client: ClientManager.Client, arg: str): @@ -4790,8 +4686,7 @@ def ooc_cmd_showname_set(client: ClientManager.Client, arg: str): /showname_set 1234567890 :: Clears the showname of the client(s) whose IPID is 1234567890. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) try: separator = arg.index(" ") @@ -4856,8 +4751,7 @@ def ooc_cmd_sneak(client: ClientManager.Client, arg: str): /sneak 1234567890 :: Set all clients opened by the user whose IPID is 1234567890 to be sneaking. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) # Sneak matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -4917,8 +4811,7 @@ def ooc_cmd_st(client: ClientManager.Client, arg: str): /st Need help in area 0. :: Sends "Need help in area 0" to all staff members. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) pre = '{} [Staff] {}'.format(client.server.config['hostname'], client.name) client.server.send_all_cmd_pred('CT', pre, arg, pred=lambda c: c.is_staff()) @@ -4940,7 +4833,9 @@ def ooc_cmd_switch(client: ClientManager.Client, arg: str): /switch Phoenix_HD """ - if len(arg) == 0: + try: + Constants.assert_command(client, arg, parameters='>0') + except ArgumentError: raise ArgumentError('You must specify a character name.') # Obtain char_id if character exists and then try and change to given char if available @@ -4962,8 +4857,7 @@ def ooc_cmd_time(client: ClientManager.Client, arg: str): /time :: May return something like "Sat Apr 27 09:04:17 2019" """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') client.send_ooc(Constants.get_time()) @@ -4982,8 +4876,7 @@ def ooc_cmd_time12(client: ClientManager.Client, arg: str): /time12 :: May return something like "Sat Apr 27 09:04:47 AM (EST) 2019" """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') client.send_ooc(time.strftime('%a %b %e %I:%M:%S %p (%z) %Y')) @@ -5001,8 +4894,7 @@ def ooc_cmd_time_est(client, arg): /time_est :: May return something like "Sat Apr 27 09:04:47 AM 2019" """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') est = datetime.datetime.utcnow() - datetime.timedelta(hours=5) client.send_ooc(est.strftime("%a %b %e %I:%M:%S %p %Y")) @@ -5038,11 +4930,8 @@ def ooc_cmd_timer(client: ClientManager.Client, arg: str): anyone at any area. """ - if len(arg) == 0: - raise ClientError('This command takes at least one argument.') + Constants.assert_command(client, arg, parameters='&1-3', split_spaces=True) arg = arg.split(' ') - if len(arg) >= 4: - raise ClientError('This command takes at most three arguments.') # Check if valid length and convert to seconds length = Constants.parse_time_length(arg[0]) # Also internally validates @@ -5089,11 +4978,8 @@ def ooc_cmd_timer_cancel(client: ClientManager.Client, arg: str): /timer_cancel P :: Only Phantom would cancel timer P if either ran this. """ - if len(arg) == 0: - raise ArgumentError('Expected timer name.') + Constants.assert_command(client, arg, parameters='=1', split_spaces=True) arg = arg.split(' ') - if len(arg) != 1: - raise ClientError('This command variation takes exactly one argument.') timer_name = arg[0] try: @@ -5127,11 +5013,10 @@ def ooc_cmd_timer_get(client: ClientManager.Client, arg: str): /timer_get P :: Spam, Phantom and Eggs would get the remaining time of P. /timer_get E :: Spam, Phantom and Eggs would get the remaining time of E. """ - + + Constants.assert_command(client, arg, parameters='<2') arg = arg.split(' ') if arg else list() - if len(arg) > 1: - raise ClientError('This command takes at most one argument.') - + string_of_timers = "" if len(arg) == 1: @@ -5192,8 +5077,7 @@ def ooc_cmd_ToD(client: ClientManager.Client, arg: str): /ToD :: May return something like 'Phoenix_HD has to do a truth.' """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') coin = ['truth', 'dare'] flip = random.choice(coin) @@ -5216,10 +5100,7 @@ def ooc_cmd_toggle_allrolls(client: ClientManager.Client, arg: str): /toggle_allrolls """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') client.get_foreign_rolls = not client.get_foreign_rolls status = {False: 'no longer', True: 'now'} @@ -5244,8 +5125,7 @@ def ooc_cmd_toggle_fp(client: ClientManager.Client, arg: str): /toggle_fp :: Toggles first person mode off. """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') client.first_person = not client.first_person status = {True: 'now', False: 'no longer'} @@ -5311,8 +5191,7 @@ def ooc_cmd_toggle_shownames(client: ClientManager.Client, arg: str): /toggle_shownames :: All subsequent messages will include the shownames of the senders if they have one. """ - if len(arg) != 0: - raise ArgumentError("This command has no arguments.") + Constants.assert_command(client, arg, parameters='=0') client.show_shownames = not client.show_shownames status = {False: 'off', True: 'on'} @@ -5342,9 +5221,8 @@ def ooc_cmd_transient(client: ClientManager.Client, arg: str): /transient 1234567890 :: This player can now only access only reachable areas. """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - + Constants.assert_command(client, arg, is_staff=True) + # Invert current transient status of matching targets status = {False: 'no longer', True: 'now'} for c in Constants.parse_id_or_ipid(client, arg): @@ -5445,8 +5323,7 @@ def ooc_cmd_unblockdj(client: ClientManager.Client, arg: str): /unblockdj 1234567890 :: Restores DJ permissions to all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_officer=True) # Restore DJ permissions to matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -5476,8 +5353,7 @@ def ooc_cmd_undisemconsonant(client: ClientManager.Client, arg: str): /undisemconsonant 1234567890 :: Undisemconsonants all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # Undisemconsonant matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -5507,8 +5383,7 @@ def ooc_cmd_undisemvowel(client: ClientManager.Client, arg: str): /undisemvowel 1234567890 :: Undisemvowel all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # Undisemvowel matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -5532,10 +5407,7 @@ def ooc_cmd_unfollow(client: ClientManager.Client, arg: str): /unfollow :: Stops following the player """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') client.unfollow_user() @@ -5561,9 +5433,8 @@ def ooc_cmd_ungimp(client: ClientManager.Client, arg: str): /ungimp 1234567890 :: Gimps all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') - + Constants.assert_command(client, arg, is_mod=True) + # Ungimp matching targets for c in Constants.parse_id_or_ipid(client, arg): c.gimp = False @@ -5585,10 +5456,7 @@ def ooc_cmd_unglobalic(client: ClientManager.Client, arg: str): /unglobalic :: Send subsequent messages normally (only to users in current area). """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, is_staff=True, parameters='=0') client.multi_ic = None client.send_ooc('Your IC messages will now be only sent to your current area.') @@ -5617,8 +5485,7 @@ def ooc_cmd_unhandicap(client: ClientManager.Client, arg: str): /unhandicap 1234567890 :: Removes all movement handicaps on the clients whose IPID is 1234567890 """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) # Obtain targets for c in Constants.parse_id_or_ipid(client, arg): @@ -5667,9 +5534,8 @@ def ooc_cmd_unilock(client: ClientManager.Client, arg: str): (note the ,\ in the command). """ + Constants.assert_command(client, arg, parameters='&1-2', split_commas=True) areas = arg.split(', ') - if len(areas) > 2 or arg == '': - raise ArgumentError('This command takes one or two arguments.') if len(areas) == 2 and not client.is_staff(): raise ClientError('You must be authorized to use the two-parameter version of this command.') @@ -5764,8 +5630,7 @@ def ooc_cmd_unlock(client: ClientManager.Client, arg: str): /unlock :: Perform one of the unlocks described above if possible """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') if not client.area.is_locked and not client.area.is_modlocked and not client.area.is_gmlocked: raise ClientError('Area is already open.') @@ -5801,8 +5666,7 @@ def ooc_cmd_unmute(client: ClientManager.Client, arg: str): /unmute 1234567890 :: Unmutes all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod and not client.is_cm: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_officer=True) # Mute matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -5832,8 +5696,7 @@ def ooc_cmd_unremove_h(client: ClientManager.Client, arg: str): /unremove_h 1234567890 :: Removes the 'Remove H' effect on all clients opened by the user whose IPID is 1234567890. """ - if not client.is_mod: - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_mod=True) # Remove the 'Remove H' effect on matching targets for c in Constants.parse_id_or_ipid(client, arg): @@ -5855,8 +5718,7 @@ def ooc_cmd_version(client: ClientManager.Client, arg: str): /version :: May return something like: This server is running TsuserverDR 4.0.0 (190801a) """ - if len(arg) != 0: - raise ArgumentError('This command has no arguments.') + Constants.assert_command(client, arg, parameters='=0') client.send_ooc('This server is running {}.'.format(client.server.version)) @@ -5883,8 +5745,7 @@ def ooc_cmd_whereis(client: ClientManager.Client, arg: str): /whereis 1234567890 :: May return something like this: Client 1 (1234567890) is in Basement (0) """ - if not client.is_staff(): - raise ClientError('You must be authorized to do that.') + Constants.assert_command(client, arg, is_staff=True) for c in Constants.parse_id_or_ipid(client, arg): client.send_ooc("Client {} ({}) is in {} ({})." @@ -6233,9 +6094,11 @@ def ooc_cmd_zone_global(client: ClientManager.Client, arg: str): /zone_glabal Hello World :: Sends Hello World to global chat. """ - if not arg: + try: + Constants.assert_command(client, arg, parameters='>0') + except ArgumentError: raise ArgumentError('You cannot send an empty message.') - + if client.zone_watched: target_zone = client.zone_watched elif client.area.in_zone: diff --git a/server/tsuserver.py b/server/tsuserver.py index e7693fe2..13753329 100644 --- a/server/tsuserver.py +++ b/server/tsuserver.py @@ -42,8 +42,8 @@ def __init__(self, protocol=None, client_manager=None, in_test=False): self.release = 4 self.major_version = 3 self.minor_version = 0 - self.segment_version = 'a13' - self.internal_version = '200127a' + self.segment_version = 'a14' + self.internal_version = '200204a' version_string = self.get_version_string() self.software = 'TsuserverDR {}'.format(version_string) self.version = 'TsuserverDR {} ({})'.format(version_string, self.internal_version)