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];
38 LOG_TRACEF("error: status is %d", status);
47 cvar_set(do_cvar, data);
56 // =======================
57 // Command Sub-Functions
58 // =======================
60 void GenericCommand_addtolist(float request, float argc)
64 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
73 cvar_set(original_cvar, tmp_string);
75 else // add it to the end of the list if the list doesn't already have it
77 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
79 for(i = 0; i < argc; ++i)
80 if(argv(i) == tmp_string)
81 return; // already in list
83 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
90 LOG_INFO("Incorrect parameters for ^2addtolist^7\n");
91 case CMD_REQUEST_USAGE:
93 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
94 LOG_INFO(" Where 'variable' is what to add 'value' to.\n");
95 LOG_INFO("See also: ^2removefromlist^7\n");
101 void GenericCommand_qc_curl(float request, float argc)
105 case CMD_REQUEST_COMMAND:
107 bool do_exec = false;
108 string do_cvar = string_null;
111 for(i = 1; i+1 < argc; ++i)
113 if(argv(i) == "--cvar" && i+2 < argc)
116 do_cvar = strzone(argv(i));
119 if(argv(i) == "--exec")
124 if(argv(i) == "--key" && i+2 < argc)
133 // now, argv(i) is the URL
134 // following args may be POST parameters
135 string url = argv(i);
137 float buf = buf_create();
139 for(j = 0; i+1 < argc; i += 2)
140 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
142 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
145 if(j == 0) // no args: GET
146 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
147 else // with args: POST
148 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
152 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
153 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
154 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
157 LOG_INFO(_("error creating curl handle\n"));
165 case CMD_REQUEST_USAGE:
167 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
173 GENERIC_COMMAND(dumpcommands, "Dump all commands on the program to *_cmd_dump.txt")
177 case CMD_REQUEST_COMMAND:
180 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
181 fh = fopen(filename, FILE_WRITE);
186 CMD_Write("dump of server console commands:\n");
187 GameCommand_macro_write_aliases(fh);
189 CMD_Write("\ndump of networked client only commands:\n");
190 ClientCommand_macro_write_aliases(fh);
192 CMD_Write("\ndump of common commands:\n");
193 CommonCommand_macro_write_aliases(fh);
195 CMD_Write("\ndump of ban commands:\n");
196 BanCommand_macro_write_aliases(fh);
200 CMD_Write("dump of client commands:\n");
201 LocalCommand_macro_write_aliases(fh);
204 CMD_Write("\ndump of generic commands:\n");
205 GenericCommand_macro_write_aliases(fh);
207 LOG_INFO("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
213 LOG_INFO("^1Error: ^7Could not dump to file!\n");
219 case CMD_REQUEST_USAGE:
221 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
222 LOG_INFO(" No arguments required.\n");
228 void GenericCommand_maplist(float request, float argc)
232 case CMD_REQUEST_COMMAND:
239 case "add": // appends new maps to the maplist
243 if (!fexists(strcat("maps/", argv(2), ".bsp")))
245 LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!\n");
249 if(cvar_string("g_maplist") == "")
250 cvar_set("g_maplist", argv(2));
252 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
256 break; // go to usage
259 case "cleanup": // scans maplist and only adds back the ones which are really usable
262 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
263 string filtered = "";
264 FOREACH_WORD(cvar_string("g_maplist"), MapInfo_CheckMap(it), filtered = cons(filtered, it));
265 cvar_set("g_maplist", filtered);
269 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
273 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
276 for(i = 0; i < argc; ++i)
277 if(argv(i) != argv(2))
278 tmp_string = strcat(tmp_string, " ", argv(i));
280 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
281 cvar_set("g_maplist", tmp_string);
285 break; // go to usage
288 case "shuffle": // randomly shuffle the maplist
290 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
299 LOG_INFO("Incorrect parameters for ^2maplist^7\n");
300 case CMD_REQUEST_USAGE:
302 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
303 LOG_INFO(" Where 'action' is the command to complete,\n");
304 LOG_INFO(" and 'map' is what it acts upon (if required).\n");
305 LOG_INFO(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
311 void GenericCommand_nextframe(float request, float arguments, string command)
315 case CMD_REQUEST_COMMAND:
317 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
322 case CMD_REQUEST_USAGE:
324 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
325 LOG_INFO(" Where command will be executed next frame of this VM\n");
331 void GenericCommand_removefromlist(float request, float argc)
335 case CMD_REQUEST_COMMAND:
340 string original_cvar = argv(1);
341 string removal = argv(2);
344 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
347 for(i = 0; i < argc; ++i)
348 if(argv(i) != removal)
349 tmp_string = strcat(tmp_string, " ", argv(i));
351 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
352 cvar_set(original_cvar, tmp_string);
359 LOG_INFO("Incorrect parameters for ^2removefromlist^7\n");
360 case CMD_REQUEST_USAGE:
362 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
363 LOG_INFO(" Where 'variable' is what cvar to remove 'value' from.\n");
364 LOG_INFO("See also: ^2addtolist^7\n");
370 void GenericCommand_restartnotifs(float request)
374 case CMD_REQUEST_COMMAND:
377 int NOTIF_ANNCE_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_ANNCE, { ++NOTIF_ANNCE_COUNT; });
378 int NOTIF_INFO_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_INFO, { ++NOTIF_INFO_COUNT; });
379 int NOTIF_CENTER_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_CENTER, { ++NOTIF_CENTER_COUNT; });
380 int NOTIF_MULTI_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_MULTI, { ++NOTIF_MULTI_COUNT; });
381 int NOTIF_CHOICE_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_CHOICE, { ++NOTIF_CHOICE_COUNT; });
384 "Restart_Notifications(): Restarting %d notifications... ",
385 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
400 Destroy_All_Notifications();
401 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
403 LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
409 case CMD_REQUEST_USAGE:
411 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
412 LOG_INFO(" No arguments required.\n");
418 void GenericCommand_settemp(float request, float argc)
422 case CMD_REQUEST_COMMAND:
426 float f = cvar_settemp(argv(1), argv(2));
428 LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.");
430 LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".");
431 // else cvar_settemp itself errors out
438 LOG_INFO("Incorrect parameters for ^2settemp^7\n");
439 case CMD_REQUEST_USAGE:
441 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
442 LOG_INFO(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
443 LOG_INFO("See also: ^2settemp_restore^7\n");
449 void GenericCommand_settemp_restore(float request, float argc)
453 case CMD_REQUEST_COMMAND:
455 float i = cvar_settemp_restore();
458 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.");
460 LOG_TRACE("Nothing to restore.");
466 case CMD_REQUEST_USAGE:
468 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
469 LOG_INFO(" No arguments required.\n");
470 LOG_INFO("See also: ^2settemp^7\n");
476 void GenericCommand_runtest(float request, float argc)
480 case CMD_REQUEST_COMMAND:
485 for(i = 1; i < argc; ++i)
494 case CMD_REQUEST_USAGE:
496 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
502 /* use this when creating a new command, making sure to place it in alphabetical order... also,
503 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
504 void GenericCommand_(float request)
508 case CMD_REQUEST_COMMAND:
515 case CMD_REQUEST_USAGE:
517 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
518 print(" No arguments required.\n");
525 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
526 GENERIC_COMMAND(addtolist, "Add a string to a cvar") { GenericCommand_addtolist(request, arguments); }
527 GENERIC_COMMAND(maplist, "Automatic control of maplist") { GenericCommand_maplist(request, arguments); }
528 GENERIC_COMMAND(nextframe, "Execute the given command next frame of this VM") { GenericCommand_nextframe(request, arguments, command); }
529 GENERIC_COMMAND(qc_curl, "Queries a URL") { GenericCommand_qc_curl(request, arguments); }
530 GENERIC_COMMAND(removefromlist, "Remove a string from a cvar") { GenericCommand_removefromlist(request, arguments); }
531 GENERIC_COMMAND(restartnotifs, "Re-initialize all notifications") { GenericCommand_restartnotifs(request); }
532 GENERIC_COMMAND(rpn, "RPN calculator") { GenericCommand_rpn(request, arguments, command); }
533 GENERIC_COMMAND(settemp, "Temporarily set a value to a cvar which is restored later") { GenericCommand_settemp(request, arguments); }
534 GENERIC_COMMAND(settemp_restore, "Restore all cvars set by settemp command") { GenericCommand_settemp_restore(request, arguments); }
535 GENERIC_COMMAND(runtest, "Run unit tests") { GenericCommand_runtest(request, arguments); }
537 void GenericCommand_macro_help()
539 FOREACH(GENERIC_COMMANDS, true, LOG_INFOF(" ^2%s^7: %s\n", it.m_name, it.m_description));
542 float GenericCommand_macro_command(float argc, string command)
544 string c = strtolower(argv(0));
545 FOREACH(GENERIC_COMMANDS, it.m_name == c, {
546 it.m_invokecmd(it, CMD_REQUEST_COMMAND, NULL, argc, command);
552 float GenericCommand_macro_usage(float argc)
554 string c = strtolower(argv(1));
555 FOREACH(GENERIC_COMMANDS, it.m_name == c, {
556 it.m_invokecmd(it, CMD_REQUEST_USAGE, NULL, argc, "");
562 void GenericCommand_macro_write_aliases(float fh)
564 FOREACH(GENERIC_COMMANDS, true, CMD_Write_Alias("qc_cmd_svmenu", it.m_name, it.m_description));
568 // ===========================================
569 // Main Common Function For Generic Commands
570 // ===========================================
571 // Commands spread out among all programs (menu, client, and server)
573 float GenericCommand(string command)
575 float argc = tokenize_console(command);
580 // Guide for working with argc arguments by example:
581 // argc: 1 - 2 - 3 - 4
582 // argv: 0 - 1 - 2 - 3
583 // cmd vote - master - login - password
585 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
587 return true; // handled by one of the above GenericCommand_* functions
589 else if(argc >= 3 && argv(0) == "red")
591 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
592 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
595 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
598 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
600 n = floor(random() * 6 + 2);
603 for(i = 0; i < n; ++i)
605 s2 = strcat(s2, "AH");
609 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
615 s = strcat(s, " ", s2);
617 s = strcat(s2, " ", s);
619 s2 = substring(s, strlen(s) - 2, 2);
620 if(s2 == "AH" || s2 == "AY")
623 s = strcat(s, " ))");
626 s = substring(s, 0, strlen(s) - 1);
629 s = strconv(1, 0, 0, s);
631 localcmd(strcat(argv(1), " ", s));
635 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
637 // test case for terencehill's color codes
638 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
642 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
645 for(i = 0; i < n; ++i)
647 c = substring(s, i, 1);
654 if(substring(s, i+1, 1) == "^")
660 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
661 c = strcat(rgb_to_hexcolor(rgb), c);
666 localcmd(strcat(argv(1), " ", s2));