Skip to content

Commit

Permalink
add regex filter (#370)
Browse files Browse the repository at this point in the history
* add regex list

* replace CT as well

* replace instead of remove

* add a default filter
  • Loading branch information
stonedDiscord authored Aug 2, 2024
1 parent aab4198 commit db50c81
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions bin/config_sample/text/filter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tranny
6 changes: 6 additions & 0 deletions src/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ bool ConfigManager::verifyServerConfig()
m_commands->praises = (loadConfigFile("praise"));
m_commands->reprimands = (loadConfigFile("reprimands"));
m_commands->gimps = (loadConfigFile("gimp"));
m_commands->filters = (loadConfigFile("filter"));
m_commands->cdns = (loadConfigFile("cdns"));
if (m_commands->cdns.isEmpty())
m_commands->cdns = QStringList{"cdn.discord.com"};
Expand Down Expand Up @@ -621,6 +622,11 @@ QStringList ConfigManager::gimpList()
return m_commands->gimps;
}

QStringList ConfigManager::filterList()
{
return m_commands->filters;
}

QStringList ConfigManager::cdnList()
{
return m_commands->cdns;
Expand Down
8 changes: 8 additions & 0 deletions src/config_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ class ConfigManager
*/
static QStringList gimpList();

/**
* @brief Returns the server regex filter list
*
* @return See short description.
*/
static QStringList filterList();

/**
* @brief Returns the server approved domain list.
*
Expand Down Expand Up @@ -519,6 +526,7 @@ class ConfigManager
QStringList praises; //!< Contains command praises, found in config/text/praises.txt
QStringList reprimands; //!< Contains command reprimands, found in config/text/reprimands.txt
QStringList gimps; //!< Contains phrases for /gimp, found in config/text/gimp.txt
QStringList filters; //!< Contains filter regex, found in config/text/filter.txt
QStringList cdns; // !< Contains domains for custom song validation, found in config/text/cdns.txt
};

Expand Down
11 changes: 10 additions & 1 deletion src/packet/packet_ct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ void PacketCT::handlePacket(AreaData *area, AOClient &client) const
}

QString l_message = client.dezalgo(m_content[1]);

if (l_message.length() == 0 || l_message.length() > ConfigManager::maxCharacters())
return;
AOPacket *final_packet = PacketFactory::createPacket("CT", {client.name(), l_message, "0"});

if (!ConfigManager::filterList().isEmpty()) {
foreach (const QString &regex, ConfigManager::filterList()) {
QRegularExpression re(regex, QRegularExpression::CaseInsensitiveOption);
l_message.replace(re, "");
}
}

if (l_message.at(0) == '/') {
QStringList l_cmd_argv = l_message.split(" ", Qt::SkipEmptyParts);
QString l_command = l_cmd_argv[0].trimmed().toLower();
Expand All @@ -58,6 +66,7 @@ void PacketCT::handlePacket(AreaData *area, AOClient &client) const
return;
}
else {
AOPacket *final_packet = PacketFactory::createPacket("CT", {client.name(), l_message, "0"});
client.getServer()->broadcast(final_packet, client.areaId());
}
emit client.logOOC((client.character() + " " + client.characterName()), client.name(), client.m_ipid, area->name(), l_message);
Expand Down
7 changes: 7 additions & 0 deletions src/packet/packet_ms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ AOPacket *PacketMS::validateIcPacket(AOClient &client) const
return l_invalid;
}

if (!ConfigManager::filterList().isEmpty()) {
foreach (const QString &regex, ConfigManager::filterList()) {
QRegularExpression re(regex, QRegularExpression::CaseInsensitiveOption);
l_incoming_msg.replace(re, "");
}
}

if (client.m_is_gimped) {
QString l_gimp_message = ConfigManager::gimpList().at((client.genRand(1, ConfigManager::gimpList().size() - 1)));
l_incoming_msg = l_gimp_message;
Expand Down

0 comments on commit db50c81

Please sign in to comment.