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