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 "../../server/sys-post.qh"
15 #include "../constants.qh"
19 #include "shared_defs.qh"
20 #include "../weapons/config.qh"
21 #include "../notifications.qh"
22 #include "../mapinfo.qh"
23 #include "../../server/command/common.qh"
24 #include "../../server/command/banning.qh"
25 #include "../../server/command/cmd.qh"
26 #include "../../server/command/sv_cmd.qh"
29 // =========================================================
30 // Generic program common command code, written by Samual
31 // Last updated: February 19th, 2012
32 // =========================================================
34 // used by generic commands for better help/usage information
35 string GetProgramCommandPrefix(void)
48 // used by curl command
49 void Curl_URI_Get_Callback(int id, float status, string data)
51 int i = id - URI_GET_CURL;
52 float do_exec = curl_uri_get_exec[i];
53 string do_cvar = curl_uri_get_cvar[i];
56 dprintf("error: status is %d\n", status);
65 cvar_set(do_cvar, data);
74 // =======================
75 // Command Sub-Functions
76 // =======================
78 void GenericCommand_addtolist(float request, float argc)
82 case CMD_REQUEST_COMMAND:
86 string original_cvar = argv(1);
87 string tmp_string = argv(2);
89 if(cvar_string(original_cvar) == "") // cvar was empty
91 cvar_set(original_cvar, tmp_string);
93 else // add it to the end of the list if the list doesn't already have it
95 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
97 for(i = 0; i < argc; ++i)
98 if(argv(i) == tmp_string)
99 return; // already in list
101 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
108 print("Incorrect parameters for ^2addtolist^7\n");
109 case CMD_REQUEST_USAGE:
111 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
112 print(" Where 'variable' is what to add 'value' to.\n");
113 print("See also: ^2removefromlist^7\n");
119 void GenericCommand_qc_curl(float request, float argc)
123 case CMD_REQUEST_COMMAND:
125 bool do_exec = false;
126 string do_cvar = string_null;
129 for(i = 1; i+1 < argc; ++i)
131 if(argv(i) == "--cvar" && i+2 < argc)
134 do_cvar = strzone(argv(i));
137 if(argv(i) == "--exec")
142 if(argv(i) == "--key" && i+2 < argc)
151 // now, argv(i) is the URL
152 // following args may be POST parameters
153 string url = argv(i);
155 float buf = buf_create();
157 for(j = 0; i+1 < argc; i += 2)
158 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
160 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
163 if(j == 0) // no args: GET
164 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
165 else // with args: POST
166 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
170 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
171 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
172 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
175 print(_("error creating curl handle\n"));
183 case CMD_REQUEST_USAGE:
185 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
191 void GenericCommand_dumpcommands(float request)
195 case CMD_REQUEST_COMMAND:
198 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
199 fh = fopen(filename, FILE_WRITE);
204 CMD_Write("dump of server console commands:\n");
205 GameCommand_macro_write_aliases(fh);
207 CMD_Write("\ndump of networked client only commands:\n");
208 ClientCommand_macro_write_aliases(fh);
210 CMD_Write("\ndump of common commands:\n");
211 CommonCommand_macro_write_aliases(fh);
213 CMD_Write("\ndump of ban commands:\n");
214 BanCommand_macro_write_aliases(fh);
218 CMD_Write("dump of client commands:\n");
219 LocalCommand_macro_write_aliases(fh);
222 CMD_Write("\ndump of generic commands:\n");
223 GenericCommand_macro_write_aliases(fh);
225 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
231 print("^1Error: ^7Could not dump to file!\n");
237 case CMD_REQUEST_USAGE:
239 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
240 print(" No arguments required.\n");
246 void GenericCommand_dumpnotifs(float request)
250 case CMD_REQUEST_COMMAND:
253 float fh, alsoprint = false;
254 string filename = argv(1);
258 filename = "notifications_dump.cfg";
261 else if(filename == "-")
263 filename = "notifications_dump.cfg";
266 fh = fopen(filename, FILE_WRITE);
270 Dump_Notifications(fh, alsoprint);
271 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
276 printf("^1Error: ^7Could not open file '%s'!\n", filename);
279 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
285 case CMD_REQUEST_USAGE:
287 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
288 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
289 print(" if supplied with '-' output to console as well as default,\n");
290 print(" if left blank, it will only write to default.\n");
296 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
300 case CMD_REQUEST_COMMAND:
303 wep_config_file = -1;
304 wep_config_alsoprint = -1;
305 string filename = argv(1);
309 filename = "weapons_dump.cfg";
310 wep_config_alsoprint = false;
312 else if(filename == "-")
314 filename = "weapons_dump.cfg";
315 wep_config_alsoprint = true;
317 wep_config_file = fopen(filename, FILE_WRITE);
319 if(wep_config_file >= 0)
321 Dump_Weapon_Settings();
322 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
323 fclose(wep_config_file);
324 wep_config_file = -1;
325 wep_config_alsoprint = -1;
329 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
332 print(_("Weapons dump command only works with sv_cmd.\n"));
338 case CMD_REQUEST_USAGE:
340 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
341 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
342 print(" if supplied with '-' output to console as well as default,\n");
343 print(" if left blank, it will only write to default.\n");
349 void GenericCommand_maplist(float request, float argc)
353 case CMD_REQUEST_COMMAND:
360 case "add": // appends new maps to the maplist
364 if (!fexists(strcat("maps/", argv(2), ".bsp")))
366 print("maplist: ERROR: ", argv(2), " does not exist!\n");
370 if(cvar_string("g_maplist") == "")
371 cvar_set("g_maplist", argv(2));
373 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
377 break; // go to usage
380 case "cleanup": // scans maplist and only adds back the ones which are really usable
383 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
384 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
387 for(i = 0; i < argc; ++i)
388 if(MapInfo_CheckMap(argv(i)))
389 tmp_string = strcat(tmp_string, " ", argv(i));
391 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
392 cvar_set("g_maplist", tmp_string);
397 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
401 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
404 for(i = 0; i < argc; ++i)
405 if(argv(i) != argv(2))
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);
413 break; // go to usage
416 case "shuffle": // randomly shuffle the maplist
418 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
427 print("Incorrect parameters for ^2maplist^7\n");
428 case CMD_REQUEST_USAGE:
430 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
431 print(" Where 'action' is the command to complete,\n");
432 print(" and 'map' is what it acts upon (if required).\n");
433 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
439 void GenericCommand_nextframe(float request, float arguments, string command)
443 case CMD_REQUEST_COMMAND:
445 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
450 case CMD_REQUEST_USAGE:
452 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
453 print(" Where command will be executed next frame of this VM\n");
459 void GenericCommand_removefromlist(float request, float argc)
463 case CMD_REQUEST_COMMAND:
468 string original_cvar = argv(1);
469 string removal = argv(2);
472 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
475 for(i = 0; i < argc; ++i)
476 if(argv(i) != removal)
477 tmp_string = strcat(tmp_string, " ", argv(i));
479 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
480 cvar_set(original_cvar, tmp_string);
487 print("Incorrect parameters for ^2removefromlist^7\n");
488 case CMD_REQUEST_USAGE:
490 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
491 print(" Where 'variable' is what cvar to remove 'value' from.\n");
492 print("See also: ^2addtolist^7\n");
498 void GenericCommand_restartnotifs(float request)
502 case CMD_REQUEST_COMMAND:
507 "Restart_Notifications(): Restarting %d notifications... ",
508 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
523 Destroy_All_Notifications();
524 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
526 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
532 case CMD_REQUEST_USAGE:
534 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
535 print(" No arguments required.\n");
541 void GenericCommand_settemp(float request, float argc)
545 case CMD_REQUEST_COMMAND:
549 float f = cvar_settemp(argv(1), argv(2));
551 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
553 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
554 // else cvar_settemp itself errors out
561 print("Incorrect parameters for ^2settemp^7\n");
562 case CMD_REQUEST_USAGE:
564 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
565 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
566 print("See also: ^2settemp_restore^7\n");
572 void GenericCommand_settemp_restore(float request, float argc)
576 case CMD_REQUEST_COMMAND:
578 float i = cvar_settemp_restore();
581 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
583 dprint("Nothing to restore.\n");
589 case CMD_REQUEST_USAGE:
591 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
592 print(" No arguments required.\n");
593 print("See also: ^2settemp^7\n");
599 void GenericCommand_runtest(float request, float argc)
603 case CMD_REQUEST_COMMAND:
608 for(i = 1; i < argc; ++i)
617 case CMD_REQUEST_USAGE:
619 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
625 /* use this when creating a new command, making sure to place it in alphabetical order... also,
626 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
627 void GenericCommand_(float request)
631 case CMD_REQUEST_COMMAND:
638 case CMD_REQUEST_USAGE:
640 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
641 print(" No arguments required.\n");
648 // ==================================
649 // Macro system for server commands
650 // ==================================
652 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
653 #define GENERIC_COMMANDS(request,arguments,command) \
654 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
655 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
656 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
657 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
658 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
659 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
660 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
661 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
662 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
663 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
664 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
665 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
666 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
669 void GenericCommand_macro_help()
671 #define GENERIC_COMMAND(name,function,description) \
672 { print(" ^2", name, "^7: ", description, "\n"); }
674 GENERIC_COMMANDS(0, 0, "");
675 #undef GENERIC_COMMAND
680 float GenericCommand_macro_command(float argc, string command)
682 #define GENERIC_COMMAND(name,function,description) \
683 { if(name == strtolower(argv(0))) { function; return true; } }
685 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
686 #undef GENERIC_COMMAND
691 float GenericCommand_macro_usage(float argc)
693 #define GENERIC_COMMAND(name,function,description) \
694 { if(name == strtolower(argv(1))) { function; return true; } }
696 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
697 #undef GENERIC_COMMAND
702 void GenericCommand_macro_write_aliases(float fh)
704 #define GENERIC_COMMAND(name,function,description) \
705 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
707 GENERIC_COMMANDS(0, 0, "");
708 #undef GENERIC_COMMAND
714 // ===========================================
715 // Main Common Function For Generic Commands
716 // ===========================================
717 // Commands spread out among all programs (menu, client, and server)
719 float GenericCommand(string command)
721 float argc = tokenize_console(command);
726 // Guide for working with argc arguments by example:
727 // argc: 1 - 2 - 3 - 4
728 // argv: 0 - 1 - 2 - 3
729 // cmd vote - master - login - password
731 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
733 return true; // handled by one of the above GenericCommand_* functions
735 else if(argc >= 3 && argv(0) == "red")
737 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
738 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
741 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
744 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
746 n = floor(random() * 6 + 2);
749 for(i = 0; i < n; ++i)
751 s2 = strcat(s2, "AH");
755 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
761 s = strcat(s, " ", s2);
763 s = strcat(s2, " ", s);
765 s2 = substring(s, strlen(s) - 2, 2);
766 if(s2 == "AH" || s2 == "AY")
769 s = strcat(s, " ))");
772 s = substring(s, 0, strlen(s) - 1);
775 s = strconv(1, 0, 0, s);
777 localcmd(strcat(argv(1), " ", s));
781 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
783 // test case for terencehill's color codes
784 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
788 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
791 for(i = 0; i < n; ++i)
793 c = substring(s, i, 1);
800 if(substring(s, i+1, 1) == "^")
806 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
807 c = strcat(rgb_to_hexcolor(rgb), c);
812 localcmd(strcat(argv(1), " ", s2));