From 2d793f4a9985db01bbfd7d28756c683523efa4d1 Mon Sep 17 00:00:00 2001 From: MangosArentLiterature <58055358+MangosArentLiterature@users.noreply.github.com> Date: Wed, 7 Apr 2021 15:26:14 -0500 Subject: [PATCH] Add /baninfo command - Adds getBanInfo() to db_manager, which queries bans by banid, hdid, or ipid --- include/aoclient.h | 13 +++++++++++++ include/db_manager.h | 9 +++++++++ src/commands.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/db_manager.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) diff --git a/include/aoclient.h b/include/aoclient.h index 423d2bb..8601207 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -1408,6 +1408,18 @@ class AOClient : public QObject { */ void cmdJudgeLog(int argc, QStringList argv); + /** + * @brief Looks up info on a ban. + * + * @details If it is called with **one argument**, that argument is the ban ID to look up. + * + * If it is called with **two arguments**, then the first argument is either a ban ID, an IPID, + * or an HDID, and the the second argument specifies the ID type. + * + * @iscommand + */ + void cmdBanInfo(int argc, QStringList argv); + ///@} /** @@ -1603,6 +1615,7 @@ class AOClient : public QObject { {"lm", {ACLFlags.value("MODCHAT"), 1, &AOClient::cmdLM}}, {"judgelog", {ACLFlags.value("CM"), 0, &AOClient::cmdJudgeLog}}, {"allow_blankposting", {ACLFlags.value("MODCHAT"), 0, &AOClient::cmdAllow_Blankposting}}, + {"baninfo", {ACLFlags.value("BAN"), 1, &AOClient::cmdBanInfo}}, }; /** diff --git a/include/db_manager.h b/include/db_manager.h index 575df07..b696dfe 100644 --- a/include/db_manager.h +++ b/include/db_manager.h @@ -239,6 +239,15 @@ public: */ QStringList getUsers(); + /** + * @brief Gets information on a ban. + * + * @param lookup_type The type of ID to search + * + * @param id A Ban ID, IPID, or HDID to search for + */ + QList getBanInfo(QString lookup_type, QString id); + private: /** * @brief The name of the database connection driver. diff --git a/src/commands.cpp b/src/commands.cpp index 7bbd543..c336026 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -1311,6 +1311,45 @@ void AOClient::cmdAllow_Blankposting(int argc, QStringList argv) } } +void AOClient::cmdBanInfo(int argc, QStringList argv) +{ + QStringList ban_info; + ban_info << ("Ban Info for " + argv[0]); + ban_info << "-----"; + QString lookup_type; + + if (argc == 1) { + lookup_type = "banid"; + } + else if (argc == 2) { + lookup_type = argv[1]; + if (!((lookup_type == "banid") || (lookup_type == "ipid") || (lookup_type == "hdid"))) { + sendServerMessage("Invalid ID type."); + return; + } + } + else { + sendServerMessage("Invalid command."); + return; + } + QString id = argv[0]; + for (DBManager::BanInfo ban : server->db_manager->getBanInfo(lookup_type, id)) { + QString banned_until; + if (ban.duration == -2) + banned_until = "The heat death of the universe"; + else + banned_until = QDateTime::fromSecsSinceEpoch(ban.time).addSecs(ban.duration).toString("dd.MM.yyyy, hh:mm"); + ban_info << "Affected IPID: " + ban.ipid; + ban_info << "Affected HDID: " + ban.hdid; + ban_info << "Reason for ban: " + ban.reason; + ban_info << "Date of ban: " + QDateTime::fromSecsSinceEpoch(ban.time).toString("dd.MM.yyyy, hh:mm"); + ban_info << "Ban lasts until: " + banned_until; + ban_info << "-----"; + } + sendServerMessage(ban_info.join("\n")); +} + + QStringList AOClient::buildAreaList(int area_idx) { QStringList entries; diff --git a/src/db_manager.cpp b/src/db_manager.cpp index 217ca25..2aeb12e 100644 --- a/src/db_manager.cpp +++ b/src/db_manager.cpp @@ -325,6 +325,47 @@ QStringList DBManager::getUsers() return users; } +QList DBManager::getBanInfo(QString lookup_type, QString id) +{ + QList return_list; + QSqlQuery query; + QList invalid; + if (lookup_type == "banid") { + query.prepare("SELECT * FROM BANS WHERE ID = ?"); + query.addBindValue(id); + query.setForwardOnly(true); + query.exec(); + } + else if (lookup_type == "hdid") { + query.prepare("SELECT * FROM BANS WHERE HDID = ?"); + query.addBindValue(id); + query.setForwardOnly(true); + query.exec(); + } + else if (lookup_type == "ipid") { + query.prepare("SELECT * FROM BANS WHERE IPID = ?"); + query.addBindValue(id); + query.setForwardOnly(true); + query.exec(); + } + else { + qCritical("Invalid ban lookup type!"); + return invalid; + } + while (query.next()) { + BanInfo ban; + ban.ipid = query.value(0).toString(); + ban.hdid = query.value(1).toString(); + ban.ip = QHostAddress(query.value(2).toString()); + ban.time = static_cast(query.value(3).toULongLong()); + ban.reason = query.value(4).toString(); + ban.duration = query.value(5).toLongLong(); + return_list.append(ban); + } + std::reverse(return_list.begin(), return_list.end()); + return return_list; +} + DBManager::~DBManager() { db.close();