]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
Merge remote-tracking branch 'origin/master' into morosophos/server-current4
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / command / generic.qc
1 #include "generic.qh"
2
3 #if defined(CSQC)
4         #include <client/command/cl_cmd.qh>
5         #include <common/command/_mod.qh>
6         #include <common/command/markup.qh>
7         #include <common/command/reg.qh>
8         #include <common/command/rpn.qh>
9         #include <common/mapinfo.qh>
10         #include <common/notifications/all.qh>
11 #elif defined(MENUQC)
12 #elif defined(SVQC)
13         #include <common/command/_mod.qh>
14         #include <common/command/markup.qh>
15         #include <common/command/reg.qh>
16         #include <common/command/rpn.qh>
17         #include <common/mapinfo.qh>
18         #include <common/notifications/all.qh>
19         #include <common/turrets/config.qh>
20         #include <common/weapons/config.qh>
21         #include <server/command/_mod.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(int request, int 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                                         FOREACH_WORD(cvar_string(original_cvar), it == tmp_string,
78                                         {
79                                                 return; // already in the list
80                                         });
81
82                                         cvar_set(original_cvar, cons(cvar_string(original_cvar), tmp_string));
83                                 }
84                                 return;
85                         }
86                 }
87
88                 default:
89                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
90                 case CMD_REQUEST_USAGE:
91                 {
92                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " addtolist <cvar> <value>");
93                         LOG_HELP("  Where <cvar> is the cvar to add <value> to.");
94                         LOG_HELP("See also: ^2removefromlist^7");
95                         return;
96                 }
97         }
98 }
99
100 void GenericCommand_qc_curl(int request, int 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"));
157
158                         buf_del(buf);
159
160                         return;
161                 }
162
163                 default:
164                 case CMD_REQUEST_USAGE:
165                 {
166                         LOG_HELP("Usage:^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 <program>_cmd_dump.txt", false)
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                                         CMD_Write("\n");
188
189                                         CMD_Write("dump of networked client only commands:\n");
190                                         ClientCommand_macro_write_aliases(fh);
191                                         CMD_Write("\n");
192
193                                         CMD_Write("dump of common commands:\n");
194                                         CommonCommand_macro_write_aliases(fh);
195                                         CMD_Write("\n");
196
197                                         CMD_Write("dump of ban commands:\n");
198                                         BanCommand_macro_write_aliases(fh);
199                                         CMD_Write("\n");
200                                 #endif
201
202                                 #ifdef CSQC
203                                         CMD_Write("dump of client commands:\n");
204                                         LocalCommand_macro_write_aliases(fh);
205                                         CMD_Write("\n");
206                                 #endif
207
208                                 CMD_Write("dump of generic commands:\n");
209                                 GenericCommand_macro_write_aliases(fh);
210
211                                 LOG_INFO("Completed dump of aliases in ^2data/data/", GetProgramCommandPrefix(), "_dump.txt^7.");
212
213                                 fclose(fh);
214                         }
215                         else
216                         {
217                                 LOG_INFO("^1Error: ^7Could not dump to file!");
218                         }
219                         return;
220                 }
221
222                 default:
223                 case CMD_REQUEST_USAGE:
224                 {
225                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " dumpcommands");
226                         LOG_HELP("  No arguments required.");
227                         return;
228                 }
229         }
230 }
231
232 void GenericCommand_maplist(int request, int argc)
233 {
234         switch(request)
235         {
236                 case CMD_REQUEST_COMMAND:
237                 {
238                         string tmp_string;
239                         float i;
240
241                         switch(argv(1))
242                         {
243                                 case "add": // appends new maps to the maplist
244                                 {
245                                         if(argc == 3)
246                                         {
247                                                 if (!fexists(strcat("maps/", argv(2), ".bsp")))
248                                                 {
249                                                         LOG_INFO("maplist: ERROR: ", argv(2), " does not exist!");
250                                                         break;
251                                                 }
252
253                                                 if(cvar_string("g_maplist") == "")
254                                                         cvar_set("g_maplist", argv(2));
255                                                 else
256                                                         cvar_set("g_maplist", strcat(argv(2), " ", cvar_string("g_maplist")));
257
258                                                 return;
259                                         }
260                                         break; // go to usage
261                                 }
262
263                                 case "cleanup": // scans maplist and only adds back the ones which are really usable
264                                 {
265                                         MapInfo_Enumerate();
266                                         MapInfo_FilterGametype(MapInfo_CurrentGametype(), MapInfo_CurrentFeatures(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags(), 0);
267                                         string filtered = "";
268                                         FOREACH_WORD(cvar_string("g_maplist"), MapInfo_CheckMap(it), filtered = cons(filtered, it));
269                                         cvar_set("g_maplist", filtered);
270                                         return;
271                                 }
272
273                         case "rebuild": // rebuilds maplist to include available maps, useful after doing fs_rescan
274                           {
275                             cvar_set("g_maplist", MapInfo_ListAllowedMaps(MapInfo_CurrentGametype(), MapInfo_RequiredFlags(), MapInfo_ForbiddenFlags()));
276                             return;
277                           }
278
279                                 case "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
280                                 {
281                                         if(argc == 3)
282                                         {
283                                                 // save argv(2) from being overridden by tokenize engine call
284                                                 string del_map_name = argv(2);
285                                                 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
286
287                                                 tmp_string = "";
288                                                 for(i = 0; i < argc; ++i)
289                                                         if(argv(i) != del_map_name)
290                                                                 tmp_string = strcat(tmp_string, " ", argv(i));
291
292                                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
293                                                 cvar_set("g_maplist", tmp_string);
294
295                                                 return;
296                                         }
297                                         break; // go to usage
298                                 }
299
300                                 case "shuffle": // randomly shuffle the maplist
301                                 {
302                                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
303                                         return;
304                                 }
305
306                                 default: break;
307                         }
308                 }
309
310                 default:
311                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
312                 case CMD_REQUEST_USAGE:
313                 {
314                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " maplist <action> [<map>]");
315                         LOG_HELP("  Where <action> is the command to complete,");
316                         LOG_HELP("  and <map> is what it acts upon (for the 'add' and 'remove' actions).");
317                         LOG_HELP("  Full list of commands here: add, cleanup, remove, shuffle.");
318                         return;
319                 }
320         }
321 }
322
323 void GenericCommand_nextframe(int request, string command)
324 {
325         switch(request)
326         {
327                 case CMD_REQUEST_COMMAND:
328                 {
329                         queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
330                         return;
331                 }
332
333                 default:
334                 case CMD_REQUEST_USAGE:
335                 {
336                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " nextframe <command>");
337                         LOG_HELP("  Where <command> will be executed next frame of this VM");
338                         return;
339                 }
340         }
341 }
342
343 void GenericCommand_removefromlist(int request, int argc)
344 {
345         switch(request)
346         {
347                 case CMD_REQUEST_COMMAND:
348                 {
349                         if(argc == 3)
350                         {
351                                 string original_cvar = argv(1);
352                                 string removal = argv(2);
353
354                                 string tmp_string = "";
355                                 FOREACH_WORD(cvar_string(original_cvar), it != removal,
356                                 {
357                                         tmp_string = cons(tmp_string, it);
358                                 });
359
360                                 cvar_set(original_cvar, tmp_string);
361
362                                 return;
363                         }
364                 }
365
366                 default:
367                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
368                 case CMD_REQUEST_USAGE:
369                 {
370                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " removefromlist <cvar> <value>");
371                         LOG_HELP("  Where <cvar> is the cvar to remove <value> from.");
372                         LOG_HELP("See also: ^2addtolist^7");
373                         return;
374                 }
375         }
376 }
377
378 void GenericCommand_restartnotifs(int request)
379 {
380         switch(request)
381         {
382                 case CMD_REQUEST_COMMAND:
383                 {
384                         #ifdef GAMEQC
385                         int NOTIF_ANNCE_COUNT = 0;
386                         int NOTIF_INFO_COUNT = 0;
387                         int NOTIF_CENTER_COUNT = 0;
388                         int NOTIF_MULTI_COUNT = 0;
389                         int NOTIF_CHOICE_COUNT = 0;
390                         FOREACH(Notifications, true, {
391                                 switch (it.nent_type)
392                                 {
393                                         case MSG_ANNCE: ++NOTIF_ANNCE_COUNT; break;
394                                         case MSG_INFO: ++NOTIF_INFO_COUNT; break;
395                                         case MSG_CENTER: ++NOTIF_CENTER_COUNT; break;
396                                         case MSG_MULTI: ++NOTIF_MULTI_COUNT; break;
397                                         case MSG_CHOICE: ++NOTIF_CHOICE_COUNT; break;
398                                 }
399                         });
400
401                         LOG_INFOF(
402                                 "Restart_Notifications(): Restarting %d notifications... "
403                                 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d",
404                                 (
405                                         NOTIF_ANNCE_COUNT +
406                                         NOTIF_INFO_COUNT +
407                                         NOTIF_CENTER_COUNT +
408                                         NOTIF_MULTI_COUNT +
409                                         NOTIF_CHOICE_COUNT
410                                 ),
411                                 NOTIF_ANNCE_COUNT,
412                                 NOTIF_INFO_COUNT,
413                                 NOTIF_CENTER_COUNT,
414                                 NOTIF_MULTI_COUNT,
415                                 NOTIF_CHOICE_COUNT
416                         );
417                         Destroy_All_Notifications();
418                         CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
419                         #else
420                         LOG_INFO("Notification restart command only works with cl_cmd and sv_cmd.");
421                         #endif
422                         return;
423                 }
424
425                 default:
426                 case CMD_REQUEST_USAGE:
427                 {
428                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " restartnotifs");
429                         LOG_HELP("  No arguments required.");
430                         return;
431                 }
432         }
433 }
434
435 void GenericCommand_settemp(int request, int argc)
436 {
437         switch(request)
438         {
439                 case CMD_REQUEST_COMMAND:
440                 {
441                         if(argc >= 3)
442                         {
443                                 float f = cvar_settemp(argv(1), argv(2));
444                                 if(f == 1)
445                                         LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.");
446                                 else if(f == -1)
447                                         LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".");
448                                 // else cvar_settemp itself errors out
449
450                                 return;
451                         }
452                 }
453
454                 default:
455                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
456                 case CMD_REQUEST_USAGE:
457                 {
458                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " settemp <cvar> \"<arguments>\"");
459                         LOG_HELP("  Where <cvar> is the cvar you want to temporarily set with <arguments>.");
460                         LOG_HELP("See also: ^2settemp_restore^7");
461                         return;
462                 }
463         }
464 }
465
466 void GenericCommand_settemp_restore(int request)
467 {
468         switch(request)
469         {
470                 case CMD_REQUEST_COMMAND:
471                 {
472                         float i = cvar_settemp_restore();
473
474                         if(i)
475                                 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.");
476                         else
477                                 LOG_TRACE("Nothing to restore.");
478
479                         return;
480                 }
481
482                 default:
483                 case CMD_REQUEST_USAGE:
484                 {
485                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " settemp_restore");
486                         LOG_HELP("  No arguments required.");
487                         LOG_HELP("See also: ^2settemp^7");
488                         return;
489                 }
490         }
491 }
492
493 void GenericCommand_runtest(int request, int argc)
494 {
495         switch(request)
496         {
497                 case CMD_REQUEST_COMMAND:
498                 {
499                         if(argc > 1)
500                         {
501                                 float i;
502                                 for(i = 1; i < argc; ++i)
503                                         TEST_Run(argv(i));
504                         }
505                         else
506                                 RUN_ALL_TESTS();
507                         return;
508                 }
509
510                 default:
511                 case CMD_REQUEST_USAGE:
512                 {
513                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " <function>");
514                         return;
515                 }
516         }
517 }
518
519 /* use this when creating a new command, making sure to place it in alphabetical order... also,
520 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
521 void GenericCommand_(int request)
522 {
523         switch(request)
524         {
525                 case CMD_REQUEST_COMMAND:
526                 {
527
528                         return;
529                 }
530
531                 default:
532                 case CMD_REQUEST_USAGE:
533                 {
534                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " ");
535                         LOG_HELP("  No arguments required.");
536                         return;
537                 }
538         }
539 }
540 */
541
542 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
543 GENERIC_COMMAND(addtolist, "Add a string to a cvar", true) { GenericCommand_addtolist(request, arguments); }
544 GENERIC_COMMAND(maplist, "Automatic control of maplist", true) { GenericCommand_maplist(request, arguments); }
545 GENERIC_COMMAND(nextframe, "Execute the given command next frame of this VM", true) { GenericCommand_nextframe(request, command); }
546 GENERIC_COMMAND(qc_curl, "Queries a URL", true) { GenericCommand_qc_curl(request, arguments); }
547 GENERIC_COMMAND(removefromlist, "Remove a string from a cvar", true) { GenericCommand_removefromlist(request, arguments); }
548 GENERIC_COMMAND(restartnotifs, "Re-initialize all notifications", false) { GenericCommand_restartnotifs(request); }
549 GENERIC_COMMAND(rpn, "RPN calculator", true) { GenericCommand_rpn(request, arguments, command); }
550 GENERIC_COMMAND(settemp, "Temporarily set a value to a cvar which is restored later", false) { GenericCommand_settemp(request, arguments); }
551 GENERIC_COMMAND(settemp_restore, "Restore all cvars set by settemp command", false) { GenericCommand_settemp_restore(request); }
552 GENERIC_COMMAND(runtest, "Run unit tests", false) { GenericCommand_runtest(request, arguments); }
553
554 void GenericCommand_macro_help()
555 {
556         FOREACH(GENERIC_COMMANDS, true, LOG_HELPF("  ^2%s^7: %s", it.m_name, it.m_description));
557 }
558
559 float GenericCommand_macro_command(int argc, string command)
560 {
561         string c = strtolower(argv(0));
562         FOREACH(GENERIC_COMMANDS, it.m_name == c, {
563                 it.m_invokecmd(it, CMD_REQUEST_COMMAND, NULL, argc, command);
564                 return true;
565         });
566         return false;
567 }
568
569 float GenericCommand_macro_usage(int argc)
570 {
571         string c = strtolower(argv(1));
572         FOREACH(GENERIC_COMMANDS, it.m_name == c, {
573                 it.m_invokecmd(it, CMD_REQUEST_USAGE, NULL, argc, "");
574                 return true;
575         });
576         return false;
577 }
578
579 void GenericCommand_macro_write_aliases(float fh)
580 {
581         FOREACH(GENERIC_COMMANDS, true, CMD_Write_Alias("qc_cmd_svmenu", it.m_name, it.m_description));
582 }
583
584
585 // ===========================================
586 //  Main Common Function For Generic Commands
587 // ===========================================
588 // Commands spread out among all programs (menu, client, and server)
589
590 float GenericCommand(string command)
591 {
592         int argc = tokenize_console(command);
593         float n, j, f, i;
594         string s, s2, c;
595         vector rgb;
596
597         // Guide for working with argc arguments by example:
598         // argc:   1    - 2      - 3     - 4
599         // argv:   0    - 1      - 2     - 3
600         // cmd     vote - master - login - password
601
602         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
603         {
604                 return true; // handled by one of the above GenericCommand_* functions
605         }
606         else if(argc >= 3 && argv(0) == "red")
607         {
608                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
609                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
610                 return true;
611         }
612         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
613         {
614                 // other test case
615                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
616
617                 n = floor(random() * 6 + 2);
618
619                 s2 = "";
620                 for(i = 0; i < n; ++i)
621                 {
622                         s2 = strcat(s2, "AH");
623                 }
624
625                 if(random() < 0.1)
626                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
627
628                 if(s == "")
629                         s = s2;
630                 else
631                         if(random() < 0.8)
632                                 s = strcat(s, " ", s2);
633                         else
634                                 s = strcat(s2, " ", s);
635
636                 s2 = substring(s, strlen(s) - 2, 2);
637                 if(s2 == "AH" || s2 == "AY")
638                         s = strcat(s, "))");
639                 else
640                         s = strcat(s, " ))");
641
642                 if(random() < 0.1)
643                         s = substring(s, 0, strlen(s) - 1);
644
645                 if(random() < 0.1)
646                         s = strconv(1, 0, 0, s);
647
648                 localcmd(strcat(argv(1), " ", s));
649
650                 return true;
651         }
652         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
653         {
654                 // test case for terencehill's color codes
655                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
656                 s2 = "";
657
658                 n = strlen(s);
659                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
660                 f = random() * 6;
661
662                 for(i = 0; i < n; ++i)
663                 {
664                         c = substring(s, i, 1);
665
666                         if(c == ";")
667                                 c = ":";
668                         else if(c == "^")
669                         {
670                                 c = "^^";
671                                 if(substring(s, i+1, 1) == "^")
672                                         ++i;
673                         }
674
675                         if(c != " ")
676                         {
677                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
678                                 c = strcat(rgb_to_hexcolor(rgb), c);
679                         }
680                         s2 = strcat(s2, c);
681                 }
682
683                 localcmd(strcat(argv(1), " ", s2));
684
685                 return true;
686         }
687
688         return false;
689 }