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