7 #include "../mapinfo.qh"
10 #include "../notifications.qh"
14 #include "../../client/command/cl_cmd.qh"
18 #include "../../server/command/banning.qh"
19 #include "../../server/command/cmd.qh"
20 #include "../../server/command/common.qh"
21 #include "../../server/command/sv_cmd.qh"
22 #include "../../common/turrets/config.qh"
23 #include "../../common/weapons/config.qh"
26 // =========================================================
27 // Generic program common command code, written by Samual
28 // Last updated: February 19th, 2012
29 // =========================================================
31 // used by generic commands for better help/usage information
32 string GetProgramCommandPrefix(void)
45 // used by curl command
46 void Curl_URI_Get_Callback(int id, float status, string data)
48 int i = id - URI_GET_CURL;
49 float do_exec = curl_uri_get_exec[i];
50 string do_cvar = curl_uri_get_cvar[i];
53 LOG_TRACEF("error: status is %d\n", status);
62 cvar_set(do_cvar, data);
71 // =======================
72 // Command Sub-Functions
73 // =======================
75 void GenericCommand_addtolist(float request, float argc)
79 case CMD_REQUEST_COMMAND:
83 string original_cvar = argv(1);
84 string tmp_string = argv(2);
86 if(cvar_string(original_cvar) == "") // cvar was empty
88 cvar_set(original_cvar, tmp_string);
90 else // add it to the end of the list if the list doesn't already have it
92 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
94 for(i = 0; i < argc; ++i)
95 if(argv(i) == tmp_string)
96 return; // already in list
98 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
105 LOG_INFO("Incorrect parameters for ^2addtolist^7\n");
106 case CMD_REQUEST_USAGE:
108 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
109 LOG_INFO(" Where 'variable' is what to add 'value' to.\n");
110 LOG_INFO("See also: ^2removefromlist^7\n");
116 void GenericCommand_qc_curl(float request, float argc)
120 case CMD_REQUEST_COMMAND:
122 bool do_exec = false;
123 string do_cvar = string_null;
126 for(i = 1; i+1 < argc; ++i)
128 if(argv(i) == "--cvar" && i+2 < argc)
131 do_cvar = strzone(argv(i));
134 if(argv(i) == "--exec")
139 if(argv(i) == "--key" && i+2 < argc)
148 // now, argv(i) is the URL
149 // following args may be POST parameters
150 string url = argv(i);
152 float buf = buf_create();
154 for(j = 0; i+1 < argc; i += 2)
155 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
157 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
160 if(j == 0) // no args: GET
161 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
162 else // with args: POST
163 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
167 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
168 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
169 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
172 LOG_INFO(_("error creating curl handle\n"));
180 case CMD_REQUEST_USAGE:
182 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
188 void GenericCommand_dumpcommands(float request)
192 case CMD_REQUEST_COMMAND:
195 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
196 fh = fopen(filename, FILE_WRITE);
201 CMD_Write("dump of server console commands:\n");
202 GameCommand_macro_write_aliases(fh);
204 CMD_Write("\ndump of networked client only commands:\n");
205 ClientCommand_macro_write_aliases(fh);
207 CMD_Write("\ndump of common commands:\n");
208 CommonCommand_macro_write_aliases(fh);
210 CMD_Write("\ndump of ban commands:\n");
211 BanCommand_macro_write_aliases(fh);
215 CMD_Write("dump of client commands:\n");
216 LocalCommand_macro_write_aliases(fh);
219 CMD_Write("\ndump of generic commands:\n");
220 GenericCommand_macro_write_aliases(fh);
222 LOG_INFO("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
228 LOG_INFO("^1Error: ^7Could not dump to file!\n");
234 case CMD_REQUEST_USAGE:
236 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
237 LOG_INFO(" No arguments required.\n");
243 void effectinfo_dump(int fh, bool alsoprint);
244 void GenericCommand_dumpeffectinfo(float request)
247 case CMD_REQUEST_COMMAND: {
249 string filename = argv(1);
250 bool alsoprint = false;
251 if (filename == "") {
252 filename = "effectinfo_dump.txt";
254 } else if (filename == "-") {
255 filename = "effectinfo_dump.txt";
258 int fh = fopen(filename, FILE_WRITE);
260 effectinfo_dump(fh, alsoprint);
261 LOG_INFOF("Dumping effectinfo... File located at ^2data/data/%s^7.\n", filename);
262 LOG_INFOF("Reload with ^2cl_particles_reloadeffects data/%s^7.\n", filename);
265 LOG_WARNINGF("Could not open file '%s'!\n", filename);
268 LOG_INFO(_("Effectinfo dump command only works with cl_cmd and sv_cmd.\n"));
273 case CMD_REQUEST_USAGE: {
274 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpeffectinfo [filename]"));
275 LOG_INFO(" Where 'filename' is the file to write (default is effectinfo_dump.txt),\n");
276 LOG_INFO(" if supplied with '-' output to console as well as default,\n");
277 LOG_INFO(" if left blank, it will only write to default.\n");
282 STATIC_INIT(dumpeffectinfo) { localcmd("alias dumpeffectinfo \"qc_cmd_svcl dumpeffectinfo ${* ?}\"\n"); }
284 void GenericCommand_dumpitems(float request)
288 case CMD_REQUEST_COMMAND:
295 case CMD_REQUEST_USAGE:
297 LOG_INFOF("\nUsage:^3 %s dumpitems", GetProgramCommandPrefix());
303 void GenericCommand_dumpnotifs(float request)
307 case CMD_REQUEST_COMMAND:
310 float fh, alsoprint = false;
311 string filename = argv(1);
315 filename = "notifications_dump.cfg";
318 else if(filename == "-")
320 filename = "notifications_dump.cfg";
323 fh = fopen(filename, FILE_WRITE);
327 Dump_Notifications(fh, alsoprint);
328 LOG_INFOF("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
333 LOG_INFOF("^1Error: ^7Could not open file '%s'!\n", filename);
336 LOG_INFO(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
342 case CMD_REQUEST_USAGE:
344 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
345 LOG_INFO(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
346 LOG_INFO(" if supplied with '-' output to console as well as default,\n");
347 LOG_INFO(" if left blank, it will only write to default.\n");
353 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
357 case CMD_REQUEST_COMMAND:
360 wep_config_file = -1;
361 wep_config_alsoprint = -1;
362 string filename = argv(1);
366 filename = "weapons_dump.cfg";
367 wep_config_alsoprint = false;
369 else if(filename == "-")
371 filename = "weapons_dump.cfg";
372 wep_config_alsoprint = true;
374 wep_config_file = fopen(filename, FILE_WRITE);
376 if(wep_config_file >= 0)
378 Dump_Weapon_Settings();
379 LOG_INFO(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
380 fclose(wep_config_file);
381 wep_config_file = -1;
382 wep_config_alsoprint = -1;
386 LOG_INFO(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
389 LOG_INFO(_("Weapons dump command only works with sv_cmd.\n"));
395 case CMD_REQUEST_USAGE:
397 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
398 LOG_INFO(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
399 LOG_INFO(" if supplied with '-' output to console as well as default,\n");
400 LOG_INFO(" if left blank, it will only write to default.\n");
406 void GenericCommand_dumpturrets(float request)
410 case CMD_REQUEST_COMMAND:
413 tur_config_file = -1;
414 tur_config_alsoprint = -1;
415 string filename = argv(1);
419 filename = "turrets_dump.cfg";
420 tur_config_alsoprint = FALSE;
422 else if(filename == "-")
424 filename = "turrets_dump.cfg";
425 tur_config_alsoprint = TRUE;
427 tur_config_file = fopen(filename, FILE_WRITE);
429 if(tur_config_file >= 0)
431 Dump_Turret_Settings();
432 LOG_INFO(sprintf("Dumping turrets... File located in ^2data/data/%s^7.\n", filename));
433 fclose(tur_config_file);
434 tur_config_file = -1;
435 tur_config_alsoprint = -1;
439 LOG_INFO(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
442 LOG_INFO(_("Turrets dump command only works with sv_cmd.\n"));
448 case CMD_REQUEST_USAGE:
450 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpturrets [filename]"));
451 LOG_INFO(" Where 'filename' is the file to write (default is turrets_dump.cfg),\n");
452 LOG_INFO(" if supplied with '-' output to console as well as default,\n");
453 LOG_INFO(" if left blank, it will only write to default.\n");
459 void GenericCommand_maplist(float request, float argc)
463 case CMD_REQUEST_COMMAND:
470 case "add": // appends new maps to the maplist
474 if (!fexists(strcat("maps/", argv(2), ".bsp")))
476 LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!\n");
480 if(cvar_string("g_maplist") == "")
481 cvar_set("g_maplist", argv(2));
483 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
487 break; // go to usage
490 case "cleanup": // scans maplist and only adds back the ones which are really usable
493 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
494 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
497 for(i = 0; i < argc; ++i)
498 if(MapInfo_CheckMap(argv(i)))
499 tmp_string = strcat(tmp_string, " ", argv(i));
501 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
502 cvar_set("g_maplist", tmp_string);
507 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
511 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
514 for(i = 0; i < argc; ++i)
515 if(argv(i) != argv(2))
516 tmp_string = strcat(tmp_string, " ", argv(i));
518 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
519 cvar_set("g_maplist", tmp_string);
523 break; // go to usage
526 case "shuffle": // randomly shuffle the maplist
528 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
537 LOG_INFO("Incorrect parameters for ^2maplist^7\n");
538 case CMD_REQUEST_USAGE:
540 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
541 LOG_INFO(" Where 'action' is the command to complete,\n");
542 LOG_INFO(" and 'map' is what it acts upon (if required).\n");
543 LOG_INFO(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
549 void GenericCommand_nextframe(float request, float arguments, string command)
553 case CMD_REQUEST_COMMAND:
555 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
560 case CMD_REQUEST_USAGE:
562 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
563 LOG_INFO(" Where command will be executed next frame of this VM\n");
569 void GenericCommand_removefromlist(float request, float argc)
573 case CMD_REQUEST_COMMAND:
578 string original_cvar = argv(1);
579 string removal = argv(2);
582 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
585 for(i = 0; i < argc; ++i)
586 if(argv(i) != removal)
587 tmp_string = strcat(tmp_string, " ", argv(i));
589 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
590 cvar_set(original_cvar, tmp_string);
597 LOG_INFO("Incorrect parameters for ^2removefromlist^7\n");
598 case CMD_REQUEST_USAGE:
600 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
601 LOG_INFO(" Where 'variable' is what cvar to remove 'value' from.\n");
602 LOG_INFO("See also: ^2addtolist^7\n");
608 void GenericCommand_restartnotifs(float request)
612 case CMD_REQUEST_COMMAND:
617 "Restart_Notifications(): Restarting %d notifications... ",
618 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
633 Destroy_All_Notifications();
634 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
636 LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
642 case CMD_REQUEST_USAGE:
644 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
645 LOG_INFO(" No arguments required.\n");
651 void GenericCommand_settemp(float request, float argc)
655 case CMD_REQUEST_COMMAND:
659 float f = cvar_settemp(argv(1), argv(2));
661 LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
663 LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
664 // else cvar_settemp itself errors out
671 LOG_INFO("Incorrect parameters for ^2settemp^7\n");
672 case CMD_REQUEST_USAGE:
674 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
675 LOG_INFO(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
676 LOG_INFO("See also: ^2settemp_restore^7\n");
682 void GenericCommand_settemp_restore(float request, float argc)
686 case CMD_REQUEST_COMMAND:
688 float i = cvar_settemp_restore();
691 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
693 LOG_TRACE("Nothing to restore.\n");
699 case CMD_REQUEST_USAGE:
701 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
702 LOG_INFO(" No arguments required.\n");
703 LOG_INFO("See also: ^2settemp^7\n");
709 void GenericCommand_runtest(float request, float argc)
713 case CMD_REQUEST_COMMAND:
718 for(i = 1; i < argc; ++i)
727 case CMD_REQUEST_USAGE:
729 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
735 /* use this when creating a new command, making sure to place it in alphabetical order... also,
736 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
737 void GenericCommand_(float request)
741 case CMD_REQUEST_COMMAND:
748 case CMD_REQUEST_USAGE:
750 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
751 print(" No arguments required.\n");
758 // ==================================
759 // Macro system for server commands
760 // ==================================
762 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
763 #define GENERIC_COMMANDS(request,arguments,command) \
764 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
765 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
766 GENERIC_COMMAND("dumpeffectinfo", GenericCommand_dumpeffectinfo(request), "Dump all effectinfo to effectinfo_dump.txt") \
767 GENERIC_COMMAND("dumpitems", GenericCommand_dumpitems(request), "Dump all items to the console") \
768 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
769 GENERIC_COMMAND("dumpturrets", GenericCommand_dumpturrets(request), "Dump all turrets into turrets_dump.txt") \
770 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
771 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
772 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
773 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
774 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
775 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
776 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
777 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
778 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
779 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
782 void GenericCommand_macro_help()
784 #define GENERIC_COMMAND(name,function,description) \
785 { LOG_INFO(" ^2", name, "^7: ", description, "\n"); }
787 GENERIC_COMMANDS(0, 0, "");
788 #undef GENERIC_COMMAND
793 float GenericCommand_macro_command(float argc, string command)
795 #define GENERIC_COMMAND(name,function,description) \
796 { if(name == strtolower(argv(0))) { function; return true; } }
798 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
799 #undef GENERIC_COMMAND
804 float GenericCommand_macro_usage(float argc)
806 #define GENERIC_COMMAND(name,function,description) \
807 { if(name == strtolower(argv(1))) { function; return true; } }
809 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
810 #undef GENERIC_COMMAND
815 void GenericCommand_macro_write_aliases(float fh)
817 #define GENERIC_COMMAND(name,function,description) \
818 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
820 GENERIC_COMMANDS(0, 0, "");
821 #undef GENERIC_COMMAND
827 // ===========================================
828 // Main Common Function For Generic Commands
829 // ===========================================
830 // Commands spread out among all programs (menu, client, and server)
832 float GenericCommand(string command)
834 float argc = tokenize_console(command);
839 // Guide for working with argc arguments by example:
840 // argc: 1 - 2 - 3 - 4
841 // argv: 0 - 1 - 2 - 3
842 // cmd vote - master - login - password
844 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
846 return true; // handled by one of the above GenericCommand_* functions
848 else if(argc >= 3 && argv(0) == "red")
850 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
851 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
854 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
857 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
859 n = floor(random() * 6 + 2);
862 for(i = 0; i < n; ++i)
864 s2 = strcat(s2, "AH");
868 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
874 s = strcat(s, " ", s2);
876 s = strcat(s2, " ", s);
878 s2 = substring(s, strlen(s) - 2, 2);
879 if(s2 == "AH" || s2 == "AY")
882 s = strcat(s, " ))");
885 s = substring(s, 0, strlen(s) - 1);
888 s = strconv(1, 0, 0, s);
890 localcmd(strcat(argv(1), " ", s));
894 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
896 // test case for terencehill's color codes
897 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
901 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
904 for(i = 0; i < n; ++i)
906 c = substring(s, i, 1);
913 if(substring(s, i+1, 1) == "^")
919 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
920 c = strcat(rgb_to_hexcolor(rgb), c);
925 localcmd(strcat(argv(1), " ", s2));