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}},
{"getareas", {false, 0 }},
{"getarea", {false, 0}},
{"ban", {true, 2}}
{"ban", {true, 2}},
{"kick", {true, 2}}
};
void handlePacket(AOPacket packet);

View File

@ -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!");
}
}

View File

@ -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()