1 // =========================================================
2 // Generic program common command code, written by Samual
3 // Last updated: February 19th, 2012
4 // =========================================================
6 // used by generic commands for better help/usage information
7 string GetProgramCommandPrefix(void)
20 // used by curl command
21 void Curl_URI_Get_Callback(float id, float status, string data)
26 i = id - URI_GET_CURL;
27 do_exec = curl_uri_get_exec[i];
28 do_cvar = curl_uri_get_cvar[i];
31 dprintf("error: status is %d\n", status);
40 cvar_set(do_cvar, data);
49 // =======================
50 // Command Sub-Functions
51 // =======================
53 void GenericCommand_addtolist(float request, float argc)
57 case CMD_REQUEST_COMMAND:
63 string original_cvar = argv(1);
64 string tmp_string = argv(2);
66 if(cvar_string(original_cvar) == "") // cvar was empty
68 cvar_set(original_cvar, tmp_string);
70 else // add it to the end of the list if the list doesn't already have it
72 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
74 for(i = 0; i < argc; ++i)
75 if(argv(i) == tmp_string)
76 return; // already in list
78 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
85 print("Incorrect parameters for ^2addtolist^7\n");
86 case CMD_REQUEST_USAGE:
88 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
89 print(" Where 'variable' is what to add 'value' to.\n");
90 print("See also: ^2removefromlist^7\n");
96 void GenericCommand_qc_curl(float request, float argc)
100 case CMD_REQUEST_COMMAND:
111 do_cvar = string_null;
114 for(i = 1; i+1 < argc; ++i)
116 if(argv(i) == "--cvar" && i+2 < argc)
119 do_cvar = strzone(argv(i));
122 if(argv(i) == "--exec")
127 if(argv(i) == "--key" && i+2 < argc)
136 // now, argv(i) is the URL
137 // following args may be POST parameters
142 for(; i+1 < argc; i += 2)
143 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
145 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
147 if(j == 0) // no args: GET
148 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
149 else // with args: POST
150 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
154 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
155 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
156 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
159 print(_("error creating curl handle\n"));
167 case CMD_REQUEST_USAGE:
169 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
175 void GenericCommand_dumpcommands(float request)
179 case CMD_REQUEST_COMMAND:
182 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
183 fh = fopen(filename, FILE_WRITE);
188 CMD_Write("dump of server console commands:\n");
189 GameCommand_macro_write_aliases(fh);
191 CMD_Write("\ndump of networked client only commands:\n");
192 ClientCommand_macro_write_aliases(fh);
194 CMD_Write("\ndump of common commands:\n");
195 CommonCommand_macro_write_aliases(fh);
197 CMD_Write("\ndump of ban commands:\n");
198 BanCommand_macro_write_aliases(fh);
202 CMD_Write("dump of client commands:\n");
203 LocalCommand_macro_write_aliases(fh);
206 CMD_Write("\ndump of generic commands:\n");
207 GenericCommand_macro_write_aliases(fh);
209 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
215 print("^1Error: ^7Could not dump to file!\n");
221 case CMD_REQUEST_USAGE:
223 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
224 print(" No arguments required.\n");
230 void GenericCommand_dumpnotifs(float request)
234 case CMD_REQUEST_COMMAND:
237 float fh, alsoprint = FALSE;
238 string filename = argv(1);
242 filename = "notifications_dump.cfg";
245 else if(filename == "-")
247 filename = "notifications_dump.cfg";
250 fh = fopen(filename, FILE_WRITE);
254 Dump_Notifications(fh, alsoprint);
255 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
260 printf("^1Error: ^7Could not open file '%s'!\n", filename);
263 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
269 case CMD_REQUEST_USAGE:
271 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
272 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
273 print(" if supplied with '-' output to console as well as default,\n");
274 print(" if left blank, it will only write to default.\n");
280 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
284 case CMD_REQUEST_COMMAND:
287 wep_config_file = -1;
288 wep_config_alsoprint = -1;
289 string filename = argv(1);
293 filename = "weapons_dump.cfg";
294 wep_config_alsoprint = FALSE;
296 else if(filename == "-")
298 filename = "weapons_dump.cfg";
299 wep_config_alsoprint = TRUE;
301 wep_config_file = fopen(filename, FILE_WRITE);
303 if(wep_config_file >= 0)
305 Dump_Weapon_Settings();
306 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
307 fclose(wep_config_file);
308 wep_config_file = -1;
309 wep_config_alsoprint = -1;
313 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
316 print(_("Weapons dump command only works with sv_cmd.\n"));
322 case CMD_REQUEST_USAGE:
324 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
325 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
326 print(" if supplied with '-' output to console as well as default,\n");
327 print(" if left blank, it will only write to default.\n");
333 void GenericCommand_dumpturrets(float request)
337 case CMD_REQUEST_COMMAND:
340 tur_config_file = -1;
341 tur_config_alsoprint = -1;
342 string filename = argv(1);
346 filename = "turrets_dump.cfg";
347 tur_config_alsoprint = FALSE;
349 else if(filename == "-")
351 filename = "turrets_dump.cfg";
352 tur_config_alsoprint = TRUE;
354 tur_config_file = fopen(filename, FILE_WRITE);
356 if(tur_config_file >= 0)
358 Dump_Turret_Settings();
359 print(sprintf("Dumping turrets... File located in ^2data/data/%s^7.\n", filename));
360 fclose(tur_config_file);
361 tur_config_file = -1;
362 tur_config_alsoprint = -1;
366 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
369 print(_("Turrets dump command only works with sv_cmd.\n"));
375 case CMD_REQUEST_USAGE:
377 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpturrets [filename]"));
378 print(" Where 'filename' is the file to write (default is turrets_dump.cfg),\n");
379 print(" if supplied with '-' output to console as well as default,\n");
380 print(" if left blank, it will only write to default.\n");
386 void GenericCommand_maplist(float request, float argc)
390 case CMD_REQUEST_COMMAND:
397 case "add": // appends new maps to the maplist
401 if (!fexists(strcat("maps/", argv(2), ".bsp")))
403 print("maplist: ERROR: ", argv(2), " does not exist!\n");
407 if(cvar_string("g_maplist") == "")
408 cvar_set("g_maplist", argv(2));
410 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
414 break; // go to usage
417 case "cleanup": // scans maplist and only adds back the ones which are really usable
420 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
421 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
424 for(i = 0; i < argc; ++i)
425 if(MapInfo_CheckMap(argv(i)))
426 tmp_string = strcat(tmp_string, " ", argv(i));
428 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
429 cvar_set("g_maplist", tmp_string);
434 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
438 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
441 for(i = 0; i < argc; ++i)
442 if(argv(i) != argv(2))
443 tmp_string = strcat(tmp_string, " ", argv(i));
445 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
446 cvar_set("g_maplist", tmp_string);
450 break; // go to usage
453 case "shuffle": // randomly shuffle the maplist
455 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
464 print("Incorrect parameters for ^2maplist^7\n");
465 case CMD_REQUEST_USAGE:
467 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
468 print(" Where 'action' is the command to complete,\n");
469 print(" and 'map' is what it acts upon (if required).\n");
470 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
476 void GenericCommand_nextframe(float request, float arguments, string command)
480 case CMD_REQUEST_COMMAND:
482 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
487 case CMD_REQUEST_USAGE:
489 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
490 print(" Where command will be executed next frame of this VM\n");
496 void GenericCommand_removefromlist(float request, float argc)
500 case CMD_REQUEST_COMMAND:
505 string original_cvar = argv(1);
506 string removal = argv(2);
509 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
512 for(i = 0; i < argc; ++i)
513 if(argv(i) != removal)
514 tmp_string = strcat(tmp_string, " ", argv(i));
516 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
517 cvar_set(original_cvar, tmp_string);
524 print("Incorrect parameters for ^2removefromlist^7\n");
525 case CMD_REQUEST_USAGE:
527 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
528 print(" Where 'variable' is what cvar to remove 'value' from.\n");
529 print("See also: ^2addtolist^7\n");
535 void GenericCommand_restartnotifs(float request)
539 case CMD_REQUEST_COMMAND:
544 "Restart_Notifications(): Restarting %d notifications... ",
545 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
560 Destroy_All_Notifications();
561 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
563 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
569 case CMD_REQUEST_USAGE:
571 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
572 print(" No arguments required.\n");
578 void GenericCommand_settemp(float request, float argc)
582 case CMD_REQUEST_COMMAND:
586 float f = cvar_settemp(argv(1), argv(2));
588 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
590 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
591 // else cvar_settemp itself errors out
598 print("Incorrect parameters for ^2settemp^7\n");
599 case CMD_REQUEST_USAGE:
601 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
602 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
603 print("See also: ^2settemp_restore^7\n");
609 void GenericCommand_settemp_restore(float request, float argc)
613 case CMD_REQUEST_COMMAND:
615 float i = cvar_settemp_restore();
618 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
620 dprint("Nothing to restore.\n");
626 case CMD_REQUEST_USAGE:
628 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
629 print(" No arguments required.\n");
630 print("See also: ^2settemp^7\n");
636 void GenericCommand_runtest(float request, float argc)
640 case CMD_REQUEST_COMMAND:
645 for(i = 1; i < argc; ++i)
654 case CMD_REQUEST_USAGE:
656 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
662 /* use this when creating a new command, making sure to place it in alphabetical order... also,
663 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
664 void GenericCommand_(float request)
668 case CMD_REQUEST_COMMAND:
675 case CMD_REQUEST_USAGE:
677 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
678 print(" No arguments required.\n");
685 // ==================================
686 // Macro system for server commands
687 // ==================================
689 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
690 #define GENERIC_COMMANDS(request,arguments,command) \
691 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
692 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
693 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
694 GENERIC_COMMAND("dumpturrets", GenericCommand_dumpturrets(request), "Dump all turrets into turrets_dump.txt") \
695 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
696 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
697 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
698 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
699 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
700 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
701 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
702 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
703 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
704 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
707 void GenericCommand_macro_help()
709 #define GENERIC_COMMAND(name,function,description) \
710 { print(" ^2", name, "^7: ", description, "\n"); }
712 GENERIC_COMMANDS(0, 0, "")
713 #undef GENERIC_COMMAND
718 float GenericCommand_macro_command(float argc, string command)
720 #define GENERIC_COMMAND(name,function,description) \
721 { if(name == strtolower(argv(0))) { function; return TRUE; } }
723 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
724 #undef GENERIC_COMMAND
729 float GenericCommand_macro_usage(float argc)
731 #define GENERIC_COMMAND(name,function,description) \
732 { if(name == strtolower(argv(1))) { function; return TRUE; } }
734 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
735 #undef GENERIC_COMMAND
740 void GenericCommand_macro_write_aliases(float fh)
742 #define GENERIC_COMMAND(name,function,description) \
743 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
745 GENERIC_COMMANDS(0, 0, "")
746 #undef GENERIC_COMMAND
752 // ===========================================
753 // Main Common Function For Generic Commands
754 // ===========================================
755 // Commands spread out among all programs (menu, client, and server)
757 float GenericCommand(string command)
759 float argc = tokenize_console(command);
764 // Guide for working with argc arguments by example:
765 // argc: 1 - 2 - 3 - 4
766 // argv: 0 - 1 - 2 - 3
767 // cmd vote - master - login - password
769 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
771 return TRUE; // handled by one of the above GenericCommand_* functions
773 else if(argc >= 3 && argv(0) == "red")
775 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
776 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
779 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
782 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
784 n = floor(random() * 6 + 2);
787 for(i = 0; i < n; ++i)
789 s2 = strcat(s2, "AH");
793 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
799 s = strcat(s, " ", s2);
801 s = strcat(s2, " ", s);
803 s2 = substring(s, strlen(s) - 2, 2);
804 if(s2 == "AH" || s2 == "AY")
807 s = strcat(s, " ))");
810 s = substring(s, 0, strlen(s) - 1);
813 s = strconv(1, 0, 0, s);
815 localcmd(strcat(argv(1), " ", s));
819 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
821 // test case for terencehill's color codes
822 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
826 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
829 for(i = 0; i < n; ++i)
831 c = substring(s, i, 1);
838 if(substring(s, i+1, 1) == "^")
844 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
845 c = strcat(rgb_to_hexcolor(rgb), c);
850 localcmd(strcat(argv(1), " ", s2));