From 2720d909f6f41afdfbdd572f3a4550464af8d479 Mon Sep 17 00:00:00 2001
From: AwesomeAim <30537683+AwesomeAim@users.noreply.github.com>
Date: Fri, 5 May 2023 10:04:24 -0700
Subject: [PATCH] change ban checks to return full BanInfo and reorder
 db_manager to allow for it

---
 core/include/db_manager.h | 36 ++++++++++++-------------
 core/src/db_manager.cpp   | 56 ++++++++++++++++++++++++---------------
 2 files changed, 52 insertions(+), 40 deletions(-)

diff --git a/core/include/db_manager.h b/core/include/db_manager.h
index ca29a8f..117d236 100644
--- a/core/include/db_manager.h
+++ b/core/include/db_manager.h
@@ -59,6 +59,21 @@ class DBManager : public QObject
      */
     ~DBManager();
 
+    /**
+     * @brief Details about a ban.
+     */
+    struct BanInfo
+    {
+        QString ipid;       //!< The banned user's IPID.
+        QHostAddress ip;    //!< The banned user's IP.
+        QString hdid;       //!< The banned user's hardware ID.
+        unsigned long time; //!< The time the ban was registered.
+        QString reason;     //!< The reason given for the ban by the moderator who registered it.
+        long long duration; //!< The duration of the ban, in seconds.
+        int id;             //!< The unique ID of the ban.
+        QString moderator;  //!< The moderator who issued the ban.
+    };
+
     /**
      * @brief Checks if there is a record in the Bans table with the given IPID.
      *
@@ -68,7 +83,7 @@ class DBManager : public QObject
      * * First, a `bool` that is true if the query could return at least one such record.
      * * Then, a `QString` that is the reason for the ban.
      */
-    QPair<bool, QString> isIPBanned(QString ipid);
+    QPair<bool, BanInfo> isIPBanned(QString ipid);
 
     /**
      * @brief Checks if there is a record in the Bans table with the given hardware ID.
@@ -79,7 +94,7 @@ class DBManager : public QObject
      * * First, a `bool` that is true if the query could return at least one such record.
      * * Then, a `QString` that is the reason for the ban.
      */
-    QPair<bool, QString> isHDIDBanned(QString hdid);
+    QPair<bool, BanInfo> isHDIDBanned(QString hdid);
 
     /**
      * @brief Gets the ID number of a given ban.
@@ -96,21 +111,6 @@ class DBManager : public QObject
      */
     int getBanID(QString hdid);
 
-    /**
-     * @brief Details about a ban.
-     */
-    struct BanInfo
-    {
-        QString ipid;       //!< The banned user's IPID.
-        QHostAddress ip;    //!< The banned user's IP.
-        QString hdid;       //!< The banned user's hardware ID.
-        unsigned long time; //!< The time the ban was registered.
-        QString reason;     //!< The reason given for the ban by the moderator who registered it.
-        long long duration; //!< The duration of the ban, in seconds.
-        int id;             //!< The unique ID of the ban.
-        QString moderator;  //!< The moderator who issued the ban.
-    };
-
     /**
      * @brief Gets the last five bans made on the server.
      *
@@ -274,4 +274,4 @@ class DBManager : public QObject
     void updateDB(int current_version);
 };
 
-#endif // BAN_MANAGER_H
\ No newline at end of file
+#endif // BAN_MANAGER_H
diff --git a/core/src/db_manager.cpp b/core/src/db_manager.cpp
index 9b2b11d..63d7f7b 100644
--- a/core/src/db_manager.cpp
+++ b/core/src/db_manager.cpp
@@ -44,48 +44,60 @@ DBManager::DBManager() :
         updateDB(db_version);
 }
 
-QPair<bool, QString> DBManager::isIPBanned(QString ipid)
+QPair<bool, DBManager::BanInfo> DBManager::isIPBanned(QString ipid)
 {
     QSqlQuery query;
-    query.prepare("SELECT TIME,REASON,DURATION FROM BANS WHERE IPID = ? ORDER BY TIME DESC");
+    query.prepare("SELECT * FROM BANS WHERE IPID = ? ORDER BY TIME DESC");
     query.addBindValue(ipid);
     query.exec();
+    BanInfo ban;
     if (query.first()) {
-        long long ban_time = query.value(0).toLongLong();
-        QString reason = query.value(1).toString();
-        long long duration = query.value(2).toLongLong();
-        if (duration == -2)
-            return {true, reason};
+        ban.id = query.value(0).toInt();
+        ban.ipid = query.value(1).toString();
+        ban.hdid = query.value(2).toString();
+        ban.ip = QHostAddress(query.value(3).toString());
+        ban.time = static_cast<unsigned long>(query.value(4).toULongLong());
+        ban.reason = query.value(5).toString();
+        ban.duration = query.value(6).toLongLong();
+        ban.moderator = query.value(7).toString();
+        if (ban.duration == -2)
+            return {true, ban};
         long long current_time = QDateTime::currentDateTime().toSecsSinceEpoch();
-        if (ban_time + duration > current_time)
-            return {true, reason};
+        if (ban.time + ban.duration > current_time)
+            return {true, ban};
         else
-            return {false, nullptr};
+            return {false, ban};
     }
     else
-        return {false, nullptr};
+        return {false, ban};
 }
 
-QPair<bool, QString> DBManager::isHDIDBanned(QString hdid)
+QPair<bool, DBManager::BanInfo> DBManager::isHDIDBanned(QString hdid)
 {
     QSqlQuery query;
-    query.prepare("SELECT TIME,REASON,DURATION FROM BANS WHERE HDID = ? ORDER BY TIME DESC");
+    query.prepare("SELECT * FROM BANS WHERE HDID = ? ORDER BY TIME DESC");
     query.addBindValue(hdid);
     query.exec();
+    BanInfo ban;
     if (query.first()) {
-        long long ban_time = query.value(0).toLongLong();
-        QString reason = query.value(1).toString();
-        long long duration = query.value(2).toLongLong();
-        if (duration == -2)
-            return {true, reason};
+        ban.id = query.value(0).toInt();
+        ban.ipid = query.value(1).toString();
+        ban.hdid = query.value(2).toString();
+        ban.ip = QHostAddress(query.value(3).toString());
+        ban.time = static_cast<unsigned long>(query.value(4).toULongLong());
+        ban.reason = query.value(5).toString();
+        ban.duration = query.value(6).toLongLong();
+        ban.moderator = query.value(7).toString();
+        if (ban.duration == -2)
+            return {true, ban};
         long long current_time = QDateTime::currentDateTime().toSecsSinceEpoch();
-        if (ban_time + duration > current_time)
-            return {true, reason};
+        if (ban.time + ban.duration > current_time)
+            return {true, ban};
         else
-            return {false, nullptr};
+            return {false, ban};
     }
     else
-        return {false, nullptr};
+        return {false, ban};
 }
 
 int DBManager::getBanID(QString hdid)