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)
26 i = id - URI_GET_CURL;
27 do_exec = curl_uri_get_exec[i];
28 do_cvar = curl_uri_get_cvar[i];
31 print(sprintf(_("error: status is %d\n"), status));
40 cvar_set(do_cvar, data);
43 if(!do_exec && !do_cvar)
48 // =======================
49 // Command Sub-Functions
50 // =======================
52 void GenericCommand_addtolist(float request, float argc)
56 case CMD_REQUEST_COMMAND:
62 string original_cvar = argv(1);
63 string tmp_string = argv(2);
65 if(cvar_string(original_cvar) == "") // cvar was empty
67 cvar_set(original_cvar, tmp_string);
69 else // add it to the end of the list if the list doesn't already have it
71 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
73 for(i = 0; i < argc; ++i)
74 if(argv(i) == tmp_string)
75 return; // already in list
77 cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
84 print("Incorrect parameters for ^2addtolist^7\n");
85 case CMD_REQUEST_USAGE:
87 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value\n"));
88 print(" Where 'variable' is what to add 'value' to.\n");
89 print("See also: ^2removefromlist^7\n");
95 void GenericCommand_qc_curl(float request, float argc)
99 case CMD_REQUEST_COMMAND:
110 do_cvar = string_null;
113 for(i = 1; i+1 < argc; ++i)
115 if(argv(i) == "--cvar" && i+2 < argc)
118 do_cvar = strzone(argv(i));
121 if(argv(i) == "--exec")
126 if(argv(i) == "--key" && i+2 < argc)
135 // now, argv(i) is the URL
136 // following args may be POST parameters
141 for(; i+1 < argc; i += 2)
142 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
144 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
146 if(j == 0) // no args: GET
147 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
148 else // with args: POST
149 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
153 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
154 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
155 curl_uri_get_pos = mod(curl_uri_get_pos + 1, URI_GET_CURL_END - URI_GET_CURL + 1);
158 print(_("error creating curl handle\n"));
166 case CMD_REQUEST_USAGE:
168 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
174 void GenericCommand_dumpcommands(float request)
178 case CMD_REQUEST_COMMAND:
181 string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
182 fh = fopen(filename, FILE_WRITE);
187 CMD_Write("dump of server console commands:\n");
188 GameCommand_macro_write_aliases(fh);
190 CMD_Write("\ndump of networked client only commands:\n");
191 ClientCommand_macro_write_aliases(fh);
193 CMD_Write("\ndump of common commands:\n");
194 CommonCommand_macro_write_aliases(fh);
196 CMD_Write("\ndump of ban commands:\n");
197 BanCommand_macro_write_aliases(fh);
201 CMD_Write("dump of client commands:\n");
202 LocalCommand_macro_write_aliases(fh);
205 CMD_Write("\ndump of generic commands:\n");
206 GenericCommand_macro_write_aliases(fh);
208 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
214 print("^1Error: ^7Could not dump to file!\n");
220 case CMD_REQUEST_USAGE:
222 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
223 print(" No arguments required.\n");
229 void GenericCommand_dumpnotifs(float request)
233 case CMD_REQUEST_COMMAND:
237 string filename = argv(1);
241 filename = "notifications_dump.txt";
244 else if(filename == "-")
246 filename = "notifications_dump.txt";
249 fh = fopen(filename, FILE_WRITE);
253 fputs(fh, "dump of notifications list:\n");
254 if(alsoprint) { print("dump of notifications list:\n"); }
255 Dump_Notifications(fh, alsoprint);
256 print(sprintf("File located in ^2data/data/%s^7.\n", filename));
261 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
267 case CMD_REQUEST_USAGE:
269 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
270 print(" Where 'filename' is the file to write (default is notifications_dump.txt),\n");
271 print(" if supplied with '-' output to console as well as default,\n");
272 print(" if left blank, it will only write to default.\n");
278 void GenericCommand_maplist(float request, float argc)
282 case CMD_REQUEST_COMMAND:
289 case "add": // appends new maps to the maplist
293 if (!fexists(strcat("maps/", argv(2), ".bsp")))
295 print("maplist: ERROR: ", argv(2), " does not exist!\n");
299 if(cvar_string("g_maplist") == "")
300 cvar_set("g_maplist", argv(2));
302 cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
306 break; // go to usage
309 case "cleanup": // scans maplist and only adds back the ones which are really usable
312 MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
313 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
316 for(i = 0; i < argc; ++i)
317 if(MapInfo_CheckMap(argv(i)))
318 tmp_string = strcat(tmp_string, " ", argv(i));
320 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
321 cvar_set("g_maplist", tmp_string);
326 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
330 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
333 for(i = 0; i < argc; ++i)
334 if(argv(i) != argv(2))
335 tmp_string = strcat(tmp_string, " ", argv(i));
337 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
338 cvar_set("g_maplist", tmp_string);
342 break; // go to usage
345 case "shuffle": // randomly shuffle the maplist
347 cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
356 print("Incorrect parameters for ^2maplist^7\n");
357 case CMD_REQUEST_USAGE:
359 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
360 print(" Where 'action' is the command to complete,\n");
361 print(" and 'map' is what it acts upon (if required).\n");
362 print(" Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
368 void GenericCommand_nextframe(float request, float arguments, string command)
372 case CMD_REQUEST_COMMAND:
374 queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
379 case CMD_REQUEST_USAGE:
381 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
382 print(" Where command will be executed next frame of this VM\n");
388 void GenericCommand_removefromlist(float request, float argc)
392 case CMD_REQUEST_COMMAND:
397 string original_cvar = argv(1);
398 string removal = argv(2);
401 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
404 for(i = 0; i < argc; ++i)
405 if(argv(i) != removal)
406 tmp_string = strcat(tmp_string, " ", argv(i));
408 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
409 cvar_set(original_cvar, tmp_string);
416 print("Incorrect parameters for ^2removefromlist^7\n");
417 case CMD_REQUEST_USAGE:
419 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
420 print(" Where 'variable' is what cvar to remove 'value' from.\n");
421 print("See also: ^2addtolist^7\n");
427 void GenericCommand_settemp(float request, float argc)
431 case CMD_REQUEST_COMMAND:
435 float f = cvar_settemp(argv(1), argv(2));
437 dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
439 dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
440 // else cvar_settemp itself errors out
447 print("Incorrect parameters for ^2settemp^7\n");
448 case CMD_REQUEST_USAGE:
450 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
451 print(" Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
452 print("See also: ^2settemp_restore^7\n");
458 void GenericCommand_settemp_restore(float request, float argc)
462 case CMD_REQUEST_COMMAND:
464 float i = cvar_settemp_restore();
467 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
469 dprint("Nothing to restore.\n");
475 case CMD_REQUEST_USAGE:
477 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
478 print(" No arguments required.\n");
479 print("See also: ^2settemp^7\n");
485 /* use this when creating a new command, making sure to place it in alphabetical order... also,
486 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
487 void GenericCommand_(float request)
491 case CMD_REQUEST_COMMAND:
498 case CMD_REQUEST_USAGE:
500 print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
501 print(" No arguments required.\n");
508 // ==================================
509 // Macro system for server commands
510 // ==================================
512 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
513 #define GENERIC_COMMANDS(request,arguments,command) \
514 GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
515 GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
516 GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
517 GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
518 GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
519 GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
520 GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
521 GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
522 GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
523 GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
526 void GenericCommand_macro_help()
528 #define GENERIC_COMMAND(name,function,description) \
529 { print(" ^2", name, "^7: ", description, "\n"); }
531 GENERIC_COMMANDS(0, 0, "")
532 #undef GENERIC_COMMAND
537 float GenericCommand_macro_command(float argc, string command)
539 #define GENERIC_COMMAND(name,function,description) \
540 { if(name == strtolower(argv(0))) { function; return TRUE; } }
542 GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
543 #undef GENERIC_COMMAND
548 float GenericCommand_macro_usage(float argc)
550 #define GENERIC_COMMAND(name,function,description) \
551 { if(name == strtolower(argv(1))) { function; return TRUE; } }
553 GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
554 #undef GENERIC_COMMAND
559 void GenericCommand_macro_write_aliases(float fh)
561 #define GENERIC_COMMAND(name,function,description) \
562 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
564 GENERIC_COMMANDS(0, 0, "")
565 #undef GENERIC_COMMAND
571 // ===========================================
572 // Main Common Function For Generic Commands
573 // ===========================================
574 // Commands spread out among all programs (menu, client, and server)
576 float GenericCommand(string command)
578 float argc = tokenize_console(command);
583 // Guide for working with argc arguments by example:
584 // argc: 1 - 2 - 3 - 4
585 // argv: 0 - 1 - 2 - 3
586 // cmd vote - master - login - password
588 if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
590 return TRUE; // handled by one of the above GenericCommand_* functions
592 else if(argc >= 3 && argv(0) == "red")
594 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
595 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
598 else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
601 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
603 n = floor(random() * 6 + 2);
606 for(i = 0; i < n; ++i)
608 s2 = strcat(s2, "AH");
612 s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
618 s = strcat(s, " ", s2);
620 s = strcat(s2, " ", s);
622 s2 = substring(s, strlen(s) - 2, 2);
623 if(s2 == "AH" || s2 == "AY")
626 s = strcat(s, " ))");
629 s = substring(s, 0, strlen(s) - 1);
632 s = strconv(1, 0, 0, s);
634 localcmd(strcat(argv(1), " ", s));
638 else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
640 // test case for terencehill's color codes
641 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
645 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
648 for(i = 0; i < n; ++i)
650 c = substring(s, i, 1);
657 if(substring(s, i+1, 1) == "^")
663 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
664 c = strcat(rgb_to_hexcolor(rgb), c);
669 localcmd(strcat(argv(1), " ", s2));