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