feat(Core/Chat): new argument parsing and unify chat hyperlink parsing (#6243)

This commit is contained in:
Kargatum
2021-10-23 15:15:42 +07:00
committed by GitHub
parent 1101f9dd2a
commit bc9473482e
90 changed files with 4280 additions and 2508 deletions

View File

@@ -33,7 +33,7 @@ public:
~SOAPCommand() { }
void appendToPrintBuffer(char const* msg)
void appendToPrintBuffer(std::string_view msg)
{
m_printBuffer += msg;
}
@@ -49,7 +49,7 @@ public:
return m_success;
}
static void print(void* callbackArg, char const* msg)
static void print(void* callbackArg, std::string_view msg)
{
((SOAPCommand*)callbackArg)->appendToPrintBuffer(msg);
}

View File

@@ -19,19 +19,22 @@
/// @{
/// \file
#include "AccountMgr.h"
#include "Chat.h"
#include "CliRunnable.h"
#include "Common.h"
#include "Config.h"
#include "Language.h"
#include "Log.h"
#include "MapMgr.h"
#include "Errors.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "Util.h"
#include "World.h"
#include "WorldSession.h"
#include "Config.h"
#include "CliRunnable.h"
#include "Log.h"
#include "Util.h"
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
#include "Chat.h"
#include "ChatCommand.h"
#include <cstring>
#include <readline/readline.h>
#include <readline/history.h>
#endif
static constexpr char CLI_PREFIX[] = "AC> ";
@@ -41,75 +44,49 @@ static inline void PrintCliPrefix()
}
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
#include <readline/readline.h>
#include <readline/history.h>
char* command_finder(const char* text, int state)
namespace Acore::Impl::Readline
{
static size_t idx, len;
const char* ret;
std::vector<ChatCommand> const& cmd = ChatHandler::getCommandTable();
if (!state)
static std::vector<std::string> vec;
char* cli_unpack_vector(char const*, int state)
{
idx = 0;
len = strlen(text);
static size_t i=0;
if (!state)
i = 0;
if (i < vec.size())
return strdup(vec[i++].c_str());
else
return nullptr;
}
while (idx < cmd.size())
char** cli_completion(char const* text, int /*start*/, int /*end*/)
{
ret = cmd[idx].Name;
if (!cmd[idx].AllowConsole)
{
++idx;
continue;
}
++idx;
//printf("Checking %s \n", cmd[idx].Name);
if (strncmp(ret, text, len) == 0)
return strdup(ret);
::rl_attempted_completion_over = 1;
vec = Acore::ChatCommands::GetAutoCompletionsFor(CliHandler(nullptr,nullptr), text);
return ::rl_completion_matches(text, &cli_unpack_vector);
}
return ((char*)nullptr);
int cli_hook_func()
{
if (World::IsStopped())
::rl_done = 1;
return 0;
}
}
char** cli_completion(const char* text, int start, int /*end*/)
{
char** matches = nullptr;
if (start)
rl_bind_key('\t', rl_abort);
else
matches = rl_completion_matches((char*)text, &command_finder);
return matches;
}
int cli_hook_func()
{
if (World::IsStopped())
rl_done = 1;
return 0;
}
#endif
void utf8print(void* /*arg*/, const char* str)
void utf8print(void* /*arg*/, std::string_view str)
{
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
wchar_t wtemp_buf[6000];
size_t wtemp_len = 6000 - 1;
if (!Utf8toWStr(str, strlen(str), wtemp_buf, wtemp_len))
std::wstring wbuf;
if (!Utf8toWStr(str, wbuf))
return;
char temp_buf[6000];
CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len + 1);
printf(temp_buf);
wprintf(L"%s", wbuf.c_str());
#else
{
printf("%s", str);
fflush(stdout);
}
{
printf(STRING_VIEW_FMT, STRING_VIEW_FMT_ARG(str));
fflush(stdout);
}
#endif
}
@@ -129,7 +106,7 @@ int kb_hit_return()
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv);
select(STDIN_FILENO+1, &fds, nullptr, nullptr, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
#endif
@@ -137,15 +114,21 @@ int kb_hit_return()
/// %Thread start
void CliThread()
{
///- Display the list of available CLI functions then beep
//TC_LOG_INFO("server.worldserver", "");
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
rl_attempted_completion_function = cli_completion;
rl_event_hook = cli_hook_func;
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
// print this here the first time
// later it will be printed after command queue updates
PrintCliPrefix();
#else
::rl_attempted_completion_function = &Acore::Impl::Readline::cli_completion;
{
static char BLANK = '\0';
::rl_completer_word_break_characters = &BLANK;
}
::rl_event_hook = &Acore::Impl::Readline::cli_hook_func;
#endif
if (sConfigMgr->GetOption<bool>("BeepAtStart", true))
printf("\a"); // \a = Alert
printf("\a"); // \a = Alert
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
if (sConfigMgr->GetOption<bool>("FlashAtStart", true))
@@ -160,60 +143,53 @@ void CliThread()
}
#endif
// print this here the first time
// later it will be printed after command queue updates
PrintCliPrefix();
///- As long as the World is running (no World::m_stopEvent), get the command line and handle it
while (!World::IsStopped())
{
fflush(stdout);
char* command_str ; // = fgets(commandbuf, sizeof(commandbuf), stdin);
std::string command;
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
char commandbuf[256];
command_str = fgets(commandbuf, sizeof(commandbuf), stdin);
#else
command_str = readline(CLI_PREFIX);
rl_bind_key('\t', rl_complete);
#endif
if (command_str != nullptr)
wchar_t commandbuf[256];
if (fgetws(commandbuf, sizeof(commandbuf), stdin))
{
for (int x = 0; command_str[x]; ++x)
if (command_str[x] == '\r' || command_str[x] == '\n')
{
command_str[x] = 0;
break;
}
if (!*command_str)
if (!WStrToUtf8(commandbuf, wcslen(commandbuf), command))
{
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
PrintCliPrefix();
#else
free(command_str);
#endif
continue;
}
std::string command;
if (!consoleToUtf8(command_str, command)) // convert from console encoding to utf8
{
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
PrintCliPrefix();
}
#else
free(command_str);
char* command_str = readline(CLI_PREFIX);
::rl_bind_key('\t', ::rl_complete);
if (command_str != nullptr)
{
command = command_str;
free(command_str);
}
#endif
continue;
if (!command.empty())
{
std::size_t nextLineIndex = command.find_first_of("\r\n");
if (nextLineIndex != std::string::npos)
{
if (nextLineIndex == 0)
{
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
PrintCliPrefix();
#endif
continue;
}
command.erase(nextLineIndex);
}
fflush(stdout);
sWorld->QueueCliCommand(new CliCommandHolder(nullptr, command.c_str(), &utf8print, &commandFinished));
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
add_history(command.c_str());
free(command_str);
#endif
}
else if (feof(stdin))

View File

@@ -91,7 +91,7 @@ void RASession::Start()
_socket.close();
}
int RASession::Send(const char* data)
int RASession::Send(std::string_view data)
{
std::ostream os(&_writeBuffer);
os << data;
@@ -206,9 +206,9 @@ bool RASession::ProcessCommand(std::string& command)
return false;
}
void RASession::CommandPrint(void* callbackArg, const char* text)
void RASession::CommandPrint(void* callbackArg, std::string_view text)
{
if (!text || !*text)
if (text.empty())
{
return;
}

View File

@@ -40,13 +40,13 @@ public:
unsigned short GetRemotePort() const { return _socket.remote_endpoint().port(); }
private:
int Send(const char* data);
int Send(std::string_view data);
std::string ReadString();
bool CheckAccessLevel(const std::string& user);
bool CheckPassword(const std::string& user, const std::string& pass);
bool ProcessCommand(std::string& command);
static void CommandPrint(void* callbackArg, const char* text);
static void CommandPrint(void* callbackArg, std::string_view text);
static void CommandFinished(void* callbackArg, bool);
tcp::socket _socket;

View File

@@ -1788,30 +1788,34 @@ NpcRegenHPTimeIfTargetIsUnreachable = 10
# Collapses multiple subsequent whitespaces into a single whitespace.
# Not applied to the addon language, but may break old addons that use
# "normal" chat messages for sending data to other clients.
# Default: 0 - (Disabled)
# 1 - (Enabled)
# Default: 1 - (Enabled, Blizzlike)
# 0 - (Disabled)
#
ChatFakeMessagePreventing = 0
ChatFakeMessagePreventing = 1
#
# ChatStrictLinkChecking.Severity
# Description: Check chat messages for in-game links to spells, items, quests, etc.
# Default: 0 - (Disabled)
# 1 - (Enabled, Check if only valid pipe commands are used, Prevents posting
# pictures.)
# 2 - (Enabled, Verify that pipe commands are used in a correct order)
# 3 - (Check if color, entry and name don't contradict each other. For this to
# work correctly, please assure that you have extracted locale DBCs of
# every language specific client playing on this server)
# -1 - (Only verify validity of link data, but permit use of custom colors)
# Default: 0 - (Only verify that link data and color are valid without checking text)
# 1 - (Additionally verifies that the link text matches the provided data)
#
# Note: If this is set to '1', you must additionally provide .dbc files for all
# client locales that are in use on your server.
# If any files are missing, messages with links from clients using those
# locales will likely be blocked by the server.
#
ChatStrictLinkChecking.Severity = 0
#
# ChatStrictLinkChecking.Kick
# Description: Defines what should be done if a message is considered to contain invalid
# pipe commands.
# Description: Defines what should be done if a message containing invalid control characters
# is received.
# Default: 0 - (Silently ignore message)
# 1 - (Disconnect players who sent malformed messages)
# 1 - (Ignore message and kick player)
#
ChatStrictLinkChecking.Kick = 0