1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4#include "console.h"
5
6#include "config.h"
7#include "linereader.h"
8
9#include <base/color.h>
10#include <base/dbg.h>
11#include <base/io.h>
12#include <base/log.h>
13#include <base/math.h>
14#include <base/mem.h>
15#include <base/str.h>
16
17#include <engine/client/checksum.h>
18#include <engine/console.h>
19#include <engine/shared/protocol.h>
20#include <engine/storage.h>
21
22#include <algorithm>
23#include <iterator> // std::size
24#include <new>
25
26// todo: rework this
27
28CConsole::CResult::CResult(int ClientId) :
29 IResult(ClientId)
30{
31 mem_zero(block: m_aStringStorage, size: sizeof(m_aStringStorage));
32 m_pArgsStart = nullptr;
33 m_pCommand = nullptr;
34 std::fill(first: std::begin(arr&: m_apArgs), last: std::end(arr&: m_apArgs), value: nullptr);
35}
36
37CConsole::CResult::CResult(const CResult &Other) :
38 IResult(Other)
39{
40 mem_copy(dest: m_aStringStorage, source: Other.m_aStringStorage, size: sizeof(m_aStringStorage));
41 m_pArgsStart = m_aStringStorage + (Other.m_pArgsStart - Other.m_aStringStorage);
42 m_pCommand = m_aStringStorage + (Other.m_pCommand - Other.m_aStringStorage);
43 for(unsigned i = 0; i < Other.m_NumArgs; ++i)
44 m_apArgs[i] = m_aStringStorage + (Other.m_apArgs[i] - Other.m_aStringStorage);
45}
46
47void CConsole::CResult::AddArgument(const char *pArg)
48{
49 m_apArgs[m_NumArgs++] = pArg;
50}
51
52void CConsole::CResult::RemoveArgument(unsigned Index)
53{
54 dbg_assert(Index < m_NumArgs, "invalid argument index");
55 for(unsigned i = Index; i < m_NumArgs - 1; i++)
56 m_apArgs[i] = m_apArgs[i + 1];
57
58 m_apArgs[m_NumArgs--] = nullptr;
59}
60
61const char *CConsole::CResult::GetString(unsigned Index) const
62{
63 if(Index >= m_NumArgs)
64 return "";
65 return m_apArgs[Index];
66}
67
68int CConsole::CResult::GetInteger(unsigned Index) const
69{
70 if(Index >= m_NumArgs)
71 return 0;
72 int Out;
73 return str_toint(str: m_apArgs[Index], out: &Out) ? Out : 0;
74}
75
76float CConsole::CResult::GetFloat(unsigned Index) const
77{
78 if(Index >= m_NumArgs)
79 return 0.0f;
80 float Out;
81 return str_tofloat(str: m_apArgs[Index], out: &Out) ? Out : 0.0f;
82}
83
84ColorHSLA CConsole::CResult::GetColor(unsigned Index, float DarkestLighting) const
85{
86 if(Index >= m_NumArgs)
87 return ColorHSLA(0, 0, 0);
88 return ColorParse(pStr: m_apArgs[Index], DarkestLighting).value_or(u: ColorHSLA(0, 0, 0));
89}
90
91void CConsole::CCommand::SetAccessLevel(EAccessLevel AccessLevel)
92{
93 m_AccessLevel = AccessLevel;
94}
95
96const IConsole::ICommandInfo *CConsole::FirstCommandInfo(int ClientId, int FlagMask) const
97{
98 for(const CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
99 {
100 if(pCommand->m_Flags & FlagMask && CanUseCommand(ClientId, pCommand))
101 return pCommand;
102 }
103
104 return nullptr;
105}
106
107const IConsole::ICommandInfo *CConsole::NextCommandInfo(const IConsole::ICommandInfo *pInfo, int ClientId, int FlagMask) const
108{
109 const CCommand *pNext = ((CCommand *)pInfo)->Next();
110 while(pNext)
111 {
112 if(pNext->m_Flags & FlagMask && CanUseCommand(ClientId, pCommand: pNext))
113 break;
114 pNext = pNext->Next();
115 }
116 return pNext;
117}
118
119std::optional<CConsole::EAccessLevel> CConsole::AccessLevelToEnum(const char *pAccessLevel)
120{
121 // alias for legacy integer access levels
122 if(!str_comp(a: pAccessLevel, b: "0"))
123 return EAccessLevel::ADMIN;
124 if(!str_comp(a: pAccessLevel, b: "1"))
125 return EAccessLevel::MODERATOR;
126 if(!str_comp(a: pAccessLevel, b: "2"))
127 return EAccessLevel::HELPER;
128 if(!str_comp(a: pAccessLevel, b: "3"))
129 return EAccessLevel::USER;
130
131 // string access levels
132 if(!str_comp(a: pAccessLevel, b: "admin"))
133 return EAccessLevel::ADMIN;
134 if(!str_comp(a: pAccessLevel, b: "moderator"))
135 return EAccessLevel::MODERATOR;
136 if(!str_comp(a: pAccessLevel, b: "helper"))
137 return EAccessLevel::HELPER;
138 if(!str_comp(a: pAccessLevel, b: "all"))
139 return EAccessLevel::USER;
140 return std::nullopt;
141}
142
143const char *CConsole::AccessLevelToString(EAccessLevel AccessLevel)
144{
145 switch(AccessLevel)
146 {
147 case EAccessLevel::ADMIN:
148 return "admin";
149 case EAccessLevel::MODERATOR:
150 return "moderator";
151 case EAccessLevel::HELPER:
152 return "helper";
153 case EAccessLevel::USER:
154 return "all";
155 }
156 dbg_assert_failed("invalid access level: %d", (int)AccessLevel);
157}
158
159// the maximum number of tokens occurs in a string of length CONSOLE_MAX_STR_LENGTH with tokens size 1 separated by single spaces
160
161int CConsole::ParseStart(CResult *pResult, const char *pString, int Length)
162{
163 char *pStr;
164 int Len = sizeof(pResult->m_aStringStorage);
165 if(Length < Len)
166 Len = Length;
167
168 str_copy(dst: pResult->m_aStringStorage, src: pString, dst_size: Len);
169 pStr = pResult->m_aStringStorage;
170
171 // get command
172 pStr = str_skip_whitespaces(str: pStr);
173 pResult->m_pCommand = pStr;
174 pStr = str_skip_to_whitespace(str: pStr);
175
176 if(*pStr)
177 {
178 pStr[0] = 0;
179 pStr++;
180 }
181
182 pResult->m_pArgsStart = pStr;
183 return 0;
184}
185
186int CConsole::ParseArgs(CResult *pResult, const char *pFormat)
187{
188 char *pStr = pResult->m_pArgsStart;
189 bool Optional = false;
190
191 pResult->ResetVictim();
192
193 for(char Command = *pFormat; Command != '\0'; Command = NextParam(pFormat))
194 {
195 if(Command == '?')
196 {
197 Optional = true;
198 continue;
199 }
200
201 pStr = str_skip_whitespaces(str: pStr);
202
203 if(*pStr == '\0') // error, non optional command needs value
204 {
205 if(!Optional)
206 {
207 return PARSEARGS_MISSING_VALUE;
208 }
209
210 while(Command)
211 {
212 if(Command == 'v')
213 {
214 pResult->SetVictim("me");
215 break;
216 }
217 Command = NextParam(pFormat);
218 }
219 return PARSEARGS_OK;
220 }
221
222 // add token
223 if(*pStr == '"')
224 {
225 pStr++;
226 pResult->AddArgument(pArg: pStr);
227
228 char *pDst = pStr; // we might have to process escape data
229 while(pStr[0] != '"')
230 {
231 if(pStr[0] == '\\')
232 {
233 if(pStr[1] == '\\')
234 pStr++; // skip due to escape
235 else if(pStr[1] == '"')
236 pStr++; // skip due to escape
237 }
238 else if(pStr[0] == '\0')
239 {
240 return PARSEARGS_MISSING_VALUE; // return error
241 }
242
243 *pDst = *pStr;
244 pDst++;
245 pStr++;
246 }
247 *pDst = '\0';
248
249 pStr++;
250 }
251 else
252 {
253 pResult->AddArgument(pArg: pStr);
254
255 if(Command == 'r') // rest of the string
256 {
257 return PARSEARGS_OK;
258 }
259
260 pStr = str_skip_to_whitespace(str: pStr);
261 if(pStr[0] != '\0') // check for end of string
262 {
263 pStr[0] = '\0';
264 pStr++;
265 }
266
267 // validate arguments
268 if(Command == 'v')
269 {
270 pResult->SetVictim(pResult->GetString(Index: pResult->NumArguments() - 1));
271 }
272 else if(Command == 'i')
273 {
274 int Value;
275 if(!str_toint(str: pResult->GetString(Index: pResult->NumArguments() - 1), out: &Value) ||
276 Value == std::numeric_limits<int>::max() ||
277 Value == std::numeric_limits<int>::min())
278 {
279 return PARSEARGS_INVALID_INTEGER;
280 }
281 }
282 else if(Command == 'c')
283 {
284 auto Color = ColorParse(pStr: pResult->GetString(Index: pResult->NumArguments() - 1), DarkestLighting: 0.0f);
285 if(!Color.has_value())
286 {
287 return PARSEARGS_INVALID_COLOR;
288 }
289 }
290 else if(Command == 'f')
291 {
292 float Value;
293 if(!str_tofloat(str: pResult->GetString(Index: pResult->NumArguments() - 1), out: &Value) ||
294 Value == std::numeric_limits<float>::max() ||
295 Value == std::numeric_limits<float>::min())
296 {
297 return PARSEARGS_INVALID_FLOAT;
298 }
299 }
300 // 's' and unknown commands are handled as strings
301 }
302 }
303
304 return PARSEARGS_OK;
305}
306
307char CConsole::NextParam(const char *&pFormat)
308{
309 if(*pFormat)
310 {
311 pFormat++;
312
313 if(*pFormat == '[')
314 {
315 // skip bracket contents
316 for(; *pFormat != ']'; pFormat++)
317 {
318 if(!*pFormat)
319 return *pFormat;
320 }
321
322 // skip ']'
323 pFormat++;
324
325 // skip space if there is one
326 if(*pFormat == ' ')
327 pFormat++;
328 }
329 }
330 return *pFormat;
331}
332
333LEVEL IConsole::ToLogLevel(int Level)
334{
335 switch(Level)
336 {
337 case IConsole::OUTPUT_LEVEL_STANDARD:
338 return LEVEL_INFO;
339 case IConsole::OUTPUT_LEVEL_ADDINFO:
340 return LEVEL_DEBUG;
341 case IConsole::OUTPUT_LEVEL_DEBUG:
342 return LEVEL_TRACE;
343 }
344 dbg_assert(0, "invalid log level");
345 return LEVEL_INFO;
346}
347
348int IConsole::ToLogLevelFilter(int Level)
349{
350 if(!(-3 <= Level && Level <= 2))
351 {
352 dbg_assert(0, "invalid log level filter");
353 }
354 return Level + 2;
355}
356
357static LOG_COLOR ColorToLogColor(ColorRGBA Color)
358{
359 return LOG_COLOR{
360 .r: (uint8_t)(Color.r * 255.0),
361 .g: (uint8_t)(Color.g * 255.0),
362 .b: (uint8_t)(Color.b * 255.0)};
363}
364
365void CConsole::Print(int Level, const char *pFrom, const char *pStr, ColorRGBA PrintColor) const
366{
367 LEVEL LogLevel = IConsole::ToLogLevel(Level);
368 // if console colors are not enabled or if the color is pure white, use default terminal color
369 if(g_Config.m_ConsoleEnableColors && PrintColor != CONSOLE_DEFAULT_COLOR)
370 {
371 log_log_color(level: LogLevel, color: ColorToLogColor(Color: PrintColor), sys: pFrom, fmt: "%s", pStr);
372 }
373 else
374 {
375 log_log(level: LogLevel, sys: pFrom, fmt: "%s", pStr);
376 }
377}
378
379void CConsole::SetGetVictimsCommandCallback(FGetVictimsCommandCallback pfnCallback, void *pUser)
380{
381 m_pfnGetVictimsCommandCallback = pfnCallback;
382 m_pGetVictimsCommandUserData = pUser;
383}
384
385void CConsole::SetTeeHistorianCommandCallback(FTeeHistorianCommandCallback pfnCallback, void *pUser)
386{
387 m_pfnTeeHistorianCommandCallback = pfnCallback;
388 m_pTeeHistorianCommandUserdata = pUser;
389}
390
391void CConsole::SetUnknownCommandCallback(FUnknownCommandCallback pfnCallback, void *pUser)
392{
393 m_pfnUnknownCommandCallback = pfnCallback;
394 m_pUnknownCommandUserdata = pUser;
395}
396
397void CConsole::SetCanUseCommandCallback(FCanUseCommandCallback pfnCallback, void *pUser)
398{
399 m_pfnCanUseCommandCallback = pfnCallback;
400 m_pCanUseCommandUserData = pUser;
401}
402
403void CConsole::InitChecksum(CChecksumData *pData) const
404{
405 pData->m_NumCommands = 0;
406 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
407 {
408 if(pData->m_NumCommands < (int)(std::size(pData->m_aCommandsChecksum)))
409 {
410 FCommandCallback pfnCallback = pCommand->m_pfnCallback;
411 void *pUserData = pCommand->m_pUserData;
412 TraverseChain(ppfnCallback: &pfnCallback, ppUserData: &pUserData);
413 int CallbackBits = (uintptr_t)pfnCallback & 0xfff;
414 int *pTarget = &pData->m_aCommandsChecksum[pData->m_NumCommands];
415 *pTarget = ((uint8_t)pCommand->m_pName[0]) | ((uint8_t)pCommand->m_pName[1] << 8) | (CallbackBits << 16);
416 }
417 pData->m_NumCommands += 1;
418 }
419}
420
421bool CConsole::LineIsValid(const char *pStr)
422{
423 if(!pStr || *pStr == 0)
424 return false;
425
426 do
427 {
428 CResult Result(IConsole::CLIENT_ID_UNSPECIFIED);
429 const char *pEnd = pStr;
430 const char *pNextPart = nullptr;
431 bool InString = false;
432 bool IsEscaping = false;
433
434 while(*pEnd)
435 {
436 if(IsEscaping)
437 {
438 IsEscaping = false;
439 }
440 else if(*pEnd == '"')
441 {
442 InString = !InString;
443 }
444 else if(InString && *pEnd == '\\') // escape sequences
445 {
446 IsEscaping = true;
447 }
448
449 if(!InString)
450 {
451 if(*pEnd == ';') // command separator
452 {
453 pNextPart = pEnd + 1;
454 break;
455 }
456 else if(*pEnd == '#') // comment, no need to do anything more
457 break;
458 }
459
460 pEnd++;
461 }
462
463 if(ParseStart(pResult: &Result, pString: pStr, Length: (pEnd - pStr) + 1) != 0)
464 return false;
465
466 CCommand *pCommand = FindCommand(pName: Result.m_pCommand, FlagMask: m_FlagMask);
467 if(!pCommand || ParseArgs(pResult: &Result, pFormat: pCommand->m_pParams))
468 return false;
469
470 pStr = pNextPart;
471 } while(pStr && *pStr);
472
473 return true;
474}
475
476void CConsole::ExecuteLineStroked(int Stroke, const char *pStr, int ClientId, bool InterpretSemicolons)
477{
478 const char *pWithoutPrefix = str_startswith(str: pStr, prefix: "mc;");
479 if(pWithoutPrefix)
480 {
481 InterpretSemicolons = true;
482 pStr = pWithoutPrefix;
483 }
484 while(pStr && *pStr)
485 {
486 CResult Result(ClientId);
487 const char *pEnd = pStr;
488 const char *pNextPart = nullptr;
489 bool InString = false;
490 bool IsEscaping = false;
491
492 while(*pEnd)
493 {
494 if(IsEscaping)
495 {
496 IsEscaping = false;
497 }
498 else if(*pEnd == '"')
499 {
500 InString = !InString;
501 }
502 else if(InString && *pEnd == '\\') // escape sequences
503 {
504 IsEscaping = true;
505 }
506
507 if(!InString && InterpretSemicolons)
508 {
509 if(*pEnd == ';') // command separator
510 {
511 pNextPart = pEnd + 1;
512 break;
513 }
514 else if(*pEnd == '#') // comment, no need to do anything more
515 break;
516 }
517
518 pEnd++;
519 }
520
521 if(ParseStart(pResult: &Result, pString: pStr, Length: (pEnd - pStr) + 1) != 0)
522 return;
523
524 if(!*Result.m_pCommand)
525 {
526 if(pNextPart)
527 {
528 pStr = pNextPart;
529 continue;
530 }
531 return;
532 }
533
534 CCommand *pCommand;
535 if(ClientId == IConsole::CLIENT_ID_GAME)
536 pCommand = FindCommand(pName: Result.m_pCommand, FlagMask: m_FlagMask | CFGFLAG_GAME);
537 else
538 pCommand = FindCommand(pName: Result.m_pCommand, FlagMask: m_FlagMask);
539
540 if(pCommand)
541 {
542 if(ClientId == IConsole::CLIENT_ID_GAME && !(pCommand->m_Flags & CFGFLAG_GAME))
543 {
544 if(Stroke)
545 {
546 char aBuf[CMDLINE_LENGTH + 64];
547 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Command '%s' cannot be executed from a map.", Result.m_pCommand);
548 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
549 }
550 }
551 else if(ClientId == IConsole::CLIENT_ID_NO_GAME && pCommand->m_Flags & CFGFLAG_GAME)
552 {
553 if(Stroke)
554 {
555 char aBuf[CMDLINE_LENGTH + 64];
556 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Command '%s' cannot be executed from a non-map config file.", Result.m_pCommand);
557 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
558 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Hint: Put the command in '%s.cfg' instead of '%s.map.cfg' ", g_Config.m_SvMap, g_Config.m_SvMap);
559 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
560 }
561 }
562 else if(CanUseCommand(ClientId: Result.m_ClientId, pCommand))
563 {
564 int IsStrokeCommand = 0;
565 if(Result.m_pCommand[0] == '+')
566 {
567 // insert the stroke direction token
568 Result.AddArgument(pArg: m_apStrokeStr[Stroke]);
569 IsStrokeCommand = 1;
570 }
571
572 if(Stroke || IsStrokeCommand)
573 {
574 if(int Error = ParseArgs(pResult: &Result, pFormat: pCommand->m_pParams))
575 {
576 char aBuf[CMDLINE_LENGTH + 64];
577 if(Error == PARSEARGS_INVALID_INTEGER)
578 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s is not a valid integer.", Result.GetString(Index: Result.NumArguments() - 1));
579 else if(Error == PARSEARGS_INVALID_COLOR)
580 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s is not a valid color.", Result.GetString(Index: Result.NumArguments() - 1));
581 else if(Error == PARSEARGS_INVALID_FLOAT)
582 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%s is not a valid decimal number.", Result.GetString(Index: Result.NumArguments() - 1));
583 else
584 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Invalid arguments. Usage: %s %s", pCommand->m_pName, pCommand->m_pParams);
585 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "chatresp", pStr: aBuf);
586 }
587 else if(m_StoreCommands && pCommand->m_Flags & CFGFLAG_STORE)
588 {
589 m_vExecutionQueue.emplace_back(args&: pCommand, args&: Result);
590 }
591 else
592 {
593 if(pCommand->m_Flags & CMDFLAG_TEST && !g_Config.m_SvTestingCommands)
594 {
595 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: "Test commands aren't allowed, enable them with 'sv_test_cmds 1' in your initial config.");
596 return;
597 }
598
599 if(m_pfnTeeHistorianCommandCallback && !(pCommand->m_Flags & CFGFLAG_NONTEEHISTORIC))
600 {
601 m_pfnTeeHistorianCommandCallback(ClientId, m_FlagMask, pCommand->m_pName, &Result, m_pTeeHistorianCommandUserdata);
602 }
603
604 if(Result.m_aSpecialVictim[0])
605 {
606 std::optional<std::vector<int>> Victims;
607 if(m_pfnGetVictimsCommandCallback)
608 {
609 Victims = m_pfnGetVictimsCommandCallback(ClientId, Result.m_aSpecialVictim, m_pGetVictimsCommandUserData);
610 }
611 else
612 {
613 Victims = std::nullopt;
614 }
615
616 if(!Victims.has_value())
617 {
618 log_error("console", "Invalid victim '%s'", Result.m_aSpecialVictim);
619 return;
620 }
621 for(const int VictimId : Victims.value())
622 {
623 Result.SetVictim(VictimId);
624 pCommand->m_pfnCallback(&Result, pCommand->m_pUserData);
625 }
626 }
627 else
628 {
629 pCommand->m_pfnCallback(&Result, pCommand->m_pUserData);
630 }
631
632 if(pCommand->m_Flags & CMDFLAG_TEST)
633 m_Cheated = true;
634 }
635 }
636 }
637 else if(Stroke)
638 {
639 char aBuf[CMDLINE_LENGTH + 32];
640 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "Access for command %s denied.", Result.m_pCommand);
641 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
642 }
643 }
644 else if(Stroke)
645 {
646 // Pass the original string to the unknown command callback instead of the parsed command, as the latter
647 // ends at the first whitespace, which breaks for unknown commands (filenames) containing spaces.
648 if(!m_pfnUnknownCommandCallback(pStr, m_pUnknownCommandUserdata))
649 {
650 char aBuf[CMDLINE_LENGTH + 32];
651 if(m_FlagMask & CFGFLAG_CHAT)
652 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "No such command: %s. Use /cmdlist for a list of all commands.", Result.m_pCommand);
653 else
654 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "No such command: %s.", Result.m_pCommand);
655 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "chatresp", pStr: aBuf);
656 }
657 }
658
659 pStr = pNextPart;
660 }
661}
662
663bool CConsole::CanUseCommand(int ClientId, const IConsole::ICommandInfo *pCommand) const
664{
665 // the fallback is needed for the client and rust tests
666 if(!m_pfnCanUseCommandCallback)
667 return true;
668 return m_pfnCanUseCommandCallback(ClientId, pCommand, m_pCanUseCommandUserData);
669}
670
671int CConsole::PossibleCommands(const char *pStr, int FlagMask, bool Temp, FPossibleCallback pfnCallback, void *pUser)
672{
673 int Index = 0;
674 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
675 {
676 if(pCommand->m_Flags & FlagMask && pCommand->m_Temp == Temp)
677 {
678 if(str_find_nocase(haystack: pCommand->m_pName, needle: pStr))
679 {
680 pfnCallback(Index, pCommand->m_pName, pUser);
681 Index++;
682 }
683 }
684 }
685 return Index;
686}
687
688CConsole::CCommand *CConsole::FindCommand(const char *pName, int FlagMask)
689{
690 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
691 {
692 if(pCommand->m_Flags & FlagMask)
693 {
694 if(str_comp_nocase(a: pCommand->m_pName, b: pName) == 0)
695 return pCommand;
696 }
697 }
698
699 return nullptr;
700}
701
702void CConsole::ExecuteLine(const char *pStr, int ClientId, bool InterpretSemicolons)
703{
704 CConsole::ExecuteLineStroked(Stroke: 1, pStr, ClientId, InterpretSemicolons); // press it
705 CConsole::ExecuteLineStroked(Stroke: 0, pStr, ClientId, InterpretSemicolons); // then release it
706}
707
708void CConsole::ExecuteLineFlag(const char *pStr, int FlagMask, int ClientId, bool InterpretSemicolons)
709{
710 int Temp = m_FlagMask;
711 m_FlagMask = FlagMask;
712 ExecuteLine(pStr, ClientId, InterpretSemicolons);
713 m_FlagMask = Temp;
714}
715
716bool CConsole::ExecuteFile(const char *pFilename, int ClientId, bool LogFailure, int StorageType)
717{
718 int Count = 0;
719 // make sure that this isn't being executed already and that recursion limit isn't met
720 for(CExecFile *pCur = m_pFirstExec; pCur; pCur = pCur->m_pPrev)
721 {
722 Count++;
723
724 if(str_comp(a: pFilename, b: pCur->m_pFilename) == 0 || Count > FILE_RECURSION_LIMIT)
725 return false;
726 }
727 if(!m_pStorage)
728 return false;
729
730 // push this one to the stack
731 CExecFile ThisFile;
732 CExecFile *pPrev = m_pFirstExec;
733 ThisFile.m_pFilename = pFilename;
734 ThisFile.m_pPrev = m_pFirstExec;
735 m_pFirstExec = &ThisFile;
736
737 // exec the file
738 CLineReader LineReader;
739 bool Success = false;
740 char aBuf[32 + IO_MAX_PATH_LENGTH];
741 if(LineReader.OpenFile(File: m_pStorage->OpenFile(pFilename, Flags: IOFLAG_READ, Type: StorageType)))
742 {
743 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "executing '%s'", pFilename);
744 Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
745
746 while(const char *pLine = LineReader.Get())
747 {
748 ExecuteLine(pStr: pLine, ClientId);
749 }
750
751 Success = true;
752 }
753 else if(LogFailure)
754 {
755 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "failed to open '%s'", pFilename);
756 Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
757 }
758
759 m_pFirstExec = pPrev;
760 return Success;
761}
762
763void CConsole::Con_Echo(IResult *pResult, void *pUserData)
764{
765 ((CConsole *)pUserData)->Print(Level: IConsole::OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: pResult->GetString(Index: 0));
766}
767
768void CConsole::Con_Exec(IResult *pResult, void *pUserData)
769{
770 ((CConsole *)pUserData)->ExecuteFile(pFilename: pResult->GetString(Index: 0), ClientId: pResult->m_ClientId, LogFailure: true, StorageType: IStorage::TYPE_ALL);
771}
772
773void CConsole::ConCommandAccess(IResult *pResult, void *pUser)
774{
775 CConsole *pConsole = static_cast<CConsole *>(pUser);
776 char aBuf[CMDLINE_LENGTH + 64];
777 CCommand *pCommand = pConsole->FindCommand(pName: pResult->GetString(Index: 0), FlagMask: CFGFLAG_SERVER);
778 if(pCommand)
779 {
780 if(pResult->NumArguments() == 2)
781 {
782 std::optional<EAccessLevel> AccessLevel = AccessLevelToEnum(pAccessLevel: pResult->GetString(Index: 1));
783 if(!AccessLevel.has_value())
784 {
785 log_error("console", "Invalid access level '%s'. Allowed values are admin, moderator, helper and all.", pResult->GetString(1));
786 return;
787 }
788 pCommand->SetAccessLevel(AccessLevel.value());
789 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "moderator access for '%s' is now %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::MODERATOR ? "enabled" : "disabled");
790 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
791 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "helper access for '%s' is now %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::HELPER ? "enabled" : "disabled");
792 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
793 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "user access for '%s' is now %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::USER ? "enabled" : "disabled");
794 }
795 else
796 {
797 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "moderator access for '%s' is %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::MODERATOR ? "enabled" : "disabled");
798 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
799 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "helper access for '%s' is %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::HELPER ? "enabled" : "disabled");
800 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
801 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "user access for '%s' is %s", pResult->GetString(Index: 0), pCommand->GetAccessLevel() >= EAccessLevel::USER ? "enabled" : "disabled");
802 }
803 }
804 else
805 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "No such command: '%s'.", pResult->GetString(Index: 0));
806
807 pConsole->Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "console", pStr: aBuf);
808}
809
810void CConsole::PrintCommandList(EAccessLevel MinAccessLevel, int ExcludeFlagMask)
811{
812 char aBuf[240] = "";
813 int Used = 0;
814
815 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
816 {
817 if((pCommand->m_Flags & m_FlagMask) &&
818 !(pCommand->m_Flags & ExcludeFlagMask) &&
819 pCommand->GetAccessLevel() >= MinAccessLevel)
820 {
821 int Length = str_length(str: pCommand->m_pName);
822 if(Used + Length + 2 < (int)(sizeof(aBuf)))
823 {
824 if(Used > 0)
825 {
826 Used += 2;
827 str_append(dst&: aBuf, src: ", ");
828 }
829 str_append(dst&: aBuf, src: pCommand->m_pName);
830 Used += Length;
831 }
832 else
833 {
834 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "chatresp", pStr: aBuf);
835 str_copy(dst&: aBuf, src: pCommand->m_pName);
836 Used = Length;
837 }
838 }
839 }
840 if(Used > 0)
841 Print(Level: OUTPUT_LEVEL_STANDARD, pFrom: "chatresp", pStr: aBuf);
842}
843
844void CConsole::ConCommandStatus(IResult *pResult, void *pUser)
845{
846 CConsole *pConsole = static_cast<CConsole *>(pUser);
847 std::optional<EAccessLevel> AccessLevel = AccessLevelToEnum(pAccessLevel: pResult->GetString(Index: 0));
848 if(!AccessLevel.has_value())
849 {
850 log_error("console", "Invalid access level '%s'. Allowed values are admin, moderator, helper and all.", pResult->GetString(0));
851 return;
852 }
853 pConsole->PrintCommandList(MinAccessLevel: AccessLevel.value(), ExcludeFlagMask: 0);
854}
855
856void CConsole::ConUserCommandStatus(IResult *pResult, void *pUser)
857{
858 CConsole *pConsole = static_cast<CConsole *>(pUser);
859 pConsole->PrintCommandList(MinAccessLevel: EAccessLevel::USER, ExcludeFlagMask: CMDFLAG_PRACTICE);
860}
861
862void CConsole::TraverseChain(FCommandCallback *ppfnCallback, void **ppUserData)
863{
864 while(*ppfnCallback == Con_Chain)
865 {
866 CChain *pChainInfo = static_cast<CChain *>(*ppUserData);
867 *ppfnCallback = pChainInfo->m_pfnCallback;
868 *ppUserData = pChainInfo->m_pCallbackUserData;
869 }
870}
871
872CConsole::CConsole(int FlagMask)
873{
874 m_FlagMask = FlagMask;
875 m_pRecycleList = nullptr;
876 m_TempCommands.Reset();
877 m_StoreCommands = true;
878 m_apStrokeStr[0] = "0";
879 m_apStrokeStr[1] = "1";
880 m_pFirstCommand = nullptr;
881 m_pFirstExec = nullptr;
882 m_pfnTeeHistorianCommandCallback = nullptr;
883 m_pTeeHistorianCommandUserdata = nullptr;
884 m_pfnGetVictimsCommandCallback = nullptr;
885 m_pGetVictimsCommandUserData = nullptr;
886
887 m_pStorage = nullptr;
888
889 // register some basic commands
890 Register(pName: "echo", pParams: "r[text]", Flags: CFGFLAG_SERVER, pfnFunc: Con_Echo, pUser: this, pHelp: "Echo the text");
891 Register(pName: "exec", pParams: "r[file]", Flags: CFGFLAG_SERVER | CFGFLAG_CLIENT, pfnFunc: Con_Exec, pUser: this, pHelp: "Execute the specified file");
892
893 Register(pName: "access_level", pParams: "s[command] ?s['admin'|'moderator'|'helper'|'all']", Flags: CFGFLAG_SERVER, pfnFunc: ConCommandAccess, pUser: this, pHelp: "Specify command accessibility for given access level");
894 Register(pName: "access_status", pParams: "s['admin'|'moderator'|'helper'|'all']", Flags: CFGFLAG_SERVER, pfnFunc: ConCommandStatus, pUser: this, pHelp: "List all commands which are accessible for given access level");
895 Register(pName: "cmdlist", pParams: "", Flags: CFGFLAG_SERVER | CFGFLAG_CHAT, pfnFunc: ConUserCommandStatus, pUser: this, pHelp: "List all commands which are accessible for users");
896
897 // DDRace
898
899 m_Cheated = false;
900}
901
902CConsole::~CConsole()
903{
904 CCommand *pCommand = m_pFirstCommand;
905 while(pCommand)
906 {
907 CCommand *pNext = pCommand->Next();
908 {
909 FCommandCallback pfnCallback = pCommand->m_pfnCallback;
910 void *pUserData = pCommand->m_pUserData;
911 CChain *pChain = nullptr;
912 while(pfnCallback == Con_Chain)
913 {
914 pChain = static_cast<CChain *>(pUserData);
915 pfnCallback = pChain->m_pfnCallback;
916 pUserData = pChain->m_pCallbackUserData;
917 delete pChain;
918 }
919 }
920 // Temp commands are on m_TempCommands heap, so don't delete them
921 if(!pCommand->m_Temp)
922 delete pCommand;
923 pCommand = pNext;
924 }
925}
926
927void CConsole::Init()
928{
929 m_pStorage = Kernel()->RequestInterface<IStorage>();
930}
931
932void CConsole::ParseArguments(int NumArgs, const char **ppArguments)
933{
934 for(int i = 0; i < NumArgs; i++)
935 {
936 // check for scripts to execute
937 if(ppArguments[i][0] == '-' && ppArguments[i][1] == 'f' && ppArguments[i][2] == 0)
938 {
939 if(NumArgs - i > 1)
940 ExecuteFile(pFilename: ppArguments[i + 1], ClientId: IConsole::CLIENT_ID_UNSPECIFIED, LogFailure: true, StorageType: IStorage::TYPE_ABSOLUTE);
941 i++;
942 }
943 else if(!str_comp(a: "-s", b: ppArguments[i]) || !str_comp(a: "--silent", b: ppArguments[i]))
944 {
945 // skip silent param
946 continue;
947 }
948 else
949 {
950 // search arguments for overrides
951 ExecuteLine(pStr: ppArguments[i]);
952 }
953 }
954}
955
956void CConsole::AddCommandSorted(CCommand *pCommand)
957{
958 if(!m_pFirstCommand || str_comp(a: pCommand->m_pName, b: m_pFirstCommand->m_pName) <= 0)
959 {
960 if(m_pFirstCommand && m_pFirstCommand->Next())
961 pCommand->SetNext(m_pFirstCommand);
962 else
963 pCommand->SetNext(nullptr);
964 m_pFirstCommand = pCommand;
965 }
966 else
967 {
968 for(CCommand *p = m_pFirstCommand; p; p = p->Next())
969 {
970 if(!p->Next() || str_comp(a: pCommand->m_pName, b: p->Next()->m_pName) <= 0)
971 {
972 pCommand->SetNext(p->Next());
973 p->SetNext(pCommand);
974 break;
975 }
976 }
977 }
978}
979
980void CConsole::Register(const char *pName, const char *pParams,
981 int Flags, FCommandCallback pfnFunc, void *pUser, const char *pHelp)
982{
983 CCommand *pCommand = FindCommand(pName, FlagMask: Flags);
984 bool DoAdd = false;
985 if(pCommand == nullptr)
986 {
987 pCommand = new CCommand();
988 DoAdd = true;
989 }
990 pCommand->m_pfnCallback = pfnFunc;
991 pCommand->m_pUserData = pUser;
992
993 pCommand->m_pName = pName;
994 pCommand->m_pHelp = pHelp;
995 pCommand->m_pParams = pParams;
996
997 pCommand->m_Flags = Flags;
998 pCommand->m_Temp = false;
999
1000 if(DoAdd)
1001 AddCommandSorted(pCommand);
1002
1003 if(pCommand->m_Flags & CFGFLAG_CHAT)
1004 pCommand->SetAccessLevel(EAccessLevel::USER);
1005}
1006
1007void CConsole::RegisterTemp(const char *pName, const char *pParams, int Flags, const char *pHelp)
1008{
1009 CCommand *pCommand;
1010 if(m_pRecycleList)
1011 {
1012 pCommand = m_pRecycleList;
1013 str_copy(dst: const_cast<char *>(pCommand->m_pName), src: pName, dst_size: TEMPCMD_NAME_LENGTH);
1014 str_copy(dst: const_cast<char *>(pCommand->m_pHelp), src: pHelp, dst_size: TEMPCMD_HELP_LENGTH);
1015 str_copy(dst: const_cast<char *>(pCommand->m_pParams), src: pParams, dst_size: TEMPCMD_PARAMS_LENGTH);
1016
1017 m_pRecycleList = m_pRecycleList->Next();
1018 }
1019 else
1020 {
1021 pCommand = new(m_TempCommands.Allocate(Size: sizeof(CCommand))) CCommand;
1022 char *pMem = static_cast<char *>(m_TempCommands.Allocate(Size: TEMPCMD_NAME_LENGTH));
1023 str_copy(dst: pMem, src: pName, dst_size: TEMPCMD_NAME_LENGTH);
1024 pCommand->m_pName = pMem;
1025 pMem = static_cast<char *>(m_TempCommands.Allocate(Size: TEMPCMD_HELP_LENGTH));
1026 str_copy(dst: pMem, src: pHelp, dst_size: TEMPCMD_HELP_LENGTH);
1027 pCommand->m_pHelp = pMem;
1028 pMem = static_cast<char *>(m_TempCommands.Allocate(Size: TEMPCMD_PARAMS_LENGTH));
1029 str_copy(dst: pMem, src: pParams, dst_size: TEMPCMD_PARAMS_LENGTH);
1030 pCommand->m_pParams = pMem;
1031 }
1032
1033 pCommand->m_pfnCallback = nullptr;
1034 pCommand->m_pUserData = nullptr;
1035 pCommand->m_Flags = Flags;
1036 pCommand->m_Temp = true;
1037
1038 AddCommandSorted(pCommand);
1039}
1040
1041void CConsole::DeregisterTemp(const char *pName)
1042{
1043 if(!m_pFirstCommand)
1044 return;
1045
1046 CCommand *pRemoved = nullptr;
1047
1048 // remove temp entry from command list
1049 if(m_pFirstCommand->m_Temp && str_comp(a: m_pFirstCommand->m_pName, b: pName) == 0)
1050 {
1051 pRemoved = m_pFirstCommand;
1052 m_pFirstCommand = m_pFirstCommand->Next();
1053 }
1054 else
1055 {
1056 for(CCommand *pCommand = m_pFirstCommand; pCommand->Next(); pCommand = pCommand->Next())
1057 if(pCommand->Next()->m_Temp && str_comp(a: pCommand->Next()->m_pName, b: pName) == 0)
1058 {
1059 pRemoved = pCommand->Next();
1060 pCommand->SetNext(pCommand->Next()->Next());
1061 break;
1062 }
1063 }
1064
1065 // add to recycle list
1066 if(pRemoved)
1067 {
1068 pRemoved->SetNext(m_pRecycleList);
1069 m_pRecycleList = pRemoved;
1070 }
1071}
1072
1073void CConsole::DeregisterTempAll()
1074{
1075 // set non temp as first one
1076 for(; m_pFirstCommand && m_pFirstCommand->m_Temp; m_pFirstCommand = m_pFirstCommand->Next())
1077 ;
1078
1079 // remove temp entries from command list
1080 for(CCommand *pCommand = m_pFirstCommand; pCommand && pCommand->Next(); pCommand = pCommand->Next())
1081 {
1082 CCommand *pNext = pCommand->Next();
1083 if(pNext->m_Temp)
1084 {
1085 for(; pNext && pNext->m_Temp; pNext = pNext->Next())
1086 ;
1087 pCommand->SetNext(pNext);
1088 }
1089 }
1090
1091 m_TempCommands.Reset();
1092 m_pRecycleList = nullptr;
1093}
1094
1095void CConsole::Con_Chain(IResult *pResult, void *pUserData)
1096{
1097 CChain *pInfo = (CChain *)pUserData;
1098 pInfo->m_pfnChainCallback(pResult, pInfo->m_pUserData, pInfo->m_pfnCallback, pInfo->m_pCallbackUserData);
1099}
1100
1101void CConsole::Chain(const char *pName, FChainCommandCallback pfnChainFunc, void *pUser)
1102{
1103 CCommand *pCommand = FindCommand(pName, FlagMask: m_FlagMask);
1104
1105 if(!pCommand)
1106 {
1107 char aBuf[256];
1108 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "failed to chain '%s'", pName);
1109 Print(Level: IConsole::OUTPUT_LEVEL_DEBUG, pFrom: "console", pStr: aBuf);
1110 return;
1111 }
1112
1113 CChain *pChainInfo = new CChain();
1114
1115 // store info
1116 pChainInfo->m_pfnChainCallback = pfnChainFunc;
1117 pChainInfo->m_pUserData = pUser;
1118 pChainInfo->m_pfnCallback = pCommand->m_pfnCallback;
1119 pChainInfo->m_pCallbackUserData = pCommand->m_pUserData;
1120
1121 // chain
1122 pCommand->m_pfnCallback = Con_Chain;
1123 pCommand->m_pUserData = pChainInfo;
1124}
1125
1126void CConsole::StoreCommands(bool Store)
1127{
1128 if(!Store)
1129 {
1130 for(CExecutionQueueEntry &Entry : m_vExecutionQueue)
1131 {
1132 Entry.m_pCommand->m_pfnCallback(&Entry.m_Result, Entry.m_pCommand->m_pUserData);
1133 }
1134 m_vExecutionQueue.clear();
1135 }
1136 m_StoreCommands = Store;
1137}
1138
1139const IConsole::ICommandInfo *CConsole::GetCommandInfo(const char *pName, int FlagMask, bool Temp)
1140{
1141 for(CCommand *pCommand = m_pFirstCommand; pCommand; pCommand = pCommand->Next())
1142 {
1143 if(pCommand->m_Flags & FlagMask && pCommand->m_Temp == Temp)
1144 {
1145 if(str_comp_nocase(a: pCommand->Name(), b: pName) == 0)
1146 return pCommand;
1147 }
1148 }
1149
1150 return nullptr;
1151}
1152
1153std::unique_ptr<IConsole> CreateConsole(int FlagMask) { return std::make_unique<CConsole>(args&: FlagMask); }
1154
1155int CConsole::CResult::GetVictim() const
1156{
1157 dbg_assert(m_VictimId.has_value(), "m_VictimId has no value");
1158 return m_VictimId.value();
1159}
1160
1161void CConsole::CResult::ResetVictim()
1162{
1163 m_VictimId = std::nullopt;
1164 m_aSpecialVictim[0] = '\0';
1165}
1166
1167void CConsole::CResult::SetVictim(int Victim)
1168{
1169 dbg_assert(in_range(Victim, 0, MAX_CLIENTS - 1), "Victim ID %d out of range [0, %d]", Victim, MAX_CLIENTS - 1);
1170 m_VictimId = Victim;
1171}
1172
1173void CConsole::CResult::SetVictim(const char *pVictim)
1174{
1175 int Value;
1176 if(!str_toint(str: pVictim, out: &Value) || !in_range(a: Value, lower: 0, upper: MAX_CLIENTS - 1))
1177 {
1178 str_copy(dst&: m_aSpecialVictim, src: pVictim);
1179 return;
1180 }
1181
1182 SetVictim(Value);
1183}
1184
1185std::optional<ColorHSLA> CConsole::ColorParse(const char *pStr, float DarkestLighting)
1186{
1187 if(str_isallnum(str: pStr) || ((pStr[0] == '-' || pStr[0] == '+') && str_isallnum(str: pStr + 1))) // Teeworlds Color (Packed HSL)
1188 {
1189 unsigned long Value = str_toulong_base(str: pStr, base: 10);
1190 if(Value == std::numeric_limits<unsigned long>::max())
1191 return std::nullopt;
1192 return ColorHSLA(Value, true).UnclampLighting(Darkest: DarkestLighting);
1193 }
1194 else if(*pStr == '$') // Hex RGB/RGBA
1195 {
1196 auto ParsedColor = color_parse<ColorRGBA>(pStr: pStr + 1);
1197 if(ParsedColor)
1198 return color_cast<ColorHSLA>(rgb: ParsedColor.value());
1199 else
1200 return std::nullopt;
1201 }
1202 else if(!str_comp_nocase(a: pStr, b: "red"))
1203 return ColorHSLA(0.0f / 6.0f, 1.0f, 0.5f);
1204 else if(!str_comp_nocase(a: pStr, b: "yellow"))
1205 return ColorHSLA(1.0f / 6.0f, 1.0f, 0.5f);
1206 else if(!str_comp_nocase(a: pStr, b: "green"))
1207 return ColorHSLA(2.0f / 6.0f, 1.0f, 0.5f);
1208 else if(!str_comp_nocase(a: pStr, b: "cyan"))
1209 return ColorHSLA(3.0f / 6.0f, 1.0f, 0.5f);
1210 else if(!str_comp_nocase(a: pStr, b: "blue"))
1211 return ColorHSLA(4.0f / 6.0f, 1.0f, 0.5f);
1212 else if(!str_comp_nocase(a: pStr, b: "magenta"))
1213 return ColorHSLA(5.0f / 6.0f, 1.0f, 0.5f);
1214 else if(!str_comp_nocase(a: pStr, b: "white"))
1215 return ColorHSLA(0.0f, 0.0f, 1.0f);
1216 else if(!str_comp_nocase(a: pStr, b: "gray"))
1217 return ColorHSLA(0.0f, 0.0f, 0.5f);
1218 else if(!str_comp_nocase(a: pStr, b: "black"))
1219 return ColorHSLA(0.0f, 0.0f, 0.0f);
1220
1221 return std::nullopt;
1222}
1223