6 #include "../mapinfo.qh"
9 #include "../notifications.qh"
13 #include "../../client/command/cl_cmd.qh"
17 #include "../../server/command/banning.qh"
18 #include "../../server/command/cmd.qh"
19 #include "../../server/command/common.qh"
20 #include "../../server/command/sv_cmd.qh"
21 #include "../../common/turrets/config.qh"
22 #include "../../common/weapons/config.qh"
25 // =========================================================
26 // Generic program common command code, written by Samual
27 // Last updated: February 19th, 2012
28 // =========================================================
30 // used by generic commands for better help/usage information
31 string GetProgramCommandPrefix(void)
44 // used by curl command
45 void Curl_URI_Get_Callback(int id, float status, string data)
47 int i = id - URI_GET_CURL;
48 float do_exec = curl_uri_get_exec[i];
49 string do_cvar = curl_uri_get_cvar[i];
52 LOG_TRACEF("error: status is %d\n", status);
61 cvar_set(do_cvar, data);
70 // =======================
71 // Command Sub-Functions
72 // =======================
74 void GenericCommand_addtolist(float request, float argc)
78 case CMD_REQUEST_COMMAND:
82 string original_cvar = argv(1);
83 string tmp_string = argv(2);
85 if(cvar_string(original_cvar) == "") // cvar was empty
87 cvar_set(original_cvar, tmp_string);
89 else // add it to the end of the list if the list doesn't already have it
91 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
93 for(i = 0; i < argc; ++i)
94 if(argv(i) == tmp_string)
95 return; // already in list
97 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
104 LOG_INFO("Incorrect parameters for ^2addtolist^7\n");
105 case CMD_REQUEST_USAGE:
107 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
108 LOG_INFO(" Where 'variable' is what to add 'value' to.\n");
109 LOG_INFO("See also: ^2removefromlist^7\n");
115 void GenericCommand_qc_curl(float request, float argc)
119 case CMD_REQUEST_COMMAND:
121 bool do_exec = false;
122 string do_cvar = string_null;
125 for(i = 1; i+1 < argc; ++i)
127 if(argv(i) == "--cvar" && i+2 < argc)
130 do_cvar = strzone(argv(i));
133 if(argv(i) == "--exec")
138 if(argv(i) == "--key" && i+2 < argc)
147 // now, argv(i) is the URL
148 // following args may be POST parameters
149 string url = argv(i);
151 float buf = buf_create();
153 for(j = 0; i+1 < argc; i += 2)
154 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
156 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
159 if(j == 0) // no args: GET
160 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
161 else // with args: POST
162 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
166 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
167 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
168 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
171 LOG_INFO(_("error creating curl handle\n"));
179 case CMD_REQUEST_USAGE:
181 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
187 GENERIC_COMMAND(dumpcommands, "Dump all commands on the program to *_cmd_dump.txt")
191 case CMD_REQUEST_COMMAND:
194 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
195 fh = fopen(filename, FILE_WRITE);
200 CMD_Write("dump of server console commands:\n");
201 GameCommand_macro_write_aliases(fh);
203 CMD_Write("\ndump of networked client only commands:\n");
204 ClientCommand_macro_write_aliases(fh);
206 CMD_Write("\ndump of common commands:\n");
207 CommonCommand_macro_write_aliases(fh);
209 CMD_Write("\ndump of ban commands:\n");
210 BanCommand_macro_write_aliases(fh);
214 CMD_Write("dump of client commands:\n");
215 LocalCommand_macro_write_aliases(fh);
218 CMD_Write("\ndump of generic commands:\n");
219 GenericCommand_macro_write_aliases(fh);
221 LOG_INFO("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
227 LOG_INFO("^1Error: ^7Could not dump to file!\n");
233 case CMD_REQUEST_USAGE:
235 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
236 LOG_INFO(" No arguments required.\n");
242 void GenericCommand_maplist(float request, float argc)
246 case CMD_REQUEST_COMMAND:
253 case "add": // appends new maps to the maplist
257 if (!fexists(strcat("maps/", argv(2), ".bsp")))
259 LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!\n");
263 if(cvar_string("g_maplist") == "")
264 cvar_set("g_maplist", argv(2));
266 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
270 break; // go to usage
273 case "cleanup": // scans maplist and only adds back the ones which are really usable
276 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
277 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
280 for(i = 0; i < argc; ++i)
281 if(MapInfo_CheckMap(argv(i)))
282 tmp_string = strcat(tmp_string, " ", argv(i));
284 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
285 cvar_set("g_maplist", tmp_string);
290 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
294 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
297 for(i = 0; i < argc; ++i)
298 if(argv(i) != argv(2))
299 tmp_string = strcat(tmp_string, " ", argv(i));
301 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
302 cvar_set("g_maplist", tmp_string);
306 break; // go to usage
309 case "shuffle": // randomly shuffle the maplist
311 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
320 LOG_INFO("Incorrect parameters for ^2maplist^7\n");
321 case CMD_REQUEST_USAGE:
323 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
324 LOG_INFO(" Where 'action' is the command to complete,\n");
325 LOG_INFO(" and 'map' is what it acts upon (if required).\n");
326 LOG_INFO(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
332 void GenericCommand_nextframe(float request, float arguments, string command)
336 case CMD_REQUEST_COMMAND:
338 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
343 case CMD_REQUEST_USAGE:
345 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
346 LOG_INFO(" Where command will be executed next frame of this VM\n");
352 void GenericCommand_removefromlist(float request, float argc)
356 case CMD_REQUEST_COMMAND:
361 string original_cvar = argv(1);
362 string removal = argv(2);
365 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
368 for(i = 0; i < argc; ++i)
369 if(argv(i) != removal)
370 tmp_string = strcat(tmp_string, " ", argv(i));
372 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
373 cvar_set(original_cvar, tmp_string);
380 LOG_INFO("Incorrect parameters for ^2removefromlist^7\n");
381 case CMD_REQUEST_USAGE:
383 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
384 LOG_INFO(" Where 'variable' is what cvar to remove 'value' from.\n");
385 LOG_INFO("See also: ^2addtolist^7\n");
391 void GenericCommand_restartnotifs(float request)
395 case CMD_REQUEST_COMMAND:
400 "Restart_Notifications(): Restarting %d notifications... ",
401 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
416 Destroy_All_Notifications();
417 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
419 LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
425 case CMD_REQUEST_USAGE:
427 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
428 LOG_INFO(" No arguments required.\n");
434 void GenericCommand_settemp(float request, float argc)
438 case CMD_REQUEST_COMMAND:
442 float f = cvar_settemp(argv(1), argv(2));
444 LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
446 LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
447 // else cvar_settemp itself errors out
454 LOG_INFO("Incorrect parameters for ^2settemp^7\n");
455 case CMD_REQUEST_USAGE:
457 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
458 LOG_INFO(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
459 LOG_INFO("See also: ^2settemp_restore^7\n");
465 void GenericCommand_settemp_restore(float request, float argc)
469 case CMD_REQUEST_COMMAND:
471 float i = cvar_settemp_restore();
474 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
476 LOG_TRACE("Nothing to restore.\n");
482 case CMD_REQUEST_USAGE:
484 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
485 LOG_INFO(" No arguments required.\n");
486 LOG_INFO("See also: ^2settemp^7\n");
492 void GenericCommand_runtest(float request, float argc)
496 case CMD_REQUEST_COMMAND:
501 for(i = 1; i < argc; ++i)
510 case CMD_REQUEST_USAGE:
512 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
518 /* use this when creating a new command, making sure to place it in alphabetical order... also,
519 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
520 void GenericCommand_(float request)
524 case CMD_REQUEST_COMMAND:
531 case CMD_REQUEST_USAGE:
533 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
534 print(" No arguments required.\n");
541 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
542 GENERIC_COMMAND(addtolist, "Add a string to a cvar") { GenericCommand_addtolist(request, arguments); }
543 GENERIC_COMMAND(maplist, "Automatic control of maplist") { GenericCommand_maplist(request, arguments); }
544 GENERIC_COMMAND(nextframe, "Execute the given command next frame of this VM") { GenericCommand_nextframe(request, arguments, command); }
545 GENERIC_COMMAND(qc_curl, "Queries a URL") { GenericCommand_qc_curl(request, arguments); }
546 GENERIC_COMMAND(removefromlist, "Remove a string from a cvar") { GenericCommand_removefromlist(request, arguments); }
547 GENERIC_COMMAND(restartnotifs, "Re-initialize all notifications") { GenericCommand_restartnotifs(request); }
548 GENERIC_COMMAND(rpn, "RPN calculator") { GenericCommand_rpn(request, arguments, command); }
549 GENERIC_COMMAND(settemp, "Temporarily set a value to a cvar which is restored later") { GenericCommand_settemp(request, arguments); }
550 GENERIC_COMMAND(settemp_restore, "Restore all cvars set by settemp command") { GenericCommand_settemp_restore(request, arguments); }
551 GENERIC_COMMAND(runtest, "Run unit tests") { GenericCommand_runtest(request, arguments); }
553 void GenericCommand_macro_help()
555 FOREACH(GENERIC_COMMANDS, true, LAMBDA(LOG_INFOF(" ^2%s^7: %s\n", it.m_name, it.m_description)));
558 float GenericCommand_macro_command(float argc, string command)
560 string c = strtolower(argv(0));
561 FOREACH(GENERIC_COMMANDS, it.m_name == c, LAMBDA(
562 it.m_invokecmd(CMD_REQUEST_COMMAND, argc, command);
568 float GenericCommand_macro_usage(float argc)
570 string c = strtolower(argv(1));
571 FOREACH(GENERIC_COMMANDS, it.m_name == c, LAMBDA(
572 it.m_invokecmd(CMD_REQUEST_USAGE, argc, "");
578 void GenericCommand_macro_write_aliases(float fh)
580 FOREACH(GENERIC_COMMANDS, true, LAMBDA(CMD_Write_Alias("qc_cmd_svmenu", it.m_name, it.m_description)));
584 // ===========================================
585 // Main Common Function For Generic Commands
586 // ===========================================
587 // Commands spread out among all programs (menu, client, and server)
589 float GenericCommand(string command)
591 float argc = tokenize_console(command);
596 // Guide for working with argc arguments by example:
597 // argc: 1 - 2 - 3 - 4
598 // argv: 0 - 1 - 2 - 3
599 // cmd vote - master - login - password
601 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
605 else if(argc >= 3 && argv(0) == "red")
607 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
608 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
611 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
614 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
616 n = floor(random() * 6 + 2);
619 for(i = 0; i < n; ++i)
621 s2 = strcat(s2, "AH");
625 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
631 s = strcat(s, " ", s2);
633 s = strcat(s2, " ", s);
635 s2 = substring(s, strlen(s) - 2, 2);
636 if(s2 == "AH" || s2 == "AY")
639 s = strcat(s, " ))");
642 s = substring(s, 0, strlen(s) - 1);
645 s = strconv(1, 0, 0, s);
647 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)
653 // test case for terencehill's color codes
654 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
658 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
661 for(i = 0; i < n; ++i)
663 c = substring(s, i, 1);
670 if(substring(s, i+1, 1) == "^")
676 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
677 c = strcat(rgb_to_hexcolor(rgb), c);
682 localcmd(strcat(argv(1), " ", s2));