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