From 79254d6adc56ebee54e0b08015febfffc4dc1ed1 Mon Sep 17 00:00:00 2001 From: Jared Dunbar Date: Tue, 28 Dec 2021 23:00:12 -0500 Subject: [PATCH] Various fixes, bump to 1.1: * No more JSON injection * Threads used for config loading and webhook sending * More useful error messages for users * Corrected some issues in the readme. * Version bump to 1.1 --- README.md | 4 +-- build.gradle | 2 +- .../java/org/ageseries/chatage/ChatAge.kt | 8 ++--- .../org/ageseries/chatage/ChatAgeConfig.kt | 35 ++++++++++++------- .../org/ageseries/chatage/DiscordWebhook.kt | 23 ++++++------ 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 7078214..3fbef24 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,14 @@ This is Chat Age. It is a Minecraft Mod for 1.18.1 -You put a Discord webhook into the config file and it mirrors your Minecraft +You put a Discord webhook into the config file, and it mirrors your Minecraft chat messages into your Discord chat channel. Pretty simple and effective. This mod depends on Kotlin for Forge to be installed. ## Config File -See `config/chat_age.yaml` for configuration options. A blank config will be +See `config/chat_age.json` for configuration options. A blank config will be generated on first start of the server for you. ```json diff --git a/build.gradle b/build.gradle index f396def..70b2024 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ apply from: 'https://raw.githubusercontent.com/thedarkcolour/KotlinForForge/site apply plugin: 'eclipse' apply plugin: 'maven-publish' -version = '1.0' +version = '1.1' group = 'org.ageseries.chatage' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'chatage' diff --git a/src/main/java/org/ageseries/chatage/ChatAge.kt b/src/main/java/org/ageseries/chatage/ChatAge.kt index 1c5c4b1..bb6e029 100644 --- a/src/main/java/org/ageseries/chatage/ChatAge.kt +++ b/src/main/java/org/ageseries/chatage/ChatAge.kt @@ -9,25 +9,21 @@ import net.minecraftforge.network.NetworkConstants.IGNORESERVERONLY import thedarkcolour.kotlinforforge.forge.DIST import thedarkcolour.kotlinforforge.forge.LOADING_CONTEXT import java.util.function.Supplier +import kotlin.concurrent.thread @Mod(ChatAge.MODID) object ChatAge { const val MODID: String = "chatage" private fun chatEvent(event: ServerChatEvent) { - LOGGER.info(event.player) - LOGGER.info(event.username) - LOGGER.info(event.message) DiscordWebhook.discordWebhook(event.message, event.username) } private fun playerLogsIn(event: PlayerEvent.PlayerLoggedInEvent) { - LOGGER.info("Player ${event.player.name.contents} joined") DiscordWebhook.discordWebhook("Player ${event.player.name.contents} joined") } private fun playerLogsOut(event: PlayerEvent.PlayerLoggedOutEvent) { - LOGGER.info("Player ${event.player.name.contents} left") DiscordWebhook.discordWebhook("Player ${event.player.name.contents} left") } @@ -37,7 +33,7 @@ object ChatAge { EVENT_BUS.addListener {event: PlayerEvent.PlayerLoggedInEvent -> playerLogsIn(event)} EVENT_BUS.addListener {event: PlayerEvent.PlayerLoggedOutEvent -> playerLogsOut(event)} EVENT_BUS.register(this) - ChatAgeConfig.loadConfig() + thread (name = "CA Config Loader Thread"){ ChatAgeConfig.loadConfig() } val extension = Supplier( fun(): IExtensionPoint.DisplayTest = IExtensionPoint.DisplayTest( diff --git a/src/main/java/org/ageseries/chatage/ChatAgeConfig.kt b/src/main/java/org/ageseries/chatage/ChatAgeConfig.kt index 0b0ce34..737d204 100644 --- a/src/main/java/org/ageseries/chatage/ChatAgeConfig.kt +++ b/src/main/java/org/ageseries/chatage/ChatAgeConfig.kt @@ -9,24 +9,33 @@ object ChatAgeConfig { var config: ChatAgeData = ChatAgeData() fun loadConfig(configFile: File = CONFIG_FILE) { - if(configFile.isFile) { - LOGGER.info("Reading config from ${configFile.absoluteFile}") - config = Json.decodeFromString(ChatAgeData.serializer(), configFile.readLines().joinToString("\n")) - //config = Yaml.default.decodeFromStream(ChatAgeData.serializer(), configFile.inputStream()) - } else { - config = ChatAgeData() - saveConfig() + try { + if (configFile.isFile) { + LOGGER.info("[Chat Age] Reading config from ${configFile.absoluteFile}") + config = Json.decodeFromString(ChatAgeData.serializer(), configFile.readLines().joinToString("\n")) + //config = Yaml.default.decodeFromStream(ChatAgeData.serializer(), configFile.inputStream()) + } else { + config = ChatAgeData() + saveConfig() + } + } catch (e: Exception) { + LOGGER.error("Chat Age had an issue with loading the configuration file, please check the file for errors.") + LOGGER.error("Check that 1) You have valid JSON 2) the config directives are spelled correctly (see documentation)") } } private fun saveConfig(configFile: File = CONFIG_FILE) { - LOGGER.info("Writing config to ${configFile.absoluteFile}") - val configText = Json.encodeToString(ChatAgeData.serializer(), config) - //val configText = Yaml.default.encodeToString(ChatAgeData.serializer(), config) - if (!configFile.exists()) { - configFile.createNewFile() + try { + LOGGER.info("[Chat Age] Writing config to ${configFile.absoluteFile}") + val configText = Json.encodeToString(ChatAgeData.serializer(), config) + //val configText = Yaml.default.encodeToString(ChatAgeData.serializer(), config) + if (!configFile.exists()) { + configFile.createNewFile() + } + configFile.writeText(configText) + } catch (e: Exception) { + LOGGER.error("Chat Age was unable to write back the config file, please check filesystem permissions: $e") } - configFile.writeText(configText) } } diff --git a/src/main/java/org/ageseries/chatage/DiscordWebhook.kt b/src/main/java/org/ageseries/chatage/DiscordWebhook.kt index 7ee1f19..7ebf72a 100644 --- a/src/main/java/org/ageseries/chatage/DiscordWebhook.kt +++ b/src/main/java/org/ageseries/chatage/DiscordWebhook.kt @@ -1,6 +1,7 @@ package org.ageseries.chatage import java.io.File +import kotlin.concurrent.thread object DiscordWebhook { @@ -48,21 +49,23 @@ object DiscordWebhook { } */ - fun discordWebhook(message: String, username: String? = ChatAgeConfig.config.botName) { - val json = """ + fun discordWebhook(message: String, username: String = ChatAgeConfig.config.botName) { + thread (name = "CA Discord Webhook Thread") { + val json = """ { - "username": "$username", - "content": "$message" + "username": "${username.replace("\"", "")}", + "content": "${message.replace("\"", "")}" } """.trimIndent() - // Currently, not used because I need to come up with a logo to use and a CDN to provide it from. - // 'avatar_url': '${avatar_url}', + // Currently, not used because I need to come up with a logo to use and a CDN to provide it from. + // 'avatar_url': '${avatar_url}', - if (ChatAgeConfig.config.webhookUrl.isNotBlank()) - httpsPost(ChatAgeConfig.config.webhookUrl, json) - else { - LOGGER.info("Error! Webhook URL must be defined in the config!") + if (ChatAgeConfig.config.webhookUrl.isNotBlank()) + httpsPost(ChatAgeConfig.config.webhookUrl, json) + else { + LOGGER.error("[Chat Age] Webhook URL must be defined in the config.") + } } } }