mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-16 22:58:34 +00:00
[Commands] Cleanup #profanity Command. (#2113)
* [Commands] Cleanup #profanity Command. - Cleanup messages and logic. * Update profanity_manager.cpp * Update profanity_manager.cpp * Update profanity_manager.cpp * Update profanity_manager.cpp
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "profanity_manager.h"
|
||||
#include "dbcore.h"
|
||||
#include "string_util.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <cstring>
|
||||
@@ -34,15 +35,17 @@ bool EQ::ProfanityManager::LoadProfanityList(DBcore *db) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!load_database_entries(db))
|
||||
if (!load_database_entries(db)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::UpdateProfanityList(DBcore *db) {
|
||||
if (!load_database_entries(db))
|
||||
if (!load_database_entries(db)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
update_originator_flag = true;
|
||||
|
||||
@@ -58,53 +61,60 @@ bool EQ::ProfanityManager::DeleteProfanityList(DBcore *db) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::AddProfanity(DBcore *db, const char *profanity) {
|
||||
if (!db || !profanity)
|
||||
bool EQ::ProfanityManager::AddProfanity(DBcore *db, std::string profanity) {
|
||||
if (!db || profanity.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string entry(profanity);
|
||||
std::string entry = str_tolower(profanity);
|
||||
|
||||
std::transform(entry.begin(), entry.end(), entry.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
|
||||
|
||||
if (check_for_existing_entry(entry.c_str()))
|
||||
if (check_for_existing_entry(entry)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (entry.length() < REDACTION_LENGTH_MIN)
|
||||
if (entry.length() < REDACTION_LENGTH_MIN) {
|
||||
return false;
|
||||
}
|
||||
|
||||
profanity_list.push_back(entry);
|
||||
|
||||
std::string query = "REPLACE INTO `profanity_list` (`word`) VALUES ('";
|
||||
query.append(entry);
|
||||
query.append("')");
|
||||
auto query = fmt::format(
|
||||
"REPLACE INTO `profanity_list` (`word`) VALUES ('{}')",
|
||||
profanity
|
||||
);
|
||||
auto results = db->QueryDatabase(query);
|
||||
if (!results.Success())
|
||||
|
||||
if (!results.Success()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
update_originator_flag = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::RemoveProfanity(DBcore *db, const char *profanity) {
|
||||
if (!db || !profanity)
|
||||
bool EQ::ProfanityManager::RemoveProfanity(DBcore *db, std::string profanity) {
|
||||
if (!db || profanity.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string entry(profanity);
|
||||
std::string entry = str_tolower(profanity);
|
||||
|
||||
std::transform(entry.begin(), entry.end(), entry.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
|
||||
|
||||
if (!check_for_existing_entry(entry.c_str()))
|
||||
if (!check_for_existing_entry(entry)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
profanity_list.remove(entry);
|
||||
|
||||
std::string query = "DELETE FROM `profanity_list` WHERE `word` LIKE '";
|
||||
query.append(entry);
|
||||
query.append("'");
|
||||
auto query = fmt::format(
|
||||
"DELETE FROM `profanity_list` WHERE `word` = '{}'",
|
||||
entry
|
||||
);
|
||||
auto results = db->QueryDatabase(query);
|
||||
if (!results.Success())
|
||||
|
||||
if (!results.Success()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
update_originator_flag = true;
|
||||
|
||||
@@ -112,16 +122,16 @@ bool EQ::ProfanityManager::RemoveProfanity(DBcore *db, const char *profanity) {
|
||||
}
|
||||
|
||||
void EQ::ProfanityManager::RedactMessage(char *message) {
|
||||
if (!message)
|
||||
if (!message) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string test_message(message);
|
||||
std::string test_message = str_tolower(message);
|
||||
// hard-coded max length based on channel message buffer size (4096 bytes)..
|
||||
// ..will need to change or remove if other sources are used for redaction
|
||||
if (test_message.length() < REDACTION_LENGTH_MIN || test_message.length() >= 4096)
|
||||
if (test_message.length() < REDACTION_LENGTH_MIN || test_message.length() >= 4096) {
|
||||
return;
|
||||
|
||||
std::transform(test_message.begin(), test_message.end(), test_message.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
|
||||
}
|
||||
|
||||
for (const auto &iter : profanity_list) { // consider adding textlink checks if it becomes an issue
|
||||
size_t pos = 0;
|
||||
@@ -129,12 +139,17 @@ void EQ::ProfanityManager::RedactMessage(char *message) {
|
||||
|
||||
while (pos != std::string::npos) {
|
||||
pos = test_message.find(iter, start_pos);
|
||||
if (pos == std::string::npos)
|
||||
if (pos == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((pos + iter.length()) == test_message.length() || !isalpha(test_message.at(pos + iter.length()))) {
|
||||
if (pos == 0 || !isalpha(test_message.at(pos - 1)))
|
||||
if (
|
||||
(pos + iter.length()) == test_message.length() ||
|
||||
!isalpha(test_message.at(pos + iter.length()))
|
||||
) {
|
||||
if (pos == 0 || !isalpha(test_message.at(pos - 1))) {
|
||||
memset((message + pos), REDACTION_CHARACTER, iter.length());
|
||||
}
|
||||
}
|
||||
|
||||
start_pos = (pos + iter.length());
|
||||
@@ -143,25 +158,29 @@ void EQ::ProfanityManager::RedactMessage(char *message) {
|
||||
}
|
||||
|
||||
void EQ::ProfanityManager::RedactMessage(std::string &message) {
|
||||
if (message.length() < REDACTION_LENGTH_MIN || message.length() >= 4096)
|
||||
if (message.length() < REDACTION_LENGTH_MIN || message.length() >= 4096) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string test_message(const_cast<const std::string&>(message));
|
||||
std::string test_message = str_tolower(message);
|
||||
|
||||
std::transform(test_message.begin(), test_message.end(), test_message.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
|
||||
|
||||
for (const auto &iter : profanity_list) { // consider adding textlink checks if it becomes an issue
|
||||
for (const auto &iter : profanity_list) {
|
||||
size_t pos = 0;
|
||||
size_t start_pos = 0;
|
||||
|
||||
while (pos != std::string::npos) {
|
||||
pos = test_message.find(iter, start_pos);
|
||||
if (pos == std::string::npos)
|
||||
if (pos == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((pos + iter.length()) == test_message.length() || !isalpha(test_message.at(pos + iter.length()))) {
|
||||
if (pos == 0 || !isalpha(test_message.at(pos - 1)))
|
||||
if (
|
||||
(pos + iter.length()) == test_message.length() ||
|
||||
!isalpha(test_message.at(pos + iter.length()))
|
||||
) {
|
||||
if (pos == 0 || !isalpha(test_message.at(pos - 1))) {
|
||||
message.replace(pos, iter.length(), iter.length(), REDACTION_CHARACTER);
|
||||
}
|
||||
}
|
||||
|
||||
start_pos = (pos + iter.length());
|
||||
@@ -169,24 +188,18 @@ void EQ::ProfanityManager::RedactMessage(std::string &message) {
|
||||
}
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::ContainsCensoredLanguage(const char *message) {
|
||||
if (!message)
|
||||
return false;
|
||||
|
||||
return ContainsCensoredLanguage(std::string(message));
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::ContainsCensoredLanguage(const std::string &message) {
|
||||
if (message.length() < REDACTION_LENGTH_MIN || message.length() >= 4096)
|
||||
if (message.length() < REDACTION_LENGTH_MIN || message.length() >= 4096) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string test_message(message);
|
||||
|
||||
std::transform(test_message.begin(), test_message.end(), test_message.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
|
||||
std::string test_message = str_tolower(message);
|
||||
|
||||
for (const auto &iter : profanity_list) {
|
||||
if (test_message.find(iter) != std::string::npos)
|
||||
if (test_message.find(iter) != std::string::npos) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -197,26 +210,28 @@ const std::list<std::string> &EQ::ProfanityManager::GetProfanityList() {
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::IsCensorshipActive() {
|
||||
return (profanity_list.size() != 0);
|
||||
return profanity_list.size() != 0;
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::load_database_entries(DBcore *db) {
|
||||
if (!db)
|
||||
if (!db) {
|
||||
return false;
|
||||
}
|
||||
|
||||
profanity_list.clear();
|
||||
|
||||
std::string query = "SELECT `word` FROM `profanity_list`";
|
||||
auto results = db->QueryDatabase(query);
|
||||
if (!results.Success())
|
||||
if (!results.Success()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto row = results.begin(); row != results.end(); ++row) {
|
||||
if (std::strlen(row[0]) >= REDACTION_LENGTH_MIN) {
|
||||
std::string entry(row[0]);
|
||||
std::transform(entry.begin(), entry.end(), entry.begin(), [](unsigned char c) -> unsigned char { return tolower(c); });
|
||||
if (!check_for_existing_entry(entry.c_str()))
|
||||
profanity_list.push_back((std::string)entry);
|
||||
for (auto row : results) {
|
||||
std::string entry = str_tolower(row[0]);
|
||||
if (entry.length() >= REDACTION_LENGTH_MIN) {
|
||||
if (!check_for_existing_entry(entry)) {
|
||||
profanity_list.push_back(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,26 +239,31 @@ bool EQ::ProfanityManager::load_database_entries(DBcore *db) {
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::clear_database_entries(DBcore *db) {
|
||||
if (!db)
|
||||
if (!db) {
|
||||
return false;
|
||||
}
|
||||
|
||||
profanity_list.clear();
|
||||
|
||||
std::string query = "DELETE FROM `profanity_list`";
|
||||
auto results = db->QueryDatabase(query);
|
||||
if (!results.Success())
|
||||
|
||||
if (!results.Success()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EQ::ProfanityManager::check_for_existing_entry(const char *profanity) {
|
||||
if (!profanity)
|
||||
bool EQ::ProfanityManager::check_for_existing_entry(std::string profanity) {
|
||||
if (profanity.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto &iter : profanity_list) {
|
||||
if (iter.compare(profanity) == 0)
|
||||
if (!iter.compare(profanity)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user