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