Merge branch 'master' into Mario/turrets
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / command / generic.qc
1 #include "generic.qh"
2 #include "shared_defs.qh"
3 #include "../turrets/config.qh"
4
5 // =========================================================
6 //  Generic program common command code, written by Samual
7 //  Last updated: February 19th, 2012
8 // =========================================================
9
10 // used by generic commands for better help/usage information
11 string GetProgramCommandPrefix(void)
12 {
13         #ifdef SVQC
14         return "sv_cmd";
15         #endif
16         #ifdef CSQC
17         return "cl_cmd";
18         #endif
19         #ifdef MENUQC
20         return "menu_cmd";
21         #endif
22 }
23
24 // used by curl command
25 void Curl_URI_Get_Callback(int id, float status, string data)
26 {
27         int i = id - URI_GET_CURL;
28         float do_exec = curl_uri_get_exec[i];
29         string do_cvar = curl_uri_get_cvar[i];
30         if(status != 0)
31         {
32                 dprintf("error: status is %d\n", status);
33                 if(do_cvar)
34                         strunzone(do_cvar);
35                 return;
36         }
37         if(do_exec)
38                 localcmd(data);
39         if(do_cvar)
40         {
41                 cvar_set(do_cvar, data);
42                 strunzone(do_cvar);
43         }
44         if(!do_exec)
45                 if (!do_cvar)
46                         print(data);
47 }
48
49
50 // =======================
51 //  Command Sub-Functions
52 // =======================
53
54 void GenericCommand_addtolist(float request, float argc)
55 {
56         switch(request)
57         {
58                 case CMD_REQUEST_COMMAND:
59                 {
60                         if(argc >= 2)
61                         {
62                                 string original_cvar = argv(1);
63                                 string tmp_string = argv(2);
64
65                                 if(cvar_string(original_cvar) == "") // cvar was empty
66                                 {
67                                         cvar_set(original_cvar, tmp_string);
68                                 }
69                                 else // add it to the end of the list if the list doesn't already have it
70                                 {
71                                         argc = tokenizebyseparator(cvar_string(original_cvar), " ");
72                                         int i;
73                                         for(i = 0; i < argc; ++i)
74                                                 if(argv(i) == tmp_string)
75                                                         return; // already in list
76
77                                         cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
78                                 }
79                                 return;
80                         }
81                 }
82
83                 default:
84                         print("Incorrect parameters for ^2addtolist^7\n");
85                 case CMD_REQUEST_USAGE:
86                 {
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");
90                         return;
91                 }
92         }
93 }
94
95 void GenericCommand_qc_curl(float request, float argc)
96 {
97         switch(request)
98         {
99                 case CMD_REQUEST_COMMAND:
100                 {
101                         bool do_exec = false;
102                         string do_cvar = string_null;
103                         float key = -1;
104                         int i;
105                         for(i = 1; i+1 < argc; ++i)
106                         {
107                                 if(argv(i) == "--cvar" && i+2 < argc)
108                                 {
109                                         ++i;
110                                         do_cvar = strzone(argv(i));
111                                         continue;
112                                 }
113                                 if(argv(i) == "--exec")
114                                 {
115                                         do_exec = true;
116                                         continue;
117                                 }
118                                 if(argv(i) == "--key" && i+2 < argc)
119                                 {
120                                         ++i;
121                                         key = stof(argv(i));
122                                         continue;
123                                 }
124                                 break;
125                         }
126
127                         // now, argv(i) is the URL
128                         // following args may be POST parameters
129                         string url = argv(i);
130                         ++i;
131                         float buf = buf_create();
132                         int j;
133                         for(j = 0; i+1 < argc; i += 2)
134                                 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
135                         if(i < argc)
136                                 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
137
138                         float r;
139                         if(j == 0) // no args: GET
140                                 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
141                         else // with args: POST
142                                 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
143
144                         if(r)
145                         {
146                                 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
147                                 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
148                                 curl_uri_get_pos = (curl_uri_get_pos + 1) % (URI_GET_CURL_END - URI_GET_CURL + 1);
149                         }
150                         else
151                                 print(_("error creating curl handle\n"));
152
153                         buf_del(buf);
154
155                         return;
156                 }
157
158                 default:
159                 case CMD_REQUEST_USAGE:
160                 {
161                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " qc_curl [--key N] [--cvar] [--exec] URL [postargs...]"));
162                         return;
163                 }
164         }
165 }
166
167 void GenericCommand_dumpcommands(float request)
168 {
169         switch(request)
170         {
171                 case CMD_REQUEST_COMMAND:
172                 {
173                         float fh;
174                         string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
175                         fh = fopen(filename, FILE_WRITE);
176
177                         if(fh >= 0)
178                         {
179                                 #ifdef SVQC
180                                         CMD_Write("dump of server console commands:\n");
181                                         GameCommand_macro_write_aliases(fh);
182
183                                         CMD_Write("\ndump of networked client only commands:\n");
184                                         ClientCommand_macro_write_aliases(fh);
185
186                                         CMD_Write("\ndump of common commands:\n");
187                                         CommonCommand_macro_write_aliases(fh);
188
189                                         CMD_Write("\ndump of ban commands:\n");
190                                         BanCommand_macro_write_aliases(fh);
191                                 #endif
192
193                                 #ifdef CSQC
194                                         CMD_Write("dump of client commands:\n");
195                                         LocalCommand_macro_write_aliases(fh);
196                                 #endif
197
198                                 CMD_Write("\ndump of generic commands:\n");
199                                 GenericCommand_macro_write_aliases(fh);
200
201                                 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
202
203                                 fclose(fh);
204                         }
205                         else
206                         {
207                                 print("^1Error: ^7Could not dump to file!\n");
208                         }
209                         return;
210                 }
211
212                 default:
213                 case CMD_REQUEST_USAGE:
214                 {
215                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
216                         print("  No arguments required.\n");
217                         return;
218                 }
219         }
220 }
221
222 void GenericCommand_dumpnotifs(float request)
223 {
224         switch(request)
225         {
226                 case CMD_REQUEST_COMMAND:
227                 {
228                         #ifndef MENUQC
229                         float fh, alsoprint = false;
230                         string filename = argv(1);
231
232                         if(filename == "")
233                         {
234                                 filename = "notifications_dump.cfg";
235                                 alsoprint = false;
236                         }
237                         else if(filename == "-")
238                         {
239                                 filename = "notifications_dump.cfg";
240                                 alsoprint = true;
241                         }
242                         fh = fopen(filename, FILE_WRITE);
243
244                         if(fh >= 0)
245                         {
246                                 Dump_Notifications(fh, alsoprint);
247                                 printf("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
248                                 fclose(fh);
249                         }
250                         else
251                         {
252                                 printf("^1Error: ^7Could not open file '%s'!\n", filename);
253                         }
254                         #else
255                         print(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
256                         #endif
257                         return;
258                 }
259
260                 default:
261                 case CMD_REQUEST_USAGE:
262                 {
263                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
264                         print("  Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
265                         print("  if supplied with '-' output to console as well as default,\n");
266                         print("  if left blank, it will only write to default.\n");
267                         return;
268                 }
269         }
270 }
271
272 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
273 {
274         switch(request)
275         {
276                 case CMD_REQUEST_COMMAND:
277                 {
278                         #ifdef SVQC
279                         wep_config_file = -1;
280                         wep_config_alsoprint = -1;
281                         string filename = argv(1);
282
283                         if(filename == "")
284                         {
285                                 filename = "weapons_dump.cfg";
286                                 wep_config_alsoprint = false;
287                         }
288                         else if(filename == "-")
289                         {
290                                 filename = "weapons_dump.cfg";
291                                 wep_config_alsoprint = true;
292                         }
293                         wep_config_file = fopen(filename, FILE_WRITE);
294
295                         if(wep_config_file >= 0)
296                         {
297                                 Dump_Weapon_Settings();
298                                 print(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
299                                 fclose(wep_config_file);
300                                 wep_config_file = -1;
301                                 wep_config_alsoprint = -1;
302                         }
303                         else
304                         {
305                                 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
306                         }
307                         #else
308                         print(_("Weapons dump command only works with sv_cmd.\n"));
309                         #endif
310                         return;
311                 }
312
313                 default:
314                 case CMD_REQUEST_USAGE:
315                 {
316                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
317                         print("  Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
318                         print("  if supplied with '-' output to console as well as default,\n");
319                         print("  if left blank, it will only write to default.\n");
320                         return;
321                 }
322         }
323 }
324
325 void GenericCommand_dumpturrets(float request)
326 {
327         switch(request)
328         {
329                 case CMD_REQUEST_COMMAND:
330                 {
331                         #ifdef SVQC
332                         tur_config_file = -1;
333                         tur_config_alsoprint = -1;
334                         string filename = argv(1);
335                         
336                         if(filename == "")
337                         {
338                                 filename = "turrets_dump.cfg";
339                                 tur_config_alsoprint = FALSE;
340                         }
341                         else if(filename == "-")
342                         {
343                                 filename = "turrets_dump.cfg";
344                                 tur_config_alsoprint = TRUE;
345                         }
346                         tur_config_file = fopen(filename, FILE_WRITE);
347                         
348                         if(tur_config_file >= 0)
349                         {
350                                 Dump_Turret_Settings();
351                                 print(sprintf("Dumping turrets... File located in ^2data/data/%s^7.\n", filename));
352                                 fclose(tur_config_file);
353                                 tur_config_file = -1;
354                                 tur_config_alsoprint = -1;
355                         }
356                         else
357                         {
358                                 print(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
359                         }
360                         #else
361                         print(_("Turrets dump command only works with sv_cmd.\n"));
362                         #endif
363                         return;
364                 }
365                         
366                 default:
367                 case CMD_REQUEST_USAGE:
368                 {
369                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpturrets [filename]"));
370                         print("  Where 'filename' is the file to write (default is turrets_dump.cfg),\n");
371                         print("  if supplied with '-' output to console as well as default,\n");
372                         print("  if left blank, it will only write to default.\n");
373                         return;
374                 }
375         }
376 }
377
378 void GenericCommand_maplist(float request, float argc)
379 {
380         switch(request)
381         {
382                 case CMD_REQUEST_COMMAND:
383                 {
384                         string tmp_string;
385                         float i;
386
387                         switch(argv(1))
388                         {
389                                 case "add": // appends new maps to the maplist
390                                 {
391                                         if(argc == 3)
392                                         {
393                                                 if (!fexists(strcat("maps/", argv(2), ".bsp")))
394                                                 {
395                                                         print("maplist: ERROR: ", argv(2), " does not exist!\n");
396                                                         break;
397                                                 }
398
399                                                 if(cvar_string("g_maplist") == "")
400                                                         cvar_set("g_maplist", argv(2));
401                                                 else
402                                                         cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
403
404                                                 return;
405                                         }
406                                         break; // go to usage
407                                 }
408
409                                 case "cleanup": // scans maplist and only adds back the ones which are really usable
410                                 {
411                                         MapInfo_Enumerate();
412                                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
413                                         argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
414
415                                         tmp_string = "";
416                                         for(i = 0; i < argc; ++i)
417                                                 if(MapInfo_CheckMap(argv(i)))
418                                                         tmp_string = strcat(tmp_string, " ", argv(i));
419
420                                         tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
421                                         cvar_set("g_maplist", tmp_string);
422
423                                         return;
424                                 }
425
426                                 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
427                                 {
428                                         if(argc == 3)
429                                         {
430                                                 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
431
432                                                 tmp_string = "";
433                                                 for(i = 0; i < argc; ++i)
434                                                         if(argv(i) != argv(2))
435                                                                 tmp_string = strcat(tmp_string, " ", argv(i));
436
437                                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
438                                                 cvar_set("g_maplist", tmp_string);
439
440                                                 return;
441                                         }
442                                         break; // go to usage
443                                 }
444
445                                 case "shuffle": // randomly shuffle the maplist
446                                 {
447                                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
448                                         return;
449                                 }
450
451                                 default: break;
452                         }
453                 }
454
455                 default:
456                         print("Incorrect parameters for ^2maplist^7\n");
457                 case CMD_REQUEST_USAGE:
458                 {
459                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
460                         print("  Where 'action' is the command to complete,\n");
461                         print("  and 'map' is what it acts upon (if required).\n");
462                         print("  Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
463                         return;
464                 }
465         }
466 }
467
468 void GenericCommand_nextframe(float request, float arguments, string command)
469 {
470         switch(request)
471         {
472                 case CMD_REQUEST_COMMAND:
473                 {
474                         queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
475                         return;
476                 }
477
478                 default:
479                 case CMD_REQUEST_USAGE:
480                 {
481                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
482                         print("  Where command will be executed next frame of this VM\n");
483                         return;
484                 }
485         }
486 }
487
488 void GenericCommand_removefromlist(float request, float argc)
489 {
490         switch(request)
491         {
492                 case CMD_REQUEST_COMMAND:
493                 {
494                         if(argc == 3)
495                         {
496                                 float i;
497                                 string original_cvar = argv(1);
498                                 string removal = argv(2);
499                                 string tmp_string;
500
501                                 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
502
503                                 tmp_string = "";
504                                 for(i = 0; i < argc; ++i)
505                                         if(argv(i) != removal)
506                                                 tmp_string = strcat(tmp_string, " ", argv(i));
507
508                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
509                                 cvar_set(original_cvar, tmp_string);
510
511                                 return;
512                         }
513                 }
514
515                 default:
516                         print("Incorrect parameters for ^2removefromlist^7\n");
517                 case CMD_REQUEST_USAGE:
518                 {
519                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
520                         print("  Where 'variable' is what cvar to remove 'value' from.\n");
521                         print("See also: ^2addtolist^7\n");
522                         return;
523                 }
524         }
525 }
526
527 void GenericCommand_restartnotifs(float request)
528 {
529         switch(request)
530         {
531                 case CMD_REQUEST_COMMAND:
532                 {
533                         #ifndef MENUQC
534                         printf(
535                                 strcat(
536                                         "Restart_Notifications(): Restarting %d notifications... ",
537                                         "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
538                                 ),
539                                 (
540                                         NOTIF_ANNCE_COUNT +
541                                         NOTIF_INFO_COUNT +
542                                         NOTIF_CENTER_COUNT +
543                                         NOTIF_MULTI_COUNT +
544                                         NOTIF_CHOICE_COUNT
545                                 ),
546                                 NOTIF_ANNCE_COUNT,
547                                 NOTIF_INFO_COUNT,
548                                 NOTIF_CENTER_COUNT,
549                                 NOTIF_MULTI_COUNT,
550                                 NOTIF_CHOICE_COUNT
551                         );
552                         Destroy_All_Notifications();
553                         CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
554                         #else
555                         print(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
556                         #endif
557                         return;
558                 }
559
560                 default:
561                 case CMD_REQUEST_USAGE:
562                 {
563                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
564                         print("  No arguments required.\n");
565                         return;
566                 }
567         }
568 }
569
570 void GenericCommand_settemp(float request, float argc)
571 {
572         switch(request)
573         {
574                 case CMD_REQUEST_COMMAND:
575                 {
576                         if(argc >= 3)
577                         {
578                                 float f = cvar_settemp(argv(1), argv(2));
579                                 if(f == 1)
580                                         dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
581                                 else if(f == -1)
582                                         dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
583                                 // else cvar_settemp itself errors out
584
585                                 return;
586                         }
587                 }
588
589                 default:
590                         print("Incorrect parameters for ^2settemp^7\n");
591                 case CMD_REQUEST_USAGE:
592                 {
593                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
594                         print("  Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
595                         print("See also: ^2settemp_restore^7\n");
596                         return;
597                 }
598         }
599 }
600
601 void GenericCommand_settemp_restore(float request, float argc)
602 {
603         switch(request)
604         {
605                 case CMD_REQUEST_COMMAND:
606                 {
607                         float i = cvar_settemp_restore();
608
609                         if(i)
610                                 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
611                         else
612                                 dprint("Nothing to restore.\n");
613
614                         return;
615                 }
616
617                 default:
618                 case CMD_REQUEST_USAGE:
619                 {
620                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
621                         print("  No arguments required.\n");
622                         print("See also: ^2settemp^7\n");
623                         return;
624                 }
625         }
626 }
627
628 void GenericCommand_runtest(float request, float argc)
629 {
630         switch(request)
631         {
632                 case CMD_REQUEST_COMMAND:
633                 {
634                         if(argc > 1)
635                         {
636                                 float i;
637                                 for(i = 1; i < argc; ++i)
638                                         TEST_Run(argv(i));
639                         }
640                         else
641                                 TEST_RunAll();
642                         return;
643                 }
644
645                 default:
646                 case CMD_REQUEST_USAGE:
647                 {
648                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
649                         return;
650                 }
651         }
652 }
653
654 /* use this when creating a new command, making sure to place it in alphabetical order... also,
655 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
656 void GenericCommand_(float request)
657 {
658         switch(request)
659         {
660                 case CMD_REQUEST_COMMAND:
661                 {
662
663                         return;
664                 }
665
666                 default:
667                 case CMD_REQUEST_USAGE:
668                 {
669                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
670                         print("  No arguments required.\n");
671                         return;
672                 }
673         }
674 }
675 */
676
677 // ==================================
678 //  Macro system for server commands
679 // ==================================
680
681 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
682 #define GENERIC_COMMANDS(request,arguments,command) \
683         GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
684         GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
685         GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
686         GENERIC_COMMAND("dumpturrets", GenericCommand_dumpturrets(request), "Dump all turrets into turrets_dump.txt") \
687         GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
688         GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
689         GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
690         GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
691         GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
692         GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
693         GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
694         GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
695         GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
696         GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
697         /* nothing */
698
699 void GenericCommand_macro_help()
700 {
701         #define GENERIC_COMMAND(name,function,description) \
702                 { print("  ^2", name, "^7: ", description, "\n"); }
703
704         GENERIC_COMMANDS(0, 0, "");
705         #undef GENERIC_COMMAND
706
707         return;
708 }
709
710 float GenericCommand_macro_command(float argc, string command)
711 {
712         #define GENERIC_COMMAND(name,function,description) \
713                 { if(name == strtolower(argv(0))) { function; return true; } }
714
715         GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
716         #undef GENERIC_COMMAND
717
718         return false;
719 }
720
721 float GenericCommand_macro_usage(float argc)
722 {
723         #define GENERIC_COMMAND(name,function,description) \
724                 { if(name == strtolower(argv(1))) { function; return true; } }
725
726         GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
727         #undef GENERIC_COMMAND
728
729         return false;
730 }
731
732 void GenericCommand_macro_write_aliases(float fh)
733 {
734         #define GENERIC_COMMAND(name,function,description) \
735                 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
736
737         GENERIC_COMMANDS(0, 0, "");
738         #undef GENERIC_COMMAND
739
740         return;
741 }
742
743
744 // ===========================================
745 //  Main Common Function For Generic Commands
746 // ===========================================
747 // Commands spread out among all programs (menu, client, and server)
748
749 float GenericCommand(string command)
750 {
751         float argc = tokenize_console(command);
752         float n, j, f, i;
753         string s, s2, c;
754         vector rgb;
755
756         // Guide for working with argc arguments by example:
757         // argc:   1    - 2      - 3     - 4
758         // argv:   0    - 1      - 2     - 3
759         // cmd     vote - master - login - password
760
761         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
762         {
763                 return true; // handled by one of the above GenericCommand_* functions
764         }
765         else if(argc >= 3 && argv(0) == "red")
766         {
767                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
768                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
769                 return true;
770         }
771         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
772         {
773                 // other test case
774                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
775
776                 n = floor(random() * 6 + 2);
777
778                 s2 = "";
779                 for(i = 0; i < n; ++i)
780                 {
781                         s2 = strcat(s2, "AH");
782                 }
783
784                 if(random() < 0.1)
785                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
786
787                 if(s == "")
788                         s = s2;
789                 else
790                         if(random() < 0.8)
791                                 s = strcat(s, " ", s2);
792                         else
793                                 s = strcat(s2, " ", s);
794
795                 s2 = substring(s, strlen(s) - 2, 2);
796                 if(s2 == "AH" || s2 == "AY")
797                         s = strcat(s, "))");
798                 else
799                         s = strcat(s, " ))");
800
801                 if(random() < 0.1)
802                         s = substring(s, 0, strlen(s) - 1);
803
804                 if(random() < 0.1)
805                         s = strconv(1, 0, 0, s);
806
807                 localcmd(strcat(argv(1), " ", s));
808
809                 return true;
810         }
811         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
812         {
813                 // test case for terencehill's color codes
814                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
815                 s2 = "";
816
817                 n = strlen(s);
818                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
819                 f = random() * 6;
820
821                 for(i = 0; i < n; ++i)
822                 {
823                         c = substring(s, i, 1);
824
825                         if(c == ";")
826                                 c = ":";
827                         else if(c == "^")
828                         {
829                                 c = "^^";
830                                 if(substring(s, i+1, 1) == "^")
831                                         ++i;
832                         }
833
834                         if(c != " ")
835                         {
836                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
837                                 c = strcat(rgb_to_hexcolor(rgb), c);
838                         }
839                         s2 = strcat(s2, c);
840                 }
841
842                 localcmd(strcat(argv(1), " ", s2));
843
844                 return true;
845         }
846
847         return false;
848 }