]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
Merge branch 'master' of git://de.git.xonotic.org/xonotic/xonotic-data.pk3dir
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / command / generic.qc
1 // =========================================================
2 //  Generic program common command code, written by Samual
3 //  Last updated: December 28th, 2011
4 // =========================================================
5
6 // used by generic commands for better help/usage information
7 string GetProgramCommandPrefix(void)
8 {
9         #ifdef SVQC
10         return "sv_cmd";
11         #endif
12         #ifdef CSQC
13         return "cl_cmd";
14         #endif
15         #ifdef MENUQC
16         return "menu_cmd";
17         #endif
18 }
19
20
21 // =======================
22 //  Command Sub-Functions
23 // =======================
24
25 void GenericCommand_addtolist(float request, float argc)
26 {
27         switch(request)
28         {
29                 case CMD_REQUEST_COMMAND:
30                 {
31                         float i;
32                         
33                         if(argc >= 2)
34                         {
35                                 string original_cvar = argv(1);
36                                 string tmp_string = argv(2);
37                                 
38                                 if(cvar_string(original_cvar) == "") // cvar was empty
39                                 {
40                                         cvar_set(original_cvar, tmp_string);
41                                 }
42                                 else // add it to the end of the list if the list doesn't already have it
43                                 {
44                                         argc = tokenizebyseparator(cvar_string(original_cvar), " ");
45                                         
46                                         for(i = 0; i < argc; ++i)
47                                                 if(argv(i) == tmp_string)
48                                                         return; // already in list
49                                                         
50                                         cvar_set(original_cvar, strcat(tmp_string, " ", cvar_string(original_cvar)));
51                                 }
52                                 return;
53                         }
54                 }
55                         
56                 default:
57                         print("Incorrect parameters for ^2addtolist^7\n");
58                 case CMD_REQUEST_USAGE:
59                 {
60                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " addtolist variable value"));
61                         print("  Where 'variable' is what to add 'value' to.\n");
62                         print("See also: ^2removefromlist^7\n");
63                         return;
64                 }
65         }
66 }
67
68 float curl_uri_get_pos;
69 float curl_uri_get_exec[URI_GET_CURL_END - URI_GET_CURL + 1];
70 string curl_uri_get_cvar[URI_GET_CURL_END - URI_GET_CURL + 1];
71 void Curl_URI_Get_Callback(float id, float status, string data)
72 {
73         float i;
74         float do_exec;
75         string do_cvar;
76         i = id - URI_GET_CURL;
77         do_exec = curl_uri_get_exec[i];
78         do_cvar = curl_uri_get_cvar[i];
79         if(status != 0)
80         {
81                 print(sprintf(_("error: status is %d\n"), status));
82                 if(do_cvar)
83                         strunzone(do_cvar);
84                 return;
85         }
86         if(do_exec)
87                 localcmd(data);
88         if(do_cvar)
89         {
90                 cvar_set(do_cvar, data);
91                 strunzone(do_cvar);
92         }
93         if(!do_exec && !do_cvar)
94                 print(data);
95 }
96
97 void GenericCommand_curl(float request, float argc)
98 {
99         switch(request)
100         {
101                 case CMD_REQUEST_COMMAND:
102                 {
103                         float do_exec;
104                         string do_cvar;
105                         float key;
106                         float i, j;
107                         string url;
108                         float buf;
109                         float r;
110
111                         do_exec = FALSE;
112                         do_cvar = string_null;
113                         key = -1;
114
115                         for(i = 1; i+1 < argc; ++i)
116                         {
117                                 if(argv(i) == "--cvar" && i+2 < argc)
118                                 {
119                                         ++i;
120                                         do_cvar = strzone(argv(i));
121                                         continue;
122                                 }
123                                 if(argv(i) == "--exec")
124                                 {
125                                         do_exec = TRUE;
126                                         continue;
127                                 }
128                                 if(argv(i) == "--key" && i+2 < argc)
129                                 {
130                                         ++i;
131                                         key = stof(argv(i));
132                                         continue;
133                                 }
134                                 break;
135                         }
136
137                         // now, argv(i) is the URL
138                         // following args may be POST parameters
139                         url = argv(i);
140                         ++i;
141                         buf = buf_create();
142                         j = 0;
143                         for(; i+1 < argc; i += 2)
144                                 bufstr_set(buf, ++j, sprintf("%s=%s", uri_escape(argv(i)), uri_escape(argv(i+1))));
145                         if(i < argc)
146                                 bufstr_set(buf, ++j, sprintf("submit=%s", uri_escape(argv(i))));
147
148                         if(j == 0) // no args: GET
149                                 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, string_null, string_null, -1, key);
150                         else // with args: POST
151                                 r = crypto_uri_postbuf(url, URI_GET_CURL + curl_uri_get_pos, "application/x-www-form-urlencoded", "&", buf, key);
152
153                         if(r)
154                         {
155                                 curl_uri_get_exec[curl_uri_get_pos] = do_exec;
156                                 curl_uri_get_cvar[curl_uri_get_pos] = do_cvar;
157                                 curl_uri_get_pos = mod(curl_uri_get_pos + 1, URI_GET_CURL_END - URI_GET_CURL + 1);
158                         }
159                         else
160                                 print(_("error creating curl handle\n"));
161
162                         buf_del(buf);
163
164                         return;
165                 }
166                         
167                 default:
168                 case CMD_REQUEST_USAGE:
169                 {
170                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " curl [--key N] [--cvar] [--exec] URL [postargs...]"));
171                         return;
172                 }
173         }
174 }
175
176 void GenericCommand_dumpcommands(float request)
177 {
178         switch(request)
179         {
180                 case CMD_REQUEST_COMMAND:
181                 {
182                         float fh;
183                         string filename = strcat(GetProgramCommandPrefix(), "_dump.txt");
184                         fh = fopen(filename, FILE_WRITE);
185                         
186                         if(fh >= 0)
187                         {
188                                 #ifdef SVQC
189                                         CMD_Write("dump of server console commands:\n");
190                                         GameCommand_macro_write_aliases(fh);
191                                         
192                                         CMD_Write("\ndump of networked client only commands:\n");
193                                         ClientCommand_macro_write_aliases(fh);
194                                         
195                                         CMD_Write("\ndump of common commands:\n");
196                                         CommonCommand_macro_write_aliases(fh);
197
198                                         CMD_Write("\ndump of ban commands:\n");
199                                         BanCommand_macro_write_aliases(fh);
200                                 #endif
201                                                                 
202                                 #ifdef CSQC
203                                         CMD_Write("dump of client commands:\n");
204                                         LocalCommand_macro_write_aliases(fh);
205                                 #endif
206                                 
207                                 CMD_Write("\ndump of generic commands:\n");
208                                 GenericCommand_macro_write_aliases(fh);
209                                 
210                                 print("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.\n");
211                                 
212                                 fclose(fh);
213                         }
214                         else
215                         {
216                                 print("^1Error: ^7Could not dump to file!\n");
217                         }
218                         return;
219                 }
220                         
221                 default:
222                 case CMD_REQUEST_USAGE:
223                 {
224                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " dumpcommands"));
225                         print("  No arguments required.\n");
226                         return;
227                 }
228         }
229 }
230
231 void GenericCommand_maplist(float request, float argc)
232 {
233         switch(request)
234         {
235                 case CMD_REQUEST_COMMAND:
236                 {
237                         string tmp_string;
238                         float i;
239                         
240                         switch(argv(1))
241                         {
242                                 case "add": // appends new maps to the maplist
243                                 {
244                                         if(argc == 3)
245                                         {
246                                                 if (!fexists(strcat("maps/", argv(2), ".bsp")))
247                                                 {
248                                                         print("maplist: ERROR: ", argv(2), " does not exist!\n");
249                                                         break;
250                                                 }
251                                                 
252                                                 if(cvar_string("g_maplist") == "")
253                                                         cvar_set("g_maplist", argv(2));
254                                                 else
255                                                         cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
256                                                         
257                                                 return;
258                                         }
259                                         break; // go to usage
260                                 }
261                                 
262                                 case "cleanup": // scans maplist and only adds back the ones which are really usable
263                                 {
264                                         MapInfo_Enumerate();
265                                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
266                                         argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
267                                         
268                                         for(i = 0; i < argc; ++i)
269                                                 if(MapInfo_CheckMap(argv(i)))
270                                                         tmp_string = strcat(tmp_string, " ", argv(i));
271                                                         
272                                         tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
273                                         cvar_set("g_maplist", tmp_string);
274                                         
275                                         return;
276                                 }
277                                 
278                                 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
279                                 {
280                                         if(argc == 3)
281                                         {
282                                                 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
283                                                 
284                                                 for(i = 0; i < argc; ++i)
285                                                         if(argv(i) != argv(2))
286                                                                 tmp_string = strcat(tmp_string, " ", argv(i));
287                                                                 
288                                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
289                                                 cvar_set("g_maplist", tmp_string);
290                                                 
291                                                 return;
292                                         }
293                                         break; // go to usage
294                                 }
295                                 
296                                 case "shuffle": // randomly shuffle the maplist
297                                 {
298                                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
299                                         return;
300                                 }
301                                         
302                                 default: break;
303                         }
304                 }
305                         
306                 default:
307                         print("Incorrect parameters for ^2maplist^7\n");
308                 case CMD_REQUEST_USAGE:
309                 {
310                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " maplist action [map]"));
311                         print("  Where 'action' is the command to complete,\n");
312                         print("  and 'map' is what it acts upon (if required).\n");
313                         print("  Full list of commands here: \"add, cleanup, remove, shuffle.\"\n");
314                         return;
315                 }
316         }
317 }
318
319 void GenericCommand_nextframe(float request, float arguments, string command)
320 {
321         switch(request)
322         {
323                 case CMD_REQUEST_COMMAND:
324                 {
325                         queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
326                         return;
327                 }
328                         
329                 default:
330                 case CMD_REQUEST_USAGE:
331                 {
332                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " nextframe command..."));
333                         print("  Where command will be executed next frame of this VM\n");
334                         return;
335                 }
336         }
337 }
338
339 void GenericCommand_removefromlist(float request, float argc)
340 {
341         switch(request)
342         {
343                 case CMD_REQUEST_COMMAND:
344                 {
345                         if(argc == 3)
346                         {
347                                 float i;
348                                 string original_cvar = argv(1);
349                                 string removal = argv(2);
350                                 string tmp_string;
351                                 
352                                 argc = tokenizebyseparator(cvar_string(original_cvar), " ");
353                                 
354                                 for(i = 0; i < argc; ++i)
355                                         if(argv(i) != removal)
356                                                 tmp_string = strcat(tmp_string, " ", argv(i));
357                                                 
358                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
359                                 cvar_set(original_cvar, tmp_string);
360                                 
361                                 return;
362                         }
363                 }
364                         
365                 default:
366                         print("Incorrect parameters for ^2removefromlist^7\n");
367                 case CMD_REQUEST_USAGE:
368                 {
369                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " removefromlist variable value"));
370                         print("  Where 'variable' is what cvar to remove 'value' from.\n");
371                         print("See also: ^2addtolist^7\n");
372                         return;
373                 }
374         }
375 }
376
377 void GenericCommand_settemp(float request, float argc)
378 {
379         switch(request)
380         {
381                 case CMD_REQUEST_COMMAND:
382                 {
383                         if(argc >= 3)
384                         {
385                                 if(cvar_settemp(argv(1), argv(2)))
386                                         dprint("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.\n"); 
387                                 else
388                                         dprint("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".\n");
389                         
390                                 return;
391                         }
392                 }
393                         
394                 default:
395                         print("Incorrect parameters for ^2settemp^7\n");
396                 case CMD_REQUEST_USAGE:
397                 {
398                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp \"cvar\" \"arguments\"\n"));
399                         print("  Where 'cvar' is the cvar you want to temporarily set with 'arguments'.\n");
400                         print("See also: ^2settemp_restore^7\n");
401                         return;
402                 }
403         }
404 }
405
406 void GenericCommand_settemp_restore(float request, float argc)
407 {
408         switch(request)
409         {
410                 case CMD_REQUEST_COMMAND:
411                 {
412                         float i = cvar_settemp_restore();
413                         
414                         if(i)
415                                 dprint("Restored ", ftos(i), " temporary cvar settings to their original values.\n");
416                         else
417                                 dprint("Nothing to restore.\n");
418                         
419                         return;
420                 }
421                         
422                 default:
423                 case CMD_REQUEST_USAGE:
424                 {
425                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " settemp_restore\n"));
426                         print("  No arguments required.\n");
427                         print("See also: ^2settemp^7\n");
428                         return;
429                 }
430         }
431 }
432
433 /* use this when creating a new command, making sure to place it in alphabetical order... also,
434 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
435 void GenericCommand_(float request)
436 {
437         switch(request)
438         {
439                 case CMD_REQUEST_COMMAND:
440                 {
441                         
442                         return;
443                 }
444                         
445                 default:
446                 case CMD_REQUEST_USAGE:
447                 {
448                         print(strcat("\nUsage:^3 ", GetProgramCommandPrefix(), " "));
449                         print("  No arguments required.\n");
450                         return;
451                 }
452         }
453 }
454 */
455
456 // ==================================
457 //  Macro system for server commands
458 // ==================================
459
460 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
461 #define GENERIC_COMMANDS(request,arguments,command) \
462         GENERIC_COMMAND("addtolist", GenericCommand_addtolist(request, arguments), "Add a string to a cvar") \
463         GENERIC_COMMAND("curl", GenericCommand_curl(request, arguments), "Queries an URL") \
464         GENERIC_COMMAND("dumpcommands", GenericCommand_dumpcommands(request), "Dump all commands on the program to *_cmd_dump.txt") \
465         GENERIC_COMMAND("maplist", GenericCommand_maplist(request, arguments), "Automatic control of maplist") \
466         GENERIC_COMMAND("nextframe", GenericCommand_nextframe(request, arguments, command), "Execute the given command next frame of this VM") \
467         GENERIC_COMMAND("removefromlist", GenericCommand_removefromlist(request, arguments), "Remove a string from a cvar") \
468         GENERIC_COMMAND("rpn", GenericCommand_rpn(request, arguments, command), "RPN calculator") \
469         GENERIC_COMMAND("settemp", GenericCommand_settemp(request, arguments), "Temporarily set a value to a cvar which is restored later") \
470         GENERIC_COMMAND("settemp_restore", GenericCommand_settemp_restore(request, arguments), "Restore all cvars set by settemp command") \
471         /* nothing */
472
473 void GenericCommand_macro_help()
474 {
475         #define GENERIC_COMMAND(name,function,description) \
476                 { print("  ^2", name, "^7: ", description, "\n"); }
477                 
478         GENERIC_COMMANDS(0, 0, "")
479         #undef GENERIC_COMMAND
480         
481         return;
482 }
483
484 float GenericCommand_macro_command(float argc, string command)
485 {
486         #define GENERIC_COMMAND(name,function,description) \
487                 { if(name == strtolower(argv(0))) { function; return TRUE; } }
488                 
489         GENERIC_COMMANDS(CMD_REQUEST_COMMAND, argc, command)
490         #undef GENERIC_COMMAND
491         
492         return FALSE;
493 }
494
495 float GenericCommand_macro_usage(float argc)
496 {
497         #define GENERIC_COMMAND(name,function,description) \
498                 { if(name == strtolower(argv(1))) { function; return TRUE; } }
499                 
500         GENERIC_COMMANDS(CMD_REQUEST_USAGE, argc, "")
501         #undef GENERIC_COMMAND
502         
503         return FALSE;
504 }
505
506 void GenericCommand_macro_write_aliases(float fh)
507 {
508         #define GENERIC_COMMAND(name,function,description) \
509                 { CMD_Write_Alias("qc_cmd_svmenu", name, description); }
510         
511         GENERIC_COMMANDS(0, 0, "")
512         #undef GENERIC_COMMAND
513         
514         return;
515 }
516         
517
518 // ===========================================
519 //  Main Common Function For Generic Commands
520 // ===========================================
521 // Commands spread out among all programs (menu, client, and server) 
522
523 float GenericCommand(string command)
524 {
525         float argc = tokenize_console(command);
526         float n, j, f, i;
527         string s, s2, c;
528         vector rgb;
529
530         // Guide for working with argc arguments by example:
531         // argc:   1    - 2      - 3     - 4
532         // argv:   0    - 1      - 2     - 3 
533         // cmd     vote - master - login - password
534         
535         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
536         {
537                 return TRUE; // handled by one of the above GenericCommand_* functions
538         }
539         else if(argc >= 3 && argv(0) == "red")
540         {
541                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
542                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
543                 return TRUE;
544         }
545         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
546         {
547                 // other test case
548                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
549
550                 n = floor(random() * 6 + 2);
551
552                 s2 = "";
553                 for(i = 0; i < n; ++i)
554                 {
555                         s2 = strcat(s2, "AH");
556                 }
557
558                 if(random() < 0.1)
559                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
560
561                 if(s == "")
562                         s = s2;
563                 else
564                         if(random() < 0.8)
565                                 s = strcat(s, " ", s2);
566                         else
567                                 s = strcat(s2, " ", s);
568
569                 s2 = substring(s, strlen(s) - 2, 2);
570                 if(s2 == "AH" || s2 == "AY")
571                         s = strcat(s, "))");
572                 else
573                         s = strcat(s, " ))");
574
575                 if(random() < 0.1)
576                         s = substring(s, 0, strlen(s) - 1);
577
578                 if(random() < 0.1)
579                         s = strconv(1, 0, 0, s);
580
581                 localcmd(strcat(argv(1), " ", s));
582
583                 return TRUE;
584         }
585         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
586         {
587                 // test case for terencehill's color codes
588                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
589                 s2 = "";
590                 
591                 n = strlen(s);
592                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
593                 f = random() * 6;
594
595                 for(i = 0; i < n; ++i)
596                 {
597                         c = substring(s, i, 1);
598
599                         if(c == ";")
600                                 c = ":";
601                         else if(c == "^")
602                         {
603                                 c = "^^";
604                                 if(substring(s, i+1, 1) == "^")
605                                         ++i;
606                         }
607
608                         if(c != " ")
609                         {
610                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
611                                 c = strcat(rgb_to_hexcolor(rgb), c);
612                         }
613                         s2 = strcat(s2, c);
614                 }
615
616                 localcmd(strcat(argv(1), " ", s2));
617
618                 return TRUE;
619         }
620
621         return FALSE;
622 }