]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
Merge branch 'master' into TimePath/effectinfo
[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 effectinfo_dump(int fh, bool alsoprint);
244 void GenericCommand_dumpeffectinfo(float request)
245 {
246     switch (request) {
247         case CMD_REQUEST_COMMAND: {
248             #ifndef MENUQC
249             string filename = argv(1);
250                         bool alsoprint = false;
251             if (filename == "") {
252                 filename = "effectinfo_dump.txt";
253                 alsoprint = false;
254             } else if (filename == "-") {
255                 filename = "effectinfo_dump.txt";
256                 alsoprint = true;
257             }
258             int fh = fopen(filename, FILE_WRITE);
259             if (fh >= 0) {
260                 effectinfo_dump(fh, alsoprint);
261                 LOG_INFOF("Dumping effectinfo... File located at ^2data/data/%s^7.\n", filename);
262                                 LOG_INFOF("Reload with ^2cl_particles_reloadeffects data/%s^7.\n", filename);
263                 fclose(fh);
264             } else {
265                 LOG_WARNINGF("Could not open file '%s'!\n", filename);
266             }
267             #else
268             LOG_INFO(_("Effectinfo dump command only works with cl_cmd and sv_cmd.\n"));
269             #endif
270             return;
271         }
272         default:
273         case CMD_REQUEST_USAGE: {
274             LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpeffectinfo [filename]"));
275             LOG_INFO("  Where 'filename' is the file to write (default is effectinfo_dump.txt),\n");
276             LOG_INFO("  if supplied with '-' output to console as well as default,\n");
277             LOG_INFO("  if left blank, it will only write to default.\n");
278             return;
279         }
280     }
281 }
282 STATIC_INIT(dumpeffectinfo) { localcmd("alias dumpeffectinfo \"qc_cmd_svcl dumpeffectinfo ${* ?}\"\n"); }
283
284 void GenericCommand_dumpitems(float request)
285 {
286         switch(request)
287         {
288                 case CMD_REQUEST_COMMAND:
289                 {
290                         Dump_Items();
291                         return;
292                 }
293
294                 default:
295                 case CMD_REQUEST_USAGE:
296                 {
297                         LOG_INFOF("\nUsage:^3 %s dumpitems", GetProgramCommandPrefix());
298                         return;
299                 }
300         }
301 }
302
303 void GenericCommand_dumpnotifs(float request)
304 {
305         switch(request)
306         {
307                 case CMD_REQUEST_COMMAND:
308                 {
309                         #ifndef MENUQC
310                         float fh, alsoprint = false;
311                         string filename = argv(1);
312
313                         if(filename == "")
314                         {
315                                 filename = "notifications_dump.cfg";
316                                 alsoprint = false;
317                         }
318                         else if(filename == "-")
319                         {
320                                 filename = "notifications_dump.cfg";
321                                 alsoprint = true;
322                         }
323                         fh = fopen(filename, FILE_WRITE);
324
325                         if(fh >= 0)
326                         {
327                                 Dump_Notifications(fh, alsoprint);
328                                 LOG_INFOF("Dumping notifications... File located in ^2data/data/%s^7.\n", filename);
329                                 fclose(fh);
330                         }
331                         else
332                         {
333                                 LOG_INFOF("^1Error: ^7Could not open file '%s'!\n", filename);
334                         }
335                         #else
336                         LOG_INFO(_("Notification dump command only works with cl_cmd and sv_cmd.\n"));
337                         #endif
338                         return;
339                 }
340
341                 default:
342                 case CMD_REQUEST_USAGE:
343                 {
344                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpnotifs [filename]"));
345                         LOG_INFO("  Where 'filename' is the file to write (default is notifications_dump.cfg),\n");
346                         LOG_INFO("  if supplied with '-' output to console as well as default,\n");
347                         LOG_INFO("  if left blank, it will only write to default.\n");
348                         return;
349                 }
350         }
351 }
352
353 void GenericCommand_dumpweapons(float request) // WEAPONTODO: make this work with other progs than just server
354 {
355         switch(request)
356         {
357                 case CMD_REQUEST_COMMAND:
358                 {
359                         #ifdef SVQC
360                         wep_config_file = -1;
361                         wep_config_alsoprint = -1;
362                         string filename = argv(1);
363
364                         if(filename == "")
365                         {
366                                 filename = "weapons_dump.cfg";
367                                 wep_config_alsoprint = false;
368                         }
369                         else if(filename == "-")
370                         {
371                                 filename = "weapons_dump.cfg";
372                                 wep_config_alsoprint = true;
373                         }
374                         wep_config_file = fopen(filename, FILE_WRITE);
375
376                         if(wep_config_file >= 0)
377                         {
378                                 Dump_Weapon_Settings();
379                                 LOG_INFO(sprintf("Dumping weapons... File located in ^2data/data/%s^7.\n", filename));
380                                 fclose(wep_config_file);
381                                 wep_config_file = -1;
382                                 wep_config_alsoprint = -1;
383                         }
384                         else
385                         {
386                                 LOG_INFO(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
387                         }
388                         #else
389                         LOG_INFO(_("Weapons dump command only works with sv_cmd.\n"));
390                         #endif
391                         return;
392                 }
393
394                 default:
395                 case CMD_REQUEST_USAGE:
396                 {
397                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpweapons [filename]"));
398                         LOG_INFO("  Where 'filename' is the file to write (default is weapons_dump.cfg),\n");
399                         LOG_INFO("  if supplied with '-' output to console as well as default,\n");
400                         LOG_INFO("  if left blank, it will only write to default.\n");
401                         return;
402                 }
403         }
404 }
405
406 void GenericCommand_dumpturrets(float request)
407 {
408         switch(request)
409         {
410                 case CMD_REQUEST_COMMAND:
411                 {
412                         #ifdef SVQC
413                         tur_config_file = -1;
414                         tur_config_alsoprint = -1;
415                         string filename = argv(1);
416
417                         if(filename == "")
418                         {
419                                 filename = "turrets_dump.cfg";
420                                 tur_config_alsoprint = FALSE;
421                         }
422                         else if(filename == "-")
423                         {
424                                 filename = "turrets_dump.cfg";
425                                 tur_config_alsoprint = TRUE;
426                         }
427                         tur_config_file = fopen(filename, FILE_WRITE);
428
429                         if(tur_config_file >= 0)
430                         {
431                                 Dump_Turret_Settings();
432                                 LOG_INFO(sprintf("Dumping turrets... File located in ^2data/data/%s^7.\n", filename));
433                                 fclose(tur_config_file);
434                                 tur_config_file = -1;
435                                 tur_config_alsoprint = -1;
436                         }
437                         else
438                         {
439                                 LOG_INFO(sprintf("^1Error: ^7Could not open file '%s'!\n", filename));
440                         }
441                         #else
442                         LOG_INFO(_("Turrets dump command only works with sv_cmd.\n"));
443                         #endif
444                         return;
445                 }
446
447                 default:
448                 case CMD_REQUEST_USAGE:
449                 {
450                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpturrets [filename]"));
451                         LOG_INFO("  Where 'filename' is the file to write (default is turrets_dump.cfg),\n");
452                         LOG_INFO("  if supplied with '-' output to console as well as default,\n");
453                         LOG_INFO("  if left blank, it will only write to default.\n");
454                         return;
455                 }
456         }
457 }
458
459 void GenericCommand_maplist(float request, float argc)
460 {
461         switch(request)
462         {
463                 case CMD_REQUEST_COMMAND:
464                 {
465                         string tmp_string;
466                         float i;
467
468                         switch(argv(1))
469                         {
470                                 case "add": // appends new maps to the maplist
471                                 {
472                                         if(argc == 3)
473                                         {
474                                                 if (!fexists(strcat("maps/", argv(2), ".bsp")))
475                                                 {
476                                                         LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!\n");
477                                                         break;
478                                                 }
479
480                                                 if(cvar_string("g_maplist") == "")
481                                                         cvar_set("g_maplist", argv(2));
482                                                 else
483                                                         cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
484
485                                                 return;
486                                         }
487                                         break; // go to usage
488                                 }
489
490                                 case "cleanup": // scans maplist and only adds back the ones which are really usable
491                                 {
492                                         MapInfo_Enumerate();
493                                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
494                                         argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
495
496                                         tmp_string = "";
497                                         for(i = 0; i < argc; ++i)
498                                                 if(MapInfo_CheckMap(argv(i)))
499                                                         tmp_string = strcat(tmp_string, " ", argv(i));
500
501                                         tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
502                                         cvar_set("g_maplist", tmp_string);
503
504                                         return;
505                                 }
506
507                                 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
508                                 {
509                                         if(argc == 3)
510                                         {
511                                                 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
512
513                                                 tmp_string = "";
514                                                 for(i = 0; i < argc; ++i)
515                                                         if(argv(i) != argv(2))
516                                                                 tmp_string = strcat(tmp_string, " ", argv(i));
517
518                                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
519                                                 cvar_set("g_maplist", tmp_string);
520
521                                                 return;
522                                         }
523                                         break; // go to usage
524                                 }
525
526                                 case "shuffle": // randomly shuffle the maplist
527                                 {
528                                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
529                                         return;
530                                 }
531
532                                 default: break;
533                         }
534                 }
535
536                 default:
537                         LOG_INFO("Incorrect parameters for ^2maplist^7\n");
538                 case CMD_REQUEST_USAGE:
539                 {
540                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]\n"));
541                         LOG_INFO("  Where 'action' is the command to complete,\n");
542                         LOG_INFO("  and 'map' is what it acts upon (if required).\n");
543                         LOG_INFO("  Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
544                         return;
545                 }
546         }
547 }
548
549 void GenericCommand_nextframe(float request, float arguments, string command)
550 {
551         switch(request)
552         {
553                 case CMD_REQUEST_COMMAND:
554                 {
555                         queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
556                         return;
557                 }
558
559                 default:
560                 case CMD_REQUEST_USAGE:
561                 {
562                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command...\n"));
563                         LOG_INFO("  Where command will be executed next frame of this VM\n");
564                         return;
565                 }
566         }
567 }
568
569 void GenericCommand_removefromlist(float request, float argc)
570 {
571         switch(request)
572         {
573                 case CMD_REQUEST_COMMAND:
574                 {
575                         if(argc == 3)
576                         {
577                                 float i;
578                                 string original_cvar = argv(1);
579                                 string removal = argv(2);
580                                 string tmp_string;
581
582                                 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
583
584                                 tmp_string = "";
585                                 for(i = 0; i < argc; ++i)
586                                         if(argv(i) != removal)
587                                                 tmp_string = strcat(tmp_string, " ", argv(i));
588
589                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
590                                 cvar_set(original_cvar, tmp_string);
591
592                                 return;
593                         }
594                 }
595
596                 default:
597                         LOG_INFO("Incorrect parameters for ^2removefromlist^7\n");
598                 case CMD_REQUEST_USAGE:
599                 {
600                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value\n"));
601                         LOG_INFO("  Where 'variable' is what cvar to remove 'value' from.\n");
602                         LOG_INFO("See also: ^2addtolist^7\n");
603                         return;
604                 }
605         }
606 }
607
608 void GenericCommand_restartnotifs(float request)
609 {
610         switch(request)
611         {
612                 case CMD_REQUEST_COMMAND:
613                 {
614                         #ifndef MENUQC
615                         LOG_INFOF(
616                                 strcat(
617                                         "Restart_Notifications(): Restarting %d notifications... ",
618                                         "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d\n"
619                                 ),
620                                 (
621                                         NOTIF_ANNCE_COUNT +
622                                         NOTIF_INFO_COUNT +
623                                         NOTIF_CENTER_COUNT +
624                                         NOTIF_MULTI_COUNT +
625                                         NOTIF_CHOICE_COUNT
626                                 ),
627                                 NOTIF_ANNCE_COUNT,
628                                 NOTIF_INFO_COUNT,
629                                 NOTIF_CENTER_COUNT,
630                                 NOTIF_MULTI_COUNT,
631                                 NOTIF_CHOICE_COUNT
632                         );
633                         Destroy_All_Notifications();
634                         CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
635                         #else
636                         LOG_INFO(_("Notification restart command only works with cl_cmd and sv_cmd.\n"));
637                         #endif
638                         return;
639                 }
640
641                 default:
642                 case CMD_REQUEST_USAGE:
643                 {
644                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " restartnotifs"));
645                         LOG_INFO("  No arguments required.\n");
646                         return;
647                 }
648         }
649 }
650
651 void GenericCommand_settemp(float request, float argc)
652 {
653         switch(request)
654         {
655                 case CMD_REQUEST_COMMAND:
656                 {
657                         if(argc >= 3)
658                         {
659                                 float f = cvar_settemp(argv(1), argv(2));
660                                 if(f == 1)
661                                         LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n");
662                                 else if(f == -1)
663                                         LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
664                                 // else cvar_settemp itself errors out
665
666                                 return;
667                         }
668                 }
669
670                 default:
671                         LOG_INFO("Incorrect parameters for ^2settemp^7\n");
672                 case CMD_REQUEST_USAGE:
673                 {
674                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
675                         LOG_INFO("  Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
676                         LOG_INFO("See also: ^2settemp_restore^7\n");
677                         return;
678                 }
679         }
680 }
681
682 void GenericCommand_settemp_restore(float request, float argc)
683 {
684         switch(request)
685         {
686                 case CMD_REQUEST_COMMAND:
687                 {
688                         float i = cvar_settemp_restore();
689
690                         if(i)
691                                 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
692                         else
693                                 LOG_TRACE("Nothing to restore.\n");
694
695                         return;
696                 }
697
698                 default:
699                 case CMD_REQUEST_USAGE:
700                 {
701                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
702                         LOG_INFO("  No arguments required.\n");
703                         LOG_INFO("See also: ^2settemp^7\n");
704                         return;
705                 }
706         }
707 }
708
709 void GenericCommand_runtest(float request, float argc)
710 {
711         switch(request)
712         {
713                 case CMD_REQUEST_COMMAND:
714                 {
715                         if(argc > 1)
716                         {
717                                 float i;
718                                 for(i = 1; i < argc; ++i)
719                                         TEST_Run(argv(i));
720                         }
721                         else
722                                 TEST_RunAll();
723                         return;
724                 }
725
726                 default:
727                 case CMD_REQUEST_USAGE:
728                 {
729                         LOG_INFO(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " [function to run]"));
730                         return;
731                 }
732         }
733 }
734
735 /* use this when creating a new command, making sure to place it in alphabetical order... also,
736 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
737 void GenericCommand_(float request)
738 {
739         switch(request)
740         {
741                 case CMD_REQUEST_COMMAND:
742                 {
743
744                         return;
745                 }
746
747                 default:
748                 case CMD_REQUEST_USAGE:
749                 {
750                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
751                         print("  No arguments required.\n");
752                         return;
753                 }
754         }
755 }
756 */
757
758 // ==================================
759 //  Macro system for server commands
760 // ==================================
761
762 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
763 #define GENERIC_COMMANDS(request,arguments,command) \
764         GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
765         GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
766         GENERIC_COMMAND("dumpeffectinfo", GenericCommand_dumpeffectinfo(request), "Dump all effectinfo to effectinfo_dump.txt") \
767         GENERIC_COMMAND("dumpitems", GenericCommand_dumpitems(request), "Dump all items to the console") \
768         GENERIC_COMMAND("dumpnotifs", GenericCommand_dumpnotifs(request), "Dump all notifications into notifications_dump.txt") \
769         GENERIC_COMMAND("dumpturrets", GenericCommand_dumpturrets(request), "Dump all turrets into turrets_dump.txt") \
770         GENERIC_COMMAND("dumpweapons", GenericCommand_dumpweapons(request), "Dump all weapons into weapons_dump.txt") \
771         GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
772         GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
773         GENERIC_COMMAND("qc_curl", GenericCommand_qc_curl(request, arguments), "Queries a URL") \
774         GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
775         GENERIC_COMMAND("restartnotifs", GenericCommand_restartnotifs(request), "Re-initialize all notifications") \
776         GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
777         GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
778         GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
779         GENERIC_COMMAND("runtest", GenericCommand_runtest(request, arguments), "Run unit tests") \
780         /* nothing */
781
782 void GenericCommand_macro_help()
783 {
784         #define GENERIC_COMMAND(name,function,description) \
785                 { LOG_INFO("  ^2", name, "^7: ", description, "\n"); }
786
787         GENERIC_COMMANDS(0, 0, "");
788         #undef GENERIC_COMMAND
789
790         return;
791 }
792
793 float GenericCommand_macro_command(float argc, string command)
794 {
795         #define GENERIC_COMMAND(name,function,description) \
796                 { if(name == strtolower(argv(0))) { function; return true; } }
797
798         GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command);
799         #undef GENERIC_COMMAND
800
801         return false;
802 }
803
804 float GenericCommand_macro_usage(float argc)
805 {
806         #define GENERIC_COMMAND(name,function,description) \
807                 { if(name == strtolower(argv(1))) { function; return true; } }
808
809         GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "");
810         #undef GENERIC_COMMAND
811
812         return false;
813 }
814
815 void GenericCommand_macro_write_aliases(float fh)
816 {
817         #define GENERIC_COMMAND(name,function,description) \
818                 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
819
820         GENERIC_COMMANDS(0, 0, "");
821         #undef GENERIC_COMMAND
822
823         return;
824 }
825
826
827 // ===========================================
828 //  Main Common Function For Generic Commands
829 // ===========================================
830 // Commands spread out among all programs (menu, client, and server)
831
832 float GenericCommand(string command)
833 {
834         float argc = tokenize_console(command);
835         float n, j, f, i;
836         string s, s2, c;
837         vector rgb;
838
839         // Guide for working with argc arguments by example:
840         // argc:   1    - 2      - 3     - 4
841         // argv:   0    - 1      - 2     - 3
842         // cmd     vote - master - login - password
843
844         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
845         {
846                 return true; // handled by one of the above GenericCommand_* functions
847         }
848         else if(argc >= 3 && argv(0) == "red")
849         {
850                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
851                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
852                 return true;
853         }
854         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
855         {
856                 // other test case
857                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
858
859                 n = floor(random() * 6 + 2);
860
861                 s2 = "";
862                 for(i = 0; i < n; ++i)
863                 {
864                         s2 = strcat(s2, "AH");
865                 }
866
867                 if(random() < 0.1)
868                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
869
870                 if(s == "")
871                         s = s2;
872                 else
873                         if(random() < 0.8)
874                                 s = strcat(s, " ", s2);
875                         else
876                                 s = strcat(s2, " ", s);
877
878                 s2 = substring(s, strlen(s) - 2, 2);
879                 if(s2 == "AH" || s2 == "AY")
880                         s = strcat(s, "))");
881                 else
882                         s = strcat(s, " ))");
883
884                 if(random() < 0.1)
885                         s = substring(s, 0, strlen(s) - 1);
886
887                 if(random() < 0.1)
888                         s = strconv(1, 0, 0, s);
889
890                 localcmd(strcat(argv(1), " ", s));
891
892                 return true;
893         }
894         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
895         {
896                 // test case for terencehill's color codes
897                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
898                 s2 = "";
899
900                 n = strlen(s);
901                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
902                 f = random() * 6;
903
904                 for(i = 0; i < n; ++i)
905                 {
906                         c = substring(s, i, 1);
907
908                         if(c == ";")
909                                 c = ":";
910                         else if(c == "^")
911                         {
912                                 c = "^^";
913                                 if(substring(s, i+1, 1) == "^")
914                                         ++i;
915                         }
916
917                         if(c != " ")
918                         {
919                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
920                                 c = strcat(rgb_to_hexcolor(rgb), c);
921                         }
922                         s2 = strcat(s2, c);
923                 }
924
925                 localcmd(strcat(argv(1), " ", s2));
926
927                 return true;
928         }
929
930         return false;
931 }