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_dumpnotifs(float request)
248 case CMD_REQUEST_COMMAND:
251 float fh, alsoprint = false;
252 string filename = argv(1);
256 filename = "notifications_dump.cfg";
259 else if(filename == "-")
261 filename = "notifications_dump.cfg";
264 fh = fopen(filename, FILE_WRITE);
268 Dump_Notifications(fh, alsoprint);
269 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
274 printf("^1Error: ^7Could not open file '%s'!\n", filename);
277 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
283 case CMD_REQUEST_USAGE:
285 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
286 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
287 print(" if supplied with '-' output to console as well as default,\n");
288 print(" if left blank, it will only write to default.\n");
294 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
298 case CMD_REQUEST_COMMAND:
301 wep_config_file = -1;
302 wep_config_alsoprint = -1;
303 string filename = argv(1);
307 filename = "weapons_dump.cfg";
308 wep_config_alsoprint = false;
310 else if(filename == "-")
312 filename = "weapons_dump.cfg";
313 wep_config_alsoprint = true;
315 wep_config_file = fopen(filename, FILE_WRITE);
317 if(wep_config_file >= 0)
319 Dump_Weapon_Settings();
320 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
321 fclose(wep_config_file);
322 wep_config_file = -1;
323 wep_config_alsoprint = -1;
327 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
330 print(_("Weapons dump command only works with sv_cmd.\n"));
336 case CMD_REQUEST_USAGE:
338 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
339 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
340 print(" if supplied with '-' output to console as well as default,\n");
341 print(" if left blank, it will only write to default.\n");
347 void GenericCommand_maplist(float request, float argc)
351 case CMD_REQUEST_COMMAND:
358 case "add": // appends new maps to the maplist
362 if (!fexists(strcat("maps/", argv(2), ".bsp")))
364 print("maplist: ERROR: ", argv(2), " does not exist!\n");
368 if(cvar_string("g_maplist") == "")
369 cvar_set("g_maplist", argv(2));
371 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
375 break; // go to usage
378 case "cleanup": // scans maplist and only adds back the ones which are really usable
381 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
382 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
385 for(i = 0; i < argc; ++i)
386 if(MapInfo_CheckMap(argv(i)))
387 tmp_string = strcat(tmp_string, " ", argv(i));
389 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
390 cvar_set("g_maplist", tmp_string);
395 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
399 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
402 for(i = 0; i < argc; ++i)
403 if(argv(i) != argv(2))
404 tmp_string = strcat(tmp_string, " ", argv(i));
406 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
407 cvar_set("g_maplist", tmp_string);
411 break; // go to usage
414 case "shuffle": // randomly shuffle the maplist
416 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
425 print("Incorrect parameters for ^2maplist^7\n");
426 case CMD_REQUEST_USAGE:
428 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
429 print(" Where 'action' is the command to complete,\n");
430 print(" and 'map' is what it acts upon (if required).\n");
431 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
437 void GenericCommand_nextframe(float request, float arguments, string command)
441 case CMD_REQUEST_COMMAND:
443 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
448 case CMD_REQUEST_USAGE:
450 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
451 print(" Where command will be executed next frame of this VM\n");
457 void GenericCommand_removefromlist(float request, float argc)
461 case CMD_REQUEST_COMMAND:
466 string original_cvar = argv(1);
467 string removal = argv(2);
470 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
473 for(i = 0; i < argc; ++i)
474 if(argv(i) != removal)
475 tmp_string = strcat(tmp_string, " ", argv(i));
477 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
478 cvar_set(original_cvar, tmp_string);
485 print("Incorrect parameters for ^2removefromlist^7\n");
486 case CMD_REQUEST_USAGE:
488 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
489 print(" Where 'variable' is what cvar to remove 'value' from.\n");
490 print("See also: ^2addtolist^7\n");
496 void GenericCommand_restartnotifs(float request)
500 case CMD_REQUEST_COMMAND:
505 "Restart_Notifications(): Restarting %d notifications... ",
506 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
521 Destroy_All_Notifications();
522 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
524 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
530 case CMD_REQUEST_USAGE:
532 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
533 print(" No arguments required.\n");
539 void GenericCommand_settemp(float request, float argc)
543 case CMD_REQUEST_COMMAND:
547 float f = cvar_settemp(argv(1), argv(2));
549 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
551 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
552 // else cvar_settemp itself errors out
559 print("Incorrect parameters for ^2settemp^7\n");
560 case CMD_REQUEST_USAGE:
562 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
563 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
564 print("See also: ^2settemp_restore^7\n");
570 void GenericCommand_settemp_restore(float request, float argc)
574 case CMD_REQUEST_COMMAND:
576 float i = cvar_settemp_restore();
579 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
581 dprint("Nothing to restore.\n");
587 case CMD_REQUEST_USAGE:
589 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
590 print(" No arguments required.\n");
591 print("See also: ^2settemp^7\n");
597 void GenericCommand_runtest(float request, float argc)
601 case CMD_REQUEST_COMMAND:
606 for(i = 1; i < argc; ++i)
615 case CMD_REQUEST_USAGE:
617 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
623 /* use this when creating a new command, making sure to place it in alphabetical order... also,
624 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
625 void GenericCommand_(float request)
629 case CMD_REQUEST_COMMAND:
636 case CMD_REQUEST_USAGE:
638 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
639 print(" No arguments required.\n");
646 // ==================================
647 // Macro system for server commands
648 // ==================================
650 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
651 #define GENERIC_COMMANDS(request,arguments,command) \
652 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
653 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
654 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
655 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
656 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
657 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
658 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
659 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
660 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
661 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
662 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
663 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
664 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
667 void GenericCommand_macro_help()
669 #define GENERIC_COMMAND(name,function,description) \
670 { print(" ^2", name, "^7: ", description, "\n"); }
672 GENERIC_COMMANDS(0, 0, "");
673 #undef GENERIC_COMMAND
678 float GenericCommand_macro_command(float argc, string command)
680 #define GENERIC_COMMAND(name,function,description) \
681 { if(name == strtolower(argv(0))) { function; return true; } }
683 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
684 #undef GENERIC_COMMAND
689 float GenericCommand_macro_usage(float argc)
691 #define GENERIC_COMMAND(name,function,description) \
692 { if(name == strtolower(argv(1))) { function; return true; } }
694 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
695 #undef GENERIC_COMMAND
700 void GenericCommand_macro_write_aliases(float fh)
702 #define GENERIC_COMMAND(name,function,description) \
703 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
705 GENERIC_COMMANDS(0, 0, "");
706 #undef GENERIC_COMMAND
712 // ===========================================
713 // Main Common Function For Generic Commands
714 // ===========================================
715 // Commands spread out among all programs (menu, client, and server)
717 float GenericCommand(string command)
719 float argc = tokenize_console(command);
724 // Guide for working with argc arguments by example:
725 // argc: 1 - 2 - 3 - 4
726 // argv: 0 - 1 - 2 - 3
727 // cmd vote - master - login - password
729 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
731 return true; // handled by one of the above GenericCommand_* functions
733 else if(argc >= 3 && argv(0) == "red")
735 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
736 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
739 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
742 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
744 n = floor(random() * 6 + 2);
747 for(i = 0; i < n; ++i)
749 s2 = strcat(s2, "AH");
753 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
759 s = strcat(s, " ", s2);
761 s = strcat(s2, " ", s);
763 s2 = substring(s, strlen(s) - 2, 2);
764 if(s2 == "AH" || s2 == "AY")
767 s = strcat(s, " ))");
770 s = substring(s, 0, strlen(s) - 1);
773 s = strconv(1, 0, 0, s);
775 localcmd(strcat(argv(1), " ", s));
779 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
781 // test case for terencehill's color codes
782 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
786 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
789 for(i = 0; i < n; ++i)
791 c = substring(s, i, 1);
798 if(substring(s, i+1, 1) == "^")
804 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
805 c = strcat(rgb_to_hexcolor(rgb), c);
810 localcmd(strcat(argv(1), " ", s2));