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