Merge pull request #652 from DarkflameUniverse/blacklist-and-chat-changes

Blacklist and chat changes
This commit is contained in:
Jett 2022-07-19 10:40:44 +01:00 committed by GitHub
commit 8e5da2cf1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 147 additions and 63 deletions

View File

@ -15,10 +15,10 @@ foreach(variable ${variables})
# If the string contains a #, skip it # If the string contains a #, skip it
if(NOT "${variable}" MATCHES "#") if(NOT "${variable}" MATCHES "#")
# Split the variable into name and value # Split the variable into name and value
string(REPLACE "=" ";" variable ${variable}) string(REPLACE "=" ";" variable ${variable})
# Check that the length of the variable is 2 (name and value) # Check that the length of the variable is 2 (name and value)
list(LENGTH variable length) list(LENGTH variable length)
if(${length} EQUAL 2) if(${length} EQUAL 2)
@ -83,14 +83,15 @@ make_directory(${CMAKE_BINARY_DIR}/locale)
# Create a /logs directory # Create a /logs directory
make_directory(${CMAKE_BINARY_DIR}/logs) make_directory(${CMAKE_BINARY_DIR}/logs)
# Copy ini files on first build # Copy resource files on first build
set(INI_FILES "authconfig.ini" "chatconfig.ini" "worldconfig.ini" "masterconfig.ini") set(RESOURCE_FILES "authconfig.ini" "chatconfig.ini" "worldconfig.ini" "masterconfig.ini" "blacklist.dcf")
foreach(ini ${INI_FILES}) foreach(resource_file ${RESOURCE_FILES})
if (NOT EXISTS ${PROJECT_BINARY_DIR}/${ini}) if (NOT EXISTS ${PROJECT_BINARY_DIR}/${resource_file})
configure_file( configure_file(
${CMAKE_SOURCE_DIR}/resources/${ini} ${PROJECT_BINARY_DIR}/${ini} ${CMAKE_SOURCE_DIR}/resources/${resource_file} ${PROJECT_BINARY_DIR}/${resource_file}
COPYONLY COPYONLY
) )
message("Moved ${resource_file} to project binary directory")
endif() endif()
endforeach() endforeach()

View File

@ -8,8 +8,9 @@
#include <regex> #include <regex>
#include "dCommonVars.h" #include "dCommonVars.h"
#include "Database.h"
#include "dLogger.h" #include "dLogger.h"
#include "dConfig.h"
#include "Database.h"
#include "Game.h" #include "Game.h"
using namespace dChatFilterDCF; using namespace dChatFilterDCF;
@ -18,12 +19,16 @@ dChatFilter::dChatFilter(const std::string& filepath, bool dontGenerateDCF) {
m_DontGenerateDCF = dontGenerateDCF; m_DontGenerateDCF = dontGenerateDCF;
if (!BinaryIO::DoesFileExist(filepath + ".dcf") || m_DontGenerateDCF) { if (!BinaryIO::DoesFileExist(filepath + ".dcf") || m_DontGenerateDCF) {
ReadWordlistPlaintext(filepath + ".txt"); ReadWordlistPlaintext(filepath + ".txt", true);
if (!m_DontGenerateDCF) ExportWordlistToDCF(filepath + ".dcf"); if (!m_DontGenerateDCF) ExportWordlistToDCF(filepath + ".dcf", true);
} }
else if (!ReadWordlistDCF(filepath + ".dcf")) { else if (!ReadWordlistDCF(filepath + ".dcf", true)) {
ReadWordlistPlaintext(filepath + ".txt"); ReadWordlistPlaintext(filepath + ".txt", true);
ExportWordlistToDCF(filepath + ".dcf"); ExportWordlistToDCF(filepath + ".dcf", true);
}
if (BinaryIO::DoesFileExist("blacklist.dcf")) {
ReadWordlistDCF("blacklist.dcf", false);
} }
//Read player names that are ok as well: //Read player names that are ok as well:
@ -32,29 +37,31 @@ dChatFilter::dChatFilter(const std::string& filepath, bool dontGenerateDCF) {
while (res->next()) { while (res->next()) {
std::string line = res->getString(1).c_str(); std::string line = res->getString(1).c_str();
std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase
m_Words.push_back(CalculateHash(line)); m_ApprovedWords.push_back(CalculateHash(line));
} }
delete res; delete res;
delete stmt; delete stmt;
} }
dChatFilter::~dChatFilter() { dChatFilter::~dChatFilter() {
m_Words.clear(); m_ApprovedWords.clear();
m_DeniedWords.clear();
} }
void dChatFilter::ReadWordlistPlaintext(const std::string& filepath) { void dChatFilter::ReadWordlistPlaintext(const std::string& filepath, bool whiteList) {
std::ifstream file(filepath); std::ifstream file(filepath);
if (file) { if (file) {
std::string line; std::string line;
while (std::getline(file, line)) { while (std::getline(file, line)) {
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end()); line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase
m_Words.push_back(CalculateHash(line)); if (whiteList) m_ApprovedWords.push_back(CalculateHash(line));
else m_DeniedWords.push_back(CalculateHash(line));
} }
} }
} }
bool dChatFilter::ReadWordlistDCF(const std::string& filepath) { bool dChatFilter::ReadWordlistDCF(const std::string& filepath, bool whiteList) {
std::ifstream file(filepath, std::ios::binary); std::ifstream file(filepath, std::ios::binary);
if (file) { if (file) {
fileHeader hdr; fileHeader hdr;
@ -67,12 +74,14 @@ bool dChatFilter::ReadWordlistDCF(const std::string& filepath) {
if (hdr.formatVersion == formatVersion) { if (hdr.formatVersion == formatVersion) {
size_t wordsToRead = 0; size_t wordsToRead = 0;
BinaryIO::BinaryRead(file, wordsToRead); BinaryIO::BinaryRead(file, wordsToRead);
m_Words.reserve(wordsToRead); if (whiteList) m_ApprovedWords.reserve(wordsToRead);
else m_DeniedWords.reserve(wordsToRead);
size_t word = 0; size_t word = 0;
for (size_t i = 0; i < wordsToRead; ++i) { for (size_t i = 0; i < wordsToRead; ++i) {
BinaryIO::BinaryRead(file, word); BinaryIO::BinaryRead(file, word);
m_Words.push_back(word); if (whiteList) m_ApprovedWords.push_back(word);
else m_DeniedWords.push_back(word);
} }
return true; return true;
@ -86,14 +95,14 @@ bool dChatFilter::ReadWordlistDCF(const std::string& filepath) {
return false; return false;
} }
void dChatFilter::ExportWordlistToDCF(const std::string& filepath) { void dChatFilter::ExportWordlistToDCF(const std::string& filepath, bool whiteList) {
std::ofstream file(filepath, std::ios::binary | std::ios_base::out); std::ofstream file(filepath, std::ios::binary | std::ios_base::out);
if (file) { if (file) {
BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::header)); BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::header));
BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::formatVersion)); BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::formatVersion));
BinaryIO::BinaryWrite(file, size_t(m_Words.size())); BinaryIO::BinaryWrite(file, size_t(whiteList ? m_ApprovedWords.size() : m_DeniedWords.size()));
for (size_t word : m_Words) { for (size_t word : whiteList ? m_ApprovedWords : m_DeniedWords) {
BinaryIO::BinaryWrite(file, word); BinaryIO::BinaryWrite(file, word);
} }
@ -101,31 +110,45 @@ void dChatFilter::ExportWordlistToDCF(const std::string& filepath) {
} }
} }
bool dChatFilter::IsSentenceOkay(const std::string& message, int gmLevel) { std::vector<std::pair<uint8_t, uint8_t>> dChatFilter::IsSentenceOkay(const std::string& message, int gmLevel, bool whiteList) {
if (gmLevel > GAME_MASTER_LEVEL_FORUM_MODERATOR) return true; //If anything but a forum mod, return true. if (gmLevel > GAME_MASTER_LEVEL_FORUM_MODERATOR) return { }; //If anything but a forum mod, return true.
if (message.empty()) return true; if (message.empty()) return { };
if (!whiteList && m_DeniedWords.empty()) return { { 0, message.length() } };
std::stringstream sMessage(message); std::stringstream sMessage(message);
std::string segment; std::string segment;
std::regex reg("(!*|\\?*|\\;*|\\.*|\\,*)"); std::regex reg("(!*|\\?*|\\;*|\\.*|\\,*)");
std::vector<std::pair<uint8_t, uint8_t>> listOfBadSegments = std::vector<std::pair<uint8_t, uint8_t>>();
uint32_t position = 0;
while (std::getline(sMessage, segment, ' ')) { while (std::getline(sMessage, segment, ' ')) {
std::string originalSegment = segment;
std::transform(segment.begin(), segment.end(), segment.begin(), ::tolower); //Transform to lowercase std::transform(segment.begin(), segment.end(), segment.begin(), ::tolower); //Transform to lowercase
segment = std::regex_replace(segment, reg, ""); segment = std::regex_replace(segment, reg, "");
size_t hash = CalculateHash(segment); size_t hash = CalculateHash(segment);
if (std::find(m_UserUnapprovedWordCache.begin(), m_UserUnapprovedWordCache.end(), hash) != m_UserUnapprovedWordCache.end()) { if (std::find(m_UserUnapprovedWordCache.begin(), m_UserUnapprovedWordCache.end(), hash) != m_UserUnapprovedWordCache.end() && whiteList) {
return false; listOfBadSegments.emplace_back(position, originalSegment.length());
} }
if (!IsInWordlist(hash)) { if (std::find(m_ApprovedWords.begin(), m_ApprovedWords.end(), hash) == m_ApprovedWords.end() && whiteList) {
m_UserUnapprovedWordCache.push_back(hash); m_UserUnapprovedWordCache.push_back(hash);
return false; listOfBadSegments.emplace_back(position, originalSegment.length());
} }
if (std::find(m_DeniedWords.begin(), m_DeniedWords.end(), hash) != m_DeniedWords.end() && !whiteList) {
m_UserUnapprovedWordCache.push_back(hash);
listOfBadSegments.emplace_back(position, originalSegment.length());
}
position += segment.length() + 1;
} }
return true; return listOfBadSegments;
} }
size_t dChatFilter::CalculateHash(const std::string& word) { size_t dChatFilter::CalculateHash(const std::string& word) {
@ -135,7 +158,3 @@ size_t dChatFilter::CalculateHash(const std::string& word) {
return value; return value;
} }
bool dChatFilter::IsInWordlist(size_t word) {
return std::find(m_Words.begin(), m_Words.end(), word) != m_Words.end();
}

View File

@ -20,17 +20,17 @@ public:
dChatFilter(const std::string& filepath, bool dontGenerateDCF); dChatFilter(const std::string& filepath, bool dontGenerateDCF);
~dChatFilter(); ~dChatFilter();
void ReadWordlistPlaintext(const std::string & filepath); void ReadWordlistPlaintext(const std::string& filepath, bool whiteList);
bool ReadWordlistDCF(const std::string & filepath); bool ReadWordlistDCF(const std::string& filepath, bool whiteList);
void ExportWordlistToDCF(const std::string & filepath); void ExportWordlistToDCF(const std::string& filepath, bool whiteList);
bool IsSentenceOkay(const std::string& message, int gmLevel); std::vector<std::pair<uint8_t, uint8_t>> IsSentenceOkay(const std::string& message, int gmLevel, bool whiteList = true);
private: private:
bool m_DontGenerateDCF; bool m_DontGenerateDCF;
std::vector<size_t> m_Words; std::vector<size_t> m_DeniedWords;
std::vector<size_t> m_ApprovedWords;
std::vector<size_t> m_UserUnapprovedWordCache; std::vector<size_t> m_UserUnapprovedWordCache;
//Private functions: //Private functions:
size_t CalculateHash(const std::string& word); size_t CalculateHash(const std::string& word);
bool IsInWordlist(size_t word);
}; };

View File

@ -19,6 +19,8 @@ User::User(const SystemAddress& sysAddr, const std::string& username, const std:
m_Username = username; m_Username = username;
m_LoggedInCharID = 0; m_LoggedInCharID = 0;
m_IsBestFriendMap = std::unordered_map<std::string, bool>();
//HACK HACK HACK //HACK HACK HACK
//This needs to be re-enabled / updated whenever the mute stuff is moved to another table. //This needs to be re-enabled / updated whenever the mute stuff is moved to another table.
//This was only done because otherwise the website's account page dies and the website is waiting on a migration to wordpress. //This was only done because otherwise the website's account page dies and the website is waiting on a migration to wordpress.

View File

@ -42,6 +42,9 @@ public:
bool GetLastChatMessageApproved() { return m_LastChatMessageApproved; } bool GetLastChatMessageApproved() { return m_LastChatMessageApproved; }
void SetLastChatMessageApproved(bool approved) { m_LastChatMessageApproved = approved; } void SetLastChatMessageApproved(bool approved) { m_LastChatMessageApproved = approved; }
std::unordered_map<std::string, bool> GetIsBestFriendMap() { return m_IsBestFriendMap; }
void SetIsBestFriendMap(std::unordered_map<std::string, bool> mapToSet) { m_IsBestFriendMap = mapToSet; }
bool GetIsMuted() const; bool GetIsMuted() const;
time_t GetMuteExpire() const; time_t GetMuteExpire() const;
@ -63,6 +66,8 @@ private:
std::vector<Character*> m_Characters; std::vector<Character*> m_Characters;
LWOOBJID m_LoggedInCharID; LWOOBJID m_LoggedInCharID;
std::unordered_map<std::string, bool> m_IsBestFriendMap;
bool m_LastChatMessageApproved = false; bool m_LastChatMessageApproved = false;
int m_AmountOfTimesOutOfSync = 0; int m_AmountOfTimesOutOfSync = 0;
const int m_MaxDesyncAllowed = 12; const int m_MaxDesyncAllowed = 12;

View File

@ -1193,7 +1193,7 @@ void PetComponent::SetPetNameForModeration(const std::string& petName) {
int approved = 1; //default, in mod int approved = 1; //default, in mod
//Make sure that the name isn't already auto-approved: //Make sure that the name isn't already auto-approved:
if (Game::chatFilter->IsSentenceOkay(petName, 0)) { if (Game::chatFilter->IsSentenceOkay(petName, 0).empty()) {
approved = 2; //approved approved = 2; //approved
} }

View File

@ -30,6 +30,9 @@
#include "VehiclePhysicsComponent.h" #include "VehiclePhysicsComponent.h"
#include "dConfig.h" #include "dConfig.h"
#include "CharacterComponent.h" #include "CharacterComponent.h"
#include "Database.h"
void ClientPackets::HandleChatMessage(const SystemAddress& sysAddr, Packet* packet) { void ClientPackets::HandleChatMessage(const SystemAddress& sysAddr, Packet* packet) {
User* user = UserManager::Instance()->GetUser(sysAddr); User* user = UserManager::Instance()->GetUser(sysAddr);
@ -284,6 +287,12 @@ void ClientPackets::HandleChatModerationRequest(const SystemAddress& sysAddr, Pa
receiver.push_back(static_cast<uint8_t>(character)); receiver.push_back(static_cast<uint8_t>(character));
} }
if (!receiver.empty()) {
if (std::string(receiver.c_str(), 4) == "[GM]") { // Shift the string forward if we are speaking to a GM as the client appends "[GM]" if they are
receiver = std::string(receiver.c_str() + 4, receiver.size() - 4);
}
}
stream.Read(messageLength); stream.Read(messageLength);
for (uint32_t i = 0; i < messageLength; ++i) { for (uint32_t i = 0; i < messageLength; ++i) {
uint16_t character; uint16_t character;
@ -291,16 +300,61 @@ void ClientPackets::HandleChatModerationRequest(const SystemAddress& sysAddr, Pa
message.push_back(static_cast<uint8_t>(character)); message.push_back(static_cast<uint8_t>(character));
} }
std::unordered_map<char, char> unacceptedItems; bool isBestFriend = false;
bool bAllClean = Game::chatFilter->IsSentenceOkay(message, user->GetLastUsedChar()->GetGMLevel());
if (!bAllClean) { if (chatLevel == 1) {
unacceptedItems.insert(std::make_pair((char)0, (char)message.length())); // Private chat
LWOOBJID idOfReceiver = LWOOBJID_EMPTY;
{
sql::PreparedStatement* stmt = Database::CreatePreppedStmt("SELECT name FROM charinfo WHERE name = ?");
stmt->setString(1, receiver);
sql::ResultSet* res = stmt->executeQuery();
if (res->next()) {
idOfReceiver = res->getInt("id");
}
delete stmt;
delete res;
}
if (user->GetIsBestFriendMap().find(receiver) == user->GetIsBestFriendMap().end() && idOfReceiver != LWOOBJID_EMPTY) {
sql::PreparedStatement* stmt = Database::CreatePreppedStmt("SELECT * FROM friends WHERE (player_id = ? AND friend_id = ?) OR (player_id = ? AND friend_id = ?) LIMIT 1;");
stmt->setInt(1, entity->GetObjectID());
stmt->setInt(2, idOfReceiver);
stmt->setInt(3, idOfReceiver);
stmt->setInt(4, entity->GetObjectID());
sql::ResultSet* res = stmt->executeQuery();
if (res->next()) {
isBestFriend = res->getInt("best_friend") == 3;
}
if (isBestFriend) {
auto tmpBestFriendMap = user->GetIsBestFriendMap();
tmpBestFriendMap[receiver] = true;
user->SetIsBestFriendMap(tmpBestFriendMap);
}
delete res;
delete stmt;
}
else if (user->GetIsBestFriendMap().find(receiver) != user->GetIsBestFriendMap().end()) {
isBestFriend = true;
}
} }
std::vector<std::pair<uint8_t, uint8_t>> segments = Game::chatFilter->IsSentenceOkay(message, entity->GetGMLevel(), !(isBestFriend && chatLevel == 1));
bool bAllClean = segments.empty();
if (user->GetIsMuted()) { if (user->GetIsMuted()) {
bAllClean = false; bAllClean = false;
} }
user->SetLastChatMessageApproved(bAllClean); user->SetLastChatMessageApproved(bAllClean);
WorldPackets::SendChatModerationResponse(sysAddr, bAllClean, requestID, receiver, unacceptedItems); WorldPackets::SendChatModerationResponse(sysAddr, bAllClean, requestID, receiver, segments);
} }

View File

@ -188,25 +188,28 @@ void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, Entity* ent
Game::logger->Log("WorldPackets", "Sent CreateCharacter for ID: %llu\n", entity->GetObjectID()); Game::logger->Log("WorldPackets", "Sent CreateCharacter for ID: %llu\n", entity->GetObjectID());
} }
void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::unordered_map<char, char> unacceptedItems) { void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::vector<std::pair<uint8_t, uint8_t>> unacceptedItems) {
CBITSTREAM CBITSTREAM
PacketUtils::WriteHeader(bitStream, CLIENT, MSG_CLIENT_CHAT_MODERATION_STRING); PacketUtils::WriteHeader(bitStream, CLIENT, MSG_CLIENT_CHAT_MODERATION_STRING);
bitStream.Write(static_cast<char>(requestAccepted)); bitStream.Write<uint8_t>(unacceptedItems.empty()); // Is sentence ok?
bitStream.Write(static_cast<uint16_t>(0)); bitStream.Write<uint16_t>(0x16); // Source ID, unknown
bitStream.Write(static_cast<uint8_t>(requestID));
bitStream.Write(static_cast<char>(0));
for (uint32_t i = 0; i < 33; ++i) { bitStream.Write(static_cast<uint8_t>(requestID)); // request ID
bitStream.Write(static_cast<uint16_t>(receiver[i])); bitStream.Write(static_cast<char>(0)); // chat mode
}
for (std::unordered_map<char, char>::iterator it = unacceptedItems.begin(); it != unacceptedItems.end(); ++it) { PacketUtils::WritePacketWString(receiver, 42, &bitStream); // receiver name
bitStream.Write(it->first);
bitStream.Write(it->second);
}
SEND_PACKET for (auto it : unacceptedItems) {
bitStream.Write<uint8_t>(it.first); // start index
bitStream.Write<uint8_t>(it.second); // length
}
for (int i = unacceptedItems.size(); 64 > i; i++) {
bitStream.Write<uint16_t>(0);
}
SEND_PACKET
} }
void WorldPackets::SendGMLevelChange(const SystemAddress& sysAddr, bool success, uint8_t highestLevel, uint8_t prevLevel, uint8_t newLevel) { void WorldPackets::SendGMLevelChange(const SystemAddress& sysAddr, bool success, uint8_t highestLevel, uint8_t prevLevel, uint8_t newLevel) {

View File

@ -18,7 +18,7 @@ namespace WorldPackets {
void SendTransferToWorld(const SystemAddress& sysAddr, const std::string& serverIP, uint32_t serverPort, bool mythranShift); void SendTransferToWorld(const SystemAddress& sysAddr, const std::string& serverIP, uint32_t serverPort, bool mythranShift);
void SendServerState(const SystemAddress& sysAddr); void SendServerState(const SystemAddress& sysAddr);
void SendCreateCharacter(const SystemAddress& sysAddr, Entity* entity, const std::string& xmlData, const std::u16string& username, int32_t gm); void SendCreateCharacter(const SystemAddress& sysAddr, Entity* entity, const std::string& xmlData, const std::u16string& username, int32_t gm);
void SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::unordered_map<char, char> unacceptedItems); void SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::vector<std::pair<uint8_t, uint8_t>> unacceptedItems);
void SendGMLevelChange(const SystemAddress& sysAddr, bool success, uint8_t highestLevel, uint8_t prevLevel, uint8_t newLevel); void SendGMLevelChange(const SystemAddress& sysAddr, bool success, uint8_t highestLevel, uint8_t prevLevel, uint8_t newLevel);
} }

BIN
resources/blacklist.dcf Normal file

Binary file not shown.