diff --git a/include/db_manager.h b/include/db_manager.h index a93b7a4..e99ca76 100644 --- a/include/db_manager.h +++ b/include/db_manager.h @@ -39,7 +39,9 @@ public: QString getBanReason(QHostAddress ip); QString getBanReason(QString hdid); long long getBanDuration(QString hdid); - long long getBanDuration(QHostAddress hdid); + long long getBanDuration(QHostAddress ip); + int getBanID(QString hdid); + int getBanID(QHostAddress ip); struct BanInfo { QString ipid; @@ -51,7 +53,7 @@ public: }; QList getRecentBans(); - void addBan(QString ipid, QHostAddress ip, QString hdid, unsigned long time, QString reason, long long duration); + void addBan(BanInfo ban); bool createUser(QString username, QString salt, QString password, unsigned long long acl); unsigned long long getACL(QString moderator_name); diff --git a/src/commands.cpp b/src/commands.cpp index 3325a20..605f224 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -95,6 +95,8 @@ void AOClient::cmdBan(int argc, QStringList argv) args_str += " " + argv[i]; } + DBManager::BanInfo ban; + QRegularExpression quoteMatcher("['\"](.+?)[\"']"); QRegularExpressionMatchIterator matches = quoteMatcher.globalMatch(args_str); QList unquoted_args; @@ -103,7 +105,6 @@ void AOClient::cmdBan(int argc, QStringList argv) unquoted_args.append(match.captured(1)); } - QString reason; QString duration = "perma"; if (unquoted_args.length() < 1) { @@ -111,7 +112,7 @@ void AOClient::cmdBan(int argc, QStringList argv) return; } - reason = unquoted_args.at(0); + ban.reason = unquoted_args.at(0); if (unquoted_args.length() > 1) duration = unquoted_args.at(1); @@ -126,34 +127,34 @@ void AOClient::cmdBan(int argc, QStringList argv) return; } - QString target_ipid = argv[0]; - QHostAddress ip; - QString hdid; - unsigned long time = QDateTime::currentDateTime().toSecsSinceEpoch(); + ban.duration = duration_seconds; + + ban.ipid = argv[0]; + ban.time = QDateTime::currentDateTime().toSecsSinceEpoch(); bool ban_logged = false; int kick_counter = 0; if (argc > 2) { for (int i = 2; i < argv.length(); i++) { - reason += " " + argv[i]; + ban.reason += " " + argv[i]; } } - for (AOClient* client : server->getClientsByIpid(target_ipid)) { + for (AOClient* client : server->getClientsByIpid(ban.ipid)) { if (!ban_logged) { - ip = client->remote_ip; - hdid = client->hwid; - server->db_manager->addBan(target_ipid, ip, hdid, time, reason, duration_seconds); - sendServerMessage("Banned user with ipid " + target_ipid + " for reason: " + reason); + ban.ip = client->remote_ip; + ban.hdid = client->hwid; + server->db_manager->addBan(ban); + sendServerMessage("Banned user with ipid " + ban.ipid + " for reason: " + ban.reason); ban_logged = true; } - client->sendPacket("KB", {reason}); + client->sendPacket("KB", {ban.reason + "\nID: " + QString::number(server->db_manager->getBanID(ban.ip)) + "\nUntil: " + QDateTime::fromSecsSinceEpoch(ban.time).addSecs(ban.duration).toString("dd.MM.yyyy, hh:mm")}); client->socket->close(); kick_counter++; } if (kick_counter > 1) - sendServerMessage("Kicked " + QString::number(kick_counter) + " clients with matching ipids"); + sendServerMessage("Kicked " + QString::number(kick_counter) + " clients with matching ipids."); if (!ban_logged) sendServerMessage("User with ipid not found!"); } diff --git a/src/db_manager.cpp b/src/db_manager.cpp index e203fcc..335e10b 100644 --- a/src/db_manager.cpp +++ b/src/db_manager.cpp @@ -122,6 +122,36 @@ long long DBManager::getBanDuration(QHostAddress ip) } } + +int DBManager::getBanID(QString hdid) +{ + QSqlQuery query; + query.prepare("SELECT ID FROM BANS WHERE HDID = ?"); + query.addBindValue(hdid); + query.exec(); + if (query.first()) { + return query.value(0).toInt(); + } + else { + return -1; + } +} + + +int DBManager::getBanID(QHostAddress ip) +{ + QSqlQuery query; + query.prepare("SELECT ID FROM BANS WHERE IP = ?"); + query.addBindValue(ip.toString()); + query.exec(); + if (query.first()) { + return query.value(0).toInt(); + } + else { + return -1; + } +} + QList DBManager::getRecentBans() { QList return_list; @@ -143,16 +173,16 @@ QList DBManager::getRecentBans() return return_list; } -void DBManager::addBan(QString ipid, QHostAddress ip, QString hdid, unsigned long time, QString reason, long long duration) +void DBManager::addBan(BanInfo ban) { QSqlQuery query; query.prepare("INSERT INTO BANS(IPID, HDID, IP, TIME, REASON, DURATION) VALUES(?, ?, ?, ?, ?, ?)"); - query.addBindValue(ipid); - query.addBindValue(hdid); - query.addBindValue(ip.toString()); - query.addBindValue(QString::number(time)); - query.addBindValue(reason); - query.addBindValue(duration); + query.addBindValue(ban.ipid); + query.addBindValue(ban.hdid); + query.addBindValue(ban.ip.toString()); + query.addBindValue(QString::number(ban.time)); + query.addBindValue(ban.reason); + query.addBindValue(ban.duration); if (!query.exec()) qDebug() << "SQL Error:" << query.lastError().text(); }