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