mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-08-05 18:24:12 +00:00
Public release of the DLU server code!
Have fun!
This commit is contained in:
137
dChatFilter/dChatFilter.cpp
Normal file
137
dChatFilter/dChatFilter.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
#include "dChatFilter.h"
|
||||
#include "BinaryIO.h"
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
|
||||
#include "dCommonVars.h"
|
||||
#include "Database.h"
|
||||
#include "dLogger.h"
|
||||
#include "Game.h"
|
||||
|
||||
using namespace dChatFilterDCF;
|
||||
|
||||
dChatFilter::dChatFilter(const std::string& filepath, bool dontGenerateDCF) {
|
||||
m_DontGenerateDCF = dontGenerateDCF;
|
||||
|
||||
if (!BinaryIO::DoesFileExist(filepath + ".dcf") || m_DontGenerateDCF) {
|
||||
ReadWordlistPlaintext(filepath + ".txt");
|
||||
if (!m_DontGenerateDCF) ExportWordlistToDCF(filepath + ".dcf");
|
||||
}
|
||||
else {
|
||||
ReadWordlistDCF(filepath + ".dcf");
|
||||
}
|
||||
|
||||
//Read player names that are ok as well:
|
||||
auto stmt = Database::CreatePreppedStmt("select name from charinfo;");
|
||||
auto res = stmt->executeQuery();
|
||||
while (res->next()) {
|
||||
std::string line = res->getString(1).c_str();
|
||||
std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase
|
||||
m_Words.push_back(CalculateHash(line));
|
||||
}
|
||||
delete res;
|
||||
delete stmt;
|
||||
}
|
||||
|
||||
dChatFilter::~dChatFilter() {
|
||||
m_Words.clear();
|
||||
}
|
||||
|
||||
void dChatFilter::ReadWordlistPlaintext(const std::string& filepath) {
|
||||
std::ifstream file(filepath);
|
||||
if (file) {
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
std::transform(line.begin(), line.end(), line.begin(), ::tolower); //Transform to lowercase
|
||||
m_Words.push_back(CalculateHash(line));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dChatFilter::ReadWordlistDCF(const std::string& filepath) {
|
||||
std::ifstream file(filepath, std::ios::binary);
|
||||
if (file) {
|
||||
fileHeader hdr;
|
||||
BinaryIO::BinaryRead(file, hdr);
|
||||
if (hdr.header != header) {
|
||||
std::cout << "Wrong file header!" << std::endl;
|
||||
file.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (hdr.formatVersion <= formatVersion) {
|
||||
size_t wordsToRead = 0;
|
||||
BinaryIO::BinaryRead(file, wordsToRead);
|
||||
m_Words.reserve(wordsToRead);
|
||||
|
||||
size_t word = 0;
|
||||
for (size_t i = 0; i < wordsToRead; ++i) {
|
||||
BinaryIO::BinaryRead(file, word);
|
||||
m_Words.push_back(word);
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << "Newer file or corrupt" << std::endl;
|
||||
file.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dChatFilter::ExportWordlistToDCF(const std::string& filepath) {
|
||||
std::ofstream file(filepath, std::ios::binary);
|
||||
if (file) {
|
||||
BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::header));
|
||||
BinaryIO::BinaryWrite(file, uint32_t(dChatFilterDCF::formatVersion));
|
||||
BinaryIO::BinaryWrite(file, size_t(m_Words.size()));
|
||||
|
||||
for (size_t word : m_Words) {
|
||||
BinaryIO::BinaryWrite(file, word);
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
bool dChatFilter::IsSentenceOkay(const std::string& message, int gmLevel) {
|
||||
if (gmLevel > GAME_MASTER_LEVEL_FORUM_MODERATOR) return true; //If anything but a forum mod, return true.
|
||||
if (message.empty()) return true;
|
||||
|
||||
std::stringstream sMessage(message);
|
||||
std::string segment;
|
||||
std::regex reg("(!*|\\?*|\\;*|\\.*|\\,*)");
|
||||
|
||||
while (std::getline(sMessage, segment, ' ')) {
|
||||
std::transform(segment.begin(), segment.end(), segment.begin(), ::tolower); //Transform to lowercase
|
||||
segment = std::regex_replace(segment, reg, "");
|
||||
|
||||
size_t hash = CalculateHash(segment);
|
||||
|
||||
if (std::find(m_UserUnapprovedWordCache.begin(), m_UserUnapprovedWordCache.end(), hash) != m_UserUnapprovedWordCache.end()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!IsInWordlist(hash)) {
|
||||
m_UserUnapprovedWordCache.push_back(hash);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t dChatFilter::CalculateHash(const std::string& word) {
|
||||
std::hash<std::string> hash{};
|
||||
|
||||
size_t value = hash(word);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
bool dChatFilter::IsInWordlist(size_t word) {
|
||||
return std::find(m_Words.begin(), m_Words.end(), word) != m_Words.end();
|
||||
}
|
36
dChatFilter/dChatFilter.h
Normal file
36
dChatFilter/dChatFilter.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "dCommonVars.h"
|
||||
|
||||
namespace dChatFilterDCF {
|
||||
static const uint32_t header = ('D' + ('C' << 8) + ('F' << 16) + ('B' << 24));
|
||||
static const uint32_t formatVersion = 1;
|
||||
|
||||
struct fileHeader {
|
||||
uint32_t header;
|
||||
uint32_t formatVersion;
|
||||
};
|
||||
};
|
||||
|
||||
class dChatFilter
|
||||
{
|
||||
public:
|
||||
dChatFilter(const std::string& filepath, bool dontGenerateDCF);
|
||||
~dChatFilter();
|
||||
|
||||
void ReadWordlistPlaintext(const std::string & filepath);
|
||||
void ReadWordlistDCF(const std::string & filepath);
|
||||
void ExportWordlistToDCF(const std::string & filepath);
|
||||
bool IsSentenceOkay(const std::string& message, int gmLevel);
|
||||
|
||||
private:
|
||||
bool m_DontGenerateDCF;
|
||||
std::vector<size_t> m_Words;
|
||||
std::vector<size_t> m_UserUnapprovedWordCache;
|
||||
|
||||
//Private functions:
|
||||
size_t CalculateHash(const std::string& word);
|
||||
bool IsInWordlist(size_t word);
|
||||
};
|
Reference in New Issue
Block a user