8 #include "../mapinfo.qh"
11 #include "../notifications/all.qh"
15 #include <client/commands/cl_cmd.qh>
19 #include <server/command/_mod.qh>
20 #include <common/turrets/config.qh>
21 #include <common/weapons/config.qh>
24 // =========================================================
25 // Generic program common command code, written by Samual
26 // Last updated: February 19th, 2012
27 // =========================================================
30 // used by curl command
31 void Curl_URI_Get_Callback(int id, float status, string data)
33 int i = id - URI_GET_CURL;
34 float do_exec = curl_uri_get_exec[i];
35 string do_cvar = curl_uri_get_cvar[i];
37 LOG_TRACEF("error: status is %d", status);
47 cvar_set(do_cvar, data);
58 // =======================
59 // Command Sub-Functions
60 // =======================
62 void GenericCommand_addtolist(float request, float argc)
65 case CMD_REQUEST_COMMAND:
68 string original_cvar = argv(1);
69 string tmp_string = argv(2);
71 if (cvar_string(original_cvar) == "") { // cvar was empty
72 cvar_set(original_cvar, tmp_string);
73 } else { // add it to the end of the list if the list doesn't already have it
74 FOREACH_WORD(cvar_string(original_cvar), it == tmp_string,
76 return; // already in the list
79 cvar_set(original_cvar, cons(cvar_string(original_cvar), tmp_string));
86 LOG_INFO("Incorrect parameters for ^2addtolist^7");
87 case CMD_REQUEST_USAGE:
89 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " addtolist variable value");
90 LOG_INFO(" Where 'variable' is what to add 'value' to.");
91 LOG_INFO("See also: ^2removefromlist^7");
97 void GenericCommand_qc_curl(float request, float argc)
100 case CMD_REQUEST_COMMAND:
102 bool do_exec = false;
103 string do_cvar = string_null;
106 for (i = 1; i + 1 < argc; ++i) {
107 if (argv(i) == "--cvar" && i + 2 < argc) {
109 do_cvar = strzone(argv(i));
112 if (argv(i) == "--exec") {
116 if (argv(i) == "--key" && i + 2 < argc) {
124 // now, argv(i) is the URL
125 // following args may be POST parameters
126 string url = argv(i);
128 float buf = buf_create();
130 for (j = 0; i + 1 < argc; i += 2) {
131 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i + 1))));
134 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);
149 LOG_INFO(_("error creating curl handle"));
158 case CMD_REQUEST_USAGE:
160 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]");
166 GENERIC_COMMAND(dumpcommands, "Dump all commands on the program to *_cmd_dump.txt")
169 case CMD_REQUEST_COMMAND:
172 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
173 fh = fopen(filename, FILE_WRITE);
177 CMD_Write("dump of server console commands:\n");
178 GameCommand_macro_write_aliases(fh);
180 CMD_Write("\ndump of networked client only commands:\n");
181 ClientCommand_macro_write_aliases(fh);
183 CMD_Write("\ndump of common commands:\n");
184 CommonCommand_macro_write_aliases(fh);
186 CMD_Write("\ndump of ban commands:\n");
187 BanCommand_macro_write_aliases(fh);
191 CMD_Write("dump of client commands:\n");
192 LocalCommand_macro_write_aliases(fh);
195 CMD_Write("\ndump of generic commands:\n");
196 GenericCommand_macro_write_aliases(fh);
198 LOG_INFO("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.");
202 LOG_INFO("^1Error: ^7Could not dump to file!");
208 case CMD_REQUEST_USAGE:
210 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " dumpcommands");
211 LOG_INFO(" No arguments required.");
217 void GenericCommand_maplist(float request, float argc)
220 case CMD_REQUEST_COMMAND:
226 case "add": // appends new maps to the maplist
229 if (!fexists(strcat("maps/", argv(2), ".bsp"))) {
230 LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!");
234 if (cvar_string("g_maplist") == "") {
235 cvar_set("g_maplist", argv(2));
237 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
242 break; // go to usage
245 case "cleanup": // scans maplist and only adds back the ones which are really usable
248 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
249 string filtered = "";
250 FOREACH_WORD(cvar_string("g_maplist"), MapInfo_CheckMap(it), filtered = cons(filtered, it));
251 cvar_set("g_maplist", filtered);
255 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
258 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
261 for (i = 0; i < argc; ++i) {
262 if (argv(i) != argv(2)) {
263 tmp_string = strcat(tmp_string, " ", argv(i));
267 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
268 cvar_set("g_maplist", tmp_string);
272 break; // go to usage
275 case "shuffle": // randomly shuffle the maplist
277 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
286 LOG_INFO("Incorrect parameters for ^2maplist^7");
287 case CMD_REQUEST_USAGE:
289 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " maplist action [map]");
290 LOG_INFO(" Where 'action' is the command to complete,");
291 LOG_INFO(" and 'map' is what it acts upon (if required).");
292 LOG_INFO(" Full list of commands here: \"add, cleanup, remove, shuffle.\"");
298 void GenericCommand_nextframe(float request, float arguments, string command)
301 case CMD_REQUEST_COMMAND:
303 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
308 case CMD_REQUEST_USAGE:
310 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " nextframe command...");
311 LOG_INFO(" Where command will be executed next frame of this VM");
317 void GenericCommand_removefromlist(float request, float argc)
320 case CMD_REQUEST_COMMAND:
323 string original_cvar = argv(1);
324 string removal = argv(2);
326 string tmp_string = "";
327 FOREACH_WORD(cvar_string(original_cvar), it != removal,
329 tmp_string = cons(tmp_string, it);
332 cvar_set(original_cvar, tmp_string);
339 LOG_INFO("Incorrect parameters for ^2removefromlist^7");
340 case CMD_REQUEST_USAGE:
342 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value");
343 LOG_INFO(" Where 'variable' is what cvar to remove 'value' from.");
344 LOG_INFO("See also: ^2addtolist^7");
350 void GenericCommand_restartnotifs(float request)
353 case CMD_REQUEST_COMMAND:
356 int NOTIF_ANNCE_COUNT = 0;
357 FOREACH(Notifications, it.nent_type == MSG_ANNCE, { ++NOTIF_ANNCE_COUNT;
359 int NOTIF_INFO_COUNT = 0;
360 FOREACH(Notifications, it.nent_type == MSG_INFO, { ++NOTIF_INFO_COUNT;
362 int NOTIF_CENTER_COUNT = 0;
363 FOREACH(Notifications, it.nent_type == MSG_CENTER, { ++NOTIF_CENTER_COUNT;
365 int NOTIF_MULTI_COUNT = 0;
366 FOREACH(Notifications, it.nent_type == MSG_MULTI, { ++NOTIF_MULTI_COUNT;
368 int NOTIF_CHOICE_COUNT = 0;
369 FOREACH(Notifications, it.nent_type == MSG_CHOICE, { ++NOTIF_CHOICE_COUNT;
373 "Restart_Notifications(): Restarting %d notifications... "
374 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d"
389 Destroy_All_Notifications();
390 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
392 LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd."));
398 case CMD_REQUEST_USAGE:
400 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " restartnotifs");
401 LOG_INFO(" No arguments required.");
407 void GenericCommand_settemp(float request, float argc)
410 case CMD_REQUEST_COMMAND:
413 float f = cvar_settemp(argv(1), argv(2));
415 LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.");
416 } else if (f == -1) {
417 LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".");
419 // else cvar_settemp itself errors out
426 LOG_INFO("Incorrect parameters for ^2settemp^7");
427 case CMD_REQUEST_USAGE:
429 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"");
430 LOG_INFO(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.");
431 LOG_INFO("See also: ^2settemp_restore^7");
437 void GenericCommand_settemp_restore(float request, float argc)
440 case CMD_REQUEST_COMMAND:
442 float i = cvar_settemp_restore();
445 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.");
447 LOG_TRACE("Nothing to restore.");
454 case CMD_REQUEST_USAGE:
456 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " settemp_restore");
457 LOG_INFO(" No arguments required.");
458 LOG_INFO("See also: ^2settemp^7");
464 void GenericCommand_runtest(float request, float argc)
467 case CMD_REQUEST_COMMAND:
471 for (i = 1; i < argc; ++i) {
481 case CMD_REQUEST_USAGE:
483 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " [function to run]");
489 /* use this when creating a new command, making sure to place it in alphabetical order... also,
490 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
491 void GenericCommand_(float request)
495 case CMD_REQUEST_COMMAND:
502 case CMD_REQUEST_USAGE:
504 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
505 print(" No arguments required.\n");
512 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
513 GENERIC_COMMAND(addtolist, "Add a string to a cvar")
515 GenericCommand_addtolist(request, arguments);
517 GENERIC_COMMAND(maplist, "Automatic control of maplist")
519 GenericCommand_maplist(request, arguments);
521 GENERIC_COMMAND(nextframe, "Execute the given command next frame of this VM")
523 GenericCommand_nextframe(request, arguments, command);
525 GENERIC_COMMAND(qc_curl, "Queries a URL")
527 GenericCommand_qc_curl(request, arguments);
529 GENERIC_COMMAND(removefromlist, "Remove a string from a cvar")
531 GenericCommand_removefromlist(request, arguments);
533 GENERIC_COMMAND(restartnotifs, "Re-initialize all notifications")
535 GenericCommand_restartnotifs(request);
537 GENERIC_COMMAND(rpn, "RPN calculator")
539 GenericCommand_rpn(request, arguments, command);
541 GENERIC_COMMAND(settemp, "Temporarily set a value to a cvar which is restored later")
543 GenericCommand_settemp(request, arguments);
545 GENERIC_COMMAND(settemp_restore, "Restore all cvars set by settemp command")
547 GenericCommand_settemp_restore(request, arguments);
549 GENERIC_COMMAND(runtest, "Run unit tests")
551 GenericCommand_runtest(request, arguments);
554 void GenericCommand_macro_help()
556 FOREACH(GENERIC_COMMANDS, true, LOG_INFOF(" ^2%s^7: %s", it.m_name, it.m_description));
559 float GenericCommand_macro_command(float argc, string command)
561 string c = strtolower(argv(0));
562 FOREACH(GENERIC_COMMANDS, it.m_name == c, {
563 it.m_invokecmd(it, CMD_REQUEST_COMMAND, NULL, argc, command);
569 float GenericCommand_macro_usage(float argc)
571 string c = strtolower(argv(1));
572 FOREACH(GENERIC_COMMANDS, it.m_name == c, {
573 it.m_invokecmd(it, CMD_REQUEST_USAGE, NULL, argc, "");
579 void GenericCommand_macro_write_aliases(float fh)
581 FOREACH(GENERIC_COMMANDS, true, CMD_Write_Alias("qc_cmd_svmenu", it.m_name, it.m_description));
585 // ===========================================
586 // Main Common Function For Generic Commands
587 // ===========================================
588 // Commands spread out among all programs (menu, client, and server)
590 float GenericCommand(string command)
592 float argc = tokenize_console(command);
597 // Guide for working with argc arguments by example:
598 // argc: 1 - 2 - 3 - 4
599 // argv: 0 - 1 - 2 - 3
600 // cmd vote - master - login - password
602 if (GenericCommand_macro_command(argc, command)) { // continue as usual and scan for normal commands
603 return true; // handled by one of the above GenericCommand_* functions
604 } else if (argc >= 3 && argv(0) == "red") {
605 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
606 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
608 } else if (argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830) {
610 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
612 n = floor(random() * 6 + 2);
615 for (i = 0; i < n; ++i) {
616 s2 = strcat(s2, "AH");
619 if (random() < 0.1) {
620 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
626 if (random() < 0.8) {
627 s = strcat(s, " ", s2);
629 s = strcat(s2, " ", s);
633 s2 = substring(s, strlen(s) - 2, 2);
634 if (s2 == "AH" || s2 == "AY") {
637 s = strcat(s, " ))");
640 if (random() < 0.1) {
641 s = substring(s, 0, strlen(s) - 1);
644 if (random() < 0.1) {
645 s = strconv(1, 0, 0, s);
648 localcmd(strcat(argv(1), " ", s));
651 } else if (argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790) {
652 // test case for terencehill's color codes
653 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
657 j = ((6 * max(1, floor(strlen(s) / 32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
660 for (i = 0; i < n; ++i) {
661 c = substring(s, i, 1);
665 } else if (c == "^") {
667 if (substring(s, i + 1, 1) == "^") {
673 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
674 c = strcat(rgb_to_hexcolor(rgb), c);
679 localcmd(strcat(argv(1), " ", s2));