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