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"
24 #include "../../common/weapons/config.qh"
27 // =========================================================
28 // Generic program common command code, written by Samual
29 // Last updated: February 19th, 2012
30 // =========================================================
32 // used by generic commands for better help/usage information
33 string GetProgramCommandPrefix(void)
46 // used by curl command
47 void Curl_URI_Get_Callback(int id, float status, string data)
49 int i = id - URI_GET_CURL;
50 float do_exec = curl_uri_get_exec[i];
51 string do_cvar = curl_uri_get_cvar[i];
54 dprintf("error: status is %d\n", status);
63 cvar_set(do_cvar, data);
72 // =======================
73 // Command Sub-Functions
74 // =======================
76 void GenericCommand_addtolist(float request, float argc)
80 case CMD_REQUEST_COMMAND:
84 string original_cvar = argv(1);
85 string tmp_string = argv(2);
87 if(cvar_string(original_cvar) == "") // cvar was empty
89 cvar_set(original_cvar, tmp_string);
91 else // add it to the end of the list if the list doesn't already have it
93 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
95 for(i = 0; i < argc; ++i)
96 if(argv(i) == tmp_string)
97 return; // already in list
99 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
106 print("Incorrect parameters for ^2addtolist^7\n");
107 case CMD_REQUEST_USAGE:
109 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
110 print(" Where 'variable' is what to add 'value' to.\n");
111 print("See also: ^2removefromlist^7\n");
117 void GenericCommand_qc_curl(float request, float argc)
121 case CMD_REQUEST_COMMAND:
123 bool do_exec = false;
124 string do_cvar = string_null;
127 for(i = 1; i+1 < argc; ++i)
129 if(argv(i) == "--cvar" && i+2 < argc)
132 do_cvar = strzone(argv(i));
135 if(argv(i) == "--exec")
140 if(argv(i) == "--key" && i+2 < argc)
149 // now, argv(i) is the URL
150 // following args may be POST parameters
151 string url = argv(i);
153 float buf = buf_create();
155 for(j = 0; i+1 < argc; i += 2)
156 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
158 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
161 if(j == 0) // no args: GET
162 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
163 else // with args: POST
164 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
168 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
169 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
170 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
173 print(_("error creating curl handle\n"));
181 case CMD_REQUEST_USAGE:
183 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
189 void GenericCommand_dumpcommands(float request)
193 case CMD_REQUEST_COMMAND:
196 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
197 fh = fopen(filename, FILE_WRITE);
202 CMD_Write("dump of server console commands:\n");
203 GameCommand_macro_write_aliases(fh);
205 CMD_Write("\ndump of networked client only commands:\n");
206 ClientCommand_macro_write_aliases(fh);
208 CMD_Write("\ndump of common commands:\n");
209 CommonCommand_macro_write_aliases(fh);
211 CMD_Write("\ndump of ban commands:\n");
212 BanCommand_macro_write_aliases(fh);
216 CMD_Write("dump of client commands:\n");
217 LocalCommand_macro_write_aliases(fh);
220 CMD_Write("\ndump of generic commands:\n");
221 GenericCommand_macro_write_aliases(fh);
223 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
229 print("^1Error: ^7Could not dump to file!\n");
235 case CMD_REQUEST_USAGE:
237 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
238 print(" No arguments required.\n");
244 void GenericCommand_dumpitems(float request)
248 case CMD_REQUEST_COMMAND:
255 case CMD_REQUEST_USAGE:
257 printf("\nUsage:^3 %s dumpitems", GetProgramCommandPrefix());
263 void GenericCommand_dumpnotifs(float request)
267 case CMD_REQUEST_COMMAND:
270 float fh, alsoprint = false;
271 string filename = argv(1);
275 filename = "notifications_dump.cfg";
278 else if(filename == "-")
280 filename = "notifications_dump.cfg";
283 fh = fopen(filename, FILE_WRITE);
287 Dump_Notifications(fh, alsoprint);
288 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
293 printf("^1Error: ^7Could not open file '%s'!\n", filename);
296 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
302 case CMD_REQUEST_USAGE:
304 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
305 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
306 print(" if supplied with '-' output to console as well as default,\n");
307 print(" if left blank, it will only write to default.\n");
313 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
317 case CMD_REQUEST_COMMAND:
320 wep_config_file = -1;
321 wep_config_alsoprint = -1;
322 string filename = argv(1);
326 filename = "weapons_dump.cfg";
327 wep_config_alsoprint = false;
329 else if(filename == "-")
331 filename = "weapons_dump.cfg";
332 wep_config_alsoprint = true;
334 wep_config_file = fopen(filename, FILE_WRITE);
336 if(wep_config_file >= 0)
338 Dump_Weapon_Settings();
339 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
340 fclose(wep_config_file);
341 wep_config_file = -1;
342 wep_config_alsoprint = -1;
346 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
349 print(_("Weapons dump command only works with sv_cmd.\n"));
355 case CMD_REQUEST_USAGE:
357 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
358 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
359 print(" if supplied with '-' output to console as well as default,\n");
360 print(" if left blank, it will only write to default.\n");
366 void GenericCommand_maplist(float request, float argc)
370 case CMD_REQUEST_COMMAND:
377 case "add": // appends new maps to the maplist
381 if (!fexists(strcat("maps/", argv(2), ".bsp")))
383 print("maplist: ERROR: ", argv(2), " does not exist!\n");
387 if(cvar_string("g_maplist") == "")
388 cvar_set("g_maplist", argv(2));
390 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
394 break; // go to usage
397 case "cleanup": // scans maplist and only adds back the ones which are really usable
400 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
401 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
404 for(i = 0; i < argc; ++i)
405 if(MapInfo_CheckMap(argv(i)))
406 tmp_string = strcat(tmp_string, " ", argv(i));
408 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
409 cvar_set("g_maplist", tmp_string);
414 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
418 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
421 for(i = 0; i < argc; ++i)
422 if(argv(i) != argv(2))
423 tmp_string = strcat(tmp_string, " ", argv(i));
425 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
426 cvar_set("g_maplist", tmp_string);
430 break; // go to usage
433 case "shuffle": // randomly shuffle the maplist
435 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
444 print("Incorrect parameters for ^2maplist^7\n");
445 case CMD_REQUEST_USAGE:
447 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
448 print(" Where 'action' is the command to complete,\n");
449 print(" and 'map' is what it acts upon (if required).\n");
450 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
456 void GenericCommand_nextframe(float request, float arguments, string command)
460 case CMD_REQUEST_COMMAND:
462 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
467 case CMD_REQUEST_USAGE:
469 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
470 print(" Where command will be executed next frame of this VM\n");
476 void GenericCommand_removefromlist(float request, float argc)
480 case CMD_REQUEST_COMMAND:
485 string original_cvar = argv(1);
486 string removal = argv(2);
489 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
492 for(i = 0; i < argc; ++i)
493 if(argv(i) != removal)
494 tmp_string = strcat(tmp_string, " ", argv(i));
496 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
497 cvar_set(original_cvar, tmp_string);
504 print("Incorrect parameters for ^2removefromlist^7\n");
505 case CMD_REQUEST_USAGE:
507 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
508 print(" Where 'variable' is what cvar to remove 'value' from.\n");
509 print("See also: ^2addtolist^7\n");
515 void GenericCommand_restartnotifs(float request)
519 case CMD_REQUEST_COMMAND:
524 "Restart_Notifications(): Restarting %d notifications... ",
525 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
540 Destroy_All_Notifications();
541 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
543 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
549 case CMD_REQUEST_USAGE:
551 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
552 print(" No arguments required.\n");
558 void GenericCommand_settemp(float request, float argc)
562 case CMD_REQUEST_COMMAND:
566 float f = cvar_settemp(argv(1), argv(2));
568 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
570 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
571 // else cvar_settemp itself errors out
578 print("Incorrect parameters for ^2settemp^7\n");
579 case CMD_REQUEST_USAGE:
581 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
582 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
583 print("See also: ^2settemp_restore^7\n");
589 void GenericCommand_settemp_restore(float request, float argc)
593 case CMD_REQUEST_COMMAND:
595 float i = cvar_settemp_restore();
598 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
600 dprint("Nothing to restore.\n");
606 case CMD_REQUEST_USAGE:
608 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
609 print(" No arguments required.\n");
610 print("See also: ^2settemp^7\n");
616 void GenericCommand_runtest(float request, float argc)
620 case CMD_REQUEST_COMMAND:
625 for(i = 1; i < argc; ++i)
634 case CMD_REQUEST_USAGE:
636 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
642 /* use this when creating a new command, making sure to place it in alphabetical order... also,
643 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
644 void GenericCommand_(float request)
648 case CMD_REQUEST_COMMAND:
655 case CMD_REQUEST_USAGE:
657 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
658 print(" No arguments required.\n");
665 // ==================================
666 // Macro system for server commands
667 // ==================================
669 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
670 #define GENERIC_COMMANDS(request,arguments,command) \
671 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
672 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
673 GENERIC_COMMAND("dumpitems", GenericCommand_dumpitems(request), "Dump all items to the console") \
674 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
675 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
676 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
677 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
678 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
679 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
680 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
681 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
682 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
683 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
684 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
687 void GenericCommand_macro_help()
689 #define GENERIC_COMMAND(name,function,description) \
690 { print(" ^2", name, "^7: ", description, "\n"); }
692 GENERIC_COMMANDS(0, 0, "");
693 #undef GENERIC_COMMAND
698 float GenericCommand_macro_command(float argc, string command)
700 #define GENERIC_COMMAND(name,function,description) \
701 { if(name == strtolower(argv(0))) { function; return true; } }
703 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
704 #undef GENERIC_COMMAND
709 float GenericCommand_macro_usage(float argc)
711 #define GENERIC_COMMAND(name,function,description) \
712 { if(name == strtolower(argv(1))) { function; return true; } }
714 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
715 #undef GENERIC_COMMAND
720 void GenericCommand_macro_write_aliases(float fh)
722 #define GENERIC_COMMAND(name,function,description) \
723 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
725 GENERIC_COMMANDS(0, 0, "");
726 #undef GENERIC_COMMAND
732 // ===========================================
733 // Main Common Function For Generic Commands
734 // ===========================================
735 // Commands spread out among all programs (menu, client, and server)
737 float GenericCommand(string command)
739 float argc = tokenize_console(command);
744 // Guide for working with argc arguments by example:
745 // argc: 1 - 2 - 3 - 4
746 // argv: 0 - 1 - 2 - 3
747 // cmd vote - master - login - password
749 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
751 return true; // handled by one of the above GenericCommand_* functions
753 else if(argc >= 3 && argv(0) == "red")
755 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
756 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
759 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
762 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
764 n = floor(random() * 6 + 2);
767 for(i = 0; i < n; ++i)
769 s2 = strcat(s2, "AH");
773 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
779 s = strcat(s, " ", s2);
781 s = strcat(s2, " ", s);
783 s2 = substring(s, strlen(s) - 2, 2);
784 if(s2 == "AH" || s2 == "AY")
787 s = strcat(s, " ))");
790 s = substring(s, 0, strlen(s) - 1);
793 s = strconv(1, 0, 0, s);
795 localcmd(strcat(argv(1), " ", s));
799 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
801 // test case for terencehill's color codes
802 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
806 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
809 for(i = 0; i < n; ++i)
811 c = substring(s, i, 1);
818 if(substring(s, i+1, 1) == "^")
824 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
825 c = strcat(rgb_to_hexcolor(rgb), c);
830 localcmd(strcat(argv(1), " ", s2));