[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:
Kinglykrab 2022-05-06 21:36:23 -04:00 committed by GitHub
parent 4eaf44fc33
commit bf1d05d639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 229 additions and 128 deletions

View File

@ -19,6 +19,7 @@
#include "profanity_manager.h" #include "profanity_manager.h"
#include "dbcore.h" #include "dbcore.h"
#include "string_util.h"
#include <ctype.h> #include <ctype.h>
#include <cstring> #include <cstring>
@ -34,15 +35,17 @@ bool EQ::ProfanityManager::LoadProfanityList(DBcore *db) {
return true; return true;
} }
if (!load_database_entries(db)) if (!load_database_entries(db)) {
return false; return false;
}
return true; return true;
} }
bool EQ::ProfanityManager::UpdateProfanityList(DBcore *db) { bool EQ::ProfanityManager::UpdateProfanityList(DBcore *db) {
if (!load_database_entries(db)) if (!load_database_entries(db)) {
return false; return false;
}
update_originator_flag = true; update_originator_flag = true;
@ -58,53 +61,60 @@ bool EQ::ProfanityManager::DeleteProfanityList(DBcore *db) {
return true; return true;
} }
bool EQ::ProfanityManager::AddProfanity(DBcore *db, const char *profanity) { bool EQ::ProfanityManager::AddProfanity(DBcore *db, std::string profanity) {
if (!db || !profanity) if (!db || profanity.empty()) {
return false; 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)) {
if (check_for_existing_entry(entry.c_str()))
return true; return true;
}
if (entry.length() < REDACTION_LENGTH_MIN) if (entry.length() < REDACTION_LENGTH_MIN) {
return false; return false;
}
profanity_list.push_back(entry); profanity_list.push_back(entry);
std::string query = "REPLACE INTO `profanity_list` (`word`) VALUES ('"; auto query = fmt::format(
query.append(entry); "REPLACE INTO `profanity_list` (`word`) VALUES ('{}')",
query.append("')"); profanity
);
auto results = db->QueryDatabase(query); auto results = db->QueryDatabase(query);
if (!results.Success())
if (!results.Success()) {
return false; return false;
}
update_originator_flag = true; update_originator_flag = true;
return true; return true;
} }
bool EQ::ProfanityManager::RemoveProfanity(DBcore *db, const char *profanity) { bool EQ::ProfanityManager::RemoveProfanity(DBcore *db, std::string profanity) {
if (!db || !profanity) if (!db || profanity.empty()) {
return false; 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)) {
if (!check_for_existing_entry(entry.c_str()))
return true; return true;
}
profanity_list.remove(entry); profanity_list.remove(entry);
std::string query = "DELETE FROM `profanity_list` WHERE `word` LIKE '"; auto query = fmt::format(
query.append(entry); "DELETE FROM `profanity_list` WHERE `word` = '{}'",
query.append("'"); entry
);
auto results = db->QueryDatabase(query); auto results = db->QueryDatabase(query);
if (!results.Success())
if (!results.Success()) {
return false; return false;
}
update_originator_flag = true; update_originator_flag = true;
@ -112,16 +122,16 @@ bool EQ::ProfanityManager::RemoveProfanity(DBcore *db, const char *profanity) {
} }
void EQ::ProfanityManager::RedactMessage(char *message) { void EQ::ProfanityManager::RedactMessage(char *message) {
if (!message) if (!message) {
return; 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).. // 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 // ..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; 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 for (const auto &iter : profanity_list) { // consider adding textlink checks if it becomes an issue
size_t pos = 0; size_t pos = 0;
@ -129,12 +139,17 @@ void EQ::ProfanityManager::RedactMessage(char *message) {
while (pos != std::string::npos) { while (pos != std::string::npos) {
pos = test_message.find(iter, start_pos); pos = test_message.find(iter, start_pos);
if (pos == std::string::npos) if (pos == std::string::npos) {
continue; continue;
}
if ((pos + iter.length()) == test_message.length() || !isalpha(test_message.at(pos + iter.length()))) { if (
if (pos == 0 || !isalpha(test_message.at(pos - 1))) (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()); memset((message + pos), REDACTION_CHARACTER, iter.length());
}
} }
start_pos = (pos + iter.length()); start_pos = (pos + iter.length());
@ -143,25 +158,29 @@ void EQ::ProfanityManager::RedactMessage(char *message) {
} }
void EQ::ProfanityManager::RedactMessage(std::string &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; 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) {
for (const auto &iter : profanity_list) { // consider adding textlink checks if it becomes an issue
size_t pos = 0; size_t pos = 0;
size_t start_pos = 0; size_t start_pos = 0;
while (pos != std::string::npos) { while (pos != std::string::npos) {
pos = test_message.find(iter, start_pos); pos = test_message.find(iter, start_pos);
if (pos == std::string::npos) if (pos == std::string::npos) {
continue; continue;
}
if ((pos + iter.length()) == test_message.length() || !isalpha(test_message.at(pos + iter.length()))) { if (
if (pos == 0 || !isalpha(test_message.at(pos - 1))) (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); message.replace(pos, iter.length(), iter.length(), REDACTION_CHARACTER);
}
} }
start_pos = (pos + iter.length()); 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) { 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; return false;
}
std::string test_message(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) { 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 true;
}
} }
return false; return false;
@ -197,26 +210,28 @@ const std::list<std::string> &EQ::ProfanityManager::GetProfanityList() {
} }
bool EQ::ProfanityManager::IsCensorshipActive() { bool EQ::ProfanityManager::IsCensorshipActive() {
return (profanity_list.size() != 0); return profanity_list.size() != 0;
} }
bool EQ::ProfanityManager::load_database_entries(DBcore *db) { bool EQ::ProfanityManager::load_database_entries(DBcore *db) {
if (!db) if (!db) {
return false; return false;
}
profanity_list.clear(); profanity_list.clear();
std::string query = "SELECT `word` FROM `profanity_list`"; std::string query = "SELECT `word` FROM `profanity_list`";
auto results = db->QueryDatabase(query); auto results = db->QueryDatabase(query);
if (!results.Success()) if (!results.Success()) {
return false; return false;
}
for (auto row = results.begin(); row != results.end(); ++row) { for (auto row : results) {
if (std::strlen(row[0]) >= REDACTION_LENGTH_MIN) { std::string entry = str_tolower(row[0]);
std::string entry(row[0]); if (entry.length() >= REDACTION_LENGTH_MIN) {
std::transform(entry.begin(), entry.end(), entry.begin(), [](unsigned char c) -> unsigned char { return tolower(c); }); if (!check_for_existing_entry(entry)) {
if (!check_for_existing_entry(entry.c_str())) profanity_list.push_back(entry);
profanity_list.push_back((std::string)entry); }
} }
} }
@ -224,26 +239,31 @@ bool EQ::ProfanityManager::load_database_entries(DBcore *db) {
} }
bool EQ::ProfanityManager::clear_database_entries(DBcore *db) { bool EQ::ProfanityManager::clear_database_entries(DBcore *db) {
if (!db) if (!db) {
return false; return false;
}
profanity_list.clear(); profanity_list.clear();
std::string query = "DELETE FROM `profanity_list`"; std::string query = "DELETE FROM `profanity_list`";
auto results = db->QueryDatabase(query); auto results = db->QueryDatabase(query);
if (!results.Success())
if (!results.Success()) {
return false; return false;
}
return true; return true;
} }
bool EQ::ProfanityManager::check_for_existing_entry(const char *profanity) { bool EQ::ProfanityManager::check_for_existing_entry(std::string profanity) {
if (!profanity) if (profanity.empty()) {
return false; return false;
}
for (const auto &iter : profanity_list) { for (const auto &iter : profanity_list) {
if (iter.compare(profanity) == 0) if (!iter.compare(profanity)) {
return true; return true;
}
} }
return false; return false;

View File

@ -22,6 +22,7 @@
#include <string> #include <string>
#include <list> #include <list>
#include <fmt/format.h>
class DBcore; class DBcore;
@ -34,13 +35,12 @@ namespace EQ
static bool UpdateProfanityList(DBcore *db); static bool UpdateProfanityList(DBcore *db);
static bool DeleteProfanityList(DBcore *db); static bool DeleteProfanityList(DBcore *db);
static bool AddProfanity(DBcore *db, const char *profanity); static bool AddProfanity(DBcore *db, std::string profanity);
static bool RemoveProfanity(DBcore *db, const char *profanity); static bool RemoveProfanity(DBcore *db, std::string profanity);
static void RedactMessage(char *message); static void RedactMessage(char *message);
static void RedactMessage(std::string &message); static void RedactMessage(std::string &message);
static bool ContainsCensoredLanguage(const char *message);
static bool ContainsCensoredLanguage(const std::string &message); static bool ContainsCensoredLanguage(const std::string &message);
static const std::list<std::string> &GetProfanityList(); static const std::list<std::string> &GetProfanityList();
@ -53,7 +53,7 @@ namespace EQ
private: private:
static bool load_database_entries(DBcore *db); static bool load_database_entries(DBcore *db);
static bool clear_database_entries(DBcore *db); static bool clear_database_entries(DBcore *db);
static bool check_for_existing_entry(const char *profanity); static bool check_for_existing_entry(std::string profanity);
}; };

View File

@ -7,68 +7,149 @@ extern WorldServer worldserver;
void command_profanity(Client *c, const Seperator *sep) void command_profanity(Client *c, const Seperator *sep)
{ {
std::string arg1(sep->arg[1]); int arguments = sep->argnum;
if (!arguments) {
while (true) { c->Message(Chat::White, "Usage: #profanity add [Word] - Adds a word to the profanity list");
if (arg1.compare("list") == 0) { c->Message(Chat::White, "Usage: #profanity clear - Deletes all profanity list");
// do nothing c->Message(Chat::White, "Usage: #profanity delete [Word] - Deletes a word from the profanity list");
} c->Message(Chat::White, "Usage: #profanity list - Shows the profanity list");
else if (arg1.compare("clear") == 0) { c->Message(Chat::White, "Usage: #profanity reload - Reloads the profanity list");
EQ::ProfanityManager::DeleteProfanityList(&database); return;
auto pack = new ServerPacket(ServerOP_RefreshCensorship); }
worldserver.SendPacket(pack);
safe_delete(pack); bool is_add = !strcasecmp(sep->arg[1], "add");
} bool is_clear = !strcasecmp(sep->arg[1], "clear");
else if (arg1.compare("add") == 0) { bool is_delete = !strcasecmp(sep->arg[1], "delete");
if (!EQ::ProfanityManager::AddProfanity(&database, sep->arg[2])) { bool is_list = !strcasecmp(sep->arg[1], "list");
c->Message(Chat::Red, "Could not add '%s' to the profanity list.", sep->arg[2]); bool is_reload = !strcasecmp(sep->arg[1], "reload");
} if (
auto pack = new ServerPacket(ServerOP_RefreshCensorship); !is_add &&
worldserver.SendPacket(pack); !is_clear &&
safe_delete(pack); !is_delete &&
} !is_list &&
else if (arg1.compare("del") == 0) { !is_reload
if (!EQ::ProfanityManager::RemoveProfanity(&database, sep->arg[2])) { ) {
c->Message(Chat::Red, "Could not delete '%s' from the profanity list.", sep->arg[2]); c->Message(Chat::White, "Usage: #profanity add [Word] - Adds a word to the profanity list");
} c->Message(Chat::White, "Usage: #profanity clear - Deletes all profanity list");
auto pack = new ServerPacket(ServerOP_RefreshCensorship); c->Message(Chat::White, "Usage: #profanity delete [Word] - Deletes a word from the profanity list");
worldserver.SendPacket(pack); c->Message(Chat::White, "Usage: #profanity list - Shows the profanity list");
safe_delete(pack); c->Message(Chat::White, "Usage: #profanity reload - Reloads the profanity list");
}
else if (arg1.compare("reload") == 0) {
if (!EQ::ProfanityManager::UpdateProfanityList(&database)) {
c->Message(Chat::Red, "Could not reload the profanity list.");
}
auto pack = new ServerPacket(ServerOP_RefreshCensorship);
worldserver.SendPacket(pack);
safe_delete(pack);
}
else {
break;
}
std::string popup;
const auto &list = EQ::ProfanityManager::GetProfanityList();
for (const auto &iter : list) {
popup.append(iter);
popup.append("<br>");
}
if (list.empty()) {
popup.append("** Censorship Inactive **<br>");
}
else {
popup.append("** End of List **<br>");
}
c->SendPopupToClient("Profanity List", popup.c_str());
return; return;
} }
c->Message(Chat::White, "Usage: #profanity [list] - shows profanity list"); if (is_add) {
c->Message(Chat::White, "Usage: #profanity [clear] - deletes all entries"); if (arguments < 2) {
c->Message(Chat::White, "Usage: #profanity [add] [<word>] - adds entry"); c->Message(Chat::White, "Usage: #profanity add [Word] - Adds a word to the profanity list");
c->Message(Chat::White, "Usage: #profanity [del] [<word>] - deletes entry"); return;
c->Message(Chat::White, "Usage: #profanity [reload] - reloads profanity list"); }
std::string profanity = sep->arg[2];
if (!EQ::ProfanityManager::AddProfanity(&database, profanity)) {
c->Message(
Chat::White,
fmt::format(
"Could not add '{}' to the profanity list.",
profanity
).c_str()
);
return;
} else {
c->Message(
Chat::White,
fmt::format(
"Added '{}' to the profanity list.",
profanity
).c_str()
);
}
auto pack = new ServerPacket(ServerOP_RefreshCensorship);
worldserver.SendPacket(pack);
safe_delete(pack);
} else if (is_clear) {
if (!EQ::ProfanityManager::DeleteProfanityList(&database)) {
c->Message(Chat::White, "Could not clear the profanity list.");
return;
} else {
c->Message(Chat::White, "Cleared the profanity list.");
}
auto pack = new ServerPacket(ServerOP_RefreshCensorship);
worldserver.SendPacket(pack);
safe_delete(pack);
} else if (is_delete) {
if (arguments < 2) {
c->Message(Chat::White, "Usage: #profanity delete [Word] - Deletes a word from the profanity list");
return;
}
std::string profanity = sep->arg[2];
if (!EQ::ProfanityManager::RemoveProfanity(&database, profanity)) {
c->Message(
Chat::White,
fmt::format(
"Could not delete '{}' from the profanity list.",
profanity
).c_str()
);
return;
} else {
c->Message(
Chat::White,
fmt::format(
"Deleted '{}' from the profanity list.",
profanity
).c_str()
);
}
auto pack = new ServerPacket(ServerOP_RefreshCensorship);
worldserver.SendPacket(pack);
safe_delete(pack);
} else if (is_list) {
std::string popup_message;
std::string popup_title;
if (!EQ::ProfanityManager::IsCensorshipActive()) {
popup_title = "Profanity List [Empty]";
popup_message = "The profanity list is empty.";
} else {
auto profanity_index = 1;
auto profanity_list = EQ::ProfanityManager::GetProfanityList();
popup_title = fmt::format(
"Profanity List [{} Entr{}]",
profanity_list.size(),
profanity_list.size() != 1 ? "ies" : "y"
);
for (const auto& profanity : profanity_list) {
popup_message.append(
fmt::format(
"{}. {}<br>",
profanity_index,
profanity
)
);
profanity_index++;
}
}
c->SendPopupToClient(
popup_title.c_str(),
popup_message.c_str()
);
} else if (is_reload) {
if (!EQ::ProfanityManager::UpdateProfanityList(&database)) {
c->Message(Chat::White, "Could not reload the profanity list.");
return;
} else {
c->Message(Chat::White, "Reloaded the profanity list.");
}
auto pack = new ServerPacket(ServerOP_RefreshCensorship);
worldserver.SendPacket(pack);
safe_delete(pack);
}
} }