2 #include "shared_defs.qh"
4 // =========================================================
5 // Generic program common command code, written by Samual
6 // Last updated: February 19th, 2012
7 // =========================================================
9 // used by generic commands for better help/usage information
10 string GetProgramCommandPrefix(void)
23 // used by curl command
24 void Curl_URI_Get_Callback(int id, float status, string data)
26 int i = id - URI_GET_CURL;
27 float do_exec = curl_uri_get_exec[i];
28 string 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:
61 string original_cvar = argv(1);
62 string tmp_string = argv(2);
64 if(cvar_string(original_cvar) == "") // cvar was empty
66 cvar_set(original_cvar, tmp_string);
68 else // add it to the end of the list if the list doesn't already have it
70 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
72 for(i = 0; i < argc; ++i)
73 if(argv(i) == tmp_string)
74 return; // already in list
76 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
83 print("Incorrect parameters for ^2addtolist^7\n");
84 case CMD_REQUEST_USAGE:
86 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
87 print(" Where 'variable' is what to add 'value' to.\n");
88 print("See also: ^2removefromlist^7\n");
94 void GenericCommand_qc_curl(float request, float argc)
98 case CMD_REQUEST_COMMAND:
100 bool do_exec = false;
101 string do_cvar = string_null;
104 for(i = 1; i+1 < argc; ++i)
106 if(argv(i) == "--cvar" && i+2 < argc)
109 do_cvar = strzone(argv(i));
112 if(argv(i) == "--exec")
117 if(argv(i) == "--key" && i+2 < argc)
126 // now, argv(i) is the URL
127 // following args may be POST parameters
128 string url = argv(i);
130 float buf = buf_create();
132 for(j = 0; i+1 < argc; i += 2)
133 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
135 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
138 if(j == 0) // no args: GET
139 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
140 else // with args: POST
141 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
145 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
146 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
147 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
150 print(_("error creating curl handle\n"));
158 case CMD_REQUEST_USAGE:
160 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
166 void GenericCommand_dumpcommands(float request)
170 case CMD_REQUEST_COMMAND:
173 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
174 fh = fopen(filename, FILE_WRITE);
179 CMD_Write("dump of server console commands:\n");
180 GameCommand_macro_write_aliases(fh);
182 CMD_Write("\ndump of networked client only commands:\n");
183 ClientCommand_macro_write_aliases(fh);
185 CMD_Write("\ndump of common commands:\n");
186 CommonCommand_macro_write_aliases(fh);
188 CMD_Write("\ndump of ban commands:\n");
189 BanCommand_macro_write_aliases(fh);
193 CMD_Write("dump of client commands:\n");
194 LocalCommand_macro_write_aliases(fh);
197 CMD_Write("\ndump of generic commands:\n");
198 GenericCommand_macro_write_aliases(fh);
200 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
206 print("^1Error: ^7Could not dump to file!\n");
212 case CMD_REQUEST_USAGE:
214 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
215 print(" No arguments required.\n");
221 void GenericCommand_dumpnotifs(float request)
225 case CMD_REQUEST_COMMAND:
228 float fh, alsoprint = false;
229 string filename = argv(1);
233 filename = "notifications_dump.cfg";
236 else if(filename == "-")
238 filename = "notifications_dump.cfg";
241 fh = fopen(filename, FILE_WRITE);
245 Dump_Notifications(fh, alsoprint);
246 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
251 printf("^1Error: ^7Could not open file '%s'!\n", filename);
254 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
260 case CMD_REQUEST_USAGE:
262 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
263 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
264 print(" if supplied with '-' output to console as well as default,\n");
265 print(" if left blank, it will only write to default.\n");
271 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
275 case CMD_REQUEST_COMMAND:
278 wep_config_file = -1;
279 wep_config_alsoprint = -1;
280 string filename = argv(1);
284 filename = "weapons_dump.cfg";
285 wep_config_alsoprint = false;
287 else if(filename == "-")
289 filename = "weapons_dump.cfg";
290 wep_config_alsoprint = true;
292 wep_config_file = fopen(filename, FILE_WRITE);
294 if(wep_config_file >= 0)
296 Dump_Weapon_Settings();
297 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
298 fclose(wep_config_file);
299 wep_config_file = -1;
300 wep_config_alsoprint = -1;
304 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
307 print(_("Weapons dump command only works with sv_cmd.\n"));
313 case CMD_REQUEST_USAGE:
315 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
316 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
317 print(" if supplied with '-' output to console as well as default,\n");
318 print(" if left blank, it will only write to default.\n");
324 void GenericCommand_maplist(float request, float argc)
328 case CMD_REQUEST_COMMAND:
335 case "add": // appends new maps to the maplist
339 if (!fexists(strcat("maps/", argv(2), ".bsp")))
341 print("maplist: ERROR: ", argv(2), " does not exist!\n");
345 if(cvar_string("g_maplist") == "")
346 cvar_set("g_maplist", argv(2));
348 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
352 break; // go to usage
355 case "cleanup": // scans maplist and only adds back the ones which are really usable
358 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
359 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
362 for(i = 0; i < argc; ++i)
363 if(MapInfo_CheckMap(argv(i)))
364 tmp_string = strcat(tmp_string, " ", argv(i));
366 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
367 cvar_set("g_maplist", tmp_string);
372 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
376 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
379 for(i = 0; i < argc; ++i)
380 if(argv(i) != argv(2))
381 tmp_string = strcat(tmp_string, " ", argv(i));
383 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
384 cvar_set("g_maplist", tmp_string);
388 break; // go to usage
391 case "shuffle": // randomly shuffle the maplist
393 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
402 print("Incorrect parameters for ^2maplist^7\n");
403 case CMD_REQUEST_USAGE:
405 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
406 print(" Where 'action' is the command to complete,\n");
407 print(" and 'map' is what it acts upon (if required).\n");
408 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
414 void GenericCommand_nextframe(float request, float arguments, string command)
418 case CMD_REQUEST_COMMAND:
420 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
425 case CMD_REQUEST_USAGE:
427 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
428 print(" Where command will be executed next frame of this VM\n");
434 void GenericCommand_removefromlist(float request, float argc)
438 case CMD_REQUEST_COMMAND:
443 string original_cvar = argv(1);
444 string removal = argv(2);
447 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
450 for(i = 0; i < argc; ++i)
451 if(argv(i) != removal)
452 tmp_string = strcat(tmp_string, " ", argv(i));
454 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
455 cvar_set(original_cvar, tmp_string);
462 print("Incorrect parameters for ^2removefromlist^7\n");
463 case CMD_REQUEST_USAGE:
465 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
466 print(" Where 'variable' is what cvar to remove 'value' from.\n");
467 print("See also: ^2addtolist^7\n");
473 void GenericCommand_restartnotifs(float request)
477 case CMD_REQUEST_COMMAND:
482 "Restart_Notifications(): Restarting %d notifications... ",
483 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
498 Destroy_All_Notifications();
499 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
501 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
507 case CMD_REQUEST_USAGE:
509 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
510 print(" No arguments required.\n");
516 void GenericCommand_settemp(float request, float argc)
520 case CMD_REQUEST_COMMAND:
524 float f = cvar_settemp(argv(1), argv(2));
526 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
528 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
529 // else cvar_settemp itself errors out
536 print("Incorrect parameters for ^2settemp^7\n");
537 case CMD_REQUEST_USAGE:
539 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
540 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
541 print("See also: ^2settemp_restore^7\n");
547 void GenericCommand_settemp_restore(float request, float argc)
551 case CMD_REQUEST_COMMAND:
553 float i = cvar_settemp_restore();
556 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
558 dprint("Nothing to restore.\n");
564 case CMD_REQUEST_USAGE:
566 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
567 print(" No arguments required.\n");
568 print("See also: ^2settemp^7\n");
574 void GenericCommand_runtest(float request, float argc)
578 case CMD_REQUEST_COMMAND:
583 for(i = 1; i < argc; ++i)
592 case CMD_REQUEST_USAGE:
594 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
600 /* use this when creating a new command, making sure to place it in alphabetical order... also,
601 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
602 void GenericCommand_(float request)
606 case CMD_REQUEST_COMMAND:
613 case CMD_REQUEST_USAGE:
615 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
616 print(" No arguments required.\n");
623 // ==================================
624 // Macro system for server commands
625 // ==================================
627 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
628 #define GENERIC_COMMANDS(request,arguments,command) \
629 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
630 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
631 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
632 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
633 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
634 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
635 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
636 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
637 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
638 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
639 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
640 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
641 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
644 void GenericCommand_macro_help()
646 #define GENERIC_COMMAND(name,function,description) \
647 { print(" ^2", name, "^7: ", description, "\n"); }
649 GENERIC_COMMANDS(0, 0, "");
650 #undef GENERIC_COMMAND
655 float GenericCommand_macro_command(float argc, string command)
657 #define GENERIC_COMMAND(name,function,description) \
658 { if(name == strtolower(argv(0))) { function; return true; } }
660 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
661 #undef GENERIC_COMMAND
666 float GenericCommand_macro_usage(float argc)
668 #define GENERIC_COMMAND(name,function,description) \
669 { if(name == strtolower(argv(1))) { function; return true; } }
671 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
672 #undef GENERIC_COMMAND
677 void GenericCommand_macro_write_aliases(float fh)
679 #define GENERIC_COMMAND(name,function,description) \
680 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
682 GENERIC_COMMANDS(0, 0, "");
683 #undef GENERIC_COMMAND
689 // ===========================================
690 // Main Common Function For Generic Commands
691 // ===========================================
692 // Commands spread out among all programs (menu, client, and server)
694 float GenericCommand(string command)
696 float argc = tokenize_console(command);
701 // Guide for working with argc arguments by example:
702 // argc: 1 - 2 - 3 - 4
703 // argv: 0 - 1 - 2 - 3
704 // cmd vote - master - login - password
706 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
708 return true; // handled by one of the above GenericCommand_* functions
710 else if(argc >= 3 && argv(0) == "red")
712 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
713 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
716 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
719 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
721 n = floor(random() * 6 + 2);
724 for(i = 0; i < n; ++i)
726 s2 = strcat(s2, "AH");
730 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
736 s = strcat(s, " ", s2);
738 s = strcat(s2, " ", s);
740 s2 = substring(s, strlen(s) - 2, 2);
741 if(s2 == "AH" || s2 == "AY")
744 s = strcat(s, " ))");
747 s = substring(s, 0, strlen(s) - 1);
750 s = strconv(1, 0, 0, s);
752 localcmd(strcat(argv(1), " ", s));
756 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
758 // test case for terencehill's color codes
759 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
763 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
766 for(i = 0; i < n; ++i)
768 c = substring(s, i, 1);
775 if(substring(s, i+1, 1) == "^")
781 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
782 c = strcat(rgb_to_hexcolor(rgb), c);
787 localcmd(strcat(argv(1), " ", s2));