1 // =========================================================
2 // Generic program common command code, written by Samual
3 // Last updated: February 19th, 2012
4 // =========================================================
6 // used by generic commands for better help/usage information
7 string GetProgramCommandPrefix(void)
20 // used by curl command
21 void Curl_URI_Get_Callback(float id, float status, string data)
23 int i = id - URI_GET_CURL;
24 float do_exec = curl_uri_get_exec[i];
25 string do_cvar = curl_uri_get_cvar[i];
28 dprintf("error: status is %d\n", status);
37 cvar_set(do_cvar, data);
46 // =======================
47 // Command Sub-Functions
48 // =======================
50 void GenericCommand_addtolist(float request, float argc)
54 case CMD_REQUEST_COMMAND:
58 string original_cvar = argv(1);
59 string tmp_string = argv(2);
61 if(cvar_string(original_cvar) == "") // cvar was empty
63 cvar_set(original_cvar, tmp_string);
65 else // add it to the end of the list if the list doesn't already have it
67 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
69 for(i = 0; i < argc; ++i)
70 if(argv(i) == tmp_string)
71 return; // already in list
73 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
80 print("Incorrect parameters for ^2addtolist^7\n");
81 case CMD_REQUEST_USAGE:
83 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
84 print(" Where 'variable' is what to add 'value' to.\n");
85 print("See also: ^2removefromlist^7\n");
91 void GenericCommand_qc_curl(float request, float argc)
95 case CMD_REQUEST_COMMAND:
98 string do_cvar = string_null;
101 for(i = 1; i+1 < argc; ++i)
103 if(argv(i) == "--cvar" && i+2 < argc)
106 do_cvar = strzone(argv(i));
109 if(argv(i) == "--exec")
114 if(argv(i) == "--key" && i+2 < argc)
123 // now, argv(i) is the URL
124 // following args may be POST parameters
125 string url = argv(i);
127 float buf = buf_create();
129 for(j = 0; i+1 < argc; i += 2)
130 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
132 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
135 if(j == 0) // no args: GET
136 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
137 else // with args: POST
138 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
142 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
143 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
144 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
147 print(_("error creating curl handle\n"));
155 case CMD_REQUEST_USAGE:
157 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
163 void GenericCommand_dumpcommands(float request)
167 case CMD_REQUEST_COMMAND:
170 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
171 fh = fopen(filename, FILE_WRITE);
176 CMD_Write("dump of server console commands:\n");
177 GameCommand_macro_write_aliases(fh);
179 CMD_Write("\ndump of networked client only commands:\n");
180 ClientCommand_macro_write_aliases(fh);
182 CMD_Write("\ndump of common commands:\n");
183 CommonCommand_macro_write_aliases(fh);
185 CMD_Write("\ndump of ban commands:\n");
186 BanCommand_macro_write_aliases(fh);
190 CMD_Write("dump of client commands:\n");
191 LocalCommand_macro_write_aliases(fh);
194 CMD_Write("\ndump of generic commands:\n");
195 GenericCommand_macro_write_aliases(fh);
197 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
203 print("^1Error: ^7Could not dump to file!\n");
209 case CMD_REQUEST_USAGE:
211 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
212 print(" No arguments required.\n");
218 void GenericCommand_dumpnotifs(float request)
222 case CMD_REQUEST_COMMAND:
225 float fh, alsoprint = false;
226 string filename = argv(1);
230 filename = "notifications_dump.cfg";
233 else if(filename == "-")
235 filename = "notifications_dump.cfg";
238 fh = fopen(filename, FILE_WRITE);
242 Dump_Notifications(fh, alsoprint);
243 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
248 printf("^1Error: ^7Could not open file '%s'!\n", filename);
251 print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
257 case CMD_REQUEST_USAGE:
259 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
260 print(" Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
261 print(" if supplied with '-' output to console as well as default,\n");
262 print(" if left blank, it will only write to default.\n");
268 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
272 case CMD_REQUEST_COMMAND:
275 wep_config_file = -1;
276 wep_config_alsoprint = -1;
277 string filename = argv(1);
281 filename = "weapons_dump.cfg";
282 wep_config_alsoprint = false;
284 else if(filename == "-")
286 filename = "weapons_dump.cfg";
287 wep_config_alsoprint = true;
289 wep_config_file = fopen(filename, FILE_WRITE);
291 if(wep_config_file >= 0)
293 Dump_Weapon_Settings();
294 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
295 fclose(wep_config_file);
296 wep_config_file = -1;
297 wep_config_alsoprint = -1;
301 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
304 print(_("Weapons dump command only works with sv_cmd.\n"));
310 case CMD_REQUEST_USAGE:
312 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
313 print(" Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
314 print(" if supplied with '-' output to console as well as default,\n");
315 print(" if left blank, it will only write to default.\n");
321 void GenericCommand_maplist(float request, float argc)
325 case CMD_REQUEST_COMMAND:
332 case "add": // appends new maps to the maplist
336 if (!fexists(strcat("maps/", argv(2), ".bsp")))
338 print("maplist: ERROR: ", argv(2), " does not exist!\n");
342 if(cvar_string("g_maplist") == "")
343 cvar_set("g_maplist", argv(2));
345 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
349 break; // go to usage
352 case "cleanup": // scans maplist and only adds back the ones which are really usable
355 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
356 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
359 for(i = 0; i < argc; ++i)
360 if(MapInfo_CheckMap(argv(i)))
361 tmp_string = strcat(tmp_string, " ", argv(i));
363 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
364 cvar_set("g_maplist", tmp_string);
369 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
373 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
376 for(i = 0; i < argc; ++i)
377 if(argv(i) != argv(2))
378 tmp_string = strcat(tmp_string, " ", argv(i));
380 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
381 cvar_set("g_maplist", tmp_string);
385 break; // go to usage
388 case "shuffle": // randomly shuffle the maplist
390 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
399 print("Incorrect parameters for ^2maplist^7\n");
400 case CMD_REQUEST_USAGE:
402 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
403 print(" Where 'action' is the command to complete,\n");
404 print(" and 'map' is what it acts upon (if required).\n");
405 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
411 void GenericCommand_nextframe(float request, float arguments, string command)
415 case CMD_REQUEST_COMMAND:
417 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
422 case CMD_REQUEST_USAGE:
424 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
425 print(" Where command will be executed next frame of this VM\n");
431 void GenericCommand_removefromlist(float request, float argc)
435 case CMD_REQUEST_COMMAND:
440 string original_cvar = argv(1);
441 string removal = argv(2);
444 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
447 for(i = 0; i < argc; ++i)
448 if(argv(i) != removal)
449 tmp_string = strcat(tmp_string, " ", argv(i));
451 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
452 cvar_set(original_cvar, tmp_string);
459 print("Incorrect parameters for ^2removefromlist^7\n");
460 case CMD_REQUEST_USAGE:
462 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
463 print(" Where 'variable' is what cvar to remove 'value' from.\n");
464 print("See also: ^2addtolist^7\n");
470 void GenericCommand_restartnotifs(float request)
474 case CMD_REQUEST_COMMAND:
479 "Restart_Notifications(): Restarting %d notifications... ",
480 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
495 Destroy_All_Notifications();
496 CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
498 print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
504 case CMD_REQUEST_USAGE:
506 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
507 print(" No arguments required.\n");
513 void GenericCommand_settemp(float request, float argc)
517 case CMD_REQUEST_COMMAND:
521 float f = cvar_settemp(argv(1), argv(2));
523 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
525 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
526 // else cvar_settemp itself errors out
533 print("Incorrect parameters for ^2settemp^7\n");
534 case CMD_REQUEST_USAGE:
536 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
537 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
538 print("See also: ^2settemp_restore^7\n");
544 void GenericCommand_settemp_restore(float request, float argc)
548 case CMD_REQUEST_COMMAND:
550 float i = cvar_settemp_restore();
553 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
555 dprint("Nothing to restore.\n");
561 case CMD_REQUEST_USAGE:
563 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
564 print(" No arguments required.\n");
565 print("See also: ^2settemp^7\n");
571 void GenericCommand_runtest(float request, float argc)
575 case CMD_REQUEST_COMMAND:
580 for(i = 1; i < argc; ++i)
589 case CMD_REQUEST_USAGE:
591 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
597 /* use this when creating a new command, making sure to place it in alphabetical order... also,
598 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
599 void GenericCommand_(float request)
603 case CMD_REQUEST_COMMAND:
610 case CMD_REQUEST_USAGE:
612 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
613 print(" No arguments required.\n");
620 // ==================================
621 // Macro system for server commands
622 // ==================================
624 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
625 #define GENERIC_COMMANDS(request,arguments,command) \
626 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
627 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
628 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
629 GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
630 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
631 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
632 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
633 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
634 GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
635 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
636 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
637 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
638 GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
641 void GenericCommand_macro_help()
643 #define GENERIC_COMMAND(name,function,description) \
644 { print(" ^2", name, "^7: ", description, "\n"); }
646 GENERIC_COMMANDS(0, 0, "")
647 #undef GENERIC_COMMAND
652 float GenericCommand_macro_command(float argc, string command)
654 #define GENERIC_COMMAND(name,function,description) \
655 { if(name == strtolower(argv(0))) { function; return true; } }
657 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
658 #undef GENERIC_COMMAND
663 float GenericCommand_macro_usage(float argc)
665 #define GENERIC_COMMAND(name,function,description) \
666 { if(name == strtolower(argv(1))) { function; return true; } }
668 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
669 #undef GENERIC_COMMAND
674 void GenericCommand_macro_write_aliases(float fh)
676 #define GENERIC_COMMAND(name,function,description) \
677 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
679 GENERIC_COMMANDS(0, 0, "")
680 #undef GENERIC_COMMAND
686 // ===========================================
687 // Main Common Function For Generic Commands
688 // ===========================================
689 // Commands spread out among all programs (menu, client, and server)
691 float GenericCommand(string command)
693 float argc = tokenize_console(command);
698 // Guide for working with argc arguments by example:
699 // argc: 1 - 2 - 3 - 4
700 // argv: 0 - 1 - 2 - 3
701 // cmd vote - master - login - password
703 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
705 return true; // handled by one of the above GenericCommand_* functions
707 else if(argc >= 3 && argv(0) == "red")
709 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
710 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
713 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
716 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
718 n = floor(random() * 6 + 2);
721 for(i = 0; i < n; ++i)
723 s2 = strcat(s2, "AH");
727 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
733 s = strcat(s, " ", s2);
735 s = strcat(s2, " ", s);
737 s2 = substring(s, strlen(s) - 2, 2);
738 if(s2 == "AH" || s2 == "AY")
741 s = strcat(s, " ))");
744 s = substring(s, 0, strlen(s) - 1);
747 s = strconv(1, 0, 0, s);
749 localcmd(strcat(argv(1), " ", s));
753 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
755 // test case for terencehill's color codes
756 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
760 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
763 for(i = 0; i < n; ++i)
765 c = substring(s, i, 1);
772 if(substring(s, i+1, 1) == "^")
778 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
779 c = strcat(rgb_to_hexcolor(rgb), c);
784 localcmd(strcat(argv(1), " ", s2));