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 FOREACH_WORD(cvar_string(original_cvar), it == tmp_string,
79 return; // already in the list
82 cvar_set(original_cvar, cons(cvar_string(original_cvar), tmp_string));
89 LOG_INFO("Incorrect parameters for ^2addtolist^7\n");
90 case CMD_REQUEST_USAGE:
92 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n");
93 LOG_INFO(" Where 'variable' is what to add 'value' to.\n");
94 LOG_INFO("See also: ^2removefromlist^7\n");
100 void GenericCommand_qc_curl(float request, float argc)
104 case CMD_REQUEST_COMMAND:
106 bool do_exec = false;
107 string do_cvar = string_null;
110 for(i = 1; i+1 < argc; ++i)
112 if(argv(i) == "--cvar" && i+2 < argc)
115 do_cvar = strzone(argv(i));
118 if(argv(i) == "--exec")
123 if(argv(i) == "--key" && i+2 < argc)
132 // now, argv(i) is the URL
133 // following args may be POST parameters
134 string url = argv(i);
136 float buf = buf_create();
138 for(j = 0; i+1 < argc; i += 2)
139 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
141 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
144 if(j == 0) // no args: GET
145 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
146 else // with args: POST
147 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
151 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
152 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
153 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
156 LOG_INFO(_("error creating curl handle\n"));
164 case CMD_REQUEST_USAGE:
166 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]");
172 GENERIC_COMMAND(dumpcommands, "Dump all commands on the program to *_cmd_dump.txt")
176 case CMD_REQUEST_COMMAND:
179 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
180 fh = fopen(filename, FILE_WRITE);
185 CMD_Write("dump of server console commands:\n");
186 GameCommand_macro_write_aliases(fh);
188 CMD_Write("\ndump of networked client only commands:\n");
189 ClientCommand_macro_write_aliases(fh);
191 CMD_Write("\ndump of common commands:\n");
192 CommonCommand_macro_write_aliases(fh);
194 CMD_Write("\ndump of ban commands:\n");
195 BanCommand_macro_write_aliases(fh);
199 CMD_Write("dump of client commands:\n");
200 LocalCommand_macro_write_aliases(fh);
203 CMD_Write("\ndump of generic commands:\n");
204 GenericCommand_macro_write_aliases(fh);
206 LOG_INFO("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
212 LOG_INFO("^1Error: ^7Could not dump to file!\n");
218 case CMD_REQUEST_USAGE:
220 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " dumpcommands");
221 LOG_INFO(" No arguments required.\n");
227 void GenericCommand_maplist(float request, float argc)
231 case CMD_REQUEST_COMMAND:
238 case "add": // appends new maps to the maplist
242 if (!fexists(strcat("maps/", argv(2), ".bsp")))
244 LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!\n");
248 if(cvar_string("g_maplist") == "")
249 cvar_set("g_maplist", argv(2));
251 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
255 break; // go to usage
258 case "cleanup": // scans maplist and only adds back the ones which are really usable
261 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
262 string filtered = "";
263 FOREACH_WORD(cvar_string("g_maplist"), MapInfo_CheckMap(it), filtered = cons(filtered, it));
264 cvar_set("g_maplist", filtered);
268 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
272 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
275 for(i = 0; i < argc; ++i)
276 if(argv(i) != argv(2))
277 tmp_string = strcat(tmp_string, " ", argv(i));
279 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
280 cvar_set("g_maplist", tmp_string);
284 break; // go to usage
287 case "shuffle": // randomly shuffle the maplist
289 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
298 LOG_INFO("Incorrect parameters for ^2maplist^7\n");
299 case CMD_REQUEST_USAGE:
301 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n");
302 LOG_INFO(" Where 'action' is the command to complete,\n");
303 LOG_INFO(" and 'map' is what it acts upon (if required).\n");
304 LOG_INFO(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
310 void GenericCommand_nextframe(float request, float arguments, string command)
314 case CMD_REQUEST_COMMAND:
316 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
321 case CMD_REQUEST_USAGE:
323 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n");
324 LOG_INFO(" Where command will be executed next frame of this VM\n");
330 void GenericCommand_removefromlist(float request, float argc)
334 case CMD_REQUEST_COMMAND:
338 string original_cvar = argv(1);
339 string removal = argv(2);
341 string tmp_string = "";
342 FOREACH_WORD(cvar_string(original_cvar), it != removal,
344 tmp_string = cons(tmp_string, it);
347 cvar_set(original_cvar, tmp_string);
354 LOG_INFO("Incorrect parameters for ^2removefromlist^7\n");
355 case CMD_REQUEST_USAGE:
357 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n");
358 LOG_INFO(" Where 'variable' is what cvar to remove 'value' from.\n");
359 LOG_INFO("See also: ^2addtolist^7\n");
365 void GenericCommand_restartnotifs(float request)
369 case CMD_REQUEST_COMMAND:
372 int NOTIF_ANNCE_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_ANNCE, { ++NOTIF_ANNCE_COUNT; });
373 int NOTIF_INFO_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_INFO, { ++NOTIF_INFO_COUNT; });
374 int NOTIF_CENTER_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_CENTER, { ++NOTIF_CENTER_COUNT; });
375 int NOTIF_MULTI_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_MULTI, { ++NOTIF_MULTI_COUNT; });
376 int NOTIF_CHOICE_COUNT = 0; FOREACH(Notifications, it.nent_type == MSG_CHOICE, { ++NOTIF_CHOICE_COUNT; });
379 "Restart_Notifications(): Restarting %d notifications... "
380 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d"
395 Destroy_All_Notifications();
396 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
398 LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
404 case CMD_REQUEST_USAGE:
406 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " restartnotifs");
407 LOG_INFO(" No arguments required.\n");
413 void GenericCommand_settemp(float request, float argc)
417 case CMD_REQUEST_COMMAND:
421 float f = cvar_settemp(argv(1), argv(2));
423 LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.");
425 LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".");
426 // else cvar_settemp itself errors out
433 LOG_INFO("Incorrect parameters for ^2settemp^7\n");
434 case CMD_REQUEST_USAGE:
436 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n");
437 LOG_INFO(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
438 LOG_INFO("See also: ^2settemp_restore^7\n");
444 void GenericCommand_settemp_restore(float request, float argc)
448 case CMD_REQUEST_COMMAND:
450 float i = cvar_settemp_restore();
453 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.");
455 LOG_TRACE("Nothing to restore.");
461 case CMD_REQUEST_USAGE:
463 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n");
464 LOG_INFO(" No arguments required.\n");
465 LOG_INFO("See also: ^2settemp^7\n");
471 void GenericCommand_runtest(float request, float argc)
475 case CMD_REQUEST_COMMAND:
480 for(i = 1; i < argc; ++i)
489 case CMD_REQUEST_USAGE:
491 LOG_INFO("Usage:^3 ", GetProgramCommandPrefix(), " [function to run]");
497 /* use this when creating a new command, making sure to place it in alphabetical order... also,
498 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
499 void GenericCommand_(float request)
503 case CMD_REQUEST_COMMAND:
510 case CMD_REQUEST_USAGE:
512 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
513 print(" No arguments required.\n");
520 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
521 GENERIC_COMMAND(addtolist, "Add a string to a cvar") { GenericCommand_addtolist(request, arguments); }
522 GENERIC_COMMAND(maplist, "Automatic control of maplist") { GenericCommand_maplist(request, arguments); }
523 GENERIC_COMMAND(nextframe, "Execute the given command next frame of this VM") { GenericCommand_nextframe(request, arguments, command); }
524 GENERIC_COMMAND(qc_curl, "Queries a URL") { GenericCommand_qc_curl(request, arguments); }
525 GENERIC_COMMAND(removefromlist, "Remove a string from a cvar") { GenericCommand_removefromlist(request, arguments); }
526 GENERIC_COMMAND(restartnotifs, "Re-initialize all notifications") { GenericCommand_restartnotifs(request); }
527 GENERIC_COMMAND(rpn, "RPN calculator") { GenericCommand_rpn(request, arguments, command); }
528 GENERIC_COMMAND(settemp, "Temporarily set a value to a cvar which is restored later") { GenericCommand_settemp(request, arguments); }
529 GENERIC_COMMAND(settemp_restore, "Restore all cvars set by settemp command") { GenericCommand_settemp_restore(request, arguments); }
530 GENERIC_COMMAND(runtest, "Run unit tests") { GenericCommand_runtest(request, arguments); }
532 void GenericCommand_macro_help()
534 FOREACH(GENERIC_COMMANDS, true, LOG_INFOF(" ^2%s^7: %s", it.m_name, it.m_description));
537 float GenericCommand_macro_command(float argc, string command)
539 string c = strtolower(argv(0));
540 FOREACH(GENERIC_COMMANDS, it.m_name == c, {
541 it.m_invokecmd(it, CMD_REQUEST_COMMAND, NULL, argc, command);
547 float GenericCommand_macro_usage(float argc)
549 string c = strtolower(argv(1));
550 FOREACH(GENERIC_COMMANDS, it.m_name == c, {
551 it.m_invokecmd(it, CMD_REQUEST_USAGE, NULL, argc, "");
557 void GenericCommand_macro_write_aliases(float fh)
559 FOREACH(GENERIC_COMMANDS, true, CMD_Write_Alias("qc_cmd_svmenu", it.m_name, it.m_description));
563 // ===========================================
564 // Main Common Function For Generic Commands
565 // ===========================================
566 // Commands spread out among all programs (menu, client, and server)
568 float GenericCommand(string command)
570 float argc = tokenize_console(command);
575 // Guide for working with argc arguments by example:
576 // argc: 1 - 2 - 3 - 4
577 // argv: 0 - 1 - 2 - 3
578 // cmd vote - master - login - password
580 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
582 return true; // handled by one of the above GenericCommand_* functions
584 else if(argc >= 3 && argv(0) == "red")
586 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
587 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
590 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
593 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
595 n = floor(random() * 6 + 2);
598 for(i = 0; i < n; ++i)
600 s2 = strcat(s2, "AH");
604 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
610 s = strcat(s, " ", s2);
612 s = strcat(s2, " ", s);
614 s2 = substring(s, strlen(s) - 2, 2);
615 if(s2 == "AH" || s2 == "AY")
618 s = strcat(s, " ))");
621 s = substring(s, 0, strlen(s) - 1);
624 s = strconv(1, 0, 0, s);
626 localcmd(strcat(argv(1), " ", s));
630 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
632 // test case for terencehill's color codes
633 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
637 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
640 for(i = 0; i < n; ++i)
642 c = substring(s, i, 1);
649 if(substring(s, i+1, 1) == "^")
655 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
656 c = strcat(rgb_to_hexcolor(rgb), c);
661 localcmd(strcat(argv(1), " ", s2));