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