]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/command/generic.qc
3ce25e26d71272da71edde5d46f32ae482c7d879
[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 "remove": // scans maplist and only adds back whatever maps were not provided in argv(2)
274                                 {
275                                         if(argc == 3)
276                                         {
277                                                 // save argv(2) from being overridden by tokenize engine call
278                                                 string del_map_name = argv(2);
279                                                 argc = tokenizebyseparator(cvar_string("g_maplist"), " ");
280
281                                                 tmp_string = "";
282                                                 for(i = 0; i < argc; ++i)
283                                                         if(argv(i) != del_map_name)
284                                                                 tmp_string = strcat(tmp_string, " ", argv(i));
285
286                                                 tmp_string = substring(tmp_string, 1, strlen(tmp_string) - 1);
287                                                 cvar_set("g_maplist", tmp_string);
288
289                                                 return;
290                                         }
291                                         break; // go to usage
292                                 }
293
294                                 case "shuffle": // randomly shuffle the maplist
295                                 {
296                                         cvar_set("g_maplist", shufflewords(cvar_string("g_maplist")));
297                                         return;
298                                 }
299
300                                 default: break;
301                         }
302                 }
303
304                 default:
305                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
306                 case CMD_REQUEST_USAGE:
307                 {
308                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " maplist <action> [<map>]");
309                         LOG_HELP("  Where <action> is the command to complete,");
310                         LOG_HELP("  and <map> is what it acts upon (for the 'add' and 'remove' actions).");
311                         LOG_HELP("  Full list of commands here: add, cleanup, remove, shuffle.");
312                         return;
313                 }
314         }
315 }
316
317 void GenericCommand_nextframe(int request, string command)
318 {
319         switch(request)
320         {
321                 case CMD_REQUEST_COMMAND:
322                 {
323                         queue_to_execute_next_frame(substring(command, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
324                         return;
325                 }
326
327                 default:
328                 case CMD_REQUEST_USAGE:
329                 {
330                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " nextframe <command>");
331                         LOG_HELP("  Where <command> will be executed next frame of this VM");
332                         return;
333                 }
334         }
335 }
336
337 void GenericCommand_removefromlist(int request, int argc)
338 {
339         switch(request)
340         {
341                 case CMD_REQUEST_COMMAND:
342                 {
343                         if(argc == 3)
344                         {
345                                 string original_cvar = argv(1);
346                                 string removal = argv(2);
347
348                                 string tmp_string = "";
349                                 FOREACH_WORD(cvar_string(original_cvar), it != removal,
350                                 {
351                                         tmp_string = cons(tmp_string, it);
352                                 });
353
354                                 cvar_set(original_cvar, tmp_string);
355
356                                 return;
357                         }
358                 }
359
360                 default:
361                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
362                 case CMD_REQUEST_USAGE:
363                 {
364                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " removefromlist <cvar> <value>");
365                         LOG_HELP("  Where <cvar> is the cvar to remove <value> from.");
366                         LOG_HELP("See also: ^2addtolist^7");
367                         return;
368                 }
369         }
370 }
371
372 void GenericCommand_restartnotifs(int request)
373 {
374         switch(request)
375         {
376                 case CMD_REQUEST_COMMAND:
377                 {
378                         #ifdef GAMEQC
379                         int NOTIF_ANNCE_COUNT = 0;
380                         int NOTIF_INFO_COUNT = 0;
381                         int NOTIF_CENTER_COUNT = 0;
382                         int NOTIF_MULTI_COUNT = 0;
383                         int NOTIF_CHOICE_COUNT = 0;
384                         FOREACH(Notifications, true, {
385                                 switch (it.nent_type)
386                                 {
387                                         case MSG_ANNCE: ++NOTIF_ANNCE_COUNT; break;
388                                         case MSG_INFO: ++NOTIF_INFO_COUNT; break;
389                                         case MSG_CENTER: ++NOTIF_CENTER_COUNT; break;
390                                         case MSG_MULTI: ++NOTIF_MULTI_COUNT; break;
391                                         case MSG_CHOICE: ++NOTIF_CHOICE_COUNT; break;
392                                 }
393                         });
394
395                         LOG_INFOF(
396                                 "Restart_Notifications(): Restarting %d notifications... "
397                                 "Counts: MSG_ANNCE = %d, MSG_INFO = %d, MSG_CENTER = %d, MSG_MULTI = %d, MSG_CHOICE = %d",
398                                 (
399                                         NOTIF_ANNCE_COUNT +
400                                         NOTIF_INFO_COUNT +
401                                         NOTIF_CENTER_COUNT +
402                                         NOTIF_MULTI_COUNT +
403                                         NOTIF_CHOICE_COUNT
404                                 ),
405                                 NOTIF_ANNCE_COUNT,
406                                 NOTIF_INFO_COUNT,
407                                 NOTIF_CENTER_COUNT,
408                                 NOTIF_MULTI_COUNT,
409                                 NOTIF_CHOICE_COUNT
410                         );
411                         Destroy_All_Notifications();
412                         CALL_ACCUMULATED_FUNCTION(RegisterNotifications);
413                         #else
414                         LOG_INFO("Notification restart command only works with cl_cmd and sv_cmd.");
415                         #endif
416                         return;
417                 }
418
419                 default:
420                 case CMD_REQUEST_USAGE:
421                 {
422                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " restartnotifs");
423                         LOG_HELP("  No arguments required.");
424                         return;
425                 }
426         }
427 }
428
429 void GenericCommand_settemp(int request, int argc)
430 {
431         switch(request)
432         {
433                 case CMD_REQUEST_COMMAND:
434                 {
435                         if(argc >= 3)
436                         {
437                                 float f = cvar_settemp(argv(1), argv(2));
438                                 if(f == 1)
439                                         LOG_TRACE("Creating new settemp tracker for ", argv(1), " and setting it to \"", argv(2), "\" temporarily.");
440                                 else if(f == -1)
441                                         LOG_TRACE("Already had a tracker for ", argv(1), ", updating it to \"", argv(2), "\".");
442                                 // else cvar_settemp itself errors out
443
444                                 return;
445                         }
446                 }
447
448                 default:
449                         LOG_INFOF("Incorrect parameters for ^2%s^7", argv(0));
450                 case CMD_REQUEST_USAGE:
451                 {
452                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " settemp <cvar> \"<arguments>\"");
453                         LOG_HELP("  Where <cvar> is the cvar you want to temporarily set with <arguments>.");
454                         LOG_HELP("See also: ^2settemp_restore^7");
455                         return;
456                 }
457         }
458 }
459
460 void GenericCommand_settemp_restore(int request)
461 {
462         switch(request)
463         {
464                 case CMD_REQUEST_COMMAND:
465                 {
466                         float i = cvar_settemp_restore();
467
468                         if(i)
469                                 LOG_TRACE("Restored ", ftos(i), " temporary cvar settings to their original values.");
470                         else
471                                 LOG_TRACE("Nothing to restore.");
472
473                         return;
474                 }
475
476                 default:
477                 case CMD_REQUEST_USAGE:
478                 {
479                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " settemp_restore");
480                         LOG_HELP("  No arguments required.");
481                         LOG_HELP("See also: ^2settemp^7");
482                         return;
483                 }
484         }
485 }
486
487 void GenericCommand_runtest(int request, int argc)
488 {
489         switch(request)
490         {
491                 case CMD_REQUEST_COMMAND:
492                 {
493                         if(argc > 1)
494                         {
495                                 float i;
496                                 for(i = 1; i < argc; ++i)
497                                         TEST_Run(argv(i));
498                         }
499                         else
500                                 RUN_ALL_TESTS();
501                         return;
502                 }
503
504                 default:
505                 case CMD_REQUEST_USAGE:
506                 {
507                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " <function>");
508                         return;
509                 }
510         }
511 }
512
513 /* use this when creating a new command, making sure to place it in alphabetical order... also,
514 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
515 void GenericCommand_(int request)
516 {
517         switch(request)
518         {
519                 case CMD_REQUEST_COMMAND:
520                 {
521
522                         return;
523                 }
524
525                 default:
526                 case CMD_REQUEST_USAGE:
527                 {
528                         LOG_HELP("Usage:^3 ", GetProgramCommandPrefix(), " ");
529                         LOG_HELP("  No arguments required.");
530                         return;
531                 }
532         }
533 }
534 */
535
536 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
537 GENERIC_COMMAND(addtolist, "Add a string to a cvar", true) { GenericCommand_addtolist(request, arguments); }
538 GENERIC_COMMAND(maplist, "Automatic control of maplist", true) { GenericCommand_maplist(request, arguments); }
539 GENERIC_COMMAND(nextframe, "Execute the given command next frame of this VM", true) { GenericCommand_nextframe(request, command); }
540 GENERIC_COMMAND(qc_curl, "Queries a URL", true) { GenericCommand_qc_curl(request, arguments); }
541 GENERIC_COMMAND(removefromlist, "Remove a string from a cvar", true) { GenericCommand_removefromlist(request, arguments); }
542 GENERIC_COMMAND(restartnotifs, "Re-initialize all notifications", false) { GenericCommand_restartnotifs(request); }
543 GENERIC_COMMAND(rpn, "RPN calculator", true) { GenericCommand_rpn(request, arguments, command); }
544 GENERIC_COMMAND(settemp, "Temporarily set a value to a cvar which is restored later", false) { GenericCommand_settemp(request, arguments); }
545 GENERIC_COMMAND(settemp_restore, "Restore all cvars set by settemp command", false) { GenericCommand_settemp_restore(request); }
546 GENERIC_COMMAND(runtest, "Run unit tests", false) { GenericCommand_runtest(request, arguments); }
547
548 void GenericCommand_macro_help()
549 {
550         FOREACH(GENERIC_COMMANDS, true, LOG_HELPF("  ^2%s^7: %s", it.m_name, it.m_description));
551 }
552
553 float GenericCommand_macro_command(int argc, string command)
554 {
555         string c = strtolower(argv(0));
556         FOREACH(GENERIC_COMMANDS, it.m_name == c, {
557                 it.m_invokecmd(it, CMD_REQUEST_COMMAND, NULL, argc, command);
558                 return true;
559         });
560         return false;
561 }
562
563 float GenericCommand_macro_usage(int argc)
564 {
565         string c = strtolower(argv(1));
566         FOREACH(GENERIC_COMMANDS, it.m_name == c, {
567                 it.m_invokecmd(it, CMD_REQUEST_USAGE, NULL, argc, "");
568                 return true;
569         });
570         return false;
571 }
572
573 void GenericCommand_macro_write_aliases(float fh)
574 {
575         FOREACH(GENERIC_COMMANDS, true, CMD_Write_Alias("qc_cmd_svmenu", it.m_name, it.m_description));
576 }
577
578
579 // ===========================================
580 //  Main Common Function For Generic Commands
581 // ===========================================
582 // Commands spread out among all programs (menu, client, and server)
583
584 float GenericCommand(string command)
585 {
586         int argc = tokenize_console(command);
587         float n, j, f, i;
588         string s, s2, c;
589         vector rgb;
590
591         // Guide for working with argc arguments by example:
592         // argc:   1    - 2      - 3     - 4
593         // argv:   0    - 1      - 2     - 3
594         // cmd     vote - master - login - password
595
596         if(GenericCommand_macro_command(argc, command)) // continue as usual and scan for normal commands
597         {
598                 return true; // handled by one of the above GenericCommand_* functions
599         }
600         else if(argc >= 3 && argv(0) == "red")
601         {
602                 s = substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
603                 localcmd(strcat(argv(1), " ", GenericCommand_markup(s)));
604                 return true;
605         }
606         else if(argc >= 3 && crc16(0, argv(0)) == 38566 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 59830)
607         {
608                 // other test case
609                 s = strconv(2, 0, 0, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
610
611                 n = floor(random() * 6 + 2);
612
613                 s2 = "";
614                 for(i = 0; i < n; ++i)
615                 {
616                         s2 = strcat(s2, "AH");
617                 }
618
619                 if(random() < 0.1)
620                         s2 = strcat(substring(s2, 1, strlen(s2) - 1), "A");
621
622                 if(s == "")
623                         s = s2;
624                 else
625                         if(random() < 0.8)
626                                 s = strcat(s, " ", s2);
627                         else
628                                 s = strcat(s2, " ", s);
629
630                 s2 = substring(s, strlen(s) - 2, 2);
631                 if(s2 == "AH" || s2 == "AY")
632                         s = strcat(s, "))");
633                 else
634                         s = strcat(s, " ))");
635
636                 if(random() < 0.1)
637                         s = substring(s, 0, strlen(s) - 1);
638
639                 if(random() < 0.1)
640                         s = strconv(1, 0, 0, s);
641
642                 localcmd(strcat(argv(1), " ", s));
643
644                 return true;
645         }
646         else if(argc >= 3 && crc16(0, argv(0)) == 3826 && crc16(0, strcat(argv(0), argv(0), argv(0))) == 55790)
647         {
648                 // test case for terencehill's color codes
649                 s = strdecolorize(substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)));
650                 s2 = "";
651
652                 n = strlen(s);
653                 j = ((6 * max(1, floor(strlen(s)/32 + random() * 2 - 1))) / n) * (1 - 2 * (random() > 0.5));
654                 f = random() * 6;
655
656                 for(i = 0; i < n; ++i)
657                 {
658                         c = substring(s, i, 1);
659
660                         if(c == ";")
661                                 c = ":";
662                         else if(c == "^")
663                         {
664                                 c = "^^";
665                                 if(substring(s, i+1, 1) == "^")
666                                         ++i;
667                         }
668
669                         if(c != " ")
670                         {
671                                 rgb = hsl_to_rgb('1 0 0' * (j * i + f) + '0 1 .5');
672                                 c = strcat(rgb_to_hexcolor(rgb), c);
673                         }
674                         s2 = strcat(s2, c);
675                 }
676
677                 localcmd(strcat(argv(1), " ", s2));
678
679                 return true;
680         }
681
682         return false;
683 }