7 #include "../mapinfo.qh"
11 #include "../notifications.qh"
15 #include "../../client/command/cl_cmd.qh"
19 #include "../../server/command/banning.qh"
20 #include "../../server/command/cmd.qh"
21 #include "../../server/command/common.qh"
22 #include "../../server/command/sv_cmd.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 dprintf("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 print("Incorrect parameters for ^2addtolist^7\n");
105 case CMD_REQUEST_USAGE:
107 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
108 print(" Where 'variable' is what to add 'value' to.\n");
109 print("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 print(_("error creating curl handle\n"));
179 case CMD_REQUEST_USAGE:
181 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
187 void GenericCommand_dumpcommands(float request)
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 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
227 print("^1Error: ^7Could not dump to file!\n");
233 case CMD_REQUEST_USAGE:
235 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
236 print(" No arguments required.\n");
242 void GenericCommand_dumpnotifs(float request)
246 case CMD_REQUEST_COMMAND:
249 float fh, alsoprint = false;
250 string filename = argv(1);
254 filename = "notifications_dump.cfg";
257 else if(filename == "-")
259 filename = "notifications_dump.cfg";
262 fh = fopen(filename, FILE_WRITE);
266 Dump_Notifications(fh, alsoprint);
267 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
272 printf("^1Error: ^7Could not open file '%s'!\n", filename);
275 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
281 case CMD_REQUEST_USAGE:
283 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
284 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
285 print(" if supplied with '-' output to console as well as default,\n");
286 print(" if left blank, it will only write to default.\n");
292 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
296 case CMD_REQUEST_COMMAND:
299 wep_config_file = -1;
300 wep_config_alsoprint = -1;
301 string filename = argv(1);
305 filename = "weapons_dump.cfg";
306 wep_config_alsoprint = false;
308 else if(filename == "-")
310 filename = "weapons_dump.cfg";
311 wep_config_alsoprint = true;
313 wep_config_file = fopen(filename, FILE_WRITE);
315 if(wep_config_file >= 0)
317 Dump_Weapon_Settings();
318 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
319 fclose(wep_config_file);
320 wep_config_file = -1;
321 wep_config_alsoprint = -1;
325 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
328 print(_("Weapons dump command only works with sv_cmd.\n"));
334 case CMD_REQUEST_USAGE:
336 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
337 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
338 print(" if supplied with '-' output to console as well as default,\n");
339 print(" if left blank, it will only write to default.\n");
345 void GenericCommand_maplist(float request, float argc)
349 case CMD_REQUEST_COMMAND:
356 case "add": // appends new maps to the maplist
360 if (!fexists(strcat("maps/", argv(2), ".bsp")))
362 print("maplist: ERROR: ", argv(2), " does not exist!\n");
366 if(cvar_string("g_maplist") == "")
367 cvar_set("g_maplist", argv(2));
369 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
373 break; // go to usage
376 case "cleanup": // scans maplist and only adds back the ones which are really usable
379 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
380 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
383 for(i = 0; i < argc; ++i)
384 if(MapInfo_CheckMap(argv(i)))
385 tmp_string = strcat(tmp_string, " ", argv(i));
387 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
388 cvar_set("g_maplist", tmp_string);
393 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
397 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
400 for(i = 0; i < argc; ++i)
401 if(argv(i) != argv(2))
402 tmp_string = strcat(tmp_string, " ", argv(i));
404 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
405 cvar_set("g_maplist", tmp_string);
409 break; // go to usage
412 case "shuffle": // randomly shuffle the maplist
414 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
423 print("Incorrect parameters for ^2maplist^7\n");
424 case CMD_REQUEST_USAGE:
426 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
427 print(" Where 'action' is the command to complete,\n");
428 print(" and 'map' is what it acts upon (if required).\n");
429 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
435 void GenericCommand_nextframe(float request, float arguments, string command)
439 case CMD_REQUEST_COMMAND:
441 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
446 case CMD_REQUEST_USAGE:
448 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
449 print(" Where command will be executed next frame of this VM\n");
455 void GenericCommand_removefromlist(float request, float argc)
459 case CMD_REQUEST_COMMAND:
464 string original_cvar = argv(1);
465 string removal = argv(2);
468 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
471 for(i = 0; i < argc; ++i)
472 if(argv(i) != removal)
473 tmp_string = strcat(tmp_string, " ", argv(i));
475 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
476 cvar_set(original_cvar, tmp_string);
483 print("Incorrect parameters for ^2removefromlist^7\n");
484 case CMD_REQUEST_USAGE:
486 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
487 print(" Where 'variable' is what cvar to remove 'value' from.\n");
488 print("See also: ^2addtolist^7\n");
494 void GenericCommand_restartnotifs(float request)
498 case CMD_REQUEST_COMMAND:
503 "Restart_Notifications(): Restarting %d notifications... ",
504 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
519 Destroy_All_Notifications();
520 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
522 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
528 case CMD_REQUEST_USAGE:
530 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
531 print(" No arguments required.\n");
537 void GenericCommand_settemp(float request, float argc)
541 case CMD_REQUEST_COMMAND:
545 float f = cvar_settemp(argv(1), argv(2));
547 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
549 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
550 // else cvar_settemp itself errors out
557 print("Incorrect parameters for ^2settemp^7\n");
558 case CMD_REQUEST_USAGE:
560 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
561 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
562 print("See also: ^2settemp_restore^7\n");
568 void GenericCommand_settemp_restore(float request, float argc)
572 case CMD_REQUEST_COMMAND:
574 float i = cvar_settemp_restore();
577 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
579 dprint("Nothing to restore.\n");
585 case CMD_REQUEST_USAGE:
587 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
588 print(" No arguments required.\n");
589 print("See also: ^2settemp^7\n");
595 void GenericCommand_runtest(float request, float argc)
599 case CMD_REQUEST_COMMAND:
604 for(i = 1; i < argc; ++i)
613 case CMD_REQUEST_USAGE:
615 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
621 /* use this when creating a new command, making sure to place it in alphabetical order... also,
622 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
623 void GenericCommand_(float request)
627 case CMD_REQUEST_COMMAND:
634 case CMD_REQUEST_USAGE:
636 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
637 print(" No arguments required.\n");
644 // ==================================
645 // Macro system for server commands
646 // ==================================
648 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
649 #define GENERIC_COMMANDS(request,arguments,command) \
650 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
651 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
652 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
653 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
654 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
655 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
656 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
657 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
658 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
659 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
660 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
661 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
662 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
665 void GenericCommand_macro_help()
667 #define GENERIC_COMMAND(name,function,description) \
668 { print(" ^2", name, "^7: ", description, "\n"); }
670 GENERIC_COMMANDS(0, 0, "");
671 #undef GENERIC_COMMAND
676 float GenericCommand_macro_command(float argc, string command)
678 #define GENERIC_COMMAND(name,function,description) \
679 { if(name == strtolower(argv(0))) { function; return true; } }
681 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
682 #undef GENERIC_COMMAND
687 float GenericCommand_macro_usage(float argc)
689 #define GENERIC_COMMAND(name,function,description) \
690 { if(name == strtolower(argv(1))) { function; return true; } }
692 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
693 #undef GENERIC_COMMAND
698 void GenericCommand_macro_write_aliases(float fh)
700 #define GENERIC_COMMAND(name,function,description) \
701 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
703 GENERIC_COMMANDS(0, 0, "");
704 #undef GENERIC_COMMAND
710 // ===========================================
711 // Main Common Function For Generic Commands
712 // ===========================================
713 // Commands spread out among all programs (menu, client, and server)
715 float GenericCommand(string command)
717 float argc = tokenize_console(command);
722 // Guide for working with argc arguments by example:
723 // argc: 1 - 2 - 3 - 4
724 // argv: 0 - 1 - 2 - 3
725 // cmd vote - master - login - password
727 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
729 return true; // handled by one of the above GenericCommand_* functions
731 else if(argc >= 3 && argv(0) == "red")
733 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
734 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
737 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
740 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
742 n = floor(random() * 6 + 2);
745 for(i = 0; i < n; ++i)
747 s2 = strcat(s2, "AH");
751 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
757 s = strcat(s, " ", s2);
759 s = strcat(s2, " ", s);
761 s2 = substring(s, strlen(s) - 2, 2);
762 if(s2 == "AH" || s2 == "AY")
765 s = strcat(s, " ))");
768 s = substring(s, 0, strlen(s) - 1);
771 s = strconv(1, 0, 0, s);
773 localcmd(strcat(argv(1), " ", s));
777 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
779 // test case for terencehill's color codes
780 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
784 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
787 for(i = 0; i < n; ++i)
789 c = substring(s, i, 1);
796 if(substring(s, i+1, 1) == "^")
802 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
803 c = strcat(rgb_to_hexcolor(rgb), c);
808 localcmd(strcat(argv(1), " ", s2));