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