1 // =========================================================
2 // Server side networked commands code, reworked by Samual
3 // Last updated: December 28th, 2011
4 // =========================================================
6 float SV_ParseClientCommand_floodcheck()
8 if not(timeout_status) // not while paused
10 if(time <= (self.cmd_floodtime + autocvar_sv_clientcommand_antispam_time))
12 self.cmd_floodcount += 1;
13 if(self.cmd_floodcount > autocvar_sv_clientcommand_antispam_count) { return FALSE; } // too much spam, halt
17 self.cmd_floodtime = time;
18 self.cmd_floodcount = 1;
21 return TRUE; // continue, as we're not flooding yet
25 // =======================
26 // Command Sub-Functions
27 // =======================
29 void ClientCommand_autoswitch(float request, float argc)
33 case CMD_REQUEST_COMMAND:
37 self.autoswitch = InterpretBoolean(argv(1));
38 sprint(self, strcat("^1autoswitch is currently turned ", (self.autoswitch ? "on" : "off"), ".\n"));
44 sprint(self, "Incorrect parameters for ^2autoswitch^7\n");
45 case CMD_REQUEST_USAGE:
47 sprint(self, "\nUsage:^3 cmd autoswitch selection\n");
48 sprint(self, " Where 'selection' controls if autoswitch is on or off.\n");
54 void ClientCommand_checkfail(float request, string command) // internal command, used only by code
58 case CMD_REQUEST_COMMAND:
60 print(sprintf("CHECKFAIL: %s (%s) epically failed check %s\n", self.netname, self.netaddress, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1))));
62 return; // never fall through to usage
66 sprint(self, "Incorrect parameters for ^2checkfail^7\n");
67 case CMD_REQUEST_USAGE:
69 sprint(self, "\nUsage:^3 cmd checkfail <message>\n");
70 sprint(self, " Where 'message' is the message reported by client about the fail.\n");
76 void ClientCommand_clientversion(float request, float argc) // internal command, used only by code
80 case CMD_REQUEST_COMMAND:
84 if(self.flags & FL_CLIENT)
86 self.version = ((argv(1) == "$gameversion") ? 1 : stof(argv(1)));
88 if(self.version < autocvar_gameversion_min || self.version > autocvar_gameversion_max)
90 self.version_mismatch = 1;
91 ClientKill_TeamChange(-2); // observe
93 else if(autocvar_g_campaign || autocvar_g_balance_teams)
95 //JoinBestTeam(self, FALSE, TRUE);
97 else if(teamplay && !autocvar_sv_spectate && !(self.team_forced > 0))
99 self.classname = "observer"; // really?
100 stuffcmd(self, "menu_showteamselect\n");
109 sprint(self, "Incorrect parameters for ^2clientversion^7\n");
110 case CMD_REQUEST_USAGE:
112 sprint(self, "\nUsage:^3 cmd clientversion version\n");
113 sprint(self, " Where 'version' is the game version reported by self.\n");
119 void ClientCommand_mv_getpicture(float request, float argc) // internal command, used only by code
123 case CMD_REQUEST_COMMAND:
127 if(intermission_running)
128 MapVote_SendPicture(stof(argv(1)));
135 sprint(self, "Incorrect parameters for ^2mv_getpicture^7\n");
136 case CMD_REQUEST_USAGE:
138 sprint(self, "\nUsage:^3 cmd mv_getpicture mapid\n");
139 sprint(self, " Where 'mapid' is the id number of the map to request an image of on the map vote selection menu.\n");
145 void ClientCommand_join(float request)
149 case CMD_REQUEST_COMMAND:
151 if(self.flags & FL_CLIENT)
153 if(self.classname != "player" && !lockteams && !g_arena)
155 if(nJoinAllowed(self))
157 if(autocvar_g_campaign) { campaign_bots_may_start = 1; }
159 self.classname = "player";
160 PlayerScore_Clear(self);
161 bprint ("^4", self.netname, "^4 is playing now\n");
166 //player may not join because of g_maxplayers is set
167 centerprint(self, PREVENT_JOIN_TEXT);
171 return; // never fall through to usage
175 case CMD_REQUEST_USAGE:
177 sprint(self, "\nUsage:^3 cmd join\n");
178 sprint(self, " No arguments required.\n");
184 void ClientCommand_ready(float request) // todo: anti-spam for toggling readyness
188 case CMD_REQUEST_COMMAND:
190 if(self.flags & FL_CLIENT)
192 if(inWarmupStage || autocvar_sv_ready_restart || g_race_qualifying == 2)
194 if(!readyrestart_happened || autocvar_sv_ready_restart_repeatable)
196 if(time < game_starttime + 1) // game is already restarting
198 if (self.ready) // toggle
201 bprint(self.netname, "^2 is ^1NOT^2 ready\n");
206 bprint(self.netname, "^2 is ready\n");
209 // cannot reset the game while a timeout is active!
210 if not(timeout_status)
213 sprint(self, "^1Game has already been restarted\n");
217 return; // never fall through to usage
221 case CMD_REQUEST_USAGE:
223 sprint(self, "\nUsage:^3 cmd ready\n");
224 sprint(self, " No arguments required.\n");
230 void ClientCommand_say(float request, float argc, string command)
234 case CMD_REQUEST_COMMAND:
236 if(argc >= 2) { Say(self, FALSE, world, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1); }
237 return; // never fall through to usage
241 case CMD_REQUEST_USAGE:
243 sprint(self, "\nUsage:^3 cmd say <message>\n");
244 sprint(self, " Where 'message' is the string of text to say.\n");
250 void ClientCommand_say_team(float request, float argc, string command)
254 case CMD_REQUEST_COMMAND:
256 if(argc >= 2) { Say(self, TRUE, world, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), 1); }
257 return; // never fall through to usage
261 case CMD_REQUEST_USAGE:
263 sprint(self, "\nUsage:^3 cmd say_team <message>\n");
264 sprint(self, " Where 'message' is the string of text to say.\n");
270 void ClientCommand_selectteam(float request, float argc)
274 case CMD_REQUEST_COMMAND:
278 if(self.flags & FL_CLIENT)
281 if not(self.team_forced > 0)
288 case "red": selection = COLOR_TEAM1; break;
289 case "blue": selection = COLOR_TEAM2; break;
290 case "yellow": selection = COLOR_TEAM3; break;
291 case "pink": selection = COLOR_TEAM4; break;
292 case "auto": selection = (-1); break;
294 default: selection = 0; break;
299 if(self.team == selection && self.deadflag == DEAD_NO)
300 sprint(self, "^7You already are on that team.\n");
301 else if(self.wasplayer && autocvar_g_changeteam_banned)
302 sprint(self, "^1You cannot change team, forbidden by the server.\n");
304 ClientKill_TeamChange(selection);
308 sprint(self, "^7The game has already begun, you must wait until the next map to be able to join a team.\n");
310 sprint(self, "^7selectteam can not be used as your team is forced\n");
312 sprint(self, "^7selectteam can only be used in teamgames\n");
319 sprint(self, "Incorrect parameters for ^2selectteam^7\n");
320 case CMD_REQUEST_USAGE:
322 sprint(self, "\nUsage:^3 cmd selectteam team\n");
323 sprint(self, " Where 'team' is the prefered team to try and join.\n");
324 sprint(self, " Full list of options here: \"red, blue, yellow, pink, auto\"\n");
330 void ClientCommand_selfstuff(float request, string command)
334 case CMD_REQUEST_COMMAND:
338 stuffcmd(self, substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
344 sprint(self, "Incorrect parameters for ^2selectteam^7\n");
345 case CMD_REQUEST_USAGE:
347 sprint(self, "\nUsage:^3 cmd selfstuff <command>\n");
348 sprint(self, " Where 'command' is the string to be stuffed to your client.\n");
354 void ClientCommand_sentcvar(float request, float argc, string command)
358 case CMD_REQUEST_COMMAND:
365 if(argc == 2) // undefined cvar: use the default value on the server then
367 s = strcat(substring(command, argv_start_index(0), argv_end_index(1) - argv_start_index(0)), " \"", cvar_defstring(argv(1)), "\"");
378 sprint(self, "Incorrect parameters for ^2sentcvar^7\n");
379 case CMD_REQUEST_USAGE:
381 sprint(self, "\nUsage:^3 cmd sentcvar <cvar>\n");
382 sprint(self, " Where 'cvar' is the cvar plus arguments to send to the server.\n");
388 void ClientCommand_spectate(float request)
392 case CMD_REQUEST_COMMAND:
394 if(self.flags & FL_CLIENT)
396 if(g_arena) { return; }
399 if(self.lms_spectate_warning)
401 // mark player as spectator
402 PlayerScore_Add(self, SP_LMS_RANK, 666 - PlayerScore_Add(self, SP_LMS_RANK, 0));
406 self.lms_spectate_warning = 1;
407 sprint(self, "WARNING: you won't be able to enter the game again after spectating in LMS. Use the same command again to spectate anyway.\n");
412 if(self.classname == "player" && autocvar_sv_spectate == 1)
413 ClientKill_TeamChange(-2); // observe
415 // in CA, allow a dead player to move to spectators (without that, caplayer!=0 will be moved back to the player list)
416 // note: if arena game mode is ever done properly, this needs to be removed.
417 if(g_ca && self.caplayer && (self.classname == "spectator" || self.classname == "observer"))
419 sprint(self, "WARNING: you will spectate in the next round.\n");
423 return; // never fall through to usage
427 case CMD_REQUEST_USAGE:
429 sprint(self, "\nUsage:^3 cmd spectate\n");
430 sprint(self, " No arguments required.\n");
436 void ClientCommand_suggestmap(float request, float argc)
440 case CMD_REQUEST_COMMAND:
444 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
450 sprint(self, "Incorrect parameters for ^2suggestmap^7\n");
451 case CMD_REQUEST_USAGE:
453 sprint(self, "\nUsage:^3 cmd suggestmap map\n");
454 sprint(self, " Where 'map' is the name of the map to suggest.\n");
460 void ClientCommand_tell(float request, float argc, string command)
464 case CMD_REQUEST_COMMAND:
468 entity tell_to = GetIndexedEntity(argc, 1);
469 float tell_accepted = VerifyClientEntity(tell_to, TRUE, FALSE);
471 if(tell_accepted > 0) // the target is a real client
473 if(tell_to != self) // and we're allowed to send to them :D
475 Say(self, FALSE, tell_to, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)), TRUE);
478 else { print_to(self, "You can't ^2tell^7 a message to yourself."); return; }
480 else if(argv(1) == "#0")
482 trigger_magicear_processmessage_forallears(self, -1, world, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)));
485 else { print_to(self, strcat("tell: ", GetClientErrorString(tell_accepted, argv(1)), ".")); return; }
490 sprint(self, "Incorrect parameters for ^2tell^7\n");
491 case CMD_REQUEST_USAGE:
493 sprint(self, "\nUsage:^3 cmd tell client <message>\n");
494 sprint(self, " Where 'client' is the entity number or name of the player to send 'message' to.\n");
500 void ClientCommand_voice(float request, float argc, string command)
504 case CMD_REQUEST_COMMAND:
509 VoiceMessage(argv(1), substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
511 VoiceMessage(argv(1), "");
518 sprint(self, "Incorrect parameters for ^2voice^7\n");
519 case CMD_REQUEST_USAGE:
521 sprint(self, "\nUsage:^3 cmd voice messagetype <soundname>\n");
522 sprint(self, " 'messagetype' is the type of broadcast to do, like team only or such,\n");
523 sprint(self, " and 'soundname' is the string/filename of the sound/voice message to play.\n");
529 /* use this when creating a new command, making sure to place it in alphabetical order... also,
530 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
531 void ClientCommand_(float request)
535 case CMD_REQUEST_COMMAND:
538 return; // never fall through to usage
542 case CMD_REQUEST_USAGE:
544 sprint(self, "\nUsage:^3 cmd \n");
545 sprint(self, " No arguments required.\n");
553 // =====================================
554 // Macro system for networked commands
555 // =====================================
557 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
558 #define CLIENT_COMMANDS(request,arguments,command) \
559 CLIENT_COMMAND("autoswitch", ClientCommand_autoswitch(request, arguments), "Whether or not to switch automatically when getting a better weapon") \
560 CLIENT_COMMAND("checkfail", ClientCommand_checkfail(request, command), "Report if a client-side check failed") \
561 CLIENT_COMMAND("clientversion", ClientCommand_clientversion(request, arguments), "Release version of the game") \
562 CLIENT_COMMAND("mv_getpicture", ClientCommand_mv_getpicture(request, arguments), "Retrieve mapshot picture from the server") \
563 CLIENT_COMMAND("join", ClientCommand_join(request), "Become a player in the game") \
564 CLIENT_COMMAND("ready", ClientCommand_ready(request), "Qualify as ready to end warmup stage (or restart server if allowed)") \
565 CLIENT_COMMAND("say", ClientCommand_say(request, arguments, command), "Print a message to chat to all players") \
566 CLIENT_COMMAND("say_team", ClientCommand_say_team(request, arguments, command), "Print a message to chat to all team mates") \
567 CLIENT_COMMAND("selectteam", ClientCommand_selectteam(request, arguments), "Attempt to choose a team to join into") \
568 CLIENT_COMMAND("selfstuff", ClientCommand_selfstuff(request, command), "Stuffcmd a command to your own client") \
569 CLIENT_COMMAND("sentcvar", ClientCommand_sentcvar(request, arguments, command), "New system for sending a client cvar to the server") \
570 CLIENT_COMMAND("spectate", ClientCommand_spectate(request), "Become an observer") \
571 CLIENT_COMMAND("suggestmap", ClientCommand_suggestmap(request, arguments), "Suggest a map to the mapvote at match end") \
572 CLIENT_COMMAND("tell", ClientCommand_tell(request, arguments, command), "Send a message directly to a player") \
573 CLIENT_COMMAND("voice", ClientCommand_voice(request, arguments, command), "Send voice message via sound") \
576 void ClientCommand_macro_help()
578 #define CLIENT_COMMAND(name,function,description) \
579 { sprint(self, " ^2", name, "^7: ", description, "\n"); }
581 CLIENT_COMMANDS(0, 0, "")
582 #undef CLIENT_COMMAND
587 float ClientCommand_macro_command(float argc, string command)
589 #define CLIENT_COMMAND(name,function,description) \
590 { if(name == strtolower(argv(0))) { function; return TRUE; } }
592 CLIENT_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
593 #undef CLIENT_COMMAND
598 float ClientCommand_macro_usage(float argc)
600 #define CLIENT_COMMAND(name,function,description) \
601 { if(name == strtolower(argv(1))) { function; return TRUE; } }
603 CLIENT_COMMANDS(CMD_REQUEST_USAGE, argc, "")
604 #undef CLIENT_COMMAND
609 void ClientCommand_macro_write_aliases(float fh)
611 #define CLIENT_COMMAND(name,function,description) \
612 { CMD_Write_Alias("qc_cmd_cmd", name, description); }
614 CLIENT_COMMANDS(0, 0, "")
615 #undef CLIENT_COMMAND
620 // ======================================
621 // Main Function Called By Engine (cmd)
622 // ======================================
623 // If this function exists, server game code parses clientcommand before the engine code gets it.
625 void SV_ParseClientCommand(string command)
627 // if we're banned, don't even parse the command
628 if(Ban_MaybeEnforceBanOnce(self))
631 float argc = tokenize_console(command);
633 // for the mutator hook system
634 cmd_name = strtolower(argv(0));
636 cmd_string = command;
638 // Guide for working with argc arguments by example:
639 // argc: 1 - 2 - 3 - 4
640 // argv: 0 - 1 - 2 - 3
641 // cmd vote - master - login - password
644 switch(strtolower(argv(0)))
646 // exempt commands which are not subject to floodcheck
647 case "begin": break; // handled by engine in host_cmd.c
648 case "download": break; // handled by engine in cl_parse.c
649 case "mv_getpicture": break; // handled by server in this file
650 case "pause": break; // handled by engine in host_cmd.c
651 case "prespawn": break; // handled by engine in host_cmd.c
652 case "sentcvar": break; // handled by server in this file
653 case "spawn": break; // handled by engine in host_cmd.c
656 if(SV_ParseClientCommand_floodcheck())
657 break; // "TRUE": continue, as we're not flooding yet
659 return; // "FALSE": not allowed to continue, halt // print("^1ERROR: ^7ANTISPAM CAUGHT: ", command, ".\n");
662 /* NOTE: should this be disabled? It can be spammy perhaps, but hopefully it's okay for now */
663 if(argv(0) == "help")
667 sprint(self, "\nClient networked commands:\n");
668 ClientCommand_macro_help();
670 sprint(self, "\nCommon networked commands:\n");
671 CommonCommand_macro_help(self);
673 sprint(self, "\nUsage:^3 cmd COMMAND...^7, where possible commands are listed above.\n");
674 sprint(self, "For help about a specific command, type cmd help COMMAND\n");
677 else if(CommonCommand_macro_usage(argc, self)) // Instead of trying to call a command, we're going to see detailed information about it
681 else if(ClientCommand_macro_usage(argc)) // same, but for normal commands now
686 else if(MUTATOR_CALLHOOK(SV_ParseClientCommand))
688 return; // handled by a mutator
690 else if(CheatCommand(argc))
692 return; // handled by server/cheats.qc
694 else if(CommonCommand_macro_command(argc, self, command))
696 return; // handled by server/command/common.qc
698 else if(ClientCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
700 return; // handled by one of the above ClientCommand_* functions
703 clientcommand(self, command);