2 #include "../../dpdefs/csprogsdefs.qh"
3 #include "../constants.qh"
5 #include "../mapinfo.qh"
7 #include "shared_defs.qh"
8 #include "../../client/command/cl_cmd.qh"
9 #include "../notifications.qh"
12 #include "../../dpdefs/progsdefs.qh"
13 #include "../../dpdefs/dpextensions.qh"
14 #include "../constants.qh"
18 #include "shared_defs.qh"
19 #include "../weapons/config.qh"
20 #include "../notifications.qh"
21 #include "../mapinfo.qh"
22 #include "../../server/command/common.qh"
23 #include "../../server/command/banning.qh"
24 #include "../../server/command/cmd.qh"
25 #include "../../server/command/sv_cmd.qh"
28 // =========================================================
29 // Generic program common command code, written by Samual
30 // Last updated: February 19th, 2012
31 // =========================================================
33 // used by generic commands for better help/usage information
34 string GetProgramCommandPrefix(void)
47 // used by curl command
48 void Curl_URI_Get_Callback(int id, float status, string data)
50 int i = id - URI_GET_CURL;
51 float do_exec = curl_uri_get_exec[i];
52 string do_cvar = curl_uri_get_cvar[i];
55 dprintf("error: status is %d\n", status);
64 cvar_set(do_cvar, data);
73 // =======================
74 // Command Sub-Functions
75 // =======================
77 void GenericCommand_addtolist(float request, float argc)
81 case CMD_REQUEST_COMMAND:
85 string original_cvar = argv(1);
86 string tmp_string = argv(2);
88 if(cvar_string(original_cvar) == "") // cvar was empty
90 cvar_set(original_cvar, tmp_string);
92 else // add it to the end of the list if the list doesn't already have it
94 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
96 for(i = 0; i < argc; ++i)
97 if(argv(i) == tmp_string)
98 return; // already in list
100 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
107 print("Incorrect parameters for ^2addtolist^7\n");
108 case CMD_REQUEST_USAGE:
110 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
111 print(" Where 'variable' is what to add 'value' to.\n");
112 print("See also: ^2removefromlist^7\n");
118 void GenericCommand_qc_curl(float request, float argc)
122 case CMD_REQUEST_COMMAND:
124 bool do_exec = false;
125 string do_cvar = string_null;
128 for(i = 1; i+1 < argc; ++i)
130 if(argv(i) == "--cvar" && i+2 < argc)
133 do_cvar = strzone(argv(i));
136 if(argv(i) == "--exec")
141 if(argv(i) == "--key" && i+2 < argc)
150 // now, argv(i) is the URL
151 // following args may be POST parameters
152 string url = argv(i);
154 float buf = buf_create();
156 for(j = 0; i+1 < argc; i += 2)
157 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
159 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
162 if(j == 0) // no args: GET
163 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
164 else // with args: POST
165 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
169 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
170 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
171 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
174 print(_("error creating curl handle\n"));
182 case CMD_REQUEST_USAGE:
184 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
190 void GenericCommand_dumpcommands(float request)
194 case CMD_REQUEST_COMMAND:
197 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
198 fh = fopen(filename, FILE_WRITE);
203 CMD_Write("dump of server console commands:\n");
204 GameCommand_macro_write_aliases(fh);
206 CMD_Write("\ndump of networked client only commands:\n");
207 ClientCommand_macro_write_aliases(fh);
209 CMD_Write("\ndump of common commands:\n");
210 CommonCommand_macro_write_aliases(fh);
212 CMD_Write("\ndump of ban commands:\n");
213 BanCommand_macro_write_aliases(fh);
217 CMD_Write("dump of client commands:\n");
218 LocalCommand_macro_write_aliases(fh);
221 CMD_Write("\ndump of generic commands:\n");
222 GenericCommand_macro_write_aliases(fh);
224 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
230 print("^1Error: ^7Could not dump to file!\n");
236 case CMD_REQUEST_USAGE:
238 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
239 print(" No arguments required.\n");
245 void GenericCommand_dumpnotifs(float request)
249 case CMD_REQUEST_COMMAND:
252 float fh, alsoprint = false;
253 string filename = argv(1);
257 filename = "notifications_dump.cfg";
260 else if(filename == "-")
262 filename = "notifications_dump.cfg";
265 fh = fopen(filename, FILE_WRITE);
269 Dump_Notifications(fh, alsoprint);
270 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
275 printf("^1Error: ^7Could not open file '%s'!\n", filename);
278 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
284 case CMD_REQUEST_USAGE:
286 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
287 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
288 print(" if supplied with '-' output to console as well as default,\n");
289 print(" if left blank, it will only write to default.\n");
295 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
299 case CMD_REQUEST_COMMAND:
302 wep_config_file = -1;
303 wep_config_alsoprint = -1;
304 string filename = argv(1);
308 filename = "weapons_dump.cfg";
309 wep_config_alsoprint = false;
311 else if(filename == "-")
313 filename = "weapons_dump.cfg";
314 wep_config_alsoprint = true;
316 wep_config_file = fopen(filename, FILE_WRITE);
318 if(wep_config_file >= 0)
320 Dump_Weapon_Settings();
321 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
322 fclose(wep_config_file);
323 wep_config_file = -1;
324 wep_config_alsoprint = -1;
328 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
331 print(_("Weapons dump command only works with sv_cmd.\n"));
337 case CMD_REQUEST_USAGE:
339 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
340 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
341 print(" if supplied with '-' output to console as well as default,\n");
342 print(" if left blank, it will only write to default.\n");
348 void GenericCommand_maplist(float request, float argc)
352 case CMD_REQUEST_COMMAND:
359 case "add": // appends new maps to the maplist
363 if (!fexists(strcat("maps/", argv(2), ".bsp")))
365 print("maplist: ERROR: ", argv(2), " does not exist!\n");
369 if(cvar_string("g_maplist") == "")
370 cvar_set("g_maplist", argv(2));
372 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
376 break; // go to usage
379 case "cleanup": // scans maplist and only adds back the ones which are really usable
382 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
383 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
386 for(i = 0; i < argc; ++i)
387 if(MapInfo_CheckMap(argv(i)))
388 tmp_string = strcat(tmp_string, " ", argv(i));
390 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
391 cvar_set("g_maplist", tmp_string);
396 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
400 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
403 for(i = 0; i < argc; ++i)
404 if(argv(i) != argv(2))
405 tmp_string = strcat(tmp_string, " ", argv(i));
407 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
408 cvar_set("g_maplist", tmp_string);
412 break; // go to usage
415 case "shuffle": // randomly shuffle the maplist
417 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
426 print("Incorrect parameters for ^2maplist^7\n");
427 case CMD_REQUEST_USAGE:
429 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
430 print(" Where 'action' is the command to complete,\n");
431 print(" and 'map' is what it acts upon (if required).\n");
432 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
438 void GenericCommand_nextframe(float request, float arguments, string command)
442 case CMD_REQUEST_COMMAND:
444 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
449 case CMD_REQUEST_USAGE:
451 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
452 print(" Where command will be executed next frame of this VM\n");
458 void GenericCommand_removefromlist(float request, float argc)
462 case CMD_REQUEST_COMMAND:
467 string original_cvar = argv(1);
468 string removal = argv(2);
471 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
474 for(i = 0; i < argc; ++i)
475 if(argv(i) != removal)
476 tmp_string = strcat(tmp_string, " ", argv(i));
478 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
479 cvar_set(original_cvar, tmp_string);
486 print("Incorrect parameters for ^2removefromlist^7\n");
487 case CMD_REQUEST_USAGE:
489 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
490 print(" Where 'variable' is what cvar to remove 'value' from.\n");
491 print("See also: ^2addtolist^7\n");
497 void GenericCommand_restartnotifs(float request)
501 case CMD_REQUEST_COMMAND:
506 "Restart_Notifications(): Restarting %d notifications... ",
507 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
522 Destroy_All_Notifications();
523 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
525 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
531 case CMD_REQUEST_USAGE:
533 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
534 print(" No arguments required.\n");
540 void GenericCommand_settemp(float request, float argc)
544 case CMD_REQUEST_COMMAND:
548 float f = cvar_settemp(argv(1), argv(2));
550 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
552 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
553 // else cvar_settemp itself errors out
560 print("Incorrect parameters for ^2settemp^7\n");
561 case CMD_REQUEST_USAGE:
563 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
564 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
565 print("See also: ^2settemp_restore^7\n");
571 void GenericCommand_settemp_restore(float request, float argc)
575 case CMD_REQUEST_COMMAND:
577 float i = cvar_settemp_restore();
580 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
582 dprint("Nothing to restore.\n");
588 case CMD_REQUEST_USAGE:
590 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
591 print(" No arguments required.\n");
592 print("See also: ^2settemp^7\n");
598 void GenericCommand_runtest(float request, float argc)
602 case CMD_REQUEST_COMMAND:
607 for(i = 1; i < argc; ++i)
616 case CMD_REQUEST_USAGE:
618 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
624 /* use this when creating a new command, making sure to place it in alphabetical order... also,
625 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
626 void GenericCommand_(float request)
630 case CMD_REQUEST_COMMAND:
637 case CMD_REQUEST_USAGE:
639 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
640 print(" No arguments required.\n");
647 // ==================================
648 // Macro system for server commands
649 // ==================================
651 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
652 #define GENERIC_COMMANDS(request,arguments,command) \
653 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
654 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
655 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
656 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
657 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
658 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
659 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
660 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
661 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
662 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
663 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
664 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
665 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
668 void GenericCommand_macro_help()
670 #define GENERIC_COMMAND(name,function,description) \
671 { print(" ^2", name, "^7: ", description, "\n"); }
673 GENERIC_COMMANDS(0, 0, "");
674 #undef GENERIC_COMMAND
679 float GenericCommand_macro_command(float argc, string command)
681 #define GENERIC_COMMAND(name,function,description) \
682 { if(name == strtolower(argv(0))) { function; return true; } }
684 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
685 #undef GENERIC_COMMAND
690 float GenericCommand_macro_usage(float argc)
692 #define GENERIC_COMMAND(name,function,description) \
693 { if(name == strtolower(argv(1))) { function; return true; } }
695 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
696 #undef GENERIC_COMMAND
701 void GenericCommand_macro_write_aliases(float fh)
703 #define GENERIC_COMMAND(name,function,description) \
704 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
706 GENERIC_COMMANDS(0, 0, "");
707 #undef GENERIC_COMMAND
713 // ===========================================
714 // Main Common Function For Generic Commands
715 // ===========================================
716 // Commands spread out among all programs (menu, client, and server)
718 float GenericCommand(string command)
720 float argc = tokenize_console(command);
725 // Guide for working with argc arguments by example:
726 // argc: 1 - 2 - 3 - 4
727 // argv: 0 - 1 - 2 - 3
728 // cmd vote - master - login - password
730 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
732 return true; // handled by one of the above GenericCommand_* functions
734 else if(argc >= 3 && argv(0) == "red")
736 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
737 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
740 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
743 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
745 n = floor(random() * 6 + 2);
748 for(i = 0; i < n; ++i)
750 s2 = strcat(s2, "AH");
754 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
760 s = strcat(s, " ", s2);
762 s = strcat(s2, " ", s);
764 s2 = substring(s, strlen(s) - 2, 2);
765 if(s2 == "AH" || s2 == "AY")
768 s = strcat(s, " ))");
771 s = substring(s, 0, strlen(s) - 1);
774 s = strconv(1, 0, 0, s);
776 localcmd(strcat(argv(1), " ", s));
780 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
782 // test case for terencehill's color codes
783 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
787 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
790 for(i = 0; i < n; ++i)
792 c = substring(s, i, 1);
799 if(substring(s, i+1, 1) == "^")
805 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
806 c = strcat(rgb_to_hexcolor(rgb), c);
811 localcmd(strcat(argv(1), " ", s2));