diff --git a/bin/config_sample/config.ini b/bin/config_sample/config.ini index 767a902..863379d 100644 --- a/bin/config_sample/config.ini +++ b/bin/config_sample/config.ini @@ -94,6 +94,9 @@ webhook_sendfile=false ; Additional text to send with the webhook message. Usually for adding tags for role. Ensure the format is <@&[RoleID]>. webhook_content= +; Enables the ban webhook. +webhook_ban_enabled = false + [Password] ; Whether or not to enforce password requirements. Only applicable under advanced authorization. password_requirements = true diff --git a/core/include/config_manager.h b/core/include/config_manager.h index c661ef3..3364c0d 100644 --- a/core/include/config_manager.h +++ b/core/include/config_manager.h @@ -218,6 +218,13 @@ class ConfigManager { */ static bool discordWebhookSendFile(); + /** + * @brief Returns true if the discord ban webhook is enabled. + * + * @return See short description. + */ + static bool discordBanWebhookEnabled(); + /** * @brief Returns true if password requirements should be enforced. * diff --git a/core/include/discord.h b/core/include/discord.h index b9150e8..dc39e6f 100644 --- a/core/include/discord.h +++ b/core/include/discord.h @@ -71,6 +71,18 @@ public: */ QJsonDocument constructModcallJson(const QString& f_name, const QString& f_area, const QString& f_reason) const; + /** + * @brief Constructs a new JSON document for bans. + * + * @param f_ipid The IPID of the client. + * @param f_moderator The name of the moderator banning. + * @param f_duration The date the ban expires. + * @param f_reason The reason of the ban. + * + * @return A JSON document for the ban. + */ + QJsonDocument constructBanJson(const QString& f_ipid, const QString& f_moderator, const QString& f_duration, const QString& f_reason, const int& f_banID); + /** * @brief Constructs a new QHttpMultiPart document for log files. * @@ -91,6 +103,16 @@ public slots: */ void onModcallWebhookRequested(const QString& f_name, const QString& f_area, const QString& f_reason, const QQueue& f_buffer); + /** + * @brief Handles a ban webhook request. + * + * @param f_ipid The IPID of the client. + * @param f_moderator The name of the moderator banning. + * @param f_duration The date the ban expires. + * @param f_reason The reason of the ban. + */ + void onBanWebhookRequested(const QString& f_ipid, const QString& f_moderator, const QString& f_duration, const QString& f_reason, const int& f_banID); + private: /** * @brief The QNetworkAccessManager for webhooks. diff --git a/core/include/server.h b/core/include/server.h index 3456b64..f783687 100644 --- a/core/include/server.h +++ b/core/include/server.h @@ -249,6 +249,9 @@ class Server : public QObject { */ void modcallWebhookRequest(const QString& f_name, const QString& f_area, const QString& f_reason, const QQueue& f_buffer); + + void banWebhookRequest(const QString& f_ipid, const QString& f_moderator, const QString& f_duration, const QString& f_reason, const int& f_banID); + private: /** * @brief The proxy used for WebSocket connections. diff --git a/core/src/commands/moderation.cpp b/core/src/commands/moderation.cpp index e7e4d46..bb3df47 100644 --- a/core/src/commands/moderation.cpp +++ b/core/src/commands/moderation.cpp @@ -72,9 +72,13 @@ void AOClient::cmdBan(int argc, QStringList argv) else { ban_duration = "The heat death of the universe."; } - client->sendPacket("KB", {ban.reason + "\nID: " + QString::number(server->db_manager->getBanID(ban.ip)) + "\nUntil: " + ban_duration}); + int ban_id = server->db_manager->getBanID(ban.ip); + client->sendPacket("KB", {ban.reason + "\nID: " + QString::number(ban_id) + "\nUntil: " + ban_duration}); client->socket->close(); kick_counter++; + + if (ConfigManager::discordBanWebhookEnabled()) + emit server->banWebhookRequest(ban.ipid, ban.moderator, ban_duration, ban.reason, ban_id); } if (kick_counter > 1) diff --git a/core/src/config_manager.cpp b/core/src/config_manager.cpp index 9f15f08..f54e805 100644 --- a/core/src/config_manager.cpp +++ b/core/src/config_manager.cpp @@ -285,6 +285,11 @@ bool ConfigManager::discordWebhookSendFile() return m_settings->value("Discord/webhook_sendfile", false).toBool(); } +bool ConfigManager::discordBanWebhookEnabled() +{ + return m_settings->value("Discord/webhook_ban_enabled", false).toBool(); +} + bool ConfigManager::passwordRequirements() { return m_settings->value("Password/password_requirements", true).toBool(); diff --git a/core/src/discord.cpp b/core/src/discord.cpp index 0cecaea..0b4693c 100644 --- a/core/src/discord.cpp +++ b/core/src/discord.cpp @@ -39,6 +39,12 @@ void Discord::onModcallWebhookRequested(const QString &f_name, const QString &f_ } } +void Discord::onBanWebhookRequested(const QString &f_ipid, const QString &f_moderator, const QString &f_duration, const QString &f_reason, const int &f_banID) +{ + QJsonDocument l_json = constructBanJson(f_ipid,f_moderator, f_duration, f_reason, f_banID); + postJsonWebhook(l_json); +} + QJsonDocument Discord::constructModcallJson(const QString &f_name, const QString &f_area, const QString &f_reason) const { QJsonObject l_json; @@ -56,6 +62,21 @@ QJsonDocument Discord::constructModcallJson(const QString &f_name, const QString return QJsonDocument(l_json); } +QJsonDocument Discord::constructBanJson(const QString &f_ipid, const QString &f_moderator, const QString &f_duration, const QString &f_reason, const int &f_banID) +{ + QJsonObject l_json; + QJsonArray l_array; + QJsonObject l_object { + {"color", "13312842"}, + {"title", "Ban issued by " + f_moderator}, + {"description", "Client IPID : " + f_ipid + "\nBan ID: " + QString::number(f_banID) + "\nBan reason : " + f_reason +"\nBanned until : " +f_duration} + }; + l_array.append(l_object); + l_json["embeds"] = l_array; + + return QJsonDocument(l_json); +} + QHttpMultiPart* Discord::constructLogMultipart(const QQueue &f_buffer) const { QHttpMultiPart* l_multipart = new QHttpMultiPart(); diff --git a/core/src/server.cpp b/core/src/server.cpp index 184e031..e187770 100644 --- a/core/src/server.cpp +++ b/core/src/server.cpp @@ -56,6 +56,8 @@ void Server::start() discord = new Discord(this); connect(this, &Server::modcallWebhookRequest, discord, &Discord::onModcallWebhookRequested); + connect(this, &Server::banWebhookRequest, + discord, &Discord::onBanWebhookRequested); } if (ConfigManager::advertiseHTTPServer()) {