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 GenericCommand_dumpitems(float request)
247 case CMD_REQUEST_COMMAND:
254 case CMD_REQUEST_USAGE:
256 LOG_INFOF("\nUsage:^3 %s dumpitems", GetProgramCommandPrefix());
262 void GenericCommand_dumpnotifs(float request)
266 case CMD_REQUEST_COMMAND:
269 float fh, alsoprint = false;
270 string filename = argv(1);
274 filename = "notifications_dump.cfg";
277 else if(filename == "-")
279 filename = "notifications_dump.cfg";
282 fh = fopen(filename, FILE_WRITE);
286 Dump_Notifications(fh, alsoprint);
287 LOG_INFOF("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
292 LOG_INFOF("^1Error: ^7Could not open file '%s'!\n", filename);
295 LOG_INFO(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
301 case CMD_REQUEST_USAGE:
303 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
304 LOG_INFO(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
305 LOG_INFO(" if supplied with '-' output to console as well as default,\n");
306 LOG_INFO(" if left blank, it will only write to default.\n");
312 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
316 case CMD_REQUEST_COMMAND:
319 wep_config_file = -1;
320 wep_config_alsoprint = -1;
321 string filename = argv(1);
325 filename = "weapons_dump.cfg";
326 wep_config_alsoprint = false;
328 else if(filename == "-")
330 filename = "weapons_dump.cfg";
331 wep_config_alsoprint = true;
333 wep_config_file = fopen(filename, FILE_WRITE);
335 if(wep_config_file >= 0)
337 Dump_Weapon_Settings();
338 LOG_INFO(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
339 fclose(wep_config_file);
340 wep_config_file = -1;
341 wep_config_alsoprint = -1;
345 LOG_INFO(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
348 LOG_INFO(_("Weapons dump command only works with sv_cmd.\n"));
354 case CMD_REQUEST_USAGE:
356 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
357 LOG_INFO(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
358 LOG_INFO(" if supplied with '-' output to console as well as default,\n");
359 LOG_INFO(" if left blank, it will only write to default.\n");
365 void GenericCommand_dumpturrets(float request)
369 case CMD_REQUEST_COMMAND:
372 tur_config_file = -1;
373 tur_config_alsoprint = -1;
374 string filename = argv(1);
378 filename = "turrets_dump.cfg";
379 tur_config_alsoprint = FALSE;
381 else if(filename == "-")
383 filename = "turrets_dump.cfg";
384 tur_config_alsoprint = TRUE;
386 tur_config_file = fopen(filename, FILE_WRITE);
388 if(tur_config_file >= 0)
390 Dump_Turret_Settings();
391 LOG_INFO(sprintf("Dumping turrets... File located in ^2data/data/%s^7.\n", filename));
392 fclose(tur_config_file);
393 tur_config_file = -1;
394 tur_config_alsoprint = -1;
398 LOG_INFO(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
401 LOG_INFO(_("Turrets dump command only works with sv_cmd.\n"));
407 case CMD_REQUEST_USAGE:
409 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpturrets [filename]"));
410 LOG_INFO(" Where 'filename' is the file to write (default is turrets_dump.cfg),\n");
411 LOG_INFO(" if supplied with '-' output to console as well as default,\n");
412 LOG_INFO(" if left blank, it will only write to default.\n");
418 void GenericCommand_maplist(float request, float argc)
422 case CMD_REQUEST_COMMAND:
429 case "add": // appends new maps to the maplist
433 if (!fexists(strcat("maps/", argv(2), ".bsp")))
435 LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!\n");
439 if(cvar_string("g_maplist") == "")
440 cvar_set("g_maplist", argv(2));
442 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
446 break; // go to usage
449 case "cleanup": // scans maplist and only adds back the ones which are really usable
452 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
453 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
456 for(i = 0; i < argc; ++i)
457 if(MapInfo_CheckMap(argv(i)))
458 tmp_string = strcat(tmp_string, " ", argv(i));
460 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
461 cvar_set("g_maplist", tmp_string);
466 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
470 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
473 for(i = 0; i < argc; ++i)
474 if(argv(i) != argv(2))
475 tmp_string = strcat(tmp_string, " ", argv(i));
477 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
478 cvar_set("g_maplist", tmp_string);
482 break; // go to usage
485 case "shuffle": // randomly shuffle the maplist
487 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
496 LOG_INFO("Incorrect parameters for ^2maplist^7\n");
497 case CMD_REQUEST_USAGE:
499 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
500 LOG_INFO(" Where 'action' is the command to complete,\n");
501 LOG_INFO(" and 'map' is what it acts upon (if required).\n");
502 LOG_INFO(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
508 void GenericCommand_nextframe(float request, float arguments, string command)
512 case CMD_REQUEST_COMMAND:
514 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
519 case CMD_REQUEST_USAGE:
521 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
522 LOG_INFO(" Where command will be executed next frame of this VM\n");
528 void GenericCommand_removefromlist(float request, float argc)
532 case CMD_REQUEST_COMMAND:
537 string original_cvar = argv(1);
538 string removal = argv(2);
541 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
544 for(i = 0; i < argc; ++i)
545 if(argv(i) != removal)
546 tmp_string = strcat(tmp_string, " ", argv(i));
548 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
549 cvar_set(original_cvar, tmp_string);
556 LOG_INFO("Incorrect parameters for ^2removefromlist^7\n");
557 case CMD_REQUEST_USAGE:
559 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
560 LOG_INFO(" Where 'variable' is what cvar to remove 'value' from.\n");
561 LOG_INFO("See also: ^2addtolist^7\n");
567 void GenericCommand_restartnotifs(float request)
571 case CMD_REQUEST_COMMAND:
576 "Restart_Notifications(): Restarting %d notifications... ",
577 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
592 Destroy_All_Notifications();
593 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
595 LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
601 case CMD_REQUEST_USAGE:
603 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
604 LOG_INFO(" No arguments required.\n");
610 void GenericCommand_settemp(float request, float argc)
614 case CMD_REQUEST_COMMAND:
618 float f = cvar_settemp(argv(1), argv(2));
620 LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
622 LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
623 // else cvar_settemp itself errors out
630 LOG_INFO("Incorrect parameters for ^2settemp^7\n");
631 case CMD_REQUEST_USAGE:
633 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
634 LOG_INFO(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
635 LOG_INFO("See also: ^2settemp_restore^7\n");
641 void GenericCommand_settemp_restore(float request, float argc)
645 case CMD_REQUEST_COMMAND:
647 float i = cvar_settemp_restore();
650 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
652 LOG_TRACE("Nothing to restore.\n");
658 case CMD_REQUEST_USAGE:
660 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
661 LOG_INFO(" No arguments required.\n");
662 LOG_INFO("See also: ^2settemp^7\n");
668 void GenericCommand_runtest(float request, float argc)
672 case CMD_REQUEST_COMMAND:
677 for(i = 1; i < argc; ++i)
686 case CMD_REQUEST_USAGE:
688 LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
694 /* use this when creating a new command, making sure to place it in alphabetical order... also,
695 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
696 void GenericCommand_(float request)
700 case CMD_REQUEST_COMMAND:
707 case CMD_REQUEST_USAGE:
709 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
710 print(" No arguments required.\n");
717 // ==================================
718 // Macro system for server commands
719 // ==================================
721 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
722 #define GENERIC_COMMANDS(request,arguments,command) \
723 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
724 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
725 GENERIC_COMMAND("dumpitems", GenericCommand_dumpitems(request), "Dump all items to the console") \
726 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
727 GENERIC_COMMAND("dumpturrets", GenericCommand_dumpturrets(request), "Dump all turrets into turrets_dump.txt") \
728 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
729 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
730 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
731 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
732 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
733 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
734 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
735 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
736 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
737 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
740 void GenericCommand_macro_help()
742 #define GENERIC_COMMAND(name,function,description) \
743 { LOG_INFO(" ^2", name, "^7: ", description, "\n"); }
745 GENERIC_COMMANDS(0, 0, "");
746 #undef GENERIC_COMMAND
751 float GenericCommand_macro_command(float argc, string command)
753 #define GENERIC_COMMAND(name,function,description) \
754 { if(name == strtolower(argv(0))) { function; return true; } }
756 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
757 #undef GENERIC_COMMAND
762 float GenericCommand_macro_usage(float argc)
764 #define GENERIC_COMMAND(name,function,description) \
765 { if(name == strtolower(argv(1))) { function; return true; } }
767 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
768 #undef GENERIC_COMMAND
773 void GenericCommand_macro_write_aliases(float fh)
775 #define GENERIC_COMMAND(name,function,description) \
776 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
778 GENERIC_COMMANDS(0, 0, "");
779 #undef GENERIC_COMMAND
785 // ===========================================
786 // Main Common Function For Generic Commands
787 // ===========================================
788 // Commands spread out among all programs (menu, client, and server)
790 float GenericCommand(string command)
792 float argc = tokenize_console(command);
797 // Guide for working with argc arguments by example:
798 // argc: 1 - 2 - 3 - 4
799 // argv: 0 - 1 - 2 - 3
800 // cmd vote - master - login - password
802 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
804 return true; // handled by one of the above GenericCommand_* functions
806 else if(argc >= 3 && argv(0) == "red")
808 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
809 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
812 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
815 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
817 n = floor(random() * 6 + 2);
820 for(i = 0; i < n; ++i)
822 s2 = strcat(s2, "AH");
826 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
832 s = strcat(s, " ", s2);
834 s = strcat(s2, " ", s);
836 s2 = substring(s, strlen(s) - 2, 2);
837 if(s2 == "AH" || s2 == "AY")
840 s = strcat(s, " ))");
843 s = substring(s, 0, strlen(s) - 1);
846 s = strconv(1, 0, 0, s);
848 localcmd(strcat(argv(1), " ", s));
852 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
854 // test case for terencehill's color codes
855 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
859 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
862 for(i = 0; i < n; ++i)
864 c = substring(s, i, 1);
871 if(substring(s, i+1, 1) == "^")
877 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
878 c = strcat(rgb_to_hexcolor(rgb), c);
883 localcmd(strcat(argv(1), " ", s2));