finish implementing kicks and bans

This commit is contained in:
scatterflower 2020-09-27 19:27:35 -05:00
parent 26cf130bd0
commit acc6966216
3 changed files with 57 additions and 23 deletions

View File

@ -75,7 +75,8 @@ class AOClient : public QObject {
{"login", {false, 1}}, {"login", {false, 1}},
{"getareas", {false, 0 }}, {"getareas", {false, 0 }},
{"getarea", {false, 0}}, {"getarea", {false, 0}},
{"ban", {true, 2}} {"ban", {true, 2}},
{"kick", {true, 2}}
}; };
void handlePacket(AOPacket packet); void handlePacket(AOPacket packet);

View File

@ -322,11 +322,11 @@ void AOClient::handleCommand(QString command, int argc, QStringList argv)
AreaData* area = server->areas[i]; AreaData* area = server->areas[i];
entries.append("=== " + area_name + " ==="); entries.append("=== " + area_name + " ===");
entries.append("[" + QString::number(area->player_count) + " users][" + area->status + "]"); entries.append("[" + QString::number(area->player_count) + " users][" + area->status + "]");
for (QString char_name : server->characters) { for (AOClient* client : server->clients) {
if (area->characters_taken.value(char_name)) { if (client->current_area == i) {
QString char_entry = char_name; QString char_entry = client->current_char;
if (authenticated) if (authenticated)
char_entry += " (" + getIpid() + "): " + ooc_name; char_entry += " (" + client->getIpid() + "): " + ooc_name;
entries.append(char_entry); entries.append(char_entry);
} }
} }
@ -340,42 +340,70 @@ void AOClient::handleCommand(QString command, int argc, QStringList argv)
AreaData* area = server->areas[current_area]; AreaData* area = server->areas[current_area];
entries.append("=== " + area_name + " ==="); entries.append("=== " + area_name + " ===");
entries.append("[" + QString::number(area->player_count) + " users][" + area->status + "]"); entries.append("[" + QString::number(area->player_count) + " users][" + area->status + "]");
for (QString char_name : server->characters) { for (AOClient* client : server->clients) {
if (area->characters_taken.value(char_name)) { if (client->current_area == current_area) {
QString char_entry = char_name; QString char_entry = client->current_char;
if (authenticated) if (authenticated)
char_entry += " (" + getIpid() + "): " + ooc_name; char_entry += " (" + client->getIpid() + "): " + ooc_name;
entries.append(char_entry); entries.append(char_entry);
} }
} }
sendServerMessage(entries.join("\n")); sendServerMessage(entries.join("\n"));
} }
else if (command == "ban") { else if (command == "ban") {
QString ipid = argv[0]; QString target_ipid = argv[0];
QHostAddress ip; QHostAddress ip;
QString hdid; QString hdid;
unsigned long time = QDateTime::currentDateTime().toTime_t(); unsigned long time = QDateTime::currentDateTime().toTime_t();
QString reason = argv[1]; QString reason = argv[1];
bool ban_logged = false;
if (argc > 2) { if (argc > 2) {
for (int i = 2; i < argv.length(); i++) { for (int i = 2; i < argv.length(); i++) {
reason += argv[i]; reason += " " + argv[i];
} }
} }
for (AOClient* client : server->clients) { for (AOClient* client : server->clients) {
if (client->getIpid() == ipid) { if (client->getIpid() == target_ipid) {
ip = client->remote_ip; if (!ban_logged) {
hdid = client->hwid; ip = client->remote_ip;
server->ban_manager->addBan(ipid, ip, hdid, time, reason); hdid = client->hwid;
sendServerMessage("Banned user with ipid " + ipid + " for reason: " + reason); server->ban_manager->addBan(target_ipid, ip, hdid, time, reason);
sendPacket("KB", {reason}); sendServerMessage("Banned user with ipid " + target_ipid + " for reason: " + reason);
socket->close(); ban_logged = true;
return; }
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!");
} }
} }

View File

@ -32,6 +32,7 @@ bool BanManager::isIPBanned(QHostAddress ip)
QSqlQuery query; QSqlQuery query;
query.prepare("SELECT ID FROM BANS WHERE IP = ?"); query.prepare("SELECT ID FROM BANS WHERE IP = ?");
query.addBindValue(ip.toString()); query.addBindValue(ip.toString());
query.exec();
return query.first(); return query.first();
} }
@ -40,14 +41,16 @@ bool BanManager::isHDIDBanned(QString hdid)
QSqlQuery query; QSqlQuery query;
query.prepare("SELECT ID FROM BANS WHERE HDID = ?"); query.prepare("SELECT ID FROM BANS WHERE HDID = ?");
query.addBindValue(hdid); query.addBindValue(hdid);
query.exec();
return query.first(); return query.first();
} }
QString BanManager::getBanReason(QHostAddress ip) QString BanManager::getBanReason(QHostAddress ip)
{ {
QSqlQuery query; QSqlQuery query;
query.prepare("SELECT ID FROM BANS WHERE IP = ?"); query.prepare("SELECT REASON FROM BANS WHERE IP = ?");
query.addBindValue(ip.toString()); query.addBindValue(ip.toString());
query.exec();
if (query.first()) { if (query.first()) {
return query.value(0).toString(); return query.value(0).toString();
} }
@ -59,8 +62,9 @@ QString BanManager::getBanReason(QHostAddress ip)
QString BanManager::getBanReason(QString hdid) QString BanManager::getBanReason(QString hdid)
{ {
QSqlQuery query; QSqlQuery query;
query.prepare("SELECT ID FROM BANS WHERE HDID = ?"); query.prepare("SELECT REASON FROM BANS WHERE HDID = ?");
query.addBindValue(hdid); query.addBindValue(hdid);
query.exec();
if (query.first()) { if (query.first()) {
return query.value(0).toString(); 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(ip.toString());
query.addBindValue(QString::number(time)); query.addBindValue(QString::number(time));
query.addBindValue(reason); query.addBindValue(reason);
query.exec(); if (!query.exec())
qDebug() << "SQL Error:" << query.lastError().text();
} }
BanManager::~BanManager() BanManager::~BanManager()