7 #include "../mapinfo.qh"
11 #include "../notifications.qh"
15 #include "../../client/command/cl_cmd.qh"
19 #include "../../server/command/banning.qh"
20 #include "../../server/command/cmd.qh"
21 #include "../../server/command/common.qh"
22 #include "../../server/command/sv_cmd.qh"
23 #include "../../common/turrets/config.qh"
24 #include "../../common/weapons/config.qh"
27 // =========================================================
28 // Generic program common command code, written by Samual
29 // Last updated: February 19th, 2012
30 // =========================================================
32 // used by generic commands for better help/usage information
33 string GetProgramCommandPrefix(void)
46 // used by curl command
47 void Curl_URI_Get_Callback(int id, float status, string data)
49 int i = id - URI_GET_CURL;
50 float do_exec = curl_uri_get_exec[i];
51 string do_cvar = curl_uri_get_cvar[i];
54 dprintf("error: status is %d\n", status);
63 cvar_set(do_cvar, data);
72 // =======================
73 // Command Sub-Functions
74 // =======================
76 void GenericCommand_addtolist(float request, float argc)
80 case CMD_REQUEST_COMMAND:
84 string original_cvar = argv(1);
85 string tmp_string = argv(2);
87 if(cvar_string(original_cvar) == "") // cvar was empty
89 cvar_set(original_cvar, tmp_string);
91 else // add it to the end of the list if the list doesn't already have it
93 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
95 for(i = 0; i < argc; ++i)
96 if(argv(i) == tmp_string)
97 return; // already in list
99 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
106 print("Incorrect parameters for ^2addtolist^7\n");
107 case CMD_REQUEST_USAGE:
109 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
110 print(" Where 'variable' is what to add 'value' to.\n");
111 print("See also: ^2removefromlist^7\n");
117 void GenericCommand_qc_curl(float request, float argc)
121 case CMD_REQUEST_COMMAND:
123 bool do_exec = false;
124 string do_cvar = string_null;
127 for(i = 1; i+1 < argc; ++i)
129 if(argv(i) == "--cvar" && i+2 < argc)
132 do_cvar = strzone(argv(i));
135 if(argv(i) == "--exec")
140 if(argv(i) == "--key" && i+2 < argc)
149 // now, argv(i) is the URL
150 // following args may be POST parameters
151 string url = argv(i);
153 float buf = buf_create();
155 for(j = 0; i+1 < argc; i += 2)
156 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
158 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
161 if(j == 0) // no args: GET
162 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
163 else // with args: POST
164 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
168 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
169 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
170 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
173 print(_("error creating curl handle\n"));
181 case CMD_REQUEST_USAGE:
183 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
189 void GenericCommand_dumpcommands(float request)
193 case CMD_REQUEST_COMMAND:
196 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
197 fh = fopen(filename, FILE_WRITE);
202 CMD_Write("dump of server console commands:\n");
203 GameCommand_macro_write_aliases(fh);
205 CMD_Write("\ndump of networked client only commands:\n");
206 ClientCommand_macro_write_aliases(fh);
208 CMD_Write("\ndump of common commands:\n");
209 CommonCommand_macro_write_aliases(fh);
211 CMD_Write("\ndump of ban commands:\n");
212 BanCommand_macro_write_aliases(fh);
216 CMD_Write("dump of client commands:\n");
217 LocalCommand_macro_write_aliases(fh);
220 CMD_Write("\ndump of generic commands:\n");
221 GenericCommand_macro_write_aliases(fh);
223 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
229 print("^1Error: ^7Could not dump to file!\n");
235 case CMD_REQUEST_USAGE:
237 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
238 print(" No arguments required.\n");
244 void GenericCommand_dumpitems(float request)
248 case CMD_REQUEST_COMMAND:
255 case CMD_REQUEST_USAGE:
257 printf("\nUsage:^3 %s dumpitems", GetProgramCommandPrefix());
263 void GenericCommand_dumpnotifs(float request)
267 case CMD_REQUEST_COMMAND:
270 float fh, alsoprint = false;
271 string filename = argv(1);
275 filename = "notifications_dump.cfg";
278 else if(filename == "-")
280 filename = "notifications_dump.cfg";
283 fh = fopen(filename, FILE_WRITE);
287 Dump_Notifications(fh, alsoprint);
288 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
293 printf("^1Error: ^7Could not open file '%s'!\n", filename);
296 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
302 case CMD_REQUEST_USAGE:
304 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
305 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
306 print(" if supplied with '-' output to console as well as default,\n");
307 print(" if left blank, it will only write to default.\n");
313 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
317 case CMD_REQUEST_COMMAND:
320 wep_config_file = -1;
321 wep_config_alsoprint = -1;
322 string filename = argv(1);
326 filename = "weapons_dump.cfg";
327 wep_config_alsoprint = false;
329 else if(filename == "-")
331 filename = "weapons_dump.cfg";
332 wep_config_alsoprint = true;
334 wep_config_file = fopen(filename, FILE_WRITE);
336 if(wep_config_file >= 0)
338 Dump_Weapon_Settings();
339 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
340 fclose(wep_config_file);
341 wep_config_file = -1;
342 wep_config_alsoprint = -1;
346 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
349 print(_("Weapons dump command only works with sv_cmd.\n"));
355 case CMD_REQUEST_USAGE:
357 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
358 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
359 print(" if supplied with '-' output to console as well as default,\n");
360 print(" if left blank, it will only write to default.\n");
366 void GenericCommand_dumpturrets(float request)
370 case CMD_REQUEST_COMMAND:
373 tur_config_file = -1;
374 tur_config_alsoprint = -1;
375 string filename = argv(1);
379 filename = "turrets_dump.cfg";
380 tur_config_alsoprint = FALSE;
382 else if(filename == "-")
384 filename = "turrets_dump.cfg";
385 tur_config_alsoprint = TRUE;
387 tur_config_file = fopen(filename, FILE_WRITE);
389 if(tur_config_file >= 0)
391 Dump_Turret_Settings();
392 print(sprintf("Dumping turrets... File located in ^2data/data/%s^7.\n", filename));
393 fclose(tur_config_file);
394 tur_config_file = -1;
395 tur_config_alsoprint = -1;
399 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
402 print(_("Turrets dump command only works with sv_cmd.\n"));
408 case CMD_REQUEST_USAGE:
410 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpturrets [filename]"));
411 print(" Where 'filename' is the file to write (default is turrets_dump.cfg),\n");
412 print(" if supplied with '-' output to console as well as default,\n");
413 print(" if left blank, it will only write to default.\n");
419 void GenericCommand_maplist(float request, float argc)
423 case CMD_REQUEST_COMMAND:
430 case "add": // appends new maps to the maplist
434 if (!fexists(strcat("maps/", argv(2), ".bsp")))
436 print("maplist: ERROR: ", argv(2), " does not exist!\n");
440 if(cvar_string("g_maplist") == "")
441 cvar_set("g_maplist", argv(2));
443 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
447 break; // go to usage
450 case "cleanup": // scans maplist and only adds back the ones which are really usable
453 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
454 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
457 for(i = 0; i < argc; ++i)
458 if(MapInfo_CheckMap(argv(i)))
459 tmp_string = strcat(tmp_string, " ", argv(i));
461 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
462 cvar_set("g_maplist", tmp_string);
467 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
471 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
474 for(i = 0; i < argc; ++i)
475 if(argv(i) != argv(2))
476 tmp_string = strcat(tmp_string, " ", argv(i));
478 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
479 cvar_set("g_maplist", tmp_string);
483 break; // go to usage
486 case "shuffle": // randomly shuffle the maplist
488 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
497 print("Incorrect parameters for ^2maplist^7\n");
498 case CMD_REQUEST_USAGE:
500 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
501 print(" Where 'action' is the command to complete,\n");
502 print(" and 'map' is what it acts upon (if required).\n");
503 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
509 void GenericCommand_nextframe(float request, float arguments, string command)
513 case CMD_REQUEST_COMMAND:
515 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
520 case CMD_REQUEST_USAGE:
522 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
523 print(" Where command will be executed next frame of this VM\n");
529 void GenericCommand_removefromlist(float request, float argc)
533 case CMD_REQUEST_COMMAND:
538 string original_cvar = argv(1);
539 string removal = argv(2);
542 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
545 for(i = 0; i < argc; ++i)
546 if(argv(i) != removal)
547 tmp_string = strcat(tmp_string, " ", argv(i));
549 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
550 cvar_set(original_cvar, tmp_string);
557 print("Incorrect parameters for ^2removefromlist^7\n");
558 case CMD_REQUEST_USAGE:
560 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
561 print(" Where 'variable' is what cvar to remove 'value' from.\n");
562 print("See also: ^2addtolist^7\n");
568 void GenericCommand_restartnotifs(float request)
572 case CMD_REQUEST_COMMAND:
577 "Restart_Notifications(): Restarting %d notifications... ",
578 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
593 Destroy_All_Notifications();
594 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
596 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
602 case CMD_REQUEST_USAGE:
604 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
605 print(" No arguments required.\n");
611 void GenericCommand_settemp(float request, float argc)
615 case CMD_REQUEST_COMMAND:
619 float f = cvar_settemp(argv(1), argv(2));
621 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
623 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
624 // else cvar_settemp itself errors out
631 print("Incorrect parameters for ^2settemp^7\n");
632 case CMD_REQUEST_USAGE:
634 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
635 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
636 print("See also: ^2settemp_restore^7\n");
642 void GenericCommand_settemp_restore(float request, float argc)
646 case CMD_REQUEST_COMMAND:
648 float i = cvar_settemp_restore();
651 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
653 dprint("Nothing to restore.\n");
659 case CMD_REQUEST_USAGE:
661 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
662 print(" No arguments required.\n");
663 print("See also: ^2settemp^7\n");
669 void GenericCommand_runtest(float request, float argc)
673 case CMD_REQUEST_COMMAND:
678 for(i = 1; i < argc; ++i)
687 case CMD_REQUEST_USAGE:
689 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
695 /* use this when creating a new command, making sure to place it in alphabetical order... also,
696 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
697 void GenericCommand_(float request)
701 case CMD_REQUEST_COMMAND:
708 case CMD_REQUEST_USAGE:
710 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
711 print(" No arguments required.\n");
718 // ==================================
719 // Macro system for server commands
720 // ==================================
722 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
723 #define GENERIC_COMMANDS(request,arguments,command) \
724 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
725 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
726 GENERIC_COMMAND("dumpitems", GenericCommand_dumpitems(request), "Dump all items to the console") \
727 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
728 GENERIC_COMMAND("dumpturrets", GenericCommand_dumpturrets(request), "Dump all turrets into turrets_dump.txt") \
729 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
730 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
731 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
732 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
733 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
734 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
735 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
736 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
737 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
738 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
741 void GenericCommand_macro_help()
743 #define GENERIC_COMMAND(name,function,description) \
744 { print(" ^2", name, "^7: ", description, "\n"); }
746 GENERIC_COMMANDS(0, 0, "");
747 #undef GENERIC_COMMAND
752 float GenericCommand_macro_command(float argc, string command)
754 #define GENERIC_COMMAND(name,function,description) \
755 { if(name == strtolower(argv(0))) { function; return true; } }
757 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
758 #undef GENERIC_COMMAND
763 float GenericCommand_macro_usage(float argc)
765 #define GENERIC_COMMAND(name,function,description) \
766 { if(name == strtolower(argv(1))) { function; return true; } }
768 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
769 #undef GENERIC_COMMAND
774 void GenericCommand_macro_write_aliases(float fh)
776 #define GENERIC_COMMAND(name,function,description) \
777 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
779 GENERIC_COMMANDS(0, 0, "");
780 #undef GENERIC_COMMAND
786 // ===========================================
787 // Main Common Function For Generic Commands
788 // ===========================================
789 // Commands spread out among all programs (menu, client, and server)
791 float GenericCommand(string command)
793 float argc = tokenize_console(command);
798 // Guide for working with argc arguments by example:
799 // argc: 1 - 2 - 3 - 4
800 // argv: 0 - 1 - 2 - 3
801 // cmd vote - master - login - password
803 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
805 return true; // handled by one of the above GenericCommand_* functions
807 else if(argc >= 3 && argv(0) == "red")
809 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
810 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
813 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
816 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
818 n = floor(random() * 6 + 2);
821 for(i = 0; i < n; ++i)
823 s2 = strcat(s2, "AH");
827 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
833 s = strcat(s, " ", s2);
835 s = strcat(s2, " ", s);
837 s2 = substring(s, strlen(s) - 2, 2);
838 if(s2 == "AH" || s2 == "AY")
841 s = strcat(s, " ))");
844 s = substring(s, 0, strlen(s) - 1);
847 s = strconv(1, 0, 0, s);
849 localcmd(strcat(argv(1), " ", s));
853 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
855 // test case for terencehill's color codes
856 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
860 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
863 for(i = 0; i < n; ++i)
865 c = substring(s, i, 1);
872 if(substring(s, i+1, 1) == "^")
878 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
879 c = strcat(rgb_to_hexcolor(rgb), c);
884 localcmd(strcat(argv(1), " ", s2));