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