From acc69662162c39588b561e903b4799d79d20b73e Mon Sep 17 00:00:00 2001 From: scatterflower Date: Sun, 27 Sep 2020 19:27:35 -0500 Subject: [PATCH] finish implementing kicks and bans --- include/aoclient.h | 3 ++- src/aoclient.cpp | 66 ++++++++++++++++++++++++++++++++------------- src/ban_manager.cpp | 11 +++++--- 3 files changed, 57 insertions(+), 23 deletions(-) diff --git a/include/aoclient.h b/include/aoclient.h index 0f48c30..f5a73cc 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -75,7 +75,8 @@ class AOClient : public QObject { {"login", {false, 1}}, {"getareas", {false, 0 }}, {"getarea", {false, 0}}, - {"ban", {true, 2}} + {"ban", {true, 2}}, + {"kick", {true, 2}} }; void handlePacket(AOPacket packet); diff --git a/src/aoclient.cpp b/src/aoclient.cpp index acb2f4a..90d0204 100644 --- a/src/aoclient.cpp +++ b/src/aoclient.cpp @@ -322,11 +322,11 @@ void AOClient::handleCommand(QString command, int argc, QStringList argv) AreaData* area = server->areas[i]; entries.append("=== " + area_name + " ==="); entries.append("[" + QString::number(area->player_count) + " users][" + area->status + "]"); - for (QString char_name : server->characters) { - if (area->characters_taken.value(char_name)) { - QString char_entry = char_name; + for (AOClient* client : server->clients) { + if (client->current_area == i) { + QString char_entry = client->current_char; if (authenticated) - char_entry += " (" + getIpid() + "): " + ooc_name; + char_entry += " (" + client->getIpid() + "): " + ooc_name; entries.append(char_entry); } } @@ -340,42 +340,70 @@ void AOClient::handleCommand(QString command, int argc, QStringList argv) AreaData* area = server->areas[current_area]; entries.append("=== " + area_name + " ==="); entries.append("[" + QString::number(area->player_count) + " users][" + area->status + "]"); - for (QString char_name : server->characters) { - if (area->characters_taken.value(char_name)) { - QString char_entry = char_name; + for (AOClient* client : server->clients) { + if (client->current_area == current_area) { + QString char_entry = client->current_char; if (authenticated) - char_entry += " (" + getIpid() + "): " + ooc_name; + char_entry += " (" + client->getIpid() + "): " + ooc_name; entries.append(char_entry); } } sendServerMessage(entries.join("\n")); } else if (command == "ban") { - QString ipid = argv[0]; + QString target_ipid = argv[0]; QHostAddress ip; QString hdid; unsigned long time = QDateTime::currentDateTime().toTime_t(); QString reason = argv[1]; + bool ban_logged = false; if (argc > 2) { for (int i = 2; i < argv.length(); i++) { - reason += argv[i]; + reason += " " + argv[i]; } } for (AOClient* client : server->clients) { - if (client->getIpid() == ipid) { - ip = client->remote_ip; - hdid = client->hwid; - server->ban_manager->addBan(ipid, ip, hdid, time, reason); - sendServerMessage("Banned user with ipid " + ipid + " for reason: " + reason); - sendPacket("KB", {reason}); - socket->close(); - return; + if (client->getIpid() == target_ipid) { + if (!ban_logged) { + ip = client->remote_ip; + hdid = client->hwid; + server->ban_manager->addBan(target_ipid, ip, hdid, time, reason); + sendServerMessage("Banned user with ipid " + target_ipid + " for reason: " + reason); + ban_logged = true; + } + client->sendPacket("KB", {reason}); + client->socket->close(); } } - sendServerMessage("User with ipid not found!"); + if (!ban_logged) + sendServerMessage("User with ipid not found!"); + } + else if (command == "kick") { + QString target_ipid = argv[0]; + QString reason = argv[1]; + bool did_kick = false; + + if (argc > 2) { + for (int i = 2; i < argv.length(); i++) { + reason += " " + argv[i]; + } + } + + for (AOClient* client : server->clients) { + if (client->getIpid() == target_ipid) { + client->sendPacket("KK", {reason}); + client->socket->close(); + did_kick = true; + } + } + + if (did_kick) + sendServerMessage("Banned user with ipid " + target_ipid + " for reason: " + reason); + else + sendServerMessage("User with ipid not found!"); } } diff --git a/src/ban_manager.cpp b/src/ban_manager.cpp index 4368012..a18a2a7 100644 --- a/src/ban_manager.cpp +++ b/src/ban_manager.cpp @@ -32,6 +32,7 @@ bool BanManager::isIPBanned(QHostAddress ip) QSqlQuery query; query.prepare("SELECT ID FROM BANS WHERE IP = ?"); query.addBindValue(ip.toString()); + query.exec(); return query.first(); } @@ -40,14 +41,16 @@ bool BanManager::isHDIDBanned(QString hdid) QSqlQuery query; query.prepare("SELECT ID FROM BANS WHERE HDID = ?"); query.addBindValue(hdid); + query.exec(); return query.first(); } QString BanManager::getBanReason(QHostAddress ip) { QSqlQuery query; - query.prepare("SELECT ID FROM BANS WHERE IP = ?"); + query.prepare("SELECT REASON FROM BANS WHERE IP = ?"); query.addBindValue(ip.toString()); + query.exec(); if (query.first()) { return query.value(0).toString(); } @@ -59,8 +62,9 @@ QString BanManager::getBanReason(QHostAddress ip) QString BanManager::getBanReason(QString hdid) { QSqlQuery query; - query.prepare("SELECT ID FROM BANS WHERE HDID = ?"); + query.prepare("SELECT REASON FROM BANS WHERE HDID = ?"); query.addBindValue(hdid); + query.exec(); if (query.first()) { return query.value(0).toString(); } @@ -78,7 +82,8 @@ void BanManager::addBan(QString ipid, QHostAddress ip, QString hdid, unsigned lo query.addBindValue(ip.toString()); query.addBindValue(QString::number(time)); query.addBindValue(reason); - query.exec(); + if (!query.exec()) + qDebug() << "SQL Error:" << query.lastError().text(); } BanManager::~BanManager()