ban words (#185)

禁用词汇表。。。
过滤ID、房间名、聊天信息。。
我做这个干啥呢
This commit is contained in:
notify 2023-06-10 11:35:45 +08:00 committed by GitHub
parent 7f718503bd
commit 59d1623dbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View File

@ -305,6 +305,9 @@ void Room::chat(ServerPlayer *sender, const QString &jsonData) {
auto msg = doc["msg"].toString();
msg.replace(".", "");
doc["msg"] = msg;
if (!server->checkBanWord(msg)) {
return;
}
if (type == 1) {
doc["userName"] = sender->getScreenName();

View File

@ -95,6 +95,12 @@ bool Server::listen(const QHostAddress &address, ushort port) {
void Server::createRoom(ServerPlayer *owner, const QString &name, int capacity,
int timeout, const QByteArray &settings) {
if (!checkBanWord(name)) {
if (owner) {
owner->doNotify("ErrorMsg", "unk error");
}
return;
}
Room *room;
if (!idle_rooms.isEmpty()) {
room = idle_rooms.pop();
@ -332,7 +338,7 @@ void Server::handleNameAndPassword(ClientSocket *client, const QString &name,
QJsonArray result;
QJsonObject obj;
if (CheckSqlString(name)) {
if (CheckSqlString(name) && checkBanWord(name)) {
// Then we check the database,
QString sql_find = QString("SELECT * FROM userinfo \
WHERE name='%1';")
@ -541,3 +547,17 @@ void Server::readConfig() {
}
QJsonValue Server::getConfig(const QString &key) { return config.value(key); }
bool Server::checkBanWord(const QString &str) {
auto arr = getConfig("banwords").toArray();
if (arr.isEmpty()) {
return true;
}
foreach (auto v, arr) {
auto s = v.toString();
if (str.indexOf(s) != -1) {
return false;
}
}
return true;
}

View File

@ -42,6 +42,7 @@ public:
bool isListening;
QJsonValue getConfig(const QString &command);
bool checkBanWord(const QString &str);
signals:
void roomCreated(Room *room);
void playerAdded(ServerPlayer *player);