use BanInfo for inserting bans, send ban id and duration with KB packet

This commit is contained in:
in1tiate 2021-03-14 13:28:59 -05:00
parent b5779b1aa5
commit b562b77be9
3 changed files with 56 additions and 23 deletions

View File

@ -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<BanInfo> 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);

View File

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

View File

@ -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::BanInfo> DBManager::getRecentBans()
{
QList<BanInfo> return_list;
@ -143,16 +173,16 @@ QList<DBManager::BanInfo> 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();
}